Skip to content

Commit

Permalink
crypto: fix DiffieHellman argument validation
Browse files Browse the repository at this point in the history
Fixes: #37808

PR-URL: #37810
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
aduh95 committed Mar 23, 2021
1 parent 134fb5a commit 98ff5ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,20 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
if (typeof sizeOrKey !== 'number')
sizeOrKey = toBuf(sizeOrKey, keyEncoding);

if (!generator)
if (!generator) {
generator = DH_GENERATOR;
else if (typeof generator === 'number')
} else if (typeof generator === 'number') {
validateInt32(generator, 'generator');
else
} else if (generator !== true) {
generator = toBuf(generator, genEncoding);
} else {
throw new ERR_INVALID_ARG_TYPE(
'generator',
['number', 'string', 'ArrayBuffer', 'Buffer', 'TypedArray', 'DataView'],
generator
);
}


this[kHandle] = new _DiffieHellman(sizeOrKey, generator);
ObjectDefineProperty(this, 'verifyError', {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-crypto-dh.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,9 @@ assert.throws(
'crypto.getDiffieHellman(\'modp1\').setPublicKey(\'\') ' +
'failed to throw the expected error.'
);
assert.throws(
() => crypto.createDiffieHellman('', true),
{
code: 'ERR_INVALID_ARG_TYPE'
}
);

0 comments on commit 98ff5ac

Please sign in to comment.