2 * Copyright 2019-2022 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"
17 #include "crypto/ctype.h"
18 #include "crypto/rand.h"
20 struct ossl_lib_ctx_onfree_list_st {
21 ossl_lib_ctx_onfree_fn *fn;
22 struct ossl_lib_ctx_onfree_list_st *next;
25 struct ossl_lib_ctx_st {
30 * For most data in the OSSL_LIB_CTX we just use ex_data to store it. But
31 * that doesn't work for ex_data itself - so we store that directly.
33 OSSL_EX_DATA_GLOBAL global;
35 /* Map internal static indexes to dynamically created indexes */
36 int dyn_indexes[OSSL_LIB_CTX_MAX_INDEXES];
38 /* Keep a separate lock for each index */
39 CRYPTO_RWLOCK *index_locks[OSSL_LIB_CTX_MAX_INDEXES];
41 CRYPTO_RWLOCK *oncelock;
42 int run_once_done[OSSL_LIB_CTX_MAX_RUN_ONCE];
43 int run_once_ret[OSSL_LIB_CTX_MAX_RUN_ONCE];
44 struct ossl_lib_ctx_onfree_list_st *onfreelist;
45 unsigned int ischild:1;
48 int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx)
50 return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
53 int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx)
55 return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
58 int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx)
60 return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock);
63 int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx)
65 ctx = ossl_lib_ctx_get_concrete(ctx);
72 static int context_init(OSSL_LIB_CTX *ctx)
77 ctx->lock = CRYPTO_THREAD_lock_new();
78 if (ctx->lock == NULL)
81 ctx->oncelock = CRYPTO_THREAD_lock_new();
82 if (ctx->oncelock == NULL)
85 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++) {
86 ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
87 ctx->dyn_indexes[i] = -1;
88 if (ctx->index_locks[i] == NULL)
92 /* OSSL_LIB_CTX is built on top of ex_data so we initialise that directly */
93 if (!ossl_do_ex_data_init(ctx))
97 if (!ossl_crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
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 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
110 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
111 CRYPTO_THREAD_lock_free(ctx->oncelock);
112 CRYPTO_THREAD_lock_free(ctx->lock);
113 memset(ctx, '\0', sizeof(*ctx));
117 static int context_deinit(OSSL_LIB_CTX *ctx)
119 struct ossl_lib_ctx_onfree_list_st *tmp, *onfree;
125 ossl_ctx_thread_stop(ctx);
127 onfree = ctx->onfreelist;
128 while (onfree != NULL) {
131 onfree = onfree->next;
134 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL, &ctx->data);
135 ossl_crypto_cleanup_all_ex_data_int(ctx);
136 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
137 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
139 CRYPTO_THREAD_lock_free(ctx->oncelock);
140 CRYPTO_THREAD_lock_free(ctx->lock);
146 /* The default default context */
147 static OSSL_LIB_CTX default_context_int;
149 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
150 static CRYPTO_THREAD_LOCAL default_context_thread_local;
152 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
154 return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
155 && context_init(&default_context_int);
158 void ossl_lib_ctx_default_deinit(void)
160 context_deinit(&default_context_int);
161 CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
164 static OSSL_LIB_CTX *get_thread_default_context(void)
166 if (!RUN_ONCE(&default_context_init, default_context_do_init))
169 return CRYPTO_THREAD_get_local(&default_context_thread_local);
172 static OSSL_LIB_CTX *get_default_context(void)
174 OSSL_LIB_CTX *current_defctx = get_thread_default_context();
176 if (current_defctx == NULL)
177 current_defctx = &default_context_int;
178 return current_defctx;
181 static int set_default_context(OSSL_LIB_CTX *defctx)
183 if (defctx == &default_context_int)
186 return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
190 OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
192 OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
194 if (ctx != NULL && !context_init(ctx)) {
202 OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
203 const OSSL_DISPATCH *in)
205 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
210 if (!ossl_bio_init_core(ctx, in)) {
211 OSSL_LIB_CTX_free(ctx);
218 OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
219 const OSSL_DISPATCH *in)
221 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
226 if (!ossl_provider_init_as_child(ctx, handle, in)) {
227 OSSL_LIB_CTX_free(ctx);
235 int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
237 return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
241 void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
243 if (ossl_lib_ctx_is_default(ctx))
248 ossl_provider_deinit_child(ctx);
255 OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
257 if (!RUN_ONCE(&default_context_init, default_context_do_init))
260 return &default_context_int;
263 OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
265 OSSL_LIB_CTX *current_defctx;
267 if ((current_defctx = get_default_context()) != NULL) {
269 set_default_context(libctx);
270 return current_defctx;
276 void ossl_release_default_drbg_ctx(void)
278 int dynidx = default_context_int.dyn_indexes[OSSL_LIB_CTX_DRBG_INDEX];
280 /* early release of the DRBG in global default libctx, no locking */
284 data = CRYPTO_get_ex_data(&default_context_int.data, dynidx);
285 ossl_rand_ctx_free(data);
286 CRYPTO_set_ex_data(&default_context_int.data, dynidx, NULL);
291 OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
295 return get_default_context();
300 int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
303 if (ctx == NULL || ctx == get_default_context())
309 int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
312 if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
318 static void ossl_lib_ctx_generic_new(void *parent_ign, void *ptr_ign,
319 CRYPTO_EX_DATA *ad, int index,
320 long argl_ign, void *argp)
322 const OSSL_LIB_CTX_METHOD *meth = argp;
323 OSSL_LIB_CTX *ctx = ossl_crypto_ex_data_get_ossl_lib_ctx(ad);
324 void *ptr = meth->new_func(ctx);
327 if (!CRYPTO_THREAD_write_lock(ctx->lock))
329 * Can't return something, so best to hope that something will
333 CRYPTO_set_ex_data(ad, index, ptr);
334 CRYPTO_THREAD_unlock(ctx->lock);
337 static void ossl_lib_ctx_generic_free(void *parent_ign, void *ptr,
338 CRYPTO_EX_DATA *ad, int index,
339 long argl_ign, void *argp)
341 const OSSL_LIB_CTX_METHOD *meth = argp;
343 meth->free_func(ptr);
346 static int ossl_lib_ctx_init_index(OSSL_LIB_CTX *ctx, int static_index,
347 const OSSL_LIB_CTX_METHOD *meth)
351 ctx = ossl_lib_ctx_get_concrete(ctx);
355 idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
357 ossl_lib_ctx_generic_new,
358 NULL, ossl_lib_ctx_generic_free,
363 ctx->dyn_indexes[static_index] = idx;
367 void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
368 const OSSL_LIB_CTX_METHOD *meth)
373 ctx = ossl_lib_ctx_get_concrete(ctx);
377 if (!CRYPTO_THREAD_read_lock(ctx->lock))
379 dynidx = ctx->dyn_indexes[index];
380 CRYPTO_THREAD_unlock(ctx->lock);
383 if (!CRYPTO_THREAD_read_lock(ctx->index_locks[index]))
385 if (!CRYPTO_THREAD_read_lock(ctx->lock)) {
386 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
389 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
390 CRYPTO_THREAD_unlock(ctx->lock);
391 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
395 if (!CRYPTO_THREAD_write_lock(ctx->index_locks[index]))
397 if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
398 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
402 dynidx = ctx->dyn_indexes[index];
404 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
405 CRYPTO_THREAD_unlock(ctx->lock);
406 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
410 if (!ossl_lib_ctx_init_index(ctx, index, meth)) {
411 CRYPTO_THREAD_unlock(ctx->lock);
412 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
416 CRYPTO_THREAD_unlock(ctx->lock);
419 * The alloc call ensures there's a value there. We release the ctx->lock
420 * for this, because the allocation itself may recursively call
421 * ossl_lib_ctx_get_data for other indexes (never this one). The allocation
422 * will itself aquire the ctx->lock when it actually comes to store the
423 * allocated data (see ossl_lib_ctx_generic_new() above). We call
424 * ossl_crypto_alloc_ex_data_intern() here instead of CRYPTO_alloc_ex_data().
425 * They do the same thing except that the latter calls CRYPTO_get_ex_data()
426 * as well - which we must not do without holding the ctx->lock.
428 if (ossl_crypto_alloc_ex_data_intern(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
429 &ctx->data, ctx->dyn_indexes[index])) {
430 if (!CRYPTO_THREAD_read_lock(ctx->lock))
432 data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
433 CRYPTO_THREAD_unlock(ctx->lock);
437 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
441 OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
443 ctx = ossl_lib_ctx_get_concrete(ctx);
449 int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
450 ossl_lib_ctx_run_once_fn run_once_fn)
452 int done = 0, ret = 0;
454 ctx = ossl_lib_ctx_get_concrete(ctx);
458 if (!CRYPTO_THREAD_read_lock(ctx->oncelock))
460 done = ctx->run_once_done[idx];
462 ret = ctx->run_once_ret[idx];
463 CRYPTO_THREAD_unlock(ctx->oncelock);
468 if (!CRYPTO_THREAD_write_lock(ctx->oncelock))
470 if (ctx->run_once_done[idx]) {
471 ret = ctx->run_once_ret[idx];
472 CRYPTO_THREAD_unlock(ctx->oncelock);
476 ret = run_once_fn(ctx);
477 ctx->run_once_done[idx] = 1;
478 ctx->run_once_ret[idx] = ret;
479 CRYPTO_THREAD_unlock(ctx->oncelock);
484 int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn)
486 struct ossl_lib_ctx_onfree_list_st *newonfree
487 = OPENSSL_malloc(sizeof(*newonfree));
489 if (newonfree == NULL)
492 newonfree->fn = onfreefn;
493 newonfree->next = ctx->onfreelist;
494 ctx->onfreelist = newonfree;
499 const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
502 return "FIPS internal library context";
504 if (ossl_lib_ctx_is_global_default(libctx))
505 return "Global default library context";
506 if (ossl_lib_ctx_is_default(libctx))
507 return "Thread-local default library context";
508 return "Non-default library context";