Refactor OSSL_LIB_CTX to avoid using CRYPTO_EX_DATA
authorHugo Landau <hlandau@openssl.org>
Mon, 14 Mar 2022 08:13:12 +0000 (08:13 +0000)
committerPauli <pauli@openssl.org>
Thu, 31 Mar 2022 23:49:19 +0000 (10:49 +1100)
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)

23 files changed:
crypto/bio/bss_core.c
crypto/context.c
crypto/core_namemap.c
crypto/encode_decode/decoder_meth.c
crypto/encode_decode/encoder_meth.c
crypto/evp/evp_fetch.c
crypto/initthread.c
crypto/property/defn_cache.c
crypto/property/property.c
crypto/property/property_string.c
crypto/provider_child.c
crypto/provider_conf.c
crypto/provider_core.c
crypto/rand/rand_lib.c
crypto/self_test_core.c
crypto/store/store_meth.c
doc/internal/man3/ossl_lib_ctx_get_data.pod
include/crypto/context.h [new file with mode: 0644]
include/internal/cryptlib.h
providers/fips/fipsprov.c
providers/implementations/rands/crngt.c
providers/implementations/rands/drbg.c
test/context_internal_test.c

index b78b1bedaabc8415268480134a144f2fe0e4bfb4..66b56647f6b2230a33e63b2dcd6937d34fa0fd09 100644 (file)
@@ -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,
index e84eeea59652b9c75d5cd46e5141afe1fc3bc157..4a79e2eec6f938944940f3f31631f0a3e2deebe3 100644 (file)
@@ -14,6 +14,7 @@
 #include "internal/core.h"
 #include "internal/bio.h"
 #include "internal/provider.h"
+#include "crypto/context.h"
 
 struct ossl_lib_ctx_onfree_list_st {
     ossl_lib_ctx_onfree_fn *fn;
@@ -21,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];
@@ -67,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();
@@ -80,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);
@@ -129,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;
 }
@@ -299,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)
index f058e629f3b21f286809e3abbb0cf618f58e4810..d8de0af3ffbf78ea82f8c80bd3d1ab086ccb77f0 100644 (file)
@@ -13,6 +13,7 @@
 #include "crypto/lhash.h"      /* ossl_lh_strcasehash */
 #include "internal/tsan_assist.h"
 #include "internal/sizes.h"
+#include "crypto/context.h"
 
 /*-
  * The namenum entry
@@ -61,7 +62,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();
 
@@ -71,7 +72,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;
 
@@ -82,12 +83,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
  * =============
@@ -469,8 +464,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;
index c469f84558322cccdf5ecac862d043356b9e75e8..2a8b044f78ab72321819739060093b968d432d17 100644 (file)
@@ -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);
 }
 
 /* Get decoder methods from a store, or put one in */
index 57b2f998e237de24b61fec24c73187920cacc3c6..67bfcc23fe938908c17415aaafffd526769b2923 100644 (file)
@@ -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);
 }
 
 /* Get encoder methods from a store, or put one in */
index 3066e0b0d4a8af54261ed4eb9261c8a97330d0d6..06839777f04ce21505b5c62e40604ad634d22471 100644 (file)
 
 #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);
 }
 
 /*
index 1bdaeda9fc8ed60d0773421eceb70faa97a9f07b..ee57d144662889559a5ddc7127e0d200b515d1a6 100644 (file)
@@ -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
index 8007599526c61e45063577ed8680e362970f00ca..ed2a675455fd4acee82104d33ba39f4f39a05277 100644 (file)
@@ -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,
-                                           OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
-                                           &property_defns_method);
+                                           OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
     if (property_defns == NULL || !ossl_lib_ctx_read_lock(ctx))
         return NULL;
 
@@ -94,8 +88,7 @@ int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
     int res = 1;
 
     property_defns = ossl_lib_ctx_get_data(ctx,
-                                           OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
-                                           &property_defns_method);
+                                           OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
     if (property_defns == NULL)
         return 0;
 
index 2967b80a0918531aefc400c17a30f86586bb813f..728b78be50cb2159d166d971c1dfb148e9107201 100644 (file)
@@ -22,6 +22,7 @@
 #include "crypto/lhash.h"
 #include "crypto/sparse_array.h"
 #include "property_local.h"
+#include "crypto/context.h"
 
 /*
  * The number of elements in the query cache before we initiate a flush.
@@ -85,7 +86,7 @@ typedef struct ossl_global_properties_st {
 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
 
 /* Global properties are stored per library context */
-static void ossl_ctx_global_properties_free(void *vglobp)
+void ossl_ctx_global_properties_free(void *vglobp)
 {
     OSSL_GLOBAL_PROPERTIES *globp = vglobp;
 
@@ -95,17 +96,11 @@ static void ossl_ctx_global_properties_free(void *vglobp)
     }
 }
 
-static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
+void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
 {
     return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
 }
 
