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 <[email protected]>
Reviewed-by: Todd Short <[email protected]>
(Merged from openssl/openssl#22741)

(cherry picked from commit 3af29bf9f99d3e0e90cc72180898802375b88d3b)
Signed-off-by: hhhFun <[email protected]>
  • Loading branch information
bernd-edlinger authored and hhhFun committed Jul 9, 2024
1 parent 30dd000 commit 0506172
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 @@ -1045,31 +1045,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 0506172

Please sign in to comment.