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