-static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = {
-    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-    ossl_ctx_global_properties_new,
-    ossl_ctx_global_properties_free,
-};
-
 OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
                                                 int loadconfig)
 {
@@ -115,8 +110,7 @@ OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
     if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
         return NULL;
 #endif
-    globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
-                                  &ossl_ctx_global_properties_method);
+    globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
 
     return globp != NULL ? &globp->list : NULL;
 }
@@ -125,8 +119,7 @@ OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
 int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
 {
     OSSL_GLOBAL_PROPERTIES *globp
-        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
-                                &ossl_ctx_global_properties_method);
+        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
 
     return globp != NULL && globp->no_mirrored ? 1 : 0;
 }
@@ -134,8 +127,7 @@ int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
 {
     OSSL_GLOBAL_PROPERTIES *globp
-        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
-                                &ossl_ctx_global_properties_method);
+        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
 
     if (globp != NULL)
         globp->no_mirrored = 1;
index 9191453d5a96b8246f77050a5a6d709e1d04387d..eb48052508584486f5764a1a55875716a9097f08 100644 (file)
@@ -13,6 +13,7 @@
 #include <openssl/lhash.h>
 #include "crypto/lhash.h"
 #include "property_local.h"
+#include "crypto/context.h"
 
 /*
  * Property strings are a consolidation of all strings seen by the property
@@ -72,7 +73,7 @@ static void property_table_free(PROP_TABLE **pt)
     }
 }
 
-static void property_string_data_free(void *vpropdata)
+void ossl_property_string_data_free(void *vpropdata)
 {
     PROPERTY_STRING_DATA *propdata = vpropdata;
 
@@ -92,7 +93,7 @@ static void property_string_data_free(void *vpropdata)
     OPENSSL_free(propdata);
 }
 
-static void *property_string_data_new(OSSL_LIB_CTX *ctx) {
+void *ossl_property_string_data_new(OSSL_LIB_CTX *ctx) {
     PROPERTY_STRING_DATA *propdata = OPENSSL_zalloc(sizeof(*propdata));
 
     if (propdata == NULL)
@@ -114,18 +115,12 @@ static void *property_string_data_new(OSSL_LIB_CTX *ctx) {
 #endif
             || propdata->prop_names == NULL
             || propdata->prop_values == NULL) {
-        property_string_data_free(propdata);
+        ossl_property_string_data_free(propdata);
         return NULL;
     }
     return propdata;
 }
 
-static const OSSL_LIB_CTX_METHOD property_string_data_method = {
-    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-    property_string_data_new,
-    property_string_data_free,
-};
-
 static PROPERTY_STRING *new_property_string(const char *s,
                                             OSSL_PROPERTY_IDX *pidx)
 {
@@ -151,8 +146,7 @@ static OSSL_PROPERTY_IDX ossl_property_string(OSSL_LIB_CTX *ctx, int name,
     PROP_TABLE *t;
     OSSL_PROPERTY_IDX *pidx;
     PROPERTY_STRING_DATA *propdata
-        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
-                                &property_string_data_method);
+        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX);
 
     if (propdata == NULL)
         return 0;
@@ -224,8 +218,7 @@ static const char *ossl_property_str(int name, OSSL_LIB_CTX *ctx,
 {
     const char *r;
     PROPERTY_STRING_DATA *propdata
-        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
-                                &property_string_data_method);
+        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX);
 
     if (propdata == NULL)
         return NULL;
index 977ea4db3bf2155f69cb771b827a8df3092d6ad3..5bf3ad3a3da2082021763b382545344b6b384925 100644 (file)
@@ -16,6 +16,7 @@
 #include "internal/provider.h"
 #include "internal/cryptlib.h"
 #include "crypto/evp.h"
+#include "crypto/context.h"
 
 DEFINE_STACK_OF(OSSL_PROVIDER)
 
@@ -33,12 +34,12 @@ struct child_prov_globals {
     OSSL_FUNC_provider_free_fn *c_prov_free;
 };
 
-static void *child_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
+void *ossl_child_prov_ctx_new(OSSL_LIB_CTX *libctx)
 {
     return OPENSSL_zalloc(sizeof(struct child_prov_globals));
 }
 
-static void child_prov_ossl_ctx_free(void *vgbl)
+void ossl_child_prov_ctx_free(void *vgbl)
 {
     struct child_prov_globals *gbl = vgbl;
 
@@ -46,12 +47,6 @@ static void child_prov_ossl_ctx_free(void *vgbl)
     OPENSSL_free(gbl);
 }
 
-static const OSSL_LIB_CTX_METHOD child_prov_ossl_ctx_method = {
-    OSSL_LIB_CTX_METHOD_LOW_PRIORITY,
-    child_prov_ossl_ctx_new,
-    child_prov_ossl_ctx_free,
-};
-
 static OSSL_provider_init_fn ossl_child_provider_init;
 
 static int ossl_child_provider_init(const OSSL_CORE_HANDLE *handle,
@@ -84,8 +79,7 @@ static int ossl_child_provider_init(const OSSL_CORE_HANDLE *handle,
      */
     ctx = (OSSL_LIB_CTX *)c_get_libctx(handle);
 
