OpenSSL::config: Fix VMS guesses
[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 }
156
157 void ossl_lib_ctx_default_deinit(void)
158 {
159     context_deinit(&default_context_int);
160     CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
161 }
162
163 static OSSL_LIB_CTX *get_thread_default_context(void)
164 {
165     if (!RUN_ONCE(&default_context_init, default_context_do_init))
166         return NULL;
167
168     return CRYPTO_THREAD_get_local(&default_context_thread_local);
169 }
170
171 static OSSL_LIB_CTX *get_default_context(void)
172 {
173     OSSL_LIB_CTX *current_defctx = get_thread_default_context();
174
175     if (current_defctx == NULL)
176         current_defctx = &default_context_int;
177     return current_defctx;
178 }
179
180 static int set_default_context(OSSL_LIB_CTX *defctx)
181 {
182     if (defctx == &default_context_int)
183         defctx = NULL;
184
185     return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
186 }
187 #endif
188
189 OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
190 {
191     OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
192
193     if (ctx != NULL && !context_init(ctx)) {
194         OPENSSL_free(ctx);
195         ctx = NULL;
196     }
197     return ctx;
198 }
199
200 #ifndef FIPS_MODULE
201 OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
202                                              const OSSL_DISPATCH *in)
203 {
204     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
205
206     if (ctx == NULL)
207         return NULL;
208
209     if (!ossl_bio_init_core(ctx, in)) {
210         OSSL_LIB_CTX_free(ctx);
211         return NULL;
212     }
213
214     return ctx;
215 }
216
217 OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
218                                      const OSSL_DISPATCH *in)
219 {
220     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
221
222     if (ctx == NULL)
223         return NULL;
224
225     if (!ossl_provider_init_as_child(ctx, handle, in)) {
226         OSSL_LIB_CTX_free(ctx);
227         return NULL;
228     }
229     ctx->ischild = 1;
230
231     return ctx;
232 }
233
234 int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
235 {
236     return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
237 }
238 #endif
239
240 void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
241 {
242     if (ossl_lib_ctx_is_default(ctx))
243         return;
244
245 #ifndef FIPS_MODULE
246     if (ctx->ischild)
247         ossl_provider_deinit_child(ctx);
248 #endif
249     context_deinit(ctx);
250     OPENSSL_free(ctx);
251 }
252
253 #ifndef FIPS_MODULE
254 OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
255 {
256     if (!RUN_ONCE(&default_context_init, default_context_do_init))
257         return NULL;
258
259     return &default_context_int;
260 }
261
262 OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
263 {
264     OSSL_LIB_CTX *current_defctx;
265
266     if ((current_defctx = get_default_context()) != NULL) {
267         if (libctx != NULL)
268             set_default_context(libctx);
269         return current_defctx;
270     }
271
272     return NULL;
273 }
274 #endif
275
276 OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
277 {
278 #ifndef FIPS_MODULE
279     if (ctx == NULL)
280         return get_default_context();
281 #endif
282     return ctx;
283 }
284
285 int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
286 {
287 #ifndef FIPS_MODULE
288     if (ctx == NULL || ctx == get_default_context())
289         return 1;
290 #endif
291     return 0;
292 }
293
294 int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
295 {
296 #ifndef FIPS_MODULE
297     if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
298         return 1;
299 #endif
300     return 0;
301 }
302
303 static void ossl_lib_ctx_generic_new(void *parent_ign, void *ptr_ign,
304                                      CRYPTO_EX_DATA *ad, int index,
305                                      long argl_ign, void *argp)
306 {
307     const OSSL_LIB_CTX_METHOD *meth = argp;
308     OSSL_LIB_CTX *ctx = ossl_crypto_ex_data_get_ossl_lib_ctx(ad);
309     void *ptr = meth->new_func(ctx);
310
311     if (ptr != NULL) {
312         if (!CRYPTO_THREAD_write_lock(ctx->lock))
313             /*
314              * Can't return something, so best to hope that something will
315              * fail later. :(
316              */
317             return;
318         CRYPTO_set_ex_data(ad, index, ptr);
319         CRYPTO_THREAD_unlock(ctx->lock);
320     }
321 }
322 static void ossl_lib_ctx_generic_free(void *parent_ign, void *ptr,
323                                       CRYPTO_EX_DATA *ad, int index,
324                                       long argl_ign, void *argp)
325 {
326     const OSSL_LIB_CTX_METHOD *meth = argp;
327
328     meth->free_func(ptr);
329 }
330
331 static int ossl_lib_ctx_init_index(OSSL_LIB_CTX *ctx, int static_index,
332                                    const OSSL_LIB_CTX_METHOD *meth)
333 {
334     int idx;
335
336     ctx = ossl_lib_ctx_get_concrete(ctx);
337     if (ctx == NULL)
338         return 0;
339
340     idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
341                                           (void *)meth,
342                                           ossl_lib_ctx_generic_new,
343                                           NULL, ossl_lib_ctx_generic_free,
344                                           meth->priority);
345     if (idx < 0)
346         return 0;
347
348     ctx->dyn_indexes[static_index] = idx;
349     return 1;
350 }
351
352 void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
353                             const OSSL_LIB_CTX_METHOD *meth)
354 {
355     void *data = NULL;
356     int dynidx;
357
358     ctx = ossl_lib_ctx_get_concrete(ctx);
359     if (ctx == NULL)
360         return NULL;
361
362     if (!CRYPTO_THREAD_read_lock(ctx->lock))
363         return NULL;
364     dynidx = ctx->dyn_indexes[index];
365     CRYPTO_THREAD_unlock(ctx->lock);
366
367     if (dynidx != -1) {
368         if (!CRYPTO_THREAD_read_lock(ctx->index_locks[index]))
369             return NULL;
370         if (!CRYPTO_THREAD_read_lock(ctx->lock)) {
371             CRYPTO_THREAD_unlock(ctx->index_locks[index]);
372             return NULL;
373         }
374         data = CRYPTO_get_ex_data(&ctx->data, dynidx);
375         CRYPTO_THREAD_unlock(ctx->lock);
376         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
377         return data;
378     }
379
380     if (!CRYPTO_THREAD_write_lock(ctx->index_locks[index]))
381         return NULL;
382     if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
383         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
384         return NULL;
385     }
386
387     dynidx = ctx->dyn_indexes[index];
388     if (dynidx != -1) {
389         data = CRYPTO_get_ex_data(&ctx->data, dynidx);
390         CRYPTO_THREAD_unlock(ctx->lock);
391         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
392         return data;
393     }
394
395     if (!ossl_lib_ctx_init_index(ctx, index, meth)) {
396         CRYPTO_THREAD_unlock(ctx->lock);
397         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
398         return NULL;
399     }
400
401     CRYPTO_THREAD_unlock(ctx->lock);
402
403     /*
404      * The alloc call ensures there's a value there. We release the ctx->lock
405      * for this, because the allocation itself may recursively call
406      * ossl_lib_ctx_get_data for other indexes (never this one). The allocation
407      * will itself aquire the ctx->lock when it actually comes to store the
408      * allocated data (see ossl_lib_ctx_generic_new() above). We call
409      * ossl_crypto_alloc_ex_data_intern() here instead of CRYPTO_alloc_ex_data().
410      * They do the same thing except that the latter calls CRYPTO_get_ex_data()
411      * as well - which we must not do without holding the ctx->lock.
412      */
413     if (ossl_crypto_alloc_ex_data_intern(CRYPTO_EX_INDEX_OSSL_LIB_CTX, NULL,
414                                          &ctx->data, ctx->dyn_indexes[index])) {
415         if (!CRYPTO_THREAD_read_lock(ctx->lock))
416             goto end;
417         data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
418         CRYPTO_THREAD_unlock(ctx->lock);
419     }
420
421 end:
422     CRYPTO_THREAD_unlock(ctx->index_locks[index]);
423     return data;
424 }
425
426 OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
427 {
428     ctx = ossl_lib_ctx_get_concrete(ctx);
429     if (ctx == NULL)
430         return NULL;
431     return &ctx->global;
432 }
433
434 int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
435                           ossl_lib_ctx_run_once_fn run_once_fn)
436 {
437     int done = 0, ret = 0;
438
439     ctx = ossl_lib_ctx_get_concrete(ctx);
440     if (ctx == NULL)
441         return 0;
442
443     if (!CRYPTO_THREAD_read_lock(ctx->oncelock))
444         return 0;
445     done = ctx->run_once_done[idx];
446     if (done)
447         ret = ctx->run_once_ret[idx];
448     CRYPTO_THREAD_unlock(ctx->oncelock);
449
450     if (done)
451         return ret;
452
453     if (!CRYPTO_THREAD_write_lock(ctx->oncelock))
454         return 0;
455     if (ctx->run_once_done[idx]) {
456         ret = ctx->run_once_ret[idx];
457         CRYPTO_THREAD_unlock(ctx->oncelock);
458         return ret;
459     }
460
461     ret = run_once_fn(ctx);
462     ctx->run_once_done[idx] = 1;
463     ctx->run_once_ret[idx] = ret;
464     CRYPTO_THREAD_unlock(ctx->oncelock);
465
466     return ret;
467 }
468
469 int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn)
470 {
471     struct ossl_lib_ctx_onfree_list_st *newonfree
472         = OPENSSL_malloc(sizeof(*newonfree));
473
474     if (newonfree == NULL)
475         return 0;
476
477     newonfree->fn = onfreefn;
478     newonfree->next = ctx->onfreelist;
479     ctx->onfreelist = newonfree;
480
481     return 1;
482 }
483
484 const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
485 {
486 #ifdef FIPS_MODULE
487     return "FIPS internal library context";
488 #else
489     if (ossl_lib_ctx_is_global_default(libctx))
490         return "Global default library context";
491     if (ossl_lib_ctx_is_default(libctx))
492         return "Thread-local default library context";
493     return "Non-default library context";
494 #endif
495 }