From 978cd0227f2c4cfd48a2454df6aa70db868254f0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 7 Jan 2021 16:27:29 -0800 Subject: [PATCH 1/2] crypto: fixup bug in keygen error handling Signed-off-by: James M Snell --- lib/internal/crypto/keygen.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/crypto/keygen.js b/lib/internal/crypto/keygen.js index 2b0d58afd663fc..56f06f7a79fe2a 100644 --- a/lib/internal/crypto/keygen.js +++ b/lib/internal/crypto/keygen.js @@ -94,13 +94,15 @@ function generateKeyPairSync(type, options) { } function handleError(ret) { - if (ret === undefined) + if (ret == null) return; // async - const [err, [publicKey, privateKey]] = ret; + const [err, keys] = ret; if (err !== undefined) throw err; + const [publicKey, privateKey] = keys; + // If no encoding was chosen, return key objects instead. return { publicKey: wrapKey(publicKey, PublicKeyObject), From 8ef059853675bd29eddc01190682b909b1523abc Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 4 Jan 2021 09:06:26 -0800 Subject: [PATCH 2/2] crypto: implement basic secure heap support Adds two new command line arguments: * `--secure-heap=n`, which causes node.js to initialize an openssl secure heap of `n` bytes on openssl initialization. * `--secure-heap-min=n`, which specifies the minimum allocation from the secure heap. * A new method `crypto.secureHeapUsed()` that returns details about the total and used secure heap allocation. The secure heap is an openssl feature that allows certain kinds of potentially sensitive information (such as private key BigNums) to be allocated from a dedicated memory area that is protected against pointer over- and underruns. The secure heap is a fixed size, so it's important that users pick a large enough size to cover the crypto operations they intend to utilize. The secure heap is disabled by default. Signed-off-by: James M Snell --- doc/api/cli.md | 37 ++++++++++ doc/api/crypto.md | 15 ++++ doc/node.1 | 7 ++ lib/crypto.js | 2 + lib/internal/crypto/util.js | 16 +++++ src/crypto/crypto_util.cc | 28 ++++++++ src/node_options.cc | 24 +++++++ src/node_options.h | 2 + test/parallel/test-crypto-secure-heap.js | 72 +++++++++++++++++++ ...rocess-env-allowed-flags-are-documented.js | 10 ++- 10 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-crypto-secure-heap.js diff --git a/doc/api/cli.md b/doc/api/cli.md index c2c2efbcc0eef8..af1d6a7ad09751 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -848,6 +848,40 @@ Enables report to be generated on uncaught exceptions. Useful when inspecting the JavaScript stack in conjunction with native stack and other runtime environment data. +### `--secure-heap=n` + + +Initializes an OpenSSL secure heap of `n` bytes. When initialized, the +secure heap is used for selected types of allocations within OpenSSL +during key generation and other operations. This is useful, for instance, +to prevent sensitive information from leaking due to pointer overruns +or underruns. + +The secure heap is a fixed size and cannot be resized at runtime so, +if used, it is important to select a large enough heap to cover all +application uses. + +The heap size given must be a power of two. Any value less than 2 +will disable the secure heap. + +The secure heap is disabled by default. + +The secure heap is not available on Windows. + +See [`CRYPTO_secure_malloc_init`][] for more details. + +### `--secure-heap-min=n` + + +When using `--secure-heap`, the `--secure-heap-min` flag specifies the +minimum allocation from the secure heap. The minimum value is `2`. +The maximum value is the lesser of `--secure-heap` or `2147483647`. +The value given must be a power of two. + ### `--throw-deprecation` + +* Returns: {Object} + * `total` {number} The total allocated secure heap size as specified + using the `--secure-heap=n` command-line flag. + * `min` {number} The minimum allocation from the secure heap as + specified using the `--secure-heap-min` command-line flag. + * `used` {number} The total number of bytes currently allocated from + the secure heap. + * `utilization` {number} The calculated ratio of `used` to `total` + allocated bytes. + ### `crypto.setEngine(engine[, flags])`