-    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
-                                &child_prov_ossl_ctx_method);
+    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
     if (gbl == NULL)
         return 0;
 
@@ -103,8 +97,7 @@ static int provider_create_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata)
     OSSL_PROVIDER *cprov;
     int ret = 0;
 
-    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
-                                &child_prov_ossl_ctx_method);
+    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
     if (gbl == NULL)
         return 0;
 
@@ -166,8 +159,7 @@ static int provider_remove_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata)
     const char *provname;
     OSSL_PROVIDER *cprov;
 
-    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
-                                &child_prov_ossl_ctx_method);
+    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
     if (gbl == NULL)
         return 0;
 
@@ -203,8 +195,7 @@ int ossl_provider_init_as_child(OSSL_LIB_CTX *ctx,
     if (ctx == NULL)
         return 0;
 
-    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
-                                &child_prov_ossl_ctx_method);
+    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
     if (gbl == NULL)
         return 0;
 
@@ -271,8 +262,7 @@ int ossl_provider_init_as_child(OSSL_LIB_CTX *ctx,
 void ossl_provider_deinit_child(OSSL_LIB_CTX *ctx)
 {
     struct child_prov_globals *gbl
-        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
-                                &child_prov_ossl_ctx_method);
+        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
     if (gbl == NULL)
         return;
 
@@ -284,8 +274,7 @@ int ossl_provider_up_ref_parent(OSSL_PROVIDER *prov, int activate)
     struct child_prov_globals *gbl;
 
     gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
-                                OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
-                                &child_prov_ossl_ctx_method);
+                                OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
     if (gbl == NULL)
         return 0;
 
@@ -297,8 +286,7 @@ int ossl_provider_free_parent(OSSL_PROVIDER *prov, int deactivate)
     struct child_prov_globals *gbl;
 
     gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
-                                OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
-                                &child_prov_ossl_ctx_method);
+                                OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
     if (gbl == NULL)
         return 0;
 
index 6a62f0df6064be5747955e6d27126d5905c16e2d..e3b576d6c2bbfba31789da23c88a33a0a92d530d 100644 (file)
@@ -16,6 +16,7 @@
 #include "internal/provider.h"
 #include "internal/cryptlib.h"
 #include "provider_local.h"
+#include "crypto/context.h"
 
 DEFINE_STACK_OF(OSSL_PROVIDER)
 
@@ -26,7 +27,7 @@ typedef struct {
     STACK_OF(OSSL_PROVIDER) *activated_providers;
 } PROVIDER_CONF_GLOBAL;
 
-static void *prov_conf_ossl_ctx_new(OSSL_LIB_CTX *libctx)
+void *ossl_prov_conf_ctx_new(OSSL_LIB_CTX *libctx)
 {
     PROVIDER_CONF_GLOBAL *pcgbl = OPENSSL_zalloc(sizeof(*pcgbl));
 
@@ -42,7 +43,7 @@ static void *prov_conf_ossl_ctx_new(OSSL_LIB_CTX *libctx)
     return pcgbl;
 }
 
-static void prov_conf_ossl_ctx_free(void *vpcgbl)
+void ossl_prov_conf_ctx_free(void *vpcgbl)
 {
     PROVIDER_CONF_GLOBAL *pcgbl = vpcgbl;
 
@@ -54,13 +55,6 @@ static void prov_conf_ossl_ctx_free(void *vpcgbl)
     OPENSSL_free(pcgbl);
 }
 
-static const OSSL_LIB_CTX_METHOD provider_conf_ossl_ctx_method = {
-    /* Must be freed before the provider store is freed */
-    OSSL_LIB_CTX_METHOD_PRIORITY_2,
-    prov_conf_ossl_ctx_new,
-    prov_conf_ossl_ctx_free,
-};
-
 static const char *skip_dot(const char *name)
 {
     const char *p = strchr(name, '.');
@@ -141,8 +135,7 @@ static int provider_conf_activate(OSSL_LIB_CTX *libctx, const char *name,
                                   int soft, const CONF *cnf)
 {
     PROVIDER_CONF_GLOBAL *pcgbl
-        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX,
-                                &provider_conf_ossl_ctx_method);
+        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX);
     OSSL_PROVIDER *prov = NULL, *actual = NULL;
     int ok = 0;
 
index e04734c12e3c90ea56404d81f6762672b2fa748a..48fad8242fc8740f75994536558f82d621f741d7 100644 (file)
@@ -24,6 +24,7 @@
 #include "internal/bio.h"
 #include "internal/core.h"
 #include "provider_local.h"
+#include "crypto/context.h"
 #ifndef FIPS_MODULE
 # include <openssl/self_test.h>
 #endif
@@ -277,7 +278,7 @@ void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
     sk_INFOPAIR_pop_free(info->parameters, infopair_free);
 }
 
-static void provider_store_free(void *vstore)
+void ossl_provider_store_free(void *vstore)
 {
     struct provider_store_st *store = vstore;
     size_t i;
@@ -299,7 +300,7 @@ static void provider_store_free(void *vstore)
     OPENSSL_free(store);
 }
 
