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