Fix a memory leak in rsa_priv_encode
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Wed, 16 Nov 2022 11:32:06 +0000 (12:32 +0100)
committerTomas Mraz <tomas@openssl.org>
Fri, 16 Dec 2022 18:05:37 +0000 (19:05 +0100)
If PKCS8_pkey_set0 fails, the memory in rk need to be clear freed
otherwise it is owned by the PKCS8_PRIV_KEY_INFO.

Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19694)

crypto/rsa/rsa_ameth.c

index fb045544a832fbafed31132daaa06fae82724f26..2c9c46ea53c8becaf1f43e9eff467e135e2e760e 100644 (file)
@@ -172,6 +172,7 @@ static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
                          strtype, str, rk, rklen)) {
         RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
         ASN1_STRING_free(str);
+        OPENSSL_clear_free(rk, rklen);
         return 0;
     }