-static void *provider_store_new(OSSL_LIB_CTX *ctx)
+void *ossl_provider_store_new(OSSL_LIB_CTX *ctx)
 {
     struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
 
@@ -310,7 +311,7 @@ static void *provider_store_new(OSSL_LIB_CTX *ctx)
         || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
 #endif
         || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
-        provider_store_free(store);
+        ossl_provider_store_free(store);
         return NULL;
     }
     store->libctx = ctx;
@@ -319,19 +320,11 @@ static void *provider_store_new(OSSL_LIB_CTX *ctx)
     return store;
 }
 
-static const OSSL_LIB_CTX_METHOD provider_store_method = {
-    /* Needs to be freed before the child provider data is freed */
-    OSSL_LIB_CTX_METHOD_PRIORITY_1,
-    provider_store_new,
-    provider_store_free,
-};
-
 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
 {
     struct provider_store_st *store = NULL;
 
-    store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
-                                  &provider_store_method);
+    store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX);
     if (store == NULL)
         ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
     return store;
index c772bcc79cd7b7a47d5ade54474c11ed2e3f1dc9..79f5ce322b2e65b4e5e763988c36c4edc63a293e 100644 (file)
@@ -18,6 +18,7 @@
 #include "crypto/rand.h"
 #include "crypto/cryptlib.h"
 #include "rand_local.h"
+#include "crypto/context.h"
 
 #ifndef FIPS_MODULE
 # include <stdio.h>
@@ -434,7 +435,7 @@ typedef struct rand_global_st {
  * Initialize the OSSL_LIB_CTX global DRBGs on first use.
  * Returns the allocated global data on success or NULL on failure.
  */
-static void *rand_ossl_ctx_new(OSSL_LIB_CTX *libctx)
+void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx)
 {
     RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
 
@@ -469,7 +470,7 @@ static void *rand_ossl_ctx_new(OSSL_LIB_CTX *libctx)
     return NULL;
 }
 
-static void rand_ossl_ctx_free(void *vdgbl)
+void ossl_rand_ctx_free(void *vdgbl)
 {
     RAND_GLOBAL *dgbl = vdgbl;
 
@@ -491,16 +492,9 @@ static void rand_ossl_ctx_free(void *vdgbl)
     OPENSSL_free(dgbl);
 }
 
-static const OSSL_LIB_CTX_METHOD rand_drbg_ossl_ctx_method = {
-    OSSL_LIB_CTX_METHOD_PRIORITY_2,
-    rand_ossl_ctx_new,
-    rand_ossl_ctx_free,
-};
-
 static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx)
 {
-    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX,
-                                 &rand_drbg_ossl_ctx_method);
+    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
 }
 
 static void rand_delete_thread_state(void *arg)
index dad4be208a4a47119e437d3b17491b1781c06b21..e0999fb05f0b84c1221be563dcc831ac9a5b4578 100644 (file)
@@ -11,6 +11,7 @@
 #include <openssl/core_names.h>
 #include <openssl/params.h>
 #include "internal/cryptlib.h"
+#include "crypto/context.h"
 
 typedef struct self_test_cb_st
 {
@@ -32,7 +33,7 @@ struct ossl_self_test_st
 };
 
 #ifndef FIPS_MODULE
-static void *self_test_set_callback_new(OSSL_LIB_CTX *ctx)
+void *ossl_self_test_set_callback_new(OSSL_LIB_CTX *ctx)
 {
     SELF_TEST_CB *stcb;
 
@@ -40,21 +41,14 @@ static void *self_test_set_callback_new(OSSL_LIB_CTX *ctx)
     return stcb;
 }
 
-static void self_test_set_callback_free(void *stcb)
+void ossl_self_test_set_callback_free(void *stcb)
 {
     OPENSSL_free(stcb);
 }
 
-static const OSSL_LIB_CTX_METHOD self_test_set_callback_method = {
-    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-    self_test_set_callback_new,
-    self_test_set_callback_free,
-};
-
 static SELF_TEST_CB *get_self_test_callback(OSSL_LIB_CTX *libctx)
 {
-    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_SELF_TEST_CB_INDEX,
-                                 &self_test_set_callback_method);
+    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_SELF_TEST_CB_INDEX);
 }
 
 void OSSL_SELF_TEST_set_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK *cb,
index 4d18645b21ce6baa9e58559cdc5891e89e725be8..8256970d30dd9143800e7e9ccecd02d352608bea 100644 (file)
@@ -14,6 +14,7 @@
 #include "internal/property.h"
 #include "internal/provider.h"
 #include "store_local.h"
+#include "crypto/context.h"
 
 int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader)
 {
@@ -68,25 +69,6 @@ static void free_loader(void *method)
     OSSL_STORE_LOADER_free(method);
 }
 
