Coverage Report

Created: 2024-05-04 01:12

/src/openssl/crypto/context.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019 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 "internal/thread_once.h"
12
#include "internal/property.h"
13
14
struct openssl_ctx_onfree_list_st {
15
    openssl_ctx_onfree_fn *fn;
16
    struct openssl_ctx_onfree_list_st *next;
17
};
18
19
struct openssl_ctx_st {
20
    CRYPTO_RWLOCK *lock;
21
    CRYPTO_EX_DATA data;
22
23
    /*
24
     * For most data in the OPENSSL_CTX we just use ex_data to store it. But
25
     * that doesn't work for ex_data itself - so we store that directly.
26
     */
27
    OSSL_EX_DATA_GLOBAL global;
28
29
    /* Map internal static indexes to dynamically created indexes */
30
    int dyn_indexes[OPENSSL_CTX_MAX_INDEXES];
31
32
    /* Keep a separate lock for each index */
33
    CRYPTO_RWLOCK *index_locks[OPENSSL_CTX_MAX_INDEXES];
34
35
    CRYPTO_RWLOCK *oncelock;
36
    int run_once_done[OPENSSL_CTX_MAX_RUN_ONCE];
37
    int run_once_ret[OPENSSL_CTX_MAX_RUN_ONCE];
38
    struct openssl_ctx_onfree_list_st *onfreelist;
39
};
40
41
#ifndef FIPS_MODE
42
static OPENSSL_CTX default_context_int;
43
44
/* Always points at default_context_int if it has been initialised */
45
static OPENSSL_CTX *default_context = NULL;
46
#endif
47
48
static int context_init(OPENSSL_CTX *ctx)
49
1.03k
{
50
1.03k
    size_t i;
51
1.03k
    int exdata_done = 0;
52
53
1.03k
    ctx->lock = CRYPTO_THREAD_lock_new();
54
1.03k
    if (ctx->lock == NULL)
55
0
        return 0;
56
57
1.03k
    ctx->oncelock = CRYPTO_THREAD_lock_new();
58
1.03k
    if (ctx->oncelock == NULL)
59
0
        goto err;
60
61
13.4k
    for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++) {
62
12.4k
        ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
63
12.4k
        ctx->dyn_indexes[i] = -1;
64
12.4k
        if (ctx->index_locks[i] == NULL)
65
0
            goto err;
66
12.4k
    }
67
68
    /* OPENSSL_CTX is built on top of ex_data so we initialise that directly */
69
1.03k
    if (!do_ex_data_init(ctx))
70
0
        goto err;
71
1.03k
    exdata_done = 1;
72
73
1.03k
    if (!crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
74
1.03k
                               &ctx->data)) {
75
0
        crypto_cleanup_all_ex_data_int(ctx);
76
0
        goto err;
77
0
    }
78
79
    /* Everything depends on properties, so we also pre-initialise that */
80
1.03k
    if (!ossl_property_parse_init(ctx))
81
0
        goto err;
82
83
1.03k
    return 1;
84
0
 err:
85
0
    if (exdata_done)
86
0
        crypto_cleanup_all_ex_data_int(ctx);
87
0
    CRYPTO_THREAD_lock_free(ctx->oncelock);
88
0
    CRYPTO_THREAD_lock_free(ctx->lock);
89
0
    ctx->lock = NULL;
90
0
    return 0;
91
1.03k
}
92
93
static int context_deinit(OPENSSL_CTX *ctx)
94
837
{
95
837
    struct openssl_ctx_onfree_list_st *tmp, *onfree;
96
837
    int i;
97
98
837
    if (ctx == NULL)
99
0
        return 1;
100
101
837
    ossl_ctx_thread_stop(ctx);
102
103
837
    onfree = ctx->onfreelist;
104
837
    while (onfree != NULL) {
105
0
        onfree->fn(ctx);
106
0
        tmp = onfree;
107
0
        onfree = onfree->next;
108
0
        OPENSSL_free(tmp);
109
0
    }
110
837
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
111
837
    crypto_cleanup_all_ex_data_int(ctx);
112
10.8k
    for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++)
113
10.0k
        CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
114
115
837
    CRYPTO_THREAD_lock_free(ctx->oncelock);
116
837
    CRYPTO_THREAD_lock_free(ctx->lock);
117
837
    ctx->lock = NULL;
118
837
    return 1;
119
837
}
120
121
#ifndef FIPS_MODE
122
void openssl_ctx_default_deinit(void)
123
837
{
124
837
    context_deinit(default_context);
125
837
}
126
127
static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
128
DEFINE_RUN_ONCE_STATIC(do_default_context_init)
129
1.03k
{
130
1.03k
    if (context_init(&default_context_int))
131
1.03k
        default_context = &default_context_int;
132
133
1.03k
    return 1;
134
1.03k
}
135
#endif
136
137
OPENSSL_CTX *OPENSSL_CTX_new(void)
138
0
{
139
0
    OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
140
141
0
    if (ctx != NULL && !context_init(ctx)) {
142
0
        OPENSSL_CTX_free(ctx);
143
0
        ctx = NULL;
144
0
    }
145
0
    return ctx;
146
0
}
147
148
void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
149
0
{
150
0
    if (ctx != NULL)
151
0
        context_deinit(ctx);
152
0
    OPENSSL_free(ctx);
153
0
}
154
155
OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx)
156
565k
{
157
565k
#ifndef FIPS_MODE
158
565k
    if (ctx == NULL) {
159
544k
        if (!RUN_ONCE(&default_context_init, do_default_context_init))
160
0
            return 0;
161
544k
        return default_context;
162
544k
    }
163
21.1k
#endif
164
21.1k
    return ctx;
165
565k
}
166
167
static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
168
                                    CRYPTO_EX_DATA *ad, int index,
