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