Skip to content

Commit

Permalink
CMS_decrypt_set1_*(): fix NULL deref on unsuitable content type
Browse files Browse the repository at this point in the history
Fixes openssl#19975
for CMS_decrypt_set1_pkey_and_peer() in the obvious way,
and a related potential crash in CMS_decrypt_set1_password().

The point is that the input might have an unexpected content type,
so a guard is needed at both places after `ec` is obtained.

Note that in CMS_decrypt_set1_pkey_and_peer() there was
no such ec != NULL guard for
```
    if (ris != NULL)
        debug = ec->debug;
```
maybe because it is implied here by ris != NULL.
  • Loading branch information
DDvO committed Jan 2, 2023
1 parent 43a9e68 commit e126b85
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions crypto/cms/cms_smime.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,11 @@ int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
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;
if (ec != NULL) {
OPENSSL_clear_free(ec->key, ec->keylen);
ec->key = NULL;
ec->keylen = 0;
}

if (ris != NULL)
debug = ec->debug;
Expand Down Expand Up @@ -828,9 +830,11 @@ int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
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;
if (ec != NULL) {
OPENSSL_clear_free(ec->key, ec->keylen);
ec->key = NULL;
ec->keylen = 0;
}

for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
ri = sk_CMS_RecipientInfo_value(ris, i);
Expand Down

0 comments on commit e126b85

Please sign in to comment.