Skip to content

Commit

Permalink
Fix a possible memory leak in CMS_add_simple_smimecap
Browse files Browse the repository at this point in the history
The return code of X509_ALGOR_set0 was not checked,
and if it fails the key will be leaked.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #22741)

(cherry picked from commit 3af29bf)
  • Loading branch information
bernd-edlinger committed Dec 4, 2023
1 parent ad56250 commit a72abc4
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions crypto/cms/cms_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1037,31 +1037,32 @@ int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
int algnid, int keysize)
{
X509_ALGOR *alg;
X509_ALGOR *alg = NULL;
ASN1_INTEGER *key = NULL;

if (keysize > 0) {
key = ASN1_INTEGER_new();
if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
ASN1_INTEGER_free(key);
return 0;
}
if (key == NULL || !ASN1_INTEGER_set(key, keysize))
goto err;
}
alg = X509_ALGOR_new();
if (alg == NULL) {
ASN1_INTEGER_free(key);
return 0;
}
if (alg == NULL)
goto err;

X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
if (!X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key))
goto err;
key = NULL;
if (*algs == NULL)
*algs = sk_X509_ALGOR_new_null();
if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
X509_ALGOR_free(alg);
return 0;
}
if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg))
goto err;
return 1;

err:
ASN1_INTEGER_free(key);
X509_ALGOR_free(alg);
return 0;
}

/* Check to see if a cipher exists and if so add S/MIME capabilities */
Expand Down

0 comments on commit a72abc4

Please sign in to comment.