summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Landau <hlandau@openssl.org>2022-03-14 08:13:12 +0000
committerTomas Mraz <tomas@openssl.org>2022-11-21 10:49:51 +0100
commit8436ef8bdb96c0a977a15ec707d28404d97c3a6c (patch)
tree4af422951654f59fbd8e100892ecb21f3475c941
parentee246234bf591cd2a9779a4ad3a2ee3c53848213 (diff)
Refactor OSSL_LIB_CTX to avoid using CRYPTO_EX_DATA
This refactors OSSL_LIB_CTX to avoid using CRYPTO_EX_DATA. The assorted objects to be managed by OSSL_LIB_CTX are hardcoded and are initialized eagerly rather than lazily, which avoids the need for locking on access in most cases. Fixes #17116. Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/17881) (cherry picked from commit 927d0566ded0dff9d6c5abc8a40bb84068446b76)
-rw-r--r--crypto/bio/bss_core.c14
-rw-r--r--crypto/context.c441
-rw-r--r--crypto/core_namemap.c14
-rw-r--r--crypto/encode_decode/decoder_meth.c23
-rw-r--r--crypto/encode_decode/encoder_meth.c23
-rw-r--r--crypto/evp/evp_fetch.c21
-rw-r--r--crypto/initthread.c17
-rw-r--r--crypto/property/defn_cache.c17
-rw-r--r--crypto/property/property.c20
-rw-r--r--crypto/property/property_string.c19
-rw-r--r--crypto/provider_child.c32
-rw-r--r--crypto/provider_conf.c15
-rw-r--r--crypto/provider_core.c17
-rw-r--r--crypto/rand/rand_lib.c14
-rw-r--r--crypto/self_test_core.c14
-rw-r--r--crypto/store/store_meth.c23
-rw-r--r--doc/internal/man3/ossl_lib_ctx_get_data.pod81
-rw-r--r--include/crypto/context.h40
-rw-r--r--include/internal/cryptlib.h14
-rw-r--r--providers/fips/fipsprov.c27
-rw-r--r--providers/implementations/rands/crngt.c14
-rw-r--r--providers/implementations/rands/drbg.c14
-rw-r--r--test/context_internal_test.c92
23 files changed, 445 insertions, 561 deletions
diff --git a/crypto/bio/bss_core.c b/crypto/bio/bss_core.c
index 7a84b20460..b9a8eff346 100644
--- a/crypto/bio/bss_core.c
+++ b/crypto/bio/bss_core.c
@@ -10,6 +10,7 @@
#include <openssl/core_dispatch.h>
#include "bio_local.h"
#include "internal/cryptlib.h"
+#include "crypto/context.h"
typedef struct {
OSSL_FUNC_BIO_read_ex_fn *c_bio_read_ex;
@@ -21,26 +22,19 @@ typedef struct {
OSSL_FUNC_BIO_free_fn *c_bio_free;
} BIO_CORE_GLOBALS;
-static void bio_core_globals_free(void *vbcg)
+void ossl_bio_core_globals_free(void *vbcg)
{
OPENSSL_free(vbcg);
}
-static void *bio_core_globals_new(OSSL_LIB_CTX *ctx)
+void *ossl_bio_core_globals_new(OSSL_LIB_CTX *ctx)
{
return OPENSSL_zalloc(sizeof(BIO_CORE_GLOBALS));
}
-static const OSSL_LIB_CTX_METHOD bio_core_globals_method = {
- OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
- bio_core_globals_new,
- bio_core_globals_free,
-};
-
static ossl_inline BIO_CORE_GLOBALS *get_globals(OSSL_LIB_CTX *libctx)
{
- return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_BIO_CORE_INDEX,
- &bio_core_globals_method);
+ return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_BIO_CORE_INDEX);
}
static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
diff --git a/crypto/context.c b/crypto/context.c
index 1647371bb7..c9f976c68c 100644
--- a/crypto/context.c
+++ b/crypto/context.c
@@ -14,7 +14,7 @@
#include "internal/core.h"
#include "internal/bio.h"
#include "internal/provider.h"
-#include "crypto/ctype.h"
+#include "crypto/context.h"
struct ossl_lib_ctx_onfree_list_st {
ossl_lib_ctx_onfree_fn *fn;
@@ -22,20 +22,31 @@ struct ossl_lib_ctx_onfree_list_st {
};
struct ossl_lib_ctx_st {
- CRYPTO_RWLOCK *lock;
- CRYPTO_EX_DATA data;
-
- /*
- * For most data in the OSSL_LIB_CTX we just use ex_data to store it. But
- * that doesn't work for ex_data itself - so we store that directly.
- */
+ CRYPTO_RWLOCK *lock, *rand_crngt_lock;
OSSL_EX_DATA_GLOBAL global;
- /* Map internal static indexes to dynamically created indexes */
- int dyn_indexes[OSSL_LIB_CTX_MAX_INDEXES];
-
- /* Keep a separate lock for each index */
- CRYPTO_RWLOCK *index_locks[OSSL_LIB_CTX_MAX_INDEXES];
+ void *property_string_data;
+ void *evp_method_store;
+ void *provider_store;
+ void *namemap;
+ void *property_defns;
+ void *global_properties;
+ void *drbg;
+ void *drbg_nonce;
+#ifndef FIPS_MODULE
+ void *provider_conf;
+ void *bio_core;
+ void *child_provider;
+ OSSL_METHOD_STORE *decoder_store;
+ OSSL_METHOD_STORE *encoder_store;
+ OSSL_METHOD_STORE *store_loader_store;
+ void *self_test_cb;
+#endif
+ void *rand_crngt;
+#ifdef FIPS_MODULE
+ void *thread_event_handler;
+ void *fips_prov;
+#endif
CRYPTO_RWLOCK *oncelock;
int run_once_done[OSSL_LIB_CTX_MAX_RUN_ONCE];
@@ -68,9 +79,10 @@ int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx)
return ctx->ischild;
}
+static void context_deinit_objs(OSSL_LIB_CTX *ctx);
+
static int context_init(OSSL_LIB_CTX *ctx)
{
- size_t i;
int exdata_done = 0;
ctx->lock = CRYPTO_THREAD_lock_new();
@@ -81,48 +93,246 @@ static int context_init(OSSL_LIB_CTX *ctx)
if (ctx->oncelock == NULL)
goto err;
- for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++) {
- ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
- ctx->dyn_indexes[i] = -1;
- if (ctx->index_locks[i] == NULL)
- goto err;
- }
+ ctx->rand_crngt_lock = CRYPTO_THREAD_lock_new();
+ if (ctx->rand_crngt_lock == NULL)
+ goto err;
- /* OSSL_LIB_CTX is built on top of ex_data so we initialise that directly */
+ /* Initialize ex_data. */
if (!ossl_do_ex_data_init(ctx))
goto err;
exdata_done = 1;
- if (!ossl_crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
- &ctx->data))
+ /* P2. We want evp_method_store to be cleaned up before the provider store */
+ ctx->evp_method_store = ossl_method_store_new(ctx);
+ if (ctx->evp_method_store == NULL)
+ goto err;
+
+#ifndef FIPS_MODULE
+ /* P2. Must be freed before the provider store is freed */
+ ctx->provider_conf = ossl_prov_conf_ctx_new(ctx);
+ if (ctx->provider_conf == NULL)
+ goto err;
+#endif
+
+ /* P2. */
+ ctx->drbg = ossl_rand_ctx_new(ctx);
+ if (ctx->drbg == NULL)
+ goto err;
+
+#ifndef FIPS_MODULE
+ /* P2. We want decoder_store to be cleaned up before the provider store */
+ ctx->decoder_store = ossl_method_store_new(ctx);
+ if (ctx->decoder_store == NULL)
+ goto err;
+
+ /* P2. We want encoder_store to be cleaned up before the provider store */
+ ctx->encoder_store = ossl_method_store_new(ctx);
+ if (ctx->encoder_store == NULL)
+ goto err;
+
+ /* P2. We want loader_store to be cleaned up before the provider store */
+ ctx->store_loader_store = ossl_method_store_new(ctx);
+ if (ctx->store_loader_store == NULL)
+ goto err;
+#endif
+
+ /* P1. Needs to be freed before the child provider data is freed */
+ ctx->provider_store = ossl_provider_store_new(ctx);
+ if (ctx->provider_store == NULL)
+ goto err;
+
+ /* Default priority. */
+ ctx->property_string_data = ossl_property_string_data_new(ctx);
+ if (ctx->property_string_data == NULL)
goto err;
+ ctx->namemap = ossl_stored_namemap_new(ctx);
+ if (ctx->namemap == NULL)
+ goto err;
+
+ ctx->property_defns = ossl_property_defns_new(ctx);
+ if (ctx->property_defns == NULL)
+ goto err;
+
+ ctx->global_properties = ossl_ctx_global_properties_new(ctx);
+ if (ctx->global_properties == NULL)
+ goto err;
+
+#ifndef FIPS_MODULE
+ ctx->bio_core = ossl_bio_core_globals_new(ctx);
+ if (ctx->bio_core == NULL)
+ goto err;
+#endif
+
+ ctx->drbg_nonce = ossl_prov_drbg_nonce_ctx_new(ctx);
+ if (ctx->drbg_nonce == NULL)
+ goto err;
+
+#ifndef FIPS_MODULE
+ ctx->self_test_cb = ossl_self_test_set_callback_new(ctx);
+ if (ctx->self_test_cb == NULL)
+ goto err;
+#endif
+
+#ifdef FIPS_MODULE
+ ctx->thread_event_handler = ossl_thread_event_ctx_new(ctx);
+ if (ctx->thread_event_handler == NULL)
+ goto err;
+
+ ctx->fips_prov = ossl_fips_prov_ossl_ctx_new(ctx);
+ if (ctx->fips_prov == NULL)
+ goto err;
+#endif
+
+ /* Low priority. */
+#ifndef FIPS_MODULE
+ ctx->child_provider = ossl_child_prov_ctx_new(ctx);
+ if (ctx->child_provider == NULL)
+ goto err;
+#endif
+
/* Everything depends on properties, so we also pre-initialise that */
if (!ossl_property_parse_init(ctx))
goto err;
return 1;
+
err:
+ context_deinit_objs(ctx);
+
if (exdata_done)
ossl_crypto_cleanup_all_ex_data_int(ctx);
- for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
- CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
+
+ CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
CRYPTO_THREAD_lock_free(ctx->oncelock);
CRYPTO_THREAD_lock_free(ctx->lock);
memset(ctx, '\0', sizeof(*ctx));
return 0;
}
+static void context_deinit_objs(OSSL_LIB_CTX *ctx)
+{
+ /* P2. We want evp_method_store to be cleaned up before the provider store */
+ if (ctx->evp_method_store != NULL) {
+ ossl_method_store_free(ctx->evp_method_store);
+ ctx->evp_method_store = NULL;
+ }
+
+ /* P2. */
+ if (ctx->drbg != NULL) {
+ ossl_rand_ctx_free(ctx->drbg);
+ ctx->drbg = NULL;
+ }
+
+#ifndef FIPS_MODULE
+ /* P2. */
+ if (ctx->provider_conf != NULL) {
+ ossl_prov_conf_ctx_free(ctx->provider_conf);
+ ctx->provider_conf = NULL;
+ }
+
+ /* P2. We want decoder_store to be cleaned up before the provider store */
+ if (ctx->decoder_store != NULL) {
+ ossl_method_store_free(ctx->decoder_store);
+ ctx->decoder_store = NULL;
+ }
+
+ /* P2. We want encoder_store to be cleaned up before the provider store */
+ if (ctx->encoder_store != NULL) {
+ ossl_method_store_free(ctx->encoder_store);
+ ctx->encoder_store = NULL;
+ }
+
+ /* P2. We want loader_store to be cleaned up before the provider store */
+ if (ctx->store_loader_store != NULL) {
+ ossl_method_store_free(ctx->store_loader_store);
+ ctx->store_loader_store = NULL;
+ }
+#endif
+
+ /* P1. Needs to be freed before the child provider data is freed */
+ if (ctx->provider_store != NULL) {
+ ossl_provider_store_free(ctx->provider_store);
+ ctx->provider_store = NULL;
+ }
+
+ /* Default priority. */
+ if (ctx->property_string_data != NULL) {
+ ossl_property_string_data_free(ctx->property_string_data);
+ ctx->property_string_data = NULL;
+ }
+
+ if (ctx->namemap != NULL) {
+ ossl_stored_namemap_free(ctx->namemap);
+ ctx->namemap = NULL;
+ }
+
+ if (ctx->property_defns != NULL) {
+ ossl_property_defns_free(ctx->property_defns);
+ ctx->property_defns = NULL;
+ }
+
+ if (ctx->global_properties != NULL) {
+ ossl_ctx_global_properties_free(ctx->global_properties);
+ ctx->global_properties = NULL;
+ }
+
+#ifndef FIPS_MODULE
+ if (ctx->bio_core != NULL) {
+ ossl_bio_core_globals_free(ctx->bio_core);
+ ctx->bio_core = NULL;
+ }
+#endif
+
+ if (ctx->drbg_nonce != NULL) {
+ ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce);
+ ctx->drbg_nonce = NULL;
+ }
+
+#ifndef FIPS_MODULE
+ if (ctx->self_test_cb != NULL) {
+ ossl_self_test_set_callback_free(ctx->self_test_cb);
+ ctx->self_test_cb = NULL;
+ }
+#endif
+
+ if (ctx->rand_crngt != NULL) {
+ ossl_rand_crng_ctx_free(ctx->rand_crngt);
+ ctx->rand_crngt = NULL;
+ }
+
+#ifdef FIPS_MODULE
+ if (ctx->thread_event_handler != NULL) {
+ ossl_thread_event_ctx_free(ctx->thread_event_handler);
+ ctx->thread_event_handler = NULL;
+ }
+
+ if (ctx->fips_prov != NULL) {
+ ossl_fips_prov_ossl_ctx_free(ctx->fips_prov);
+ ctx->fips_prov = NULL;
+ }
+#endif
+
+ /* Low priority. */
+#ifndef FIPS_MODULE
+ if (ctx->child_provider != NULL) {
+ ossl_child_prov_ctx_free(ctx->child_provider);
+ ctx->child_provider = NULL;
+ }
+#endif
+}
+
static int context_deinit(OSSL_LIB_CTX *ctx)
{
struct ossl_lib_ctx_onfree_list_st *tmp, *onfree;
- int i;
if (ctx == NULL)
return 1;
ossl_ctx_thread_stop(ctx);
+ context_deinit_objs(ctx);
+
onfree = ctx->onfreelist;
while (onfree != NULL) {
onfree->fn(ctx);
@@ -130,13 +340,14 @@ static int context_deinit(OSSL_LIB_CTX *ctx)
onfree = onfree->next;
OPENSSL_free(tmp);
}
- CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL, &ctx->data);
+
ossl_crypto_cleanup_all_ex_data_int(ctx);
- for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
- CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
+ CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
CRYPTO_THREAD_lock_free(ctx->oncelock);
CRYPTO_THREAD_lock_free(ctx->lock);
+ ctx->rand_crngt_lock = NULL;
+ ctx->oncelock = NULL;
ctx->lock = NULL;
return 1;
}
@@ -300,127 +511,89 @@ int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
return 0;
}
-static void ossl_lib_ctx_generic_new(void *parent_ign, void *ptr_ign,
- CRYPTO_EX_DATA *ad, int index,
- long argl_ign, void *argp)
+void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index)
{
- const OSSL_LIB_CTX_METHOD *meth = argp;
- OSSL_LIB_CTX *ctx = ossl_crypto_ex_data_get_ossl_lib_ctx(ad);
- void *ptr = meth->new_func(ctx);
-
- if (ptr != NULL) {
- if (!CRYPTO_THREAD_write_lock(ctx->lock))
- /*
- * Can't return something, so best to hope that something will
- * fail later. :(
- */
- return;
- CRYPTO_set_ex_data(ad, index, ptr);
- CRYPTO_THREAD_unlock(ctx->lock);
- }
-}
-static void ossl_lib_ctx_generic_free(void *parent_ign, void *ptr,
- CRYPTO_EX_DATA *ad, int index,
- long argl_ign, void *argp)
-{
- const OSSL_LIB_CTX_METHOD *meth = argp;
-
- meth->free_func(ptr);
-}
-
-static int ossl_lib_ctx_init_index(OSSL_LIB_CTX *ctx, int static_index,
- const OSSL_LIB_CTX_METHOD *meth)
-{
- int idx;
+ void *p;
ctx = ossl_lib_ctx_get_concrete(ctx);
if (ctx == NULL)
- return 0;
+ return NULL;
- idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
- (void *)meth,
- ossl_lib_ctx_generic_new,
- NULL, ossl_lib_ctx_generic_free,
- meth->priority);
- if (idx < 0)
- return 0;
+ switch (index) {
+ case OSSL_LIB_CTX_PROPERTY_STRING_INDEX:
+ return ctx->property_string_data;
+ case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX:
+ return ctx->evp_method_store;
+ case OSSL_LIB_CTX_PROVIDER_STORE_INDEX:
+ return ctx->provider_store;
+ case OSSL_LIB_CTX_NAMEMAP_INDEX:
+ return ctx->namemap;
+ case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX:
+ return ctx->property_defns;
+ case OSSL_LIB_CTX_GLOBAL_PROPERTIES:
+ return ctx->global_properties;
+ case OSSL_LIB_CTX_DRBG_INDEX:
+ return ctx->drbg;
+ case OSSL_LIB_CTX_DRBG_NONCE_INDEX:
+ return ctx->drbg_nonce;
+#ifndef FIPS_MODULE
+ case OSSL_LIB_CTX_PROVIDER_CONF_INDEX:
+ return ctx->provider_conf;
+ case OSSL_LIB_CTX_BIO_CORE_INDEX:
+ return ctx->bio_core;
+ case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX:
+ return ctx->child_provider;
+ case OSSL_LIB_CTX_DECODER_STORE_INDEX:
+ return ctx->decoder_store;
+ case OSSL_LIB_CTX_ENCODER_STORE_INDEX:
+ return ctx->encoder_store;
+ case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX:
+ return ctx->store_loader_store;
+ case OSSL_LIB_CTX_SELF_TEST_CB_INDEX:
+ return ctx->self_test_cb;
+#endif
- ctx->dyn_indexes[static_index] = idx;
- return 1;
-}
+ case OSSL_LIB_CTX_RAND_CRNGT_INDEX: {
+
+ /*
+ * rand_crngt must be lazily initialized because it calls into
+ * libctx, so must not be called from context_init, else a deadlock
+ * will occur.
+ *
+ * We use a separate lock because code called by the instantiation
+ * of rand_crngt is liable to try and take the libctx lock.
+ */
+ if (CRYPTO_THREAD_read_lock(ctx->rand_crngt_lock) != 1)
+ return NULL;
-void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
- const OSSL_LIB_CTX_METHOD *meth)
-{
- void *data = NULL;
- int dynidx;
+ if (ctx->rand_crngt == NULL) {
+ CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
- ctx = ossl_lib_ctx_get_concrete(ctx);
- if (ctx == NULL)
- return NULL;
-
- if (!CRYPTO_THREAD_read_lock(ctx->lock))
- return NULL;
- dynidx = ctx->dyn_indexes[index];
- CRYPTO_THREAD_unlock(ctx->lock);
+ if (CRYPTO_THREAD_write_lock(ctx->rand_crngt_lock) != 1)
+ return NULL;
- if (dynidx != -1) {
- if (!CRYPTO_THREAD_read_lock(ctx->index_locks[index]))
- return NULL;
- if (!CRYPTO_THREAD_read_lock(ctx->lock)) {
- CRYPTO_THREAD_unlock(ctx->index_locks[index]);
- return NULL;
+ if (ctx->rand_crngt == NULL)
+ ctx->rand_crngt = ossl_rand_crng_ctx_new(ctx);
}
- data = CRYPTO_get_ex_data(&ctx->data, dynidx);
- CRYPTO_THREAD_unlock(ctx->lock);
- CRYPTO_THREAD_unlock(ctx->index_locks[index]);
- return data;
- }
- if (!CRYPTO_THREAD_write_lock(ctx->index_locks[index]))
- return NULL;
- if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
- CRYPTO_THREAD_unlock(ctx->index_locks[index]);
- return NULL;
- }
+ p = ctx->rand_crngt;
- dynidx = ctx->dyn_indexes[index];
- if (dynidx != -1) {
- data = CRYPTO_get_ex_data(&ctx->data, dynidx);
- CRYPTO_THREAD_unlock(ctx->lock);
- CRYPTO_THREAD_unlock(ctx->index_locks[index]);
- return data;
- }
+ CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
- if (!ossl_lib_ctx_init_index(ctx, index, meth)) {
- CRYPTO_THREAD_unlock(ctx->lock);
- CRYPTO_THREAD_unlock(ctx->index_locks[index]);
- return NULL;
+ return p;
}
- CRYPTO_THREAD_unlock(ctx->lock);
-
- /*
- * The alloc call ensures there's a value there. We release the ctx->lock
- * for this, because the allocation itself may recursively call
- * ossl_lib_ctx_get_data for other indexes (never this one). The allocation
- * will itself acquire the ctx->lock when it actually comes to store the
- * allocated data (see ossl_lib_ctx_generic_new() above). We call
- * ossl_crypto_alloc_ex_data_intern() here instead of CRYPTO_alloc_ex_data().
- * They do the same thing except that the latter calls CRYPTO_get_ex_data()
- * as well - which we must not do without holding the ctx->lock.
- */
- if (ossl_crypto_alloc_ex_data_intern(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
- &ctx->data, ctx->dyn_indexes[index])) {
- if (!CRYPTO_THREAD_read_lock(ctx->lock))
- goto end;
- data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
- CRYPTO_THREAD_unlock(ctx->lock);
- }
+#ifdef FIPS_MODULE
+ case OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX:
+ return ctx->thread_event_handler;
-end:
- CRYPTO_THREAD_unlock(ctx->index_locks[index]);
- return data;
+ case OSSL_LIB_CTX_FIPS_PROV_INDEX:
+ return ctx->fips_prov;
+#endif
+
+ default:
+ return NULL;
+ }
}
OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
diff --git a/crypto/core_namemap.c b/crypto/core_namemap.c
index 7e11ab1c88..554524a5c4 100644
--- a/crypto/core_namemap.c
+++ b/crypto/core_namemap.c
@@ -12,6 +12,7 @@
#include "crypto/lhash.h" /* ossl_lh_strcasehash */
#include "internal/tsan_assist.h"
#include "internal/sizes.h"
+#include "crypto/context.h"
/*-
* The namenum entry
@@ -60,7 +61,7 @@ static void namenum_free(NAMENUM_ENTRY *n)
/* OSSL_LIB_CTX_METHOD functions for a namemap stored in a library context */
-static void *stored_namemap_new(OSSL_LIB_CTX *libctx)
+void *ossl_stored_namemap_new(OSSL_LIB_CTX *libctx)
{
OSSL_NAMEMAP *namemap = ossl_namemap_new();
@@ -70,7 +71,7 @@ static void *stored_namemap_new(OSSL_LIB_CTX *libctx)
return namemap;
}
-static void stored_namemap_free(void *vnamemap)
+void ossl_stored_namemap_free(void *vnamemap)
{
OSSL_NAMEMAP *namemap = vnamemap;
@@ -81,12 +82,6 @@ static void stored_namemap_free(void *vnamemap)
}
}
-static const OSSL_LIB_CTX_METHOD stored_namemap_method = {
- OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
- stored_namemap_new,
- stored_namemap_free,
-};
-
/*-
* API functions
* =============
@@ -468,8 +463,7 @@ OSSL_NAMEMAP *ossl_namemap_stored(OSSL_LIB_CTX *libctx)
int nms;
#endif
OSSL_NAMEMAP *namemap =
- ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_NAMEMAP_INDEX,
- &stored_namemap_method);
+ ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_NAMEMAP_INDEX);
if (namemap == NULL)
return NULL;
diff --git a/crypto/encode_decode/decoder_meth.c b/crypto/encode_decode/decoder_meth.c
index 496fbe3320..62e30ccb1a 100644
--- a/crypto/encode_decode/decoder_meth.c
+++ b/crypto/encode_decode/decoder_meth.c
@@ -17,6 +17,7 @@
#include "internal/provider.h"
#include "crypto/decoder.h"
#include "encoder_local.h"
+#include "crypto/context.h"
/*
* Decoder can have multiple names, separated with colons in a name string
@@ -65,25 +66,6 @@ void OSSL_DECODER_free(OSSL_DECODER *decoder)
OPENSSL_free(decoder);
}
-/* Permanent decoder method store, constructor and destructor */
-static void decoder_store_free(void *vstore)
-{
- ossl_method_store_free(vstore);
-}
-
-static void *decoder_store_new(OSSL_LIB_CTX *ctx)
-{
- return ossl_method_store_new(ctx);
-}
-
-
-static const OSSL_LIB_CTX_METHOD decoder_store_method = {
- /* We want decoder_store to be cleaned up before the provider store */
- OSSL_LIB_CTX_METHOD_PRIORITY_2,
- decoder_store_new,
- decoder_store_free,
-};
-
/* Data to be passed through ossl_method_construct() */
struct decoder_data_st {
OSSL_LIB_CTX *libctx;
@@ -120,8 +102,7 @@ static void dealloc_tmp_decoder_store(void *store)
/* Get the permanent decoder store */
static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
{
- return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
- &decoder_store_method);
+ return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX);
}
static int reserve_decoder_store(void *store, void *data)
diff --git a/crypto/encode_decode/encoder_meth.c b/crypto/encode_decode/encoder_meth.c
index 89e7b6abf8..f91d349587 100644
--- a/crypto/encode_decode/encoder_meth.c
+++ b/crypto/encode_decode/encoder_meth.c
@@ -17,6 +17,7 @@
#include "internal/provider.h"
#include "crypto/encoder.h"
#include "encoder_local.h"
+#include "crypto/context.h"
/*
* Encoder can have multiple names, separated with colons in a name string
@@ -65,25 +66,6 @@ void OSSL_ENCODER_free(OSSL_ENCODER *encoder)
OPENSSL_free(encoder);
}
-/* Permanent encoder method store, constructor and destructor */
-static void encoder_store_free(void *vstore)
-{
- ossl_method_store_free(vstore);
-}
-
-static void *encoder_store_new(OSSL_LIB_CTX *ctx)
-{
- return ossl_method_store_new(ctx);
-}
-
-
-static const OSSL_LIB_CTX_METHOD encoder_store_method = {
- /* We want encoder_store to be cleaned up before the provider store */
- OSSL_LIB_CTX_METHOD_PRIORITY_2,
- encoder_store_new,
- encoder_store_free,
-};
-
/* Data to be passed through ossl_method_construct() */
struct encoder_data_st {
OSSL_LIB_CTX *libctx;
@@ -120,8 +102,7 @@ static void dealloc_tmp_encoder_store(void *store)
/* Get the permanent encoder store */
static OSSL_METHOD_STORE *get_encoder_store(OSSL_LIB_CTX *libctx)
{
- return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX,
- &encoder_store_method);
+ return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX);
}
static int reserve_encoder_store(void *store, void *data)
diff --git a/crypto/evp/evp_fetch.c b/crypto/evp/evp_fetch.c
index aafd927e63..b9ef0200bb 100644
--- a/crypto/evp/evp_fetch.c
+++ b/crypto/evp/evp_fetch.c
@@ -23,24 +23,6 @@
#define NAME_SEPARATOR ':'
-static void evp_method_store_free(void *vstore)
-{
- ossl_method_store_free(vstore);
-}
-
-static void *evp_method_store_new(OSSL_LIB_CTX *ctx)
-{
- return ossl_method_store_new(ctx);
-}
-
-
-static const OSSL_LIB_CTX_METHOD evp_method_store_method = {
- /* We want evp_method_store to be cleaned up before the provider store */
- OSSL_LIB_CTX_METHOD_PRIORITY_2,
- evp_method_store_new,
- evp_method_store_free,
-};
-
/* Data to be passed through ossl_method_construct() */
struct evp_method_data_st {
OSSL_LIB_CTX *libctx;
@@ -79,8 +61,7 @@ static void *get_tmp_evp_method_store(void *data)
static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
{
- return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX,
- &evp_method_store_method);
+ return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX);
}
static int reserve_evp_method_store(void *store, void *data)
diff --git a/crypto/initthread.c b/crypto/initthread.c
index 1bdaeda9fc..ee57d14466 100644
--- a/crypto/initthread.c
+++ b/crypto/initthread.c
@@ -12,6 +12,7 @@
#include "crypto/cryptlib.h"
#include "prov/providercommon.h"
#include "internal/thread_once.h"
+#include "crypto/context.h"
#ifdef FIPS_MODULE
#include "prov/provider_ctx.h"
@@ -248,7 +249,7 @@ void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
#else
-static void *thread_event_ossl_ctx_new(OSSL_LIB_CTX *libctx)
+void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *libctx)
{
THREAD_EVENT_HANDLER **hands = NULL;
CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal));
@@ -274,17 +275,11 @@ static void *thread_event_ossl_ctx_new(OSSL_LIB_CTX *libctx)
return NULL;
}
-static void thread_event_ossl_ctx_free(void *tlocal)
+void ossl_thread_event_ctx_free(void *tlocal)
{
OPENSSL_free(tlocal);
}
-static const OSSL_LIB_CTX_METHOD thread_event_ossl_ctx_method = {
- OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
- thread_event_ossl_ctx_new,
- thread_event_ossl_ctx_free,
-};
-
static void ossl_arg_thread_stop(void *arg)
{
ossl_ctx_thread_stop((OSSL_LIB_CTX *)arg);
@@ -294,8 +289,7 @@ void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
{
THREAD_EVENT_HANDLER **hands;
CRYPTO_THREAD_LOCAL *local
- = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX,
- &thread_event_ossl_ctx_method);
+ = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
if (local == NULL)
return;
@@ -363,8 +357,7 @@ int ossl_init_thread_start(const void *index, void *arg,
* OSSL_LIB_CTX gets informed about thread stop events individually.
*/
CRYPTO_THREAD_LOCAL *local
- = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX,
- &thread_event_ossl_ctx_method);
+ = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
#else
/*
* Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per
diff --git a/crypto/property/defn_cache.c b/crypto/property/defn_cache.c
index b4cd67c990..c697e6f474 100644
--- a/crypto/property/defn_cache.c
+++ b/crypto/property/defn_cache.c
@@ -15,6 +15,7 @@
#include "internal/property.h"
#include "internal/core.h"
#include "property_local.h"
+#include "crypto/context.h"
/*
* Implement a property definition cache.
@@ -47,7 +48,7 @@ static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
OPENSSL_free(elem);
}
-static void property_defns_free(void *vproperty_defns)
+void ossl_property_defns_free(void *vproperty_defns)
{
LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
@@ -58,24 +59,17 @@ static void property_defns_free(void *vproperty_defns)
}
}
-static void *property_defns_new(OSSL_LIB_CTX *ctx) {
+void *ossl_property_defns_new(OSSL_LIB_CTX *ctx) {
return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
}
-static const OSSL_LIB_CTX_METHOD property_defns_method = {
- OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
- property_defns_new,
- property_defns_free,
-};
-
OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop)
{
PROPERTY_DEFN_ELEM elem, *r;
LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
property_defns = ossl_lib_ctx_get_data(ctx,
-