/src/openssl33/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/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.55k | { |
57 | 1.55k | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
58 | 0 | return 0; |
59 | 1.55k | return CRYPTO_THREAD_write_lock(ctx->lock); |
60 | 1.55k | } |
61 | | |
62 | | int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx) |
63 | 23.3k | { |
64 | 23.3k | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
65 | 0 | return 0; |
66 | 23.3k | return CRYPTO_THREAD_read_lock(ctx->lock); |
67 | 23.3k | } |
68 | | |
69 | | int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx) |
70 | 24.8k | { |
71 | 24.8k | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
72 | 0 | return 0; |
73 | 24.8k | return CRYPTO_THREAD_unlock(ctx->lock); |
74 | 24.8k | } |
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 | 42 | { |
229 | | /* P2. We want evp_method_store to be cleaned up before the provider store */ |
230 | 42 | if (ctx->evp_method_store != NULL) { |
231 | 42 | ossl_method_store_free(ctx->evp_method_store); |
232 | 42 | ctx->evp_method_store = NULL; |
233 | 42 | } |
234 | | |
235 | | /* P2. */ |
236 | 42 | if (ctx->drbg != NULL) { |
237 | 31 | ossl_rand_ctx_free(ctx->drbg); |
238 | 31 | ctx->drbg = NULL; |
239 | 31 | } |
240 | | |
241 | 42 | #ifndef FIPS_MODULE |
242 | | /* P2. */ |
243 | 42 | if (ctx->provider_conf != NULL) { |
244 | 42 | ossl_prov_conf_ctx_free(ctx->provider_conf); |
245 | 42 | ctx->provider_conf = NULL; |
246 | 42 | } |
247 | | |
248 | | /* |
249 | | * P2. We want decoder_store/decoder_cache to be cleaned up before the |
250 | | * provider store |
251 | | */ |
252 | 42 | if (ctx->decoder_store != NULL) { |
253 | 42 | ossl_method_store_free(ctx->decoder_store); |
254 | 42 | ctx->decoder_store = NULL; |
255 | 42 | } |
256 | 42 | if (ctx->decoder_cache != NULL) { |
257 | 42 | ossl_decoder_cache_free(ctx->decoder_cache); |
258 | 42 | ctx->decoder_cache = NULL; |
259 | 42 | } |
260 | | |
261 | | /* P2. We want encoder_store to be cleaned up before the provider store */ |
262 | 42 | if (ctx->encoder_store != NULL) { |
263 | 42 | ossl_method_store_free(ctx->encoder_store); |
264 | 42 | ctx->encoder_store = NULL; |
265 | 42 | } |
266 | | |
267 | | /* P2. We want loader_store to be cleaned up before the provider store */ |
268 | 42 | if (ctx->store_loader_store != NULL) { |
269 | 42 | ossl_method_store_free(ctx->store_loader_store); |
270 | 42 | ctx->store_loader_store = NULL; |
271 | 42 | } |
272 | 42 | #endif |
273 | | |
274 | | /* P1. Needs to be freed before the child provider data is freed */ |
275 | 42 | if (ctx->provider_store != NULL) { |
276 | 42 | ossl_provider_store_free(ctx->provider_store); |
277 | 42 | ctx->provider_store = NULL; |
278 | 42 | } |
279 | | |
280 | | /* Default priority. */ |
281 | 42 | if (ctx->property_string_data != NULL) { |
282 | 42 | ossl_property_string_data_free(ctx->property_string_data); |
283 | 42 | ctx->property_string_data = NULL; |
284 | 42 | } |
285 | | |
286 | 42 | if (ctx->namemap != NULL) { |
287 | 42 | ossl_stored_namemap_free(ctx->namemap); |
288 | 42 | ctx->namemap = NULL; |
289 | 42 | } |
290 | | |
291 | 42 | if (ctx->property_defns != NULL) { |
292 | 42 | ossl_property_defns_free(ctx->property_defns); |
293 | 42 | ctx->property_defns = NULL; |
294 | 42 | } |
295 | | |
296 | 42 | if (ctx->global_properties != NULL) { |
297 | 42 | ossl_ctx_global_properties_free(ctx->global_properties); |
298 | 42 | ctx->global_properties = NULL; |
299 | 42 | } |
300 | | |
301 | 42 | #ifndef FIPS_MODULE |
302 | 42 | if (ctx->bio_core != NULL) { |
303 | 42 | ossl_bio_core_globals_free(ctx->bio_core); |
304 | 42 | ctx->bio_core = NULL; |
305 | 42 | } |
306 | 42 | #endif |
307 | | |
308 | 42 | if (ctx->drbg_nonce != NULL) { |
309 | 42 | ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce); |
310 | 42 | ctx->drbg_nonce = NULL; |
311 | 42 | } |
312 | | |
313 | 42 | #ifndef FIPS_MODULE |
314 | 42 | if (ctx->self_test_cb != NULL) { |
315 | 42 | ossl_self_test_set_callback_free(ctx->self_test_cb); |
316 | 42 | ctx->self_test_cb = NULL; |
317 | 42 | } |
318 | 42 | #endif |
319 | | |
320 | 42 | if (ctx->rand_crngt != NULL) { |
321 | 0 | ossl_rand_crng_ctx_free(ctx->rand_crngt); |
322 | 0 | ctx->rand_crngt = NULL; |
323 | 0 | } |
324 | | |
325 | | #ifdef FIPS_MODULE |
326 | | if (ctx->thread_event_handler != NULL) { |
327 | | ossl_thread_event_ctx_free(ctx->thread_event_handler); |
328 | | ctx->thread_event_handler = NULL; |
329 | | } |
330 | | |
331 | | if (ctx->fips_prov != NULL) { |
332 | | ossl_fips_prov_ossl_ctx_free(ctx->fips_prov); |
333 | | ctx->fips_prov = NULL; |
334 | | } |
335 | | #endif |
336 | | |
337 | 42 | #ifndef OPENSSL_NO_THREAD_POOL |
338 | 42 | if (ctx->threads != NULL) { |
339 | 42 | ossl_threads_ctx_free(ctx->threads); |
340 | 42 | ctx->threads = NULL; |
341 | 42 | } |
342 | 42 | #endif |
343 | | |
344 | | /* Low priority. */ |
345 | 42 | #ifndef FIPS_MODULE |
346 | 42 | if (ctx->child_provider != NULL) { |
347 | 42 | ossl_child_prov_ctx_free(ctx->child_provider); |
348 | 42 | ctx->child_provider = NULL; |
349 | 42 | } |
350 | 42 | #endif |
351 | 42 | } |
352 | | |
353 | | static int context_deinit(OSSL_LIB_CTX *ctx) |
354 | 196 | { |
355 | 196 | if (ctx == NULL) |
356 | 0 | return 1; |
357 | | |
358 | 196 | ossl_ctx_thread_stop(ctx); |
359 | | |
360 | 196 | context_deinit_objs(ctx); |
361 | | |
362 | 196 | ossl_crypto_cleanup_all_ex_data_int(ctx); |
363 | | |
364 | 196 | CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock); |
365 | 196 | CRYPTO_THREAD_lock_free(ctx->lock); |
366 | 196 | ctx->rand_crngt_lock = NULL; |
367 | 196 | ctx->lock = NULL; |
368 | 196 | CRYPTO_THREAD_cleanup_local(&ctx->rcu_local_key); |
369 | 196 | return 1; |
370 | 196 | } |
371 | | |
372 | | #ifndef FIPS_MODULE |
373 | | /* The default default context */ |
374 | | static OSSL_LIB_CTX default_context_int; |
375 | | |
376 | | static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT; |
377 | | static CRYPTO_THREAD_LOCAL default_context_thread_local; |
378 | | static int default_context_inited = 0; |
379 | | |
380 | | DEFINE_RUN_ONCE_STATIC(default_context_do_init) |
381 | 244 | { |
382 | 244 | if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)) |
383 | 0 | goto err; |
384 | | |
385 | 244 | if (!context_init(&default_context_int)) |
386 | 0 | goto deinit_thread; |
387 | | |
388 | 244 | default_context_inited = 1; |
389 | 244 | return 1; |
390 | | |
391 | 0 | deinit_thread: |
392 | 0 | CRYPTO_THREAD_cleanup_local(&default_context_thread_local); |
393 | 0 | err: |
394 | 0 | return 0; |
395 | 0 | } |
396 | | |
397 | | void ossl_lib_ctx_default_deinit(void) |
398 | 196 | { |
399 | 196 | if (!default_context_inited) |
400 | 0 | return; |
401 | 196 | context_deinit(&default_context_int); |
402 | 196 | CRYPTO_THREAD_cleanup_local(&default_context_thread_local); |
403 | 196 | default_context_inited = 0; |
404 | 196 | } |
405 | | |
406 | | static OSSL_LIB_CTX *get_thread_default_context(void) |
407 | 150M | { |
408 | 150M | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
409 | 0 | return NULL; |
410 | | |
411 | 150M | return CRYPTO_THREAD_get_local(&default_context_thread_local); |
412 | 150M | } |
413 | | |
414 | | static OSSL_LIB_CTX *get_default_context(void) |
415 | 130M | { |
416 | 130M | OSSL_LIB_CTX *current_defctx = get_thread_default_context(); |
417 | | |
418 | 130M | if (current_defctx == NULL && default_context_inited) |
419 | 130M | current_defctx = &default_context_int; |
420 | 130M | return current_defctx; |
421 | 130M | } |
422 | | |
423 | | static int set_default_context(OSSL_LIB_CTX *defctx) |
424 | 0 | { |
425 | 0 | if (defctx == &default_context_int) |
426 | 0 | defctx = NULL; |
427 | |
|
428 | 0 | return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx); |
429 | 0 | } |
430 | | #endif |
431 | | |
432 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new(void) |
433 | 144 | { |
434 | 144 | OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
435 | | |
436 | 144 | if (ctx != NULL && !context_init(ctx)) { |
437 | 0 | OPENSSL_free(ctx); |
438 | 0 | ctx = NULL; |
439 | 0 | } |
440 | 144 | return ctx; |
441 | 144 | } |
442 | | |
443 | | #ifndef FIPS_MODULE |
444 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle, |
445 | | const OSSL_DISPATCH *in) |
446 | 0 | { |
447 | 0 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); |
448 | |
|
449 | 0 | if (ctx == NULL) |
450 | 0 | return NULL; |
451 | | |
452 | 0 | if (!ossl_bio_init_core(ctx, in)) { |
453 | 0 | OSSL_LIB_CTX_free(ctx); |
454 | 0 | return NULL; |
455 | 0 | } |
456 | | |
457 | 0 | return ctx; |
458 | 0 | } |
459 | | |
460 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle, |
461 | | const OSSL_DISPATCH *in) |
462 | 0 | { |
463 | 0 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in); |
464 | |
|
465 | 0 | if (ctx == NULL) |
466 | 0 | return NULL; |
467 | | |
468 | 0 | if (!ossl_provider_init_as_child(ctx, handle, in)) { |
469 | 0 | OSSL_LIB_CTX_free(ctx); |
470 | 0 | return NULL; |
471 | 0 | } |
472 | 0 | ctx->ischild = 1; |
473 | |
|
474 | 0 | return ctx; |
475 | 0 | } |
476 | | |
477 | | int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file) |
478 | 0 | { |
479 | 0 | return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0; |
480 | 0 | } |
481 | | #endif |
482 | | |
483 | | void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx) |
484 | 0 | { |
485 | 0 | if (ctx == NULL || ossl_lib_ctx_is_default(ctx)) |
486 | 0 | return; |
487 | | |
488 | 0 | #ifndef FIPS_MODULE |
489 | 0 | if (ctx->ischild) |
490 | 0 | ossl_provider_deinit_child(ctx); |
491 | 0 | #endif |
492 | 0 | context_deinit(ctx); |
493 | 0 | OPENSSL_free(ctx); |
494 | 0 | } |
495 | | |
496 | | #ifndef FIPS_MODULE |
497 | | OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void) |
498 | 29.7k | { |
499 | 29.7k | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
500 | 0 | return NULL; |
501 | | |
502 | 29.7k | return &default_context_int; |
503 | 29.7k | } |
504 | | |
505 | | OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx) |
506 | 163k | { |
507 | 163k | OSSL_LIB_CTX *current_defctx; |
508 | | |
509 | 163k | if ((current_defctx = get_default_context()) != NULL) { |
510 | 163k | if (libctx != NULL) |
511 | 0 | set_default_context(libctx); |
512 | 163k | return current_defctx; |
513 | 163k | } |
514 | | |
515 | 0 | return NULL; |
516 | 163k | } |
517 | | |
518 | | void ossl_release_default_drbg_ctx(void) |
519 | 61 | { |
520 | | /* early release of the DRBG in global default libctx */ |
521 | 61 | if (default_context_int.drbg != NULL) { |
522 | 61 | ossl_rand_ctx_free(default_context_int.drbg); |
523 | 61 | default_context_int.drbg = NULL; |
524 | 61 | } |
525 | 61 | } |
526 | | #endif |
527 | | |
528 | | OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx) |
529 | 429M | { |
530 | 429M | #ifndef FIPS_MODULE |
531 | 429M | if (ctx == NULL) |
532 | 149M | return get_default_context(); |
533 | 280M | #endif |
534 | 280M | return ctx; |
535 | 429M | } |
536 | | |
537 | | int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx) |
538 | 4.76M | { |
539 | 4.76M | #ifndef FIPS_MODULE |
540 | 4.76M | if (ctx == NULL || ctx == get_default_context()) |
541 | 4.74M | return 1; |
542 | 16.6k | #endif |
543 | 16.6k | return 0; |
544 | 4.76M | } |
545 | | |
546 | | int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx) |
547 | 2.62M | { |
548 | 2.62M | #ifndef FIPS_MODULE |
549 | 2.62M | if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int) |
550 | 2.62M | return 1; |
551 | 2.12k | #endif |
552 | 2.12k | return 0; |
553 | 2.62M | } |
554 | | |
555 | | void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index) |
556 | 16.8M | { |
557 | 16.8M | void *p; |
558 | | |
559 | 16.8M | ctx = ossl_lib_ctx_get_concrete(ctx); |
560 | 16.8M | if (ctx == NULL) |
561 | 0 | return NULL; |
562 | | |
563 | 16.8M | switch (index) { |
564 | 540k | case OSSL_LIB_CTX_PROPERTY_STRING_INDEX: |
565 | 540k | return ctx->property_string_data; |
566 | 7.15M | case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX: |
567 | 7.15M | return ctx->evp_method_store; |
568 | 611k | case OSSL_LIB_CTX_PROVIDER_STORE_INDEX: |
569 | 611k | return ctx->provider_store; |
570 | 7.74M | case OSSL_LIB_CTX_NAMEMAP_INDEX: |
571 | 7.74M | return ctx->namemap; |
572 | 2.96k | case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX: |
573 | 2.96k | return ctx->property_defns; |
574 | 282 | case OSSL_LIB_CTX_GLOBAL_PROPERTIES: |
575 | 282 | return ctx->global_properties; |
576 | 363k | case OSSL_LIB_CTX_DRBG_INDEX: |
577 | 363k | return ctx->drbg; |
578 | 0 | case OSSL_LIB_CTX_DRBG_NONCE_INDEX: |
579 | 0 | return ctx->drbg_nonce; |
580 | 0 | #ifndef FIPS_MODULE |
581 | 0 | case OSSL_LIB_CTX_PROVIDER_CONF_INDEX: |
582 | 0 | return ctx->provider_conf; |
583 | 0 | case OSSL_LIB_CTX_BIO_CORE_INDEX: |
584 | 0 | return ctx->bio_core; |
585 | 0 | case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX: |
586 | 0 | return ctx->child_provider; |
587 | 17.8k | case OSSL_LIB_CTX_DECODER_STORE_INDEX: |
588 | 17.8k | return ctx->decoder_store; |
589 | 236k | case OSSL_LIB_CTX_DECODER_CACHE_INDEX: |
590 | 236k | return ctx->decoder_cache; |
591 | 216k | case OSSL_LIB_CTX_ENCODER_STORE_INDEX: |
592 | 216k | return ctx->encoder_store; |
593 | 24 | case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX: |
594 | 24 | return ctx->store_loader_store; |
595 | 0 | case OSSL_LIB_CTX_SELF_TEST_CB_INDEX: |
596 | 0 | return ctx->self_test_cb; |
597 | 0 | #endif |
598 | 0 | #ifndef OPENSSL_NO_THREAD_POOL |
599 | 0 | case OSSL_LIB_CTX_THREAD_INDEX: |
600 | 0 | return ctx->threads; |
601 | 0 | #endif |
602 | | |
603 | 0 | case OSSL_LIB_CTX_RAND_CRNGT_INDEX: { |
604 | | |
605 | | /* |
606 | | * rand_crngt must be lazily initialized because it calls into |
607 | | * libctx, so must not be called from context_init, else a deadlock |
608 | | * will occur. |
609 | | * |
610 | | * We use a separate lock because code called by the instantiation |
611 | | * of rand_crngt is liable to try and take the libctx lock. |
612 | | */ |
613 | 0 | if (CRYPTO_THREAD_read_lock(ctx->rand_crngt_lock) != 1) |
614 | 0 | return NULL; |
615 | | |
616 | 0 | if (ctx->rand_crngt == NULL) { |
617 | 0 | CRYPTO_THREAD_unlock(ctx->rand_crngt_lock); |
618 | |
|
619 | 0 | if (CRYPTO_THREAD_write_lock(ctx->rand_crngt_lock) != 1) |
620 | 0 | return NULL; |
621 | | |
622 | 0 | if (ctx->rand_crngt == NULL) |
623 | 0 | ctx->rand_crngt = ossl_rand_crng_ctx_new(ctx); |
624 | 0 | } |
625 | | |
626 | 0 | p = ctx->rand_crngt; |
627 | |
|
628 | 0 | CRYPTO_THREAD_unlock(ctx->rand_crngt_lock); |
629 | |
|
630 | 0 | return p; |
631 | 0 | } |
632 | | |
633 | | #ifdef FIPS_MODULE |
634 | | case OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX: |
635 | | return ctx->thread_event_handler; |
636 | | |
637 | | case OSSL_LIB_CTX_FIPS_PROV_INDEX: |
638 | | return ctx->fips_prov; |
639 | | #endif |
640 | | |
641 | 0 | default: |
642 | 0 | return NULL; |
643 | 16.8M | } |
644 | 16.8M | } |
645 | | |
646 | | OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx) |
647 | 72.2M | { |
648 | 72.2M | ctx = ossl_lib_ctx_get_concrete(ctx); |
649 | 72.2M | if (ctx == NULL) |
650 | 0 | return NULL; |
651 | 72.2M | return &ctx->global; |
652 | 72.2M | } |
653 | | |
654 | | const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx) |
655 | 2.62M | { |
656 | | #ifdef FIPS_MODULE |
657 | | return "FIPS internal library context"; |
658 | | #else |
659 | 2.62M | if (ossl_lib_ctx_is_global_default(libctx)) |
660 | 2.62M | return "Global default library context"; |
661 | 2.12k | if (ossl_lib_ctx_is_default(libctx)) |
662 | 0 | return "Thread-local default library context"; |
663 | 2.12k | return "Non-default library context"; |
664 | 2.12k | #endif |
665 | 2.12k | } |
666 | | |
667 | | CRYPTO_THREAD_LOCAL *ossl_lib_ctx_get_rcukey(OSSL_LIB_CTX *libctx) |
668 | 58 | { |
669 | 58 | libctx = ossl_lib_ctx_get_concrete(libctx); |
670 | 58 | if (libctx == NULL) |
671 | 0 | return NULL; |
672 | 58 | return &libctx->rcu_local_key; |
673 | 58 | } |