2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "crypto/cryptlib.h"
11 #include <openssl/conf.h>
12 #include "internal/thread_once.h"
13 #include "internal/property.h"
15 struct ossl_lib_ctx_onfree_list_st {
16 ossl_lib_ctx_onfree_fn *fn;
17 struct ossl_lib_ctx_onfree_list_st *next;
20 struct ossl_lib_ctx_st {
25 * For most data in the OSSL_LIB_CTX we just use ex_data to store it. But
26 * that doesn't work for ex_data itself - so we store that directly.
28 OSSL_EX_DATA_GLOBAL global;
30 /* Map internal static indexes to dynamically created indexes */
31 int dyn_indexes[OSSL_LIB_CTX_MAX_INDEXES];
33 /* Keep a separate lock for each index */
34 CRYPTO_RWLOCK *index_locks[OSSL_LIB_CTX_MAX_INDEXES];
36 CRYPTO_RWLOCK *oncelock;
37 int run_once_done[OSSL_LIB_CTX_MAX_RUN_ONCE];
38 int run_once_ret[OSSL_LIB_CTX_MAX_RUN_ONCE];
39 struct ossl_lib_ctx_onfree_list_st *onfreelist;
42 static int context_init(OSSL_LIB_CTX *ctx)
47 ctx->lock = CRYPTO_THREAD_lock_new();
48 if (ctx->lock == NULL)
51 ctx->oncelock = CRYPTO_THREAD_lock_new();
52 if (ctx->oncelock == NULL)
55 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++) {
56 ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
57 ctx->dyn_indexes[i] = -1;
58 if (ctx->index_locks[i] == NULL)
62 /* OSSL_LIB_CTX is built on top of ex_data so we initialise that directly */
63 if (!ossl_do_ex_data_init(ctx))
67 if (!ossl_crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
69 ossl_crypto_cleanup_all_ex_data_int(ctx);
73 /* Everything depends on properties, so we also pre-initialise that */
74 if (!ossl_property_parse_init(ctx))
80 ossl_crypto_cleanup_all_ex_data_int(ctx);
81 CRYPTO_THREAD_lock_free(ctx->oncelock);
82 CRYPTO_THREAD_lock_free(ctx->lock);
87 static int context_deinit(OSSL_LIB_CTX *ctx)
89 struct ossl_lib_ctx_onfree_list_st *tmp, *onfree;
95 ossl_ctx_thread_stop(ctx);
97 onfree = ctx->onfreelist;
98 while (onfree != NULL) {
101 onfree = onfree->next;
104 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL, &ctx->data);
105 ossl_crypto_cleanup_all_ex_data_int(ctx);
106 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
107 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
109 CRYPTO_THREAD_lock_free(ctx->oncelock);
110 CRYPTO_THREAD_lock_free(ctx->lock);
116 /* The default default context */
117 static OSSL_LIB_CTX default_context_int;
119 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
120 static CRYPTO_THREAD_LOCAL default_context_thread_local;
122 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
124 return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
125 && context_init(&default_context_int);
128 void ossl_lib_ctx_default_deinit(void)
130 context_deinit(&default_context_int);
133 static OSSL_LIB_CTX *get_thread_default_context(void)
135 if (!RUN_ONCE(&default_context_init, default_context_do_init))
138 return CRYPTO_THREAD_get_local(&default_context_thread_local);
141 static OSSL_LIB_CTX *get_default_context(void)
143 OSSL_LIB_CTX *current_defctx = get_thread_default_context();
145 if (current_defctx == NULL)
146 current_defctx = &default_context_int;
147 return current_defctx;
150 static int set_default_context(OSSL_LIB_CTX *defctx)
152 if (defctx == &default_context_int)
155 return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
159 OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
161 OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
163 if (ctx != NULL && !context_init(ctx)) {
164 OSSL_LIB_CTX_free(ctx);
171 int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
173 return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
177 void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
179 if (ossl_lib_ctx_is_default(ctx))
186 OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
189 OSSL_LIB_CTX *current_defctx;
191 if ((current_defctx = get_default_context()) != NULL
192 && set_default_context(libctx))
193 return current_defctx;
199 OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
203 return get_default_context();
208 int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
211 if (ctx == NULL || ctx == get_default_context())
217 int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
220 if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
226 static void ossl_lib_ctx_generic_new(void *parent_ign, void *ptr_ign,
227 CRYPTO_EX_DATA *ad, int index,
228 long argl_ign, void *argp)
230 const OSSL_LIB_CTX_METHOD *meth = argp;
231 OSSL_LIB_CTX *ctx = ossl_crypto_ex_data_get_ossl_lib_ctx(ad);
232 void *ptr = meth->new_func(ctx);
235 if (!CRYPTO_THREAD_write_lock(ctx->lock))
237 * Can't return something, so best to hope that something will
241 CRYPTO_set_ex_data(ad, index, ptr);
242 CRYPTO_THREAD_unlock(ctx->lock);
245 static void ossl_lib_ctx_generic_free(void *parent_ign, void *ptr,
246 CRYPTO_EX_DATA *ad, int index,
247 long argl_ign, void *argp)
249 const OSSL_LIB_CTX_METHOD *meth = argp;
251 meth->free_func(ptr);
254 /* Non-static so we can use it in context_internal_test */
255 static int ossl_lib_ctx_init_index(OSSL_LIB_CTX *ctx, int static_index,
256 const OSSL_LIB_CTX_METHOD *meth)
260 ctx = ossl_lib_ctx_get_concrete(ctx);
264 idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
266 ossl_lib_ctx_generic_new,
267 NULL, ossl_lib_ctx_generic_free);
271 ctx->dyn_indexes[static_index] = idx;
275 void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
276 const OSSL_LIB_CTX_METHOD *meth)
281 ctx = ossl_lib_ctx_get_concrete(ctx);
285 if (!CRYPTO_THREAD_read_lock(ctx->lock))
287 dynidx = ctx->dyn_indexes[index];
288 CRYPTO_THREAD_unlock(ctx->lock);
291 if (!CRYPTO_THREAD_read_lock(ctx->index_locks[index]))
293 if (!CRYPTO_THREAD_read_lock(ctx->lock)) {
294 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
297 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
298 CRYPTO_THREAD_unlock(ctx->lock);
299 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
303 if (!CRYPTO_THREAD_write_lock(ctx->index_locks[index]))
305 if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
306 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
310 dynidx = ctx->dyn_indexes[index];
312 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
313 CRYPTO_THREAD_unlock(ctx->lock);
314 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
318 if (!ossl_lib_ctx_init_index(ctx, index, meth)) {
319 CRYPTO_THREAD_unlock(ctx->lock);
320 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
324 CRYPTO_THREAD_unlock(ctx->lock);
327 * The alloc call ensures there's a value there. We release the ctx->lock
328 * for this, because the allocation itself may recursively call
329 * ossl_lib_ctx_get_data for other indexes (never this one). The allocation
330 * will itself aquire the ctx->lock when it actually comes to store the
331 * allocated data (see ossl_lib_ctx_generic_new() above). We call
332 * ossl_crypto_alloc_ex_data_intern() here instead of CRYPTO_alloc_ex_data().
333 * They do the same thing except that the latter calls CRYPTO_get_ex_data()
334 * as well - which we must not do without holding the ctx->lock.
336 if (ossl_crypto_alloc_ex_data_intern(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
337 &ctx->data, ctx->dyn_indexes[index])) {
338 if (!CRYPTO_THREAD_read_lock(ctx->lock))
340 data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
341 CRYPTO_THREAD_unlock(ctx->lock);
345 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
349 OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
351 ctx = ossl_lib_ctx_get_concrete(ctx);
357 int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
358 ossl_lib_ctx_run_once_fn run_once_fn)
360 int done = 0, ret = 0;
362 ctx = ossl_lib_ctx_get_concrete(ctx);
366 if (!CRYPTO_THREAD_read_lock(ctx->oncelock))
368 done = ctx->run_once_done[idx];
370 ret = ctx->run_once_ret[idx];
371 CRYPTO_THREAD_unlock(ctx->oncelock);
376 if (!CRYPTO_THREAD_write_lock(ctx->oncelock))
378 if (ctx->run_once_done[idx]) {
379 ret = ctx->run_once_ret[idx];
380 CRYPTO_THREAD_unlock(ctx->oncelock);
384 ret = run_once_fn(ctx);
385 ctx->run_once_done[idx] = 1;
386 ctx->run_once_ret[idx] = ret;
387 CRYPTO_THREAD_unlock(ctx->oncelock);
392 int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn)
394 struct ossl_lib_ctx_onfree_list_st *newonfree
395 = OPENSSL_malloc(sizeof(*newonfree));
397 if (newonfree == NULL)
400 newonfree->fn = onfreefn;
401 newonfree->next = ctx->onfreelist;
402 ctx->onfreelist = newonfree;
407 const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
410 return "FIPS internal library context";
412 if (ossl_lib_ctx_is_global_default(libctx))
413 return "Global default library context";
414 if (ossl_lib_ctx_is_default(libctx))
415 return "Thread-local default library context";
416 return "Non-default library context";