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