From e636d0b98aa8562280b56671d4f17d88754d7231 Mon Sep 17 00:00:00 2001
From: Jakub Zelenka <jakub.openssl@gmail.com>
Date: Thu, 22 May 2025 18:40:30 +0200
Subject: [PATCH 1/3] CMS: Produce error when AEAD algorithms are used in
 enveloped data

Fixes GH-21414

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/27772)
---
 crypto/cms/cms_enc.c                                |  8 ++++++--
 crypto/cms/cms_env.c                                |  6 +++---
 crypto/cms/cms_err.c                                |  4 +++-
 crypto/cms/cms_local.h                              |  2 +-
 crypto/err/openssl.txt                              |  3 ++-
 include/crypto/cmserr.h                             |  2 +-
 include/openssl/cmserr.h                            |  4 ++--
 test/cms-msg/enveloped-content-type-for-aes-gcm.pem |  7 +++++++
 test/cmsapitest.c                                   |  6 ++++++
 test/recipes/80-test_cms.t                          | 12 +++++++++++-
 10 files changed, 42 insertions(+), 12 deletions(-)
 create mode 100644 test/cms-msg/enveloped-content-type-for-aes-gcm.pem

Index: openssl-3.1.4/crypto/cms/cms_enc.c
===================================================================
--- openssl-3.1.4.orig/crypto/cms/cms_enc.c
+++ openssl-3.1.4/crypto/cms/cms_enc.c
@@ -23,7 +23,7 @@
 /* Return BIO based on EncryptedContentInfo and key */
 
 BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
-                                        const CMS_CTX *cms_ctx)
+                                        const CMS_CTX *cms_ctx, int auth)
 {
     BIO *b;
     EVP_CIPHER_CTX *ctx;
@@ -104,13 +104,19 @@ BIO *ossl_cms_EncryptedContent_init_bio(
             goto err;
         }
         if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
+            if (!auth) {
+                ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_IN_ENVELOPED_DATA);
+                goto err;
+            }
             piv = aparams.iv;
-            if (ec->taglen > 0
-                    && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
-                                           ec->taglen, ec->tag) <= 0) {
+            if (ec->taglen < 4 || ec->taglen > 16
+                || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (int)ec->taglen, ec->tag) <= 0) {
                 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR);
                 goto err;
             }
+        } else if (auth) {
+            ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
+            goto err;
         }
     }
     len = EVP_CIPHER_CTX_get_key_length(ctx);
@@ -255,5 +261,5 @@ BIO *ossl_cms_EncryptedData_init_bio(con
     if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
         enc->version = 2;
     return ossl_cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
-                                              ossl_cms_get0_cmsctx(cms));
+                                              ossl_cms_get0_cmsctx(cms), 0);
 }
Index: openssl-3.1.4/crypto/cms/cms_env.c
===================================================================
--- openssl-3.1.4.orig/crypto/cms/cms_env.c
+++ openssl-3.1.4/crypto/cms/cms_env.c
@@ -1120,7 +1120,7 @@ static BIO *cms_EnvelopedData_Decryption
 {
     CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
     BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec,
-                                                         ossl_cms_get0_cmsctx(cms));
+                                                         ossl_cms_get0_cmsctx(cms), 0);
     EVP_CIPHER_CTX *ctx = NULL;
 
     if (contentBio == NULL)
@@ -1156,7 +1156,7 @@ static BIO *cms_EnvelopedData_Encryption
     /* Get BIO first to set up key */
 
     ec = env->encryptedContentInfo;
-    ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
+    ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms), 0);
 
     /* If error end of processing */
     if (!ret)
@@ -1208,7 +1208,7 @@ BIO *ossl_cms_AuthEnvelopedData_init_bio
         ec->tag = aenv->mac->data;
         ec->taglen = aenv->mac->length;
     }
-    ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
+    ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms), 1);
 
     /* If error or no cipher end of processing */
     if (ret == NULL || ec->cipher == NULL)
Index: openssl-3.1.4/crypto/cms/cms_err.c
===================================================================
--- openssl-3.1.4.orig/crypto/cms/cms_err.c
+++ openssl-3.1.4/crypto/cms/cms_err.c
@@ -25,6 +25,8 @@ static const ERR_STRING_DATA CMS_str_rea
     "certificate has no keyid"},
     {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CERTIFICATE_VERIFY_ERROR),
     "certificate verify error"},
+    { ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CIPHER_AEAD_IN_ENVELOPED_DATA),
+    "cipher aead in enveloped data" },
     {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CIPHER_AEAD_SET_TAG_ERROR),
     "cipher aead set tag error"},
     {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CIPHER_GET_TAG), "cipher get tag"},
Index: openssl-3.1.4/crypto/cms/cms_local.h
===================================================================
--- openssl-3.1.4.orig/crypto/cms/cms_local.h
+++ openssl-3.1.4/crypto/cms/cms_local.h
@@ -429,7 +429,7 @@ int ossl_cms_set1_ias(CMS_IssuerAndSeria
 int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
 
 BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
-                                        const CMS_CTX *ctx);
+                                        const CMS_CTX *ctx, int auth);
 BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms);
 int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
                                    const EVP_CIPHER *cipher,
Index: openssl-3.1.4/crypto/err/openssl.txt
===================================================================
--- openssl-3.1.4.orig/crypto/err/openssl.txt
+++ openssl-3.1.4/crypto/err/openssl.txt
@@ -284,6 +284,7 @@ CMS_R_ATTRIBUTE_ERROR:161:attribute erro
 CMS_R_CERTIFICATE_ALREADY_PRESENT:175:certificate already present
 CMS_R_CERTIFICATE_HAS_NO_KEYID:160:certificate has no keyid
 CMS_R_CERTIFICATE_VERIFY_ERROR:100:certificate verify error
+CMS_R_CIPHER_AEAD_IN_ENVELOPED_DATA:182:cipher aead in enveloped data
 CMS_R_CIPHER_AEAD_SET_TAG_ERROR:184:cipher aead set tag error
 CMS_R_CIPHER_GET_TAG:185:cipher get tag
 CMS_R_CIPHER_INITIALISATION_ERROR:101:cipher initialisation error
Index: openssl-3.1.4/include/openssl/cmserr.h
===================================================================
--- openssl-3.1.4.orig/include/openssl/cmserr.h
+++ openssl-3.1.4/include/openssl/cmserr.h
@@ -28,6 +28,7 @@
 #  define CMS_R_CERTIFICATE_ALREADY_PRESENT                175
 #  define CMS_R_CERTIFICATE_HAS_NO_KEYID                   160
 #  define CMS_R_CERTIFICATE_VERIFY_ERROR                   100
+#  define CMS_R_CIPHER_AEAD_IN_ENVELOPED_DATA              182
 #  define CMS_R_CIPHER_AEAD_SET_TAG_ERROR                  184
 #  define CMS_R_CIPHER_GET_TAG                             185
 #  define CMS_R_CIPHER_INITIALISATION_ERROR                101
