4545#ifndef OPENSSL_NO_ENGINE
4646# include < openssl/engine.h>
4747#endif // !OPENSSL_NO_ENGINE
48+
49+ #ifdef OPENSSL_FIPS
50+ # include < openssl/fips.h>
51+ #endif // OPENSSL_FIPS
52+
4853#include < openssl/evp.h>
4954#include < openssl/pem.h>
5055#include < openssl/x509v3.h>
@@ -183,6 +188,16 @@ static int PasswordCallback(char* buf, int size, int rwflag, void* u) {
183188 return -1 ;
184189}
185190
191+ void TestFipsCrypto (const v8::FunctionCallbackInfo<v8::Value>& args) {
192+ #ifdef OPENSSL_FIPS
193+ const auto enabled = FIPS_selftest () ? 1 : 0 ;
194+ #else // OPENSSL_FIPS
195+ const auto enabled = 0 ;
196+ #endif // OPENSSL_FIPS
197+
198+ args.GetReturnValue ().Set (enabled);
199+ }
200+
186201// Loads OpenSSL engine by engine id and returns it. The loaded engine
187202// gets a reference so remember the corresponding call to ENGINE_free.
188203// In case of error the appropriate js exception is scheduled
@@ -3618,12 +3633,10 @@ void CipherBase::Init(const char* cipher_type,
36183633 HandleScope scope (env ()->isolate ());
36193634 MarkPopErrorOnReturn mark_pop_error_on_return;
36203635
3621- #ifdef NODE_FIPS_MODE
36223636 if (FIPS_mode ()) {
36233637 return env ()->ThrowError (
36243638 " crypto.createCipher() is not supported in FIPS mode." );
36253639 }
3626- #endif // NODE_FIPS_MODE
36273640
36283641 const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
36293642 if (cipher == nullptr )
@@ -3809,13 +3822,11 @@ bool CipherBase::InitAuthenticated(const char* cipher_type, int iv_len,
38093822 return false ;
38103823 }
38113824
3812- #ifdef NODE_FIPS_MODE
38133825 // TODO(tniessen) Support CCM decryption in FIPS mode
38143826 if (mode == EVP_CIPH_CCM_MODE && kind_ == kDecipher && FIPS_mode ()) {
38153827 env ()->ThrowError (" CCM decryption not supported in FIPS mode" );
38163828 return false ;
38173829 }
3818- #endif
38193830
38203831 // Tell OpenSSL about the desired length.
38213832 if (!EVP_CIPHER_CTX_ctrl (ctx_.get (), EVP_CTRL_AEAD_SET_TAG, auth_tag_len,
@@ -4690,7 +4701,6 @@ static AllocatedBuffer Node_SignFinal(Environment* env,
46904701}
46914702
46924703static inline bool ValidateDSAParameters (EVP_PKEY* key) {
4693- #ifdef NODE_FIPS_MODE
46944704 /* Validate DSA2 parameters from FIPS 186-4 */
46954705 if (FIPS_mode () && EVP_PKEY_DSA == EVP_PKEY_base_id (key)) {
46964706 DSA* dsa = EVP_PKEY_get0_DSA (key);
@@ -4706,7 +4716,6 @@ static inline bool ValidateDSAParameters(EVP_PKEY* key) {
47064716 (L == 2048 && N == 256 ) ||
47074717 (L == 3072 && N == 256 );
47084718 }
4709- #endif // NODE_FIPS_MODE
47104719
47114720 return true ;
47124721}
@@ -6866,7 +6875,6 @@ void InitCryptoOnce() {
68666875 settings = nullptr ;
68676876#endif
68686877
6869- #ifdef NODE_FIPS_MODE
68706878 /* Override FIPS settings in cnf file, if needed. */
68716879 unsigned long err = 0 ; // NOLINT(runtime/int)
68726880 if (per_process::cli_options->enable_fips_crypto ||
@@ -6881,7 +6889,6 @@ void InitCryptoOnce() {
68816889 ERR_error_string (err, nullptr ));
68826890 UNREACHABLE ();
68836891 }
6884- #endif // NODE_FIPS_MODE
68856892
68866893
68876894 // Turn off compression. Saves memory and protects against CRIME attacks.
@@ -6927,7 +6934,6 @@ void SetEngine(const FunctionCallbackInfo<Value>& args) {
69276934}
69286935#endif // !OPENSSL_NO_ENGINE
69296936
6930- #ifdef NODE_FIPS_MODE
69316937void GetFipsCrypto (const FunctionCallbackInfo<Value>& args) {
69326938 args.GetReturnValue ().Set (FIPS_mode () ? 1 : 0 );
69336939}
@@ -6945,7 +6951,6 @@ void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
69456951 return ThrowCryptoError (env, err);
69466952 }
69476953}
6948- #endif /* NODE_FIPS_MODE */
69496954
69506955namespace {
69516956// SecureBuffer uses openssl to allocate a Uint8Array using
@@ -7013,10 +7018,9 @@ void Initialize(Local<Object> target,
70137018 env->SetMethod (target, " setEngine" , SetEngine);
70147019#endif // !OPENSSL_NO_ENGINE
70157020
7016- #ifdef NODE_FIPS_MODE
70177021 env->SetMethodNoSideEffect (target, " getFipsCrypto" , GetFipsCrypto);
70187022 env->SetMethod (target, " setFipsCrypto" , SetFipsCrypto);
7019- # endif
7023+ env-> SetMethodNoSideEffect (target, " testFipsCrypto " , TestFipsCrypto);
70207024
70217025 env->SetMethod (target, " pbkdf2" , PBKDF2);
70227026 env->SetMethod (target, " generateKeyPairRSA" , GenerateKeyPairRSA);
0 commit comments