@@ -2591,6 +2591,7 @@ void CipherBase::Init(const char* cipher_type,
2591
2591
int key_buf_len,
2592
2592
unsigned int auth_tag_len) {
2593
2593
HandleScope scope (env ()->isolate ());
2594
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2594
2595
2595
2596
#ifdef NODE_FIPS_MODE
2596
2597
if (FIPS_mode ()) {
@@ -2615,6 +2616,7 @@ void CipherBase::Init(const char* cipher_type,
2615
2616
1 ,
2616
2617
key,
2617
2618
iv);
2619
+ CHECK_NE (key_len, 0 );
2618
2620
2619
2621
ctx_.reset (EVP_CIPHER_CTX_new ());
2620
2622
@@ -2623,7 +2625,11 @@ void CipherBase::Init(const char* cipher_type,
2623
2625
EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2624
2626
2625
2627
const bool encrypt = (kind_ == kCipher );
2626
- EVP_CipherInit_ex (ctx_.get (), cipher, nullptr , nullptr , nullptr , encrypt );
2628
+ if (1 != EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2629
+ nullptr , nullptr , encrypt )) {
2630
+ return ThrowCryptoError (env (), ERR_get_error (),
2631
+ " Failed to initialize cipher" );
2632
+ }
2627
2633
2628
2634
if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
2629
2635
mode == EVP_CIPH_CCM_MODE)) {
@@ -2642,12 +2648,15 @@ void CipherBase::Init(const char* cipher_type,
2642
2648
2643
2649
CHECK_EQ (1 , EVP_CIPHER_CTX_set_key_length (ctx_.get (), key_len));
2644
2650
2645
- EVP_CipherInit_ex (ctx_.get (),
2646
- nullptr ,
2647
- nullptr ,
2648
- reinterpret_cast <unsigned char *>(key),
2649
- reinterpret_cast <unsigned char *>(iv),
2650
- encrypt );
2651
+ if (1 != EVP_CipherInit_ex (ctx_.get (),
2652
+ nullptr ,
2653
+ nullptr ,
2654
+ reinterpret_cast <unsigned char *>(key),
2655
+ reinterpret_cast <unsigned char *>(iv),
2656
+ encrypt )) {
2657
+ return ThrowCryptoError (env (), ERR_get_error (),
2658
+ " Failed to initialize cipher" );
2659
+ }
2651
2660
}
2652
2661
2653
2662
@@ -2682,6 +2691,7 @@ void CipherBase::InitIv(const char* cipher_type,
2682
2691
int iv_len,
2683
2692
unsigned int auth_tag_len) {
2684
2693
HandleScope scope (env ()->isolate ());
2694
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2685
2695
2686
2696
const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
2687
2697
if (cipher == nullptr ) {
@@ -2712,7 +2722,11 @@ void CipherBase::InitIv(const char* cipher_type,
2712
2722
EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2713
2723
2714
2724
const bool encrypt = (kind_ == kCipher );
2715
- EVP_CipherInit_ex (ctx_.get (), cipher, nullptr , nullptr , nullptr , encrypt );
2725
+ if (1 != EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2726
+ nullptr , nullptr , encrypt )) {
2727
+ return ThrowCryptoError (env (), ERR_get_error (),
2728
+ " Failed to initialize cipher" );
2729
+ }
2716
2730
2717
2731
if (IsAuthenticatedMode ()) {
2718
2732
CHECK (has_iv);
@@ -2725,12 +2739,15 @@ void CipherBase::InitIv(const char* cipher_type,
2725
2739
return env ()->ThrowError (" Invalid key length" );
2726
2740
}
2727
2741
2728
- EVP_CipherInit_ex (ctx_.get (),
2729
- nullptr ,
2730
- nullptr ,
2731
- reinterpret_cast <const unsigned char *>(key),
2732
- reinterpret_cast <const unsigned char *>(iv),
2733
- encrypt );
2742
+ if (1 != EVP_CipherInit_ex (ctx_.get (),
2743
+ nullptr ,
2744
+ nullptr ,
2745
+ reinterpret_cast <const unsigned char *>(key),
2746
+ reinterpret_cast <const unsigned char *>(iv),
2747
+ encrypt )) {
2748
+ return ThrowCryptoError (env (), ERR_get_error (),
2749
+ " Failed to initialize cipher" );
2750
+ }
2734
2751
}
2735
2752
2736
2753
@@ -2775,6 +2792,7 @@ static bool IsValidGCMTagLength(unsigned int tag_len) {
2775
2792
bool CipherBase::InitAuthenticated (const char * cipher_type, int iv_len,
2776
2793
unsigned int auth_tag_len) {
2777
2794
CHECK (IsAuthenticatedMode ());
2795
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2778
2796
2779
2797
if (!EVP_CIPHER_CTX_ctrl (ctx_.get (),
2780
2798
EVP_CTRL_AEAD_SET_IVLEN,
@@ -2917,6 +2935,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
2917
2935
bool CipherBase::SetAAD (const char * data, unsigned int len, int plaintext_len) {
2918
2936
if (!ctx_ || !IsAuthenticatedMode ())
2919
2937
return false ;
2938
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2920
2939
2921
2940
int outlen;
2922
2941
const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
@@ -2976,6 +2995,7 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
2976
2995
int * out_len) {
2977
2996
if (!ctx_)
2978
2997
return kErrorState ;
2998
+ MarkPopErrorOnReturn mark_pop_error_on_return;
2979
2999
2980
3000
const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
2981
3001
@@ -2987,10 +3007,10 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
2987
3007
// on first update:
2988
3008
if (kind_ == kDecipher && IsAuthenticatedMode () && auth_tag_len_ > 0 &&
2989
3009
auth_tag_len_ != kNoAuthTagLength && !auth_tag_set_) {
2990
- EVP_CIPHER_CTX_ctrl (ctx_.get (),
2991
- EVP_CTRL_GCM_SET_TAG,
2992
- auth_tag_len_,
2993
- reinterpret_cast <unsigned char *>(auth_tag_));
3010
+ CHECK ( EVP_CIPHER_CTX_ctrl (ctx_.get (),
3011
+ EVP_CTRL_GCM_SET_TAG,
3012
+ auth_tag_len_,
3013
+ reinterpret_cast <unsigned char *>(auth_tag_) ));
2994
3014
auth_tag_set_ = true ;
2995
3015
}
2996
3016
@@ -3068,6 +3088,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3068
3088
bool CipherBase::SetAutoPadding (bool auto_padding) {
3069
3089
if (!ctx_)
3070
3090
return false ;
3091
+ MarkPopErrorOnReturn mark_pop_error_on_return;
3071
3092
return EVP_CIPHER_CTX_set_padding (ctx_.get (), auto_padding);
3072
3093
}
3073
3094
0 commit comments