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>
@@ -98,6 +103,7 @@ using v8::ReadOnly;
98103using v8::SideEffectType;
99104using v8::Signature;
100105using v8::String;
106+ using v8::TryCatch;
101107using v8::Uint32;
102108using v8::Uint8Array;
103109using v8::Undefined;
@@ -183,6 +189,16 @@ static int PasswordCallback(char* buf, int size, int rwflag, void* u) {
183189 return -1 ;
184190}
185191
192+ void TestFipsCrypto (const v8::FunctionCallbackInfo<v8::Value>& args) {
193+ #ifdef OPENSSL_FIPS
194+ const auto enabled = FIPS_selftest () ? 1 : 0 ;
195+ #else // OPENSSL_FIPS
196+ const auto enabled = 0 ;
197+ #endif // OPENSSL_FIPS
198+
199+ args.GetReturnValue ().Set (enabled);
200+ }
201+
186202// Loads OpenSSL engine by engine id and returns it. The loaded engine
187203// gets a reference so remember the corresponding call to ENGINE_free.
188204// In case of error the appropriate js exception is scheduled
@@ -3618,12 +3634,10 @@ void CipherBase::Init(const char* cipher_type,
36183634 HandleScope scope (env ()->isolate ());
36193635 MarkPopErrorOnReturn mark_pop_error_on_return;
36203636
3621- #ifdef NODE_FIPS_MODE
36223637 if (FIPS_mode ()) {
36233638 return env ()->ThrowError (
36243639 " crypto.createCipher() is not supported in FIPS mode." );
36253640 }
3626- #endif // NODE_FIPS_MODE
36273641
36283642 const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
36293643 if (cipher == nullptr )
@@ -3809,13 +3823,11 @@ bool CipherBase::InitAuthenticated(const char* cipher_type, int iv_len,
38093823 return false ;
38103824 }
38113825
3812- #ifdef NODE_FIPS_MODE
38133826 // TODO(tniessen) Support CCM decryption in FIPS mode
38143827 if (mode == EVP_CIPH_CCM_MODE && kind_ == kDecipher && FIPS_mode ()) {
38153828 env ()->ThrowError (" CCM decryption not supported in FIPS mode" );
38163829 return false ;
38173830 }
3818- #endif
38193831
38203832 // Tell OpenSSL about the desired length.
38213833 if (!EVP_CIPHER_CTX_ctrl (ctx_.get (), EVP_CTRL_AEAD_SET_TAG, auth_tag_len,
@@ -4690,7 +4702,6 @@ static AllocatedBuffer Node_SignFinal(Environment* env,
46904702}
46914703
46924704static inline bool ValidateDSAParameters (EVP_PKEY* key) {
4693- #ifdef NODE_FIPS_MODE
46944705 /* Validate DSA2 parameters from FIPS 186-4 */
46954706 if (FIPS_mode () && EVP_PKEY_DSA == EVP_PKEY_base_id (key)) {
46964707 DSA* dsa = EVP_PKEY_get0_DSA (key);
@@ -4706,7 +4717,6 @@ static inline bool ValidateDSAParameters(EVP_PKEY* key) {
47064717 (L == 2048 && N == 256 ) ||
47074718 (L == 3072 && N == 256 );
47084719 }
4709- #endif // NODE_FIPS_MODE
47104720
47114721 return true ;
47124722}
@@ -6866,7 +6876,6 @@ void InitCryptoOnce() {
68666876 settings = nullptr ;
68676877#endif
68686878
6869- #ifdef NODE_FIPS_MODE
68706879 /* Override FIPS settings in cnf file, if needed. */
68716880 unsigned long err = 0 ; // NOLINT(runtime/int)
68726881 if (per_process::cli_options->enable_fips_crypto ||
@@ -6876,12 +6885,10 @@ void InitCryptoOnce() {
68766885 }
68776886 }
68786887 if (0 != err) {
6879- fprintf (stderr,
6880- " openssl fips failed: %s\n " ,
6881- ERR_error_string (err, nullptr ));
6882- UNREACHABLE ();
6888+ auto * isolate = Isolate::GetCurrent ();
6889+ auto * env = Environment::GetCurrent (isolate);
6890+ return ThrowCryptoError (env, err);
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
@@ -6981,10 +6986,16 @@ void Initialize(Local<Object> target,
69816986 Local<Value> unused,
69826987 Local<Context> context,
69836988 void * priv) {
6989+ Environment* env = Environment::GetCurrent (context);
69846990 static uv_once_t init_once = UV_ONCE_INIT;
6991+ TryCatch try_catch{env->isolate ()};
69856992 uv_once (&init_once, InitCryptoOnce);
69866993
6987- Environment* env = Environment::GetCurrent (context);
6994+ if (try_catch.HasCaught () && !try_catch.HasTerminated ()) {
6995+ try_catch.ReThrow ();
6996+ return ;
6997+ }
6998+
69886999 SecureContext::Initialize (env, target);
69897000 target->Set (env->context (),
69907001 FIXED_ONE_BYTE_STRING (env->isolate (), " KeyObjectHandle" ),
@@ -7013,10 +7024,9 @@ void Initialize(Local<Object> target,
70137024 env->SetMethod (target, " setEngine" , SetEngine);
70147025#endif // !OPENSSL_NO_ENGINE
70157026
7016- #ifdef NODE_FIPS_MODE
70177027 env->SetMethodNoSideEffect (target, " getFipsCrypto" , GetFipsCrypto);
70187028 env->SetMethod (target, " setFipsCrypto" , SetFipsCrypto);
7019- # endif
7029+ env-> SetMethodNoSideEffect (target, " testFipsCrypto " , TestFipsCrypto);
70207030
70217031 env->SetMethod (target, " pbkdf2" , PBKDF2);
70227032 env->SetMethod (target, " generateKeyPairRSA" , GenerateKeyPairRSA);
0 commit comments