src: refactor SubtleCrypto algorithm and length validations · nodejs/node@b10ac9a · GitHub
Skip to content

Commit b10ac9a

Browse files
panvaaduh95
authored andcommitted
src: refactor SubtleCrypto algorithm and length validations
PR-URL: #57273 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com>
1 parent 90cd780 commit b10ac9a

17 files changed

Lines changed: 208 additions & 180 deletions

lib/internal/crypto/aes.js

Lines changed: 39 additions & 27 deletions

lib/internal/crypto/cfrg.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,21 @@ function cfrgImportKey(
329329
extractable);
330330
}
331331

332-
function eddsaSignVerify(key, data, { name, context }, signature) {
332+
function validateEdDSASignVerifyAlgorithm(algorithm) {
333+
if (algorithm.name === 'Ed448' && algorithm.context?.byteLength) {
334+
throw lazyDOMException(
335+
'Non zero-length context is not yet supported.', 'NotSupportedError');
336+
}
337+
}
338+
339+
function eddsaSignVerify(key, data, algorithm, signature) {
340+
validateEdDSASignVerifyAlgorithm(algorithm);
333341
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
334342
const type = mode === kSignJobModeSign ? 'private' : 'public';
335343

336344
if (key.type !== type)
337345
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
338346

339-
if (name === 'Ed448' && context?.byteLength) {
340-
throw lazyDOMException(
341-
'Non zero-length context is not yet supported.', 'NotSupportedError');
342-
}
343-
344347
return jobPromise(() => new SignJob(
345348
kCryptoJobAsync,
346349
mode,

lib/internal/crypto/diffiehellman.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,28 +298,28 @@ function diffieHellman(options) {
298298

299299
let masks;
300300

301+
function validateEcdhDeriveBitsAlgorithmAndLength(algorithm, length) {
302+
if (algorithm.public.type !== 'public') {
303+
throw lazyDOMException(
304+
'algorithm.public must be a public key', 'InvalidAccessError');
305+
}
306+
307+
if (algorithm.name !== algorithm.public.algorithm.name) {
308+
throw lazyDOMException(`algorithm.public must be an ${algorithm.name} key`, 'InvalidAccessError');
309+
}
310+
}
311+
301312
// The ecdhDeriveBits function is part of the Web Crypto API and serves both
302313
// deriveKeys and deriveBits functions.
303314
async function ecdhDeriveBits(algorithm, baseKey, length) {
315+
validateEcdhDeriveBitsAlgorithmAndLength(algorithm, length);
304316
const { 'public': key } = algorithm;
305317

306-
if (key.type !== 'public') {
307-
throw lazyDOMException(
308-
'algorithm.public must be a public key', 'InvalidAccessError');
309-
}
310318
if (baseKey.type !== 'private') {
311319
throw lazyDOMException(
312320
'baseKey must be a private key', 'InvalidAccessError');
313321
}
314322

315-
if (
316-
key.algorithm.name !== 'ECDH' &&
317-
key.algorithm.name !== 'X25519' &&
318-
key.algorithm.name !== 'X448'
319-
) {
320-
throw lazyDOMException('Keys must be ECDH, X25519, or X448 keys', 'InvalidAccessError');
321-
}
322-
323323
if (key.algorithm.name !== baseKey.algorithm.name) {
324324
throw lazyDOMException(
325325
'The public and private keys must be of the same type',

lib/internal/crypto/ec.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

33
const {
4-
ArrayPrototypeIncludes,
5-
ObjectKeys,
4+
ObjectPrototypeHasOwnProperty,
65
SafeSet,
76
} = primordials;
87

@@ -77,14 +76,17 @@ function createECPublicKeyRaw(namedCurve, keyData) {
7776
return new PublicKeyObject(handle);
7877
}
7978

80-
async function ecGenerateKey(algorithm, extractable, keyUsages) {
81-
const { name, namedCurve } = algorithm;
82-
83-
if (!ArrayPrototypeIncludes(ObjectKeys(kNamedCurveAliases), namedCurve)) {
79+
function validateEcKeyAlgorithm(algorithm) {
80+
if (!ObjectPrototypeHasOwnProperty(kNamedCurveAliases, algorithm.namedCurve)) {
8481
throw lazyDOMException(
8582
'Unrecognized namedCurve',
8683
'NotSupportedError');
8784
}
85+
}
86+
87+
async function ecGenerateKey(algorithm, extractable, keyUsages) {
88+
validateEcKeyAlgorithm(algorithm);
89+
const { name, namedCurve } = algorithm;
8890

8991
const usageSet = new SafeSet(keyUsages);
9092
switch (name) {
@@ -154,16 +156,11 @@ function ecImportKey(
154156
keyData,
155157
algorithm,
156158
extractable,
157-
keyUsages) {
158-
159+
keyUsages,
160+
) {
161+
validateEcKeyAlgorithm(algorithm);
159162
const { name, namedCurve } = algorithm;
160163

161-
if (!ArrayPrototypeIncludes(ObjectKeys(kNamedCurveAliases), namedCurve)) {
162-
throw lazyDOMException(
163-
'Unrecognized namedCurve',
164-
'NotSupportedError');
165-
}
166-
167164
let keyObject;
168165
const usagesSet = new SafeSet(keyUsages);
169166
switch (format) {

lib/internal/crypto/hkdf.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,22 @@ function hkdfSync(hash, key, salt, info, length) {
138138
}
139139

140140
const hkdfPromise = promisify(hkdf);
141-
async function hkdfDeriveBits(algorithm, baseKey, length) {
142-
const { hash, salt, info } = algorithm;
143-
144-
if (length === 0)
145-
return new ArrayBuffer(0);
141+
function validateHkdfDeriveBitsAlgorithmAndLength(algorithm, length) {
146142
if (length === null)
147143
throw lazyDOMException('length cannot be null', 'OperationError');
148144
if (length % 8) {
149145
throw lazyDOMException(
150146
'length must be a multiple of 8',
151147
'OperationError');
152148
}
149+
}
150+
151+
async function hkdfDeriveBits(algorithm, baseKey, length) {
152+
validateHkdfDeriveBitsAlgorithmAndLength(algorithm, length);
153+
const { hash, salt, info } = algorithm;
154+
155+
if (length === 0)
156+
return new ArrayBuffer(0);
153157

154158
try {
155159
return await hkdfPromise(

lib/internal/crypto/keys.js

Lines changed: 13 additions & 36 deletions

0 commit comments

Comments
 (0)