]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/context.c
Add the function OSSL_LIB_CTX_get0_global_default()
[thirdparty/openssl.git] / crypto / context.c
CommitLineData
d64b6299 1/*
4333b89f 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
d64b6299
RL
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
25f2138b 10#include "crypto/cryptlib.h"
22e27978 11#include <openssl/conf.h>
d64b6299 12#include "internal/thread_once.h"
505f4660 13#include "internal/property.h"
4b1f34f1 14#include "internal/core.h"
d64b6299 15
b4250010
DMSP
16struct ossl_lib_ctx_onfree_list_st {
17 ossl_lib_ctx_onfree_fn *fn;
18 struct ossl_lib_ctx_onfree_list_st *next;
b8fe36fe
MC
19};
20
b4250010 21struct ossl_lib_ctx_st {
d64b6299
RL
22 CRYPTO_RWLOCK *lock;
23 CRYPTO_EX_DATA data;
1aedc35f
MC
24
25 /*
b4250010 26 * For most data in the OSSL_LIB_CTX we just use ex_data to store it. But
1aedc35f
MC
27 * that doesn't work for ex_data itself - so we store that directly.
28 */
29 OSSL_EX_DATA_GLOBAL global;
30
31 /* Map internal static indexes to dynamically created indexes */
b4250010 32 int dyn_indexes[OSSL_LIB_CTX_MAX_INDEXES];
1aedc35f 33
770de346 34 /* Keep a separate lock for each index */
b4250010 35 CRYPTO_RWLOCK *index_locks[OSSL_LIB_CTX_MAX_INDEXES];
770de346 36
1aedc35f 37 CRYPTO_RWLOCK *oncelock;
b4250010
DMSP
38 int run_once_done[OSSL_LIB_CTX_MAX_RUN_ONCE];
39 int run_once_ret[OSSL_LIB_CTX_MAX_RUN_ONCE];
40 struct ossl_lib_ctx_onfree_list_st *onfreelist;
d64b6299
RL
41};
42
4b1f34f1
P
43int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx)
44{
45 return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
46}
47
48int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx)
49{
50 return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
51}
52
53int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx)
54{
55 return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock);
56}
57
b4250010 58static int context_init(OSSL_LIB_CTX *ctx)
d64b6299 59{
1aedc35f 60 size_t i;
505f4660 61 int exdata_done = 0;
1aedc35f
MC
62
63 ctx->lock = CRYPTO_THREAD_lock_new();
64 if (ctx->lock == NULL)
65 return 0;
66
67 ctx->oncelock = CRYPTO_THREAD_lock_new();
68 if (ctx->oncelock == NULL)
69 goto err;
70
b4250010 71 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++) {
770de346 72 ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
1aedc35f 73 ctx->dyn_indexes[i] = -1;
770de346
MC
74 if (ctx->index_locks[i] == NULL)
75 goto err;
76 }
1aedc35f 77
b4250010 78 /* OSSL_LIB_CTX is built on top of ex_data so we initialise that directly */
e4bec869 79 if (!ossl_do_ex_data_init(ctx))
1aedc35f 80 goto err;
505f4660 81 exdata_done = 1;
1aedc35f 82
e4bec869
SL
83 if (!ossl_crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
84 &ctx->data)) {
85 ossl_crypto_cleanup_all_ex_data_int(ctx);
1aedc35f
MC
86 goto err;
87 }
88
505f4660
MC
89 /* Everything depends on properties, so we also pre-initialise that */
90 if (!ossl_property_parse_init(ctx))
91 goto err;
92
1aedc35f
MC
93 return 1;
94 err:
505f4660 95 if (exdata_done)
e4bec869 96 ossl_crypto_cleanup_all_ex_data_int(ctx);
1aedc35f
MC
97 CRYPTO_THREAD_lock_free(ctx->oncelock);
98 CRYPTO_THREAD_lock_free(ctx->lock);
99 ctx->lock = NULL;
100 return 0;
d64b6299
RL
101}
102
b4250010 103static int context_deinit(OSSL_LIB_CTX *ctx)
d64b6299 104{
b4250010 105 struct ossl_lib_ctx_onfree_list_st *tmp, *onfree;
770de346 106 int i;
1aedc35f
MC
107
108 if (ctx == NULL)
109 return 1;
b8fe36fe 110
da747958
MC
111 ossl_ctx_thread_stop(ctx);
112
1aedc35f 113 onfree = ctx->onfreelist;
b8fe36fe
MC
114 while (onfree != NULL) {
115 onfree->fn(ctx);
116 tmp = onfree;
117 onfree = onfree->next;
118 OPENSSL_free(tmp);
119 }
b4250010 120 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL, &ctx->data);
e4bec869 121 ossl_crypto_cleanup_all_ex_data_int(ctx);
b4250010 122 for (i = 0; i < OSSL_LIB_CTX_MAX_INDEXES; i++)
770de346
MC
123 CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
124
1aedc35f 125 CRYPTO_THREAD_lock_free(ctx->oncelock);
d64b6299 126 CRYPTO_THREAD_lock_free(ctx->lock);
1aedc35f 127 ctx->lock = NULL;
d64b6299
RL
128 return 1;
129}
130
f844f9eb 131#ifndef FIPS_MODULE
5a975275 132/* The default default context */
b4250010 133static OSSL_LIB_CTX default_context_int;
5a975275
RL
134
135static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
136static CRYPTO_THREAD_LOCAL default_context_thread_local;
137
138DEFINE_RUN_ONCE_STATIC(default_context_do_init)
139{
140 return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
141 && context_init(&default_context_int);
142}
143
b4250010 144void ossl_lib_ctx_default_deinit(void)
d64b6299 145{
5a975275 146 context_deinit(&default_context_int);
d64b6299 147}
1aedc35f 148
b4250010 149static OSSL_LIB_CTX *get_thread_default_context(void)
d64b6299 150{
5a975275
RL
151 if (!RUN_ONCE(&default_context_init, default_context_do_init))
152 return NULL;
1aedc35f 153
5a975275
RL
154 return CRYPTO_THREAD_get_local(&default_context_thread_local);
155}
156
b4250010 157static OSSL_LIB_CTX *get_default_context(void)
5a975275 158{
b4250010 159 OSSL_LIB_CTX *current_defctx = get_thread_default_context();
5a975275
RL
160
161 if (current_defctx == NULL)
162 current_defctx = &default_context_int;
163 return current_defctx;
164}
165
b4250010 166static int set_default_context(OSSL_LIB_CTX *defctx)
5a975275
RL
167{
168 if (defctx == &default_context_int)
169 defctx = NULL;
170
171 return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
d64b6299 172}
1aedc35f 173#endif
d64b6299 174
b4250010 175OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
d64b6299 176{
b4250010 177 OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
d64b6299
RL
178
179 if (ctx != NULL && !context_init(ctx)) {
b4250010 180 OSSL_LIB_CTX_free(ctx);
d64b6299
RL
181 ctx = NULL;
182 }
183 return ctx;
184}
185
f844f9eb 186#ifndef FIPS_MODULE
b4250010 187int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
22e27978 188{
d8652be0 189 return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
22e27978
SL
190}
191#endif
192
b4250010 193void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
d64b6299 194{
b4250010 195 if (ossl_lib_ctx_is_default(ctx))
5a975275
RL
196 return;
197
198 context_deinit(ctx);
d64b6299
RL
199 OPENSSL_free(ctx);
200}
201
978e323a
MC
202#ifndef FIPS_MODULE
203OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
204{
205 if (!RUN_ONCE(&default_context_init, default_context_do_init))
206 return NULL;
207
208 return &default_context_int;
209}
210
b4250010 211OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
5a975275 212{
b4250010 213 OSSL_LIB_CTX *current_defctx;
5a975275 214
92b20fb8
MC
215 if ((current_defctx = get_default_context()) != NULL) {
216 if (libctx != NULL)
217 set_default_context(libctx);
5a975275 218 return current_defctx;
92b20fb8 219 }
5a975275
RL
220
221 return NULL;
222}
978e323a 223#endif
5a975275 224
b4250010 225OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
d4c051ce 226{
f844f9eb 227#ifndef FIPS_MODULE
5a975275
RL
228 if (ctx == NULL)
229 return get_default_context();
d4c051ce
MC
230#endif
231 return ctx;
232}
233
b4250010 234int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
6b1e5fa4 235{
f844f9eb 236#ifndef FIPS_MODULE
5a975275 237 if (ctx == NULL || ctx == get_default_context())
6b1e5fa4 238 return 1;
cfbd76c1
RL
239#endif
240 return 0;
241}
242
b4250010 243int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
cfbd76c1
RL
244{
245#ifndef FIPS_MODULE
b4250010 246 if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
cfbd76c1 247 return 1;
6b1e5fa4
MC
248#endif
249 return 0;
250}
251
b4250010
DMSP
252static void ossl_lib_ctx_generic_new(void *parent_ign, void *ptr_ign,
253 CRYPTO_EX_DATA *ad, int index,
254 long argl_ign, void *argp)
d64b6299 255{
b4250010 256 const OSSL_LIB_CTX_METHOD *meth = argp;
e4bec869 257 OSSL_LIB_CTX *ctx = ossl_crypto_ex_data_get_ossl_lib_ctx(ad);
2c404214 258 void *ptr = meth->new_func(ctx);
d64b6299 259
2c404214 260 if (ptr != NULL) {
cd3f8c1b
RS
261 if (!CRYPTO_THREAD_write_lock(ctx->lock))
262 /*
263 * Can't return something, so best to hope that something will
264 * fail later. :(
265 */
266 return;
d64b6299 267 CRYPTO_set_ex_data(ad, index, ptr);
2c404214
MC
268 CRYPTO_THREAD_unlock(ctx->lock);
269 }
d64b6299 270}
b4250010
DMSP
271static void ossl_lib_ctx_generic_free(void *parent_ign, void *ptr,
272 CRYPTO_EX_DATA *ad, int index,
273 long argl_ign, void *argp)
d64b6299 274{
b4250010 275 const OSSL_LIB_CTX_METHOD *meth = argp;
d64b6299
RL
276
277 meth->free_func(ptr);
278}
1aedc35f
MC
279
280/* Non-static so we can use it in context_internal_test */
b4250010
DMSP
281static int ossl_lib_ctx_init_index(OSSL_LIB_CTX *ctx, int static_index,
282 const OSSL_LIB_CTX_METHOD *meth)
d64b6299 283{
1aedc35f
MC
284 int idx;
285
b4250010 286 ctx = ossl_lib_ctx_get_concrete(ctx);
1aedc35f
MC
287 if (ctx == NULL)
288 return 0;
289
e4bec869
SL
290 idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
291 (void *)meth,
292 ossl_lib_ctx_generic_new,
293 NULL, ossl_lib_ctx_generic_free);
1aedc35f
MC
294 if (idx < 0)
295 return 0;
296
297 ctx->dyn_indexes[static_index] = idx;
298 return 1;
d64b6299
RL
299}
300
b4250010
DMSP
301void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
302 const OSSL_LIB_CTX_METHOD *meth)
d64b6299
RL
303{
304 void *data = NULL;
770de346 305 int dynidx;
d64b6299 306
b4250010 307 ctx = ossl_lib_ctx_get_concrete(ctx);
b8fe36fe
MC
308 if (ctx == NULL)
309 return NULL;
d64b6299 310
cd3f8c1b
RS
311 if (!CRYPTO_THREAD_read_lock(ctx->lock))
312 return NULL;
770de346
MC
313 dynidx = ctx->dyn_indexes[index];
314 CRYPTO_THREAD_unlock(ctx->lock);
315
316 if (dynidx != -1) {
cd3f8c1b
RS
317 if (!CRYPTO_THREAD_read_lock(ctx->index_locks[index]))
318 return NULL;
319 if (!CRYPTO_THREAD_read_lock(ctx->lock)) {
320 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
321 return NULL;
322 }
770de346 323 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
04b9435a 324 CRYPTO_THREAD_unlock(ctx->lock);
770de346
MC
325 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
326 return data;
327 }
d64b6299 328
cd3f8c1b
RS
329 if (!CRYPTO_THREAD_write_lock(ctx->index_locks[index]))
330 return NULL;
331 if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
332 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
333 return NULL;
334 }
770de346
MC
335
336 dynidx = ctx->dyn_indexes[index];
337 if (dynidx != -1) {
770de346 338 data = CRYPTO_get_ex_data(&ctx->data, dynidx);
04b9435a 339 CRYPTO_THREAD_unlock(ctx->lock);
770de346
MC
340 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
341 return data;
342 }
343
b4250010 344 if (!ossl_lib_ctx_init_index(ctx, index, meth)) {
1aedc35f 345 CRYPTO_THREAD_unlock(ctx->lock);
770de346 346 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
1aedc35f
MC
347 return NULL;
348 }
349
770de346
MC
350 CRYPTO_THREAD_unlock(ctx->lock);
351
04b9435a
MC
352 /*
353 * The alloc call ensures there's a value there. We release the ctx->lock
354 * for this, because the allocation itself may recursively call
355 * ossl_lib_ctx_get_data for other indexes (never this one). The allocation
356 * will itself aquire the ctx->lock when it actually comes to store the
357 * allocated data (see ossl_lib_ctx_generic_new() above). We call
358 * ossl_crypto_alloc_ex_data_intern() here instead of CRYPTO_alloc_ex_data().
359 * They do the same thing except that the latter calls CRYPTO_get_ex_data()
360 * as well - which we must not do without holding the ctx->lock.
361 */
362 if (ossl_crypto_alloc_ex_data_intern(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
363 &ctx->data, ctx->dyn_indexes[index])) {
cd3f8c1b
RS
364 if (!CRYPTO_THREAD_read_lock(ctx->lock))
365 goto end;
1aedc35f 366 data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
04b9435a
MC
367 CRYPTO_THREAD_unlock(ctx->lock);
368 }
d64b6299 369
cd3f8c1b 370end:
770de346 371 CRYPTO_THREAD_unlock(ctx->index_locks[index]);
d64b6299
RL
372 return data;
373}
374
b4250010 375OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
1aedc35f 376{
b4250010 377 ctx = ossl_lib_ctx_get_concrete(ctx);
1aedc35f
MC
378 if (ctx == NULL)
379 return NULL;
380 return &ctx->global;
381}
382
b4250010
DMSP
383int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
384 ossl_lib_ctx_run_once_fn run_once_fn)
b8fe36fe
MC
385{
386 int done = 0, ret = 0;
387
b4250010 388 ctx = ossl_lib_ctx_get_concrete(ctx);
b8fe36fe
MC
389 if (ctx == NULL)
390 return 0;
391
cd3f8c1b
RS
392 if (!CRYPTO_THREAD_read_lock(ctx->oncelock))
393 return 0;
b8fe36fe
MC
394 done = ctx->run_once_done[idx];
395 if (done)
396 ret = ctx->run_once_ret[idx];
1aedc35f 397 CRYPTO_THREAD_unlock(ctx->oncelock);
b8fe36fe
MC
398
399 if (done)
400 return ret;
401
cd3f8c1b
RS
402 if (!CRYPTO_THREAD_write_lock(ctx->oncelock))
403 return 0;
b8fe36fe
MC
404 if (ctx->run_once_done[idx]) {
405 ret = ctx->run_once_ret[idx];
1aedc35f 406 CRYPTO_THREAD_unlock(ctx->oncelock);
b8fe36fe
MC
407 return ret;
408 }
409
410 ret = run_once_fn(ctx);
411 ctx->run_once_done[idx] = 1;
412 ctx->run_once_ret[idx] = ret;
1aedc35f 413 CRYPTO_THREAD_unlock(ctx->oncelock);
b8fe36fe
MC
414
415 return ret;
416}
417
b4250010 418int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn)
b8fe36fe 419{
b4250010 420 struct ossl_lib_ctx_onfree_list_st *newonfree
b8fe36fe
MC
421 = OPENSSL_malloc(sizeof(*newonfree));
422
423 if (newonfree == NULL)
424 return 0;
425
426 newonfree->fn = onfreefn;
427 newonfree->next = ctx->onfreelist;
428 ctx->onfreelist = newonfree;
429
430 return 1;
431}
d6d42cda
RL
432
433const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
434{
435#ifdef FIPS_MODULE
436 return "FIPS internal library context";
437#else
438 if (ossl_lib_ctx_is_global_default(libctx))
439 return "Global default library context";
440 if (ossl_lib_ctx_is_default(libctx))
441 return "Thread-local default library context";
442 return "Non-default library context";
443#endif
444}