/src/openssl/crypto/context.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2026 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 <openssl/trace.h> |
13 | | #include "internal/thread_once.h" |
14 | | #include "internal/property.h" |
15 | | #include "internal/cryptlib.h" |
16 | | #include "internal/core.h" |
17 | | #include "internal/bio.h" |
18 | | #include "internal/provider.h" |
19 | | #include "internal/conf.h" |
20 | | #include "crypto/decoder.h" |
21 | | #include "crypto/context.h" |
22 | | |
23 | | struct ossl_lib_ctx_st { |
24 | | CRYPTO_RWLOCK *lock; |
25 | | OSSL_EX_DATA_GLOBAL global; |
26 | | CONF_IMODULE *ssl_imod; |
27 | | void *property_string_data; |
28 | | void *evp_method_store; |
29 | | void *provider_store; |
30 | | void *namemap; |
31 | | void *property_defns; |
32 | | void *global_properties; |
33 | | void *drbg; |
34 | | void *drbg_nonce; |
35 | | #ifndef FIPS_MODULE |
36 | | void *provider_conf; |
37 | | void *bio_core; |
38 | | void *child_provider; |
39 | | OSSL_METHOD_STORE *decoder_store; |
40 | | void *decoder_cache; |
41 | | OSSL_METHOD_STORE *encoder_store; |
42 | | OSSL_METHOD_STORE *store_loader_store; |
43 | | void *self_test_cb; |
44 | | void *indicator_cb; |
45 | | #endif |
46 | | #if defined(OPENSSL_THREADS) |
47 | | void *threads; |
48 | | #endif |
49 | | #ifdef FIPS_MODULE |
50 | | void *fips_prov; |
51 | | #endif |
52 | | STACK_OF(SSL_COMP) *comp_methods; |
53 | | |
54 | | int ischild; |
55 | | int conf_diagnostics; |
56 | | uint64_t new_provider_loaded; |
57 | | }; |
58 | | |
59 | | int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx) |
60 | 2 | { |
61 | 2 | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
62 | 0 | return 0; |
63 | 2 | return CRYPTO_THREAD_write_lock(ctx->lock); |
64 | 2 | } |
65 | | |
66 | | int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx) |
67 | 193 | { |
68 | 193 | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
69 | 0 | return 0; |
70 | 193 | return CRYPTO_THREAD_read_lock(ctx->lock); |
71 | 193 | } |
72 | | |
73 | | int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx) |
74 | 195 | { |
75 | 195 | if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) |
76 | 0 | return 0; |
77 | 195 | return CRYPTO_THREAD_unlock(ctx->lock); |
78 | 195 | } |
79 | | |
80 | | int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx) |
81 | 0 | { |
82 | 0 | ctx = ossl_lib_ctx_get_concrete(ctx); |
83 | |
|
84 | 0 | if (ctx == NULL) |
85 | 0 | return 0; |
86 | 0 | return ctx->ischild; |
87 | 0 | } |
88 | | |
89 | | int ossl_lib_ctx_attach_ssl_conf_imodule(OSSL_LIB_CTX *ctx, CONF_IMODULE *md) |
90 | 0 | { |
91 | 0 | if (ctx == NULL || md == NULL || ctx->ssl_imod != NULL) |
92 | 0 | return 0; |
93 | | |
94 | 0 | ctx->ssl_imod = md; |
95 | 0 | md->libctx = ctx; |
96 | 0 | return 1; |
97 | 0 | } |
98 | | |
99 | | int ossl_lib_ctx_detach_ssl_conf_imodule(OSSL_LIB_CTX *ctx, CONF_IMODULE *md) |
100 | 0 | { |
101 | 0 | if (ctx != NULL && md != NULL) |
102 | 0 | return 0; |
103 | | |
104 | 0 | if (ctx != NULL && ctx->ssl_imod) { |
105 | 0 | ctx->ssl_imod->libctx = NULL; |
106 | 0 | ctx->ssl_imod = NULL; |
107 | 0 | } |
108 | |
|
109 | 0 | if (md != NULL && md->libctx) { |
110 | 0 | md->libctx->ssl_imod = NULL; |
111 | 0 | md->libctx = NULL; |
112 | 0 | } |
113 | |
|
114 | 0 | return 1; |
115 | 0 | } |
116 | | |
117 | | static void context_deinit_objs(OSSL_LIB_CTX *ctx); |
118 | | |
119 | | static int context_init(OSSL_LIB_CTX *ctx) |
120 | 3 | { |
121 | 3 | int exdata_done = 0; |
122 | | |
123 | 3 | ctx->lock = CRYPTO_THREAD_lock_new(); |
124 | 3 | if (ctx->lock == NULL) |
125 | 0 | goto err; |
126 | | |
127 | | /* Initialize ex_data. */ |
128 | 3 | if (!ossl_do_ex_data_init(ctx)) |
129 | 0 | goto err; |
130 | 3 | exdata_done = 1; |
131 | | |
132 | 3 | if (!CRYPTO_atomic_store(&ctx->new_provider_loaded, UINT64_MAX, ctx->lock)) |
133 | 0 | goto err; |
134 | | |
135 | | /* P2. We want evp_method_store to be cleaned up before the provider store */ |
136 | 3 | ctx->evp_method_store = ossl_method_store_new(ctx); |
137 | 3 | if (ctx->evp_method_store == NULL) |
138 | 0 | goto err; |
139 | 3 | OSSL_TRACE1(QUERY, "context_init: allocating store %p\n", ctx->evp_method_store); |
140 | | |
141 | 3 | #ifndef FIPS_MODULE |
142 | | /* P2. Must be freed before the provider store is freed */ |
143 | 3 | ctx->provider_conf = ossl_prov_conf_ctx_new(ctx); |
144 | 3 | if (ctx->provider_conf == NULL) |
145 | 0 | goto err; |
146 | 3 | #endif |
147 | | |
148 | | /* P2. */ |
149 | 3 | ctx->drbg = ossl_rand_ctx_new(ctx); |
150 | 3 | if (ctx->drbg == NULL) |
151 | 0 | goto err; |
152 | | |
153 | 3 | #ifndef FIPS_MODULE |
154 | | /* |
155 | | * P2. We want decoder_store/decoder_cache to be cleaned up before the |
156 | | * provider store |
157 | | */ |
158 | 3 | ctx->decoder_store = ossl_method_store_new(ctx); |
159 | 3 | if (ctx->decoder_store == NULL) |
160 | 0 | goto err; |
161 | 3 | ctx->decoder_cache = ossl_decoder_cache_new(ctx); |
162 | 3 | if (ctx->decoder_cache == NULL) |
163 | 0 | goto err; |
164 | | |
165 | | /* P2. We want encoder_store to be cleaned up before the provider store */ |
166 | 3 | ctx->encoder_store = ossl_method_store_new(ctx); |
167 | 3 | if (ctx->encoder_store == NULL) |
168 | 0 | goto err; |
169 | | |
170 | | /* P2. We want loader_store to be cleaned up before the provider store */ |
171 | 3 | ctx->store_loader_store = ossl_method_store_new(ctx); |
172 | 3 | if (ctx->store_loader_store == NULL) |
173 | 0 | goto err; |
174 | 3 | #endif |
175 | | |
176 | | /* P1. Needs to be freed before the child provider data is freed */ |
177 | 3 | ctx->provider_store = ossl_provider_store_new(ctx); |
178 | 3 | if (ctx->provider_store == NULL) |
179 | 0 | goto err; |
180 | | |
181 | | /* Default priority. */ |
182 | 3 | ctx->property_string_data = ossl_property_string_data_new(ctx); |
183 | 3 | if (ctx->property_string_data == NULL) |
184 | 0 | goto err; |
185 | | |
186 | 3 | ctx->namemap = ossl_stored_namemap_new(ctx); |
187 | 3 | if (ctx->namemap == NULL) |
188 | 0 | goto err; |
189 | | |
190 | 3 | ctx->property_defns = ossl_property_defns_new(ctx); |
191 | 3 | if (ctx->property_defns == NULL) |
192 | 0 | goto err; |
193 | | |
194 | 3 | ctx->global_properties = ossl_ctx_global_properties_new(ctx); |
195 | 3 | if (ctx->global_properties == NULL) |
196 | 0 | goto err; |
197 | | |
198 | 3 | #ifndef FIPS_MODULE |
199 | 3 | ctx->bio_core = ossl_bio_core_globals_new(ctx); |
200 | 3 | if (ctx->bio_core == NULL) |
201 | 0 | goto err; |
202 | 3 | #endif |
203 | | |
204 | 3 | ctx->drbg_nonce = ossl_prov_drbg_nonce_ctx_new(ctx); |
205 | 3 | if (ctx->drbg_nonce == NULL) |
206 | 0 | goto err; |
207 | | |
208 | 3 | #ifndef FIPS_MODULE |
209 | 3 | ctx->self_test_cb = ossl_self_test_set_callback_new(ctx); |
210 | 3 | if (ctx->self_test_cb == NULL) |
211 | 0 | goto err; |
212 | 3 | ctx->indicator_cb = ossl_indicator_set_callback_new(ctx); |
213 | 3 | if (ctx->indicator_cb == NULL) |
214 | 0 | goto err; |
215 | 3 | #endif |
216 | | |
217 | | #ifdef FIPS_MODULE |
218 | | if (!ossl_thread_event_ctx_new(ctx)) |
219 | | goto err; |
220 | | |
221 | | ctx->fips_prov = ossl_fips_prov_ossl_ctx_new(ctx); |
222 | | if (ctx->fips_prov == NULL) |
223 | | goto err; |
224 | | #endif |
225 | | |
226 | 3 | #ifndef OPENSSL_NO_THREAD_POOL |
227 | 3 | ctx->threads = ossl_threads_ctx_new(ctx); |
228 | 3 | if (ctx->threads == NULL) |
229 | 0 | goto err; |
230 | 3 | #endif |
231 | | |
232 | | /* Low priority. */ |
233 | 3 | #ifndef FIPS_MODULE |
234 | 3 | ctx->child_provider = ossl_child_prov_ctx_new(ctx); |
235 | 3 | if (ctx->child_provider == NULL) |
236 | 0 | goto err; |
237 | 3 | #endif |
238 | | |
239 | | /* Everything depends on properties, so we also pre-initialise that */ |
240 | 3 | if (!ossl_property_parse_init(ctx)) |
241 | 0 | goto err; |
242 | | |
243 | 3 | #ifndef FIPS_MODULE |
244 | 3 | ctx->comp_methods = ossl_load_builtin_compressions(); |
245 | 3 | #endif |
246 | | |
247 | 3 | return 1; |
248 | | |
249 | 0 | err: |
250 | 0 | context_deinit_objs(ctx); |
251 | |
|
252 | 0 | if (exdata_done) |
253 | 0 | ossl_crypto_cleanup_all_ex_data_int(ctx); |
254 | |
|
255 | 0 | CRYPTO_THREAD_lock_free(ctx->lock); |
256 | 0 | memset(ctx, '\0', sizeof(*ctx)); |
257 | 0 | return 0; |
258 | 3 | } |
259 | | |
260 | | static void context_deinit_objs(OSSL_LIB_CTX *ctx) |
261 | 0 | { |
262 | | /* P2. */ |
263 | 0 | if (ctx->drbg != NULL) { |
264 | 0 | ossl_rand_ctx_free(ctx->drbg); |
265 | 0 | ctx->drbg = NULL; |
266 | 0 | } |
267 | |
|
268 | 0 | #ifndef FIPS_MODULE |
269 | | /* P2. */ |
270 | 0 | if (ctx->provider_conf != NULL) { |
271 | 0 | ossl_prov_conf_ctx_free(ctx->provider_conf); |
272 | 0 | ctx->provider_conf = NULL; |
273 | 0 | } |
274 | | |
275 | | /* |
276 | | * P2. We want decoder_store/decoder_cache to be cleaned up before the |
277 | | * provider store |
278 | | */ |
279 | 0 | if (ctx->decoder_cache != NULL) { |
280 | 0 | ossl_decoder_cache_free(ctx->decoder_cache); |
281 | 0 | ctx->decoder_cache = NULL; |
282 | 0 | } |
283 | 0 | if (ctx->decoder_store != NULL) { |
284 | 0 | ossl_method_store_free(ctx->decoder_store); |
285 | 0 | ctx->decoder_store = NULL; |
286 | 0 | } |
287 | | |
288 | | /* P2. We want encoder_store to be cleaned up before the provider store */ |
289 | 0 | if (ctx->encoder_store != NULL) { |
290 | 0 | ossl_method_store_free(ctx->encoder_store); |
291 | 0 | ctx->encoder_store = NULL; |
292 | 0 | } |
293 | | |
294 | | /* P2. We want loader_store to be cleaned up before the provider store */ |
295 | 0 | if (ctx->store_loader_store != NULL) { |
296 | 0 | ossl_method_store_free(ctx->store_loader_store); |
297 | 0 | ctx->store_loader_store = NULL; |
298 | 0 | } |
299 | 0 | #endif |
300 | | |
301 | | /* P1. Needs to be freed before the child provider data is freed */ |
302 | 0 | if (ctx->provider_store != NULL) { |
303 | 0 | ossl_provider_store_free(ctx->provider_store); |
304 | 0 | ctx->provider_store = NULL; |
305 | 0 | } |
306 | | |
307 | | /* P2. We want evp_method_store to be cleaned up before the provider store */ |
308 | 0 | if (ctx->evp_method_store != NULL) { |
309 | 0 | ossl_method_store_free(ctx->evp_method_store); |
310 | 0 | ctx->evp_method_store = NULL; |
311 | 0 | } |
312 | | |
313 | | /* Default priority. */ |
314 | 0 | if (ctx->property_string_data != NULL) { |
315 | 0 | ossl_property_string_data_free(ctx->property_string_data); |
316 | 0 | ctx->property_string_data = NULL; |
317 | 0 | } |
318 | |
|
319 | 0 | if (ctx->namemap != NULL) { |
320 | 0 | ossl_stored_namemap_free(ctx->namemap); |
321 | 0 | ctx->namemap = NULL; |
322 | 0 | } |
323 | |
|
324 | 0 | if (ctx->property_defns != NULL) { |
325 | 0 | ossl_property_defns_free(ctx->property_defns); |
326 | 0 | ctx->property_defns = NULL; |
327 | 0 | } |
328 | |
|
329 | 0 | if (ctx->global_properties != NULL) { |
330 | 0 | ossl_ctx_global_properties_free(ctx->global_properties); |
331 | 0 | ctx->global_properties = NULL; |
332 | 0 | } |
333 | |
|
334 | 0 | #ifndef FIPS_MODULE |
335 | 0 | if (ctx->bio_core != NULL) { |
336 | 0 | ossl_bio_core_globals_free(ctx->bio_core); |
337 | 0 | ctx->bio_core = NULL; |
338 | 0 | } |
339 | 0 | #endif |
340 | |
|
341 | 0 | if (ctx->drbg_nonce != NULL) { |
342 | 0 | ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce); |
343 | 0 | ctx->drbg_nonce = NULL; |
344 | 0 | } |
345 | |
|
346 | 0 | #ifndef FIPS_MODULE |
347 | 0 | if (ctx->indicator_cb != NULL) { |
348 | 0 | ossl_indicator_set_callback_free(ctx->indicator_cb); |
349 | 0 | ctx->indicator_cb = NULL; |
350 | 0 | } |
351 | |
|
352 | 0 | if (ctx->self_test_cb != NULL) { |
353 | 0 | ossl_self_test_set_callback_free(ctx->self_test_cb); |
354 | 0 | ctx->self_test_cb = NULL; |
355 | 0 | } |
356 | 0 | #endif |
357 | |
|
358 | | #ifdef FIPS_MODULE |
359 | | ossl_thread_event_ctx_free(ctx); |
360 | | |
361 | | if (ctx->fips_prov != NULL) { |
362 | | ossl_fips_prov_ossl_ctx_free(ctx->fips_prov); |
363 | | ctx->fips_prov = NULL; |
364 | | } |
365 | | #endif |
366 | |
|
367 | 0 | #ifndef OPENSSL_NO_THREAD_POOL |
368 | 0 | if (ctx->threads != NULL) { |
369 | 0 | ossl_threads_ctx_free(ctx->threads); |
370 | 0 | ctx->threads = NULL; |
371 | 0 | } |
372 | 0 | #endif |
373 | | |
374 | | /* Low priority. */ |
375 | 0 | #ifndef FIPS_MODULE |
376 | 0 | if (ctx->child_provider != NULL) { |
377 | 0 | ossl_child_prov_ctx_free(ctx->child_provider); |
378 | 0 | ctx->child_provider = NULL; |
379 | 0 | } |
380 | 0 | #endif |
381 | |
|
382 | 0 | #ifndef FIPS_MODULE |
383 | 0 | if (ctx->comp_methods != NULL) { |
384 | 0 | ossl_free_compression_methods_int(ctx->comp_methods); |
385 | 0 | ctx->comp_methods = NULL; |
386 | 0 | } |
387 | 0 | #endif |
388 | 0 | } |
389 | | |
390 | | static int context_deinit(OSSL_LIB_CTX *ctx) |
391 | 0 | { |
392 | 0 | if (ctx == NULL) |
393 | 0 | return 1; |
394 | | |
395 | 0 | ossl_lib_ctx_detach_ssl_conf_imodule(ctx, NULL); |
396 | |
|
397 | 0 | ossl_ctx_thread_stop(ctx); |
398 | |
|
399 | 0 | context_deinit_objs(ctx); |
400 | |
|
401 | 0 | ossl_crypto_cleanup_all_ex_data_int(ctx); |
402 | |
|
403 | 0 | CRYPTO_THREAD_lock_free(ctx->lock); |
404 | 0 | ctx->lock = NULL; |
405 | 0 | return 1; |
406 | 0 | } |
407 | | |
408 | | #ifndef FIPS_MODULE |
409 | | /* The default default context */ |
410 | | static OSSL_LIB_CTX default_context_int; |
411 | | |
412 | | static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT; |
413 | | static CRYPTO_ONCE default_context_thread_key_init = CRYPTO_ONCE_STATIC_INIT; |
414 | | static CRYPTO_THREAD_LOCAL default_context_thread_local; |
415 | | static int default_context_inited = 0; |
416 | | |
417 | | DEFINE_RUN_ONCE_STATIC(default_context_do_thread_key_init) |
418 | 1 | { |
419 | 1 | if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)) |
420 | 0 | return 0; |
421 | 1 | return 1; |
422 | 1 | } |
423 | | |
424 | | DEFINE_RUN_ONCE_STATIC(default_context_do_init) |
425 | 1 | { |
426 | 1 | if (!context_init(&default_context_int)) |
427 | 0 | return 0; |
428 | | |
429 | 1 | default_context_inited = 1; |
430 | 1 | return 1; |
431 | 1 | } |
432 | | |
433 | | void ossl_lib_ctx_default_deinit(void) |
434 | 0 | { |
435 | 0 | if (!default_context_inited) |
436 | 0 | return; |
437 | 0 | context_deinit(&default_context_int); |
438 | 0 | CRYPTO_THREAD_cleanup_local(&default_context_thread_local); |
439 | 0 | default_context_inited = 0; |
440 | 0 | } |
441 | | |
442 | | static OSSL_LIB_CTX *get_thread_default_context(void) |
443 | 1.02k | { |
444 | 1.02k | if (!RUN_ONCE(&default_context_thread_key_init, default_context_do_thread_key_init)) |
445 | 0 | return NULL; |
446 | | |
447 | 1.02k | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
448 | 0 | return NULL; |
449 | | |
450 | 1.02k | return CRYPTO_THREAD_get_local(&default_context_thread_local); |
451 | 1.02k | } |
452 | | |
453 | | static OSSL_LIB_CTX *check_thread_default_context(void) |
454 | 0 | { |
455 | 0 | if (!RUN_ONCE(&default_context_thread_key_init, default_context_do_thread_key_init)) |
456 | 0 | return NULL; |
457 | | |
458 | 0 | return CRYPTO_THREAD_get_local(&default_context_thread_local); |
459 | 0 | } |
460 | | |
461 | | static OSSL_LIB_CTX *get_default_context(void) |
462 | 1.02k | { |
463 | 1.02k | OSSL_LIB_CTX *current_defctx = get_thread_default_context(); |
464 | | |
465 | 1.02k | if (current_defctx == NULL && default_context_inited) |
466 | 1.02k | current_defctx = &default_context_int; |
467 | 1.02k | return current_defctx; |
468 | 1.02k | } |
469 | | |
470 | | static OSSL_LIB_CTX *check_default_context(void) |
471 | 0 | { |
472 | 0 | OSSL_LIB_CTX *current_defctx = check_thread_default_context(); |
473 | |
|
474 | 0 | if (current_defctx == NULL && default_context_inited) |
475 | 0 | current_defctx = &default_context_int; |
476 | 0 | return current_defctx; |
477 | 0 | } |
478 | | |
479 | | static int set_default_context(OSSL_LIB_CTX *defctx) |
480 | 0 | { |
481 | 0 | if (defctx == &default_context_int) |
482 | 0 | defctx = NULL; |
483 | |
|
484 | 0 | return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx); |
485 | 0 | } |
486 | | #endif |
487 | | |
488 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new(void) |
489 | 2 | { |
490 | 2 | OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
491 | | |
492 | 2 | if (ctx != NULL && !context_init(ctx)) { |
493 | 0 | OPENSSL_free(ctx); |
494 | 0 | ctx = NULL; |
495 | 0 | } |
496 | 2 | return ctx; |
497 | 2 | } |
498 | | |
499 | | #ifndef FIPS_MODULE |
500 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle, |
501 | | const OSSL_DISPATCH *in) |
502 | 1 | { |
503 | 1 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); |
504 | | |
505 | 1 | if (ctx == NULL) |
506 | 0 | return NULL; |
507 | | |
508 | 1 | if (!ossl_bio_init_core(ctx, in)) { |
509 | 0 | OSSL_LIB_CTX_free(ctx); |
510 | 0 | return NULL; |
511 | 0 | } |
512 | | |
513 | 1 | return ctx; |
514 | 1 | } |
515 | | |
516 | | OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle, |
517 | | const OSSL_DISPATCH *in) |
518 | 1 | { |
519 | 1 | OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in); |
520 | | |
521 | 1 | if (ctx == NULL) |
522 | 0 | return NULL; |
523 | | |
524 | 1 | if (!ossl_provider_init_as_child(ctx, handle, in)) { |
525 | 0 | OSSL_LIB_CTX_free(ctx); |
526 | 0 | return NULL; |
527 | 0 | } |
528 | 1 | ctx->ischild = 1; |
529 | | |
530 | 1 | return ctx; |
531 | 1 | } |
532 | | |
533 | | int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file) |
534 | 0 | { |
535 | 0 | return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0; |
536 | 0 | } |
537 | | #endif |
538 | | |
539 | | void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx) |
540 | 0 | { |
541 | 0 | if (ctx == NULL || ossl_lib_ctx_is_default_nocreate(ctx)) |
542 | 0 | return; |
543 | | |
544 | 0 | #ifndef FIPS_MODULE |
545 | 0 | if (ctx->ischild) |
546 | 0 | ossl_provider_deinit_child(ctx); |
547 | 0 | #endif |
548 | 0 | context_deinit(ctx); |
549 | 0 | OPENSSL_free(ctx); |
550 | 0 | } |
551 | | |
552 | | #ifndef FIPS_MODULE |
553 | | OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void) |
554 | 0 | { |
555 | 0 | if (!RUN_ONCE(&default_context_thread_key_init, default_context_do_thread_key_init)) |
556 | 0 | return NULL; |
557 | | |
558 | 0 | if (!RUN_ONCE(&default_context_init, default_context_do_init)) |
559 | 0 | return NULL; |
560 | | |
561 | 0 | return &default_context_int; |
562 | 0 | } |
563 | | |
564 | | OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx) |
565 | 0 | { |
566 | 0 | OSSL_LIB_CTX *current_defctx; |
567 | |
|
568 | 0 | if ((current_defctx = get_default_context()) != NULL) { |
569 | 0 | if (libctx != NULL) |
570 | 0 | set_default_context(libctx); |
571 | 0 | return current_defctx; |
572 | 0 | } |
573 | | |
574 | 0 | return NULL; |
575 | 0 | } |
576 | | |
577 | | void ossl_release_default_drbg_ctx(void) |
578 | 0 | { |
579 | | /* early release of the DRBG in global default libctx */ |
580 | 0 | if (default_context_int.drbg != NULL) { |
581 | 0 | ossl_rand_ctx_free(default_context_int.drbg); |
582 | 0 | default_context_int.drbg = NULL; |
583 | 0 | } |
584 | 0 | } |
585 | | #endif |
586 | | |
587 | | OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx) |
588 | 11.4k | { |
589 | 11.4k | #ifndef FIPS_MODULE |
590 | 11.4k | if (ctx == NULL) |
591 | 0 | return get_default_context(); |
592 | 11.4k | #endif |
593 | 11.4k | return ctx; |
594 | 11.4k | } |
595 | | |
596 | | int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx) |
597 | 1.02k | { |
598 | 1.02k | #ifndef FIPS_MODULE |
599 | 1.02k | if (ctx == NULL || ctx == get_default_context()) |
600 | 0 | return 1; |
601 | 1.02k | #endif |
602 | 1.02k | return 0; |
603 | 1.02k | } |
604 | | |
605 | | int ossl_lib_ctx_is_default_nocreate(OSSL_LIB_CTX *ctx) |
606 | 0 | { |
607 | 0 | #ifndef FIPS_MODULE |
608 | 0 | if (ctx == NULL || ctx == check_default_context()) |
609 | 0 | return 1; |
610 | 0 | #endif |
611 | 0 | return 0; |
612 | 0 | } |
613 | | |
614 | | int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx) |
615 | 500 | { |
616 | 500 | #ifndef FIPS_MODULE |
617 | 500 | if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int) |
618 | 0 | return 1; |
619 | 500 | #endif |
620 | 500 | return 0; |
621 | 500 | } |
622 | | |
623 | | void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index) |
624 | 9.99k | { |
625 | 9.99k | ctx = ossl_lib_ctx_get_concrete(ctx); |
626 | 9.99k | if (ctx == NULL) |
627 | 0 | return NULL; |
628 | | |
629 | 9.99k | switch (index) { |
630 | 28 | case OSSL_LIB_CTX_PROPERTY_STRING_INDEX: |
631 | 28 | return ctx->property_string_data; |
632 | 4.92k | case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX: |
633 | 4.92k | return ctx->evp_method_store; |
634 | 506 | case OSSL_LIB_CTX_PROVIDER_STORE_INDEX: |
635 | 506 | return ctx->provider_store; |
636 | 4.29k | case OSSL_LIB_CTX_NAMEMAP_INDEX: |
637 | 4.29k | return ctx->namemap; |
638 | 195 | case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX: |
639 | 195 | return ctx->property_defns; |
640 | 30 | case OSSL_LIB_CTX_GLOBAL_PROPERTIES: |
641 | 30 | return ctx->global_properties; |
642 | 0 | case OSSL_LIB_CTX_DRBG_INDEX: |
643 | 0 | return ctx->drbg; |
644 | 0 | case OSSL_LIB_CTX_DRBG_NONCE_INDEX: |
645 | 0 | return ctx->drbg_nonce; |
646 | 0 | #ifndef FIPS_MODULE |
647 | 0 | case OSSL_LIB_CTX_PROVIDER_CONF_INDEX: |
648 | 0 | return ctx->provider_conf; |
649 | 1 | case OSSL_LIB_CTX_BIO_CORE_INDEX: |
650 | 1 | return ctx->bio_core; |
651 | 5 | case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX: |
652 | 5 | return ctx->child_provider; |
653 | 4 | case OSSL_LIB_CTX_DECODER_STORE_INDEX: |
654 | 4 | return ctx->decoder_store; |
655 | 5 | case OSSL_LIB_CTX_DECODER_CACHE_INDEX: |
656 | 5 | return ctx->decoder_cache; |
657 | 4 | case OSSL_LIB_CTX_ENCODER_STORE_INDEX: |
658 | 4 | return ctx->encoder_store; |
659 | 4 | case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX: |
660 | 4 | return ctx->store_loader_store; |
661 | 0 | case OSSL_LIB_CTX_SELF_TEST_CB_INDEX: |
662 | 0 | return ctx->self_test_cb; |
663 | 0 | case OSSL_LIB_CTX_INDICATOR_CB_INDEX: |
664 | 0 | return ctx->indicator_cb; |
665 | 0 | #endif |
666 | 0 | #ifndef OPENSSL_NO_THREAD_POOL |
667 | 2 | case OSSL_LIB_CTX_THREAD_INDEX: |
668 | 2 | return ctx->threads; |
669 | 0 | #endif |
670 | | |
671 | | #ifdef FIPS_MODULE |
672 | | case OSSL_LIB_CTX_FIPS_PROV_INDEX: |
673 | | return ctx->fips_prov; |
674 | | #endif |
675 | | |
676 | 0 | case OSSL_LIB_CTX_COMP_METHODS: |
677 | 0 | return (void *)&ctx->comp_methods; |
678 | | |
679 | 0 | case OSSL_LIB_CTX_SSL_CONF_IMODULE: |
680 | 0 | return (void *)ctx->ssl_imod; |
681 | | |
682 | 0 | default: |
683 | 0 | return NULL; |
684 | 9.99k | } |
685 | 9.99k | } |
686 | | |
687 | | int ossl_lib_ctx_get_new_providers_loaded(OSSL_LIB_CTX *ctx, int op) |
688 | 513 | { |
689 | 513 | uint64_t ret = 0; |
690 | | |
691 | 513 | ctx = ossl_lib_ctx_get_concrete(ctx); |
692 | | |
693 | | /* |
694 | | * if the atomic read operation fails here, return 1 to pretend like new |
695 | | * providers are loaded, it ensures that we behave as though there are |
696 | | * new providers loaded if we fail. |
697 | | */ |
698 | 513 | if (!CRYPTO_atomic_load(&ctx->new_provider_loaded, &ret, ctx->lock)) |
699 | 0 | return 1; |
700 | 513 | return !!(ret & (1ULL << op)); |
701 | 513 | } |
702 | | |
703 | | int ossl_lib_ctx_set_new_providers_loaded(OSSL_LIB_CTX *ctx) |
704 | 4 | { |
705 | 4 | ctx = ossl_lib_ctx_get_concrete(ctx); |
706 | | |
707 | 4 | if (!CRYPTO_atomic_store(&ctx->new_provider_loaded, UINT64_MAX, ctx->lock)) |
708 | 0 | return 0; |
709 | 4 | return 1; |
710 | 4 | } |
711 | | |
712 | | int ossl_lib_ctx_clear_new_providers_loaded(OSSL_LIB_CTX *ctx, int op) |
713 | 2 | { |
714 | 2 | uint64_t val = ~(1ULL << op); |
715 | 2 | uint64_t ret; |
716 | | |
717 | 2 | ctx = ossl_lib_ctx_get_concrete(ctx); |
718 | | |
719 | 2 | if (!CRYPTO_atomic_and(&ctx->new_provider_loaded, val, &ret, ctx->lock)) |
720 | 0 | return 0; |
721 | 2 | return 1; |
722 | 2 | } |
723 | | |
724 | | void *OSSL_LIB_CTX_get_data(OSSL_LIB_CTX *ctx, int index) |
725 | 0 | { |
726 | 0 | return ossl_lib_ctx_get_data(ctx, index); |
727 | 0 | } |
728 | | |
729 | | OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx) |
730 | 3 | { |
731 | 3 | ctx = ossl_lib_ctx_get_concrete(ctx); |
732 | 3 | if (ctx == NULL) |
733 | 0 | return NULL; |
734 | 3 | return &ctx->global; |
735 | 3 | } |
736 | | |
737 | | const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx) |
738 | 500 | { |
739 | | #ifdef FIPS_MODULE |
740 | | return "FIPS internal library context"; |
741 | | #else |
742 | 500 | if (ossl_lib_ctx_is_global_default(libctx)) |
743 | 0 | return "Global default library context"; |
744 | 500 | if (ossl_lib_ctx_is_default(libctx)) |
745 | 0 | return "Thread-local default library context"; |
746 | 500 | return "Non-default library context"; |
747 | 500 | #endif |
748 | 500 | } |
749 | | |
750 | | int OSSL_LIB_CTX_get_conf_diagnostics(OSSL_LIB_CTX *libctx) |
751 | 0 | { |
752 | 0 | libctx = ossl_lib_ctx_get_concrete(libctx); |
753 | 0 | if (libctx == NULL) |
754 | 0 | return 0; |
755 | 0 | return libctx->conf_diagnostics; |
756 | 0 | } |
757 | | |
758 | | void OSSL_LIB_CTX_set_conf_diagnostics(OSSL_LIB_CTX *libctx, int value) |
759 | 0 | { |
760 | 0 | libctx = ossl_lib_ctx_get_concrete(libctx); |
761 | 0 | if (libctx == NULL) |
762 | 0 | return; |
763 | 0 | libctx->conf_diagnostics = value; |
764 | 0 | } |