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