Skip to content

Commit def2b01

Browse files
committed
crypto: avoid hang when no algorithm available
1 parent bcc2d58 commit def2b01

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/crypto/crypto_util.cc

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
6262

6363
MUST_USE_RESULT CSPRNGResult CSPRNG(void* buffer, size_t length) {
6464
do {
65+
#if OPENSSL_VERSION_MAJOR >= 3
66+
const uint32_t err = ERR_peek_error();
67+
if (err == ERR_PACK(ERR_LIB_RAND, 0, RAND_R_UNABLE_TO_FETCH_DRBG)) {
68+
return {false};
69+
}
70+
#endif
6571
if (1 == RAND_status())
6672
if (1 == RAND_bytes(static_cast<unsigned char*>(buffer), length))
6773
return {true};
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
nodejs_conf = nodejs_init
2+
3+
[nodejs_init]
4+
providers = provider_sect
5+
6+
# List of providers to load
7+
[provider_sect]
8+
base = base_sect
9+
10+
[base_sect]
11+
activate = 1
12+
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
if (!common.hasOpenSSL3)
9+
common.skip('this test requires OpenSSL 3.x');
10+
11+
const { internalBinding } = require('node:internal/test/binding');
12+
const { testFipsCrypto } = internalBinding('crypto');
13+
const assert = require('node:assert/strict');
14+
const crypto = require('node:crypto');
15+
16+
{
17+
// TODO(richardlau): Decide if `crypto.setFips` should error if the
18+
// provider namd "fips" is not available.
19+
crypto.setFips(1);
20+
crypto.randomBytes(20, testFipsCrypto() ?
21+
common.mustSucceed() :
22+
common.expectsError((err) => {
23+
const expected = /digital envelope routines::unsupported/;
24+
assert(err.opensslErrorStack.some((msg) => expected.test(msg)),
25+
`did not find ${expected} in ${err.opensslErrorStack}`);
26+
return true;
27+
})
28+
);
29+
}
30+
31+
{
32+
// Startup test. Should not hang.
33+
const { path } = require('../common/fixtures');
34+
const { spawnSync } = require('node:child_process');
35+
const baseConf = path('openssl3-conf', 'base_only.cnf');
36+
const cp = spawnSync(process.execPath,
37+
[ `--openssl-config=${baseConf}`, '-p', '"hello"' ],
38+
{ encoding: 'utf8' });
39+
assert(common.nodeProcessAborted(cp.status, cp.signal),
40+
`process did not abort, code:${cp.status} signal:${cp.signal}`);
41+
}

0 commit comments

Comments
 (0)