GeezerVison is showing github.com — crypto: add DH support to generateKeyPair · jmdfm/node@c6f8ea8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/crypto.md +23-6Lines changed: 23 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2093,6 +2093,9 @@ algorithm names. | 2093 | 2093 | <!-- YAML | 2094 | 2094 | added: v10.12.0 | 2095 | 2095 | changes: | | 2096 | + - version: REPLACEME | | 2097 | + pr-url: https://github.com/nodejs/node/pull/31178 | | 2098 | + description: Add support for Diffie-Hellman. | 2096 | 2099 | - version: v12.0.0 | 2097 | 2100 | pr-url: https://github.com/nodejs/node/pull/26774 | 2098 | 2101 | description: Add ability to generate X25519 and X448 key pairs. | @@ -2106,21 +2109,26 @@ changes: | 2106 | 2109 | --> | 2107 | 2110 |
| 2108 | 2111 | * `type`: {string} Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`, | 2109 | | - `'x25519'`, or `'x448'`. | | 2112 | + `'x25519'`, `'x448'`, or `'dh'`. | 2110 | 2113 | * `options`: {Object} | 2111 | 2114 | * `modulusLength`: {number} Key size in bits (RSA, DSA). | 2112 | 2115 | * `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`. | 2113 | 2116 | * `divisorLength`: {number} Size of `q` in bits (DSA). | 2114 | 2117 | * `namedCurve`: {string} Name of the curve to use (EC). | | 2118 | + * `prime`: {Buffer} The prime parameter (DH). | | 2119 | + * `primeLength`: {number} Prime length in bits (DH). | | 2120 | + * `generator`: {number} Custom generator (DH). **Default:** `2`. | | 2121 | + * `groupName`: {string} Diffie-Hellman group name (DH). See | | 2122 | + [`crypto.getDiffieHellman()`][]. | 2115 | 2123 | * `publicKeyEncoding`: {Object} See [`keyObject.export()`][]. | 2116 | 2124 | * `privateKeyEncoding`: {Object} See [`keyObject.export()`][]. | 2117 | 2125 | * `callback`: {Function} | 2118 | 2126 | * `err`: {Error} | 2119 | 2127 | * `publicKey`: {string | Buffer | KeyObject} | 2120 | 2128 | * `privateKey`: {string | Buffer | KeyObject} | 2121 | 2129 |
| 2122 | | -Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519 | 2123 | | -and Ed448 are currently supported. | | 2130 | +Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519, | | 2131 | +Ed448, X25519, X448, and DH are currently supported. | 2124 | 2132 |
| 2125 | 2133 | If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function | 2126 | 2134 | behaves as if [`keyObject.export()`][] had been called on its result. Otherwise, | @@ -2158,6 +2166,9 @@ a `Promise` for an `Object` with `publicKey` and `privateKey` properties. | 2158 | 2166 | <!-- YAML | 2159 | 2167 | added: v10.12.0 | 2160 | 2168 | changes: | | 2169 | + - version: REPLACEME | | 2170 | + pr-url: https://github.com/nodejs/node/pull/31178 | | 2171 | + description: Add support for Diffie-Hellman. | 2161 | 2172 | - version: v12.0.0 | 2162 | 2173 | pr-url: https://github.com/nodejs/node/pull/26554 | 2163 | 2174 | description: Add ability to generate Ed25519 and Ed448 key pairs. | @@ -2167,20 +2178,26 @@ changes: | 2167 | 2178 | produce key objects if no encoding was specified. | 2168 | 2179 | --> | 2169 | 2180 |
| 2170 | | -* `type`: {string} Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, or `'ed448'`. | | 2181 | +* `type`: {string} Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`, | | 2182 | + `'x25519'`, `'x448'`, or `'dh'`. | 2171 | 2183 | * `options`: {Object} | 2172 | 2184 | * `modulusLength`: {number} Key size in bits (RSA, DSA). | 2173 | 2185 | * `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`. | 2174 | 2186 | * `divisorLength`: {number} Size of `q` in bits (DSA). | 2175 | 2187 | * `namedCurve`: {string} Name of the curve to use (EC). | | 2188 | + * `prime`: {Buffer} The prime parameter (DH). | | 2189 | + * `primeLength`: {number} Prime length in bits (DH). | | 2190 | + * `generator`: {number} Custom generator (DH). **Default:** `2`. | | 2191 | + * `groupName`: {string} Diffie-Hellman group name (DH). See | | 2192 | + [`crypto.getDiffieHellman()`][]. | 2176 | 2193 | * `publicKeyEncoding`: {Object} See [`keyObject.export()`][]. | 2177 | 2194 | * `privateKeyEncoding`: {Object} See [`keyObject.export()`][]. | 2178 | 2195 | * Returns: {Object} | 2179 | 2196 | * `publicKey`: {string | Buffer | KeyObject} | 2180 | 2197 | * `privateKey`: {string | Buffer | KeyObject} | 2181 | 2198 |
| 2182 | | -Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519 | 2183 | | -and Ed448 are currently supported. | | 2199 | +Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519, | | 2200 | +Ed448, X25519, X448, and DH are currently supported. | 2184 | 2201 |
| 2185 | 2202 | If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function | 2186 | 2203 | behaves as if [`keyObject.export()`][] had been called on its result. Otherwise, | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/errors.md +13Lines changed: 13 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -826,6 +826,12 @@ A signing `key` was not provided to the [`sign.sign()`][] method. | 826 | 826 | [`crypto.timingSafeEqual()`][] was called with `Buffer`, `TypedArray`, or | 827 | 827 | `DataView` arguments of different lengths. | 828 | 828 |
| | 829 | +<a id="ERR_CRYPTO_UNKNOWN_DH_GROUP"></a> | | 830 | +### `ERR_CRYPTO_UNKNOWN_DH_GROUP` | | 831 | + | | 832 | +An unknown Diffie-Hellman group name was given. See | | 833 | +[`crypto.getDiffieHellman()`][] for a list of valid group names. | | 834 | + | 829 | 835 | <a id="ERR_DIR_CLOSED"></a> | 830 | 836 | ### `ERR_DIR_CLOSED` | 831 | 837 |
| @@ -1514,6 +1520,12 @@ strict compliance with the API specification (which in some cases may accept | 1514 | 1520 | An [ES Module][] loader hook specified `format: 'dynamic'` but did not provide | 1515 | 1521 | a `dynamicInstantiate` hook. | 1516 | 1522 |
| | 1523 | +<a id="ERR_MISSING_OPTION"></a> | | 1524 | +### `ERR_MISSING_OPTION` | | 1525 | + | | 1526 | +For APIs that accept options objects, some options might be mandatory. This code | | 1527 | +is thrown if a required option is missing. | | 1528 | + | 1517 | 1529 | <a id="ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST"></a> | 1518 | 1530 | ### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` | 1519 | 1531 |
| @@ -2423,6 +2435,7 @@ such as `process.stdout.on('data')`. | 2423 | 2435 | [`Writable`]: stream.html#stream_class_stream_writable | 2424 | 2436 | [`child_process`]: child_process.html | 2425 | 2437 | [`cipher.getAuthTag()`]: crypto.html#crypto_cipher_getauthtag | | 2438 | +[`crypto.getDiffieHellman()`]: crypto.html#crypto_crypto_getdiffiehellman_groupname | 2426 | 2439 | [`crypto.scrypt()`]: crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback | 2427 | 2440 | [`crypto.scryptSync()`]: crypto.html#crypto_crypto_scryptsync_password_salt_keylen_options | 2428 | 2441 | [`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/keygen.js +47-1Lines changed: 47 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ const { | 11 | 11 | generateKeyPairDSA, | 12 | 12 | generateKeyPairEC, | 13 | 13 | generateKeyPairNid, | | 14 | + generateKeyPairDH, | 14 | 15 | EVP_PKEY_ED25519, | 15 | 16 | EVP_PKEY_ED448, | 16 | 17 | EVP_PKEY_X25519, | @@ -28,10 +29,12 @@ const { | 28 | 29 | const { customPromisifyArgs } = require('internal/util'); | 29 | 30 | const { isUint32, validateString } = require('internal/validators'); | 30 | 31 | const { | | 32 | + ERR_INCOMPATIBLE_OPTION_PAIR, | 31 | 33 | ERR_INVALID_ARG_TYPE, | 32 | 34 | ERR_INVALID_ARG_VALUE, | 33 | 35 | ERR_INVALID_CALLBACK, | 34 | | - ERR_INVALID_OPT_VALUE | | 36 | + ERR_INVALID_OPT_VALUE, | | 37 | + ERR_MISSING_OPTION | 35 | 38 | } = require('internal/errors').codes; | 36 | 39 |
| 37 | 40 | const { isArrayBufferView } = require('internal/util/types'); | @@ -245,6 +248,49 @@ function check(type, options, callback) { | 245 | 248 | cipher, passphrase, wrap); | 246 | 249 | } | 247 | 250 | break; | | 251 | + case 'dh': | | 252 | + { | | 253 | + const { group, primeLength, prime, generator } = needOptions(); | | 254 | + let args; | | 255 | + if (group != null) { | | 256 | + if (prime != null) | | 257 | + throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'prime'); | | 258 | + if (primeLength != null) | | 259 | + throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength'); | | 260 | + if (generator != null) | | 261 | + throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator'); | | 262 | + if (typeof group !== 'string') | | 263 | + throw new ERR_INVALID_OPT_VALUE('group', group); | | 264 | + args = [group]; | | 265 | + } else { | | 266 | + if (prime != null) { | | 267 | + if (primeLength != null) | | 268 | + throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength'); | | 269 | + if (!isArrayBufferView(prime)) | | 270 | + throw new ERR_INVALID_OPT_VALUE('prime', prime); | | 271 | + } else if (primeLength != null) { | | 272 | + if (!isUint32(primeLength)) | | 273 | + throw new ERR_INVALID_OPT_VALUE('primeLength', primeLength); | | 274 | + } else { | | 275 | + throw new ERR_MISSING_OPTION( | | 276 | + 'At least one of the group, prime, or primeLength options'); | | 277 | + } | | 278 | + | | 279 | + if (generator != null) { | | 280 | + if (!isUint32(generator)) | | 281 | + throw new ERR_INVALID_OPT_VALUE('generator', generator); | | 282 | + } | | 283 | + | | 284 | + args = [prime != null ? prime : primeLength, | | 285 | + generator == null ? 2 : generator]; | | 286 | + } | | 287 | + | | 288 | + impl = (wrap) => generateKeyPairDH(...args, | | 289 | + publicFormat, publicType, | | 290 | + privateFormat, privateType, | | 291 | + cipher, passphrase, wrap); | | 292 | + } | | 293 | + break; | 248 | 294 | default: | 249 | 295 | throw new ERR_INVALID_ARG_VALUE('type', type, | 250 | 296 | 'must be a supported key type'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1187,6 +1187,7 @@ E('ERR_MISSING_ARGS', | 1187 | 1187 | E('ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK', | 1188 | 1188 | 'The ES Module loader may not return a format of \'dynamic\' when no ' + | 1189 | 1189 | 'dynamicInstantiate function was provided', Error); | | 1190 | +E('ERR_MISSING_OPTION', '%s is required', TypeError); | 1190 | 1191 | E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error); | 1191 | 1192 | E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError); | 1192 | 1193 | E('ERR_NAPI_INVALID_DATAVIEW_ARGS', | |
Collapse file Copy file name to clipboardExpand all lines: src/node_crypto.cc +99Lines changed: 99 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6625,6 +6625,71 @@ class NidKeyPairGenerationConfig : public KeyPairGenerationConfig { | 6625 | 6625 | const int id_; | 6626 | 6626 | }; | 6627 | 6627 |
| | 6628 | +// TODO(tniessen): Use std::variant instead. | | 6629 | +// Diffie-Hellman can either generate keys using a fixed prime, or by first | | 6630 | +// generating a random prime of a given size (in bits). Only one of both options | | 6631 | +// may be specified. | | 6632 | +struct PrimeInfo { | | 6633 | + BignumPointer fixed_value_; | | 6634 | + unsigned int prime_size_; | | 6635 | +}; | | 6636 | + | | 6637 | +class DHKeyPairGenerationConfig : public KeyPairGenerationConfig { | | 6638 | + public: | | 6639 | + explicit DHKeyPairGenerationConfig(PrimeInfo&& prime_info, | | 6640 | + unsigned int generator) | | 6641 | + : prime_info_(std::move(prime_info)), | | 6642 | + generator_(generator) {} | | 6643 | + | | 6644 | + EVPKeyCtxPointer Setup() override { | | 6645 | + EVPKeyPointer params; | | 6646 | + if (prime_info_.fixed_value_) { | | 6647 | + DHPointer dh(DH_new()); | | 6648 | + if (!dh) | | 6649 | + return nullptr; | | 6650 | + | | 6651 | + BIGNUM* prime = prime_info_.fixed_value_.get(); | | 6652 | + BignumPointer bn_g(BN_new()); | | 6653 | + if (!BN_set_word(bn_g.get(), generator_) || | | 6654 | + !DH_set0_pqg(dh.get(), prime, nullptr, bn_g.get())) | | 6655 | + return nullptr; | | 6656 | + | | 6657 | + prime_info_.fixed_value_.release(); | | 6658 | + bn_g.release(); | | 6659 | + | | 6660 | + params = EVPKeyPointer(EVP_PKEY_new()); | | 6661 | + CHECK(params); | | 6662 | + EVP_PKEY_assign_DH(params.get(), dh.release()); | | 6663 | + } else { | | 6664 | + EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DH, nullptr)); | | 6665 | + if (!param_ctx) | | 6666 | + return nullptr; | | 6667 | + | | 6668 | + if (EVP_PKEY_paramgen_init(param_ctx.get()) <= 0) | | 6669 | + return nullptr; | | 6670 | + | | 6671 | + if (EVP_PKEY_CTX_set_dh_paramgen_prime_len(param_ctx.get(), | | 6672 | + prime_info_.prime_size_) <= 0) | | 6673 | + return nullptr; | | 6674 | + | | 6675 | + if (EVP_PKEY_CTX_set_dh_paramgen_generator(param_ctx.get(), | | 6676 | + generator_) <= 0) | | 6677 | + return nullptr; | | 6678 | + | | 6679 | + EVP_PKEY* raw_params = nullptr; | | 6680 | + if (EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0) | | 6681 | + return nullptr; | | 6682 | + params = EVPKeyPointer(raw_params); | | 6683 | + } | | 6684 | + | | 6685 | + return EVPKeyCtxPointer(EVP_PKEY_CTX_new(params.get(), nullptr)); | | 6686 | + } | | 6687 | + | | 6688 | + private: | | 6689 | + PrimeInfo prime_info_; | | 6690 | + unsigned int generator_; | | 6691 | +}; | | 6692 | + | 6628 | 6693 | class GenerateKeyPairJob : public CryptoJob { | 6629 | 6694 | public: | 6630 | 6695 | GenerateKeyPairJob(Environment* env, | @@ -6844,6 +6909,39 @@ void GenerateKeyPairNid(const FunctionCallbackInfo<Value>& args) { | 6844 | 6909 | GenerateKeyPair(args, 1, std::move(config)); | 6845 | 6910 | } | 6846 | 6911 |
| | 6912 | +void GenerateKeyPairDH(const FunctionCallbackInfo<Value>& args) { | | 6913 | + Environment* env = Environment::GetCurrent(args); | | 6914 | + | | 6915 | + PrimeInfo prime_info = {}; | | 6916 | + unsigned int generator; | | 6917 | + if (args[0]->IsString()) { | | 6918 | + String::Utf8Value group_name(args.GetIsolate(), args[0].As<String>()); | | 6919 | + const modp_group* group = FindDiffieHellmanGroup(*group_name); | | 6920 | + if (group == nullptr) | | 6921 | + return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env); | | 6922 | + | | 6923 | + prime_info.fixed_value_ = BignumPointer( | | 6924 | + BN_bin2bn(reinterpret_cast<const unsigned char*>(group->prime), | | 6925 | + group->prime_size, nullptr)); | | 6926 | + generator = group->gen; | | 6927 | + } else { | | 6928 | + if (args[0]->IsInt32()) { | | 6929 | + prime_info.prime_size_ = args[0].As<Int32>()->Value(); | | 6930 | + } else { | | 6931 | + ArrayBufferViewContents<unsigned char> input(args[0]); | | 6932 | + prime_info.fixed_value_ = BignumPointer( | | 6933 | + BN_bin2bn(input.data(), input.length(), nullptr)); | | 6934 | + } | | 6935 | + | | 6936 | + CHECK(args[1]->IsInt32()); | | 6937 | + generator = args[1].As<Int32>()->Value(); | | 6938 | + } | | 6939 | + | | 6940 | + std::unique_ptr<KeyPairGenerationConfig> config( | | 6941 | + new DHKeyPairGenerationConfig(std::move(prime_info), generator)); | | 6942 | + GenerateKeyPair(args, 2, std::move(config)); | | 6943 | +} | | 6944 | + | 6847 | 6945 |
| 6848 | 6946 | void GetSSLCiphers(const FunctionCallbackInfo<Value>& args) { | 6849 | 6947 | Environment* env = Environment::GetCurrent(args); | @@ -7250,6 +7348,7 @@ void Initialize(Local<Object> target, | 7250 | 7348 | env->SetMethod(target, "generateKeyPairDSA", GenerateKeyPairDSA); | 7251 | 7349 | env->SetMethod(target, "generateKeyPairEC", GenerateKeyPairEC); | 7252 | 7350 | env->SetMethod(target, "generateKeyPairNid", GenerateKeyPairNid); | | 7351 | + env->SetMethod(target, "generateKeyPairDH", GenerateKeyPairDH); | 7253 | 7352 | NODE_DEFINE_CONSTANT(target, EVP_PKEY_ED25519); | 7254 | 7353 | NODE_DEFINE_CONSTANT(target, EVP_PKEY_ED448); | 7255 | 7354 | NODE_DEFINE_CONSTANT(target, EVP_PKEY_X25519); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.h +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ void PrintErrorString(const char* format, ...); | 39 | 39 | V(ERR_BUFFER_TOO_LARGE, Error) \ | 40 | 40 | V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \ | 41 | 41 | V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \ | | 42 | + V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \ | 42 | 43 | V(ERR_INVALID_ARG_VALUE, TypeError) \ | 43 | 44 | V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \ | 44 | 45 | V(ERR_INVALID_ARG_TYPE, TypeError) \ | @@ -89,6 +90,7 @@ void PrintErrorString(const char* format, ...); | 89 | 90 | "Buffer is not available for the current Context") \ | 90 | 91 | V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ | 91 | 92 | V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ | | 93 | + V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \ | 92 | 94 | V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ | 93 | 95 | V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ | 94 | 96 | V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \ | |
|