}
if (njs_strstr_eq(&kty, &njs_str_value("RSA"))) {
+ if (alg->type != NJS_ALGORITHM_RSASSA_PKCS1_v1_5
+ && alg->type != NJS_ALGORITHM_RSA_PSS
+ && alg->type != NJS_ALGORITHM_RSA_OAEP)
+ {
+ njs_vm_type_error(vm, "JWK kty \"RSA\" doesn't match "
+ "algorithm \"%V\"",
+ njs_algorithm_string(alg));
+ goto fail;
+ }
+
pkey = njs_import_jwk_rsa(vm, jwk, key);
if (njs_slow_path(pkey == NULL)) {
goto fail;
}
} else if (njs_strstr_eq(&kty, &njs_str_value("EC"))) {
+ if (alg->type != NJS_ALGORITHM_ECDSA
+ && alg->type != NJS_ALGORITHM_ECDH)
+ {
+ njs_vm_type_error(vm, "JWK kty \"EC\" doesn't match "
+ "algorithm \"%V\"",
+ njs_algorithm_string(alg));
+ goto fail;
+ }
+
ret = njs_algorithm_curve(vm, options, &key->u.a.curve);
if (njs_slow_path(ret == NJS_ERROR)) {
goto fail;
}
} else if (njs_strstr_eq(&kty, &njs_str_value("oct"))) {
+ if (!alg->raw) {
+ njs_vm_type_error(vm, "JWK kty \"oct\" doesn't match "
+ "algorithm \"%V\"",
+ njs_algorithm_string(alg));
+ goto fail;
+ }
+
ret = njs_import_jwk_oct(vm, jwk, key);
if (njs_slow_path(ret != NJS_OK)) {
goto fail;
njs_webcrypto_key_t *key = data;
if (!key->alg->raw) {
- EVP_PKEY_free(key->u.a.pkey);
+ if (key->u.a.pkey != NULL) {
+ EVP_PKEY_free(key->u.a.pkey);
+ }
+
}
}
switch (kty) {
case QJS_KEY_JWK_KTY_RSA:
+ if (alg->type != QJS_ALGORITHM_RSASSA_PKCS1_v1_5
+ && alg->type != QJS_ALGORITHM_RSA_PSS
+ && alg->type != QJS_ALGORITHM_RSA_OAEP)
+ {
+ JS_ThrowTypeError(cx, "JWK kty \"RSA\" doesn't "
+ "match algorithm \"%s\"",
+ qjs_algorithm_string(alg));
+ goto fail;
+ }
+
pkey = qjs_import_jwk_rsa(cx, jwk, wkey);
if (pkey == NULL) {
goto fail;
break;
case QJS_KEY_JWK_KTY_EC:
+ if (alg->type != QJS_ALGORITHM_ECDSA
+ && alg->type != QJS_ALGORITHM_ECDH)
+ {
+ JS_ThrowTypeError(cx, "JWK kty \"EC\" doesn't "
+ "match algorithm \"%s\"",
+ qjs_algorithm_string(alg));
+ goto fail;
+ }
+
ret = qjs_algorithm_curve(cx, options, &wkey->u.a.curve);
if (JS_IsException(ret)) {
goto fail;
case QJS_KEY_JWK_KTY_OCT:
default:
+ if (!alg->raw) {
+ JS_ThrowTypeError(cx, "JWK kty \"oct\" doesn't "
+ "match algorithm \"%s\"",
+ qjs_algorithm_string(alg));
+ goto fail;
+ }
+
ret = qjs_import_jwk_oct(cx, jwk, wkey);
if (JS_IsException(ret)) {
goto fail;
alg: { name: "AES-CBC" },
usage: [ "encrypt", "decrypt" ] },
exception: "TypeError: Invalid JWK oct alg" },
+ { key: { fmt: "jwk",
+ key: { kty: 'RSA' },
+ alg: { name: "AES-CBC" },
+ extractable: true,
+ usage: [ "encrypt" ] },
+ exception: "TypeError: JWK kty \"RSA\" doesn't match algorithm \"AES-CBC\"" },
+ { key: { fmt: "jwk",
+ key: { kty: 'EC' },
+ alg: { name: "AES-GCM" },
+ extractable: true,
+ usage: [ "encrypt" ] },
+ exception: "TypeError: JWK kty \"EC\" doesn't match algorithm \"AES-GCM\"" },
]};
let ec_tsuite = {
extractable: true,
usage: [ "verify" ] },
exception: "TypeError: EC_POINT_oct2point()" },
+ { key: { fmt: "jwk",
+ key: { kty: 'oct', k: 'AQIDBAUG', alg: 'HS256' },
+ alg: { name: "ECDSA", namedCurve: "P-256" },
+ extractable: true,
+ usage: [ "sign" ] },
+ exception: "TypeError: JWK kty \"oct\" doesn't match algorithm \"ECDSA\"" },
+ { key: { fmt: "jwk",
+ key: { kty: 'oct', k: 'AQIDBAUG', alg: 'HS256' },
+ alg: { name: "ECDH", namedCurve: "P-256" },
+ extractable: true,
+ usage: [ "deriveBits" ] },
+ exception: "TypeError: JWK kty \"oct\" doesn't match algorithm \"ECDH\"" },
+ { key: { fmt: "jwk",
+ key: { kty: 'RSA' },
+ alg: { name: "ECDSA", namedCurve: "P-256" },
+ extractable: true,
+ usage: [ "sign" ] },
+ exception: "TypeError: JWK kty \"RSA\" doesn't match algorithm \"ECDSA\"" },
]};
let hmac_tsuite = {
extractable: true,
usage: [ "verify" ]},
exception: "TypeError: Key operations and usage mismatch" },
+ { key: { fmt: "jwk",
+ key: { kty: 'RSA' },
+ alg: { name: "HMAC", hash: "SHA-256" },
+ extractable: true,
+ usage: [ "sign" ] },
+ exception: "TypeError: JWK kty \"RSA\" doesn't match algorithm \"HMAC\"" },
+ { key: { fmt: "jwk",
+ key: { kty: 'EC' },
+ alg: { name: "HMAC", hash: "SHA-256" },
+ extractable: true,
+ usage: [ "sign" ] },
+ exception: "TypeError: JWK kty \"EC\" doesn't match algorithm \"HMAC\"" },
]};
let rsa_tsuite = {
extractable: true,
usage: [ "encrypt" ] },
exception: "TypeError: d2i_PUBKEY() failed" },
+ { key: { fmt: "jwk",
+ key: { kty: 'oct', k: 'AQIDBAUG', alg: 'HS256' },
+ alg: { name: "RSA-OAEP", hash: "SHA-256" },
+ extractable: true,
+ usage: [ "encrypt" ] },
+ exception: "TypeError: JWK kty \"oct\" doesn't match algorithm \"RSA-OAEP\"" },
+ { key: { fmt: "jwk",
+ key: { kty: 'EC' },
+ alg: { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
+ extractable: true,
+ usage: [ "sign" ] },
+ exception: "TypeError: JWK kty \"EC\" doesn't match algorithm \"RSASSA-PKCS1-v1_5\"" },
]};
run([