Coverage Report

Created: 2023-06-08 06:40

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