Skip to content

Commit 59afa27

Browse files
committed
crypto: support OPENSSL_CONF again
A side-effect of https://github.com/nodejs/node-private/pull/82 was to remove support for OPENSSL_CONF, as well as removing the default read of a configuration file on startup. Partly revert this, allowing OPENSSL_CONF to be used to specify a configuration file to read on startup, but do not read a file by default. If the --openssl-config command line option is provided, its value is used, not the OPENSSL_CONF environment variable. Fix: #10938 PR-URL: #11006 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 901e926 commit 59afa27

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

doc/api/cli.md

+13
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,18 @@ misformatted, but any errors are otherwise ignored.
387387
Note that neither the well known nor extra certificates are used when the `ca`
388388
options property is explicitly specified for a TLS or HTTPS client or server.
389389

390+
### `OPENSSL_CONF=file`
391+
<!-- YAML
392+
added: REPLACEME
393+
-->
394+
395+
Load an OpenSSL configuration file on startup. Among other uses, this can be
396+
used to enable FIPS-compliant crypto if Node.js is built with `./configure
397+
\-\-openssl\-fips`.
398+
399+
If the [`--openssl-config`][] command line option is used, the environment
400+
variable is ignored.
401+
390402
### `SSL_CERT_DIR=dir`
391403

392404
If `--use-openssl-ca` is enabled, this overrides and sets OpenSSL's directory
@@ -421,3 +433,4 @@ equivalent to using the `--redirect-warnings=file` command-line flag.
421433
[debugger]: debugger.html
422434
[REPL]: repl.html
423435
[SlowBuffer]: buffer.html#buffer_class_slowbuffer
436+
[`--openssl-config`]: #cli_openssl_config_file

doc/node.1

+10
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ asynchronous when outputting to a TTY on platforms which support async stdio.
256256
Setting this will void any guarantee that stdio will not be interleaved or
257257
dropped at program exit. \fBAvoid use.\fR
258258

259+
.TP
260+
.BR OPENSSL_CONF = \fIfile\fR
261+
Load an OpenSSL configuration file on startup. Among other uses, this can be
262+
used to enable FIPS-compliant crypto if Node.js is built with
263+
\fB./configure \-\-openssl\-fips\fR.
264+
265+
If the
266+
\fB\-\-openssl\-config\fR
267+
command line option is used, the environment variable is ignored.
268+
259269
.TP
260270
.BR SSL_CERT_DIR = \fIdir\fR
261271
If \fB\-\-use\-openssl\-ca\fR is enabled, this overrides and sets OpenSSL's directory

src/node.cc

+10-4
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ bool ssl_openssl_cert_store =
176176
bool enable_fips_crypto = false;
177177
bool force_fips_crypto = false;
178178
# endif // NODE_FIPS_MODE
179-
const char* openssl_config = nullptr;
179+
std::string openssl_config; // NOLINT(runtime/string)
180180
#endif // HAVE_OPENSSL
181181

182182
// true if process warnings should be suppressed
@@ -3561,8 +3561,9 @@ static void PrintHelp() {
35613561
" --enable-fips enable FIPS crypto at startup\n"
35623562
" --force-fips force FIPS crypto (cannot be disabled)\n"
35633563
#endif /* NODE_FIPS_MODE */
3564-
" --openssl-config=path load OpenSSL configuration file from\n"
3565-
" the specified path\n"
3564+
" --openssl-config=file load OpenSSL configuration from the\n"
3565+
" specified file (overrides\n"
3566+
" OPENSSL_CONF)\n"
35663567
#endif /* HAVE_OPENSSL */
35673568
#if defined(NODE_HAVE_I18N_SUPPORT)
35683569
" --icu-data-dir=dir set ICU data load path to dir\n"
@@ -3597,6 +3598,8 @@ static void PrintHelp() {
35973598
" file\n"
35983599
"NODE_REDIRECT_WARNINGS write warnings to path instead of\n"
35993600
" stderr\n"
3601+
"OPENSSL_CONF load OpenSSL configuration from file\n"
3602+
"\n"
36003603
"Documentation can be found at https://nodejs.org/\n");
36013604
}
36023605

