From 94940ab14441d88d95a22b537d6d84d78fc0910d Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Fri, 14 Feb 2020 16:58:01 +0000 Subject: SERVER-46174 Free peer certificate in SSL_get0_verified_chain polyfill (cherry picked from commit 98042804dff69afac74a7e2681efc0d00d207f2c) --- src/mongo/util/net/ssl_manager_openssl.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/mongo/util/net/ssl_manager_openssl.cpp b/src/mongo/util/net/ssl_manager_openssl.cpp index 99320199a4f..67913363c6b 100644 --- a/src/mongo/util/net/ssl_manager_openssl.cpp +++ b/src/mongo/util/net/ssl_manager_openssl.cpp @@ -88,6 +88,15 @@ struct UniqueX509StoreCtxDeleter { }; using UniqueX509StoreCtx = std::unique_ptr; +struct UniqueX509Deleter { + void operator()(X509* cert) { + if (cert) { + X509_free(cert); + } + } +}; +using UniqueX509 = std::unique_ptr; + // Because the hostname having a slash is used by `mongo::SockAddr` to determine if a hostname is a // Unix Domain Socket endpoint, this function uses the same logic. (See // `mongo::SockAddr::Sockaddr(StringData, int, sa_family_t)`). A user explicitly specifying a Unix @@ -208,11 +217,11 @@ struct VerifiedChainDeleter { STACK_OF(X509) * SSL_get0_verified_chain(SSL* s) { auto* store = SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)); - auto* peer = SSL_get_peer_certificate(s); + UniqueX509 peer(SSL_get_peer_certificate(s)); auto* peerChain = SSL_get_peer_cert_chain(s); UniqueX509StoreCtx ctx(X509_STORE_CTX_new()); - if (!X509_STORE_CTX_init(ctx.get(), store, peer, peerChain)) { + if (!X509_STORE_CTX_init(ctx.get(), store, peer.get(), peerChain)) { return nullptr; } @@ -374,14 +383,6 @@ using UniqueSSLContext = std::unique_ptr; static const int BUFFER_SIZE = 8 * 1024; static const int DATE_LEN = 128; -struct UniqueX509Free { - void operator()(X509* ptr) const { - X509_free(ptr); - } -}; - -using UniqueX509 = std::unique_ptr; - class SSLManagerOpenSSL : public SSLManagerInterface { public: explicit SSLManagerOpenSSL(const SSLParams& params, bool isServer); -- cgit v1.2.1