From 1e4918395700384f5eea378b3dfcbe4f3dce400f Mon Sep 17 00:00:00 2001 From: John Galea Date: Tue, 5 Mar 2024 17:44:50 +0100 Subject: [PATCH] Fix memory leaks when parsing PKCS#12 containers Frees unnecessary objects when parsing PKCS#12 containers, thus fixing memory leak. --- src/openssl_wrap.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/openssl_wrap.cpp b/src/openssl_wrap.cpp index f163f52..56404ee 100644 --- a/src/openssl_wrap.cpp +++ b/src/openssl_wrap.cpp @@ -1819,6 +1819,7 @@ SSL_X509_Ptr _parseCertificateFromPkcs12(PKCS12 *p12, const std::string &pwd) X509 *cert = nullptr; OpensslCallIsPositive::callChecked( lib::OpenSSLLib::SSL_PKCS12_parse, p12, pwd.c_str(), &pkey, &cert, nullptr); + lib::OpenSSLLib::SSL_EVP_PKEY_free(pkey); if (cert == nullptr) { throw OpenSSLException("Cannot parse certificate from pkcs12 container. Not available"); @@ -1838,6 +1839,8 @@ SSL_STACK_OWNER_X509_Ptr _parseAdditionalCertsFromPkcs12(PKCS12 *p12, const std: auto additionalCerts = createOpenSSLObject(); OpensslCallIsPositive::callChecked( lib::OpenSSLLib::SSL_PKCS12_parse, p12, pwd.c_str(), &pkey, &cert, &additionalCerts); + lib::OpenSSLLib::SSL_EVP_PKEY_free(pkey); + lib::OpenSSLLib::SSL_X509_free(cert); return SSL_STACK_OWNER_X509_Ptr{additionalCerts}; }