Skip to content

Commit

Permalink
fix(ext/crypto): throw DataError for invalid EC key import (#25181)
Browse files Browse the repository at this point in the history
Fixes #20931
  • Loading branch information
littledivy authored Aug 23, 2024
1 parent d9a7b30 commit 38bc402
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ext/crypto/import_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,8 @@ fn import_key_ec(

let rng = ring::rand::SystemRandom::new();
// deserialize pkcs8 using ring crate, to VALIDATE public key
let _private_key = EcdsaKeyPair::from_pkcs8(signing_alg, &data, &rng)?;
let _private_key = EcdsaKeyPair::from_pkcs8(signing_alg, &data, &rng)
.map_err(|_| data_error("invalid key"))?;

// 11.
if named_curve != pk_named_curve {
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/webcrypto_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2045,3 +2045,24 @@ Deno.test(async function p521Generate() {
assert(key.privateKey instanceof CryptoKey);
assert(key.publicKey instanceof CryptoKey);
});

Deno.test(async function invalidEcPointDataError() {
await assertRejects(async () => {
await crypto.subtle
.importKey(
"pkcs8",
// deno-fmt-ignore
new Uint8Array([
48, 102, 2, 1, 0, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134,
72, 206, 61, 3, 1, 7, 4, 76, 48, 74, 2, 1, 1, 4, 32, 255, 255, 255, 255,
0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 188, 230, 250, 173,
167, 23, 158, 132, 243, 185, 202, 194, 252, 99, 37, 81, 161, 35, 3, 33, 0,
0, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 188,
230, 250, 173, 167, 23, 158, 132, 243, 185, 202, 194, 252, 99, 37, 81,
]),
{ name: "ECDSA", namedCurve: "P-256" },
true,
["sign"],
);
}, DOMException);
});

0 comments on commit 38bc402

Please sign in to comment.