@@ -3746,7 +3749,7 @@ static void ParseArgs(int* argc,
37463749
force_fips_crypto = true;
37473750
#endif /* NODE_FIPS_MODE */
37483751
} else if (strncmp(arg, "--openssl-config=", 17) == 0) {
3749-
openssl_config = arg + 17;
3752+
openssl_config.assign(arg + 17);
37503753
#endif /* HAVE_OPENSSL */
37513754
#if defined(NODE_HAVE_I18N_SUPPORT)
37523755
} else if (strncmp(arg, "--icu-data-dir=", 15) == 0) {
@@ -4246,6 +4249,9 @@ void Init(int* argc,
42464249
if (config_warning_file.empty())
42474250
SafeGetenv("NODE_REDIRECT_WARNINGS", &config_warning_file);
42484251

4252+
if (openssl_config.empty())
4253+
SafeGetenv("OPENSSL_CONF", &openssl_config);
4254+
42494255
// Parse a few arguments which are specific to Node.
42504256
int v8_argc;
42514257
const char** v8_argv;

src/node_crypto.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -5893,14 +5893,14 @@ void InitCryptoOnce() {
58935893
OPENSSL_no_config();
58945894

58955895
// --openssl-config=...
5896-
if (openssl_config != nullptr) {
5896+
if (!openssl_config.empty()) {
58975897
OPENSSL_load_builtin_modules();
58985898
#ifndef OPENSSL_NO_ENGINE
58995899
ENGINE_load_builtin_engines();
59005900
#endif
59015901
ERR_clear_error();
59025902
CONF_modules_load_file(
5903-
openssl_config,
5903+
openssl_config.c_str(),
59045904
nullptr,
59055905
CONF_MFLAGS_DEFAULT_SECTION);
59065906
int err = ERR_get_error();

src/node_internals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace node {
3737

3838
// Set in node.cc by ParseArgs with the value of --openssl-config.
3939
// Used in node_crypto.cc when initializing OpenSSL.
40-
extern const char* openssl_config;
40+
extern std::string openssl_config;
4141

4242
// Set in node.cc by ParseArgs when --preserve-symlinks is used.
4343
// Used in node_config.cc to set a constant on process.binding('config')

test/parallel/test-crypto-fips.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ function testHelper(stream, args, expectedOutput, cmd, env) {
3737
env: env
3838
});
3939

40-
console.error('Spawned child [pid:' + child.pid + '] with cmd ' +
41-
cmd + ' and args \'' + args + '\'');
40+
console.error('Spawned child [pid:' + child.pid + '] with cmd \'' +
41+
cmd + '\' expect %j with args \'' + args + '\'' +
42+
' OPENSSL_CONF=%j', expectedOutput, env.OPENSSL_CONF);
4243

4344
function childOk(child) {
4445
console.error('Child #' + ++num_children_ok +
@@ -92,10 +93,26 @@ testHelper(
9293
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
9394
'require("crypto").fips',
9495
process.env);
95-
// OPENSSL_CONF should _not_ be able to turn on FIPS mode
96+
97+
// OPENSSL_CONF should be able to turn on FIPS mode
9698
testHelper(
9799
'stdout',
98100
[],
101+
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
102+
'require("crypto").fips',
103+
addToEnv('OPENSSL_CONF', CNF_FIPS_ON));
104+
105+
// --openssl-config option should override OPENSSL_CONF
106+
testHelper(
107+
'stdout',
108+
[`--openssl-config=${CNF_FIPS_ON}`],
109+
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
110+
'require("crypto").fips',
111+
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF));
112+
113+
testHelper(
114+
'stdout',
115+
[`--openssl-config=${CNF_FIPS_OFF}`],
99116
FIPS_DISABLED,
100117
'require("crypto").fips',
101118
addToEnv('OPENSSL_CONF', CNF_FIPS_ON));
@@ -107,6 +124,7 @@ testHelper(
107124
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
108125
'require("crypto").fips',
109126
process.env);
127+
110128
// OPENSSL_CONF should _not_ make a difference to --enable-fips
111129
testHelper(
112130
compiledWithFips() ? 'stdout' : 'stderr',
@@ -122,6 +140,7 @@ testHelper(
122140
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
123141
'require("crypto").fips',
124142
process.env);
143+
125144
// Using OPENSSL_CONF should not make a difference to --force-fips
126145
testHelper(
127146
compiledWithFips() ? 'stdout' : 'stderr',

0 commit comments

Comments
 (0)