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