#include <openssl/crypto.h>
#include <openssl/core_dispatch.h>
#include "crypto/cryptlib.h"
#include "prov/providercommon.h"
#include "internal/thread_once.h"
#include "internal/threads_common.h"
#include "crypto/context.h"
#ifdef FIPS_MODULE
#include "prov/provider_ctx.h"
extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
#endif
typedef struct thread_event_handler_st THREAD_EVENT_HANDLER;
struct thread_event_handler_st {
#ifndef FIPS_MODULE
const void *index;
#endif
void *arg;
OSSL_thread_stop_handler_fn handfn;
THREAD_EVENT_HANDLER *next;
};
#ifndef FIPS_MODULE
DEFINE_SPECIAL_STACK_OF(THREAD_EVENT_HANDLER_PTR, THREAD_EVENT_HANDLER *)
typedef struct global_tevent_register_st GLOBAL_TEVENT_REGISTER;
struct global_tevent_register_st {
STACK_OF(THREAD_EVENT_HANDLER_PTR) *skhands;
CRYPTO_RWLOCK *lock;
};
static GLOBAL_TEVENT_REGISTER *glob_tevent_reg = NULL;
static CRYPTO_ONCE tevent_register_runonce = CRYPTO_ONCE_STATIC_INIT;
DEFINE_RUN_ONCE_STATIC(create_global_tevent_register)
{
glob_tevent_reg = OPENSSL_zalloc(sizeof(*glob_tevent_reg));
if (glob_tevent_reg == NULL)
return 0;
glob_tevent_reg->skhands = sk_THREAD_EVENT_HANDLER_PTR_new_null();
glob_tevent_reg->lock = CRYPTO_THREAD_lock_new();
if (glob_tevent_reg->skhands == NULL || glob_tevent_reg->lock == NULL) {
sk_THREAD_EVENT_HANDLER_PTR_free(glob_tevent_reg->skhands);
CRYPTO_THREAD_lock_free(glob_tevent_reg->lock);
OPENSSL_free(glob_tevent_reg);
glob_tevent_reg = NULL;
return 0;
}
return 1;
}
static GLOBAL_TEVENT_REGISTER *get_global_tevent_register(void)
{
if (!RUN_ONCE(&tevent_register_runonce, create_global_tevent_register))
return NULL;
return glob_tevent_reg;
}
#endif
#ifndef FIPS_MODULE
static union {
long sane;
CRYPTO_THREAD_LOCAL value;
} destructor_key = { -1 };
static int init_thread_push_handlers(THREAD_EVENT_HANDLER **hands);
static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin);
static void init_thread_destructor(void *hands);
static int init_thread_deregister(void *arg, int all);
#endif
static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands);
static THREAD_EVENT_HANDLER **get_thread_event_handler(OSSL_LIB_CTX *ctx)
{
#ifdef FIPS_MODULE
return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, ctx);
#else
if (destructor_key.sane != -1)
return CRYPTO_THREAD_get_local(&destructor_key.value);
return NULL;
#endif
}
static int set_thread_event_handler(OSSL_LIB_CTX *ctx, THREAD_EVENT_HANDLER **hands)
{
#ifdef FIPS_MODULE
return CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, ctx, hands);
#else
if (destructor_key.sane != -1)
return CRYPTO_THREAD_set_local(&destructor_key.value, hands);
return 0;
#endif
}
static THREAD_EVENT_HANDLER **
manage_thread_local(OSSL_LIB_CTX *ctx, int alloc, int keep)
{
THREAD_EVENT_HANDLER **hands = get_thread_event_handler(ctx);
if (alloc) {
if (hands == NULL) {
if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL)
return NULL;
if (!set_thread_event_handler(ctx, hands)) {
OPENSSL_free(hands);
return NULL;
}
#ifndef FIPS_MODULE
if (!init_thread_push_handlers(hands)) {
set_thread_event_handler(ctx, NULL);
OPENSSL_free(hands);
return NULL;
}
#endif
}
} else if (!keep) {
set_thread_event_handler(ctx, NULL);
}
return hands;
}
static ossl_inline THREAD_EVENT_HANDLER **clear_thread_local(OSSL_LIB_CTX *ctx)
{
return manage_thread_local(ctx, 0, 0);
}
static ossl_inline ossl_unused THREAD_EVENT_HANDLER **fetch_thread_local(OSSL_LIB_CTX *ctx)
{
return manage_thread_local(ctx, 0, 1);
}
static ossl_inline THREAD_EVENT_HANDLER **alloc_thread_local(OSSL_LIB_CTX *ctx)
{
return manage_thread_local(ctx, 1, 0);
}
#ifndef FIPS_MODULE
static int init_thread_push_handlers(THREAD_EVENT_HANDLER **hands)
{
int ret;
GLOBAL_TEVENT_REGISTER *gtr;
gtr = get_global_tevent_register();
if (gtr == NULL)
return 0;
if (!CRYPTO_THREAD_write_lock(gtr->lock))
return 0;
ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0);
CRYPTO_THREAD_unlock(gtr->lock);
return ret;
}
static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin)
{
GLOBAL_TEVENT_REGISTER *gtr;
int i;
gtr = get_global_tevent_register();
if (gtr == NULL)
return;
if (!CRYPTO_THREAD_write_lock(gtr->lock))
return;
for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
THREAD_EVENT_HANDLER **hands
= sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
if (hands == handsin) {
sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i);
CRYPTO_THREAD_unlock(gtr->lock);
return;
}
}
CRYPTO_THREAD_unlock(gtr->lock);
return;
}
static void init_thread_destructor(void *hands)
{
init_thread_stop(NULL, (THREAD_EVENT_HANDLER **)hands);
init_thread_remove_handlers(hands);
OPENSSL_free(hands);
}
static CRYPTO_ONCE ossl_init_thread_runonce = CRYPTO_ONCE_STATIC_INIT;
#if defined(OPENSSL_SYS_WINDOWS)
static CRYPTO_THREAD_ID recursion_guard = (CRYPTO_THREAD_ID)-1;
#elif defined(OPENSSL_SYS_TANDEM) && (defined(_PUT_MODEL_) || defined(_KLT_MODEL_))
static CRYPTO_THREAD_ID recursion_guard = { (void *)-1, (short)-1, (short)-1 };
#else
static CRYPTO_THREAD_ID recursion_guard = (CRYPTO_THREAD_ID)0;
#endif
DEFINE_RUN_ONCE_STATIC(ossl_init_thread_once)
{
recursion_guard = CRYPTO_THREAD_get_current_id();
if (!CRYPTO_THREAD_init_local(&destructor_key.value,
init_thread_destructor))
return 0;
#if defined(OPENSSL_SYS_TANDEM)
memset(&recursion_guard, 0, sizeof(recursion_guard));
#else
recursion_guard = (CRYPTO_THREAD_ID)0;
#endif
return 1;
}
int ossl_init_thread(void)
{
if (CRYPTO_THREAD_compare_id(recursion_guard,
CRYPTO_THREAD_get_current_id()))
return 1;
if (!RUN_ONCE(&ossl_init_thread_runonce, ossl_init_thread_once))
return 0;
return 1;
}
void ossl_cleanup_thread(void)
{
init_thread_deregister(NULL, 1);
CRYPTO_THREAD_cleanup_local(&destructor_key.value);
destructor_key.sane = -1;
}
void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx)
{
ctx = ossl_lib_ctx_get_concrete(ctx);
ossl_ctx_thread_stop(ctx);
}
void OPENSSL_thread_stop(void)
{
if (destructor_key.sane != -1) {
THREAD_EVENT_HANDLER **hands = clear_thread_local(NULL);
init_thread_stop(NULL, hands);
init_thread_remove_handlers(hands);
OPENSSL_free(hands);
CRYPTO_THREAD_clean_local();
}
}
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
{
if (destructor_key.sane != -1) {
THREAD_EVENT_HANDLER **hands = fetch_thread_local(ctx);
init_thread_stop(ctx, hands);
}
}
#else
static void ossl_arg_thread_stop(void *arg);
int ossl_thread_register_fips(OSSL_LIB_CTX *libctx)
{
return c_thread_start(FIPS_get_core_handle(libctx), ossl_arg_thread_stop,
libctx);
}
int ossl_thread_event_ctx_new(OSSL_LIB_CTX *libctx)
{
THREAD_EVENT_HANDLER **hands = NULL;
hands = OPENSSL_zalloc(sizeof(*hands));
if (hands == NULL)
goto err;
if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, libctx, hands))
goto err;
return 1;
err:
OPENSSL_free(hands);
return 0;
}
void ossl_thread_event_ctx_free(OSSL_LIB_CTX *ctx)
{
CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, ctx, NULL);
}
static void ossl_arg_thread_stop(void *arg)
{
ossl_ctx_thread_stop((OSSL_LIB_CTX *)arg);
}
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
{
THREAD_EVENT_HANDLER **hands;
hands = clear_thread_local(ctx);
init_thread_stop(ctx, hands);
OPENSSL_free(hands);
}
#endif
static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands)
{
THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp;
#ifndef FIPS_MODULE
GLOBAL_TEVENT_REGISTER *gtr;
#endif
if (hands == NULL)
return;
#ifndef FIPS_MODULE
gtr = get_global_tevent_register();
if (gtr == NULL)
return;
if (!CRYPTO_THREAD_write_lock(gtr->lock))
return;
#endif
curr = *hands;
while (curr != NULL) {
if (arg != NULL && curr->arg != arg) {
prev = curr;
curr = curr->next;
continue;
}
curr->handfn(curr->arg);
if (prev == NULL)
*hands = curr->next;
else
prev->next = curr->next;
tmp = curr;
curr = curr->next;
OPENSSL_free(tmp);
}
#ifndef FIPS_MODULE
CRYPTO_THREAD_unlock(gtr->lock);
#endif
}
int ossl_init_thread_start(const void *index, void *arg,
OSSL_thread_stop_handler_fn handfn)
{
THREAD_EVENT_HANDLER **hands;
THREAD_EVENT_HANDLER *hand;
OSSL_LIB_CTX *ctx = NULL;
#ifdef FIPS_MODULE
ctx = arg;
#endif
hands = alloc_thread_local(ctx);
if (hands == NULL)
return 0;
#ifdef FIPS_MODULE
if (*hands == NULL) {
if (!ossl_thread_register_fips(ctx))
return 0;
}
#endif
hand = OPENSSL_malloc(sizeof(*hand));
if (hand == NULL)
return 0;
hand->handfn = handfn;
hand->arg = arg;
#ifndef FIPS_MODULE
hand->index = index;
#endif
hand->next = *hands;
*hands = hand;
return 1;
}
#ifndef FIPS_MODULE
static int init_thread_deregister(void *index, int all)
{
GLOBAL_TEVENT_REGISTER *gtr;
int i;
gtr = get_global_tevent_register();
if (gtr == NULL)
return 0;
if (!all) {
if (!CRYPTO_THREAD_write_lock(gtr->lock))
return 0;
} else {
glob_tevent_reg = NULL;
}
for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
THREAD_EVENT_HANDLER **hands
= sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp;
if (hands == NULL) {
if (!all)
CRYPTO_THREAD_unlock(gtr->lock);
return 0;
}
curr = *hands;
while (curr != NULL) {
if (all || curr->index == index) {
if (prev != NULL)
prev->next = curr->next;
else
*hands = curr->next;
tmp = curr;
curr = curr->next;
OPENSSL_free(tmp);
continue;
}
prev = curr;
curr = curr->next;
}
if (all)
OPENSSL_free(hands);
}
if (all) {
CRYPTO_THREAD_lock_free(gtr->lock);
sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands);
OPENSSL_free(gtr);
} else {
CRYPTO_THREAD_unlock(gtr->lock);
}
return 1;
}
int ossl_init_thread_deregister(void *index)
{
return init_thread_deregister(index, 0);
}
#endif