169
                                    long argl_ign, void *argp)
170
1.03k
{
171
1.03k
    const OPENSSL_CTX_METHOD *meth = argp;
172
1.03k
    void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad));
173
174
1.03k
    if (ptr != NULL)
175
1.03k
        CRYPTO_set_ex_data(ad, index, ptr);
176
1.03k
}
177
static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
178
                                     CRYPTO_EX_DATA *ad, int index,
179
                                     long argl_ign, void *argp)
180
837
{
181
837
    const OPENSSL_CTX_METHOD *meth = argp;
182
183
837
    meth->free_func(ptr);
184
837
}
185
186
/* Non-static so we can use it in context_internal_test */
187
static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index,
188
                                  const OPENSSL_CTX_METHOD *meth)
189
1.03k
{
190
1.03k
    int idx;
191
192
1.03k
    ctx = openssl_ctx_get_concrete(ctx);
193
1.03k
    if (ctx == NULL)
194
0
        return 0;
195
196
1.03k
    idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0,
197
1.03k
                                     (void *)meth,
198
1.03k
                                     openssl_ctx_generic_new,
199
1.03k
                                     NULL, openssl_ctx_generic_free);
200
1.03k
    if (idx < 0)
201
0
        return 0;
202
203
1.03k
    ctx->dyn_indexes[static_index] = idx;
204
1.03k
    return 1;
205
1.03k
}
206
207
void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
208
                           const OPENSSL_CTX_METHOD *meth)
209
10.3k
{
210
10.3k
    void *data = NULL;
211
10.3k
    int dynidx;
212
213
10.3k
    ctx = openssl_ctx_get_concrete(ctx);
214
10.3k
    if (ctx == NULL)
215
0
        return NULL;
216
217
10.3k
    CRYPTO_THREAD_read_lock(ctx->lock);
218
10.3k
    dynidx = ctx->dyn_indexes[index];
219
10.3k
    CRYPTO_THREAD_unlock(ctx->lock);
220
221
10.3k
    if (dynidx != -1) {
222
9.33k
        CRYPTO_THREAD_read_lock(ctx->index_locks[index]);
223
9.33k
        data = CRYPTO_get_ex_data(&ctx->data, dynidx);
224
9.33k
        CRYPTO_THREAD_unlock(ctx->index_locks[index]);
225
9.33k
        return data;
226
9.33k
    }
227
228
1.03k
    CRYPTO_THREAD_write_lock(ctx->index_locks[index]);
229
1.03k
    CRYPTO_THREAD_write_lock(ctx->lock);
230
231
1.03k
    dynidx = ctx->dyn_indexes[index];
232
1.03k
    if (dynidx != -1) {
233
0
        CRYPTO_THREAD_unlock(ctx->lock);
234
0
        data = CRYPTO_get_ex_data(&ctx->data, dynidx);
235
0
        CRYPTO_THREAD_unlock(ctx->index_locks[index]);
236
0
        return data;
237
0
    }
238
239
1.03k
    if (!openssl_ctx_init_index(ctx, index, meth)) {
240
0
        CRYPTO_THREAD_unlock(ctx->lock);
241
0
        CRYPTO_THREAD_unlock(ctx->index_locks[index]);
242
0
        return NULL;
243
0
    }
244
245
1.03k
    CRYPTO_THREAD_unlock(ctx->lock);
246
247
    /* The alloc call ensures there's a value there */
248
1.03k
    if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
249
1.03k
                             &ctx->data, ctx->dyn_indexes[index]))
250
1.03k
        data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
251
252
1.03k
    CRYPTO_THREAD_unlock(ctx->index_locks[index]);
253
254
1.03k
    return data;
255
1.03k
}
256
257
OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)
258
554k
{
259
554k
    ctx = openssl_ctx_get_concrete(ctx);
260
554k
    if (ctx == NULL)
261
0
        return NULL;
262
554k
    return &ctx->global;
263
554k
}
264
265
int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
266
                         openssl_ctx_run_once_fn run_once_fn)
267
0
{
268
0
    int done = 0, ret = 0;
269
270
0
    ctx = openssl_ctx_get_concrete(ctx);
271
0
    if (ctx == NULL)
272
0
        return 0;
273
274
0
    CRYPTO_THREAD_read_lock(ctx->oncelock);
275
0
    done = ctx->run_once_done[idx];
276
0
    if (done)
277
0
        ret = ctx->run_once_ret[idx];
278
0
    CRYPTO_THREAD_unlock(ctx->oncelock);
279
280
0
    if (done)
281
0
        return ret;
282
283
0
    CRYPTO_THREAD_write_lock(ctx->oncelock);
284
0
    if (ctx->run_once_done[idx]) {
285
0
        ret = ctx->run_once_ret[idx];
286
0
        CRYPTO_THREAD_unlock(ctx->oncelock);
287
0
        return ret;
288
0
    }
289
290
0
    ret = run_once_fn(ctx);
291
0
    ctx->run_once_done[idx] = 1;
292
0
    ctx->run_once_ret[idx] = ret;
293
0
    CRYPTO_THREAD_unlock(ctx->oncelock);
294
295
0
    return ret;
296
0
}
297
298
int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
299
0
{
300
0
    struct openssl_ctx_onfree_list_st *newonfree
301
0
        = OPENSSL_malloc(sizeof(*newonfree));
302
303
0
    if (newonfree == NULL)
304
0
        return 0;
305
306
0
    newonfree->fn = onfreefn;
307
0
    newonfree->next = ctx->onfreelist;
308
0
    ctx->onfreelist = newonfree;
309
310
0
    return 1;
311
0
}