Skip to content

Commit e9ae606

Browse files
voxikkhardix
authored andcommitted
Make FIPS related options and functionality always awailable.
There is no reason to hide FIPS functionality behind build flags. OpenSSL always provide the information about FIPS availability via `FIPS_mode()` function. This makes the user experience more consistent, because the OpenSSL library is always queried and the `crypto.getFips()` always returns OpenSSL settings. Fixes nodejs#34903
1 parent 33d3a2e commit e9ae606

9 files changed

+19
-26
lines changed

node.gypi

-3
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,6 @@
319319
[ 'node_use_openssl=="true"', {
320320
'defines': [ 'HAVE_OPENSSL=1' ],
321321
'conditions': [
322-
['openssl_fips != "" or openssl_is_fips=="true"', {
323-
'defines': [ 'NODE_FIPS_MODE' ],
324-
}],
325322
[ 'node_shared_openssl=="false"', {
326323
'dependencies': [
327324
'./deps/openssl/openssl.gyp:openssl',

src/crypto/crypto_cipher.cc

-4
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,10 @@ void CipherBase::Init(const char* cipher_type,
343343
HandleScope scope(env()->isolate());
344344
MarkPopErrorOnReturn mark_pop_error_on_return;
345345

346-
#ifdef NODE_FIPS_MODE
347346
if (FIPS_mode()) {
348347
return THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env(),
349348
"crypto.createCipher() is not supported in FIPS mode.");
350349
}
351-
#endif // NODE_FIPS_MODE
352350

353351
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
354352
if (cipher == nullptr)
@@ -528,14 +526,12 @@ bool CipherBase::InitAuthenticated(
528526
return false;
529527
}
530528

531-
#ifdef NODE_FIPS_MODE
532529
// TODO(tniessen) Support CCM decryption in FIPS mode
533530
if (mode == EVP_CIPH_CCM_MODE && kind_ == kDecipher && FIPS_mode()) {
534531
THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env(),
535532
"CCM encryption not supported in FIPS mode");
536533
return false;
537534
}
538-
#endif
539535

540536
// Tell OpenSSL about the desired length.
541537
if (!EVP_CIPHER_CTX_ctrl(ctx_.get(), EVP_CTRL_AEAD_SET_TAG, auth_tag_len,

src/crypto/crypto_sig.cc

-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ using v8::Value;
2727
namespace crypto {
2828
namespace {
2929
bool ValidateDSAParameters(EVP_PKEY* key) {
30-
#ifdef NODE_FIPS_MODE
3130
/* Validate DSA2 parameters from FIPS 186-4 */
3231
if (FIPS_mode() && EVP_PKEY_DSA == EVP_PKEY_base_id(key)) {
3332
DSA* dsa = EVP_PKEY_get0_DSA(key);
@@ -43,7 +42,6 @@ bool ValidateDSAParameters(EVP_PKEY* key) {
4342
(L == 2048 && N == 256) ||
4443
(L == 3072 && N == 256);
4544
}
46-
#endif // NODE_FIPS_MODE
4745

4846
return true;
4947
}

src/crypto/crypto_util.cc

+11-6
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ void InitCryptoOnce() {
133133
}
134134
#endif
135135

136-
#ifdef NODE_FIPS_MODE
137136
/* Override FIPS settings in cnf file, if needed. */
138137
unsigned long err = 0; // NOLINT(runtime/int)
139138
if (per_process::cli_options->enable_fips_crypto ||
@@ -148,7 +147,6 @@ void InitCryptoOnce() {
148147
ERR_error_string(err, nullptr));
149148
UNREACHABLE();
150149
}
151-
#endif // NODE_FIPS_MODE
152150

153151
// Turn off compression. Saves memory and protects against CRIME attacks.
154152
// No-op with OPENSSL_NO_COMP builds of OpenSSL.
@@ -162,7 +160,6 @@ void InitCryptoOnce() {
162160
NodeBIO::GetMethod();
163161
}
164162

165-
#ifdef NODE_FIPS_MODE
166163
void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
167164
args.GetReturnValue().Set(FIPS_mode() ? 1 : 0);
168165
}
@@ -180,7 +177,16 @@ void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
180177
return ThrowCryptoError(env, err);
181178
}
182179
}
183-
#endif /* NODE_FIPS_MODE */
180+
181+
void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args) {
182+
#ifdef OPENSSL_FIPS
183+
const auto enabled = FIPS_selftest() ? 1 : 0;
184+
#else // OPENSSL_FIPS
185+
const auto enabled = 0;
186+
#endif // OPENSSL_FIPS
187+
188+
args.GetReturnValue().Set(enabled);
189+
}
184190

185191
void CryptoErrorVector::Capture() {
186192
clear();
@@ -652,10 +658,9 @@ void Initialize(Environment* env, Local<Object> target) {
652658
env->SetMethod(target, "setEngine", SetEngine);
653659
#endif // !OPENSSL_NO_ENGINE
654660

655-
#ifdef NODE_FIPS_MODE
656661
env->SetMethodNoSideEffect(target, "getFipsCrypto", GetFipsCrypto);
657662
env->SetMethod(target, "setFipsCrypto", SetFipsCrypto);
658-
#endif
663+
env->SetMethodNoSideEffect(target, "testFipsCrypto", TestFipsCrypto);
659664

660665
NODE_DEFINE_CONSTANT(target, kCryptoJobAsync);
661666
NODE_DEFINE_CONSTANT(target, kCryptoJobSync);

src/crypto/crypto_util.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#ifndef OPENSSL_NO_ENGINE
2323
# include <openssl/engine.h>
2424
#endif // !OPENSSL_NO_ENGINE
25+
#ifdef OPENSSL_FIPS
26+
# include <openssl/fips.h>
27+
#endif // OPENSSL_FIPS
2528

2629
#include <algorithm>
2730
#include <memory>
@@ -510,11 +513,11 @@ bool SetEngine(
510513
void SetEngine(const v8::FunctionCallbackInfo<v8::Value>& args);
511514
#endif // !OPENSSL_NO_ENGINE
512515

513-
#ifdef NODE_FIPS_MODE
514516
void GetFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args);
515517

516518
void SetFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args);
517-
#endif /* NODE_FIPS_MODE */
519+
520+
void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args);
518521

519522
class CipherPushContext {
520523
public:

src/node.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1012,11 +1012,11 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
10121012
if (credentials::SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs))
10131013
crypto::UseExtraCaCerts(extra_ca_certs);
10141014
}
1015-
#ifdef NODE_FIPS_MODE
10161015
// In the case of FIPS builds we should make sure
10171016
// the random source is properly initialized first.
1018-
OPENSSL_init();
1019-
#endif // NODE_FIPS_MODE
1017+
if (FIPS_mode()) {
1018+
OPENSSL_init();
1019+
}
10201020
// V8 on Windows doesn't have a good source of entropy. Seed it from
10211021
// OpenSSL's pool.
10221022
V8::SetEntropySource(crypto::EntropySource);

src/node_config.cc

-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ static void Initialize(Local<Object> target,
4242
READONLY_FALSE_PROPERTY(target, "hasOpenSSL");
4343
#endif // HAVE_OPENSSL
4444

45-
#ifdef NODE_FIPS_MODE
4645
READONLY_TRUE_PROPERTY(target, "fipsMode");
47-
#endif
4846

4947
#ifdef NODE_HAVE_I18N_SUPPORT
5048

src/node_options.cc

-2
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,6 @@ PerProcessOptionsParser::PerProcessOptionsParser(
766766
&PerProcessOptions::ssl_openssl_cert_store);
767767
Implies("--use-openssl-ca", "[ssl_openssl_cert_store]");
768768
ImpliesNot("--use-bundled-ca", "[ssl_openssl_cert_store]");
769-
#if NODE_FIPS_MODE
770769
AddOption("--enable-fips",
771770
"enable FIPS crypto at startup",
772771
&PerProcessOptions::enable_fips_crypto,
@@ -775,7 +774,6 @@ PerProcessOptionsParser::PerProcessOptionsParser(
775774
"force FIPS crypto (cannot be disabled)",
776775
&PerProcessOptions::force_fips_crypto,
777776
kAllowedInEnvironment);
778-
#endif
779777
AddOption("--secure-heap",
780778
"total size of the OpenSSL secure heap",
781779
&PerProcessOptions::secure_heap,

src/node_options.h

-2
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,8 @@ class PerProcessOptions : public Options {
245245
#endif
246246
bool use_openssl_ca = false;
247247
bool use_bundled_ca = false;
248-
#if NODE_FIPS_MODE
249248
bool enable_fips_crypto = false;
250249
bool force_fips_crypto = false;
251-
#endif
252250
#endif
253251

254252
// Per-process because reports can be triggered outside a known V8 context.

0 commit comments

Comments
 (0)