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