Skip to content

Commit

Permalink
crypto: fix webcrypto AES-KW keys accepting encrypt/decrypt usages
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jun 14, 2022
1 parent fa1454e commit bcbe4e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,17 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
validateInteger(length, 'algorithm.length');
validateOneOf(length, 'algorithm.length', kAesKeyLengths);

const usageSet = new SafeSet(keyUsages);
const checkUsages = ['wrapKey', 'unwrapKey'];
if (name !== 'AES-KW')
ArrayPrototypePush(checkUsages, 'encrypt', 'decrypt');

if (hasAnyNotIn(usageSet, ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
const usagesSet = new SafeSet(keyUsages);
if (hasAnyNotIn(usagesSet, checkUsages)) {
throw lazyDOMException(
'Unsupported key usage for an AES key',
'SyntaxError');
}

return new Promise((resolve, reject) => {
generateKey('aes', { length }, (err, key) => {
if (err) {
Expand All @@ -249,7 +253,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
resolve(new InternalCryptoKey(
key,
{ name, length },
ArrayFrom(usageSet),
ArrayFrom(usagesSet),
extractable));
});
});
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,14 @@ assert.throws(() => new CryptoKey(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });

Promise.all(tests).then(common.mustCall());
}

{
assert.rejects(
subtle.generateKey(
{ length: 128, name: 'AES-KW' },
true,
['unwrapKey', 'wrapKey', 'encrypt']
), {
message: 'Unsupported key usage for an AES key'
});
}
1 change: 0 additions & 1 deletion test/wpt/status/WebCryptoAPI.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"generateKey/failures_AES-KW.https.any.js": {
"fail": {
"unexpected": [
"assert_unreached: Operation succeeded, but should not have Reached unreachable code",
"assert_equals: Bad algorithm property not supported expected \"OperationError\" but got \"TypeError\"",
"assert_equals: Bad algorithm property not supported expected \"OperationError\" but got \"SyntaxError\""
]
Expand Down

0 comments on commit bcbe4e8

Please sign in to comment.