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