1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/crypto): fix importKey error when leading zeroes (#16009)

Co-authored-by: Jason <m.jason.liu@outlook.com>
This commit is contained in:
李瑞丰 2022-10-03 12:07:25 +08:00 committed by GitHub
parent bac3a1210f
commit b3444e0d3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View file

@ -1827,3 +1827,30 @@ Deno.test(async function exportKeyNotExtractable() {
await crypto.subtle.exportKey("raw", key);
}, DOMException);
});
// https://github.com/denoland/deno/issues/15126
Deno.test(async function testImportLeadingZeroesKey() {
const alg = { name: "ECDSA", namedCurve: "P-256" };
const jwk = {
kty: "EC",
crv: "P-256",
alg: "ES256",
x: "EvidcdFB1xC6tgfakqZsU9aIURxAJkcX62zHe1Nt6xU",
y: "AHsk6BioGM7MZWeXOE_49AGmtuaXFT3Ill3DYtz9uYg",
d: "WDeYo4o1heCF9l_2VIaClRyIeO16zsMlN8UG6Le9dU8",
"key_ops": ["sign"],
ext: true,
};
const key = await crypto.subtle.importKey(
"jwk",
jwk,
alg,
true,
["sign"],
);
assert(key instanceof CryptoKey);
assertEquals(key.type, "private");
});

View file

@ -637,7 +637,15 @@ fn decode_b64url_to_field_bytes<C: elliptic_curve::Curve>(
jwt_b64_int_or_err!(val, b64, "invalid b64 coordinate");
let mut bytes = elliptic_curve::FieldBytes::<C>::default();
let val = val.as_bytes();
let original_bytes = val.as_bytes();
let mut new_bytes: Vec<u8> = vec![];
if original_bytes.len() < bytes.len() {
new_bytes = vec![0; bytes.len() - original_bytes.len()];
}
new_bytes.extend_from_slice(original_bytes);
let val = new_bytes.as_slice();
if val.len() != bytes.len() {
return Err(data_error("invalid b64 coordinate"));
}