/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 | 7 | { |
60 | 7 | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
61 | 0 | return 0; |
62 | 7 | return CRYPTO_THREAD_write_lock(ctx->lock); |
63 | 7 | } |
64 | | |
65 | | int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx) |
66 | 278 | { |
67 | 278 | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
68 | 0 | return 0; |
69 | 278 | return CRYPTO_THREAD_read_lock(ctx->lock); |
70 | 278 | } |
71 | | |
72 | | int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx) |
73 | 285 | { |
74 | 285 | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
75 | 0 | return 0; |
76 | 285 | return CRYPTO_THREAD_unlock(ctx->lock); |
77 | 285 | } |
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 | 3 | { |
100 | 3 | if (ctx != NULL && md != NULL) |
101 | 0 | return 0; |
102 | | |
103 | 3 | if (ctx != NULL && ctx->ssl_imod) { |
104 | 0 | ctx->ssl_imod->libctx = NULL; |
105 | 0 | ctx->ssl_imod = NULL; |
106 | 0 | } |
107 | | |
108 | 3 | if (md != NULL && md->libctx) { |
109 | 0 | md->libctx->ssl_imod = NULL; |
110 | 0 | md->libctx = NULL; |
111 | 0 | } |
112 | | |
113 | 3 | return 1; |
114 | 3 | } |
115 | | |
116 | | static void context_deinit_objs(OSSL_LIB_CTX *ctx); |
117 | | |
118 | | static int context_init(OSSL_LIB_CTX *ctx) |
119 | 9 | { |
120 | 9 | int exdata_done = 0; |
121 | | |
122 | 9 | ctx->lock = CRYPTO_THREAD_lock_new(); |
123 | 9 | if (ctx->lock == NULL) |
124 | 0 | goto err; |
125 | | |
126 | | /* Initialize ex_data. */ |
127 | 9 | if (!ossl_do_ex_data_init(ctx)) |
128 | 0 | goto err; |
129 | 9 | exdata_done = 1; |
130 | | |
131 | | /* P2. We want evp_method_store to be cleaned up before the provider store */ |
132 | 9 | ctx->evp_method_store = ossl_method_store_new(ctx); |
133 | 9 | if (ctx->evp_method_store == NULL) |
134 | 0 | goto err; |
135 | 9 | OSSL_TRACE1(QUERY, "context_init: allocating store %p\n", ctx->evp_method_store); |
136 | | |
137 | 9 | #ifndef FIPS_MODULE |
138 | | /* P2. Must be freed before the provider store is freed */ |
139 | 9 | ctx->provider_conf = ossl_prov_conf_ctx_new(ctx); |
140 | 9 | if (ctx->provider_conf == NULL) |
141 | 0 | goto err; |
142 | 9 | #endif |
143 | | |
144 | | /* P2. */ |
145 | 9 | ctx->drbg = ossl_rand_ctx_new(ctx); |
146 | 9 | if (ctx->drbg == NULL) |
147 | 0 | goto err; |
148 | | |
149 | 9 | #ifndef FIPS_MODULE |
150 | | /* |
151 | | * P2. We want decoder_store/decoder_cache to be cleaned up before the |
152 | | * provider store |
153 | | */ |
154 | 9 | ctx->decoder_store = ossl_method_store_new(ctx); |
155 | 9 | if (ctx->decoder_store == NULL) |
156 | 0 | goto err; |
157 | 9 | ctx->decoder_cache = ossl_decoder_cache_new(ctx); |
158 | 9 | 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 | 9 | ctx->encoder_store = ossl_method_store_new(ctx); |
163 | 9 | 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 | 9 | ctx->store_loader_store = ossl_method_store_new(ctx); |
168 | 9 | if (ctx->store_loader_store == NULL) |
169 | 0 | goto err; |
170 | 9 | #endif |
171 | | |
172 | | /* P1. Needs to be freed before the child provider data is freed */ |
173 | 9 | ctx->provider_store = ossl_provider_store_new(ctx); |
174 | 9 | if (ctx->provider_store == NULL) |
175 | 0 | goto err; |
176 | | |
177 | | /* Default priority. */ |
178 | 9 | ctx->property_string_data = ossl_property_string_data_new(ctx); |
179 | 9 | if (ctx->property_string_data == NULL) |
180 | 0 | goto err; |
181 | | |
182 | 9 | ctx->namemap = ossl_stored_namemap_new(ctx); |
183 | 9 | if (ctx->namemap == NULL) |
184 | 0 | goto err; |
185 | | |
186 | 9 | ctx->property_defns = ossl_property_defns_new(ctx); |
187 | 9 | if (ctx->property_defns == NULL) |
188 | 0 | goto err; |
189 | | |
190 | 9 | ctx->global_properties = ossl_ctx_global_properties_new(ctx); |
191 | 9 | if (ctx->global_properties == NULL) |
192 | 0 | goto err; |
193 | | |
194 | 9 | #ifndef FIPS_MODULE |
195 | 9 | ctx->bio_core = ossl_bio_core_globals_new(ctx); |
196 | 9 | if (ctx->bio_core == NULL) |
197 | 0 | goto err; |
198 | 9 | #endif |
199 | | |
200 | 9 | ctx->drbg_nonce = ossl_prov_drbg_nonce_ctx_new(ctx); |
201 | 9 | if (ctx->drbg_nonce == NULL) |
202 | 0 | goto err; |
203 | | |
204 | 9 | #ifndef FIPS_MODULE |
205 | 9 | ctx->self_test_cb = ossl_self_test_set_callback_new(ctx); |
206 | 9 | if (ctx->self_test_cb == NULL) |
207 | 0 | goto err; |
208 | 9 | ctx->indicator_cb = ossl_indicator_set_callback_new(ctx); |
209 | 9 | if (ctx->indicator_cb == NULL) |
210 | 0 | goto err; |
211 | 9 | #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 | 9 | #ifndef OPENSSL_NO_THREAD_POOL |
223 | 9 | ctx->threads = ossl_threads_ctx_new(ctx); |
224 | 9 | if (ctx->threads == NULL) |
225 | 0 | goto err; |
226 | 9 | #endif |
227 | | |
228 | | /* Low priority. */ |
229 | 9 | #ifndef FIPS_MODULE |
230 | 9 | ctx->child_provider = ossl_child_prov_ctx_new(ctx); |
231 | 9 | if (ctx->child_provider == NULL) |
232 | 0 | goto err; |
233 | 9 | #endif |
234 | | |
235 | | /* Everything depends on properties, so we also pre-initialise that */ |
236 | 9 | if (!ossl_property_parse_init(ctx)) |
237 | 0 | goto err; |
238 | | |
239 | 9 | #ifndef FIPS_MODULE |
240 | 9 | ctx->comp_methods = ossl_load_builtin_compressions(); |
241 | 9 | #endif |
242 | | |
243 | 9 | 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 | 9 | } |
255 | | |
256 | | static void context_deinit_objs(OSSL_LIB_CTX *ctx) |
257 | 3 | { |
258 | | /* P2. We want evp_method_store to be cleaned up before the provider store */ |
259 | 3 | if (ctx->evp_method_store != NULL) { |
260 | 3 | ossl_method_store_free(ctx->evp_method_store); |
261 | 3 | ctx->evp_method_store = NULL; |
262 | 3 | } |
263 | | |
264 | | /* P2. */ |
265 | 3 | if (ctx->drbg != NULL) { |
266 | 3 | ossl_rand_ctx_free(ctx->drbg); |
267 | 3 | ctx->drbg = NULL; |
268 | 3 | } |
269 | | |
270 | 3 | #ifndef FIPS_MODULE |
271 | | /* P2. */ |
272 | 3 | if (ctx->provider_conf != NULL) { |
273 | 3 | ossl_prov_conf_ctx_free(ctx->provider_conf); |
274 | 3 | ctx->provider_conf = NULL; |
275 | 3 | } |
276 | | |
277 | | /* |
278 | | * P2. We want decoder_store/decoder_cache to be cleaned up before the |
279 | | * provider store |
280 | | */ |
281 | 3 | if (ctx->decoder_store != NULL) { |
282 | 3 | ossl_method_store_free(ctx->decoder_store); |
283 | 3 | ctx->decoder_store = NULL; |
284 | 3 | } |
285 | 3 | if (ctx->decoder_cache != NULL) { |
286 | 3 | ossl_decoder_cache_free(ctx->decoder_cache); |
287 | 3 | ctx->decoder_cache = NULL; |
288 | 3 | } |
289 | | |
290 | | /* P2. We want encoder_store to be cleaned up before the provider store */ |
291 | 3 | if (ctx->encoder_store != NULL) { |
292 | 3 | ossl_method_store_free(ctx->encoder_store); |
293 | 3 | ctx->encoder_store = NULL; |
294 | 3 | } |
295 | | |
296 | | /* P2. We want loader_store to be cleaned up before the provider store */ |
297 | 3 | if (ctx->store_loader_store != NULL) { |
298 | 3 | ossl_method_store_free(ctx->store_loader_store); |
299 | 3 | ctx->store_loader_store = NULL; |
300 | 3 | } |
301 | 3 | #endif |
302 | | |
303 | | /* P1. Needs to be freed before the child provider data is freed */ |
304 | 3 | if (ctx->provider_store != NULL) { |
305 | 3 | ossl_provider_store_free(ctx->provider_store); |
306 | 3 | ctx->provider_store = NULL; |
307 | 3 | } |
308 | | |
309 | | /* Default priority. */ |
310 | 3 | if (ctx->property_string_data != NULL) { |
311 | 3 | ossl_property_string_data_free(ctx->property_string_data); |
312 | 3 | ctx->property_string_data = NULL; |
313 | 3 | } |
314 | | |
315 | 3 | if (ctx->namemap != NULL) { |
316 | 3 | ossl_stored_namemap_free(ctx->namemap); |
317 | 3 | ctx->namemap = NULL; |
318 | 3 | } |
319 | | |
320 | 3 | if (ctx->property_defns != NULL) { |
321 | 3 | ossl_property_defns_free(ctx->property_defns); |
322 | 3 | ctx->property_defns = NULL; |
323 | 3 | } |
324 | | |
325 | 3 | if (ctx->global_properties != NULL) { |
326 | 3 | ossl_ctx_global_properties_free(ctx->global_properties); |
327 | 3 | ctx->global_properties = NULL; |
328 | 3 | } |
329 | | |
330 | 3 | #ifndef FIPS_MODULE |
331 | 3 | if (ctx->bio_core != NULL) { |
332 | 3 | ossl_bio_core_globals_free(ctx->bio_core); |
333 | 3 | ctx->bio_core = NULL; |
334 | 3 | } |
335 | 3 | #endif |
336 | | |
337 | 3 | if (ctx->drbg_nonce != NULL) { |
338 | 3 | ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce); |
339 | 3 | ctx->drbg_nonce = NULL; |
340 | 3 | } |
341 | | |
342 | 3 | #ifndef FIPS_MODULE |
343 | 3 | if (ctx->indicator_cb != NULL) { |
344 | 3 | ossl_indicator_set_callback_free(ctx->indicator_cb); |
345 | 3 | ctx->indicator_cb = NULL; |
346 | 3 | } |
347 | | |
348 | 3 | if (ctx->self_test_cb != NULL) { |
349 | 3 | ossl_self_test_set_callback_free(ctx->self_test_cb); |
350 | 3 | ctx->self_test_cb = NULL; |
351 | 3 | } |
352 | 3 | #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 | 3 | #ifndef OPENSSL_NO_THREAD_POOL |
364 | 3 | if (ctx->threads != NULL) { |
365 | 3 | ossl_threads_ctx_free(ctx->threads); |
366 | 3 | ctx->threads = NULL; |
367 | 3 | } |
368 | 3 | #endif |
369 | | |
370 | | /* Low priority. */ |
371 | 3 | #ifndef FIPS_MODULE |
372 | 3 | if (ctx->child_provider != NULL) { |
373 | 3 | ossl_child_prov_ctx_free(ctx->child_provider); |
374 | 3 | ctx->child_provider = NULL; |
375 | 3 | } |
376 | 3 | #endif |
377 | | |
378 | 3 | #ifndef FIPS_MODULE |
379 | 3 | if (ctx->comp_methods != NULL) { |
380 | 3 | ossl_free_compression_methods_int(ctx->comp_methods); |
381 | 3 | ctx->comp_methods = NULL; |
382 | 3 | } |
383 | 3 | #endif |
384 | 3 | } |
385 | | |
386 | | static int context_deinit(OSSL_LIB_CTX *ctx) |
387 | 3 | { |
388 | 3 | if (ctx == NULL) |
389 | 0 | return 1; |
390 | | |
391 | 3 | ossl_lib_ctx_detach_ssl_conf_imodule(ctx, NULL); |
392 | | |
393 | 3 | ossl_ctx_thread_stop(ctx); |
394 | | |
395 | 3 | context_deinit_objs(ctx); |
396 | | |
397 | 3 | ossl_crypto_cleanup_all_ex_data_int(ctx); |
398 | | |
399 | 3 | CRYPTO_THREAD_lock_free(ctx->lock); |
400 | 3 | ctx->lock = NULL; |
401 | 3 | return 1; |
402 | 3 | } |
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_THREAD_LOCAL default_context_thread_local; |
410 | | static int default_context_inited = 0; |
411 | | |
412 | | DEFINE_RUN_ONCE_STATIC(default_context_do_init) |
413 | 3 | { |
414 | 3 | if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)) |
415 | 0 | goto err; |
416 | | |
417 | 3 | if (!context_init(&default_context_int)) |
418 | 0 | goto deinit_thread; |
419 | | |
420 | 3 | default_context_inited = 1; |
421 | 3 | return 1; |
422 | | |
423 | 0 | deinit_thread: |
424 | 0 | CRYPTO_THREAD_cleanup_local(&default_context_thread_local); |
425 | 0 | err: |
426 | 0 | return 0; |
427 | 0 | } |
428 | | |
429 | | void ossl_lib_ctx_default_deinit(void) |
430 | 3 | { |
431 | 3 | if (!default_context_inited) |
432 | 0 | return; |
433 | 3 | context_deinit(&default_context_int); |
434 | 3 | CRYPTO_THREAD_cleanup_local(&default_context_thread_local); |
435 | 3 | default_context_inited = 0; |
436 | 3 | } |
437 | | |
438 | | static OSSL_LIB_CTX *get_thread_default_context(void) |
439 | 2.18k | { |
440 | 2.18k | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
441 | 0 | return NULL; |
442 | | |
443 | 2.18k | return CRYPTO_THREAD_get_local(&default_context_thread_local); |
444 | 2.18k | } |
445 | | |
446 | | static OSSL_LIB_CTX *get_default_context(void) |
447 | 2.18k | { |
448 | 2.18k | OSSL_LIB_CTX *current_defctx = get_thread_default_context(); |
449 | | |
450 | 2.18k | if (current_defctx == NULL && default_context_inited) |
451 | 2.18k | current_defctx = &default_context_int; |
452 | 2.18k | return current_defctx; |
453 | 2.18k | } |
454 | | |
455 | | static int set_default_context(OSSL_LIB_CTX *defctx) |
456 | 0 | { |
457 | 0 | if (defctx == &default_context_int) |
458 | 0 | defctx = NULL; |
459 | |
|
460 | 0 | return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx); |
461 | 0 | } |
462 | | #endif |
463 | | |
464 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new(void) |
465 | 6 | { |
466 | 6 | OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
467 | | |
468 | 6 | if (ctx != NULL && !context_init(ctx)) { |
469 | 0 | OPENSSL_free(ctx); |
470 | 0 | ctx = NULL; |
471 | 0 | } |
472 | 6 | return ctx; |
473 | 6 | } |
474 | | |
475 | | #ifndef FIPS_MODULE |
476 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle, |
477 | | const OSSL_DISPATCH *in) |
478 | 3 | { |
479 | 3 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); |
480 | | |
481 | 3 | if (ctx == NULL) |
482 | 0 | return NULL; |
483 | | |
484 | 3 | if (!ossl_bio_init_core(ctx, in)) { |
485 | 0 | OSSL_LIB_CTX_free(ctx); |
486 | 0 | return NULL; |
487 | 0 | } |
488 | | |
489 | 3 | return ctx; |
490 | 3 | } |
491 | | |
492 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle, |
493 | | const OSSL_DISPATCH *in) |
494 | 3 | { |
495 | 3 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in); |
496 | | |
497 | 3 | if (ctx == NULL) |
498 | 0 | return NULL; |
499 | | |
500 | 3 | if (!ossl_provider_init_as_child(ctx, handle, in)) { |
501 | 0 | OSSL_LIB_CTX_free(ctx); |
502 | 0 | return NULL; |
503 | 0 | } |
504 | 3 | ctx->ischild = 1; |
505 | | |
506 | 3 | return ctx; |
507 | 3 | } |
508 | | |
509 | | int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file) |
510 | 0 | { |
511 | 0 | return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0; |
512 | 0 | } |
513 | | #endif |
514 | | |
515 | | void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx) |
516 | 0 | { |
517 | 0 | if (ctx == NULL || ossl_lib_ctx_is_default(ctx)) |
518 | 0 | return; |
519 | | |
520 | 0 | #ifndef FIPS_MODULE |
521 | 0 | if (ctx->ischild) |
522 | 0 | ossl_provider_deinit_child(ctx); |
523 | 0 | #endif |
524 | 0 | context_deinit(ctx); |
525 | 0 | OPENSSL_free(ctx); |
526 | 0 | } |
527 | | |
528 | | #ifndef FIPS_MODULE |
529 | | OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void) |
530 | 1 | { |
531 | 1 | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
532 | 0 | return NULL; |
533 | | |
534 | 1 | return &default_context_int; |
535 | 1 | } |
536 | | |
537 | | OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx) |
538 | 0 | { |
539 | 0 | OSSL_LIB_CTX *current_defctx; |
540 | |
|
541 | 0 | if ((current_defctx = get_default_context()) != NULL) { |
542 | 0 | if (libctx != NULL) |
543 | 0 | set_default_context(libctx); |
544 | 0 | return current_defctx; |
545 | 0 | } |
546 | | |
547 | 0 | return NULL; |
548 | 0 | } |
549 | | |
550 | | void ossl_release_default_drbg_ctx(void) |
551 | 0 | { |
552 | | /* early release of the DRBG in global default libctx */ |
553 | 0 | if (default_context_int.drbg != NULL) { |
554 | 0 | ossl_rand_ctx_free(default_context_int.drbg); |
555 | 0 | default_context_int.drbg = NULL; |
556 | 0 | } |
557 | 0 | } |
558 | | #endif |
559 | | |
560 | | OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx) |
561 | 2.06M | { |
562 | 2.06M | #ifndef FIPS_MODULE |
563 | 2.06M | if (ctx == NULL) |
564 | 65 | return get_default_context(); |
565 | 2.06M | #endif |
566 | 2.06M | return ctx; |
567 | 2.06M | } |
568 | | |
569 | | int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx) |
570 | 2.12k | { |
571 | 2.12k | #ifndef FIPS_MODULE |
572 | 2.12k | if (ctx == NULL || ctx == get_default_context()) |
573 | 2 | return 1; |
574 | 2.12k | #endif |
575 | 2.12k | return 0; |
576 | 2.12k | } |
577 | | |
578 | | int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx) |
579 | 929 | { |
580 | 929 | #ifndef FIPS_MODULE |
581 | 929 | if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int) |
582 | 0 | return 1; |
583 | 929 | #endif |
584 | 929 | return 0; |
585 | 929 | } |
586 | | |
587 | | void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index) |
588 | 2.06M | { |
589 | 2.06M | ctx = ossl_lib_ctx_get_concrete(ctx); |
590 | 2.06M | if (ctx == NULL) |
591 | 0 | return NULL; |
592 | | |
593 | 2.06M | switch (index) { |
594 | 86 | case OSSL_LIB_CTX_PROPERTY_STRING_INDEX: |
595 | 86 | return ctx->property_string_data; |
596 | 1.03M | case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX: |
597 | 1.03M | return ctx->evp_method_store; |
598 | 1.02k | case OSSL_LIB_CTX_PROVIDER_STORE_INDEX: |
599 | 1.02k | return ctx->provider_store; |
600 | 1.03M | case OSSL_LIB_CTX_NAMEMAP_INDEX: |
601 | 1.03M | return ctx->namemap; |
602 | 285 | case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX: |
603 | 285 | return ctx->property_defns; |
604 | 215 | case OSSL_LIB_CTX_GLOBAL_PROPERTIES: |
605 | 215 | return ctx->global_properties; |
606 | 0 | case OSSL_LIB_CTX_DRBG_INDEX: |
607 | 0 | return ctx->drbg; |
608 | 0 | case OSSL_LIB_CTX_DRBG_NONCE_INDEX: |
609 | 0 | return ctx->drbg_nonce; |
610 | 0 | #ifndef FIPS_MODULE |
611 | 0 | case OSSL_LIB_CTX_PROVIDER_CONF_INDEX: |
612 | 0 | return ctx->provider_conf; |
613 | 3 | case OSSL_LIB_CTX_BIO_CORE_INDEX: |
614 | 3 | return ctx->bio_core; |
615 | 15 | case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX: |
616 | 15 | return ctx->child_provider; |
617 | 12 | case OSSL_LIB_CTX_DECODER_STORE_INDEX: |
618 | 12 | return ctx->decoder_store; |
619 | 16 | case OSSL_LIB_CTX_DECODER_CACHE_INDEX: |
620 | 16 | return ctx->decoder_cache; |
621 | 12 | case OSSL_LIB_CTX_ENCODER_STORE_INDEX: |
622 | 12 | return ctx->encoder_store; |
623 | 12 | case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX: |
624 | 12 | return ctx->store_loader_store; |
625 | 0 | case OSSL_LIB_CTX_SELF_TEST_CB_INDEX: |
626 | 0 | return ctx->self_test_cb; |
627 | 0 | case OSSL_LIB_CTX_INDICATOR_CB_INDEX: |
628 | 0 | return ctx->indicator_cb; |
629 | 0 | #endif |
630 | 0 | #ifndef OPENSSL_NO_THREAD_POOL |
631 | 6 | case OSSL_LIB_CTX_THREAD_INDEX: |
632 | 6 | return ctx->threads; |
633 | 0 | #endif |
634 | | |
635 | | #ifdef FIPS_MODULE |
636 | | case OSSL_LIB_CTX_FIPS_PROV_INDEX: |
637 | | return ctx->fips_prov; |
638 | | #endif |
639 | | |
640 | 0 | case OSSL_LIB_CTX_COMP_METHODS: |
641 | 0 | return (void *)&ctx->comp_methods; |
642 | | |
643 | 0 | case OSSL_LIB_CTX_SSL_CONF_IMODULE: |
644 | 0 | return (void *)ctx->ssl_imod; |
645 | | |
646 | 0 | default: |
647 | 0 | return NULL; |
648 | 2.06M | } |
649 | 2.06M | } |
650 | | |
651 | | void *OSSL_LIB_CTX_get_data(OSSL_LIB_CTX *ctx, int index) |
652 | 0 | { |
653 | 0 | return ossl_lib_ctx_get_data(ctx, index); |
654 | 0 | } |
655 | | |
656 | | OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx) |
657 | 12 | { |
658 | 12 | ctx = ossl_lib_ctx_get_concrete(ctx); |
659 | 12 | if (ctx == NULL) |
660 | 0 | return NULL; |
661 | 12 | return &ctx->global; |
662 | 12 | } |
663 | | |
664 | | const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx) |
665 | 929 | { |
666 | | #ifdef FIPS_MODULE |
667 | | return "FIPS internal library context"; |
668 | | #else |
669 | 929 | if (ossl_lib_ctx_is_global_default(libctx)) |
670 | 0 | return "Global default library context"; |
671 | 929 | if (ossl_lib_ctx_is_default(libctx)) |
672 | 0 | return "Thread-local default library context"; |
673 | 929 | return "Non-default library context"; |
674 | 929 | #endif |
675 | 929 | } |
676 | | |
677 | | int OSSL_LIB_CTX_get_conf_diagnostics(OSSL_LIB_CTX *libctx) |
678 | 1 | { |
679 | 1 | libctx = ossl_lib_ctx_get_concrete(libctx); |
680 | 1 | if (libctx == NULL) |
681 | 0 | return 0; |
682 | 1 | return libctx->conf_diagnostics; |
683 | 1 | } |
684 | | |
685 | | void OSSL_LIB_CTX_set_conf_diagnostics(OSSL_LIB_CTX *libctx, int value) |
686 | 0 | { |
687 | 0 | libctx = ossl_lib_ctx_get_concrete(libctx); |
688 | 0 | if (libctx == NULL) |
689 | 0 | return; |
690 | 0 | libctx->conf_diagnostics = value; |
691 | 0 | } |