45
45
#ifndef OPENSSL_NO_ENGINE
46
46
# include < openssl/engine.h>
47
47
#endif // !OPENSSL_NO_ENGINE
48
+
49
+ #ifdef OPENSSL_FIPS
50
+ # include < openssl/fips.h>
51
+ #endif // OPENSSL_FIPS
52
+
48
53
#include < openssl/evp.h>
49
54
#include < openssl/pem.h>
50
55
#include < openssl/x509v3.h>
@@ -98,6 +103,7 @@ using v8::ReadOnly;
98
103
using v8::SideEffectType;
99
104
using v8::Signature;
100
105
using v8::String;
106
+ using v8::TryCatch;
101
107
using v8::Uint32;
102
108
using v8::Uint8Array;
103
109
using v8::Undefined;
@@ -183,6 +189,16 @@ static int PasswordCallback(char* buf, int size, int rwflag, void* u) {
183
189
return -1 ;
184
190
}
185
191
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
+
186
202
// Loads OpenSSL engine by engine id and returns it. The loaded engine
187
203
// gets a reference so remember the corresponding call to ENGINE_free.
188
204
// In case of error the appropriate js exception is scheduled
@@ -3618,12 +3634,10 @@ void CipherBase::Init(const char* cipher_type,
3618
3634
HandleScope scope (env ()->isolate ());
3619
3635
MarkPopErrorOnReturn mark_pop_error_on_return;
3620
3636
3621
- #ifdef NODE_FIPS_MODE
3622
3637
if (FIPS_mode ()) {
3623
3638
return env ()->ThrowError (
3624
3639
" crypto.createCipher() is not supported in FIPS mode." );
3625
3640
}
3626
- #endif // NODE_FIPS_MODE
3627
3641
3628
3642
const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
3629
3643
if (cipher == nullptr )
@@ -3809,13 +3823,11 @@ bool CipherBase::InitAuthenticated(const char* cipher_type, int iv_len,
3809
3823
return false ;
3810
3824
}
3811
3825
3812
- #ifdef NODE_FIPS_MODE
3813
3826
// TODO(tniessen) Support CCM decryption in FIPS mode
3814
3827
if (mode == EVP_CIPH_CCM_MODE && kind_ == kDecipher && FIPS_mode ()) {
3815
3828
env ()->ThrowError (" CCM decryption not supported in FIPS mode" );
3816
3829
return false ;
3817
3830
}
3818
- #endif
3819
3831
3820
3832
// Tell OpenSSL about the desired length.
3821
3833
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,
4690
4702
}
4691
4703
4692
4704
static inline bool ValidateDSAParameters (EVP_PKEY* key) {
4693
- #ifdef NODE_FIPS_MODE
4694
4705
/* Validate DSA2 parameters from FIPS 186-4 */
4695
4706
if (FIPS_mode () && EVP_PKEY_DSA == EVP_PKEY_base_id (key)) {
4696
4707
DSA* dsa = EVP_PKEY_get0_DSA (key);
@@ -4706,7 +4717,6 @@ static inline bool ValidateDSAParameters(EVP_PKEY* key) {
4706
4717
(L == 2048 && N == 256 ) ||
4707
4718
(L == 3072 && N == 256 );
4708
4719
}
4709
- #endif // NODE_FIPS_MODE
4710
4720
4711
4721
return true ;
4712
4722
}
@@ -6866,7 +6876,6 @@ void InitCryptoOnce() {
6866
6876
settings = nullptr ;
6867
6877
#endif
6868
6878
6869
- #ifdef NODE_FIPS_MODE
6870
6879
/* Override FIPS settings in cnf file, if needed. */
6871
6880
unsigned long err = 0 ; // NOLINT(runtime/int)
6872
6881
if (per_process::cli_options->enable_fips_crypto ||
@@ -6876,12 +6885,10 @@ void InitCryptoOnce() {
6876
6885
}
6877
6886
}
6878
6887
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);
6883
6891
}
6884
- #endif // NODE_FIPS_MODE
6885
6892
6886
6893
6887
6894
// Turn off compression. Saves memory and protects against CRIME attacks.
@@ -6927,7 +6934,6 @@ void SetEngine(const FunctionCallbackInfo<Value>& args) {
6927
6934
}
6928
6935
#endif // !OPENSSL_NO_ENGINE
6929
6936
6930
- #ifdef NODE_FIPS_MODE
6931
6937
void GetFipsCrypto (const FunctionCallbackInfo<Value>& args) {
6932
6938
args.GetReturnValue ().Set (FIPS_mode () ? 1 : 0 );
6933
6939
}
@@ -6945,7 +6951,6 @@ void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
6945
6951
return ThrowCryptoError (env, err);
6946
6952
}
6947
6953
}
6948
- #endif /* NODE_FIPS_MODE */
6949
6954
6950
6955
namespace {
6951
6956
// SecureBuffer uses openssl to allocate a Uint8Array using
@@ -6981,10 +6986,16 @@ void Initialize(Local<Object> target,
6981
6986
Local<Value> unused,
6982
6987
Local<Context> context,
6983
6988
void * priv) {
6989
+ Environment* env = Environment::GetCurrent (context);
6984
6990
static uv_once_t init_once = UV_ONCE_INIT;
6991
+ TryCatch try_catch{env->isolate ()};
6985
6992
uv_once (&init_once, InitCryptoOnce);
6986
6993
6987
- Environment* env = Environment::GetCurrent (context);
6994
+ if (try_catch.HasCaught () && !try_catch.HasTerminated ()) {
6995
+ try_catch.ReThrow ();
6996
+ return ;
6997
+ }
6998
+
6988
6999
SecureContext::Initialize (env, target);
6989
7000
target->Set (env->context (),
6990
7001
FIXED_ONE_BYTE_STRING (env->isolate (), " KeyObjectHandle" ),
@@ -7013,10 +7024,9 @@ void Initialize(Local<Object> target,
7013
7024
env->SetMethod (target, " setEngine" , SetEngine);
7014
7025
#endif // !OPENSSL_NO_ENGINE
7015
7026
7016
- #ifdef NODE_FIPS_MODE
7017
7027
env->SetMethodNoSideEffect (target, " getFipsCrypto" , GetFipsCrypto);
7018
7028
env->SetMethod (target, " setFipsCrypto" , SetFipsCrypto);
7019
- # endif
7029
+ env-> SetMethodNoSideEffect (target, " testFipsCrypto " , TestFipsCrypto);
7020
7030
7021
7031
env->SetMethod (target, " pbkdf2" , PBKDF2);
7022
7032
env->SetMethod (target, " generateKeyPairRSA" , GenerateKeyPairRSA);
0 commit comments