Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ function deriveBitsImpl(algorithm, baseKey, length = null) {
prefix, 'AlgorithmIdentifier', algorithm, i++);
baseKey = convertSubtleArgument(prefix, 'CryptoKey', baseKey, i++);
if (length !== null) {
length = convertSubtleArgument(prefix, 'unsigned long', length, i++);
length = webidl.converters['unsigned long'](length, {
prefix,
context: kArgumentContexts[i++],
enforceRange: true,
});
}

const normalizedAlgorithm = normalizeAlgorithm(algorithm, 'deriveBits');
Expand Down Expand Up @@ -1696,6 +1700,7 @@ class SubtleCrypto {
length = webidl.converters['unsigned long'](length, {
prefix,
context: '3rd argument',
enforceRange: true,
});
}
} else if (operation === 'getPublicKey') {
Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-webcrypto-enforce-range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const common = require('../common');

if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const { SubtleCrypto } = globalThis;
const { subtle } = globalThis.crypto;

const algorithm = {
name: 'HKDF',
hash: 'SHA-256',
info: new Uint8Array(),
salt: new Uint8Array(32),
};
const invalidLength = 2 ** 32;
const expectedError = {
code: 'ERR_OUT_OF_RANGE',
name: 'TypeError',
};

assert.throws(
() => SubtleCrypto.supports('deriveBits', algorithm, invalidLength),
expectedError);

(async () => {
const key = await subtle.importKey(
'raw', new Uint8Array(32), 'HKDF', false, ['deriveBits']);

await assert.rejects(
subtle.deriveBits(algorithm, key, invalidLength),
expectedError);
})().then(common.mustCall());
98 changes: 98 additions & 0 deletions test/parallel/test-webcrypto-webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,104 @@ function assertJsonWebKey(actual, expected) {
}
}

// [EnforceRange] integer dictionary members
{
const kOctetMax = 2 ** 8 - 1;
const kUnsignedShortMax = 2 ** 16 - 1;
const kUnsignedLongMax = 2 ** 32 - 1;
const empty = Buffer.alloc(0);
const rsaKeyGen = {
name: 'RSA-PSS',
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
};
const aesKeyParams = { name: 'AES-GCM', length: 128 };
const hmacKeyParams = {
name: 'HMAC',
hash: 'SHA-256',
length: 128,
};
const kmacKeyParams = { name: 'KMAC128', length: 128 };
const cases = [
['RsaKeyGenParams', rsaKeyGen,
{ modulusLength: kUnsignedLongMax }],
['RsaHashedKeyGenParams', { ...rsaKeyGen, hash: 'SHA-256' },
{ modulusLength: kUnsignedLongMax }],
['AesKeyGenParams', aesKeyParams,
{ length: kUnsignedShortMax }],
['RsaPssParams', { name: 'RSA-PSS', saltLength: 20 },
{ saltLength: kUnsignedLongMax }],
['HmacKeyGenParams', hmacKeyParams,
{ length: kUnsignedLongMax }],
['HmacImportParams', hmacKeyParams,
{ length: kUnsignedLongMax }],
['CShakeParams', { name: 'cSHAKE128', outputLength: 256 },
{ outputLength: kUnsignedLongMax }],
['Pbkdf2Params', {
name: 'PBKDF2',
salt: empty,
iterations: 1,
hash: 'SHA-256',
}, { iterations: kUnsignedLongMax }],
['AesDerivedKeyParams', aesKeyParams,
{ length: kUnsignedShortMax }],
['AeadParams', {
name: 'AES-GCM',
iv: Buffer.alloc(12),
tagLength: 128,
}, { tagLength: kOctetMax }],
['AesCtrParams', {
name: 'AES-CTR',
counter: Buffer.alloc(16),
length: 128,
}, { length: kOctetMax }],
['Argon2Params', {
name: 'Argon2id',
nonce: Buffer.alloc(8),
parallelism: 1,
memory: 8,
passes: 1,
version: 0x13,
}, {
parallelism: kUnsignedLongMax,
memory: kUnsignedLongMax,
passes: kUnsignedLongMax,
version: kOctetMax,
}],
['KmacKeyGenParams', kmacKeyParams,
{ length: kUnsignedLongMax }],
['KmacImportParams', kmacKeyParams,
{ length: kUnsignedLongMax }],
['KmacParams', { name: 'KMAC128', outputLength: 256 },
{ outputLength: kUnsignedLongMax }],
['KangarooTwelveParams', { name: 'KT128', outputLength: 256 },
{ outputLength: kUnsignedLongMax }],
['TurboShakeParams', {
name: 'TurboSHAKE128',
outputLength: 256,
domainSeparation: 0x1f,
}, {
outputLength: kUnsignedLongMax,
domainSeparation: kOctetMax,
}],
];

for (const [dictionary, base, members] of cases) {
const converter = converters[dictionary];
assertIdlDictionary(converter(base, opts), base);

for (const [member, max] of Object.entries(members)) {
assert.throws(
() => converter({ ...base, [member]: -1 }, opts), {
name: 'TypeError',
code: 'ERR_OUT_OF_RANGE',
message: `${prefix}: ${member} in ${context} is outside ` +
`the expected range of 0 to ${max}.`,
});
}
}
}

// DOMString
{
assert.strictEqual(converters.DOMString(1), '1');
Expand Down