/src/openssl/crypto/context.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include "crypto/cryptlib.h" |
11 | | #include <openssl/conf.h> |
12 | | #include <openssl/trace.h> |
13 | | #include "internal/thread_once.h" |
14 | | #include "internal/property.h" |
15 | | #include "internal/cryptlib.h" |
16 | | #include "internal/core.h" |
17 | | #include "internal/bio.h" |
18 | | #include "internal/provider.h" |
19 | | #include "internal/conf.h" |
20 | | #include "crypto/decoder.h" |
21 | | #include "crypto/context.h" |
22 | | |
23 | | struct ossl_lib_ctx_st { |
24 | | CRYPTO_RWLOCK *lock; |
25 | | OSSL_EX_DATA_GLOBAL global; |
26 | | CONF_IMODULE *ssl_imod; |
27 | | void *property_string_data; |
28 | | void *evp_method_store; |
29 | | void *provider_store; |
30 | | void *namemap; |
31 | | void *property_defns; |
32 | | void *global_properties; |
33 | | void *drbg; |
34 | | void *drbg_nonce; |
35 | | #ifndef FIPS_MODULE |
36 | | void *provider_conf; |
37 | | void *bio_core; |
38 | | void *child_provider; |
39 | | OSSL_METHOD_STORE *decoder_store; |
40 | | void *decoder_cache; |
41 | | OSSL_METHOD_STORE *encoder_store; |
42 | | OSSL_METHOD_STORE *store_loader_store; |
43 | | void *self_test_cb; |
44 | | void *indicator_cb; |
45 | | #endif |
46 | | #if defined(OPENSSL_THREADS) |
47 | | void *threads; |
48 | | #endif |
49 | | #ifdef FIPS_MODULE |
50 | | void *fips_prov; |
51 | | #endif |
52 | | STACK_OF(SSL_COMP) *comp_methods; |
53 | | |
54 | | int ischild; |
55 | | int conf_diagnostics; |
56 | | }; |
57 | | |
58 | | int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx) |
59 | 1.57k | { |
60 | 1.57k | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
61 | 0 | return 0; |
62 | 1.57k | return CRYPTO_THREAD_write_lock(ctx->lock); |
63 | 1.57k | } |
64 | | |
65 | | int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx) |
66 | 23.9k | { |
67 | 23.9k | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
68 | 0 | return 0; |
69 | 23.9k | return CRYPTO_THREAD_read_lock(ctx->lock); |
70 | 23.9k | } |
71 | | |
72 | | int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx) |
73 | 25.5k | { |
74 | 25.5k | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
75 | 0 | return 0; |
76 | 25.5k | return CRYPTO_THREAD_unlock(ctx->lock); |
77 | 25.5k | } |
78 | | |
79 | | int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx) |
80 | 0 | { |
81 | 0 | ctx = ossl_lib_ctx_get_concrete(ctx); |
82 | |
|
83 | 0 | if (ctx == NULL) |
84 | 0 | return 0; |
85 | 0 | return ctx->ischild; |
86 | 0 | } |
87 | | |
88 | | int ossl_lib_ctx_attach_ssl_conf_imodule(OSSL_LIB_CTX *ctx, CONF_IMODULE *md) |
89 | 0 | { |
90 | 0 | if (ctx == NULL || md == NULL || ctx->ssl_imod != NULL) |
91 | 0 | return 0; |
92 | | |
93 | 0 | ctx->ssl_imod = md; |
94 | 0 | md->libctx = ctx; |
95 | 0 | return 1; |
96 | 0 | } |
97 | | |
98 | | int ossl_lib_ctx_detach_ssl_conf_imodule(OSSL_LIB_CTX *ctx, CONF_IMODULE *md) |
99 | 0 | { |
100 | 0 | if (ctx != NULL && md != NULL) |
101 | 0 | return 0; |
102 | | |
103 | 0 | if (ctx != NULL && ctx->ssl_imod) { |
104 | 0 | ctx->ssl_imod->libctx = NULL; |
105 | 0 | ctx->ssl_imod = NULL; |
106 | 0 | } |
107 | |
|
108 | 0 | if (md != NULL && md->libctx) { |
109 | 0 | md->libctx->ssl_imod = NULL; |
110 | 0 | md->libctx = NULL; |
111 | 0 | } |
112 | |
|
113 | 0 | return 1; |
114 | 0 | } |
115 | | |
116 | | static void context_deinit_objs(OSSL_LIB_CTX *ctx); |
117 | | |
118 | | static int context_init(OSSL_LIB_CTX *ctx) |
119 | 159 | { |
120 | 159 | int exdata_done = 0; |
121 | | |
122 | 159 | ctx->lock = CRYPTO_THREAD_lock_new(); |
123 | 159 | if (ctx->lock == NULL) |
124 | 0 | goto err; |
125 | | |
126 | | /* Initialize ex_data. */ |
127 | 159 | if (!ossl_do_ex_data_init(ctx)) |
128 | 0 | goto err; |
129 | 159 | exdata_done = 1; |
130 | | |
131 | | /* P2. We want evp_method_store to be cleaned up before the provider store */ |
132 | 159 | ctx->evp_method_store = ossl_method_store_new(ctx); |
133 | 159 | if (ctx->evp_method_store == NULL) |
134 | 0 | goto err; |
135 | 159 | OSSL_TRACE1(QUERY, "context_init: allocating store %p\n", ctx->evp_method_store); |
136 | | |
137 | 159 | #ifndef FIPS_MODULE |
138 | | /* P2. Must be freed before the provider store is freed */ |
139 | 159 | ctx->provider_conf = ossl_prov_conf_ctx_new(ctx); |
140 | 159 | if (ctx->provider_conf == NULL) |
141 | 0 | goto err; |
142 | 159 | #endif |
143 | | |
144 | | /* P2. */ |
145 | 159 | ctx->drbg = ossl_rand_ctx_new(ctx); |
146 | 159 | if (ctx->drbg == NULL) |
147 | 0 | goto err; |
148 | | |
149 | 159 | #ifndef FIPS_MODULE |
150 | | /* |
151 | | * P2. We want decoder_store/decoder_cache to be cleaned up before the |
152 | | * provider store |
153 | | */ |
154 | 159 | ctx->decoder_store = ossl_method_store_new(ctx); |
155 | 159 | if (ctx->decoder_store == NULL) |
156 | 0 | goto err; |
157 | 159 | ctx->decoder_cache = ossl_decoder_cache_new(ctx); |
158 | 159 | if (ctx->decoder_cache == NULL) |
159 | 0 | goto err; |
160 | | |
161 | | /* P2. We want encoder_store to be cleaned up before the provider store */ |
162 | 159 | ctx->encoder_store = ossl_method_store_new(ctx); |
163 | 159 | if (ctx->encoder_store == NULL) |
164 | 0 | goto err; |
165 | | |
166 | | /* P2. We want loader_store to be cleaned up before the provider store */ |
167 | 159 | ctx->store_loader_store = ossl_method_store_new(ctx); |
168 | 159 | if (ctx->store_loader_store == NULL) |
169 | 0 | goto err; |
170 | 159 | #endif |
171 | | |
172 | | /* P1. Needs to be freed before the child provider data is freed */ |
173 | 159 | ctx->provider_store = ossl_provider_store_new(ctx); |
174 | 159 | if (ctx->provider_store == NULL) |
175 | 0 | goto err; |
176 | | |
177 | | /* Default priority. */ |
178 | 159 | ctx->property_string_data = ossl_property_string_data_new(ctx); |
179 | 159 | if (ctx->property_string_data == NULL) |
180 | 0 | goto err; |
181 | | |
182 | 159 | ctx->namemap = ossl_stored_namemap_new(ctx); |
183 | 159 | if (ctx->namemap == NULL) |
184 | 0 | goto err; |
185 | | |
186 | 159 | ctx->property_defns = ossl_property_defns_new(ctx); |
187 | 159 | if (ctx->property_defns == NULL) |
188 | 0 | goto err; |
189 | | |
190 | 159 | ctx->global_properties = ossl_ctx_global_properties_new(ctx); |
191 | 159 | if (ctx->global_properties == NULL) |
192 | 0 | goto err; |
193 | | |
194 | 159 | #ifndef FIPS_MODULE |
195 | 159 | ctx->bio_core = ossl_bio_core_globals_new(ctx); |
196 | 159 | if (ctx->bio_core == NULL) |
197 | 0 | goto err; |
198 | 159 | #endif |
199 | | |
200 | 159 | ctx->drbg_nonce = ossl_prov_drbg_nonce_ctx_new(ctx); |
201 | 159 | if (ctx->drbg_nonce == NULL) |
202 | 0 | goto err; |
203 | | |
204 | 159 | #ifndef FIPS_MODULE |
205 | 159 | ctx->self_test_cb = ossl_self_test_set_callback_new(ctx); |
206 | 159 | if (ctx->self_test_cb == NULL) |
207 | 0 | goto err; |
208 | 159 | ctx->indicator_cb = ossl_indicator_set_callback_new(ctx); |
209 | 159 | if (ctx->indicator_cb == NULL) |
210 | 0 | goto err; |
211 | 159 | #endif |
212 | | |
213 | | #ifdef FIPS_MODULE |
214 | | if (!ossl_thread_event_ctx_new(ctx)) |
215 | | goto err; |
216 | | |
217 | | ctx->fips_prov = ossl_fips_prov_ossl_ctx_new(ctx); |
218 | | if (ctx->fips_prov == NULL) |
219 | | goto err; |
220 | | #endif |
221 | | |
222 | 159 | #ifndef OPENSSL_NO_THREAD_POOL |
223 | 159 | ctx->threads = ossl_threads_ctx_new(ctx); |
224 | 159 | if (ctx->threads == NULL) |
225 | 0 | goto err; |
226 | 159 | #endif |
227 | | |
228 | | /* Low priority. */ |
229 | 159 | #ifndef FIPS_MODULE |
230 | 159 | ctx->child_provider = ossl_child_prov_ctx_new(ctx); |
231 | 159 | if (ctx->child_provider == NULL) |
232 | 0 | goto err; |
233 | 159 | #endif |
234 | | |
235 | | /* Everything depends on properties, so we also pre-initialise that */ |
236 | 159 | if (!ossl_property_parse_init(ctx)) |
237 | 0 | goto err; |
238 | | |
239 | 159 | #ifndef FIPS_MODULE |
240 | 159 | ctx->comp_methods = ossl_load_builtin_compressions(); |
241 | 159 | #endif |
242 | | |
243 | 159 | return 1; |
244 | | |
245 | 0 | err: |
246 | 0 | context_deinit_objs(ctx); |
247 | |
|
248 | 0 | if (exdata_done) |
249 | 0 | ossl_crypto_cleanup_all_ex_data_int(ctx); |
250 | |
|
251 | 0 | CRYPTO_THREAD_lock_free(ctx->lock); |
252 | 0 | memset(ctx, '\0', sizeof(*ctx)); |
253 | 0 | return 0; |
254 | 159 | } |
255 | | |
256 | | static void context_deinit_objs(OSSL_LIB_CTX *ctx) |
257 | 154 | { |
258 | | /* P2. We want evp_method_store to be cleaned up before the provider store */ |
259 | 154 | if (ctx->evp_method_store != NULL) { |
260 | 154 | ossl_method_store_free(ctx->evp_method_store); |
261 | 154 | ctx->evp_method_store = NULL; |
262 | 154 | } |
263 | | |
264 | | /* P2. */ |
265 | 154 | if (ctx->drbg != NULL) { |
266 | 110 | ossl_rand_ctx_free(ctx->drbg); |
267 | 110 | ctx->drbg = NULL; |
268 | 110 | } |
269 | | |
270 | 154 | #ifndef FIPS_MODULE |
271 | | /* P2. */ |
272 | 154 | if (ctx->provider_conf != NULL) { |
273 | 154 | ossl_prov_conf_ctx_free(ctx->provider_conf); |
274 | 154 | ctx->provider_conf = NULL; |
275 | 154 | } |
276 | | |
277 | | /* |
278 | | * P2. We want decoder_store/decoder_cache to be cleaned up before the |
279 | | * provider store |
280 | | */ |
281 | 154 | if (ctx->decoder_store != NULL) { |
282 | 154 | ossl_method_store_free(ctx->decoder_store); |
283 | 154 | ctx->decoder_store = NULL; |
284 | 154 | } |
285 | 154 | if (ctx->decoder_cache != NULL) { |
286 | 154 | ossl_decoder_cache_free(ctx->decoder_cache); |
287 | 154 | ctx->decoder_cache = NULL; |
288 | 154 | } |
289 | | |
290 | | /* P2. We want encoder_store to be cleaned up before the provider store */ |
291 | 154 | if (ctx->encoder_store != NULL) { |
292 | 154 | ossl_method_store_free(ctx->encoder_store); |
293 | 154 | ctx->encoder_store = NULL; |
294 | 154 | } |
295 | | |
296 | | /* P2. We want loader_store to be cleaned up before the provider store */ |
297 | 154 | if (ctx->store_loader_store != NULL) { |
298 | 154 | ossl_method_store_free(ctx->store_loader_store); |
299 | 154 | ctx->store_loader_store = NULL; |
300 | 154 | } |
301 | 154 | #endif |
302 | | |
303 | | /* P1. Needs to be freed before the child provider data is freed */ |
304 | 154 | if (ctx->provider_store != NULL) { |
305 | 154 | ossl_provider_store_free(ctx->provider_store); |
306 | 154 | ctx->provider_store = NULL; |
307 | 154 | } |
308 | | |
309 | | /* Default priority. */ |
310 | 154 | if (ctx->property_string_data != NULL) { |
311 | 154 | ossl_property_string_data_free(ctx->property_string_data); |
312 | 154 | ctx->property_string_data = NULL; |
313 | 154 | } |
314 | | |
315 | 154 | if (ctx->namemap != NULL) { |
316 | 154 | ossl_stored_namemap_free(ctx->namemap); |
317 | 154 | ctx->namemap = NULL; |
318 | 154 | } |
319 | | |
320 | 154 | if (ctx->property_defns != NULL) { |
321 | 154 | ossl_property_defns_free(ctx->property_defns); |
322 | 154 | ctx->property_defns = NULL; |
323 | 154 | } |
324 | | |
325 | 154 | if (ctx->global_properties != NULL) { |
326 | 154 | ossl_ctx_global_properties_free(ctx->global_properties); |
327 | 154 | ctx->global_properties = NULL; |
328 | 154 | } |
329 | | |
330 | 154 | #ifndef FIPS_MODULE |
331 | 154 | if (ctx->bio_core != NULL) { |
332 | 154 | ossl_bio_core_globals_free(ctx->bio_core); |
333 | 154 | ctx->bio_core = NULL; |
334 | 154 | } |
335 | 154 | #endif |
336 | | |
337 | 154 | if (ctx->drbg_nonce != NULL) { |
338 | 154 | ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce); |
339 | 154 | ctx->drbg_nonce = NULL; |
340 | 154 | } |
341 | | |
342 | 154 | #ifndef FIPS_MODULE |
343 | 154 | if (ctx->indicator_cb != NULL) { |
344 | 154 | ossl_indicator_set_callback_free(ctx->indicator_cb); |
345 | 154 | ctx->indicator_cb = NULL; |
346 | 154 | } |
347 | | |
348 | 154 | if (ctx->self_test_cb != NULL) { |
349 | 154 | ossl_self_test_set_callback_free(ctx->self_test_cb); |
350 | 154 | ctx->self_test_cb = NULL; |
351 | 154 | } |
352 | 154 | #endif |
353 | | |
354 | | #ifdef FIPS_MODULE |
355 | | ossl_thread_event_ctx_free(ctx); |
356 | | |
357 | | if (ctx->fips_prov != NULL) { |
358 | | ossl_fips_prov_ossl_ctx_free(ctx->fips_prov); |
359 | | ctx->fips_prov = NULL; |
360 | | } |
361 | | #endif |
362 | | |
363 | 154 | #ifndef OPENSSL_NO_THREAD_POOL |
364 | 154 | if (ctx->threads != NULL) { |
365 | 154 | ossl_threads_ctx_free(ctx->threads); |
366 | 154 | ctx->threads = NULL; |
367 | 154 | } |
368 | 154 | #endif |
369 | | |
370 | | /* Low priority. */ |
371 | 154 | #ifndef FIPS_MODULE |
372 | 154 | if (ctx->child_provider != NULL) { |
373 | 154 | ossl_child_prov_ctx_free(ctx->child_provider); |
374 | 154 | ctx->child_provider = NULL; |
375 | 154 | } |
376 | 154 | #endif |
377 | | |
378 | 154 | #ifndef FIPS_MODULE |
379 | 154 | if (ctx->comp_methods != NULL) { |
380 | 154 | ossl_free_compression_methods_int(ctx->comp_methods); |
381 | 154 | ctx->comp_methods = NULL; |
382 | 154 | } |
383 | 154 | #endif |
384 | 154 | } |
385 | | |
386 | | static int context_deinit(OSSL_LIB_CTX *ctx) |
387 | 196 | { |
388 | 196 | if (ctx == NULL) |
389 | 0 | return 1; |
390 | | |
391 | 196 | ossl_lib_ctx_detach_ssl_conf_imodule(ctx, NULL); |
392 | | |
393 | 196 | ossl_ctx_thread_stop(ctx); |
394 | | |
395 | 196 | context_deinit_objs(ctx); |
396 | | |
397 | 196 | ossl_crypto_cleanup_all_ex_data_int(ctx); |
398 | | |
399 | 196 | CRYPTO_THREAD_lock_free(ctx->lock); |
400 | 196 | ctx->lock = NULL; |
401 | 196 | return 1; |
402 | 196 | } |
403 | | |
404 | | #ifndef FIPS_MODULE |
405 | | /* The default default context */ |
406 | | static OSSL_LIB_CTX default_context_int; |
407 | | |
408 | | static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT; |
409 | | static CRYPTO_ONCE default_context_thread_key_init = CRYPTO_ONCE_STATIC_INIT; |
410 | | static CRYPTO_THREAD_LOCAL default_context_thread_local; |
411 | | static int default_context_inited = 0; |
412 | | |
413 | | DEFINE_RUN_ONCE_STATIC(default_context_do_thread_key_init) |
414 | 50 | { |
415 | 50 | if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)) |
416 | 0 | return 0; |
417 | 50 | return 1; |
418 | 50 | } |
419 | | |
420 | | DEFINE_RUN_ONCE_STATIC(default_context_do_init) |
421 | 50 | { |
422 | 50 | if (!context_init(&default_context_int)) |
423 | 0 | return 0; |
424 | | |
425 | 50 | default_context_inited = 1; |
426 | 50 | return 1; |
427 | 50 | } |
428 | | |
429 | | void ossl_lib_ctx_default_deinit(void) |
430 | 196 | { |
431 | 196 | if (!default_context_inited) |
432 | 0 | return; |
433 | 196 | context_deinit(&default_context_int); |
434 | 196 | CRYPTO_THREAD_cleanup_local(&default_context_thread_local); |
435 | 196 | default_context_inited = 0; |
436 | 196 | } |
437 | | |
438 | | static OSSL_LIB_CTX *get_thread_default_context(void) |
439 | 23.5M | { |
440 | 23.5M | if (!RUN_ONCE(&default_context_thread_key_init, default_context_do_thread_key_init)) |
441 | 0 | return NULL; |
442 | | |
443 | 23.5M | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
444 | 0 | return NULL; |
445 | | |
446 | 23.5M | return CRYPTO_THREAD_get_local(&default_context_thread_local); |
447 | 23.5M | } |
448 | | |
449 | | static OSSL_LIB_CTX *check_thread_default_context(void) |
450 | 0 | { |
451 | 0 | if (!RUN_ONCE(&default_context_thread_key_init, default_context_do_thread_key_init)) |
452 | 0 | return NULL; |
453 | | |
454 | 0 | return CRYPTO_THREAD_get_local(&default_context_thread_local); |
455 | 0 | } |
456 | | |
457 | | static OSSL_LIB_CTX *get_default_context(void) |
458 | 132M | { |
459 | 132M | OSSL_LIB_CTX *current_defctx = get_thread_default_context(); |
460 | | |
461 | 132M | if (current_defctx == NULL && default_context_inited) |
462 | 132M | current_defctx = &default_context_int; |
463 | 132M | return current_defctx; |
464 | 132M | } |
465 | | |
466 | | static OSSL_LIB_CTX *check_default_context(void) |
467 | 0 | { |
468 | 0 | OSSL_LIB_CTX *current_defctx = check_thread_default_context(); |
469 | |
|
470 | 0 | if (current_defctx == NULL && default_context_inited) |
471 | 0 | current_defctx = &default_context_int; |
472 | 0 | return current_defctx; |
473 | 0 | } |
474 | | |
475 | | static int set_default_context(OSSL_LIB_CTX *defctx) |
476 | 0 | { |
477 | 0 | if (defctx == &default_context_int) |
478 | 0 | defctx = NULL; |
479 | |
|
480 | 0 | return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx); |
481 | 0 | } |
482 | | #endif |
483 | | |
484 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new(void) |
485 | 144 | { |
486 | 144 | OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
487 | | |
488 | 144 | if (ctx != NULL && !context_init(ctx)) { |
489 | 0 | OPENSSL_free(ctx); |
490 | 0 | ctx = NULL; |
491 | 0 | } |
492 | 144 | return ctx; |
493 | 144 | } |
494 | | |
495 | | #ifndef FIPS_MODULE |
496 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle, |
497 | | const OSSL_DISPATCH *in) |
498 | 0 | { |
499 | 0 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); |
500 | |
|
501 | 0 | if (ctx == NULL) |
502 | 0 | return NULL; |
503 | | |
504 | 0 | if (!ossl_bio_init_core(ctx, in)) { |
505 | 0 | OSSL_LIB_CTX_free(ctx); |
506 | 0 | return NULL; |
507 | 0 | } |
508 | | |
509 | 0 | return ctx; |
510 | 0 | } |
511 | | |
512 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle, |
513 | | const OSSL_DISPATCH *in) |
514 | 0 | { |
515 | 0 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in); |
516 | |
|
517 | 0 | if (ctx == NULL) |
518 | 0 | return NULL; |
519 | | |
520 | 0 | if (!ossl_provider_init_as_child(ctx, handle, in)) { |
521 | 0 | OSSL_LIB_CTX_free(ctx); |
522 | 0 | return NULL; |
523 | 0 | } |
524 | 0 | ctx->ischild = 1; |
525 | |
|
526 | 0 | return ctx; |
527 | 0 | } |
528 | | |
529 | | int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file) |
530 | 0 | { |
531 | 0 | return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0; |
532 | 0 | } |
533 | | #endif |
534 | | |
535 | | void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx) |
536 | 0 | { |
537 | 0 | if (ctx == NULL || ossl_lib_ctx_is_default_nocreate(ctx)) |
538 | 0 | return; |
539 | | |
540 | 0 | #ifndef FIPS_MODULE |
541 | 0 | if (ctx->ischild) |
542 | 0 | ossl_provider_deinit_child(ctx); |
543 | 0 | #endif |
544 | 0 | context_deinit(ctx); |
545 | 0 | OPENSSL_free(ctx); |
546 | 0 | } |
547 | | |
548 | | #ifndef FIPS_MODULE |
549 | | OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void) |
550 | 27.4k | { |
551 | 27.4k | if (!RUN_ONCE(&default_context_thread_key_init, default_context_do_thread_key_init)) |
552 | 0 | return NULL; |
553 | | |
554 | 27.4k | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
555 | 0 | return NULL; |
556 | | |
557 | 27.4k | return &default_context_int; |
558 | 27.4k | } |
559 | | |
560 | | OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx) |
561 | 163k | { |
562 | 163k | OSSL_LIB_CTX *current_defctx; |
563 | | |
564 | 163k | if ((current_defctx = get_default_context()) != NULL) { |
565 | 163k | if (libctx != NULL) |
566 | 0 | set_default_context(libctx); |
567 | 163k | return current_defctx; |
568 | 163k | } |
569 | | |
570 | 0 | return NULL; |
571 | 163k | } |
572 | | |
573 | | void ossl_release_default_drbg_ctx(void) |
574 | 61 | { |
575 | | /* early release of the DRBG in global default libctx */ |
576 | 61 | if (default_context_int.drbg != NULL) { |
577 | 61 | ossl_rand_ctx_free(default_context_int.drbg); |
578 | 61 | default_context_int.drbg = NULL; |
579 | 61 | } |
580 | 61 | } |
581 | | #endif |
582 | | |
583 | | OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx) |
584 | 424M | { |
585 | 424M | #ifndef FIPS_MODULE |
586 | 424M | if (ctx == NULL) |
587 | 151M | return get_default_context(); |
588 | 273M | #endif |
589 | 273M | return ctx; |
590 | 424M | } |
591 | | |
592 | | int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx) |
593 | 4.74M | { |
594 | 4.74M | #ifndef FIPS_MODULE |
595 | 4.74M | if (ctx == NULL || ctx == get_default_context()) |
596 | 4.72M | return 1; |
597 | 16.4k | #endif |
598 | 16.4k | return 0; |
599 | 4.74M | } |
600 | | |
601 | | int ossl_lib_ctx_is_default_nocreate(OSSL_LIB_CTX *ctx) |
602 | 0 | { |
603 | 0 | #ifndef FIPS_MODULE |
604 | 0 | if (ctx == NULL || ctx == check_default_context()) |
605 | 0 | return 1; |
606 | 0 | #endif |
607 | 0 | return 0; |
608 | 0 | } |
609 | | |
610 | | int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx) |
611 | 2.63M | { |
612 | 2.63M | #ifndef FIPS_MODULE |
613 | 2.63M | if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int) |
614 | 2.62M | return 1; |
615 | 2.23k | #endif |
616 | 2.23k | return 0; |
617 | 2.63M | } |
618 | | |
619 | | void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index) |
620 | 15.4M | { |
621 | 15.4M | ctx = ossl_lib_ctx_get_concrete(ctx); |
622 | 15.4M | if (ctx == NULL) |
623 | 0 | return NULL; |
624 | | |
625 | 15.4M | switch (index) { |
626 | 505k | case OSSL_LIB_CTX_PROPERTY_STRING_INDEX: |
627 | 505k | return ctx->property_string_data; |
628 | 6.00M | case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX: |
629 | 6.00M | return ctx->evp_method_store; |
630 | 534k | case OSSL_LIB_CTX_PROVIDER_STORE_INDEX: |
631 | 534k | return ctx->provider_store; |
632 | 7.49M | case OSSL_LIB_CTX_NAMEMAP_INDEX: |
633 | 7.49M | return ctx->namemap; |
634 | 6.55k | case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX: |
635 | 6.55k | return ctx->property_defns; |
636 | 5.74k | case OSSL_LIB_CTX_GLOBAL_PROPERTIES: |
637 | 5.74k | return ctx->global_properties; |
638 | 418k | case OSSL_LIB_CTX_DRBG_INDEX: |
639 | 418k | return ctx->drbg; |
640 | 175 | case OSSL_LIB_CTX_DRBG_NONCE_INDEX: |
641 | 175 | return ctx->drbg_nonce; |
642 | 0 | #ifndef FIPS_MODULE |
643 | 0 | case OSSL_LIB_CTX_PROVIDER_CONF_INDEX: |
644 | 0 | return ctx->provider_conf; |
645 | 0 | case OSSL_LIB_CTX_BIO_CORE_INDEX: |
646 | 0 | return ctx->bio_core; |
647 | 0 | case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX: |
648 | 0 | return ctx->child_provider; |
649 | 23.6k | case OSSL_LIB_CTX_DECODER_STORE_INDEX: |
650 | 23.6k | return ctx->decoder_store; |
651 | 242k | case OSSL_LIB_CTX_DECODER_CACHE_INDEX: |
652 | 242k | return ctx->decoder_cache; |
653 | 173k | case OSSL_LIB_CTX_ENCODER_STORE_INDEX: |
654 | 173k | return ctx->encoder_store; |
655 | 26 | case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX: |
656 | 26 | return ctx->store_loader_store; |
657 | 0 | case OSSL_LIB_CTX_SELF_TEST_CB_INDEX: |
658 | 0 | return ctx->self_test_cb; |
659 | 0 | case OSSL_LIB_CTX_INDICATOR_CB_INDEX: |
660 | 0 | return ctx->indicator_cb; |
661 | 0 | #endif |
662 | 0 | #ifndef OPENSSL_NO_THREAD_POOL |
663 | 10 | case OSSL_LIB_CTX_THREAD_INDEX: |
664 | 10 | return ctx->threads; |
665 | 0 | #endif |
666 | | |
667 | | #ifdef FIPS_MODULE |
668 | | case OSSL_LIB_CTX_FIPS_PROV_INDEX: |
669 | | return ctx->fips_prov; |
670 | | #endif |
671 | | |
672 | 23.4k | case OSSL_LIB_CTX_COMP_METHODS: |
673 | 23.4k | return (void *)&ctx->comp_methods; |
674 | | |
675 | 54.8k | case OSSL_LIB_CTX_SSL_CONF_IMODULE: |
676 | 54.8k | return (void *)ctx->ssl_imod; |
677 | | |
678 | 0 | default: |
679 | 0 | return NULL; |
680 | 15.4M | } |
681 | 15.4M | } |
682 | | |
683 | | void *OSSL_LIB_CTX_get_data(OSSL_LIB_CTX *ctx, int index) |
684 | 157k | { |
685 | 157k | return ossl_lib_ctx_get_data(ctx, index); |
686 | 157k | } |
687 | | |
688 | | OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx) |
689 | 75.4M | { |
690 | 75.4M | ctx = ossl_lib_ctx_get_concrete(ctx); |
691 | 75.4M | if (ctx == NULL) |
692 | 0 | return NULL; |
693 | 75.4M | return &ctx->global; |
694 | 75.4M | } |
695 | | |
696 | | const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx) |
697 | 2.63M | { |
698 | | #ifdef FIPS_MODULE |
699 | | return "FIPS internal library context"; |
700 | | #else |
701 | 2.63M | if (ossl_lib_ctx_is_global_default(libctx)) |
702 | 2.62M | return "Global default library context"; |
703 | 2.23k | if (ossl_lib_ctx_is_default(libctx)) |
704 | 0 | return "Thread-local default library context"; |
705 | 2.23k | return "Non-default library context"; |
706 | 2.23k | #endif |
707 | 2.23k | } |
708 | | |
709 | | int OSSL_LIB_CTX_get_conf_diagnostics(OSSL_LIB_CTX *libctx) |
710 | 144 | { |
711 | 144 | libctx = ossl_lib_ctx_get_concrete(libctx); |
712 | 144 | if (libctx == NULL) |
713 | 0 | return 0; |
714 | 144 | return libctx->conf_diagnostics; |
715 | 144 | } |
716 | | |
717 | | void OSSL_LIB_CTX_set_conf_diagnostics(OSSL_LIB_CTX *libctx, int value) |
718 | 0 | { |
719 | 0 | libctx = ossl_lib_ctx_get_concrete(libctx); |
720 | 0 | if (libctx == NULL) |
721 | 0 | return; |
722 | 0 | libctx->conf_diagnostics = value; |
723 | 0 | } |