diff --git a/include/aws/cal/cal.h b/include/aws/cal/cal.h index 1a580960..32b69bfa 100644 --- a/include/aws/cal/cal.h +++ b/include/aws/cal/cal.h @@ -45,6 +45,11 @@ AWS_EXTERN_C_BEGIN AWS_CAL_API void aws_cal_library_init(struct aws_allocator *allocator); AWS_CAL_API void aws_cal_library_clean_up(void); +/* + * Every CRT thread that might invoke aws-lc functionality should call this as part of the thread at_exit process + */ +AWS_CAL_API void aws_cal_thread_clean_up(void); + AWS_EXTERN_C_END AWS_POP_SANE_WARNING_LEVEL diff --git a/source/cal.c b/source/cal.c index 13477c8d..66848aa1 100644 --- a/source/cal.c +++ b/source/cal.c @@ -70,6 +70,7 @@ static struct aws_log_subject_info_list s_cal_log_subject_list = { #ifndef BYO_CRYPTO extern void aws_cal_platform_init(struct aws_allocator *allocator); extern void aws_cal_platform_clean_up(void); +extern void aws_cal_platform_thread_clean_up(void); #endif /* BYO_CRYPTO */ static bool s_cal_library_initialized = false; @@ -96,3 +97,9 @@ void aws_cal_library_clean_up(void) { aws_common_library_clean_up(); } } + +void aws_cal_thread_clean_up(void) { +#ifndef BYO_CRYPTO + aws_cal_platform_thread_clean_up(); +#endif /* BYO_CRYPTO */ +} diff --git a/source/darwin/commoncrypto_platform_init.c b/source/darwin/commoncrypto_platform_init.c index decedcda..f2da2805 100644 --- a/source/darwin/commoncrypto_platform_init.c +++ b/source/darwin/commoncrypto_platform_init.c @@ -10,3 +10,5 @@ void aws_cal_platform_init(struct aws_allocator *allocator) { } void aws_cal_platform_clean_up(void) {} + +void aws_cal_platform_thread_clean_up(void) {} diff --git a/source/unix/openssl_platform_init.c b/source/unix/openssl_platform_init.c index 93d3201a..550f2248 100644 --- a/source/unix/openssl_platform_init.c +++ b/source/unix/openssl_platform_init.c @@ -13,6 +13,8 @@ #include +#include + static struct openssl_hmac_ctx_table hmac_ctx_table; static struct openssl_evp_md_ctx_table evp_md_ctx_table; @@ -651,12 +653,24 @@ void aws_cal_platform_clean_up(void) { } #endif +#if defined(OPENSSL_IS_AWSLC) + AWSLC_thread_local_clear(); + AWSLC_thread_local_shutdown(); +#endif + if (s_libcrypto_module) { dlclose(s_libcrypto_module); } s_libcrypto_allocator = NULL; } + +void aws_cal_platform_thread_clean_up(void) { +#if defined(OPENSSL_IS_AWSLC) + AWSLC_thread_local_clear(); +#endif +} + #if !defined(__GNUC__) || (__GNUC__ >= 4 && __GNUC_MINOR__ > 1) # pragma GCC diagnostic pop #endif diff --git a/source/windows/bcrypt_platform_init.c b/source/windows/bcrypt_platform_init.c index decedcda..f2da2805 100644 --- a/source/windows/bcrypt_platform_init.c +++ b/source/windows/bcrypt_platform_init.c @@ -10,3 +10,5 @@ void aws_cal_platform_init(struct aws_allocator *allocator) { } void aws_cal_platform_clean_up(void) {} + +void aws_cal_platform_thread_clean_up(void) {}