Skip to content

Commit 20e97e5

Browse files
authored
crypto: cleanup validation
Many of the validations could be simplified and cleaned up by using validators and to keep consistency.
1 parent 82ae00c commit 20e97e5

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

lib/internal/crypto/keygen.js

Lines changed: 22 additions & 29 deletions
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,
44+
validateBuffer,
4645
validateCallback,
4746
validateString,
47+
validateInt32,
4848
validateInteger,
4949
validateObject,
5050
validateOneOf,
51+
validateUint32,
5152
} = require('internal/validators');
5253

5354
const {
@@ -172,16 +173,13 @@ function createJob(mode, type, options) {
172173
{
173174
validateObject(options, 'options');
174175
const { modulusLength } = options;
175-
if (!isUint32(modulusLength))
176-
throw new ERR_INVALID_ARG_VALUE('options.modulusLength', modulusLength);
176+
validateUint32(modulusLength, 'options.modulusLength');
177177

178178
let { publicExponent } = options;
179179
if (publicExponent == null) {
180180
publicExponent = 0x10001;
181-
} else if (!isUint32(publicExponent)) {
182-
throw new ERR_INVALID_ARG_VALUE(
183-
'options.publicExponent',
184-
publicExponent);
181+
} else {
182+
validateUint32(publicExponent, 'options.publicExponent');
185183
}
186184

187185
if (type === 'rsa') {
@@ -194,12 +192,12 @@ function createJob(mode, type, options) {
194192
}
195193

196194
const { hash, mgf1Hash, saltLength } = options;
197-
if (hash !== undefined && typeof hash !== 'string')
198-
throw new ERR_INVALID_ARG_VALUE('options.hash', hash);
199-
if (mgf1Hash !== undefined && typeof mgf1Hash !== 'string')
200-
throw new ERR_INVALID_ARG_VALUE('options.mgf1Hash', mgf1Hash);
201-
if (saltLength !== undefined && (!isInt32(saltLength) || saltLength < 0))
202-
throw new ERR_INVALID_ARG_VALUE('options.saltLength', saltLength);
195+
if (hash !== undefined)
196+
validateString(hash, 'options.hash');
197+
if (mgf1Hash !== undefined)
198+
validateString(mgf1Hash, 'options.mgf1Hash');
199+
if (saltLength !== undefined)
200+
validateInt32(saltLength, 'options.saltLength', 0);
203201

204202
return new RsaKeyPairGenJob(
205203
mode,
@@ -215,15 +213,13 @@ function createJob(mode, type, options) {
215213
{
216214
validateObject(options, 'options');
217215
const { modulusLength } = options;
218-
if (!isUint32(modulusLength))
219-
throw new ERR_INVALID_ARG_VALUE('options.modulusLength', modulusLength);
216+
validateUint32(modulusLength, 'options.modulusLength');
220217

221218
let { divisorLength } = options;
222219
if (divisorLength == null) {
223220
divisorLength = -1;
224-
} else if (!isInt32(divisorLength) || divisorLength < 0) {
225-
throw new ERR_INVALID_ARG_VALUE('options.divisorLength', divisorLength);
226-
}
221+
} else
222+
validateInt32(divisorLength, 'options.divisorLength', 0);
227223

228224
return new DsaKeyPairGenJob(
229225
mode,
@@ -235,8 +231,7 @@ function createJob(mode, type, options) {
235231
{
236232
validateObject(options, 'options');
237233
const { namedCurve } = options;
238-
if (typeof namedCurve !== 'string')
239-
throw new ERR_INVALID_ARG_VALUE('options.namedCurve', namedCurve);
234+
validateString(namedCurve, 'options.namedCurve');
240235
let { paramEncoding } = options;
241236
if (paramEncoding == null || paramEncoding === 'named')
242237
paramEncoding = OPENSSL_EC_NAMED_CURVE;
@@ -284,28 +279,26 @@ function createJob(mode, type, options) {
284279
throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength');
285280
if (generator != null)
286281
throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator');
287-
if (typeof group !== 'string')
288-
throw new ERR_INVALID_ARG_VALUE('options.group', group);
282+
283+
validateString(group, 'options.group');
289284

290285
return new DhKeyPairGenJob(mode, group, ...encoding);
291286
}
292287

293288
if (prime != null) {
294289
if (primeLength != null)
295290
throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');
296-
if (!isArrayBufferView(prime))
297-
throw new ERR_INVALID_ARG_VALUE('options.prime', prime);
291+
292+
validateBuffer(prime, 'options.prime');
298293
} else if (primeLength != null) {
299-
if (!isInt32(primeLength) || primeLength < 0)
300-
throw new ERR_INVALID_ARG_VALUE('options.primeLength', primeLength);
294+
validateInt32(primeLength, 'options.primeLength', 0);
301295
} else {
302296
throw new ERR_MISSING_OPTION(
303297
'At least one of the group, prime, or primeLength options');
304298
}
305299

306300
if (generator != null) {
307-
if (!isInt32(generator) || generator < 0)
308-
throw new ERR_INVALID_ARG_VALUE('options.generator', generator);
301+
validateInt32(generator, 'options.generator', 0);
309302
}
310303
return new DhKeyPairGenJob(
311304
mode,

0 commit comments

Comments
 (0)