From: Dmitry Volyntsev Date: Tue, 17 Nov 2026 02:52:24 +0000 (-0800) Subject: WebCrypto: set exceptions on key conversion failures X-Git-Url: https://git.kaiwu.me/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=a54eaab73f32241aa5b15bed8a2a9302e9c7e74f;p=njs.git WebCrypto: set exceptions on key conversion failures Previously, several key import, export and signature conversion helpers returned failure without setting an exception when an OpenSSL call failed: BN_bin2bn(), BN_new(), EVP_PKEY_new(), EVP_PKEY_set1_RSA(), d2i_ECDSA_SIG() and the bignum padding. The resulting Promise was rejected with undefined in njs and with an internal uninitialized value in QuickJS. In addition, njs_convert_der_to_p1363() returned NJS_OK on these failures, so sign() resolved with an empty signature. The QuickJS OKP JWK import also used the result of JS_ToCStringLen() for "x" and "d" without checking for NULL. Report every such failure with an exception. The issues were introduced in 86c202c7 (0.7.7), 50e7c9b2 (0.7.10), 75ca26f8 (0.8.10) and 744039e2 (0.9.7). --- diff --git a/external/njs_webcrypto_module.c b/external/njs_webcrypto_module.c index 8f3e1c13..9187898b 100644 --- a/external/njs_webcrypto_module.c +++ b/external/njs_webcrypto_module.c @@ -2362,11 +2362,13 @@ njs_export_jwk_ec(njs_vm_t *vm, njs_webcrypto_key_t *key, njs_value_t *retval) x_bn = BN_new(); if (x_bn == NULL) { + njs_webcrypto_error(vm, "BN_new() failed"); goto fail; } y_bn = BN_new(); if (y_bn == NULL) { + njs_webcrypto_error(vm, "BN_new() failed"); goto fail; } @@ -3236,6 +3238,7 @@ fail: static BIGNUM * njs_import_base64url_bignum(njs_vm_t *vm, njs_opaque_value_t *value) { + BIGNUM *bn; njs_int_t ret; njs_str_t data, decoded; u_char buf[512]; @@ -3256,7 +3259,12 @@ njs_import_base64url_bignum(njs_vm_t *vm, njs_opaque_value_t *value) njs_decode_base64url(&decoded, &data); - return BN_bin2bn(decoded.start, decoded.length, NULL); + bn = BN_bin2bn(decoded.start, decoded.length, NULL); + if (njs_slow_path(bn == NULL)) { + njs_webcrypto_error(vm, "BN_bin2bn() failed"); + } + + return bn; } @@ -3468,10 +3476,12 @@ done: pkey = EVP_PKEY_new(); if (njs_slow_path(pkey == NULL)) { + njs_webcrypto_error(vm, "EVP_PKEY_new() failed"); goto fail; } if (!EVP_PKEY_set1_RSA(pkey, rsa)) { + njs_webcrypto_error(vm, "EVP_PKEY_set1_RSA() failed"); EVP_PKEY_free(pkey); goto fail; } @@ -3677,6 +3687,7 @@ fail0: pkey = EVP_PKEY_new(); if (njs_slow_path(pkey == NULL)) { + njs_webcrypto_error(vm, "EVP_PKEY_new() failed"); goto fail; } @@ -4521,6 +4532,7 @@ njs_convert_der_to_p1363(njs_vm_t *vm, EVP_PKEY *pkey, const u_char *der, n = njs_ec_rs_size(pkey); if (n == 0) { + njs_webcrypto_error(vm, "cannot determine ECDSA signature length"); goto fail; } @@ -4531,6 +4543,7 @@ njs_convert_der_to_p1363(njs_vm_t *vm, EVP_PKEY *pkey, const u_char *der, ec_sig = d2i_ECDSA_SIG(NULL, &der, der_len); if (ec_sig == NULL) { + njs_webcrypto_error(vm, "d2i_ECDSA_SIG() failed"); goto fail; } @@ -4542,10 +4555,12 @@ njs_convert_der_to_p1363(njs_vm_t *vm, EVP_PKEY *pkey, const u_char *der, #endif if (njs_bn_bn2binpad(r, data, n) <= 0) { + njs_webcrypto_error(vm, "njs_bn_bn2binpad() failed"); goto fail; } if (njs_bn_bn2binpad(s, &data[n], n) <= 0) { + njs_webcrypto_error(vm, "njs_bn_bn2binpad() failed"); goto fail; } @@ -4556,7 +4571,7 @@ njs_convert_der_to_p1363(njs_vm_t *vm, EVP_PKEY *pkey, const u_char *der, fail: - *out_len = 0; + ret = NJS_ERROR; done: diff --git a/external/qjs_webcrypto_module.c b/external/qjs_webcrypto_module.c index 41c58d58..f4e2ca27 100644 --- a/external/qjs_webcrypto_module.c +++ b/external/qjs_webcrypto_module.c @@ -1691,11 +1691,13 @@ qjs_export_jwk_ec(JSContext *cx, qjs_webcrypto_key_t *key) x_bn = BN_new(); if (x_bn == NULL) { + qjs_webcrypto_error(cx, "BN_new() failed"); goto fail; } y_bn = BN_new(); if (y_bn == NULL) { + qjs_webcrypto_error(cx, "BN_new() failed"); goto fail; } @@ -3257,6 +3259,10 @@ qjs_import_base64url_bignum(JSContext *cx, JSValue value) bn = BN_bin2bn(decoded.start, decoded.length, NULL); JS_FreeCString(cx, (char *) data.start); + if (bn == NULL) { + qjs_webcrypto_error(cx, "BN_bin2bn() failed"); + } + return bn; } @@ -3501,10 +3507,12 @@ done: pkey = EVP_PKEY_new(); if (pkey == NULL) { + qjs_webcrypto_error(cx, "EVP_PKEY_new() failed"); goto fail; } if (!EVP_PKEY_set1_RSA(pkey, rsa)) { + qjs_webcrypto_error(cx, "EVP_PKEY_set1_RSA() failed"); EVP_PKEY_free(pkey); goto fail; } @@ -3677,6 +3685,7 @@ fail0: pkey = EVP_PKEY_new(); if (pkey == NULL) { + qjs_webcrypto_error(cx, "EVP_PKEY_new() failed"); goto fail; } @@ -3832,6 +3841,10 @@ qjs_import_jwk_okp(JSContext *cx, JSValue jwk, qjs_webcrypto_key_t *key) x_str = JS_ToCStringLen(cx, &x_len, val); JS_FreeValue(cx, val); + if (x_str == NULL) { + return NULL; + } + x_b64.start = (u_char *) x_str; x_b64.length = x_len; x_raw.start = x_buf; @@ -3852,6 +3865,10 @@ qjs_import_jwk_okp(JSContext *cx, JSValue jwk, qjs_webcrypto_key_t *key) d_str = JS_ToCStringLen(cx, &d_len, val); JS_FreeValue(cx, val); + if (d_str == NULL) { + return NULL; + } + d_b64.start = (u_char *) d_str; d_b64.length = d_len; d_raw.start = d_buf; @@ -4607,6 +4624,7 @@ qjs_convert_der_to_p1363(JSContext *cx, EVP_PKEY *pkey, const u_char *der, n = qjs_ec_rs_size(pkey); if (n == 0) { + qjs_webcrypto_error(cx, "cannot determine ECDSA signature length"); return -1; } @@ -4618,6 +4636,7 @@ qjs_convert_der_to_p1363(JSContext *cx, EVP_PKEY *pkey, const u_char *der, ec_sig = d2i_ECDSA_SIG(NULL, &der, der_len); if (ec_sig == NULL) { + qjs_webcrypto_error(cx, "d2i_ECDSA_SIG() failed"); goto fail; } @@ -4629,10 +4648,12 @@ qjs_convert_der_to_p1363(JSContext *cx, EVP_PKEY *pkey, const u_char *der, #endif if (njs_bn_bn2binpad(r, data, n) <= 0) { + qjs_webcrypto_error(cx, "njs_bn_bn2binpad() failed"); goto fail; } if (njs_bn_bn2binpad(s, &data[n], n) <= 0) { + qjs_webcrypto_error(cx, "njs_bn_bn2binpad() failed"); goto fail; }