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