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