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