diff options
author | Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> | 2022-04-04 13:38:35 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-04 13:38:35 +0300 |
commit | aed18dfe59cb47b36017d09a9e282b17ea658fcb (patch) | |
tree | 15c3bd52ab0ed5813bb55c5b50d0ea9c1812ff8c /lib | |
parent | 92567283f4027940d4a4df0e7fb7444b67842ed5 (diff) |
crypto: cleanup validation
Many of the validations could be simplified and cleaned up by using
validators and to keep consistency.
PR-URL: https://github.com/nodejs/node/pull/39841
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/crypto/keygen.js | 61 |
1 files changed, 26 insertions, 35 deletions
diff --git a/lib/internal/crypto/keygen.js b/lib/internal/crypto/keygen.js index a043a17f48d..c6909b227d0 100644 --- a/lib/internal/crypto/keygen.js +++ b/lib/internal/crypto/keygen.js @@ -41,13 +41,14 @@ const { const { customPromisifyArgs } = require('internal/util'); const { - isInt32, - isUint32, validateFunction, + validateBuffer, validateString, validateInteger, validateObject, validateOneOf, + validateInt32, + validateUint32, } = require('internal/validators'); const { @@ -174,16 +175,13 @@ function createJob(mode, type, options) { { validateObject(options, 'options'); const { modulusLength } = options; - if (!isUint32(modulusLength)) - throw new ERR_INVALID_ARG_VALUE('options.modulusLength', modulusLength); + validateUint32(modulusLength, 'options.modulusLength'); let { publicExponent } = options; if (publicExponent == null) { publicExponent = 0x10001; - } else if (!isUint32(publicExponent)) { - throw new ERR_INVALID_ARG_VALUE( - 'options.publicExponent', - publicExponent); + } else { + validateUint32(publicExponent, 'options.publicExponent'); } if (type === 'rsa') { @@ -201,22 +199,20 @@ function createJob(mode, type, options) { const pendingDeprecation = getOptionValue('--pending-deprecation'); - if (saltLength !== undefined && (!isInt32(saltLength) || saltLength < 0)) - throw new ERR_INVALID_ARG_VALUE('options.saltLength', saltLength); - if (hashAlgorithm !== undefined && typeof hashAlgorithm !== 'string') - throw new ERR_INVALID_ARG_VALUE('options.hashAlgorithm', hashAlgorithm); - if (mgf1HashAlgorithm !== undefined && - typeof mgf1HashAlgorithm !== 'string') - throw new ERR_INVALID_ARG_VALUE('options.mgf1HashAlgorithm', - mgf1HashAlgorithm); + if (saltLength !== undefined) + validateInt32(saltLength, 'options.saltLength', 0); + if (hashAlgorithm !== undefined) + validateString(hashAlgorithm, 'options.hashAlgorithm'); + if (mgf1HashAlgorithm !== undefined) + validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm'); if (hash !== undefined) { pendingDeprecation && process.emitWarning( '"options.hash" is deprecated, ' + 'use "options.hashAlgorithm" instead.', 'DeprecationWarning', 'DEP0154'); - if (typeof hash !== 'string' || - (hashAlgorithm && hash !== hashAlgorithm)) { + validateString(hash, 'options.hash'); + if (hashAlgorithm && hash !== hashAlgorithm) { throw new ERR_INVALID_ARG_VALUE('options.hash', hash); } } @@ -226,8 +222,8 @@ function createJob(mode, type, options) { 'use "options.mgf1HashAlgorithm" instead.', 'DeprecationWarning', 'DEP0154'); - if (typeof mgf1Hash !== 'string' || - (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm)) { + validateString(mgf1Hash, 'options.mgf1Hash'); + if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) { throw new ERR_INVALID_ARG_VALUE('options.mgf1Hash', mgf1Hash); } } @@ -246,15 +242,13 @@ function createJob(mode, type, options) { { validateObject(options, 'options'); const { modulusLength } = options; - if (!isUint32(modulusLength)) - throw new ERR_INVALID_ARG_VALUE('options.modulusLength', modulusLength); + validateUint32(modulusLength, 'options.modulusLength'); let { divisorLength } = options; if (divisorLength == null) { divisorLength = -1; - } else if (!isInt32(divisorLength) || divisorLength < 0) { - throw new ERR_INVALID_ARG_VALUE('options.divisorLength', divisorLength); - } + } else + validateInt32(divisorLength, 'options.divisorLength', 0); return new DsaKeyPairGenJob( mode, @@ -266,8 +260,7 @@ function createJob(mode, type, options) { { validateObject(options, 'options'); const { namedCurve } = options; - if (typeof namedCurve !== 'string') - throw new ERR_INVALID_ARG_VALUE('options.namedCurve', namedCurve); + validateString(namedCurve, 'options.namedCurve'); let { paramEncoding } = options; if (paramEncoding == null || paramEncoding === 'named') paramEncoding = OPENSSL_EC_NAMED_CURVE; @@ -315,8 +308,8 @@ function createJob(mode, type, options) { throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength'); if (generator != null) throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator'); - if (typeof group !== 'string') - throw new ERR_INVALID_ARG_VALUE('options.group', group); + + validateString(group, 'options.group'); return new DhKeyPairGenJob(mode, group, ...encoding); } @@ -324,19 +317,17 @@ function createJob(mode, type, options) { if (prime != null) { if (primeLength != null) throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength'); - if (!isArrayBufferView(prime)) - throw new ERR_INVALID_ARG_VALUE('options.prime', prime); + + validateBuffer(prime, 'options.prime'); } else if (primeLength != null) { - if (!isInt32(primeLength) || primeLength < 0) - throw new ERR_INVALID_ARG_VALUE('options.primeLength', primeLength); + validateInt32(primeLength, 'options.primeLength', 0); } else { throw new ERR_MISSING_OPTION( 'At least one of the group, prime, or primeLength options'); } if (generator != null) { - if (!isInt32(generator) || generator < 0) - throw new ERR_INVALID_ARG_VALUE('options.generator', generator); + validateInt32(generator, 'options.generator', 0); } return new DhKeyPairGenJob( mode, |