-/* Permanent loader method store, constructor and destructor */
-static void loader_store_free(void *vstore)
-{
-    ossl_method_store_free(vstore);
-}
-
-static void *loader_store_new(OSSL_LIB_CTX *ctx)
-{
-    return ossl_method_store_new(ctx);
-}
-
-
-static const OSSL_LIB_CTX_METHOD loader_store_method = {
-    /* We want loader_store to be cleaned up before the provider store */
-    OSSL_LIB_CTX_METHOD_PRIORITY_2,
-    loader_store_new,
-    loader_store_free,
-};
-
 /* Data to be passed through ossl_method_construct() */
 struct loader_data_st {
     OSSL_LIB_CTX *libctx;
@@ -123,8 +105,7 @@ static void *get_tmp_loader_store(void *data)
 /* Get the permanent loader store */
 static OSSL_METHOD_STORE *get_loader_store(OSSL_LIB_CTX *libctx)
 {
-    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX,
-                                &loader_store_method);
+    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX);
 }
 
 /* Get loader methods from a store, or put one in */
index faedf7275f08d6561f5b0c2c29e70b2bb61250f7..2ffd000da189eefc2a3eafa854e0a2f02ff889b7 100644 (file)
@@ -11,14 +11,7 @@ ossl_lib_ctx_is_child
  #include <openssl/types.h>
  #include "internal/cryptlib.h"
 
- typedef struct ossl_lib_ctx_method {
-     int priority;
-     void *(*new_func)(OSSL_LIB_CTX *ctx);
-     void (*free_func)(void *);
- } OSSL_LIB_CTX_METHOD;
-
- void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
-                             const OSSL_LIB_CTX_METHOD *meth);
+ void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index);
 
  int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
                            ossl_lib_ctx_run_once_fn run_once_fn);
@@ -28,38 +21,24 @@ ossl_lib_ctx_is_child
 
 =head1 DESCRIPTION
 
-Internally, the OpenSSL library context B<OSSL_LIB_CTX> is implemented
-as a B<CRYPTO_EX_DATA>, which allows data from diverse parts of the
-library to be added and removed dynamically.
-Each such data item must have a corresponding CRYPTO_EX_DATA index
-associated with it. Unlike normal CRYPTO_EX_DATA objects we use static indexes
-to identify data items. These are mapped transparently to CRYPTO_EX_DATA dynamic
-indexes internally to the implementation.
-See the example further down to see how that's done.
-
-ossl_lib_ctx_get_data() is used to retrieve a pointer to the data in
-the library context I<ctx> associated with the given I<index>. An
-OSSL_LIB_CTX_METHOD must be defined and given in the I<meth> parameter. The index
-for it should be defined in cryptlib.h. The functions through the method are
-used to create or free items that are stored at that index whenever a library
-context is created or freed, meaning that the code that use a data item of that
-index doesn't have to worry about that, just use the data available.
-
-Deallocation of an index happens automatically when the library
-context is freed.
-
-ossl_lib_ctx_run_once is used to run some initialisation routine I<run_once_fn>
+ossl_lib_ctx_run_once() is used to run some initialisation routine I<run_once_fn>
 exactly once per library context I<ctx> object. Each initialisation routine
 should be allocate a unique run once index in cryptlib.h.
 
 Any resources allocated via a run once initialisation routine can be cleaned up
-using ossl_lib_ctx_onfree. This associates an "on free" routine I<onfreefn> with
+using ossl_lib_ctx_onfree(). This associates an "on free" routine I<onfreefn> with
 the library context I<ctx>. When I<ctx> is freed all associated "on free"
 routines are called.
 
 ossl_lib_ctx_is_child() returns 1 if this library context is a child and 0
 otherwise.
 
+ossl_lib_ctx_get_data() allows different parts of the library to retrieve
+pointers to structures used in diverse parts of the library. The lifetime of
+these structures is managed by B<OSSL_LIB_CTX>. The different objects which can
+be retrieved are specified with the given argument I<index>. The valid values of
+I<index> are specified in cryptlib.h.
+
 =head1 RETURN VALUES
 
 ossl_lib_ctx_get_data() returns a pointer on success, or NULL on
@@ -67,51 +46,15 @@ failure.
 
 =head1 EXAMPLES
 
-=head2 Initialization
-
-For a type C<FOO> that should end up in the OpenSSL library context, a
-small bit of initialization is needed, i.e. to associate a constructor
-and a destructor to an index.
-
- typedef struct foo_st {
-     int i;
-     void *data;
- } FOO;
-
- static void *foo_new(OSSL_LIB_CTX *ctx)
- {
-     FOO *ptr = OPENSSL_zalloc(sizeof(*foo));
-     if (ptr != NULL)
-         ptr->i = 42;
-     return ptr;
- }
- static void foo_free(void *ptr)
- {
-     OPENSSL_free(ptr);
- }
-
- /*
-  * Include a reference to this in the methods table in context.c
-  * OSSL_LIB_CTX_FOO_INDEX should be added to internal/cryptlib.h
-  * Priorities can be OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-  * OSSL_LIB_CTX_METHOD_PRIORITY_1, OSSL_LIB_CTX_METHOD_PRIORITY_2, etc.
-  * Default priority is low (0). The higher the priority the earlier the
-  * method's destructor will be called when the library context is cleaned up.
-  */
- const OSSL_LIB_CTX_METHOD foo_method = {
-     OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-     foo_new,
-     foo_free
- };
-
 =head2 Usage
 
