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