@@ -2590,10 +2590,12 @@ void CipherBase::Init(const char* cipher_type, |
2590 | 2590 | 1, |
2591 | 2591 | key, |
2592 | 2592 | iv); |
| 2593 | + CHECK_NE(key_len, 0); |
2593 | 2594 |
|
2594 | 2595 | ctx_.reset(EVP_CIPHER_CTX_new()); |
2595 | 2596 | const bool encrypt = (kind_ == kCipher); |
2596 | | - EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, nullptr, nullptr, encrypt); |
| 2597 | + CHECK(EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, |
| 2598 | + nullptr, nullptr, encrypt)); |
2597 | 2599 |
|
2598 | 2600 | int mode = EVP_CIPHER_CTX_mode(ctx_.get()); |
2599 | 2601 | if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE || |
@@ -2616,12 +2618,12 @@ void CipherBase::Init(const char* cipher_type, |
2616 | 2618 |
|
2617 | 2619 | CHECK_EQ(1, EVP_CIPHER_CTX_set_key_length(ctx_.get(), key_len)); |
2618 | 2620 |
|
2619 | | - EVP_CipherInit_ex(ctx_.get(), |
2620 | | - nullptr, |
2621 | | - nullptr, |
2622 | | - reinterpret_cast<unsigned char*>(key), |
2623 | | - reinterpret_cast<unsigned char*>(iv), |
2624 | | - encrypt); |
| 2621 | + CHECK(EVP_CipherInit_ex(ctx_.get(), |
| 2622 | + nullptr, |
| 2623 | + nullptr, |
| 2624 | + reinterpret_cast<unsigned char*>(key), |
| 2625 | + reinterpret_cast<unsigned char*>(iv), |
| 2626 | + encrypt)); |
2625 | 2627 | } |
2626 | 2628 |
|
2627 | 2629 |
|
@@ -2686,25 +2688,27 @@ void CipherBase::InitIv(const char* cipher_type, |
2686 | 2688 | EVP_CIPHER_CTX_set_flags(ctx_.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
2687 | 2689 |
|
2688 | 2690 | const bool encrypt = (kind_ == kCipher); |
2689 | | - EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, nullptr, nullptr, encrypt); |
| 2691 | + CHECK(EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, |
| 2692 | + nullptr, nullptr, encrypt)); |
2690 | 2693 |
|
2691 | 2694 | if (IsAuthenticatedMode()) { |
2692 | 2695 | CHECK(has_iv); |
2693 | 2696 | if (!InitAuthenticated(cipher_type, iv_len, auth_tag_len)) |
2694 | 2697 | return; |
2695 | 2698 | } |
2696 | 2699 |
|
| 2700 | + ClearErrorOnReturn clear_error_on_return; |
2697 | 2701 | if (!EVP_CIPHER_CTX_set_key_length(ctx_.get(), key_len)) { |
2698 | 2702 | ctx_.reset(); |
2699 | 2703 | return env()->ThrowError("Invalid key length"); |
2700 | 2704 | } |
2701 | 2705 |
|
2702 | | - EVP_CipherInit_ex(ctx_.get(), |
2703 | | - nullptr, |
2704 | | - nullptr, |
2705 | | - reinterpret_cast<const unsigned char*>(key), |
2706 | | - reinterpret_cast<const unsigned char*>(iv), |
2707 | | - encrypt); |
| 2706 | + CHECK(EVP_CipherInit_ex(ctx_.get(), |
| 2707 | + nullptr, |
| 2708 | + nullptr, |
| 2709 | + reinterpret_cast<const unsigned char*>(key), |
| 2710 | + reinterpret_cast<const unsigned char*>(iv), |
| 2711 | + encrypt)); |
2708 | 2712 | } |
2709 | 2713 |
|
2710 | 2714 |
|
@@ -2749,6 +2753,7 @@ static bool IsValidGCMTagLength(unsigned int tag_len) { |
2749 | 2753 | bool CipherBase::InitAuthenticated(const char* cipher_type, int iv_len, |
2750 | 2754 | unsigned int auth_tag_len) { |
2751 | 2755 | CHECK(IsAuthenticatedMode()); |
| 2756 | + MarkPopErrorOnReturn mark_pop_error_on_return; |
2752 | 2757 |
|
2753 | 2758 | if (!EVP_CIPHER_CTX_ctrl(ctx_.get(), |
2754 | 2759 | EVP_CTRL_AEAD_SET_IVLEN, |
@@ -2893,6 +2898,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) { |
2893 | 2898 | bool CipherBase::SetAAD(const char* data, unsigned int len, int plaintext_len) { |
2894 | 2899 | if (!ctx_ || !IsAuthenticatedMode()) |
2895 | 2900 | return false; |
| 2901 | + ClearErrorOnReturn clear_error_on_return; |
2896 | 2902 |
|
2897 | 2903 | int outlen; |
2898 | 2904 | const int mode = EVP_CIPHER_CTX_mode(ctx_.get()); |
@@ -2952,6 +2958,7 @@ CipherBase::UpdateResult CipherBase::Update(const char* data, |
2952 | 2958 | int* out_len) { |
2953 | 2959 | if (!ctx_) |
2954 | 2960 | return kErrorState; |
| 2961 | + ClearErrorOnReturn clear_error_on_return; |
2955 | 2962 |
|
2956 | 2963 | const int mode = EVP_CIPHER_CTX_mode(ctx_.get()); |
2957 | 2964 |
|
@@ -2963,10 +2970,10 @@ CipherBase::UpdateResult CipherBase::Update(const char* data, |
2963 | 2970 | // on first update: |
2964 | 2971 | if (kind_ == kDecipher && IsAuthenticatedMode() && auth_tag_len_ > 0 && |
2965 | 2972 | auth_tag_len_ != kNoAuthTagLength && !auth_tag_set_) { |
2966 | | - EVP_CIPHER_CTX_ctrl(ctx_.get(), |
2967 | | - EVP_CTRL_GCM_SET_TAG, |
2968 | | - auth_tag_len_, |
2969 | | - reinterpret_cast<unsigned char*>(auth_tag_)); |
| 2973 | + CHECK(EVP_CIPHER_CTX_ctrl(ctx_.get(), |
| 2974 | + EVP_CTRL_GCM_SET_TAG, |
| 2975 | + auth_tag_len_, |
| 2976 | + reinterpret_cast<unsigned char*>(auth_tag_))); |
2970 | 2977 | auth_tag_set_ = true; |
2971 | 2978 | } |
2972 | 2979 |
|
@@ -3044,6 +3051,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) { |
3044 | 3051 | bool CipherBase::SetAutoPadding(bool auto_padding) { |
3045 | 3052 | if (!ctx_) |
3046 | 3053 | return false; |
| 3054 | + ClearErrorOnReturn clear_error_on_return; |
3047 | 3055 | return EVP_CIPHER_CTX_set_padding(ctx_.get(), auto_padding); |
3048 | 3056 | } |
3049 | 3057 |
|
|