From 08cad1d5d4e840186d932d4ee28fa6e1463e3f28 Mon Sep 17 00:00:00 2001 From: jsing <> Date: Sat, 3 Sep 2022 17:47:47 +0000 Subject: Prepare to provide OPENSSL_cleanup. OPENSSL_cleanup() cleans up and deallocates memory in use by the library. There are a couple of use cases for this, primarily related to memory leak testing. This will not be called automatically in LibreSSL, which means that OpenSSL's OPENSSL_NO_INIT_ATEXIT is implied. If code wants to clean up then they need to explicitly call this themselves. ok tb@ --- src/lib/libcrypto/crypto_init.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/lib/libcrypto/crypto_init.c') diff --git a/src/lib/libcrypto/crypto_init.c b/src/lib/libcrypto/crypto_init.c index 67e7920890..69ba62991e 100644 --- a/src/lib/libcrypto/crypto_init.c +++ b/src/lib/libcrypto/crypto_init.c @@ -19,17 +19,21 @@ #include #include -#include #include -#include +#include #include +#include +#include #include "cryptlib.h" +#include "x509_issuer_cache.h" int OpenSSL_config(const char *); int OpenSSL_no_config(void); +static pthread_once_t crypto_init_once = PTHREAD_ONCE_INIT; static pthread_t crypto_init_thread; +static int crypto_init_cleaned_up; static void OPENSSL_init_crypto_internal(void) @@ -45,12 +49,15 @@ OPENSSL_init_crypto_internal(void) int OPENSSL_init_crypto(uint64_t opts, const void *settings) { - static pthread_once_t once = PTHREAD_ONCE_INIT; + if (crypto_init_cleaned_up) { + CRYPTOerror(ERR_R_INIT_FAIL); + return 0; + } if (pthread_equal(pthread_self(), crypto_init_thread)) return 1; /* don't recurse */ - if (pthread_once(&once, OPENSSL_init_crypto_internal) != 0) + if (pthread_once(&crypto_init_once, OPENSSL_init_crypto_internal) != 0) return 0; if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) && @@ -63,3 +70,16 @@ OPENSSL_init_crypto(uint64_t opts, const void *settings) return 1; } + +void +OPENSSL_cleanup(void) +{ + /* This currently calls init... */ + ERR_free_strings(); + + ENGINE_cleanup(); + EVP_cleanup(); + x509_issuer_cache_free(); + + crypto_init_cleaned_up = 1; +} -- cgit v1.2.3-55-g6feb