<div dir="ltr">Hi all,<div><br></div><div>I'm trying to write an application in which I create an instance of SSL_CTX* using SSL_CTX_new(), and set the following things in it:</div><div><br></div><div>(1) An EVP_PKEY* :</div><div>1a> created with PEM_read_bio_PrivateKey().</div><div>1b> set in the ctx using SSL_CTX_use_PrivateKey().</div><div><br></div><div>(2) A number of X509* instances (cuz chained certs) :</div><div>2a> all created with PEM_read_bio_X509().</div><div>2b> set in the ctx using SSL_CTX_use_certificate() or SSL_CTX_add_extra_chain_cert().</div><div><br></div><div>At the end, I use SSL_CTX_free() to free up the ctx. According to the man page for SSL_CTX_free():</div><div><br></div><div><div>"SSL_CTX_free() decrements the reference count of ctx, and removes the SSL_CTX object pointed to by ctx and frees up the allocated memory if the the reference count has reached 0.</div><div>       It also calls the free()ing procedures for indirectly affected items, if applicable: the session cache, the list of ciphers, the list of Client CAs, the certificates and keys. "</div></div><div><br></div><div>... which tells me that freeing the SSL_CTX should free up its memory as well as the things I set inside of it (unless I'm interpreting it super wrong?) like " ... certificates and keys".</div><div>The problem is, when run my application under valgrind, I keep seeing memory leaks for both steps (1a) and (2a).</div><div><br></div><div>I tried to get rid of them, by using EVP_PKEY_free() after I'm done setting in step (1b). This works, and the leak for step (1a) goes away.</div><div>When I try to do the same for step (2), i.e. calling X509_free() after every successful "set" call, I get a coredump (backtrace is attached: bt_1.txt), coming out of SSL_CTX_free, suggesting that I did something wrong.</div><div><br></div><div><br></div><div>Which brings me to my question, does anyone know the correct way to free memory in SSL_CTX ? Or, what's wrong with my steps? The application doesn't even perform SSL yet, I'm just trying to create/destroy SSL_CTX objects without leaks first. Any help would be appreciated!</div><div><br></div><div><br></div><div>Thanks in advance,</div><div>-Pratyush.</div></div>