From: Dr. David von Oheimb Date: Fri, 14 Oct 2022 10:56:54 +0000 (+0200) Subject: CMS_decrypt_set1_password(): prevent mem leak on any previously set decryption key X-Git-Tag: openssl-3.2.0-alpha1~1669 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=911045afda06bec038ccd15e9f849bff05b6f1ee;p=thirdparty%2Fopenssl.git CMS_decrypt_set1_password(): prevent mem leak on any previously set decryption key Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale Reviewed-by: David von Oheimb (Merged from https://github.com/openssl/openssl/pull/19222) --- diff --git a/crypto/cms/cms_smime.c b/crypto/cms/cms_smime.c index e49c611a289..6a94d6c5e86 100644 --- a/crypto/cms/cms_smime.c +++ b/crypto/cms/cms_smime.c @@ -703,7 +703,7 @@ int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert) int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert, X509 *peer) { - STACK_OF(CMS_RecipientInfo) *ris; + STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); CMS_RecipientInfo *ri; int i, r, cms_pkey_ri_type; int debug = 0, match_ri = 0; @@ -714,7 +714,6 @@ int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk, ec->key = NULL; ec->keylen = 0; - ris = CMS_get0_RecipientInfos(cms); if (ris != NULL) debug = ec->debug; @@ -825,11 +824,16 @@ int CMS_decrypt_set1_key(CMS_ContentInfo *cms, int CMS_decrypt_set1_password(CMS_ContentInfo *cms, unsigned char *pass, ossl_ssize_t passlen) { - STACK_OF(CMS_RecipientInfo) *ris; + STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); CMS_RecipientInfo *ri; int i, r; + CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms); + + /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */ + OPENSSL_clear_free(ec->key, ec->keylen); + ec->key = NULL; + ec->keylen = 0; - ris = CMS_get0_RecipientInfos(cms); for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { ri = sk_CMS_RecipientInfo_value(ris, i); if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)