Skip to content

Commit 8e8aef8

Browse files
danbevjuanarbol
authored andcommittedJul 6, 2022
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'. PR-URL: #43124 Refs: #40366 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Beth Griggs <bgriggs@redhat.com> Backport-PR-URL: #43586
1 parent 837a1d8 commit 8e8aef8

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed
 

‎BUILDING.md

+14
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ file a new issue.
5252
* [Build with a specific ICU](#build-with-a-specific-icu)
5353
* [Unix/macOS](#unixmacos-3)
5454
* [Windows](#windows-4)
55+
* [Configuring OpenSSL config appname](#configure-openssl-appname)
5556
* [Building Node.js with FIPS-compliant OpenSSL](#building-nodejs-with-fips-compliant-openssl)
5657
* [Building Node.js with external core modules](#building-nodejs-with-external-core-modules)
5758
* [Unix/macOS](#unixmacos-4)
@@ -766,6 +767,19 @@ as `deps/icu` (You'll have: `deps/icu/source/...`)
766767
> .\vcbuild full-icu
767768
```
768769

770+
### Configure OpenSSL appname
771+
772+
Node.js can use an OpenSSL configuration file by specifying the environment
773+
variable `OPENSSL_CONF`, or using the command line option `--openssl-conf`, and
774+
if none of those are specified will default to reading the default OpenSSL
775+
configuration file `openssl.cnf`. Node.js will only read a section that is by
776+
default named `nodejs_conf`, but this name can be overridden using the following
777+
configure option:
778+
779+
```console
780+
$ ./configure --openssl-conf-name=<some_conf_name>
781+
```
782+
769783
## Building Node.js with FIPS-compliant OpenSSL
770784

771785
The current version of Node.js does not support FIPS.

‎configure.py

+8
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@
176176
"e.g. /root/x/y.js will be referenced via require('root/x/y'). "
177177
"Can be used multiple times")
178178

179+
parser.add_option("--openssl-conf-name",
180+
action="store",
181+
dest="openssl_conf_name",
182+
default='nodejs_conf',
183+
help="The OpenSSL config appname (config section name) used by Node.js")
184+
179185
parser.add_option('--openssl-default-cipher-list',
180186
action='store',
181187
dest='openssl_default_cipher_list',
@@ -1337,6 +1343,8 @@ def configure_openssl(o):
13371343
if options.openssl_no_asm:
13381344
variables['openssl_no_asm'] = 1
13391345

1346+
o['defines'] += ['NODE_OPENSSL_CONF_NAME=' + options.openssl_conf_name]
1347+
13401348
if options.without_ssl:
13411349
def without_ssl_error(option):
13421350
error('--without-ssl is incompatible with %s' % option)

‎src/node.cc

+43
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#if HAVE_OPENSSL
4545
#include "allocated_buffer-inl.h" // Inlined functions needed by node_crypto.h
4646
#include "node_crypto.h"
47+
#include <openssl/conf.h>
4748
#endif
4849

4950
#if defined(NODE_HAVE_I18N_SUPPORT)
@@ -154,6 +155,9 @@ uint64_t node_start_time;
154155
struct V8Platform v8_platform;
155156
} // namespace per_process
156157

158+
// The section in the OpenSSL configuration file to be loaded.
159+
const char* conf_section_name = STRINGIFY(NODE_OPENSSL_CONF_NAME);
160+
157161
#ifdef __POSIX__
158162
void SignalExit(int signo, siginfo_t* info, void* ucontext) {
159163
ResetStdio();
@@ -975,6 +979,7 @@ void Init(int* argc,
975979
argv[i] = strdup(argv_[i].c_str());
976980
}
977981

982+
978983
InitializationResult InitializeOncePerProcess(int argc, char** argv) {
979984
// Initialized the enabled list for Debug() calls with system
980985
// environment variables.
@@ -1040,6 +1045,44 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
10401045
if (credentials::SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs))
10411046
crypto::UseExtraCaCerts(extra_ca_certs);
10421047
}
1048+
1049+
// Passing NULL as the config file will allow the default openssl.cnf file
1050+
// to be loaded, but the default section in that file will not be used,
1051+
// instead only the section that matches the value of conf_section_name
1052+
// will be read from the default configuration file.
1053+
const char* conf_file = nullptr;
1054+
// Use OPENSSL_CONF environment variable is set.
1055+
std::string env_openssl_conf;
1056+
credentials::SafeGetenv("OPENSSL_CONF", &env_openssl_conf);
1057+
if (!env_openssl_conf.empty()) {
1058+
conf_file = env_openssl_conf.c_str();
1059+
}
1060+
// Use --openssl-conf command line option if specified.
1061+
if (!per_process::cli_options->openssl_config.empty()) {
1062+
conf_file = per_process::cli_options->openssl_config.c_str();
1063+
}
1064+
1065+
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
1066+
OPENSSL_INIT_set_config_filename(settings, conf_file);
1067+
OPENSSL_INIT_set_config_appname(settings, conf_section_name);
1068+
OPENSSL_INIT_set_config_file_flags(settings,
1069+
CONF_MFLAGS_IGNORE_MISSING_FILE);
1070+
1071+
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, settings);
1072+
OPENSSL_INIT_free(settings);
1073+
1074+
if (ERR_peek_error() != 0) {
1075+
int ossl_error_code = ERR_GET_REASON(ERR_peek_error());
1076+
if (ossl_error_code != EVP_R_FIPS_MODE_NOT_SUPPORTED) {
1077+
result.exit_code = ossl_error_code;
1078+
result.early_return = true;
1079+
fprintf(stderr, "%s", "OpenSSL configuration error:\n");
1080+
ERR_print_errors_fp(stderr);
1081+
return result;
1082+
}
1083+
}
1084+
1085+
10431086
// In the case of FIPS builds we should make sure
10441087
// the random source is properly initialized first.
10451088
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]

‎test/parallel/test-crypto-fips.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ testHelper(
6464
[],
6565
FIPS_DISABLED,
6666
'require("crypto").getFips()',
67-
{ ...process.env, 'OPENSSL_CONF': '' });
67+
{ ...process.env, 'OPENSSL_CONF': ' ' });
6868

6969
// --enable-fips should turn FIPS mode on
7070
testHelper(

0 commit comments

Comments
 (0)
Please sign in to comment.