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 fe00799 commit d51d18a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 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
29 changes: 16 additions & 13 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ const vectors = {
// Test bad usages
{
async function test(name) {
const invalidUsages = [];
allUsages.forEach((usage) => {
if (!vectors[name].usages.includes(usage))
invalidUsages.push(usage);
});
await assert.rejects(
subtle.generateKey(
{
Expand All @@ -219,14 +214,22 @@ const vectors = {
true,
[]),
{ message: /Usages cannot be empty/ });
return assert.rejects(
subtle.generateKey(
{
name, ...vectors[name].algorithm
},
true,
invalidUsages),
{ message: /Unsupported key usage/ });

const invalidUsages = [];
allUsages.forEach((usage) => {
if (!vectors[name].usages.includes(usage))
invalidUsages.push(usage);
});
for (const invalidUsage of invalidUsages) {
await assert.rejects(
subtle.generateKey(
{
name, ...vectors[name].algorithm
},
true,
[...vectors[name].usages, invalidUsage]),
{ message: /Unsupported key usage/ });
}
}

const tests = Object.keys(vectors).map(test);
Expand Down
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 d51d18a

Please sign in to comment.