Skip to content

Commit d51d18a

Browse files
committed
crypto: fix webcrypto AES-KW keys accepting encrypt/decrypt usages
1 parent fe00799 commit d51d18a

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

lib/internal/crypto/aes.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,17 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
230230
validateInteger(length, 'algorithm.length');
231231
validateOneOf(length, 'algorithm.length', kAesKeyLengths);
232232

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

235-
if (hasAnyNotIn(usageSet, ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
237+
const usagesSet = new SafeSet(keyUsages);
238+
if (hasAnyNotIn(usagesSet, checkUsages)) {
236239
throw lazyDOMException(
237240
'Unsupported key usage for an AES key',
238241
'SyntaxError');
239242
}
243+
240244
return new Promise((resolve, reject) => {
241245
generateKey('aes', { length }, (err, key) => {
242246
if (err) {
@@ -249,7 +253,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
249253
resolve(new InternalCryptoKey(
250254
key,
251255
{ name, length },
252-
ArrayFrom(usageSet),
256+
ArrayFrom(usagesSet),
253257
extractable));
254258
});
255259
});

test/parallel/test-webcrypto-keygen.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,6 @@ const vectors = {
206206
// Test bad usages
207207
{
208208
async function test(name) {
209-
const invalidUsages = [];
210-
allUsages.forEach((usage) => {
211-
if (!vectors[name].usages.includes(usage))
212-
invalidUsages.push(usage);
213-
});
214209
await assert.rejects(
215210
subtle.generateKey(
216211
{
@@ -219,14 +214,22 @@ const vectors = {
219214
true,
220215
[]),
221216
{ message: /Usages cannot be empty/ });
222-
return assert.rejects(
223-
subtle.generateKey(
224-
{
225-
name, ...vectors[name].algorithm
226-
},
227-
true,
228-
invalidUsages),
229-
{ message: /Unsupported key usage/ });
217+
218+
const invalidUsages = [];
219+
allUsages.forEach((usage) => {
220+
if (!vectors[name].usages.includes(usage))
221+
invalidUsages.push(usage);
222+
});
223+
for (const invalidUsage of invalidUsages) {
224+
await assert.rejects(
225+
subtle.generateKey(
226+
{
227+
name, ...vectors[name].algorithm
228+
},
229+
true,
230+
[...vectors[name].usages, invalidUsage]),
231+
{ message: /Unsupported key usage/ });
232+
}
230233
}
231234

232235
const tests = Object.keys(vectors).map(test);

test/wpt/status/WebCryptoAPI.json

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"generateKey/failures_AES-KW.https.any.js": {
4949
"fail": {
5050
"unexpected": [
51-
"assert_unreached: Operation succeeded, but should not have Reached unreachable code",
5251
"assert_equals: Bad algorithm property not supported expected \"OperationError\" but got \"TypeError\"",
5352
"assert_equals: Bad algorithm property not supported expected \"OperationError\" but got \"SyntaxError\""
5453
]

0 commit comments

Comments
 (0)