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 "internal/cryptlib_int.h"
11 #include "internal/thread_once.h"
13 struct openssl_ctx_onfree_list_st {
14 openssl_ctx_onfree_fn *fn;
15 struct openssl_ctx_onfree_list_st *next;
18 struct openssl_ctx_st {
23 * For most data in the OPENSSL_CTX we just use ex_data to store it. But
24 * that doesn't work for ex_data itself - so we store that directly.
26 OSSL_EX_DATA_GLOBAL global;
28 /* Map internal static indexes to dynamically created indexes */
29 int dyn_indexes[OPENSSL_CTX_MAX_INDEXES];
31 /* Keep a separate lock for each index */
32 CRYPTO_RWLOCK *index_locks[OPENSSL_CTX_MAX_INDEXES];
34 CRYPTO_RWLOCK *oncelock;
35 int run_once_done[OPENSSL_CTX_MAX_RUN_ONCE];
36 int run_once_ret[OPENSSL_CTX_MAX_RUN_ONCE];
37 struct openssl_ctx_onfree_list_st *onfreelist;
41 static OPENSSL_CTX default_context_int;
43 /* Always points at default_context_int if it has been initialised */
44 static OPENSSL_CTX *default_context = NULL;
47 static int context_init(OPENSSL_CTX *ctx)
51 ctx->lock = CRYPTO_THREAD_lock_new();
52 if (ctx->lock == NULL)
55 ctx->oncelock = CRYPTO_THREAD_lock_new();
56 if (ctx->oncelock == NULL)
59 for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++) {
60 ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
61 ctx->dyn_indexes[i] = -1;
62 if (ctx->index_locks[i] == NULL)
66 if (!do_ex_data_init(ctx))
69 if (!crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
71 crypto_cleanup_all_ex_data_int(ctx);
77 CRYPTO_THREAD_lock_free(ctx->oncelock);
78 CRYPTO_THREAD_lock_free(ctx->lock);
83 static int context_deinit(OPENSSL_CTX *ctx)
85 struct openssl_ctx_onfree_list_st *tmp, *onfree;
91 ossl_ctx_thread_stop(ctx);
93 onfree = ctx->onfreelist;
94 while (onfree != NULL) {
97 onfree = onfree->next;
100 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
101 crypto_cleanup_all_ex_data_int(ctx);
102 for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++)
103 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
105 CRYPTO_THREAD_lock_free(ctx->oncelock);
106 CRYPTO_THREAD_lock_free(ctx->lock);
112 void openssl_ctx_default_deinit(void)
114 context_deinit(default_context);
117 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
118 DEFINE_RUN_ONCE_STATIC(do_default_context_init)
120 if (context_init(&default_context_int))
121 default_context = &default_context_int;
127 OPENSSL_CTX *OPENSSL_CTX_new(void)
129 OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
131 if (ctx != NULL && !context_init(ctx)) {
132 OPENSSL_CTX_free(ctx);
138 void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
145 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx)
149 if (!RUN_ONCE(&default_context_init, do_default_context_init))
151 return default_context;
157 static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
158 CRYPTO_EX_DATA *ad, int index,
159 long argl_ign, void *argp)
161 const OPENSSL_CTX_METHOD *meth = argp;
162 void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad));
165 CRYPTO_set_ex_data(ad, index, ptr);
167 static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
168 CRYPTO_EX_DATA *ad, int index,
169 long argl_ign, void *argp)
171 const OPENSSL_CTX_METHOD *meth = argp;
173 meth->free_func(ptr);
176 /* Non-static so we can use it in context_internal_test */
177 static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index,
178 const OPENSSL_CTX_METHOD *meth)
182 ctx = openssl_ctx_get_concrete(ctx);
186 idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0,
188 openssl_ctx_generic_new,
189 NULL, openssl_ctx_generic_free);
193 ctx->dyn_indexes[static_index] = idx;
197 void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
198 const OPENSSL_CTX_METHOD *meth)
203 ctx = openssl_ctx_get_concrete(ctx);
207 CRYPTO_THREAD_read_lock(ctx->lock);
208 dynidx = ctx->dyn_indexes[index];
209 CRYPTO_THREAD_unlock(ctx->lock);
212 CRYPTO_THREAD_read_lock(ctx->index_locks[index]);
213 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
214 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
218 CRYPTO_THREAD_write_lock(ctx->index_locks[index]);
219 CRYPTO_THREAD_write_lock(ctx->lock);
221 dynidx = ctx->dyn_indexes[index];
223 CRYPTO_THREAD_unlock(ctx->lock);
224 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
225 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
229 if (!openssl_ctx_init_index(ctx, index, meth)) {
230 CRYPTO_THREAD_unlock(ctx->lock);
231 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
235 CRYPTO_THREAD_unlock(ctx->lock);
237 /* The alloc call ensures there's a value there */
238 if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
239 &ctx->data, ctx->dyn_indexes[index]))
240 data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
242 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
247 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)
249 ctx = openssl_ctx_get_concrete(ctx);
255 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
256 openssl_ctx_run_once_fn run_once_fn)
258 int done = 0, ret = 0;
260 ctx = openssl_ctx_get_concrete(ctx);
264 CRYPTO_THREAD_read_lock(ctx->oncelock);
265 done = ctx->run_once_done[idx];
267 ret = ctx->run_once_ret[idx];
268 CRYPTO_THREAD_unlock(ctx->oncelock);
273 CRYPTO_THREAD_write_lock(ctx->oncelock);
274 if (ctx->run_once_done[idx]) {
275 ret = ctx->run_once_ret[idx];
276 CRYPTO_THREAD_unlock(ctx->oncelock);
280 ret = run_once_fn(ctx);
281 ctx->run_once_done[idx] = 1;
282 ctx->run_once_ret[idx] = ret;
283 CRYPTO_THREAD_unlock(ctx->oncelock);
288 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
290 struct openssl_ctx_onfree_list_st *newonfree
291 = OPENSSL_malloc(sizeof(*newonfree));
293 if (newonfree == NULL)
296 newonfree->fn = onfreefn;
297 newonfree->next = ctx->onfreelist;
298 ctx->onfreelist = newonfree;