Skip to content

Commit

Permalink
Fix potential memory leak in PKCS12_add_key_ex()
Browse files Browse the repository at this point in the history
p8 is allocated using EVP_PKEY2PKCS8(), but when PKCS8_add_keyusage()
fails this memory is not freed. Fix this by adding a call to
PKCS8_PRIV_KEY_INFO_free().

Reviewed-by: Frederik Wedel-Heinen <[email protected]>
Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#25818)
  • Loading branch information
nielsdos authored and t8m committed Jan 6, 2025
1 parent 94f95ef commit f822a48
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crypto/pkcs12/p12_crt.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ PKCS12_SAFEBAG *PKCS12_add_key_ex(STACK_OF(PKCS12_SAFEBAG) **pbags,
/* Make a PKCS#8 structure */
if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
goto err;
if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
if (key_usage && !PKCS8_add_keyusage(p8, key_usage)) {
PKCS8_PRIV_KEY_INFO_free(p8);
goto err;
}
if (nid_key != -1) {
/* This call does not take ownership of p8 */
bag = PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(nid_key, pass, -1, NULL, 0,
Expand Down

0 comments on commit f822a48

Please sign in to comment.