Skip to content

Commit 60023fd

Browse files
panvaaduh95
authored andcommitted
crypto: resolve supports overloads by type
Convert the third argument before checking the operation, and apply additional-algorithm checks only for that overload. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66237 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 0346e4e commit 60023fd

3 files changed

Lines changed: 59 additions & 49 deletions

File tree

‎lib/internal/crypto/webcrypto.js‎

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,22 @@ class SubtleCrypto {
17741774
context: '2nd argument',
17751775
});
17761776

1777+
let length = null;
1778+
let additionalAlgorithm;
1779+
if (lengthOrAdditionalAlgorithm === null ||
1780+
typeof lengthOrAdditionalAlgorithm === 'number') {
1781+
if (lengthOrAdditionalAlgorithm !== null) {
1782+
length = webidl.converters['unsigned long'](lengthOrAdditionalAlgorithm, {
1783+
prefix,
1784+
context: '3rd argument',
1785+
enforceRange: true,
1786+
});
1787+
}
1788+
} else {
1789+
additionalAlgorithm = webidl.converters.AlgorithmIdentifier(
1790+
lengthOrAdditionalAlgorithm, { prefix, context: '3rd argument' });
1791+
}
1792+
17771793
switch (operation) {
17781794
case 'decapsulateBits':
17791795
case 'decapsulateKey':
@@ -1797,17 +1813,7 @@ class SubtleCrypto {
17971813
return false;
17981814
}
17991815

1800-
let length;
1801-
let additionalAlgorithm;
1802-
if (operation === 'deriveKey') {
1803-
additionalAlgorithm = webidl.converters.AlgorithmIdentifier(
1804-
lengthOrAdditionalAlgorithm,
1805-
{
1806-
prefix,
1807-
context: '3rd argument',
1808-
},
1809-
);
1810-
1816+
if (additionalAlgorithm !== undefined && operation === 'deriveKey') {
18111817
if (!check('importKey', additionalAlgorithm)) {
18121818
return false;
18131819
}
@@ -1819,39 +1825,14 @@ class SubtleCrypto {
18191825
}
18201826

18211827
operation = 'deriveBits';
1822-
} else if (operation === 'wrapKey') {
1823-
additionalAlgorithm = webidl.converters.AlgorithmIdentifier(
1824-
lengthOrAdditionalAlgorithm,
1825-
{
1826-
prefix,
1827-
context: '3rd argument',
1828-
},
1829-
);
1830-
1828+
} else if (additionalAlgorithm !== undefined && operation === 'wrapKey') {
18311829
if (!check('exportKey', additionalAlgorithm)) {
18321830
return false;
18331831
}
1834-
} else if (operation === 'unwrapKey') {
1835-
additionalAlgorithm = webidl.converters.AlgorithmIdentifier(
1836-
lengthOrAdditionalAlgorithm,
1837-
{
1838-
prefix,
1839-
context: '3rd argument',
1840-
},
1841-
);
1842-
1832+
} else if (additionalAlgorithm !== undefined && operation === 'unwrapKey') {
18431833
if (!check('importKey', additionalAlgorithm)) {
18441834
return false;
18451835
}
1846-
} else if (operation === 'deriveBits') {
1847-
length = lengthOrAdditionalAlgorithm;
1848-
if (length !== null) {
1849-
length = webidl.converters['unsigned long'](length, {
1850-
prefix,
1851-
context: '3rd argument',
1852-
enforceRange: true,
1853-
});
1854-
}
18551836
} else if (operation === 'getPublicKey') {
18561837
let normalizedAlgorithm;
18571838
try {
@@ -1872,15 +1853,8 @@ class SubtleCrypto {
18721853
default:
18731854
return false;
18741855
}
1875-
} else if (operation === 'encapsulateKey' || operation === 'decapsulateKey') {
1876-
additionalAlgorithm = webidl.converters.AlgorithmIdentifier(
1877-
lengthOrAdditionalAlgorithm,
1878-
{
1879-
prefix,
1880-
context: '3rd argument',
1881-
},
1882-
);
1883-
1856+
} else if (additionalAlgorithm !== undefined &&
1857+
(operation === 'encapsulateKey' || operation === 'decapsulateKey')) {
18841858
let sharedKeyLength;
18851859
let normalizedAdditionalAlgorithm;
18861860
try {

‎test/fixtures/webcrypto/supports-level-2.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ export const vectors = {
250250
[true, 'X25519'],
251251
],
252252
'wrapKey': [
253-
[false, 'AES-KW'],
253+
[true, 'AES-KW'],
254254
[true, 'AES-KW', 'AES-CTR'],
255255
[true, 'AES-KW', 'HMAC'],
256256
],
257257
'unwrapKey': [
258-
[false, 'AES-KW'],
258+
[true, 'AES-KW'],
259259
[true, 'AES-KW', 'AES-CTR'],
260260
],
261261
'unsupported operation': [
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const { supports } = SubtleCrypto;
9+
10+
for (const operation of ['wrapKey', 'unwrapKey']) {
11+
for (const length of [undefined, null, 0, 128]) {
12+
assert.strictEqual(supports(operation, 'AES-KW', length), true);
13+
}
14+
assert.strictEqual(supports(operation, 'AES-KW'), true);
15+
}
16+
17+
for (const operation of ['digest', 'unknown', 'wrapKey', 'deriveBits']) {
18+
for (const value of [-1, NaN, Infinity, 2 ** 32, Symbol()]) {
19+
assert.throws(() => supports(operation, 'SHA-256', value), TypeError);
20+
}
21+
}
22+
23+
const hkdf = {
24+
name: 'HKDF', hash: 'SHA-256', salt: new Uint8Array(), info: new Uint8Array(),
25+
};
26+
assert.strictEqual(supports('deriveBits', hkdf, 128), true);
27+
for (const value of ['128', 'AES-GCM', {}, true, 128n]) {
28+
assert.strictEqual(supports('deriveBits', hkdf, value), false);
29+
}
30+
assert.strictEqual(supports('deriveKey', hkdf), false);
31+
assert.strictEqual(supports('deriveKey', hkdf, { name: 'AES-GCM', length: 128 }), true);
32+
33+
if (supports('encapsulateBits', 'ML-KEM-768')) {
34+
assert.strictEqual(supports('encapsulateKey', 'ML-KEM-768'), true);
35+
assert.strictEqual(supports('decapsulateKey', 'ML-KEM-768', 128), true);
36+
}

0 commit comments

Comments
 (0)