Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: deriveBits/deriveKey normalize the hash, case-insensitively

orezraey1116e0c8d419
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.html
index 79c14d373..44c53ec7e 100644
--- a/src/browser/tests/crypto.html
+++ b/src/browser/tests/crypto.html
@@ -1813,6 +1813,11 @@
iterationsNaN: await errorName(() => subtle.deriveBits({ ...pbkdf2, iterations: NaN }, pbkdf2Key, 128)),
hashWithoutName: await errorName(() => subtle.deriveBits({ ...hkdf, hash: {} }, hkdfKey, 128)),
unknownHash: await errorName(() => subtle.deriveBits({ ...hkdf, hash: "foo" }, hkdfKey, 128)),
+ // The hash is normalized with the algorithm: matched case-insensitively,
+ // and an unknown one is rejected before derivedKeyType.
+ lowerHash: hex(await subtle.deriveBits({ ...pbkdf2, hash: "sha-256" }, pbkdf2Key, 128)),
+ lowerHashObject: hex(await subtle.deriveBits({ ...hkdf, hash: { name: "sha-256" } }, hkdfKey, 128)),
+ unknownHashBeforeDerived: await errorName(() => subtle.deriveKey({ ...hkdf, hash: "SHA-999" }, hkdfKey, {}, true, ["encrypt"])),
// deriveKey normalizes the algorithm before derivedKeyType.
unknownBeforeDerived: await errorName(() => subtle.deriveKey({ ...hkdf, name: "foo" }, hkdfKey, "AES-FOO", true, ["encrypt"])),
missingBeforeDerived: await errorName(() => subtle.deriveKey({ ...hkdf, info: undefined }, hkdfKey, "AES-FOO", true, ["encrypt"])),
@@ -1845,6 +1850,9 @@
testing.expectEqual("TypeError", results.iterationsNaN);
testing.expectEqual("TypeError", results.hashWithoutName);
testing.expectEqual("NotSupportedError", results.unknownHash);
+ testing.expectEqual(results.pbkdf2, results.lowerHash);
+ testing.expectEqual(results.hkdf, results.lowerHashObject);
+ testing.expectEqual("NotSupportedError", results.unknownHashBeforeDerived);
testing.expectEqual("NotSupportedError", results.unknownBeforeDerived);
testing.expectEqual("TypeError", results.missingBeforeDerived);
testing.expectEqual("InvalidAccessError", results.otherKey);
diff --git a/src/browser/webapi/SubtleCrypto.zig b/src/browser/webapi/SubtleCrypto.zig
index 6315858f9..68cff5674 100644
--- a/src/browser/webapi/SubtleCrypto.zig
+++ b/src/browser/webapi/SubtleCrypto.zig
@@ -450,7 +450,7 @@ pub fn deriveKey(
return error.NotSupported;
}
const requested = try HMAC.lengthParam(h.length, local);
- const hash_name = common.canonicalHash(try h.hash.name(local)) orelse return error.NotSupported;
+ const hash_name = try h.hash.canonical(local);
const hash_md = crypto.findDigest(hash_name) catch return error.NotSupported;
// Default length, per spec, is the hash's block size (in bits).
const length: u32 = requested orelse @intCast(crypto.EVP_MD_block_size(hash_md) * 8);
diff --git a/src/browser/webapi/crypto/HMAC.zig b/src/browser/webapi/crypto/HMAC.zig
index 885f9ac9e..12176a2be 100644
--- a/src/browser/webapi/crypto/HMAC.zig
+++ b/src/browser/webapi/crypto/HMAC.zig
@@ -45,7 +45,7 @@ pub fn init(
// Normalization converts the dictionary's members, then the hash: an
// unrecognized one surfaces as NotSupportedError, before the usages.
const length = try lengthParam(params.length, local);
- const hash_name = common.canonicalHash(try params.hash.name(local)) orelse return error.NotSupported;
+ const hash_name = try params.hash.canonical(local);
const digest = crypto.findDigest(hash_name) catch return error.NotSupported;
// HMAC only accepts sign / verify; any other usage is a SyntaxError per
diff --git a/src/browser/webapi/crypto/KDF.zig b/src/browser/webapi/crypto/KDF.zig
index 7f1466b37..32186bedf 100644
--- a/src/browser/webapi/crypto/KDF.zig
+++ b/src/browser/webapi/crypto/KDF.zig
@@ -69,7 +69,7 @@ pub fn pbkdf2(
exec: *const Execution,
) Error![]const u8 {
// Normalizing the params (the hash) comes before the key checks.
- const digest = crypto.findDigest(try params.hash.name(exec.js.local.?)) catch return error.NotSupported;
+ const digest = crypto.findDigest(try params.hash.canonical(exec.js.local.?)) catch return error.NotSupported;
try checkBaseKey(base_key, "PBKDF2", usage_ok);
const out_len = try outputLen(length);
// A zero-length derivation is valid and yields an empty buffer; the C
@@ -111,7 +111,7 @@ pub fn hkdf(
exec: *const Execution,
) Error![]const u8 {
// Normalizing the params (the hash) comes before the key checks.
- const digest = crypto.findDigest(try params.hash.name(exec.js.local.?)) catch return error.NotSupported;
+ const digest = crypto.findDigest(try params.hash.canonical(exec.js.local.?)) catch return error.NotSupported;
try checkBaseKey(base_key, "HKDF", usage_ok);
const out = try exec.local_arena.alloc(u8, try outputLen(length));
// A zero-length derivation is valid and yields an empty buffer; the C
diff --git a/src/browser/webapi/crypto/algorithm.zig b/src/browser/webapi/crypto/algorithm.zig
index e390673a8..d985855bf 100644
--- a/src/browser/webapi/crypto/algorithm.zig
+++ b/src/browser/webapi/crypto/algorithm.zig
@@ -23,6 +23,7 @@ const std = @import("std");
const js = @import("../../js/js.zig");
const CryptoKey = @import("../CryptoKey.zig");
+const common = @import("common.zig");
/// The TypeError of an algorithm identifier object without a `name`.
pub const missing_name = "required member name is undefined";
@@ -43,6 +44,12 @@ pub const Hash = union(enum) {
.nameless => local.typeError(missing_name),
};
}
+
+ /// The hash's registered name, matched case-insensitively: a TypeError
+ /// without a name, NotSupportedError for a hash that isn't supported.
+ pub fn canonical(self: Hash, local: *const js.Local) error{ TypeError, NotSupported }![]const u8 {
+ return common.canonicalHash(try self.name(local)) orelse error.NotSupported;
+ }
};
/// Passed for `generateKey()`.
@@ -127,12 +134,12 @@ pub const Derive = union(enum) {
const name = try identifier.name(local);
if (std.ascii.eqlIgnoreCase(name, "PBKDF2")) {
const params = try convert(Pbkdf2Params, value, local);
- _ = try params.hash.name(local);
+ _ = try params.hash.canonical(local);
return .{ .pbkdf2 = params };
}
if (std.ascii.eqlIgnoreCase(name, "HKDF")) {
const params = try convert(HkdfParams, value, local);
- _ = try params.hash.name(local);
+ _ = try params.hash.canonical(local);
return .{ .hkdf = params };
}
if (std.ascii.eqlIgnoreCase(name, "ECDH") or std.ascii.eqlIgnoreCase(name, "X25519")) {