2 * Copyright 2019 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 "internal/thread_once.h"
12 #include "internal/property.h"
14 struct openssl_ctx_onfree_list_st {
15 openssl_ctx_onfree_fn *fn;
16 struct openssl_ctx_onfree_list_st *next;
19 struct openssl_ctx_st {
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.
27 OSSL_EX_DATA_GLOBAL global;
29 /* Map internal static indexes to dynamically created indexes */
30 int dyn_indexes[OPENSSL_CTX_MAX_INDEXES];
32 /* Keep a separate lock for each index */
33 CRYPTO_RWLOCK *index_locks[OPENSSL_CTX_MAX_INDEXES];
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;
42 static OPENSSL_CTX default_context_int;
44 /* Always points at default_context_int if it has been initialised */
45 static OPENSSL_CTX *default_context = NULL;
48 static int context_init(OPENSSL_CTX *ctx)
53 ctx->lock = CRYPTO_THREAD_lock_new();
54 if (ctx->lock == NULL)
57 ctx->oncelock = CRYPTO_THREAD_lock_new();
58 if (ctx->oncelock == NULL)
61 for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++) {
62 ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
63 ctx->dyn_indexes[i] = -1;
64 if (ctx->index_locks[i] == NULL)
68 /* OPENSSL_CTX is built on top of ex_data so we initialise that directly */
69 if (!do_ex_data_init(ctx))
73 if (!crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
75 crypto_cleanup_all_ex_data_int(ctx);
79 /* Everything depends on properties, so we also pre-initialise that */
80 if (!ossl_property_parse_init(ctx))
86 crypto_cleanup_all_ex_data_int(ctx);
87 CRYPTO_THREAD_lock_free(ctx->oncelock);
88 CRYPTO_THREAD_lock_free(ctx->lock);
93 static int context_deinit(OPENSSL_CTX *ctx)
95 struct openssl_ctx_onfree_list_st *tmp, *onfree;
101 ossl_ctx_thread_stop(ctx);
103 onfree = ctx->onfreelist;
104 while (onfree != NULL) {
107 onfree = onfree->next;
110 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
111 crypto_cleanup_all_ex_data_int(ctx);
112 for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++)
113 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
115 CRYPTO_THREAD_lock_free(ctx->oncelock);
116 CRYPTO_THREAD_lock_free(ctx->lock);
122 void openssl_ctx_default_deinit(void)
124 context_deinit(default_context);
127 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
128 DEFINE_RUN_ONCE_STATIC(do_default_context_init)
130 if (context_init(&default_context_int))
131 default_context = &default_context_int;
137 OPENSSL_CTX *OPENSSL_CTX_new(void)
139 OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
141 if (ctx != NULL && !context_init(ctx)) {
142 OPENSSL_CTX_free(ctx);
148 void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
155 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx)
159 if (!RUN_ONCE(&default_context_init, do_default_context_init))
161 return default_context;
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)
171 const OPENSSL_CTX_METHOD *meth = argp;
172 void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad));
175 CRYPTO_set_ex_data(ad, index, ptr);
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)
181 const OPENSSL_CTX_METHOD *meth = argp;
183 meth->free_func(ptr);
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)
192 ctx = openssl_ctx_get_concrete(ctx);
196 idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0,
198 openssl_ctx_generic_new,
199 NULL, openssl_ctx_generic_free);
203 ctx->dyn_indexes[static_index] = idx;
207 void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
208 const OPENSSL_CTX_METHOD *meth)
213 ctx = openssl_ctx_get_concrete(ctx);
217 CRYPTO_THREAD_read_lock(ctx->lock);
218 dynidx = ctx->dyn_indexes[index];
219 CRYPTO_THREAD_unlock(ctx->lock);
222 CRYPTO_THREAD_read_lock(ctx->index_locks[index]);
223 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
224 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
228 CRYPTO_THREAD_write_lock(ctx->index_locks[index]);
229 CRYPTO_THREAD_write_lock(ctx->lock);
231 dynidx = ctx->dyn_indexes[index];
233 CRYPTO_THREAD_unlock(ctx->lock);
234 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
235 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
239 if (!openssl_ctx_init_index(ctx, index, meth)) {
240 CRYPTO_THREAD_unlock(ctx->lock);
241 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
245 CRYPTO_THREAD_unlock(ctx->lock);
247 /* The alloc call ensures there's a value there */
248 if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
249 &ctx->data, ctx->dyn_indexes[index]))
250 data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
252 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
257 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)
259 ctx = openssl_ctx_get_concrete(ctx);
265 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
266 openssl_ctx_run_once_fn run_once_fn)
268 int done = 0, ret = 0;
270 ctx = openssl_ctx_get_concrete(ctx);
274 CRYPTO_THREAD_read_lock(ctx->oncelock);
275 done = ctx->run_once_done[idx];
277 ret = ctx->run_once_ret[idx];
278 CRYPTO_THREAD_unlock(ctx->oncelock);
283 CRYPTO_THREAD_write_lock(ctx->oncelock);
284 if (ctx->run_once_done[idx]) {
285 ret = ctx->run_once_ret[idx];
286 CRYPTO_THREAD_unlock(ctx->oncelock);
290 ret = run_once_fn(ctx);
291 ctx->run_once_done[idx] = 1;
292 ctx->run_once_ret[idx] = ret;
293 CRYPTO_THREAD_unlock(ctx->oncelock);
298 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
300 struct openssl_ctx_onfree_list_st *newonfree
301 = OPENSSL_malloc(sizeof(*newonfree));
303 if (newonfree == NULL)
306 newonfree->fn = onfreefn;
307 newonfree->next = ctx->onfreelist;
308 ctx->onfreelist = newonfree;