summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2020-04-10 12:42:22 +0200
committerAnna Henningsen <anna@addaleax.net>2020-04-28 19:22:02 +0200
commit6fdced46db1e86728705b525dc0d2d49c214494e (patch)
treefc1c12531cfb38408cb084461aad60e754052d9d /lib
parent73324cf76a1898d45d887a44145e7ab12236c076 (diff)
downloadnode-new-6fdced46db1e86728705b525dc0d2d49c214494e.tar.gz
crypto: key size must be int32 in DiffieHellman()
The JS code accepted any value where `typeof sizeOrKey === 'number'` was true but the C++ code checked that `args[0]->IsInt32()` and subsequently aborted. Fixes: https://github.com/nodejs/node/issues/32738 PR-URL: https://github.com/nodejs/node/pull/32739 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/crypto/diffiehellman.js12
1 files changed, 11 insertions, 1 deletions
diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js
index ae6b68b73b..70e4100d50 100644
--- a/lib/internal/crypto/diffiehellman.js
+++ b/lib/internal/crypto/diffiehellman.js
@@ -14,7 +14,10 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
-const { validateString } = require('internal/validators');
+const {
+ validateString,
+ validateInt32,
+} = require('internal/validators');
const { isArrayBufferView } = require('internal/util/types');
const { KeyObject } = require('internal/crypto/keys');
const {
@@ -51,6 +54,13 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
);
}
+ // Sizes < 0 don't make sense but they _are_ accepted (and subsequently
+ // rejected with ERR_OSSL_BN_BITS_TOO_SMALL) by OpenSSL. The glue code
+ // in node_crypto.cc accepts values that are IsInt32() for that reason
+ // and that's why we do that here too.
+ if (typeof sizeOrKey === 'number')
+ validateInt32(sizeOrKey, 'sizeOrKey');
+
if (keyEncoding && !Buffer.isEncoding(keyEncoding) &&
keyEncoding !== 'buffer') {
genEncoding = generator;