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