Skip to content

Commit 62c6a95

Browse files
committed
src,deps,build,test: add OpenSSL config appname
This commit adds the setting of an appname (configuration section name), 'nodejs_conf', to be used when reading OpenSSL configuration files. The motivation for this is that currently the default OpenSSL configuration, 'openssl_conf', element will be used which may be undesirable as it might configure OpenSSL in unwanted ways. With this commit it is still possible to use a default openssl.cnf file but the only section that Node.js will read from is a section named 'nodejs_conf'. Refs: #40366
1 parent 895cc57 commit 62c6a95

File tree

6 files changed

+75
-29
lines changed

6 files changed

+75
-29
lines changed

BUILDING.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ The OpenSSL configuration files will be found in `OPENSSLDIR` directory above:
809809
```console
810810
$ ls -w 1 out/Release/obj.target/deps/openssl/*.cnf
811811
out/Release/obj.target/deps/openssl/fipsmodule.cnf
812-
out/Release/obj.target/deps/openssl/openssl.cnf
812+
out/Release/obj.target/deps/openssl/nodejs-openssl.cnf
813813
```
814814

815815
And the FIPS module will be located in the `MODULESDIR` directory:
@@ -819,6 +819,9 @@ $ ls out/Release/obj.target/deps/openssl/lib/openssl-modules/
819819
fips.so
820820
```
821821

822+
Running `configure` without `--openssl-is-fips` flag and rebuilding will reset
823+
the FIPS configuration.
824+
822825
### FIPS support when dynamically linking OpenSSL
823826

824827
For quictls/openssl 3.0 it is possible to enable FIPS when dynamically linking.

deps/openssl/nodejs-openssl.cnf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Use this in order to automatically load providers.
2+
nodejs_conf = openssl_init
3+
4+
# Optionally include a file that is generated by the OpenSSL fipsinstall
5+
# application. This file contains configuration data required by the OpenSSL
6+
# fips provider. It contains a named section e.g. [fips_sect] which is
7+
# referenced from the [provider_sect] below.
8+
# Refer to the OpenSSL security policy for more information.
9+
# .include fipsmodule.cnf
10+
11+
[openssl_init]
12+
providers = provider_sect
13+
14+
# List of providers to load
15+
[provider_sect]
16+
default = default_sect
17+
# The fips section name should match the section name inside the
18+
# included fipsmodule.cnf.
19+
# fips = fips_sect
20+
21+
# If no providers are activated explicitly, the default one is activated implicitly.
22+
# See man 7 OSSL_PROVIDER-default for more details.
23+
#
24+
# If you add a section explicitly activating any other provider(s), you most
25+
# probably need to explicitly activate the default provider, otherwise it
26+
# becomes unavailable in openssl. As a consequence applications depending on
27+
# OpenSSL may not work correctly which could lead to significant system
28+
# problems including inability to remotely access the system.
29+
[default_sect]
30+
# activate = 1

node.gyp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,18 @@
364364
'variables': {
365365
'openssl-cli': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)openssl-cli<(EXECUTABLE_SUFFIX)',
366366
'provider_name': 'libopenssl-fipsmodule',
367-
'opensslconfig': './deps/openssl/openssl/apps/openssl.cnf',
367+
'opensslconfig': './deps/openssl/nodejs-openssl.cnf',
368368
'conditions': [
369369
['GENERATOR == "ninja"', {
370370
'fipsmodule_internal': '<(PRODUCT_DIR)/lib/<(provider_name).so',
371371
'fipsmodule': '<(PRODUCT_DIR)/obj/lib/openssl-modules/fips.so',
372372
'fipsconfig': '<(PRODUCT_DIR)/obj/lib/fipsmodule.cnf',
373-
'opensslconfig_internal': '<(PRODUCT_DIR)/obj/lib/openssl.cnf',
373+
'opensslconfig_internal': '<(PRODUCT_DIR)/obj/lib/nodejs-openssl.cnf',
374374
}, {
375375
'fipsmodule_internal': '<(PRODUCT_DIR)/obj.target/deps/openssl/<(provider_name).so',
376376
'fipsmodule': '<(PRODUCT_DIR)/obj.target/deps/openssl/lib/openssl-modules/fips.so',
377-
'fipsconfig': '<(PRODUCT_DIR)/obj/deps/openssl/fipsmodule.cnf',
378-
'opensslconfig_internal': '<(PRODUCT_DIR)/obj.target/deps/openssl/openssl.cnf',
377+
'fipsconfig': '<(PRODUCT_DIR)/obj.target/deps/openssl/fipsmodule.cnf',
378+
'opensslconfig_internal': '<(PRODUCT_DIR)/obj.target/deps/openssl/nodejs-openssl.cnf',
379379
}],
380380
],
381381
},
@@ -425,8 +425,8 @@
425425
],
426426
}, {
427427
'variables': {
428-
'opensslconfig_internal': '<(obj_dir)/deps/openssl/openssl.cnf',
429-
'opensslconfig': './deps/openssl/openssl/apps/openssl.cnf',
428+
'opensslconfig_internal': '<(obj_dir)/deps/openssl/nodejs-openssl.cnf',
429+
'opensslconfig': './deps/openssl/nodejs-openssl.cnf',
430430
},
431431
'actions': [
432432
{
@@ -435,8 +435,8 @@
435435
'outputs': [ '<(opensslconfig_internal)', ],
436436
'action': [
437437
'python', 'tools/copyfile.py',
438-
'./deps/openssl/openssl/apps/openssl.cnf',
439-
'<(obj_dir)/deps/openssl/openssl.cnf',
438+
'<(opensslconfig)',
439+
'<(opensslconfig_internal)',
440440
],
441441
},
442442
],

src/node.cc

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,27 +1084,40 @@ InitializationResult InitializeOncePerProcess(
10841084
// CheckEntropy. CheckEntropy will call RAND_status which will now always
10851085
// return 0, leading to an endless loop and the node process will appear to
10861086
// hang/freeze.
1087+
1088+
// The section in the OpenSSL configuration file to be loaded.
1089+
const char* conf_section_name = "nodejs_conf";
1090+
// Passing NULL as the config file will allow the default openssl.cnf file
1091+
// to be loaded, but the default section in that file will not be used,
1092+
// instead only the section that matches the value of conf_section_name
1093+
// will be read from the default configuration file.
1094+
const char* conf_file = nullptr;
1095+
// Use OPENSSL_CONF environment variable is set.
10871096
std::string env_openssl_conf;
10881097
credentials::SafeGetenv("OPENSSL_CONF", &env_openssl_conf);
1098+
if (!env_openssl_conf.empty()) {
1099+
conf_file = env_openssl_conf.c_str();
1100+
}
1101+
// Use --openssl-conf command line option if specified.
1102+
if (!per_process::cli_options->openssl_config.empty()) {
1103+
conf_file = per_process::cli_options->openssl_config.c_str();
1104+
}
10891105

1090-
bool has_cli_conf = !per_process::cli_options->openssl_config.empty();
1091-
if (has_cli_conf || !env_openssl_conf.empty()) {
1092-
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
1093-
OPENSSL_INIT_set_config_file_flags(settings, CONF_MFLAGS_DEFAULT_SECTION);
1094-
if (has_cli_conf) {
1095-
const char* conf = per_process::cli_options->openssl_config.c_str();
1096-
OPENSSL_INIT_set_config_filename(settings, conf);
1097-
}
1098-
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, settings);
1099-
OPENSSL_INIT_free(settings);
1100-
1101-
if (ERR_peek_error() != 0) {
1102-
result.exit_code = ERR_GET_REASON(ERR_peek_error());
1103-
result.early_return = true;
1104-
fprintf(stderr, "OpenSSL configuration error:\n");
1105-
ERR_print_errors_fp(stderr);
1106-
return result;
1107-
}
1106+
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
1107+
OPENSSL_INIT_set_config_filename(settings, conf_file);
1108+
OPENSSL_INIT_set_config_appname(settings, conf_section_name);
1109+
OPENSSL_INIT_set_config_file_flags(settings,
1110+
CONF_MFLAGS_IGNORE_MISSING_FILE);
1111+
1112+
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, settings);
1113+
OPENSSL_INIT_free(settings);
1114+
1115+
if (ERR_peek_error() != 0) {
1116+
result.exit_code = ERR_GET_REASON(ERR_peek_error());
1117+
result.early_return = true;
1118+
fprintf(stderr, "OpenSSL configuration error:\n");
1119+
ERR_print_errors_fp(stderr);
1120+
return result;
11081121
}
11091122
#else // OPENSSL_VERSION_MAJOR < 3
11101123
if (FIPS_mode()) {

test/fixtures/openssl_fips_disabled.cnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Skeleton openssl.cnf for testing with FIPS
22

3-
openssl_conf = openssl_conf_section
3+
nodejs_conf = openssl_conf_section
44
authorityKeyIdentifier=keyid:always,issuer:always
55

66
[openssl_conf_section]

test/fixtures/openssl_fips_enabled.cnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Skeleton openssl.cnf for testing with FIPS
22

3-
openssl_conf = openssl_conf_section
3+
nodejs_conf = openssl_conf_section
44
authorityKeyIdentifier=keyid:always,issuer:always
55

66
[openssl_conf_section]

0 commit comments

Comments
 (0)