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