From faa09ea55ad75a791abdfee533bf1c0c833cedbd Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 10 Jan 2021 21:09:14 +0100 Subject: [PATCH] fixed small memleak in authssl --- libretroshare/src/pqi/authssl.cc | 24 +++++++++++++++++++----- libretroshare/src/pqi/authssl.h | 2 ++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libretroshare/src/pqi/authssl.cc b/libretroshare/src/pqi/authssl.cc index a31970ba0..eb89a1ed5 100644 --- a/libretroshare/src/pqi/authssl.cc +++ b/libretroshare/src/pqi/authssl.cc @@ -308,6 +308,20 @@ AuthSSLimpl::AuthSSLimpl() : p3Config(), sslctx(nullptr), mOwnCert(nullptr), sslMtx("AuthSSL"), mOwnPrivateKey(nullptr), mOwnPublicKey(nullptr), init(0) {} +AuthSSLimpl::~AuthSSLimpl() +{ + RS_STACK_MUTEX(sslMtx); + + SSL_CTX_free(sslctx); + X509_free(mOwnCert); + + EVP_PKEY_free(mOwnPrivateKey); + EVP_PKEY_free(mOwnPublicKey); + + for(auto pcert: mCerts) + X509_free(pcert.second); +} + bool AuthSSLimpl::active() { return init; } int AuthSSLimpl::InitAuth( @@ -1459,14 +1473,14 @@ bool AuthSSLimpl::encrypt(void *&out, int &outlen, const void *in, int inlen, if (peerId == mOwnId) { public_key = mOwnPublicKey; } else { - if (!mCerts[peerId]) + auto it = mCerts.find(peerId); + + if (it == mCerts.end()) { - RsErr() << __PRETTY_FUNCTION__ << " public key not found." - << std::endl; + RsErr() << __PRETTY_FUNCTION__ << " public key not found." << std::endl; return false; } - else public_key = const_cast( - RsX509Cert::getPubKey(*mCerts[peerId]) ); + else public_key = const_cast( RsX509Cert::getPubKey(*it->second) ); } EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); diff --git a/libretroshare/src/pqi/authssl.h b/libretroshare/src/pqi/authssl.h index b63bd0e7a..ddd5dc86d 100644 --- a/libretroshare/src/pqi/authssl.h +++ b/libretroshare/src/pqi/authssl.h @@ -161,6 +161,8 @@ public: /** Initialisation Functions (Unique) */ AuthSSLimpl(); + virtual ~AuthSSLimpl(); + bool validateOwnCertificate(X509 *x509, EVP_PKEY *pkey) override; bool active() override;