From 60ea150b1f535e4f0c76e23af4130b3861fabf54 Mon Sep 17 00:00:00 2001 From: "Dr. David von Oheimb" Date: Tue, 18 Oct 2022 18:12:38 +0200 Subject: [PATCH] CMS_decrypt_set1_*(): remove misleading error queue entry when recipient mismatch was not the issue Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale Reviewed-by: David von Oheimb (Merged from https://github.com/openssl/openssl/pull/19222) --- crypto/cms/cms_smime.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/crypto/cms/cms_smime.c b/crypto/cms/cms_smime.c index 6a94d6c5e86..69a35f74cb0 100644 --- a/crypto/cms/cms_smime.c +++ b/crypto/cms/cms_smime.c @@ -738,11 +738,8 @@ int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk, if (r < 0) return 0; } - /* - * If we have a cert try matching RecipientInfo otherwise try them - * all. - */ - else if (cert == NULL|| !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) { + /* If we have a cert, try matching RecipientInfo, else try them all */ + else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) { EVP_PKEY_up_ref(pk); CMS_RecipientInfo_set0_pkey(ri, pk); r = CMS_RecipientInfo_decrypt(cms, ri); @@ -779,7 +776,8 @@ int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk, return 1; } - ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); + if (!match_ri) + ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); return 0; } @@ -790,7 +788,7 @@ int CMS_decrypt_set1_key(CMS_ContentInfo *cms, { STACK_OF(CMS_RecipientInfo) *ris; CMS_RecipientInfo *ri; - int i, r; + int i, r, match_ri = 0; ris = CMS_get0_RecipientInfos(cms); for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { @@ -798,11 +796,10 @@ int CMS_decrypt_set1_key(CMS_ContentInfo *cms, if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK) continue; - /* - * If we have an id try matching RecipientInfo otherwise try them - * all. - */ - if (id == NULL || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) { + /* If we have an id, try matching RecipientInfo, else try them all */ + if (id == NULL + || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) { + match_ri = 1; CMS_RecipientInfo_set0_key(ri, key, keylen); r = CMS_RecipientInfo_decrypt(cms, ri); CMS_RecipientInfo_set0_key(ri, NULL, 0); @@ -816,7 +813,8 @@ int CMS_decrypt_set1_key(CMS_ContentInfo *cms, } } - ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); + if (!match_ri) + ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); return 0; } @@ -826,7 +824,7 @@ int CMS_decrypt_set1_password(CMS_ContentInfo *cms, { STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); CMS_RecipientInfo *ri; - int i, r; + int i, r, match_ri = 0; CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms); /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */ @@ -838,6 +836,9 @@ int CMS_decrypt_set1_password(CMS_ContentInfo *cms, ri = sk_CMS_RecipientInfo_value(ris, i); if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS) continue; + + /* Must try each PasswordRecipientInfo */ + match_ri = 1; CMS_RecipientInfo_set0_password(ri, pass, passlen); r = CMS_RecipientInfo_decrypt(cms, ri); CMS_RecipientInfo_set0_password(ri, NULL, 0); @@ -845,7 +846,8 @@ int CMS_decrypt_set1_password(CMS_ContentInfo *cms, return 1; } - ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); + if (!match_ri) + ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); return 0; } -- 2.47.3