Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/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/ctype.h"
18
#include "crypto/rand.h"
19
20
struct ossl_lib_ctx_onfree_list_st {
21
    ossl_lib_ctx_onfree_fn *fn;
22
    struct ossl_lib_ctx_onfree_list_st *next;
23
};
24
25
struct ossl_lib_ctx_st {
26
    CRYPTO_RWLOCK *lock;
27
    CRYPTO_EX_DATA data;
28
29
    /*
30
     * For most data in the OSSL_LIB_CTX we just use ex_data to store it. But
31
     * that doesn't work for ex_data itself - so we store that directly.
32
     */
33
    OSSL_EX_DATA_GLOBAL global;
34
35
    /* Map internal static indexes to dynamically created indexes */
36
    int dyn_indexes[OSSL_LIB_CTX_MAX_INDEXES];
37
38
    /* Keep a separate lock for each index */
39
    CRYPTO_RWLOCK *index_locks[OSSL_LIB_CTX_MAX_INDEXES];
40
41
    CRYPTO_RWLOCK *oncelock;
42
    int run_once_done[OSSL_LIB_CTX_MAX_RUN_ONCE];
43
    int run_once_ret[OSSL_LIB_CTX_MAX_RUN_ONCE];
44
    struct ossl_lib_ctx_onfree_list_st *onfreelist;
45
    unsigned int ischild:1;
46
};
47
48
int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx)
49
176
{
50
176
    return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
51
176
}
52
53
int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx)
54
1.24k
{
55
1.24k
    return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
56
1.24k
}
57
58
int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx)
59
1.41k
{
60
1.41k
    return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock);
61
1.41k
}
62
63
int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx)
64
0
{
65
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
66
67
0
    if (ctx == NULL)
68
0
        return 0;
69
0
    return ctx->ischild;
70
0
}
71
72
static int context_init(OSSL_LIB_CTX *ctx)
73
34
{
74
34
    size_t i;
75
34
    int exdata_done = 0;
76
77
34
    ctx->lock = CRYPTO_THREAD_lock_new();
78
34
    if (ctx->lock == NULL)
79
0
        return 0;
80
81
34
    ctx->oncelock = CRYPTO_THREAD_lock_new();
82
34
    if (ctx->oncelock == NULL)
83
0
        goto err;
84
85
680
    for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++) {
86
646
        ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
87
646
        ctx->dyn_indexes[i] = -1;
88
646
        if (ctx->index_locks[i] == NULL)
89
0
            goto err;
90
646
    }
91
92
    /* OSSL_LIB_CTX is built on top of ex_data so we initialise that directly */
93
34
    if (!ossl_do_ex_data_init(ctx))
94
0
        goto err;
95
34
    exdata_done = 1;
96
97
34
    if (!ossl_crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
98
34
                                    &ctx->data))
99
0
        goto err;
100
101
    /* Everything depends on properties, so we also pre-initialise that */
102
34
    if (!ossl_property_parse_init(ctx))
103
0
        goto err;
104
105
34
    return 1;
106
0
 err:
107
0
    if (exdata_done)
108
0
        ossl_crypto_cleanup_all_ex_data_int(ctx);
109
0
    for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
110
0
        CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
111
0
    CRYPTO_THREAD_lock_free(ctx->oncelock);
112
0
    CRYPTO_THREAD_lock_free(ctx->lock);
113
0
    memset(ctx, '\0', sizeof(*ctx));
114
0
    return 0;
115
34
}
116
117
static int context_deinit(OSSL_LIB_CTX *ctx)
118
24
{
119
24
    struct ossl_lib_ctx_onfree_list_st *tmp, *onfree;
120
24
    int i;
121
122
24
    if (ctx == NULL)
123
0
        return 1;
124
125
24
    ossl_ctx_thread_stop(ctx);
126
127
24
    onfree = ctx->onfreelist;
128
24
    while (onfree != NULL) {
129
0
        onfree->fn(ctx);
130
0
        tmp = onfree;
131
0
        onfree = onfree->next;
132
0
        OPENSSL_free(tmp);
133
0
    }
134
24
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL, &ctx->data);
135
24
    ossl_crypto_cleanup_all_ex_data_int(ctx);
136
480
    for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
137
456
        CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
138
139
24
    CRYPTO_THREAD_lock_free(ctx->oncelock);
140
24
    CRYPTO_THREAD_lock_free(ctx->lock);
