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