2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "crypto/cryptlib.h"
11 #include <openssl/conf.h>
12 #include "internal/thread_once.h"
13 #include "internal/property.h"
15 struct openssl_ctx_onfree_list_st {
16 openssl_ctx_onfree_fn *fn;
17 struct openssl_ctx_onfree_list_st *next;
20 struct openssl_ctx_st {
25 * For most data in the OPENSSL_CTX we just use ex_data to store it. But
26 * that doesn't work for ex_data itself - so we store that directly.
28 OSSL_EX_DATA_GLOBAL global;
30 /* Map internal static indexes to dynamically created indexes */
31 int dyn_indexes[OPENSSL_CTX_MAX_INDEXES];
33 /* Keep a separate lock for each index */
34 CRYPTO_RWLOCK *index_locks[OPENSSL_CTX_MAX_INDEXES];
36 CRYPTO_RWLOCK *oncelock;
37 int run_once_done[OPENSSL_CTX_MAX_RUN_ONCE];
38 int run_once_ret[OPENSSL_CTX_MAX_RUN_ONCE];
39 struct openssl_ctx_onfree_list_st *onfreelist;
43 static OPENSSL_CTX default_context_int;
45 /* Always points at default_context_int if it has been initialised */
46 static OPENSSL_CTX *default_context = NULL;
49 static int context_init(OPENSSL_CTX *ctx)
54 ctx->lock = CRYPTO_THREAD_lock_new();
55 if (ctx->lock == NULL)
58 ctx->oncelock = CRYPTO_THREAD_lock_new();
59 if (ctx->oncelock == NULL)
62 for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++) {
63 ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
64 ctx->dyn_indexes[i] = -1;
65 if (ctx->index_locks[i] == NULL)
69 /* OPENSSL_CTX is built on top of ex_data so we initialise that directly */
70 if (!do_ex_data_init(ctx))
74 if (!crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
76 crypto_cleanup_all_ex_data_int(ctx);
80 /* Everything depends on properties, so we also pre-initialise that */
81 if (!ossl_property_parse_init(ctx))
87 crypto_cleanup_all_ex_data_int(ctx);
88 CRYPTO_THREAD_lock_free(ctx->oncelock);
89 CRYPTO_THREAD_lock_free(ctx->lock);
94 static int context_deinit(OPENSSL_CTX *ctx)
96 struct openssl_ctx_onfree_list_st *tmp, *onfree;
102 ossl_ctx_thread_stop(ctx);
104 onfree = ctx->onfreelist;
105 while (onfree != NULL) {
108 onfree = onfree->next;
111 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
112 crypto_cleanup_all_ex_data_int(ctx);
113 for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++)
114 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
116 CRYPTO_THREAD_lock_free(ctx->oncelock);
117 CRYPTO_THREAD_lock_free(ctx->lock);
123 void openssl_ctx_default_deinit(void)
125 context_deinit(default_context);
128 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
129 DEFINE_RUN_ONCE_STATIC(do_default_context_init)
131 if (context_init(&default_context_int))
132 default_context = &default_context_int;
138 OPENSSL_CTX *OPENSSL_CTX_new(void)
140 OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
142 if (ctx != NULL && !context_init(ctx)) {
143 OPENSSL_CTX_free(ctx);
150 int OPENSSL_CTX_load_config(OPENSSL_CTX *ctx, const char *config_file)
152 return CONF_modules_load_file_with_libctx(ctx, config_file, NULL, 0) > 0;
156 void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
163 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx)
167 if (!RUN_ONCE(&default_context_init, do_default_context_init))
169 return default_context;
175 int openssl_ctx_is_default(OPENSSL_CTX *ctx)
178 if (ctx == NULL || ctx == default_context)
184 static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
185 CRYPTO_EX_DATA *ad, int index,
186 long argl_ign, void *argp)
188 const OPENSSL_CTX_METHOD *meth = argp;
189 void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad));
192 CRYPTO_set_ex_data(ad, index, ptr);
194 static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
195 CRYPTO_EX_DATA *ad, int index,
196 long argl_ign, void *argp)
198 const OPENSSL_CTX_METHOD *meth = argp;
200 meth->free_func(ptr);
203 /* Non-static so we can use it in context_internal_test */
204 static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index,
205 const OPENSSL_CTX_METHOD *meth)
209 ctx = openssl_ctx_get_concrete(ctx);
213 idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0,
215 openssl_ctx_generic_new,
216 NULL, openssl_ctx_generic_free);
220 ctx->dyn_indexes[static_index] = idx;
224 void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
225 const OPENSSL_CTX_METHOD *meth)
230 ctx = openssl_ctx_get_concrete(ctx);
234 CRYPTO_THREAD_read_lock(ctx->lock);
235 dynidx = ctx->dyn_indexes[index];
236 CRYPTO_THREAD_unlock(ctx->lock);
239 CRYPTO_THREAD_read_lock(ctx->index_locks[index]);
240 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
241 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
245 CRYPTO_THREAD_write_lock(ctx->index_locks[index]);
246 CRYPTO_THREAD_write_lock(ctx->lock);
248 dynidx = ctx->dyn_indexes[index];
250 CRYPTO_THREAD_unlock(ctx->lock);
251 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
252 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
256 if (!openssl_ctx_init_index(ctx, index, meth)) {
257 CRYPTO_THREAD_unlock(ctx->lock);
258 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
262 CRYPTO_THREAD_unlock(ctx->lock);
264 /* The alloc call ensures there's a value there */
265 if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
266 &ctx->data, ctx->dyn_indexes[index]))
267 data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
269 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
274 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)
276 ctx = openssl_ctx_get_concrete(ctx);
282 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
283 openssl_ctx_run_once_fn run_once_fn)
285 int done = 0, ret = 0;
287 ctx = openssl_ctx_get_concrete(ctx);
291 CRYPTO_THREAD_read_lock(ctx->oncelock);
292 done = ctx->run_once_done[idx];
294 ret = ctx->run_once_ret[idx];
295 CRYPTO_THREAD_unlock(ctx->oncelock);
300 CRYPTO_THREAD_write_lock(ctx->oncelock);
301 if (ctx->run_once_done[idx]) {
302 ret = ctx->run_once_ret[idx];
303 CRYPTO_THREAD_unlock(ctx->oncelock);
307 ret = run_once_fn(ctx);
308 ctx->run_once_done[idx] = 1;
309 ctx->run_once_ret[idx] = ret;
310 CRYPTO_THREAD_unlock(ctx->oncelock);
315 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
317 struct openssl_ctx_onfree_list_st *newonfree
318 = OPENSSL_malloc(sizeof(*newonfree));
320 if (newonfree == NULL)
323 newonfree->fn = onfreefn;
324 newonfree->next = ctx->onfreelist;
325 ctx->onfreelist = newonfree;