@@ -2557,12 +2557,21 @@ int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
2557
2557
return 1 ;
2558
2558
}
2559
2559
2560
- static bool IsSupportedAuthenticatedMode (int mode) {
2561
- return mode == EVP_CIPH_CCM_MODE ||
2560
+ static bool IsSupportedAuthenticatedMode (const EVP_CIPHER* cipher) {
2561
+ const int mode = EVP_CIPHER_mode (cipher);
2562
+ // Check `chacha20-poly1305` separately, it is also an AEAD cipher,
2563
+ // but its mode is 0 which doesn't indicate
2564
+ return EVP_CIPHER_nid (cipher) == NID_chacha20_poly1305 ||
2565
+ mode == EVP_CIPH_CCM_MODE ||
2562
2566
mode == EVP_CIPH_GCM_MODE ||
2563
2567
IS_OCB_MODE (mode);
2564
2568
}
2565
2569
2570
+ static bool IsSupportedAuthenticatedMode (const EVP_CIPHER_CTX* ctx) {
2571
+ const EVP_CIPHER* cipher = EVP_CIPHER_CTX_cipher (ctx);
2572
+ return IsSupportedAuthenticatedMode (cipher);
2573
+ }
2574
+
2566
2575
void CipherBase::Initialize (Environment* env, Local<Object> target) {
2567
2576
Local<FunctionTemplate> t = env->NewFunctionTemplate (New);
2568
2577
@@ -2610,7 +2619,7 @@ void CipherBase::CommonInit(const char* cipher_type,
2610
2619
" Failed to initialize cipher" );
2611
2620
}
2612
2621
2613
- if (IsSupportedAuthenticatedMode (mode )) {
2622
+ if (IsSupportedAuthenticatedMode (cipher )) {
2614
2623
CHECK_GE (iv_len, 0 );
2615
2624
if (!InitAuthenticated (cipher_type, iv_len, auth_tag_len))
2616
2625
return ;
@@ -2712,8 +2721,7 @@ void CipherBase::InitIv(const char* cipher_type,
2712
2721
}
2713
2722
2714
2723
const int expected_iv_len = EVP_CIPHER_iv_length (cipher);
2715
- const int mode = EVP_CIPHER_mode (cipher);
2716
- const bool is_authenticated_mode = IsSupportedAuthenticatedMode (mode);
2724
+ const bool is_authenticated_mode = IsSupportedAuthenticatedMode (cipher);
2717
2725
const bool has_iv = iv_len >= 0 ;
2718
2726
2719
2727
// Throw if no IV was passed and the cipher requires an IV
@@ -2785,7 +2793,20 @@ bool CipherBase::InitAuthenticated(const char* cipher_type, int iv_len,
2785
2793
}
2786
2794
2787
2795
const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
2788
- if (mode == EVP_CIPH_CCM_MODE || IS_OCB_MODE (mode)) {
2796
+ if (mode == EVP_CIPH_GCM_MODE) {
2797
+ if (auth_tag_len != kNoAuthTagLength ) {
2798
+ if (!IsValidGCMTagLength (auth_tag_len)) {
2799
+ char msg[50 ];
2800
+ snprintf (msg, sizeof (msg),
2801
+ " Invalid GCM authentication tag length: %u" , auth_tag_len);
2802
+ env ()->ThrowError (msg);
2803
+ return false ;
2804
+ }
2805
+
2806
+ // Remember the given authentication tag length for later.
2807
+ auth_tag_len_ = auth_tag_len;
2808
+ }
2809
+ } else {
2789
2810
if (auth_tag_len == kNoAuthTagLength ) {
2790
2811
char msg[128 ];
2791
2812
snprintf (msg, sizeof (msg), " authTagLength required for %s" , cipher_type);
@@ -2818,21 +2839,6 @@ bool CipherBase::InitAuthenticated(const char* cipher_type, int iv_len,
2818
2839
if (iv_len == 12 ) max_message_size_ = 16777215 ;
2819
2840
if (iv_len == 13 ) max_message_size_ = 65535 ;
2820
2841
}
2821
- } else {
2822
- CHECK_EQ (mode, EVP_CIPH_GCM_MODE);
2823
-
2824
- if (auth_tag_len != kNoAuthTagLength ) {
2825
- if (!IsValidGCMTagLength (auth_tag_len)) {
2826
- char msg[50 ];
2827
- snprintf (msg, sizeof (msg),
2828
- " Invalid GCM authentication tag length: %u" , auth_tag_len);
2829
- env ()->ThrowError (msg);
2830
- return false ;
2831
- }
2832
-
2833
- // Remember the given authentication tag length for later.
2834
- auth_tag_len_ = auth_tag_len;
2835
- }
2836
2842
}
2837
2843
2838
2844
return true ;
@@ -2855,8 +2861,7 @@ bool CipherBase::CheckCCMMessageLength(int message_len) {
2855
2861
bool CipherBase::IsAuthenticatedMode () const {
2856
2862
// Check if this cipher operates in an AEAD mode that we support.
2857
2863
CHECK (ctx_);
2858
- const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
2859
- return IsSupportedAuthenticatedMode (mode);
2864
+ return IsSupportedAuthenticatedMode (ctx_.get ());
2860
2865
}
2861
2866
2862
2867
@@ -2913,7 +2918,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
2913
2918
} else if (mode == EVP_CIPH_OCB_MODE) {
2914
2919
// At this point, the tag length is already known and must match the
2915
2920
// length of the given authentication tag.
2916
- CHECK (mode == EVP_CIPH_CCM_MODE || IS_OCB_MODE (mode ));
2921
+ CHECK (IsSupportedAuthenticatedMode (cipher-> ctx_ . get () ));
2917
2922
CHECK_NE (cipher->auth_tag_len_ , kNoAuthTagLength );
2918
2923
if (cipher->auth_tag_len_ != tag_len) {
2919
2924
char msg[50 ];
@@ -3120,7 +3125,7 @@ bool CipherBase::Final(unsigned char** out, int* out_len) {
3120
3125
*out = Malloc<unsigned char >(
3121
3126
static_cast <size_t >(EVP_CIPHER_CTX_block_size (ctx_.get ())));
3122
3127
3123
- if (kind_ == kDecipher && IsSupportedAuthenticatedMode (mode )) {
3128
+ if (kind_ == kDecipher && IsSupportedAuthenticatedMode (ctx_. get () )) {
3124
3129
MaybePassAuthTagToOpenSSL ();
3125
3130
}
3126
3131
0 commit comments