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