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