@@ -2581,6 +2581,7 @@ void CipherBase::Init(const char* cipher_type,
2581
2581
int key_buf_len,
2582
2582
unsigned int auth_tag_len) {
2583
2583
HandleScope scope (env ()->isolate ());
2584
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2584
2585
2585
2586
#ifdef NODE_FIPS_MODE
2586
2587
if (FIPS_mode ()) {
@@ -2605,6 +2606,7 @@ void CipherBase::Init(const char* cipher_type,
2605
2606
1 ,
2606
2607
key,
2607
2608
iv);
2609
+ CHECK_NE (key_len, 0 );
2608
2610
2609
2611
ctx_.reset (EVP_CIPHER_CTX_new ());
2610
2612
@@ -2613,7 +2615,11 @@ void CipherBase::Init(const char* cipher_type,
2613
2615
EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2614
2616
2615
2617
const bool encrypt = (kind_ == kCipher );
2616
- EVP_CipherInit_ex (ctx_.get (), cipher, nullptr , nullptr , nullptr , encrypt );
2618
+ if (1 != EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2619
+ nullptr , nullptr , encrypt )) {
2620
+ return ThrowCryptoError (env (), ERR_get_error (),
2621
+ " Failed to initialize cipher" );
2622
+ }
2617
2623
2618
2624
if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
2619
2625
mode == EVP_CIPH_CCM_MODE)) {
@@ -2632,12 +2638,15 @@ void CipherBase::Init(const char* cipher_type,
2632
2638
2633
2639
CHECK_EQ (1 , EVP_CIPHER_CTX_set_key_length (ctx_.get (), key_len));
2634
2640
2635
- EVP_CipherInit_ex (ctx_.get (),
2636
- nullptr ,
2637
- nullptr ,
2638
- reinterpret_cast <unsigned char *>(key),
2639
- reinterpret_cast <unsigned char *>(iv),
2640
- encrypt );
2641
+ if (1 != EVP_CipherInit_ex (ctx_.get (),
2642
+ nullptr ,
2643
+ nullptr ,
2644
+ reinterpret_cast <unsigned char *>(key),
2645
+ reinterpret_cast <unsigned char *>(iv),
2646
+ encrypt )) {
2647
+ return ThrowCryptoError (env (), ERR_get_error (),
2648
+ " Failed to initialize cipher" );
2649
+ }
2641
2650
}
2642
2651
2643
2652
@@ -2672,6 +2681,7 @@ void CipherBase::InitIv(const char* cipher_type,
2672
2681
int iv_len,
2673
2682
unsigned int auth_tag_len) {
2674
2683
HandleScope scope (env ()->isolate ());
2684
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2675
2685
2676
2686
const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
2677
2687
if (cipher == nullptr ) {
@@ -2702,7 +2712,11 @@ void CipherBase::InitIv(const char* cipher_type,
2702
2712
EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2703
2713
2704
2714
const bool encrypt = (kind_ == kCipher );
2705
- EVP_CipherInit_ex (ctx_.get (), cipher, nullptr , nullptr , nullptr , encrypt );
2715
+ if (1 != EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2716
+ nullptr , nullptr , encrypt )) {
2717
+ return ThrowCryptoError (env (), ERR_get_error (),
2718
+ " Failed to initialize cipher" );
2719
+ }
2706
2720
2707
2721
if (IsAuthenticatedMode ()) {
2708
2722
CHECK (has_iv);
@@ -2715,12 +2729,15 @@ void CipherBase::InitIv(const char* cipher_type,
2715
2729
return env ()->ThrowError (" Invalid key length" );
2716
2730
}
2717
2731
2718
- EVP_CipherInit_ex (ctx_.get (),
2719
- nullptr ,
2720
- nullptr ,
2721
- reinterpret_cast <const unsigned char *>(key),
2722
- reinterpret_cast <const unsigned char *>(iv),
2723
- encrypt );
2732
+ if (1 != EVP_CipherInit_ex (ctx_.get (),
2733
+ nullptr ,
2734
+ nullptr ,
2735
+ reinterpret_cast <const unsigned char *>(key),
2736
+ reinterpret_cast <const unsigned char *>(iv),
2737
+ encrypt )) {
2738
+ return ThrowCryptoError (env (), ERR_get_error (),
2739
+ " Failed to initialize cipher" );
2740
+ }
2724
2741
}
2725
2742
2726
2743
@@ -2765,6 +2782,7 @@ static bool IsValidGCMTagLength(unsigned int tag_len) {
2765
2782
bool CipherBase::InitAuthenticated (const char * cipher_type, int iv_len,
2766
2783
unsigned int auth_tag_len) {
2767
2784
CHECK (IsAuthenticatedMode ());
2785
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2768
2786
2769
2787
if (!EVP_CIPHER_CTX_ctrl (ctx_.get (),
2770
2788
EVP_CTRL_AEAD_SET_IVLEN,
@@ -2910,6 +2928,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
2910
2928
bool CipherBase::SetAAD (const char * data, unsigned int len, int plaintext_len) {
2911
2929
if (!ctx_ || !IsAuthenticatedMode ())
2912
2930
return false ;
2931
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2913
2932
2914
2933
int outlen;
2915
2934
const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
@@ -2969,6 +2988,7 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
2969
2988
int * out_len) {
2970
2989
if (!ctx_)
2971
2990
return kErrorState ;
2991
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2972
2992
2973
2993
const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
2974
2994
@@ -2980,10 +3000,10 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
2980
3000
// on first update:
2981
3001
if (kind_ == kDecipher && IsAuthenticatedMode () && auth_tag_len_ > 0 &&
2982
3002
auth_tag_len_ != kNoAuthTagLength && !auth_tag_set_) {
2983
- EVP_CIPHER_CTX_ctrl (ctx_.get (),
2984
- EVP_CTRL_GCM_SET_TAG,
2985
- auth_tag_len_,
2986
- reinterpret_cast <unsigned char *>(auth_tag_));
3003
+ CHECK ( EVP_CIPHER_CTX_ctrl (ctx_.get (),
3004
+ EVP_CTRL_GCM_SET_TAG,
3005
+ auth_tag_len_,
3006
+ reinterpret_cast <unsigned char *>(auth_tag_) ));
2987
3007
auth_tag_set_ = true ;
2988
3008
}
2989
3009
@@ -3061,6 +3081,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3061
3081
bool CipherBase::SetAutoPadding (bool auto_padding) {
3062
3082
if (!ctx_)
3063
3083
return false ;
3084
+ MarkPopErrorOnReturn mark_pop_error_on_return;
3064
3085
return EVP_CIPHER_CTX_set_padding (ctx_.get (), auto_padding);
3065
3086
}
3066
3087
0 commit comments