Coverage Report

Created: 2024-07-27 06:35

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