Coverage Report

Created: 2024-07-27 06:39

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