-To get and use the data stored in the library context, simply do this:
+To obtain a pointer for an object managed by the library context, simply do
+this:
 
  /*
   * ctx is received from a caller,
   */
- FOO *data = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_FOO_INDEX, &foo_method);
+ FOO *data = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_FOO_INDEX);
 
 =head2 Run Once
 
diff --git a/include/crypto/context.h b/include/crypto/context.h
new file mode 100644 (file)
index 0000000..143f6d6
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/core.h>
+
+void *ossl_provider_store_new(OSSL_LIB_CTX *);
+void *ossl_property_string_data_new(OSSL_LIB_CTX *);
+void *ossl_stored_namemap_new(OSSL_LIB_CTX *);
+void *ossl_property_defns_new(OSSL_LIB_CTX *);
+void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *);
+void *ossl_rand_ctx_new(OSSL_LIB_CTX *);
+void *ossl_prov_conf_ctx_new(OSSL_LIB_CTX *);
+void *ossl_bio_core_globals_new(OSSL_LIB_CTX *);
+void *ossl_child_prov_ctx_new(OSSL_LIB_CTX *);
+void *ossl_prov_drbg_nonce_ctx_new(OSSL_LIB_CTX *);
+void *ossl_self_test_set_callback_new(OSSL_LIB_CTX *);
+void *ossl_rand_crng_ctx_new(OSSL_LIB_CTX *);
+void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *);
+void *ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX *);
+
+void ossl_provider_store_free(void *);
+void ossl_property_string_data_free(void *);
+void ossl_stored_namemap_free(void *);
+void ossl_property_defns_free(void *);
+void ossl_ctx_global_properties_free(void *);
+void ossl_rand_ctx_free(void *);
+void ossl_prov_conf_ctx_free(void *);
+void ossl_bio_core_globals_free(void *);
+void ossl_child_prov_ctx_free(void *);
+void ossl_prov_drbg_nonce_ctx_free(void *);
+void ossl_self_test_set_callback_free(void *);
+void ossl_rand_crng_ctx_free(void *);
+void ossl_thread_event_ctx_free(void *);
+void ossl_fips_prov_ossl_ctx_free(void *);
index c017d0648327f15e7c8b1c68e09e428fb5e0c861..ab403c512e0a4c964baa77b75c9ab71807a7a2ac 100644 (file)
@@ -117,24 +117,12 @@ typedef struct ossl_ex_data_global_st {
 # define OSSL_LIB_CTX_CHILD_PROVIDER_INDEX          18
 # define OSSL_LIB_CTX_MAX_INDEXES                   19
 
-# define OSSL_LIB_CTX_METHOD_LOW_PRIORITY          -1
-# define OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY       0
-# define OSSL_LIB_CTX_METHOD_PRIORITY_1             1
-# define OSSL_LIB_CTX_METHOD_PRIORITY_2             2
-
-typedef struct ossl_lib_ctx_method {
-    int priority;
-    void *(*new_func)(OSSL_LIB_CTX *ctx);
-    void (*free_func)(void *);
-} OSSL_LIB_CTX_METHOD;
-
 OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx);
 int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx);
 int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx);
 
 /* Functions to retrieve pointers to data by index */
-void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *, int /* index */,
-                            const OSSL_LIB_CTX_METHOD * ctx);
+void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *, int /* index */);
 
 void ossl_lib_ctx_default_deinit(void);
 OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx);
index f4605dcd6ce5f44a65ba6e04c158ff73d7aa29b7..8bd61654e83611f34afd4397dc18b0eec273d83b 100644 (file)
@@ -22,6 +22,7 @@
 #include "prov/provider_util.h"
 #include "prov/seeding.h"
 #include "self_test.h"
+#include "crypto/context.h"
 
 static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
 static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
@@ -78,7 +79,7 @@ typedef struct fips_global_st {
     const char *fips_security_check_option;
 } FIPS_GLOBAL;
 
-static void *fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
+void *ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
 {
     FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
 
@@ -90,18 +91,11 @@ static void *fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
     return fgbl;
 }
 
-static void fips_prov_ossl_ctx_free(void *fgbl)
+void ossl_fips_prov_ossl_ctx_free(void *fgbl)
 {
     OPENSSL_free(fgbl);
 }
 
-static const OSSL_LIB_CTX_METHOD fips_prov_ossl_ctx_method = {
-    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-    fips_prov_ossl_ctx_new,
-    fips_prov_ossl_ctx_free,
-};
-
-
 /* Parameters we provide to the core */
 static const OSSL_PARAM fips_param_types[] = {
     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
@@ -170,8 +164,7 @@ static int fips_get_params(void *provctx, OSSL_PARAM params[])
 {
     OSSL_PARAM *p;
     FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
-                                              OSSL_LIB_CTX_FIPS_PROV_INDEX,
-                                              &fips_prov_ossl_ctx_method);
+                                              OSSL_LIB_CTX_FIPS_PROV_INDEX);
 
     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
