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