Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: multiple webcrypto fixes #43431

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
panva marked this conversation as resolved.
Show resolved Hide resolved
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
18 changes: 10 additions & 8 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,16 @@ const vectors = {
if (!vectors[name].usages.includes(usage))
invalidUsages.push(usage);
});
return assert.rejects(
subtle.generateKey(
{
name, ...vectors[name].algorithm
},
true,
invalidUsages),
{ message: /Unsupported key 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