Skip to content

Commit 45b7659

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 f095b19 commit 45b7659

File tree

6 files changed

+75
-29
lines changed

6 files changed

+75
-29
lines changed

BUILDING.md

+4-1
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

+30
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

+8-8
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,18 @@
390390
'variables': {
391391
'openssl-cli': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)openssl-cli<(EXECUTABLE_SUFFIX)',
392392
'provider_name': 'libopenssl-fipsmodule',
393-
'opensslconfig': './deps/openssl/openssl/apps/openssl.cnf',
393+
'opensslconfig': './deps/openssl/nodejs-openssl.cnf',
394394
'conditions': [
395395
['GENERATOR == "ninja"', {
396396
'fipsmodule_internal': '<(PRODUCT_DIR)/lib/<(provider_name).so',
397397
'fipsmodule': '<(PRODUCT_DIR)/obj/lib/openssl-modules/fips.so',
398398
'fipsconfig': '<(PRODUCT_DIR)/obj/lib/fipsmodule.cnf',
399-
'opensslconfig_internal': '<(PRODUCT_DIR)/obj/lib/openssl.cnf',
399+
'opensslconfig_internal': '<(PRODUCT_DIR)/obj/lib/nodejs-openssl.cnf',
400400
}, {
401401
'fipsmodule_internal': '<(PRODUCT_DIR)/obj.target/deps/openssl/<(provider_name).so',
402402
'fipsmodule': '<(PRODUCT_DIR)/obj.target/deps/openssl/lib/openssl-modules/fips.so',
403-
'fipsconfig': '<(PRODUCT_DIR)/obj/deps/openssl/fipsmodule.cnf',
404-
'opensslconfig_internal': '<(PRODUCT_DIR)/obj.target/deps/openssl/openssl.cnf',
403+
'fipsconfig': '<(PRODUCT_DIR)/obj.target/deps/openssl/fipsmodule.cnf',
404+
'opensslconfig_internal': '<(PRODUCT_DIR)/obj.target/deps/openssl/nodejs-openssl.cnf',
405405
}],
406406
],
407407
},
@@ -451,8 +451,8 @@
451451
],
452452
}, {
453453
'variables': {
454-
'opensslconfig_internal': '<(obj_dir)/deps/openssl/openssl.cnf',
455-
'opensslconfig': './deps/openssl/openssl/apps/openssl.cnf',
454+
'opensslconfig_internal': '<(obj_dir)/deps/openssl/nodejs-openssl.cnf',
455+
'opensslconfig': './deps/openssl/nodejs-openssl.cnf',
456456
},
457457
'actions': [
458458
{
@@ -461,8 +461,8 @@
461461
'outputs': [ '<(opensslconfig_internal)', ],
462462
'action': [
463463
'python', 'tools/copyfile.py',
464-
'./deps/openssl/openssl/apps/openssl.cnf',
465-
'<(obj_dir)/deps/openssl/openssl.cnf',
464+
'<(opensslconfig)',
465+
'<(opensslconfig_internal)',
466466
],
467467
},
468468
],

src/node.cc

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

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

test/fixtures/openssl_fips_disabled.cnf

+1-1
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

+1-1
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)