@@ -208,8 +201,7 @@ static void set_self_test_cb(FIPS_GLOBAL *fgbl)
 static int fips_self_test(void *provctx)
 {
     FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
-                                              OSSL_LIB_CTX_FIPS_PROV_INDEX,
-                                              &fips_prov_ossl_ctx_method);
+                                              OSSL_LIB_CTX_FIPS_PROV_INDEX);
 
     set_self_test_cb(fgbl);
     return SELF_TEST_post(&fgbl->selftest_params, 1) ? 1 : 0;
@@ -666,8 +658,7 @@ int OSSL_provider_init_int(const OSSL_CORE_HANDLE *handle,
         goto err;
     }
 
-    if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX,
-                                      &fips_prov_ossl_ctx_method)) == NULL)
+    if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX)) == NULL)
         goto err;
 
     fgbl->handle = handle;
@@ -812,8 +803,7 @@ int ERR_pop_to_mark(void)
 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
 {
     FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
-                                              OSSL_LIB_CTX_FIPS_PROV_INDEX,
-                                              &fips_prov_ossl_ctx_method);
+                                              OSSL_LIB_CTX_FIPS_PROV_INDEX);
 
     if (fgbl == NULL)
         return NULL;
@@ -891,8 +881,7 @@ int BIO_snprintf(char *buf, size_t n, const char *format, ...)
 int FIPS_security_check_enabled(OSSL_LIB_CTX *libctx)
 {
     FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
-                                              OSSL_LIB_CTX_FIPS_PROV_INDEX,
-                                              &fips_prov_ossl_ctx_method);
+                                              OSSL_LIB_CTX_FIPS_PROV_INDEX);
 
     return fgbl->fips_security_checks;
 }
index 4095994bda11f86d284f4cff33cf10b30f8a2106..50d4a429da4cbc8235ebfa9adff4f55b6fc2790f 100644 (file)
@@ -23,6 +23,7 @@
 #include "crypto/rand_pool.h"
 #include "drbg_local.h"
 #include "prov/seeding.h"
+#include "crypto/context.h"
 
 typedef struct crng_test_global_st {
     unsigned char crngt_prev[EVP_MAX_MD_SIZE];
@@ -52,7 +53,7 @@ static int crngt_get_entropy(PROV_CTX *provctx, const EVP_MD *digest,
     return 0;
 }
 
-static void rand_crng_ossl_ctx_free(void *vcrngt_glob)
+void ossl_rand_crng_ctx_free(void *vcrngt_glob)
 {
     CRNG_TEST_GLOBAL *crngt_glob = vcrngt_glob;
 
@@ -61,7 +62,7 @@ static void rand_crng_ossl_ctx_free(void *vcrngt_glob)
     OPENSSL_free(crngt_glob);
 }
 
-static void *rand_crng_ossl_ctx_new(OSSL_LIB_CTX *ctx)
+void *ossl_rand_crng_ctx_new(OSSL_LIB_CTX *ctx)
 {
     CRNG_TEST_GLOBAL *crngt_glob = OPENSSL_zalloc(sizeof(*crngt_glob));
 
@@ -82,12 +83,6 @@ static void *rand_crng_ossl_ctx_new(OSSL_LIB_CTX *ctx)
     return crngt_glob;
 }
 
-static const OSSL_LIB_CTX_METHOD rand_crng_ossl_ctx_method = {
-    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-    rand_crng_ossl_ctx_new,
-    rand_crng_ossl_ctx_free,
-};
-
 static int prov_crngt_compare_previous(const unsigned char *prev,
                                        const unsigned char *cur,
                                        size_t sz)
@@ -113,8 +108,7 @@ size_t ossl_crngt_get_entropy(PROV_DRBG *drbg,
     int crng_test_pass = 1;
     OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx);
     CRNG_TEST_GLOBAL *crngt_glob
-        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_RAND_CRNGT_INDEX,
-                                &rand_crng_ossl_ctx_method);
+        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_RAND_CRNGT_INDEX);
     OSSL_CALLBACK *stcb = NULL;
     void *stcbarg = NULL;
     OSSL_SELF_TEST *st = NULL;
index 16d382dced8f5cc2ee4a4594b81579dd05c9b503..99e37d7bbf83ce45570aefb7db79a00441dfb7a5 100644 (file)
@@ -21,6 +21,7 @@
 #include "crypto/rand_pool.h"
 #include "prov/provider_ctx.h"
 #include "prov/providercommon.h"
+#include "crypto/context.h"
 
 /*
  * Support framework for NIST SP 800-90A DRBG
@@ -274,7 +275,7 @@ typedef struct prov_drbg_nonce_global_st {
  * to be in a different global data object. Otherwise we will go into an
  * infinite recursion loop.
  */
-static void *prov_drbg_nonce_ossl_ctx_new(OSSL_LIB_CTX *libctx)
+void *ossl_prov_drbg_nonce_ctx_new(OSSL_LIB_CTX *libctx)
 {
     PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl));
 
