Lightpanda: the headless browser designed for AI and automation
webapi: RSA generateKey checks modulusLength's range
orezraeyf24293b81124
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.htmlindex 41970f776..f53c69b18 100644--- a/src/browser/tests/crypto.html+++ b/src/browser/tests/crypto.html@@ -1313,9 +1313,39 @@true, [], "SyntaxError",),];+ // modulusLength is an [EnforceRange] unsigned long: out of range is a+ // TypeError, before the usages are looked at (2^32 + 1024 used to wrap to a+ // 1024-bit key).+ for (const name of ["RSA-OAEP", "RSASSA-PKCS1-v1_5", "RSA-PSS"]) {+ const usages = name === "RSA-OAEP" ? ["decrypt"] : ["sign"];+ for (const modulusLength of [2 ** 32 + 1024, -1024, NaN, Infinity]) {+ const algo = { name, hash: "SHA-256", modulusLength, publicExponent: goodExp };+ results.push(await captureGenKeyReject(algo, true, usages, "TypeError"));+ results.push(await captureGenKeyReject(algo, true, [], "TypeError"));+ }+ }+ // An undefined modulusLength is a TypeError too.+ results.push(await captureGenKeyReject(+ { name: "RSA-OAEP", hash: "SHA-256", modulusLength: undefined, publicExponent: goodExp }, true, ["decrypt"], "TypeError",+ ));+ // Other algorithms ignore stray RSA members, valid or not.+ results.push(await captureGenKeyReject({ name: "HMAC", hash: "SHA-256", modulusLength: -1 }, true, ["sign"], null));+ results.push(await captureGenKeyReject({ name: "HMAC", hash: "SHA-256", publicExponent: goodExp }, true, ["sign"], null));+ // The TypeError rejects the promise; it isn't thrown.+ const rejected = crypto.subtle.generateKey(+ { name: "RSA-OAEP", hash: "SHA-256", modulusLength: -1, publicExponent: goodExp }, true, ["decrypt"],+ );+ const rejectedIsPromise = rejected instanceof Promise;+ await rejected.catch(() => {});+ // In range, the value is truncated as usual.+ const truncated = await crypto.subtle.generateKey(+ { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256", modulusLength: 1024.9, publicExponent: goodExp }, true, ["sign"],+ );state.resolve();await state.done(() => {assertGenKeyRejects(results);+ testing.expectEqual(true, rejectedIsPromise);+ testing.expectEqual(1024, truncated.privateKey.algorithm.modulusLength);});</script>diff --git a/src/browser/webapi/crypto/RSA.zig b/src/browser/webapi/crypto/RSA.zigindex 131965766..e22b7b02d 100644--- a/src/browser/webapi/crypto/RSA.zig+++ b/src/browser/webapi/crypto/RSA.zig@@ -162,6 +162,12 @@ fn hashOfJwkAlg(s: *const Scheme, alg: []const u8) ?[]const u8 {const max_modulus_length = 4096;+/// RsaKeyGenParams' `modulusLength`, an [EnforceRange] unsigned long: a value+/// outside u32 (NaN, undefined, the infinities) is a TypeError.+fn modulusLength(params: algorithm.Init.RsaHashedKeyGen, local: *const js.Local) !u32 {+ return js.enforceRange(u32, try params.modulusLength.toF64()) catch local.typeError("modulusLength is out of range");+}+/// Generates an RSA key pair of `modulusLength` bits with the given public/// exponent. Mirrors EC.generate: each key keeps the EVP_PKEY of its half, and/// the requested usages are split across the pair (validate() rejected illegal@@ -173,15 +179,17 @@ pub fn generate(exec: *const Execution,) !js.Promise {const local = exec.js.local.?;- try validate(params, key_usages);-+ // Normalization: the algorithm name, then the dictionary's members, then+ // the hash (validate). The usages come after.const s = scheme(params.name) orelse return error.NotSupported;+ const requested_bits = try modulusLength(params, local);+ try validate(params, key_usages);// Generation runs on the main thread and can't be interrupted, and its cost// grows steeply with the size: 8192 bits takes seconds, 16384 (BoringSSL's// ceiling) tens of seconds, several times that on a vCPU. Pages generate// 2048 or 4096 in practice. Chrome allows 16384 but generates off-thread.- if (params.modulusLength > max_modulus_length) return error.OperationError;+ if (requested_bits > max_modulus_length) return error.OperationError;const hash_name = canonicalHash(switch (params.hash) {.string => |str| str,@@ -200,7 +208,7 @@ pub fn generate(const rsa = crypto.RSA_new() orelse return error.OutOfMemory;defer crypto.RSA_free(rsa);- if (crypto.RSA_generate_key_ex(rsa, @intCast(params.modulusLength), e, null) != 1) {+ if (crypto.RSA_generate_key_ex(rsa, @intCast(requested_bits), e, null) != 1) {return error.OperationError;}diff --git a/src/browser/webapi/crypto/algorithm.zig b/src/browser/webapi/crypto/algorithm.zigindex 96e0064bc..8c12719a1 100644--- a/src/browser/webapi/crypto/algorithm.zig+++ b/src/browser/webapi/crypto/algorithm.zig@@ -51,7 +51,12 @@ pub const Init = union(enum) {},/// This should be at least 2048./// Some organizations are now recommending that it should be 4096.- modulusLength: u32,+ /// An [EnforceRange] unsigned long, kept as a value: this dictionary+ /// is probed before the others, so converting it here would reject+ /// another algorithm's object that happens to carry one. RSA's+ /// generateKey converts it (RSA.modulusLength). It stays required, so+ /// an object without one is probed as the other dictionaries.+ modulusLength: js.Value,name: []const u8,publicExponent: js.TypedArray(u8),};