Skip to content

Commit cae9eb3

Browse files
committed
crypto: fix openssl.cnf FIPS handling & testing
* Add documentation for `--openssl-conf=file`. * Fix openssl.cnf loading and OpenSSL init ordering * Fix FIPS tests so `OPENSSL_CONF` is not longer usable but `--openssl-conf` is PR-URL: nodejs-private/node-private#82 Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 1ea0358 commit cae9eb3

File tree

6 files changed

+57
-14
lines changed

6 files changed

+57
-14
lines changed

doc/api/cli.md

+10
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ Force FIPS-compliant crypto on startup. (Cannot be disabled from script code.)
234234
(Same requirements as `--enable-fips`)
235235

236236

237+
### `--openssl-config=file`
238+
<!-- YAML
239+
added: v6.9.0
240+
-->
241+
242+
Load an OpenSSL configuration file on startup. Among other uses, this can be
243+
used to enable FIPS-compliant crypto if Node.js is built with
244+
`./configure --openssl-fips`.
245+
246+
237247
### `--icu-data-dir=file`
238248
<!-- YAML
239249
added: v0.11.15

doc/node.1

+6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ Enable FIPS-compliant crypto at startup. (Requires Node.js to be built with
165165
Force FIPS-compliant crypto on startup. (Cannot be disabled from script code.)
166166
(Same requirements as \fB\-\-enable\-fips\fR)
167167

168+
.TP
169+
.BR \-\-openssl\-config =\fIfile\fR
170+
Load an OpenSSL configuration file on startup. Among other uses, this can be
171+
used to enable FIPS-compliant crypto if Node.js is built with
172+
\fB./configure \-\-openssl\-fips\fR.
173+
168174
.TP
169175
.BR \-\-icu\-data\-dir =\fIfile\fR
170176
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)

