Skip to content

Commit

Permalink
crypto: generator must be int32 in DiffieHellman()
Browse files Browse the repository at this point in the history
Validate the generator argument in `crypto.createDiffieHellman(key, g)`.
When it's a number, it should be an int32.

Fixes: #32748

PR-URL: #32739
Fixes: #32738
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
bnoordhuis authored and targos committed May 13, 2020
1 parent 4236175 commit c1b7674
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {

if (!generator)
generator = DH_GENERATOR;
else if (typeof generator !== 'number')
else if (typeof generator === 'number')
validateInt32(generator, 'generator');
else
generator = toBuf(generator, genEncoding);

this[kHandle] = new _DiffieHellman(sizeOrKey, generator);
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-crypto-dh.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ assert.throws(() => crypto.createDiffieHellman(13.37), {
'It must be an integer. Received 13.37',
});

assert.throws(() => crypto.createDiffieHellman('abcdef', 13.37), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "generator" is out of range. ' +
'It must be an integer. Received 13.37',
});

for (const bits of [-1, 0, 1]) {
assert.throws(() => crypto.createDiffieHellman(bits), {
code: 'ERR_OSSL_BN_BITS_TOO_SMALL',
Expand Down

0 comments on commit c1b7674

Please sign in to comment.