Skip to content

Commit

Permalink
crypto: validate CFRG webcrypto JWK import "d" and "x" are a pair
Browse files Browse the repository at this point in the history
PR-URL: #45569
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
panva authored and targos committed Dec 12, 2022
1 parent 9cd106e commit d030963
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
22 changes: 17 additions & 5 deletions lib/internal/crypto/cfrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,24 @@ async function cfrgImportKey(
name,
isPublic ? 'public' : 'private',
usagesSet);
keyObject = createCFRGRawKey(

const publicKeyObject = createCFRGRawKey(
name,
Buffer.from(
isPublic ? keyData.x : keyData.d,
'base64'),
isPublic);
Buffer.from(keyData.x, 'base64'),
true);

if (isPublic) {
keyObject = publicKeyObject;
} else {
keyObject = createCFRGRawKey(
name,
Buffer.from(keyData.d, 'base64'),
false);

if (!createPublicKey(keyObject).equals(publicKeyObject)) {
throw lazyDOMException('Invalid JWK keyData', 'DataError');
}
}
break;
}
case 'raw': {
Expand Down
36 changes: 0 additions & 36 deletions test/wpt/status/WebCryptoAPI.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,5 @@
},
"idlharness.https.any.js": {
"skip": "Various non-IDL-compliant things"
},
"import_export/okp_importKey_failures_Ed25519.https.any.js": {
"fail": {
"expected": [
"Invalid key pair: importKey(jwk(private), {name: Ed25519}, true, [sign])",
"Invalid key pair: importKey(jwk(private), {name: Ed25519}, true, [sign, sign])"
]
}
},
"import_export/okp_importKey_failures_Ed448.https.any.js": {
"fail": {
"expected": [
"Invalid key pair: importKey(jwk(private), {name: Ed448}, true, [sign])",
"Invalid key pair: importKey(jwk(private), {name: Ed448}, true, [sign, sign])"
]
}
},
"import_export/okp_importKey_failures_X25519.https.any.js": {
"fail": {
"expected": [
"Invalid key pair: importKey(jwk(private), {name: X25519}, true, [deriveKey])",
"Invalid key pair: importKey(jwk(private), {name: X25519}, true, [deriveBits, deriveKey])",
"Invalid key pair: importKey(jwk(private), {name: X25519}, true, [deriveBits])",
"Invalid key pair: importKey(jwk(private), {name: X25519}, true, [deriveKey, deriveBits, deriveKey, deriveBits])"
]
}
},
"import_export/okp_importKey_failures_X448.https.any.js": {
"fail": {
"expected": [
"Invalid key pair: importKey(jwk(private), {name: X448}, true, [deriveKey])",
"Invalid key pair: importKey(jwk(private), {name: X448}, true, [deriveBits, deriveKey])",
"Invalid key pair: importKey(jwk(private), {name: X448}, true, [deriveBits])",
"Invalid key pair: importKey(jwk(private), {name: X448}, true, [deriveKey, deriveBits, deriveKey, deriveBits])"
]
}
}
}

0 comments on commit d030963

Please sign in to comment.