|
44 | 44 | #if HAVE_OPENSSL
|
45 | 45 | #include "allocated_buffer-inl.h" // Inlined functions needed by node_crypto.h
|
46 | 46 | #include "node_crypto.h"
|
| 47 | +#include <openssl/conf.h> |
47 | 48 | #endif
|
48 | 49 |
|
49 | 50 | #if defined(NODE_HAVE_I18N_SUPPORT)
|
@@ -154,6 +155,9 @@ uint64_t node_start_time;
|
154 | 155 | struct V8Platform v8_platform;
|
155 | 156 | } // namespace per_process
|
156 | 157 |
|
| 158 | +// The section in the OpenSSL configuration file to be loaded. |
| 159 | +const char* conf_section_name = STRINGIFY(NODE_OPENSSL_CONF_NAME); |
| 160 | + |
157 | 161 | #ifdef __POSIX__
|
158 | 162 | void SignalExit(int signo, siginfo_t* info, void* ucontext) {
|
159 | 163 | ResetStdio();
|
@@ -975,6 +979,7 @@ void Init(int* argc,
|
975 | 979 | argv[i] = strdup(argv_[i].c_str());
|
976 | 980 | }
|
977 | 981 |
|
| 982 | + |
978 | 983 | InitializationResult InitializeOncePerProcess(int argc, char** argv) {
|
979 | 984 | // Initialized the enabled list for Debug() calls with system
|
980 | 985 | // environment variables.
|
@@ -1040,6 +1045,44 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
|
1040 | 1045 | if (credentials::SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs))
|
1041 | 1046 | crypto::UseExtraCaCerts(extra_ca_certs);
|
1042 | 1047 | }
|
| 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 | + |
1043 | 1086 | // In the case of FIPS builds we should make sure
|
1044 | 1087 | // the random source is properly initialized first.
|
1045 | 1088 | if (FIPS_mode()) {
|
|
0 commit comments