Skip to content

Commit

Permalink
CMS and PKCS7: fix handlling of EVP_PKEY_get_size() failure
Browse files Browse the repository at this point in the history
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #22459)
  • Loading branch information
DDvO authored and hlandau committed Oct 26, 2023
1 parent f03ce9e commit d7ad09d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
7 changes: 3 additions & 4 deletions crypto/cms/cms_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,7 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
md = computed_md;
}
siglen = EVP_PKEY_get_size(si->pkey);
sig = OPENSSL_malloc(siglen);
if (sig == NULL)
if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
goto err;
if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
OPENSSL_free(sig);
Expand All @@ -780,8 +779,8 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
ERR_raise(ERR_LIB_CMS, CMS_R_OPERATION_UNSUPPORTED);
goto err;
}
sig = OPENSSL_malloc(EVP_PKEY_get_size(si->pkey));
if (sig == NULL)
siglen = EVP_PKEY_get_size(si->pkey);
if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
goto err;
if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
ossl_cms_ctx_get0_libctx(ctx),
Expand Down
7 changes: 3 additions & 4 deletions crypto/pkcs7/pk7_doit.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,9 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
goto err;
} else {
unsigned char *abuf = NULL;
unsigned int abuflen;
abuflen = EVP_PKEY_get_size(si->pkey);
abuf = OPENSSL_malloc(abuflen);
if (abuf == NULL)
unsigned int abuflen = EVP_PKEY_get_size(si->pkey);

if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL)
goto err;

if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
Expand Down

0 comments on commit d7ad09d

Please sign in to comment.