@@ -290,7 +291,7 @@ static void *prov_drbg_nonce_ossl_ctx_new(OSSL_LIB_CTX *libctx)
     return dngbl;
 }
 
-static void prov_drbg_nonce_ossl_ctx_free(void *vdngbl)
+void ossl_prov_drbg_nonce_ctx_free(void *vdngbl)
 {
     PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl;
 
@@ -302,12 +303,6 @@ static void prov_drbg_nonce_ossl_ctx_free(void *vdngbl)
     OPENSSL_free(dngbl);
 }
 
-static const OSSL_LIB_CTX_METHOD drbg_nonce_ossl_ctx_method = {
-    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-    prov_drbg_nonce_ossl_ctx_new,
-    prov_drbg_nonce_ossl_ctx_free,
-};
-
 /* Get a nonce from the operating system */
 static size_t prov_drbg_get_nonce(PROV_DRBG *drbg, unsigned char **pout,
                                   size_t min_len, size_t max_len)
@@ -316,8 +311,7 @@ static size_t prov_drbg_get_nonce(PROV_DRBG *drbg, unsigned char **pout,
     unsigned char *buf = NULL;
     OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx);
     PROV_DRBG_NONCE_GLOBAL *dngbl
-        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_NONCE_INDEX,
-                                &drbg_nonce_ossl_ctx_method);
+        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_NONCE_INDEX);
     struct {
         void *drbg;
         int count;
index 4c02f601cc5283c106d3623e2e87c603a24c0064..fd7518c02042be7f2582dfcaafef8671cca85a7a 100644 (file)
 #include "internal/cryptlib.h"
 #include "testutil.h"
 
-/*
- * Everything between BEGIN EXAMPLE and END EXAMPLE is copied from
- * doc/internal/man3/ossl_lib_ctx_get_data.pod
- */
-
-/*
- * ======================================================================
- * BEGIN EXAMPLE
- */
-
-typedef struct foo_st {
-    int i;
-    void *data;
-} FOO;
-
-static void *foo_new(OSSL_LIB_CTX *ctx)
-{
-    FOO *ptr = OPENSSL_zalloc(sizeof(*ptr));
-    if (ptr != NULL)
-        ptr->i = 42;
-    return ptr;
-}
-static void foo_free(void *ptr)
-{
-    OPENSSL_free(ptr);
-}
-static const OSSL_LIB_CTX_METHOD foo_method = {
-    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
-    foo_new,
-    foo_free
-};
-
-/*
- * END EXAMPLE
- * ======================================================================
- */
-
-static int test_context(OSSL_LIB_CTX *ctx)
-{
-    FOO *data = NULL;
-
-    return TEST_ptr(data = ossl_lib_ctx_get_data(ctx, 0, &foo_method))
-        /* OPENSSL_zalloc in foo_new() initialized it to zero */
-        && TEST_int_eq(data->i, 42);
-}
-
-static int test_app_context(void)
-{
-    OSSL_LIB_CTX *ctx = NULL;
-    int result =
-        TEST_ptr(ctx = OSSL_LIB_CTX_new())
-        && test_context(ctx);
-
-    OSSL_LIB_CTX_free(ctx);
-    return result;
-}
-
-static int test_def_context(void)
-{
-    return test_context(NULL);
-}
-
 static int test_set0_default(void)
 {
     OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
     OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
     OSSL_LIB_CTX *prev;
     int testresult = 0;
-    FOO *data = NULL;
 
     if (!TEST_ptr(global)
             || !TEST_ptr(local)
-            || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))
-            || !TEST_ptr(data = ossl_lib_ctx_get_data(local, 0, &foo_method)))
-        goto err;
-
-    /* Set local "i" value to 43. Global "i" should be 42 */
-    data->i++;
-    if (!TEST_int_eq(data->i, 43))
-        goto err;
-
-    /* The default context should still be the "global" default */
-    if (!TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
-            || !TEST_int_eq(data->i, 42))
+            || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL)))
         goto err;
 
     /* Check we can change the local default context */
     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
-            || !TEST_ptr_eq(global, prev)
-            || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
-            || !TEST_int_eq(data->i, 43))
+            || !TEST_ptr_eq(global, prev))
         goto err;
 
     /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
-    if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))
-            || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
-            || !TEST_int_eq(data->i, 43))
+    if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL)))
         goto err;
 
     /* Global default should be unchanged */
@@ -116,10 +38,8 @@ static int test_set0_default(void)
         goto err;
 
     /* Check we can swap back to the global default */
-   if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
-            || !TEST_ptr_eq(local, prev)
-            || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
-            || !TEST_int_eq(data->i, 42))
+    if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
+            || !TEST_ptr_eq(local, prev))
         goto err;
 
     testresult = 1;
@@ -130,8 +50,6 @@ static int test_set0_default(void)
 
 int setup_tests(void)
 {
-    ADD_TEST(test_app_context);
-    ADD_TEST(test_def_context);
     ADD_TEST(test_set0_default);
     return 1;
 }