src/node.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,10 @@ typedef intptr_t ssize_t;
179179
namespace node {
180180

181181
NODE_EXTERN extern bool no_deprecation;
182-
#if HAVE_OPENSSL
183-
# if NODE_FIPS_MODE
182+
#if HAVE_OPENSSL && NODE_FIPS_MODE
184183
NODE_EXTERN extern bool enable_fips_crypto;
185184
NODE_EXTERN extern bool force_fips_crypto;
186-
# endif // NODE_FIPS_MODE
187-
NODE_EXTERN extern const char* openssl_config;
188-
#endif // HAVE_OPENSSL
185+
#endif
189186

190187
NODE_EXTERN int Start(int argc, char *argv[]);
191188
NODE_EXTERN void Init(int* argc,

src/node_crypto.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -5789,14 +5789,20 @@ void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) {
57895789
}
57905790

57915791
void InitCryptoOnce() {
5792+
SSL_load_error_strings();
57925793
OPENSSL_no_config();
57935794

57945795
// --openssl-config=...
57955796
if (openssl_config != nullptr) {
5797+
OPENSSL_load_builtin_modules();
5798+
#ifndef OPENSSL_NO_ENGINE
5799+
ENGINE_load_builtin_engines();
5800+
#endif
5801+
ERR_clear_error();
57965802
CONF_modules_load_file(
57975803
openssl_config,
57985804
nullptr,
5799-
CONF_MFLAGS_DEFAULT_SECTION | CONF_MFLAGS_IGNORE_MISSING_FILE);
5805+
CONF_MFLAGS_DEFAULT_SECTION);
58005806
int err = ERR_get_error();
58015807
if (0 != err) {
58025808
fprintf(stderr,
@@ -5808,7 +5814,6 @@ void InitCryptoOnce() {
58085814

58095815
SSL_library_init();
58105816
OpenSSL_add_all_algorithms();
5811-
SSL_load_error_strings();
58125817

58135818
crypto_lock_init();
58145819
CRYPTO_set_locking_callback(crypto_lock_cb);

src/node_internals.h

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ struct sockaddr;
3232

3333
namespace node {
3434

35+
// Set in node.cc by ParseArgs with the value of --openssl-config.
36+
// Used in node_crypto.cc when initializing OpenSSL.
37+
extern const char* openssl_config;
38+
3539
// Set in node.cc by ParseArgs when --preserve-symlinks is used.
3640
// Used in node_config.cc to set a constant on process.binding('config')
3741
// that is used by lib/module.js

test/parallel/test-crypto-fips.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,26 @@ testHelper(
8888
// OpenSSL config file should be able to turn on FIPS mode
8989
testHelper(
9090
'stdout',
91-
[],
91+
[`--openssl-config=${CNF_FIPS_ON}`],
9292
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
9393
'require("crypto").fips',
94+
process.env);
95+
// OPENSSL_CONF should _not_ be able to turn on FIPS mode
96+
testHelper(
97+
'stdout',
98+
[],
99+
FIPS_DISABLED,
100+
'require("crypto").fips',
94101
addToEnv('OPENSSL_CONF', CNF_FIPS_ON));
95102

96103
// --enable-fips should take precedence over OpenSSL config file
104+
testHelper(
105+
compiledWithFips() ? 'stdout' : 'stderr',
106+
['--enable-fips', `--openssl-config=${CNF_FIPS_OFF}`],
107+
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
108+
'require("crypto").fips',
109+
process.env);
110+
// OPENSSL_CONF should _not_ make a difference to --enable-fips
97111
testHelper(
98112
compiledWithFips() ? 'stdout' : 'stderr',
99113
['--enable-fips'],
@@ -102,6 +116,13 @@ testHelper(
102116
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF));
103117

104118
// --force-fips should take precedence over OpenSSL config file
119+
testHelper(
120+
compiledWithFips() ? 'stdout' : 'stderr',
121+
['--force-fips', `--openssl-config=${CNF_FIPS_OFF}`],
122+
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
123+
'require("crypto").fips',
124+
process.env);
125+
// Using OPENSSL_CONF should not make a difference to --force-fips
105126
testHelper(
106127
compiledWithFips() ? 'stdout' : 'stderr',
107128
['--force-fips'],
@@ -116,7 +137,7 @@ testHelper(
116137
compiledWithFips() ? FIPS_ENABLED : FIPS_ERROR_STRING,
117138
'(require("crypto").fips = true,' +
118139
'require("crypto").fips)',
119-
addToEnv('OPENSSL_CONF', ''));
140+
process.env);
120141

121142
// setFipsCrypto should be able to turn FIPS mode on and off
122143
testHelper(
@@ -126,25 +147,25 @@ testHelper(
126147
'(require("crypto").fips = true,' +
127148
'require("crypto").fips = false,' +
128149
'require("crypto").fips)',
129-
addToEnv('OPENSSL_CONF', ''));
150+
process.env);
130151

131152
// setFipsCrypto takes precedence over OpenSSL config file, FIPS on
132153
testHelper(
133154
compiledWithFips() ? 'stdout' : 'stderr',
134-
[],
155+
[`--openssl-config=${CNF_FIPS_OFF}`],
135156
compiledWithFips() ? FIPS_ENABLED : FIPS_ERROR_STRING,
136157
'(require("crypto").fips = true,' +
137158
'require("crypto").fips)',
138-
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF));
159+
process.env);
139160

140161
// setFipsCrypto takes precedence over OpenSSL config file, FIPS off
141162
testHelper(
142163
compiledWithFips() ? 'stdout' : 'stderr',
143-
[],
164+
[`--openssl-config=${CNF_FIPS_ON}`],
144165
compiledWithFips() ? FIPS_DISABLED : FIPS_ERROR_STRING,
145166
'(require("crypto").fips = false,' +
146167
'require("crypto").fips)',
147-
addToEnv('OPENSSL_CONF', CNF_FIPS_ON));
168+
process.env);
148169

149170
// --enable-fips does not prevent use of setFipsCrypto API
150171
testHelper(

0 commit comments

Comments
 (0)