141
24
    ctx->lock = NULL;
142
24
    return 1;
143
24
}
144
145
#ifndef FIPS_MODULE
146
/* The default default context */
147
static OSSL_LIB_CTX default_context_int;
148
149
static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
150
static CRYPTO_THREAD_LOCAL default_context_thread_local;
151
152
DEFINE_RUN_ONCE_STATIC(default_context_do_init)
153
24
{
154
24
    return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
155
24
        && context_init(&default_context_int);
156
24
}
157
158
void ossl_lib_ctx_default_deinit(void)
159
24
{
160
24
    context_deinit(&default_context_int);
161
24
    CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
162
24
}
163
164
static OSSL_LIB_CTX *get_thread_default_context(void)
165
90.0M
{
166
90.0M
    if (!RUN_ONCE(&default_context_init, default_context_do_init))
167
0
        return NULL;
168
169
90.0M
    return CRYPTO_THREAD_get_local(&default_context_thread_local);
170
90.0M
}
171
172
static OSSL_LIB_CTX *get_default_context(void)
173
21.0M
{
174
21.0M
    OSSL_LIB_CTX *current_defctx = get_thread_default_context();
175
176
21.0M
    if (current_defctx == NULL)
177
21.0M
        current_defctx = &default_context_int;
178
21.0M
    return current_defctx;
179
21.0M
}
180
181
static int set_default_context(OSSL_LIB_CTX *defctx)
182
0
{
183
0
    if (defctx == &default_context_int)
184
0
        defctx = NULL;
185
186
0
    return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
187
0
}
188
#endif
189
190
OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
191
62
{
192
62
    OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
193
194
62
    if (ctx != NULL && !context_init(ctx)) {
195
0
        OPENSSL_free(ctx);
196
0
        ctx = NULL;
197
0
    }
198
62
    return ctx;
199
62
}
200
201
#ifndef FIPS_MODULE
202
OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
203
                                             const OSSL_DISPATCH *in)
204
0
{
205
0
    OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
206
207
0
    if (ctx == NULL)
208
0
        return NULL;
209
210
0
    if (!ossl_bio_init_core(ctx, in)) {
211
0
        OSSL_LIB_CTX_free(ctx);
212
0
        return NULL;
213
0
    }
214
215
0
    return ctx;
216
0
}
217
218
OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
219
                                     const OSSL_DISPATCH *in)
220
0
{
221
0
    OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
222
223
0
    if (ctx == NULL)
224
0
        return NULL;
225
226
0
    if (!ossl_provider_init_as_child(ctx, handle, in)) {
227
0
        OSSL_LIB_CTX_free(ctx);
228
0
        return NULL;
229
0
    }
230
0
    ctx->ischild = 1;
231
232
0
    return ctx;
233
0
}
234
235
int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
236
0
{
237
0
    return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
238
0
}
239
#endif
240
241
void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
242
0
{
243
0
    if (ctx == NULL || ossl_lib_ctx_is_default(ctx))
244
0
        return;
245
246
0
#ifndef FIPS_MODULE
247
0
    if (ctx->ischild)
248
0
        ossl_provider_deinit_child(ctx);
249
0
#endif
250
0
    context_deinit(ctx);
251
0
    OPENSSL_free(ctx);
252
0
}
253
254
#ifndef FIPS_MODULE
255
OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
256
82
{
257
82
    if (!RUN_ONCE(&default_context_init, default_context_do_init))
258
0
        return NULL;
259
260
82
    return &default_context_int;
261
82
}
262
263
OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
264
79.3k
{
265
79.3k
    OSSL_LIB_CTX *current_defctx;
266
267
79.3k
    if ((current_defctx = get_default_context()) != NULL) {
268
79.3k
        if (libctx != NULL)
269
0
            set_default_context(libctx);
270
79.3k
        return current_defctx;
271
79.3k
    }
272
273
0
    return NULL;
274
79.3k
}
275
276
void ossl_release_default_drbg_ctx(void)
277
36
{
278
36
    int dynidx = default_context_int.dyn_indexes[OSSL_LIB_CTX_DRBG_INDEX];
279
280
    /* early release of the DRBG in global default libctx, no locking */
281
36
    if (dynidx != -1) {
282
36
        void *data;
283
284
36
        data = CRYPTO_get_ex_data(&default_context_int.data, dynidx);
285
36
        ossl_rand_ctx_free(data);
286
36
        CRYPTO_set_ex_data(&default_context_int.data, dynidx, NULL);
287
36
    }
288
36
}
289
#endif
290
291
OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
292
389M
{
293
389M
#ifndef FIPS_MODULE
294
389M
    if (ctx == NULL)
295
89.4M
        return get_default_context();
296
300M
#endif
297
300M
    return ctx;
298
389M
}
299
300
int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
301
3.37M
{
302
3.37M
#ifndef FIPS_MODULE
303
3.37M
    if (ctx == NULL || ctx == get_default_context())
304
3.36M
        return 1;
305
5.75k
#endif
306
5.75k
    return 0;
307
3.37M
}
308
309
int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
310
1.27M
{
311
1.27M
#ifndef FIPS_MODULE
312
1.27M
    if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
313
1.27M
        return 1;
314
882
#endif
315
882
    return 0;
316
1.27M
}
317
318
static void ossl_lib_ctx_generic_new(void *parent_ign, void *ptr_ign,
319
                                     CRYPTO_EX_DATA *ad, int index,
320
                                     long argl_ign, void *argp)
