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