From 52b18ce10498dc2d8ced7db31ead116f4eeca134 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Tue, 28 May 2019 16:21:19 +0100 Subject: [PATCH] Add the function OPENSSL_thread_stop_ex() This adds the ability to clean up a thread on a per OPENSSL_CTX basis. Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/9040) --- crypto/initthread.c | 11 +++++++++++ include/openssl/crypto.h | 1 + test/evp_extra_test.c | 5 +++++ util/libcrypto.num | 1 + 4 files changed, 18 insertions(+) diff --git a/crypto/initthread.c b/crypto/initthread.c index 124fdccd6e..02a51ee5d2 100644 --- a/crypto/initthread.c +++ b/crypto/initthread.c @@ -97,6 +97,17 @@ void cleanup_thread(void) destructor_key.sane = -1; } +void OPENSSL_thread_stop_ex(OPENSSL_CTX *ctx) +{ + ctx = openssl_ctx_get_concrete(ctx); + /* + * TODO(3.0). It would be nice if we could figure out a way to do this on + * all threads that have used the OPENSSL_CTX when the OPENSSL_CTX is freed. + * This is currently not possible due to the use of thread local variables. + */ + ossl_ctx_thread_stop(ctx); +} + void OPENSSL_thread_stop(void) { if (destructor_key.sane != -1) { diff --git a/include/openssl/crypto.h b/include/openssl/crypto.h index 5322cc8f04..79531196ed 100644 --- a/include/openssl/crypto.h +++ b/include/openssl/crypto.h @@ -415,6 +415,7 @@ void OPENSSL_cleanup(void); int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings); int OPENSSL_atexit(void (*handler)(void)); void OPENSSL_thread_stop(void); +void OPENSSL_thread_stop_ex(OPENSSL_CTX *ctx); /* Low-level control of initialization */ OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void); diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c index 6fca3a38f5..5bf4a8c1f3 100644 --- a/test/evp_extra_test.c +++ b/test/evp_extra_test.c @@ -1220,6 +1220,11 @@ static int test_EVP_MD_fetch(int tst) EVP_MD_meth_free(md); OSSL_PROVIDER_unload(defltprov); OSSL_PROVIDER_unload(fipsprov); + /* Not normally needed, but we would like to test that + * OPENSSL_thread_stop_ex() behaves as expected. + */ + if (ctx != NULL) + OPENSSL_thread_stop_ex(ctx); OPENSSL_CTX_free(ctx); return ret; } diff --git a/util/libcrypto.num b/util/libcrypto.num index 7280649920..0b181070eb 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -4831,3 +4831,4 @@ OPENSSL_CTX_get0_public_drbg 4775 3_0_0 EXIST::FUNCTION: OPENSSL_CTX_get0_private_drbg 4776 3_0_0 EXIST::FUNCTION: BN_CTX_new_ex 4777 3_0_0 EXIST::FUNCTION: BN_CTX_secure_new_ex 4778 3_0_0 EXIST::FUNCTION: +OPENSSL_thread_stop_ex 4779 3_0_0 EXIST::FUNCTION: -- 2.25.1