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"
14 #include "internal/core.h"
15 #include "internal/bio.h"
16 #include "internal/provider.h"
18 struct ossl_lib_ctx_onfree_list_st {
19 ossl_lib_ctx_onfree_fn *fn;
20 struct ossl_lib_ctx_onfree_list_st *next;
23 struct ossl_lib_ctx_st {
28 * For most data in the OSSL_LIB_CTX we just use ex_data to store it. But
29 * that doesn't work for ex_data itself - so we store that directly.
31 OSSL_EX_DATA_GLOBAL global;
33 /* Map internal static indexes to dynamically created indexes */
34 int dyn_indexes[OSSL_LIB_CTX_MAX_INDEXES];
36 /* Keep a separate lock for each index */
37 CRYPTO_RWLOCK *index_locks[OSSL_LIB_CTX_MAX_INDEXES];
39 CRYPTO_RWLOCK *oncelock;
40 int run_once_done[OSSL_LIB_CTX_MAX_RUN_ONCE];
41 int run_once_ret[OSSL_LIB_CTX_MAX_RUN_ONCE];
42 struct ossl_lib_ctx_onfree_list_st *onfreelist;
43 unsigned int ischild:1;
46 int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx)
48 return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
51 int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx)
53 return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
56 int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx)
58 return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock);
61 int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx)
63 ctx = ossl_lib_ctx_get_concrete(ctx);
70 static int context_init(OSSL_LIB_CTX *ctx)
75 ctx->lock = CRYPTO_THREAD_lock_new();
76 if (ctx->lock == NULL)
79 ctx->oncelock = CRYPTO_THREAD_lock_new();
80 if (ctx->oncelock == NULL)
83 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++) {
84 ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
85 ctx->dyn_indexes[i] = -1;
86 if (ctx->index_locks[i] == NULL)
90 /* OSSL_LIB_CTX is built on top of ex_data so we initialise that directly */
91 if (!ossl_do_ex_data_init(ctx))
95 if (!ossl_crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
97 ossl_crypto_cleanup_all_ex_data_int(ctx);
101 /* Everything depends on properties, so we also pre-initialise that */
102 if (!ossl_property_parse_init(ctx))
108 ossl_crypto_cleanup_all_ex_data_int(ctx);
109 CRYPTO_THREAD_lock_free(ctx->oncelock);
110 CRYPTO_THREAD_lock_free(ctx->lock);
115 static int context_deinit(OSSL_LIB_CTX *ctx)
117 struct ossl_lib_ctx_onfree_list_st *tmp, *onfree;
123 ossl_ctx_thread_stop(ctx);
125 onfree = ctx->onfreelist;
126 while (onfree != NULL) {
129 onfree = onfree->next;
132 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL, &ctx->data);
133 ossl_crypto_cleanup_all_ex_data_int(ctx);
134 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
135 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
137 CRYPTO_THREAD_lock_free(ctx->oncelock);
138 CRYPTO_THREAD_lock_free(ctx->lock);
144 /* The default default context */
145 static OSSL_LIB_CTX default_context_int;
147 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
148 static CRYPTO_THREAD_LOCAL default_context_thread_local;
150 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
152 return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
153 && context_init(&default_context_int);
156 void ossl_lib_ctx_default_deinit(void)
158 context_deinit(&default_context_int);
161 static OSSL_LIB_CTX *get_thread_default_context(void)
163 if (!RUN_ONCE(&default_context_init, default_context_do_init))
166 return CRYPTO_THREAD_get_local(&default_context_thread_local);
169 static OSSL_LIB_CTX *get_default_context(void)
171 OSSL_LIB_CTX *current_defctx = get_thread_default_context();
173 if (current_defctx == NULL)
174 current_defctx = &default_context_int;
175 return current_defctx;
178 static int set_default_context(OSSL_LIB_CTX *defctx)
180 if (defctx == &default_context_int)
183 return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
187 OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
189 OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
191 if (ctx != NULL && !context_init(ctx)) {
192 OSSL_LIB_CTX_free(ctx);
199 OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
200 const OSSL_DISPATCH *in)
202 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
207 if (!ossl_bio_init_core(ctx, in)) {
208 OSSL_LIB_CTX_free(ctx);
215 OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
216 const OSSL_DISPATCH *in)
218 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
223 if (!ossl_provider_init_as_child(ctx, handle, in)) {
224 OSSL_LIB_CTX_free(ctx);
232 int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
234 return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
238 void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
240 if (ossl_lib_ctx_is_default(ctx))
245 ossl_provider_deinit_child(ctx);
252 OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
254 if (!RUN_ONCE(&default_context_init, default_context_do_init))
257 return &default_context_int;
260 OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
262 OSSL_LIB_CTX *current_defctx;
264 if ((current_defctx = get_default_context()) != NULL) {
266 set_default_context(libctx);
267 return current_defctx;
274 OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
278 return get_default_context();
283 int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
286 if (ctx == NULL || ctx == get_default_context())
292 int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
295 if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
301 static void ossl_lib_ctx_generic_new(void *parent_ign, void *ptr_ign,
302 CRYPTO_EX_DATA *ad, int index,
303 long argl_ign, void *argp)
305 const OSSL_LIB_CTX_METHOD *meth = argp;
306 OSSL_LIB_CTX *ctx = ossl_crypto_ex_data_get_ossl_lib_ctx(ad);
307 void *ptr = meth->new_func(ctx);
310 if (!CRYPTO_THREAD_write_lock(ctx->lock))
312 * Can't return something, so best to hope that something will
316 CRYPTO_set_ex_data(ad, index, ptr);
317 CRYPTO_THREAD_unlock(ctx->lock);
320 static void ossl_lib_ctx_generic_free(void *parent_ign, void *ptr,
321 CRYPTO_EX_DATA *ad, int index,
322 long argl_ign, void *argp)
324 const OSSL_LIB_CTX_METHOD *meth = argp;
326 meth->free_func(ptr);
329 static int ossl_lib_ctx_init_index(OSSL_LIB_CTX *ctx, int static_index,
330 const OSSL_LIB_CTX_METHOD *meth)
334 ctx = ossl_lib_ctx_get_concrete(ctx);
338 idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
340 ossl_lib_ctx_generic_new,
341 NULL, ossl_lib_ctx_generic_free,
346 ctx->dyn_indexes[static_index] = idx;
350 void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
351 const OSSL_LIB_CTX_METHOD *meth)
356 ctx = ossl_lib_ctx_get_concrete(ctx);
360 if (!CRYPTO_THREAD_read_lock(ctx->lock))
362 dynidx = ctx->dyn_indexes[index];
363 CRYPTO_THREAD_unlock(ctx->lock);
366 if (!CRYPTO_THREAD_read_lock(ctx->index_locks[index]))
368 if (!CRYPTO_THREAD_read_lock(ctx->lock)) {
369 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
372 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
373 CRYPTO_THREAD_unlock(ctx->lock);
374 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
378 if (!CRYPTO_THREAD_write_lock(ctx->index_locks[index]))
380 if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
381 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
385 dynidx = ctx->dyn_indexes[index];
387 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
388 CRYPTO_THREAD_unlock(ctx->lock);
389 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
393 if (!ossl_lib_ctx_init_index(ctx, index, meth)) {
394 CRYPTO_THREAD_unlock(ctx->lock);
395 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
399 CRYPTO_THREAD_unlock(ctx->lock);
402 * The alloc call ensures there's a value there. We release the ctx->lock
403 * for this, because the allocation itself may recursively call
404 * ossl_lib_ctx_get_data for other indexes (never this one). The allocation
405 * will itself aquire the ctx->lock when it actually comes to store the
406 * allocated data (see ossl_lib_ctx_generic_new() above). We call
407 * ossl_crypto_alloc_ex_data_intern() here instead of CRYPTO_alloc_ex_data().
408 * They do the same thing except that the latter calls CRYPTO_get_ex_data()
409 * as well - which we must not do without holding the ctx->lock.
411 if (ossl_crypto_alloc_ex_data_intern(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
412 &ctx->data, ctx->dyn_indexes[index])) {
413 if (!CRYPTO_THREAD_read_lock(ctx->lock))
415 data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
416 CRYPTO_THREAD_unlock(ctx->lock);
420 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
424 OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
426 ctx = ossl_lib_ctx_get_concrete(ctx);
432 int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
433 ossl_lib_ctx_run_once_fn run_once_fn)
435 int done = 0, ret = 0;
437 ctx = ossl_lib_ctx_get_concrete(ctx);
441 if (!CRYPTO_THREAD_read_lock(ctx->oncelock))
443 done = ctx->run_once_done[idx];
445 ret = ctx->run_once_ret[idx];
446 CRYPTO_THREAD_unlock(ctx->oncelock);
451 if (!CRYPTO_THREAD_write_lock(ctx->oncelock))
453 if (ctx->run_once_done[idx]) {
454 ret = ctx->run_once_ret[idx];
455 CRYPTO_THREAD_unlock(ctx->oncelock);
459 ret = run_once_fn(ctx);
460 ctx->run_once_done[idx] = 1;
461 ctx->run_once_ret[idx] = ret;
462 CRYPTO_THREAD_unlock(ctx->oncelock);
467 int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn)
469 struct ossl_lib_ctx_onfree_list_st *newonfree
470 = OPENSSL_malloc(sizeof(*newonfree));
472 if (newonfree == NULL)
475 newonfree->fn = onfreefn;
476 newonfree->next = ctx->onfreelist;
477 ctx->onfreelist = newonfree;
482 const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
485 return "FIPS internal library context";
487 if (ossl_lib_ctx_is_global_default(libctx))
488 return "Global default library context";
489 if (ossl_lib_ctx_is_default(libctx))
490 return "Thread-local default library context";
491 return "Non-default library context";