| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 28cad47 commit 8c4531b
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -99,20 +99,36 @@ int NoPasswordCallback(char* buf, int size, int rwflag, void* u) { | |||
| 99 | 99 | return 0; | |
| 100 | 100 | } | |
| 101 | 101 | ||
| 102 | - bool ProcessFipsOptions() { | ||
| 103 | - /* Override FIPS settings in configuration file, if needed. */ | ||
| 104 | - if (per_process::cli_options->enable_fips_crypto || | ||
| 105 | - per_process::cli_options->force_fips_crypto) { | ||
| 102 | + std::optional<std::string> ProcessFipsOptions() { | ||
| 103 | + const bool enable_fips = per_process::cli_options->enable_fips_crypto; | ||
| 104 | + const bool force_fips = per_process::cli_options->force_fips_crypto; | ||
| 105 | + if (!enable_fips && !force_fips) return std::nullopt; | ||
| 106 | + | ||
| 106 | 107 | #if OPENSSL_VERSION_MAJOR >= 3 | |
| 107 | - if (!ncrypto::testFipsEnabled()) return false; | ||
| 108 | - return ncrypto::setFipsEnabled(true, nullptr); | ||
| 109 | - #else | ||
| 110 | - // TODO(@jasnell): Remove this ifdef branch when openssl 1.1.1 is | ||
| 111 | - // no longer supported. | ||
| 112 | - if (FIPS_mode() == 0) return FIPS_mode_set(1); | ||
| 108 | + // Whether FIPS-approved implementations are reachable is decided by the | ||
| 109 | + // OpenSSL configuration, not by Node.js. Refuse to start rather than | ||
| 110 | + // restrict the default property query to a provider that is not there, | ||
| 111 | + // which would leave every operation failing as unsupported. | ||
| 112 | + if (!ncrypto::testFipsEnabled()) { | ||
| 113 | + const std::string option = force_fips ? "--force-fips" : "--enable-fips"; | ||
| 114 | + return option + " requires an active OpenSSL provider named \"fips\". " | ||
| 115 | + "FIPS mode is configured through OpenSSL; see " | ||
| 116 | + "https://nodejs.org/api/crypto.html#fips-mode"; | ||
| 117 | + } | ||
| 113 | 118 | #endif | |
| 119 | + | ||
| 120 | + CryptoErrorList errors{CryptoErrorList::Option::NONE}; | ||
| 121 | + if (!ncrypto::setFipsEnabled(true, &errors)) { | ||
| 122 | + std::string error = "OpenSSL error when trying to enable FIPS"; | ||
| 123 | + if (!errors.empty()) error += ':'; | ||
| 124 | + for (const auto& openssl_error : errors) { | ||
| 125 | + error += '\n'; | ||
| 126 | + error += openssl_error; | ||
| 127 | + } | ||
| 128 | + return error; | ||
| 114 | 129 | } | |
| 115 | - return true; | ||
| 130 | + | ||
| 131 | + return std::nullopt; | ||
| 116 | 132 | } | |
| 117 | 133 | ||
| 118 | 134 | bool InitCryptoOnce(Isolate* isolate) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,7 +62,10 @@ constexpr T NumBitsToBytes(T bits) { | |||
| 62 | 62 | return (bits / CHAR_BIT) + ((CHAR_BIT - 1 + (bits % CHAR_BIT)) / CHAR_BIT); | |
| 63 | 63 | } | |
| 64 | 64 | ||
| 65 | - bool ProcessFipsOptions(); | ||
| 65 | + // Applies the FIPS related command line options. Returns a description of | ||
| 66 | + // what went wrong, or std::nullopt when there was nothing to do or the | ||
| 67 | + // options were applied successfully. | ||
| 68 | + std::optional<std::string> ProcessFipsOptions(); | ||
| 66 | 69 | ||
| 67 | 70 | bool InitCryptoOnce(v8::Isolate* isolate); | |
| 68 | 71 | void InitCryptoOnce(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1168,6 +1168,7 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args, | |||
| 1168 | 1168 | if (!(flags & ProcessInitializationFlags::kNoInitOpenSSL)) { | |
| 1169 | 1169 | #if HAVE_OPENSSL | |
| 1170 | 1170 | #ifndef OPENSSL_IS_BORINGSSL | |
| 1171 | + #if OPENSSL_VERSION_MAJOR >= 3 | ||
| 1171 | 1172 | auto GetOpenSSLErrorString = []() -> std::string { | |
| 1172 | 1173 | std::string ret; | |
| 1173 | 1174 | ERR_print_errors_cb( | |
@@ -1183,7 +1184,6 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args, | |||
| 1183 | 1184 | ||
| 1184 | 1185 | // In the case of FIPS builds we should make sure | |
| 1185 | 1186 | // the random source is properly initialized first. | |
| 1186 | - #if OPENSSL_VERSION_MAJOR >= 3 | ||
| 1187 | 1187 | // Call OPENSSL_init_crypto to initialize OPENSSL_INIT_LOAD_CONFIG to | |
| 1188 | 1188 | // avoid the default behavior where errors raised during the parsing of the | |
| 1189 | 1189 | // OpenSSL configuration file are not propagated and cannot be detected. | |
@@ -1244,12 +1244,10 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args, | |||
| 1244 | 1244 | OPENSSL_init(); | |
| 1245 | 1245 | } | |
| 1246 | 1246 | #endif | |
| 1247 | - if (!crypto::ProcessFipsOptions()) { | ||
| 1247 | + if (auto fips_error = crypto::ProcessFipsOptions()) { | ||
| 1248 | 1248 | result->exit_code_ = ExitCode::kGenericUserError; | |
| 1249 | 1249 | result->early_return_ = true; | |
| 1250 | - result->errors_.emplace_back( | ||
| 1251 | - "OpenSSL error when trying to enable FIPS:\n" + | ||
| 1252 | - GetOpenSSLErrorString()); | ||
| 1250 | + result->errors_.emplace_back(std::move(*fips_error)); | ||
| 1253 | 1251 | return result; | |
| 1254 | 1252 | } | |
| 1255 | 1253 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,14 @@ const FIPS_ERROR_STRING2 = | |||
| 21 | 21 | 'Error [ERR_CRYPTO_FIPS_FORCED]: Cannot set FIPS mode, it was forced with ' + | |
| 22 | 22 | '--force-fips at startup.'; | |
| 23 | 23 | const FIPS_UNSUPPORTED_ERROR_STRING = 'fips mode not supported'; | |
| 24 | - const FIPS_ENABLE_ERROR_STRING = 'OpenSSL error when trying to enable FIPS:'; | ||
| 24 | + const FIPS_ENABLE_ERROR_STRING = | ||
| 25 | + hasOpenSSL3 ? | ||
| 26 | + '--enable-fips requires an active OpenSSL provider named "fips"' : | ||
| 27 | + 'OpenSSL error when trying to enable FIPS:'; | ||
| 28 | + const FIPS_FORCE_ERROR_STRING = | ||
| 29 | + hasOpenSSL3 ? | ||
| 30 | + '--force-fips requires an active OpenSSL provider named "fips"' : | ||
| 31 | + 'OpenSSL error when trying to enable FIPS:'; | ||
| 25 | 32 | ||
| 26 | 33 | const CNF_FIPS_ON = fixtures.path('openssl_fips_enabled.cnf'); | |
| 27 | 34 | const CNF_FIPS_OFF = fixtures.path('openssl_fips_disabled.cnf'); | |
@@ -75,16 +82,16 @@ testHelper( | |||
| 75 | 82 | ['--enable-fips'], | |
| 76 | 83 | testFipsCrypto() ? kNoFailure : kGenericUserError, | |
| 77 | 84 | testFipsCrypto() ? FIPS_ENABLED : FIPS_ENABLE_ERROR_STRING, | |
| 78 | - 'process.versions', | ||
| 85 | + 'require("crypto").getFips()', | ||
| 79 | 86 | process.env); | |
| 80 | 87 | ||
| 81 | 88 | // --force-fips should raise an error if OpenSSL is not FIPS enabled. | |
| 82 | 89 | testHelper( | |
| 83 | 90 | testFipsCrypto() ? 'stdout' : 'stderr', | |
| 84 | 91 | ['--force-fips'], | |
| 85 | 92 | testFipsCrypto() ? kNoFailure : kGenericUserError, | |
| 86 | - testFipsCrypto() ? FIPS_ENABLED : FIPS_ENABLE_ERROR_STRING, | ||
| 87 | - 'process.versions', | ||
| 93 | + testFipsCrypto() ? FIPS_ENABLED : FIPS_FORCE_ERROR_STRING, | ||
| 94 | + 'require("crypto").getFips()', | ||
| 88 | 95 | process.env); | |
| 89 | 96 | ||
| 90 | 97 | // By default FIPS should be off in both FIPS and non-FIPS builds | |
| Back | FazBrowse Home | New Git URL |
0 commit comments