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/context.h"
19 struct ossl_lib_ctx_st {
20 CRYPTO_RWLOCK *lock, *rand_crngt_lock;
21 OSSL_EX_DATA_GLOBAL global;
23 void *property_string_data;
24 void *evp_method_store;
28 void *global_properties;
35 OSSL_METHOD_STORE *decoder_store;
36 OSSL_METHOD_STORE *encoder_store;
37 OSSL_METHOD_STORE *store_loader_store;
40 #if defined(OPENSSL_THREADS)
45 void *thread_event_handler;
49 unsigned int ischild:1;
52 int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx)
54 return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
57 int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx)
59 return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
62 int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx)
64 return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock);
67 int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx)
69 ctx = ossl_lib_ctx_get_concrete(ctx);
76 static void context_deinit_objs(OSSL_LIB_CTX *ctx);
78 static int context_init(OSSL_LIB_CTX *ctx)
82 ctx->lock = CRYPTO_THREAD_lock_new();
83 if (ctx->lock == NULL)
86 ctx->rand_crngt_lock = CRYPTO_THREAD_lock_new();
87 if (ctx->rand_crngt_lock == NULL)
90 /* Initialize ex_data. */
91 if (!ossl_do_ex_data_init(ctx))
95 /* P2. We want evp_method_store to be cleaned up before the provider store */
96 ctx->evp_method_store = ossl_method_store_new(ctx);
97 if (ctx->evp_method_store == NULL)
101 /* P2. Must be freed before the provider store is freed */
102 ctx->provider_conf = ossl_prov_conf_ctx_new(ctx);
103 if (ctx->provider_conf == NULL)
108 ctx->drbg = ossl_rand_ctx_new(ctx);
109 if (ctx->drbg == NULL)
113 /* P2. We want decoder_store to be cleaned up before the provider store */
114 ctx->decoder_store = ossl_method_store_new(ctx);
115 if (ctx->decoder_store == NULL)
118 /* P2. We want encoder_store to be cleaned up before the provider store */
119 ctx->encoder_store = ossl_method_store_new(ctx);
120 if (ctx->encoder_store == NULL)
123 /* P2. We want loader_store to be cleaned up before the provider store */
124 ctx->store_loader_store = ossl_method_store_new(ctx);
125 if (ctx->store_loader_store == NULL)
129 /* P1. Needs to be freed before the child provider data is freed */
130 ctx->provider_store = ossl_provider_store_new(ctx);
131 if (ctx->provider_store == NULL)
134 /* Default priority. */
135 ctx->property_string_data = ossl_property_string_data_new(ctx);
136 if (ctx->property_string_data == NULL)
139 ctx->namemap = ossl_stored_namemap_new(ctx);
140 if (ctx->namemap == NULL)
143 ctx->property_defns = ossl_property_defns_new(ctx);
144 if (ctx->property_defns == NULL)
147 ctx->global_properties = ossl_ctx_global_properties_new(ctx);
148 if (ctx->global_properties == NULL)
152 ctx->bio_core = ossl_bio_core_globals_new(ctx);
153 if (ctx->bio_core == NULL)
157 ctx->drbg_nonce = ossl_prov_drbg_nonce_ctx_new(ctx);
158 if (ctx->drbg_nonce == NULL)
162 ctx->self_test_cb = ossl_self_test_set_callback_new(ctx);
163 if (ctx->self_test_cb == NULL)
168 ctx->thread_event_handler = ossl_thread_event_ctx_new(ctx);
169 if (ctx->thread_event_handler == NULL)
172 ctx->fips_prov = ossl_fips_prov_ossl_ctx_new(ctx);
173 if (ctx->fips_prov == NULL)
177 #if defined(OPENSSL_THREADS)
178 ctx->threads = ossl_threads_ctx_new(ctx);
179 if (ctx->threads == NULL)
185 ctx->child_provider = ossl_child_prov_ctx_new(ctx);
186 if (ctx->child_provider == NULL)
190 /* Everything depends on properties, so we also pre-initialise that */
191 if (!ossl_property_parse_init(ctx))
197 context_deinit_objs(ctx);
200 ossl_crypto_cleanup_all_ex_data_int(ctx);
202 CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
203 CRYPTO_THREAD_lock_free(ctx->lock);
204 memset(ctx, '\0', sizeof(*ctx));
208 static void context_deinit_objs(OSSL_LIB_CTX *ctx)
210 /* P2. We want evp_method_store to be cleaned up before the provider store */
211 if (ctx->evp_method_store != NULL) {
212 ossl_method_store_free(ctx->evp_method_store);
213 ctx->evp_method_store = NULL;
217 if (ctx->drbg != NULL) {
218 ossl_rand_ctx_free(ctx->drbg);
224 if (ctx->provider_conf != NULL) {
225 ossl_prov_conf_ctx_free(ctx->provider_conf);
226 ctx->provider_conf = NULL;
229 /* P2. We want decoder_store to be cleaned up before the provider store */
230 if (ctx->decoder_store != NULL) {
231 ossl_method_store_free(ctx->decoder_store);
232 ctx->decoder_store = NULL;
235 /* P2. We want encoder_store to be cleaned up before the provider store */
236 if (ctx->encoder_store != NULL) {
237 ossl_method_store_free(ctx->encoder_store);
238 ctx->encoder_store = NULL;
241 /* P2. We want loader_store to be cleaned up before the provider store */
242 if (ctx->store_loader_store != NULL) {
243 ossl_method_store_free(ctx->store_loader_store);
244 ctx->store_loader_store = NULL;
248 /* P1. Needs to be freed before the child provider data is freed */
249 if (ctx->provider_store != NULL) {
250 ossl_provider_store_free(ctx->provider_store);
251 ctx->provider_store = NULL;
254 /* Default priority. */
255 if (ctx->property_string_data != NULL) {
256 ossl_property_string_data_free(ctx->property_string_data);
257 ctx->property_string_data = NULL;
260 if (ctx->namemap != NULL) {
261 ossl_stored_namemap_free(ctx->namemap);
265 if (ctx->property_defns != NULL) {
266 ossl_property_defns_free(ctx->property_defns);
267 ctx->property_defns = NULL;
270 if (ctx->global_properties != NULL) {
271 ossl_ctx_global_properties_free(ctx->global_properties);
272 ctx->global_properties = NULL;
276 if (ctx->bio_core != NULL) {
277 ossl_bio_core_globals_free(ctx->bio_core);
278 ctx->bio_core = NULL;
282 if (ctx->drbg_nonce != NULL) {
283 ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce);
284 ctx->drbg_nonce = NULL;
288 if (ctx->self_test_cb != NULL) {
289 ossl_self_test_set_callback_free(ctx->self_test_cb);
290 ctx->self_test_cb = NULL;
294 if (ctx->rand_crngt != NULL) {
295 ossl_rand_crng_ctx_free(ctx->rand_crngt);
296 ctx->rand_crngt = NULL;
300 if (ctx->thread_event_handler != NULL) {
301 ossl_thread_event_ctx_free(ctx->thread_event_handler);
302 ctx->thread_event_handler = NULL;
305 if (ctx->fips_prov != NULL) {
306 ossl_fips_prov_ossl_ctx_free(ctx->fips_prov);
307 ctx->fips_prov = NULL;
311 #if defined(OPENSSL_THREADS)
312 if (ctx->threads != NULL) {
313 ossl_threads_ctx_free(ctx->threads);
320 if (ctx->child_provider != NULL) {
321 ossl_child_prov_ctx_free(ctx->child_provider);
322 ctx->child_provider = NULL;
327 static int context_deinit(OSSL_LIB_CTX *ctx)
332 ossl_ctx_thread_stop(ctx);
334 context_deinit_objs(ctx);
336 ossl_crypto_cleanup_all_ex_data_int(ctx);
338 CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
339 CRYPTO_THREAD_lock_free(ctx->lock);
340 ctx->rand_crngt_lock = NULL;
346 /* The default default context */
347 static OSSL_LIB_CTX default_context_int;
349 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
350 static CRYPTO_THREAD_LOCAL default_context_thread_local;
352 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
354 return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
355 && context_init(&default_context_int);
358 void ossl_lib_ctx_default_deinit(void)
360 context_deinit(&default_context_int);
361 CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
364 static OSSL_LIB_CTX *get_thread_default_context(void)
366 if (!RUN_ONCE(&default_context_init, default_context_do_init))
369 return CRYPTO_THREAD_get_local(&default_context_thread_local);
372 static OSSL_LIB_CTX *get_default_context(void)
374 OSSL_LIB_CTX *current_defctx = get_thread_default_context();
376 if (current_defctx == NULL)
377 current_defctx = &default_context_int;
378 return current_defctx;
381 static int set_default_context(OSSL_LIB_CTX *defctx)
383 if (defctx == &default_context_int)
386 return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
390 OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
392 OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
394 if (ctx != NULL && !context_init(ctx)) {
402 OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
403 const OSSL_DISPATCH *in)
405 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
410 if (!ossl_bio_init_core(ctx, in)) {
411 OSSL_LIB_CTX_free(ctx);
418 OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
419 const OSSL_DISPATCH *in)
421 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
426 if (!ossl_provider_init_as_child(ctx, handle, in)) {
427 OSSL_LIB_CTX_free(ctx);
435 int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
437 return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
441 void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
443 if (ossl_lib_ctx_is_default(ctx))
448 ossl_provider_deinit_child(ctx);
455 OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
457 if (!RUN_ONCE(&default_context_init, default_context_do_init))
460 return &default_context_int;
463 OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
465 OSSL_LIB_CTX *current_defctx;
467 if ((current_defctx = get_default_context()) != NULL) {
469 set_default_context(libctx);
470 return current_defctx;
476 void ossl_release_default_drbg_ctx(void)
478 /* early release of the DRBG in global default libctx */
479 if (default_context_int.drbg != NULL) {
480 ossl_rand_ctx_free(default_context_int.drbg);
481 default_context_int.drbg = NULL;
486 OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
490 return get_default_context();
495 int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
498 if (ctx == NULL || ctx == get_default_context())
504 int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
507 if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
513 void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index)
517 ctx = ossl_lib_ctx_get_concrete(ctx);
522 case OSSL_LIB_CTX_PROPERTY_STRING_INDEX:
523 return ctx->property_string_data;
524 case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX:
525 return ctx->evp_method_store;
526 case OSSL_LIB_CTX_PROVIDER_STORE_INDEX:
527 return ctx->provider_store;
528 case OSSL_LIB_CTX_NAMEMAP_INDEX:
530 case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX:
531 return ctx->property_defns;
532 case OSSL_LIB_CTX_GLOBAL_PROPERTIES:
533 return ctx->global_properties;
534 case OSSL_LIB_CTX_DRBG_INDEX:
536 case OSSL_LIB_CTX_DRBG_NONCE_INDEX:
537 return ctx->drbg_nonce;
539 case OSSL_LIB_CTX_PROVIDER_CONF_INDEX:
540 return ctx->provider_conf;
541 case OSSL_LIB_CTX_BIO_CORE_INDEX:
542 return ctx->bio_core;
543 case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX:
544 return ctx->child_provider;
545 case OSSL_LIB_CTX_DECODER_STORE_INDEX:
546 return ctx->decoder_store;
547 case OSSL_LIB_CTX_ENCODER_STORE_INDEX:
548 return ctx->encoder_store;
549 case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX:
550 return ctx->store_loader_store;
551 case OSSL_LIB_CTX_SELF_TEST_CB_INDEX:
552 return ctx->self_test_cb;
554 #if defined(OPENSSL_THREADS)
555 case OSSL_LIB_CTX_THREAD_INDEX:
559 case OSSL_LIB_CTX_RAND_CRNGT_INDEX: {
562 * rand_crngt must be lazily initialized because it calls into
563 * libctx, so must not be called from context_init, else a deadlock
566 * We use a separate lock because code called by the instantiation
567 * of rand_crngt is liable to try and take the libctx lock.
569 if (CRYPTO_THREAD_read_lock(ctx->rand_crngt_lock) != 1)
572 if (ctx->rand_crngt == NULL) {
573 CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
575 if (CRYPTO_THREAD_write_lock(ctx->rand_crngt_lock) != 1)
578 if (ctx->rand_crngt == NULL)
579 ctx->rand_crngt = ossl_rand_crng_ctx_new(ctx);
584 CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
590 case OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX:
591 return ctx->thread_event_handler;
593 case OSSL_LIB_CTX_FIPS_PROV_INDEX:
594 return ctx->fips_prov;
602 OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
604 ctx = ossl_lib_ctx_get_concrete(ctx);
610 const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
613 return "FIPS internal library context";
615 if (ossl_lib_ctx_is_global_default(libctx))
616 return "Global default library context";
617 if (ossl_lib_ctx_is_default(libctx))
618 return "Thread-local default library context";
619 return "Non-default library context";