2 * Copyright 2019-2023 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/decoder.h"
18 #include "crypto/context.h"
20 struct ossl_lib_ctx_st {
21 CRYPTO_RWLOCK *lock, *rand_crngt_lock;
22 OSSL_EX_DATA_GLOBAL global;
24 void *property_string_data;
25 void *evp_method_store;
29 void *global_properties;
32 CRYPTO_THREAD_LOCAL rcu_local_key;
37 OSSL_METHOD_STORE *decoder_store;
39 OSSL_METHOD_STORE *encoder_store;
40 OSSL_METHOD_STORE *store_loader_store;
43 #if defined(OPENSSL_THREADS)
48 void *thread_event_handler;
56 int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx)
58 return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
61 int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx)
63 return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
66 int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx)
68 return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock);
71 int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx)
73 ctx = ossl_lib_ctx_get_concrete(ctx);
80 static void context_deinit_objs(OSSL_LIB_CTX *ctx);
82 static int context_init(OSSL_LIB_CTX *ctx)
86 if (!CRYPTO_THREAD_init_local(&ctx->rcu_local_key, NULL))
89 ctx->lock = CRYPTO_THREAD_lock_new();
90 if (ctx->lock == NULL)
93 ctx->rand_crngt_lock = CRYPTO_THREAD_lock_new();
94 if (ctx->rand_crngt_lock == NULL)
97 /* Initialize ex_data. */
98 if (!ossl_do_ex_data_init(ctx))
102 /* P2. We want evp_method_store to be cleaned up before the provider store */
103 ctx->evp_method_store = ossl_method_store_new(ctx);
104 if (ctx->evp_method_store == NULL)
108 /* P2. Must be freed before the provider store is freed */
109 ctx->provider_conf = ossl_prov_conf_ctx_new(ctx);
110 if (ctx->provider_conf == NULL)
115 ctx->drbg = ossl_rand_ctx_new(ctx);
116 if (ctx->drbg == NULL)
121 * P2. We want decoder_store/decoder_cache to be cleaned up before the
124 ctx->decoder_store = ossl_method_store_new(ctx);
125 if (ctx->decoder_store == NULL)
127 ctx->decoder_cache = ossl_decoder_cache_new(ctx);
128 if (ctx->decoder_cache == NULL)
131 /* P2. We want encoder_store to be cleaned up before the provider store */
132 ctx->encoder_store = ossl_method_store_new(ctx);
133 if (ctx->encoder_store == NULL)
136 /* P2. We want loader_store to be cleaned up before the provider store */
137 ctx->store_loader_store = ossl_method_store_new(ctx);
138 if (ctx->store_loader_store == NULL)
142 /* P1. Needs to be freed before the child provider data is freed */
143 ctx->provider_store = ossl_provider_store_new(ctx);
144 if (ctx->provider_store == NULL)
147 /* Default priority. */
148 ctx->property_string_data = ossl_property_string_data_new(ctx);
149 if (ctx->property_string_data == NULL)
152 ctx->namemap = ossl_stored_namemap_new(ctx);
153 if (ctx->namemap == NULL)
156 ctx->property_defns = ossl_property_defns_new(ctx);
157 if (ctx->property_defns == NULL)
160 ctx->global_properties = ossl_ctx_global_properties_new(ctx);
161 if (ctx->global_properties == NULL)
165 ctx->bio_core = ossl_bio_core_globals_new(ctx);
166 if (ctx->bio_core == NULL)
170 ctx->drbg_nonce = ossl_prov_drbg_nonce_ctx_new(ctx);
171 if (ctx->drbg_nonce == NULL)
175 ctx->self_test_cb = ossl_self_test_set_callback_new(ctx);
176 if (ctx->self_test_cb == NULL)
181 ctx->thread_event_handler = ossl_thread_event_ctx_new(ctx);
182 if (ctx->thread_event_handler == NULL)
185 ctx->fips_prov = ossl_fips_prov_ossl_ctx_new(ctx);
186 if (ctx->fips_prov == NULL)
190 #ifndef OPENSSL_NO_THREAD_POOL
191 ctx->threads = ossl_threads_ctx_new(ctx);
192 if (ctx->threads == NULL)
198 ctx->child_provider = ossl_child_prov_ctx_new(ctx);
199 if (ctx->child_provider == NULL)
203 /* Everything depends on properties, so we also pre-initialise that */
204 if (!ossl_property_parse_init(ctx))
210 context_deinit_objs(ctx);
213 ossl_crypto_cleanup_all_ex_data_int(ctx);
215 CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
216 CRYPTO_THREAD_lock_free(ctx->lock);
217 CRYPTO_THREAD_cleanup_local(&ctx->rcu_local_key);
218 memset(ctx, '\0', sizeof(*ctx));
222 static void context_deinit_objs(OSSL_LIB_CTX *ctx)
224 /* P2. We want evp_method_store to be cleaned up before the provider store */
225 if (ctx->evp_method_store != NULL) {
226 ossl_method_store_free(ctx->evp_method_store);
227 ctx->evp_method_store = NULL;
231 if (ctx->drbg != NULL) {
232 ossl_rand_ctx_free(ctx->drbg);
238 if (ctx->provider_conf != NULL) {
239 ossl_prov_conf_ctx_free(ctx->provider_conf);
240 ctx->provider_conf = NULL;
244 * P2. We want decoder_store/decoder_cache to be cleaned up before the
247 if (ctx->decoder_store != NULL) {
248 ossl_method_store_free(ctx->decoder_store);
249 ctx->decoder_store = NULL;
251 if (ctx->decoder_cache != NULL) {
252 ossl_decoder_cache_free(ctx->decoder_cache);
253 ctx->decoder_cache = NULL;
257 /* P2. We want encoder_store to be cleaned up before the provider store */
258 if (ctx->encoder_store != NULL) {
259 ossl_method_store_free(ctx->encoder_store);
260 ctx->encoder_store = NULL;
263 /* P2. We want loader_store to be cleaned up before the provider store */
264 if (ctx->store_loader_store != NULL) {
265 ossl_method_store_free(ctx->store_loader_store);
266 ctx->store_loader_store = NULL;
270 /* P1. Needs to be freed before the child provider data is freed */
271 if (ctx->provider_store != NULL) {
272 ossl_provider_store_free(ctx->provider_store);
273 ctx->provider_store = NULL;
276 /* Default priority. */
277 if (ctx->property_string_data != NULL) {
278 ossl_property_string_data_free(ctx->property_string_data);
279 ctx->property_string_data = NULL;
282 if (ctx->namemap != NULL) {
283 ossl_stored_namemap_free(ctx->namemap);
287 if (ctx->property_defns != NULL) {
288 ossl_property_defns_free(ctx->property_defns);
289 ctx->property_defns = NULL;
292 if (ctx->global_properties != NULL) {
293 ossl_ctx_global_properties_free(ctx->global_properties);
294 ctx->global_properties = NULL;
298 if (ctx->bio_core != NULL) {
299 ossl_bio_core_globals_free(ctx->bio_core);
300 ctx->bio_core = NULL;
304 if (ctx->drbg_nonce != NULL) {
305 ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce);
306 ctx->drbg_nonce = NULL;
310 if (ctx->self_test_cb != NULL) {
311 ossl_self_test_set_callback_free(ctx->self_test_cb);
312 ctx->self_test_cb = NULL;
316 if (ctx->rand_crngt != NULL) {
317 ossl_rand_crng_ctx_free(ctx->rand_crngt);
318 ctx->rand_crngt = NULL;
322 if (ctx->thread_event_handler != NULL) {
323 ossl_thread_event_ctx_free(ctx->thread_event_handler);
324 ctx->thread_event_handler = NULL;
327 if (ctx->fips_prov != NULL) {
328 ossl_fips_prov_ossl_ctx_free(ctx->fips_prov);
329 ctx->fips_prov = NULL;
333 #ifndef OPENSSL_NO_THREAD_POOL
334 if (ctx->threads != NULL) {
335 ossl_threads_ctx_free(ctx->threads);
342 if (ctx->child_provider != NULL) {
343 ossl_child_prov_ctx_free(ctx->child_provider);
344 ctx->child_provider = NULL;
349 static int context_deinit(OSSL_LIB_CTX *ctx)
354 ossl_ctx_thread_stop(ctx);
356 context_deinit_objs(ctx);
358 ossl_crypto_cleanup_all_ex_data_int(ctx);
360 CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
361 CRYPTO_THREAD_lock_free(ctx->lock);
362 ctx->rand_crngt_lock = NULL;
364 CRYPTO_THREAD_cleanup_local(&ctx->rcu_local_key);
369 /* The default default context */
370 static OSSL_LIB_CTX default_context_int;
372 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
373 static CRYPTO_THREAD_LOCAL default_context_thread_local;
374 static int default_context_inited = 0;
376 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
378 if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL))
381 if (!context_init(&default_context_int))
384 default_context_inited = 1;
388 CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
393 void ossl_lib_ctx_default_deinit(void)
395 if (!default_context_inited)
397 context_deinit(&default_context_int);
398 CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
399 default_context_inited = 0;
402 static OSSL_LIB_CTX *get_thread_default_context(void)
404 if (!RUN_ONCE(&default_context_init, default_context_do_init))
407 return CRYPTO_THREAD_get_local(&default_context_thread_local);
410 static OSSL_LIB_CTX *get_default_context(void)
412 OSSL_LIB_CTX *current_defctx = get_thread_default_context();
414 if (current_defctx == NULL)
415 current_defctx = &default_context_int;
416 return current_defctx;
419 static int set_default_context(OSSL_LIB_CTX *defctx)
421 if (defctx == &default_context_int)
424 return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
428 OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
430 OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
432 if (ctx != NULL && !context_init(ctx)) {
440 OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
441 const OSSL_DISPATCH *in)
443 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
448 if (!ossl_bio_init_core(ctx, in)) {
449 OSSL_LIB_CTX_free(ctx);
456 OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
457 const OSSL_DISPATCH *in)
459 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
464 if (!ossl_provider_init_as_child(ctx, handle, in)) {
465 OSSL_LIB_CTX_free(ctx);
473 int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
475 return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
479 void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
481 if (ossl_lib_ctx_is_default(ctx))
486 ossl_provider_deinit_child(ctx);
493 OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
495 if (!RUN_ONCE(&default_context_init, default_context_do_init))
498 return &default_context_int;
501 OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
503 OSSL_LIB_CTX *current_defctx;
505 if ((current_defctx = get_default_context()) != NULL) {
507 set_default_context(libctx);
508 return current_defctx;
514 void ossl_release_default_drbg_ctx(void)
516 /* early release of the DRBG in global default libctx */
517 if (default_context_int.drbg != NULL) {
518 ossl_rand_ctx_free(default_context_int.drbg);
519 default_context_int.drbg = NULL;
524 OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
528 return get_default_context();
533 int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
536 if (ctx == NULL || ctx == get_default_context())
542 int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
545 if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
551 void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index)
555 ctx = ossl_lib_ctx_get_concrete(ctx);
560 case OSSL_LIB_CTX_PROPERTY_STRING_INDEX:
561 return ctx->property_string_data;
562 case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX:
563 return ctx->evp_method_store;
564 case OSSL_LIB_CTX_PROVIDER_STORE_INDEX:
565 return ctx->provider_store;
566 case OSSL_LIB_CTX_NAMEMAP_INDEX:
568 case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX:
569 return ctx->property_defns;
570 case OSSL_LIB_CTX_GLOBAL_PROPERTIES:
571 return ctx->global_properties;
572 case OSSL_LIB_CTX_DRBG_INDEX:
574 case OSSL_LIB_CTX_DRBG_NONCE_INDEX:
575 return ctx->drbg_nonce;
577 case OSSL_LIB_CTX_PROVIDER_CONF_INDEX:
578 return ctx->provider_conf;
579 case OSSL_LIB_CTX_BIO_CORE_INDEX:
580 return ctx->bio_core;
581 case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX:
582 return ctx->child_provider;
583 case OSSL_LIB_CTX_DECODER_STORE_INDEX:
584 return ctx->decoder_store;
585 case OSSL_LIB_CTX_DECODER_CACHE_INDEX:
586 return ctx->decoder_cache;
587 case OSSL_LIB_CTX_ENCODER_STORE_INDEX:
588 return ctx->encoder_store;
589 case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX:
590 return ctx->store_loader_store;
591 case OSSL_LIB_CTX_SELF_TEST_CB_INDEX:
592 return ctx->self_test_cb;
594 #ifndef OPENSSL_NO_THREAD_POOL
595 case OSSL_LIB_CTX_THREAD_INDEX:
599 case OSSL_LIB_CTX_RAND_CRNGT_INDEX: {
602 * rand_crngt must be lazily initialized because it calls into
603 * libctx, so must not be called from context_init, else a deadlock
606 * We use a separate lock because code called by the instantiation
607 * of rand_crngt is liable to try and take the libctx lock.
609 if (CRYPTO_THREAD_read_lock(ctx->rand_crngt_lock) != 1)
612 if (ctx->rand_crngt == NULL) {
613 CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
615 if (CRYPTO_THREAD_write_lock(ctx->rand_crngt_lock) != 1)
618 if (ctx->rand_crngt == NULL)
619 ctx->rand_crngt = ossl_rand_crng_ctx_new(ctx);
624 CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
630 case OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX:
631 return ctx->thread_event_handler;
633 case OSSL_LIB_CTX_FIPS_PROV_INDEX:
634 return ctx->fips_prov;
642 OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
644 ctx = ossl_lib_ctx_get_concrete(ctx);
650 const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
653 return "FIPS internal library context";
655 if (ossl_lib_ctx_is_global_default(libctx))
656 return "Global default library context";
657 if (ossl_lib_ctx_is_default(libctx))
658 return "Thread-local default library context";
659 return "Non-default library context";
663 CRYPTO_THREAD_LOCAL *ossl_lib_ctx_get_rcukey(OSSL_LIB_CTX *libctx)
665 libctx = ossl_lib_ctx_get_concrete(libctx);
668 return &libctx->rcu_local_key;
671 int OSSL_LIB_CTX_get_conf_diagnostics(OSSL_LIB_CTX *libctx)
673 libctx = ossl_lib_ctx_get_concrete(libctx);
676 return libctx->conf_diagnostics;
679 void OSSL_LIB_CTX_set_conf_diagnostics(OSSL_LIB_CTX *libctx, int value)
681 libctx = ossl_lib_ctx_get_concrete(libctx);
684 libctx->conf_diagnostics = value;