321
121
{
322
121
    const OSSL_LIB_CTX_METHOD *meth = argp;
323
121
    OSSL_LIB_CTX *ctx = ossl_crypto_ex_data_get_ossl_lib_ctx(ad);
324
121
    void *ptr = meth->new_func(ctx);
325
326
121
    if (ptr != NULL) {
327
121
        if (!CRYPTO_THREAD_write_lock(ctx->lock))
328
            /*
329
             * Can't return something, so best to hope that something will
330
             * fail later. :(
331
             */
332
0
            return;
333
121
        CRYPTO_set_ex_data(ad, index, ptr);
334
121
        CRYPTO_THREAD_unlock(ctx->lock);
335
121
    }
336
121
}
337
static void ossl_lib_ctx_generic_free(void *parent_ign, void *ptr,
338
                                      CRYPTO_EX_DATA *ad, int index,
339
                                      long argl_ign, void *argp)
340
111
{
341
111
    const OSSL_LIB_CTX_METHOD *meth = argp;
342
343
111
    meth->free_func(ptr);
344
111
}
345
346
static int ossl_lib_ctx_init_index(OSSL_LIB_CTX *ctx, int static_index,
347
                                   const OSSL_LIB_CTX_METHOD *meth)
348
121
{
349
121
    int idx;
350
351
121
    ctx = ossl_lib_ctx_get_concrete(ctx);
352
121
    if (ctx == NULL)
353
0
        return 0;
354
355
121
    idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
356
121
                                          (void *)meth,
357
121
                                          ossl_lib_ctx_generic_new,
358
121
                                          NULL, ossl_lib_ctx_generic_free,
359
121
                                          meth->priority);
360
121
    if (idx < 0)
361
0
        return 0;
362
363
121
    ctx->dyn_indexes[static_index] = idx;
364
121
    return 1;
365
121
}
366
367
void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
368
                            const OSSL_LIB_CTX_METHOD *meth)
369
255M
{
370
255M
    void *data = NULL;
371
255M
    int dynidx;
372
373
255M
    ctx = ossl_lib_ctx_get_concrete(ctx);
374
255M
    if (ctx == NULL)
375
0
        return NULL;
376
377
255M
    if (!CRYPTO_THREAD_read_lock(ctx->lock))
378
0
        return NULL;
379
255M
    dynidx = ctx->dyn_indexes[index];
380
255M
    CRYPTO_THREAD_unlock(ctx->lock);
381
382
255M
    if (dynidx != -1) {
383
255M
        if (!CRYPTO_THREAD_read_lock(ctx->index_locks[index]))
384
0
            return NULL;
385
255M
        if (!CRYPTO_THREAD_read_lock(ctx->lock)) {
386
0
            CRYPTO_THREAD_unlock(ctx->index_locks[index]);
387
0
            return NULL;
388
0
        }
389
255M
        data = CRYPTO_get_ex_data(&ctx->data, dynidx);
390
255M
        CRYPTO_THREAD_unlock(ctx->lock);
391
255M
        CRYPTO_THREAD_unlock(ctx->index_locks[index]);
392
255M
        return data;
393
255M
    }
394
395
121
    if (!CRYPTO_THREAD_write_lock(ctx->index_locks[index]))
396
0
        return NULL;
397
121
    if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
398
0
        CRYPTO_THREAD_unlock(ctx->index_locks[index]);
399
0
        return NULL;
400
0
    }
