Skip to content

Commit aed18df

Browse files
authored
crypto: cleanup validation
Many of the validations could be simplified and cleaned up by using validators and to keep consistency. PR-URL: #39841 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 9256728 commit aed18df

File tree

2 files changed

+210
-78
lines changed

2 files changed

+210
-78
lines changed

lib/internal/crypto/keygen.js

+26-35
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ const {
4141
const { customPromisifyArgs } = require('internal/util');
4242

4343
const {
44-
isInt32,
45-
isUint32,
4644
validateFunction,
45+
validateBuffer,
4746
validateString,
4847
validateInteger,
4948
validateObject,
5049
validateOneOf,
50+
validateInt32,
51+
validateUint32,
5152
} = require('internal/validators');
5253

5354
const {
@@ -174,16 +175,13 @@ function createJob(mode, type, options) {
174175
{
175176
validateObject(options, 'options');
176177
const { modulusLength } = options;
177-
if (!isUint32(modulusLength))
178-
throw new ERR_INVALID_ARG_VALUE('options.modulusLength', modulusLength);
178+
validateUint32(modulusLength, 'options.modulusLength');
179179

180180
let { publicExponent } = options;
181181
if (publicExponent == null) {
182182
publicExponent = 0x10001;
183-
} else if (!isUint32(publicExponent)) {
184-
throw new ERR_INVALID_ARG_VALUE(
185-
'options.publicExponent',
186-
publicExponent);
183+
} else {
184+
validateUint32(publicExponent, 'options.publicExponent');
187185
}
188186

189187
if (type === 'rsa') {
@@ -201,22 +199,20 @@ function createJob(mode, type, options) {
201199

202200
const pendingDeprecation = getOptionValue('--pending-deprecation');
203201

204-
if (saltLength !== undefined && (!isInt32(saltLength) || saltLength < 0))
205-
throw new ERR_INVALID_ARG_VALUE('options.saltLength', saltLength);
206-
if (hashAlgorithm !== undefined && typeof hashAlgorithm !== 'string')
207-
throw new ERR_INVALID_ARG_VALUE('options.hashAlgorithm', hashAlgorithm);
208-
if (mgf1HashAlgorithm !== undefined &&
209-
typeof mgf1HashAlgorithm !== 'string')
210-
throw new ERR_INVALID_ARG_VALUE('options.mgf1HashAlgorithm',
211-
mgf1HashAlgorithm);
202+
if (saltLength !== undefined)
203+
validateInt32(saltLength, 'options.saltLength', 0);
204+
if (hashAlgorithm !== undefined)
205+
validateString(hashAlgorithm, 'options.hashAlgorithm');
206+
if (mgf1HashAlgorithm !== undefined)
207+
validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm');
212208
if (hash !== undefined) {
213209
pendingDeprecation && process.emitWarning(
214210
'"options.hash" is deprecated, ' +
215211
'use "options.hashAlgorithm" instead.',
216212
'DeprecationWarning',
217213
'DEP0154');
218-
if (typeof hash !== 'string' ||
219-
(hashAlgorithm && hash !== hashAlgorithm)) {
214+
validateString(hash, 'options.hash');
215+
if (hashAlgorithm && hash !== hashAlgorithm) {
220216
throw new ERR_INVALID_ARG_VALUE('options.hash', hash);
221217
}
222218
}
@@ -226,8 +222,8 @@ function createJob(mode, type, options) {
226222
'use "options.mgf1HashAlgorithm" instead.',
227223
'DeprecationWarning',
228224
'DEP0154');
229-
if (typeof mgf1Hash !== 'string' ||
230-
(mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm)) {
225+
validateString(mgf1Hash, 'options.mgf1Hash');
226+
if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {
231227
throw new ERR_INVALID_ARG_VALUE('options.mgf1Hash', mgf1Hash);
232228
}
233229
}
@@ -246,15 +242,13 @@ function createJob(mode, type, options) {
246242
{
247243
validateObject(options, 'options');
248244
const { modulusLength } = options;
249-
if (!isUint32(modulusLength))
250-
throw new ERR_INVALID_ARG_VALUE('options.modulusLength', modulusLength);
245+
validateUint32(modulusLength, 'options.modulusLength');
251246

252247
let { divisorLength } = options;
253248
if (divisorLength == null) {
254249
divisorLength = -1;
255-
} else if (!isInt32(divisorLength) || divisorLength < 0) {
256-
throw new ERR_INVALID_ARG_VALUE('options.divisorLength', divisorLength);
257-
}
250+
} else
251+
validateInt32(divisorLength, 'options.divisorLength', 0);
258252

259253
return new DsaKeyPairGenJob(
260254
mode,
@@ -266,8 +260,7 @@ function createJob(mode, type, options) {
266260
{
267261
validateObject(options, 'options');
268262
const { namedCurve } = options;
269-
if (typeof namedCurve !== 'string')
270-
throw new ERR_INVALID_ARG_VALUE('options.namedCurve', namedCurve);
263+
validateString(namedCurve, 'options.namedCurve');
271264
let { paramEncoding } = options;
272265
if (paramEncoding == null || paramEncoding === 'named')
273266
paramEncoding = OPENSSL_EC_NAMED_CURVE;
@@ -315,28 +308,26 @@ function createJob(mode, type, options) {
315308
throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength');
316309
if (generator != null)
317310
throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator');
318-
if (typeof group !== 'string')
319-
throw new ERR_INVALID_ARG_VALUE('options.group', group);
311+
312+
validateString(group, 'options.group');
320313

321314
return new DhKeyPairGenJob(mode, group, ...encoding);
322315
}
323316

324317
if (prime != null) {
325318
if (primeLength != null)
326319
throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');
327-
if (!isArrayBufferView(prime))
328-
throw new ERR_INVALID_ARG_VALUE('options.prime', prime);
320+
321+
validateBuffer(prime, 'options.prime');
329322
} else if (primeLength != null) {
330-
if (!isInt32(primeLength) || primeLength < 0)
331-
throw new ERR_INVALID_ARG_VALUE('options.primeLength', primeLength);
323+
validateInt32(primeLength, 'options.primeLength', 0);
332324
} else {
333325
throw new ERR_MISSING_OPTION(
334326
'At least one of the group, prime, or primeLength options');
335327
}
336328

337329
if (generator != null) {
338-
if (!isInt32(generator) || generator < 0)
339-
throw new ERR_INVALID_ARG_VALUE('options.generator', generator);
330+
validateInt32(generator, 'options.generator', 0);
340331
}
341332
return new DhKeyPairGenJob(
342333
mode,

0 commit comments

Comments
 (0)