Skip to content

Lightpanda: the headless browser designed for AI and automation

add ext check

Karl Seguin1de7393216e7
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.html
index 2dfb05a0d..819e14a23 100644
--- a/src/browser/tests/crypto.html
+++ b/src/browser/tests/crypto.html
@@ -499,6 +499,7 @@
const kwWrapped = await subtle.wrapKey("raw", target, kw, "AES-KW");
const notJson = await subtle.wrapKey("raw", target, wrapper, gcm);
const noKty = await subtle.encrypt(gcm, encryptAndUnwrap, new TextEncoder().encode(JSON.stringify({ k: "AAAAAAAAAAAAAAAAAAAAAA" })));
+ const extFalse = await subtle.encrypt(gcm, encryptAndUnwrap, new TextEncoder().encode(JSON.stringify({ kty: "oct", k: "AAAAAAAAAAAAAAAAAAAAAA", ext: false })));
const results = {
// The wrapping key: an algorithm that wraps or encrypts, its own algorithm, and the wrapKey/unwrapKey usage.
@@ -524,6 +525,9 @@
// The unwrapped bytes go through importKey: a JWK must be JSON with a kty, and a secret key needs usages.
notJson: await errorName(() => subtle.unwrapKey("jwk", notJson, wrapper, gcm, "AES-GCM", true, ["encrypt"])),
jwkWithoutKty: await errorName(() => subtle.unwrapKey("jwk", noKty, encryptAndUnwrap, gcm, "AES-GCM", true, ["encrypt"])),
+ // A JWK with ext: false can't be unwrapped as extractable, but can as not.
+ extFalseExtractable: await errorName(() => subtle.unwrapKey("jwk", extFalse, encryptAndUnwrap, gcm, "AES-GCM", true, ["encrypt"])),
+ extFalse: await errorName(() => subtle.unwrapKey("jwk", extFalse, encryptAndUnwrap, gcm, "AES-GCM", false, ["encrypt"])),
emptyUsages: await errorName(() => subtle.unwrapKey("raw", kwWrapped, kw, "AES-KW", "AES-GCM", true, [])),
};
@@ -546,6 +550,8 @@
testing.expectEqual("OperationError", results.kwNotMultipleOf8);
testing.expectEqual("DataError", results.notJson);
testing.expectEqual("DataError", results.jwkWithoutKty);
+ testing.expectEqual("DataError", results.extFalseExtractable);
+ testing.expectEqual(null, results.extFalse);
testing.expectEqual("SyntaxError", results.emptyUsages);
});
</script>
diff --git a/src/browser/webapi/SubtleCrypto.zig b/src/browser/webapi/SubtleCrypto.zig
index 243bd641a..46014f3cf 100644
--- a/src/browser/webapi/SubtleCrypto.zig
+++ b/src/browser/webapi/SubtleCrypto.zig
@@ -184,6 +184,10 @@ pub fn importKey(
if (!std.mem.eql(u8, jwk.kty orelse "", "oct")) {
return error.DataError;
}
+ // A JWK marked non-extractable can't be imported as extractable.
+ if (extractable and jwk.ext == false) {
+ return error.DataError;
+ }
const k = jwk.k orelse return error.DataError;
break :blk try common.base64Decode(exec.local_arena, k);
}
@@ -273,15 +277,44 @@ pub fn exportKey(
const local = exec.js.local.?;
return switch (try exportAs(format, key, exec)) {
.bytes => |bytes| local.resolvePromise(js.ArrayBuffer{ .values = bytes }),
- .jwk => |jwk| local.resolvePromise(jwk),
+ .jwk => |jwk| local.resolvePromise(try jwk.toJs(local)),
};
}
/// A key exported in some format: the key's bytes (raw, spki, pkcs8), or its
-/// JWK object.
+/// JWK.
const Exported = union(enum) {
bytes: []const u8,
- jwk: js.Value,
+ jwk: Jwk,
+};
+
+/// A key's JWK, as the struct it's built from: exportKey resolves it as an
+/// object, wrapKey serializes it here. Going through V8's JSON.stringify would
+/// let the page steer the wrapped bytes with an inherited `toJSON`; the spec
+/// stringifies "in the context of a new global object".
+const Jwk = union(enum) {
+ secret: JwkSecret,
+ rsa: RSA.Jwk,
+
+ fn toJs(self: Jwk, local: *const js.Local) !js.Value {
+ return switch (self) {
+ .secret => |jwk| local.zigValueToJs(jwk, .{}),
+ .rsa => |rsa| switch (rsa) {
+ inline else => |jwk| local.zigValueToJs(jwk, .{}),
+ },
+ };
+ }
+
+ /// The JSON text, identical to `JSON.stringify` of the object: members
+ /// are declared in the order they'd be set.
+ fn toJson(self: Jwk, allocator: std.mem.Allocator) ![]const u8 {
+ return switch (self) {
+ .secret => |jwk| std.json.Stringify.valueAlloc(allocator, jwk, .{}),
+ .rsa => |rsa| switch (rsa) {
+ inline else => |jwk| std.json.Stringify.valueAlloc(allocator, jwk, .{}),
+ },
+ };
+ }
};
/// The "export key" steps shared by exportKey and wrapKey.
@@ -334,9 +367,7 @@ const JwkSecret = struct {
key_ops: []const []const u8,
};
-fn exportJwk(key: *CryptoKey, exec: *const Execution) !js.Value {
- const local = exec.js.local.?;
-
+fn exportJwk(key: *CryptoKey, exec: *const Execution) !Jwk {
// The `alg` registry value depends on the algorithm and key length.
const alg: []const u8 = switch (key._type) {
.aes => try exec.local_arena.print("A{d}{s}", .{
@@ -347,19 +378,19 @@ fn exportJwk(key: *CryptoKey, exec: *const Execution) !js.Value {
const hash: []const u8 = key._algorithm.hash orelse "SHA-";
break :blk try exec.local_arena.print("HS{s}", .{hash[4..]}); // strip "SHA-"
},
- .rsa => return RSA.exportJwk(key, exec),
+ .rsa => return .{ .rsa = try RSA.exportJwk(key, exec) },
else => {
log.debug(.not_implemented, "SubtleCrypto.exportKey", .{ .format = "jwk", .type = key._type });
return error.NotSupported;
},
};
- return local.zigValueToJs(JwkSecret{
+ return .{ .secret = .{
.k = try common.base64Encode(exec.local_arena, key._key),
.alg = alg,
.ext = key._extractable,
.key_ops = try key.getUsages(exec),
- }, .{});
+ } };
}
/// Derive an array of bits from a base key. `length` is in bits and may be null
@@ -490,7 +521,6 @@ pub fn wrapKey(
const bytes = switch (try exportAs(format, key, exec)) {
.bytes => |bytes| bytes,
- // JSON.stringify of the JWK object, UTF-8 encoded.
.jwk => |jwk| try jwk.toJson(exec.local_arena),
};
const out = try cryptBytes(algo, wrapping_key, bytes, .wrap, exec);
diff --git a/src/browser/webapi/crypto/RSA.zig b/src/browser/webapi/crypto/RSA.zig
index 9bd3bf499..83e3e64ff 100644
--- a/src/browser/webapi/crypto/RSA.zig
+++ b/src/browser/webapi/crypto/RSA.zig
@@ -403,10 +403,15 @@ const JwkPrivate = struct {
qi: []const u8,
};
-/// Exports an RSA-OAEP key as a JWK object: the public members for either half,
-/// plus the private members (with the CRT parameters) for the private key.
-pub fn exportJwk(key: *const CryptoKey, exec: *const Execution) !js.Value {
- const local = exec.js.local.?;
+/// The JWK of either half of an RSA-OAEP key.
+pub const Jwk = union(enum) {
+ public: JwkPublic,
+ private: JwkPrivate,
+};
+
+/// Exports an RSA-OAEP key as a JWK: the public members for either half, plus
+/// the private members (with the CRT parameters) for the private key.
+pub fn exportJwk(key: *const CryptoKey, exec: *const Execution) !Jwk {
const rsa = crypto.EVP_PKEY_get0_RSA(key.getKeyObject()) orelse return error.OperationError;
const alg = jwkAlgOfHash(key._algorithm.hash orelse "") orelse return error.OperationError;
const n = try jwkMember(crypto.RSA_get0_n(rsa), exec);
@@ -414,9 +419,9 @@ pub fn exportJwk(key: *const CryptoKey, exec: *const Execution) !js.Value {
const key_ops = try key.getUsages(exec);
if (key._kind == .public) {
- return local.zigValueToJs(JwkPublic{ .alg = alg, .e = e, .ext = key._extractable, .key_ops = key_ops, .n = n }, .{});
+ return .{ .public = .{ .alg = alg, .e = e, .ext = key._extractable, .key_ops = key_ops, .n = n } };
}
- return local.zigValueToJs(JwkPrivate{
+ return .{ .private = .{
.alg = alg,
.d = try jwkMember(crypto.RSA_get0_d(rsa), exec),
.dp = try jwkMember(crypto.RSA_get0_dmp1(rsa), exec),
@@ -428,7 +433,7 @@ pub fn exportJwk(key: *const CryptoKey, exec: *const Execution) !js.Value {
.p = try jwkMember(crypto.RSA_get0_p(rsa), exec),
.q = try jwkMember(crypto.RSA_get0_q(rsa), exec),
.qi = try jwkMember(crypto.RSA_get0_iqmp(rsa), exec),
- }, .{});
+ } };
}
/// A key component as a JWK Base64urlUInt member.