Skip to content

Commit

Permalink
free oaep label-octet-string on error
Browse files Browse the repository at this point in the history
When successful, ossl_X509_ALGOR_from_nid() returns a pointer to an
X509_ALGOR object.  Inside ossl_X509_ALGOR_from_nid(),
X509_ALGOR_set0() is called, and this passes ownership of the ASN1
object "los" (label octet string) to the X509_ALGOR object.  When
ossl_X509_ALGOR_from_nid() fails, ownership has not been passed on and
we need to free "los".

Change the scope of "los" and ensure it is freed on failure (on
success, set it to NULL so it is not freed inside the function).

Fixes #22336

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #22495)

(cherry picked from commit 83efd71)
  • Loading branch information
James Muir authored and hlandau committed Oct 30, 2023
1 parent caa16b9 commit f770578
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions crypto/cms/cms_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
const EVP_MD *md, *mgf1md;
RSA_OAEP_PARAMS *oaep = NULL;
ASN1_STRING *os = NULL;
ASN1_OCTET_STRING *los = NULL;
X509_ALGOR *alg;
EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
Expand Down Expand Up @@ -147,20 +148,21 @@ static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
if (!ossl_x509_algor_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
goto err;
if (labellen > 0) {
ASN1_OCTET_STRING *los = ASN1_OCTET_STRING_new();
los = ASN1_OCTET_STRING_new();

if (los == NULL)
goto err;
if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
ASN1_OCTET_STRING_free(los);
if (!ASN1_OCTET_STRING_set(los, label, labellen))
goto err;
}

oaep->pSourceFunc = ossl_X509_ALGOR_from_nid(NID_pSpecified,
V_ASN1_OCTET_STRING, los);
if (oaep->pSourceFunc == NULL)
goto err;

los = NULL;
}
/* create string with pss parameter encoding. */
/* create string with oaep parameter encoding. */
if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
goto err;
if (!X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os))
Expand All @@ -170,6 +172,7 @@ static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
err:
RSA_OAEP_PARAMS_free(oaep);
ASN1_STRING_free(os);
ASN1_OCTET_STRING_free(los);
return rv;
}

Expand Down

0 comments on commit f770578

Please sign in to comment.