summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTobias Nießen <tniessen@tnie.de>2019-12-31 02:12:36 +0100
committerShelley Vohr <shelley.vohr@gmail.com>2020-02-17 10:34:59 -0800
commit0d3e095941ed812f0300a6c4a3424e329d03a74a (patch)
tree6e8f04f8f31ddea7ed6170ece16b6c0bb9b67ced /lib
parent15bd2c9f0c0f47c62fd322a5ad79139ad6cb5b20 (diff)
downloadnode-new-0d3e095941ed812f0300a6c4a3424e329d03a74a.tar.gz
crypto: add DH support to generateKeyPair
This allows using the generateKeyPair API for DH instead of the old stateful DH APIs. PR-URL: https://github.com/nodejs/node/pull/31178 Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/crypto/keygen.js48
-rw-r--r--lib/internal/errors.js1
2 files changed, 48 insertions, 1 deletions
diff --git a/lib/internal/crypto/keygen.js b/lib/internal/crypto/keygen.js
index 88d2822fa6..ced1a0608f 100644
--- a/lib/internal/crypto/keygen.js
+++ b/lib/internal/crypto/keygen.js
@@ -11,6 +11,7 @@ const {
generateKeyPairDSA,
generateKeyPairEC,
generateKeyPairNid,
+ generateKeyPairDH,
EVP_PKEY_ED25519,
EVP_PKEY_ED448,
EVP_PKEY_X25519,
@@ -28,10 +29,12 @@ const {
const { customPromisifyArgs } = require('internal/util');
const { isUint32, validateString } = require('internal/validators');
const {
+ ERR_INCOMPATIBLE_OPTION_PAIR,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CALLBACK,
- ERR_INVALID_OPT_VALUE
+ ERR_INVALID_OPT_VALUE,
+ ERR_MISSING_OPTION
} = require('internal/errors').codes;
const { isArrayBufferView } = require('internal/util/types');
@@ -245,6 +248,49 @@ function check(type, options, callback) {
cipher, passphrase, wrap);
}
break;
+ case 'dh':
+ {
+ const { group, primeLength, prime, generator } = needOptions();
+ let args;
+ if (group != null) {
+ if (prime != null)
+ throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'prime');
+ if (primeLength != null)
+ 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_OPT_VALUE('group', group);
+ args = [group];
+ } else {
+ if (prime != null) {
+ if (primeLength != null)
+ throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');
+ if (!isArrayBufferView(prime))
+ throw new ERR_INVALID_OPT_VALUE('prime', prime);
+ } else if (primeLength != null) {
+ if (!isUint32(primeLength))
+ throw new ERR_INVALID_OPT_VALUE('primeLength', primeLength);
+ } else {
+ throw new ERR_MISSING_OPTION(
+ 'At least one of the group, prime, or primeLength options');
+ }
+
+ if (generator != null) {
+ if (!isUint32(generator))
+ throw new ERR_INVALID_OPT_VALUE('generator', generator);
+ }
+
+ args = [prime != null ? prime : primeLength,
+ generator == null ? 2 : generator];
+ }
+
+ impl = (wrap) => generateKeyPairDH(...args,
+ publicFormat, publicType,
+ privateFormat, privateType,
+ cipher, passphrase, wrap);
+ }
+ break;
default:
throw new ERR_INVALID_ARG_VALUE('type', type,
'must be a supported key type');
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 206724eacb..936a5253c7 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -1187,6 +1187,7 @@ E('ERR_MISSING_ARGS',
E('ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK',
'The ES Module loader may not return a format of \'dynamic\' when no ' +
'dynamicInstantiate function was provided', Error);
+E('ERR_MISSING_OPTION', '%s is required', TypeError);
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError);
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',