401
402
121
    dynidx = ctx->dyn_indexes[index];
403
121
    if (dynidx != -1) {
404
0
        data = CRYPTO_get_ex_data(&ctx->data, dynidx);
405
0
        CRYPTO_THREAD_unlock(ctx->lock);
406
0
        CRYPTO_THREAD_unlock(ctx->index_locks[index]);
407
0
        return data;
408
0
    }
409
410
121
    if (!ossl_lib_ctx_init_index(ctx, index, meth)) {
411
0
        CRYPTO_THREAD_unlock(ctx->lock);
412
0
        CRYPTO_THREAD_unlock(ctx->index_locks[index]);
413
0
        return NULL;
414
0
    }
415
416
121
    CRYPTO_THREAD_unlock(ctx->lock);
417
418
    /*
419
     * The alloc call ensures there's a value there. We release the ctx->lock
420
     * for this, because the allocation itself may recursively call
421
     * ossl_lib_ctx_get_data for other indexes (never this one). The allocation
422
     * will itself aquire the ctx->lock when it actually comes to store the
423
     * allocated data (see ossl_lib_ctx_generic_new() above). We call
424
     * ossl_crypto_alloc_ex_data_intern() here instead of CRYPTO_alloc_ex_data().
425
     * They do the same thing except that the latter calls CRYPTO_get_ex_data()
426
     * as well - which we must not do without holding the ctx->lock.
427
     */
428
121
    if (ossl_crypto_alloc_ex_data_intern(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
429
121
                                         &ctx->data, ctx->dyn_indexes[index])) {
430
121
        if (!CRYPTO_THREAD_read_lock(ctx->lock))
431
0
            goto end;
432
121
        data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
433
121
        CRYPTO_THREAD_unlock(ctx->lock);
434
121
    }
435
436
121
end:
437
121
    CRYPTO_THREAD_unlock(ctx->index_locks[index]);
438
121
    return data;
439
121
}
440
441
OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
442
42.9M
{
443
42.9M
    ctx = ossl_lib_ctx_get_concrete(ctx);
444
42.9M
    if (ctx == NULL)
445
0
        return NULL;
446
42.9M
    return &ctx->global;
447
42.9M
}
448
449
int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
450
                          ossl_lib_ctx_run_once_fn run_once_fn)
451
0
{
452
0
    int done = 0, ret = 0;
453
454
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
455
0
    if (ctx == NULL)
456
0
        return 0;
457
458
0
    if (!CRYPTO_THREAD_read_lock(ctx->oncelock))
459
0
        return 0;
460
0
    done = ctx->run_once_done[idx];
461
0
    if (done)
462
0
        ret = ctx->run_once_ret[idx];
463
0
    CRYPTO_THREAD_unlock(ctx->oncelock);
464
465
0
    if (done)
466
0
        return ret;
467
468
0
    if (!CRYPTO_THREAD_write_lock(ctx->oncelock))
469
0
        return 0;
470
0
    if (ctx->run_once_done[idx]) {
471
0
        ret = ctx->run_once_ret[idx];
472
0
        CRYPTO_THREAD_unlock(ctx->oncelock);
473
0
        return ret;
474
0
    }
475
476
0
    ret = run_once_fn(ctx);
477
0
    ctx->run_once_done[idx] = 1;
478
0
    ctx->run_once_ret[idx] = ret;
479
0
    CRYPTO_THREAD_unlock(ctx->oncelock);
480
481
0
    return ret;
482
0
}
483
484
int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn)
485
0
{
486
0
    struct ossl_lib_ctx_onfree_list_st *newonfree
487
0
        = OPENSSL_malloc(sizeof(*newonfree));
488
489
0
    if (newonfree == NULL)
490
0
        return 0;
491
492
0
    newonfree->fn = onfreefn;
493
0
    newonfree->next = ctx->onfreelist;
494
0
    ctx->onfreelist = newonfree;
495
496
0
    return 1;
497
0
}
498
499
const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
500
1.27M
{
501
#ifdef FIPS_MODULE
502
    return "FIPS internal library context";
503
#else
504
1.27M
    if (ossl_lib_ctx_is_global_default(libctx))
505
1.27M
        return "Global default library context";
506
882
    if (ossl_lib_ctx_is_default(libctx))
507
0
        return "Thread-local default library context";
508
882
    return "Non-default library context";
509
882
#endif
510
882
}