Add documentation for CMS_EnvelopedData_create()
[openssl.git] / crypto / context.c
1 /*
2  * Copyright 2019-2020 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 openssl_ctx_onfree_list_st {
16     openssl_ctx_onfree_fn *fn;
17     struct openssl_ctx_onfree_list_st *next;
18 };
19
20 struct openssl_ctx_st {
21     CRYPTO_RWLOCK *lock;
22     CRYPTO_EX_DATA data;
23
24     /*
25      * For most data in the OPENSSL_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[OPENSSL_CTX_MAX_INDEXES];
32
33     /* Keep a separate lock for each index */
34     CRYPTO_RWLOCK *index_locks[OPENSSL_CTX_MAX_INDEXES];
35
36     CRYPTO_RWLOCK *oncelock;
37     int run_once_done[OPENSSL_CTX_MAX_RUN_ONCE];
38     int run_once_ret[OPENSSL_CTX_MAX_RUN_ONCE];
39     struct openssl_ctx_onfree_list_st *onfreelist;
40 };
41
42 #ifndef FIPS_MODULE
43 static OPENSSL_CTX default_context_int;
44
45 /* Always points at default_context_int if it has been initialised */
46 static OPENSSL_CTX *default_context = NULL;
47 #endif
48
49 static int context_init(OPENSSL_CTX *ctx)
50 {
51     size_t i;
52     int exdata_done = 0;
53
54     ctx->lock = CRYPTO_THREAD_lock_new();
55     if (ctx->lock == NULL)
56         return 0;
57
58     ctx->oncelock = CRYPTO_THREAD_lock_new();
59     if (ctx->oncelock == NULL)
60         goto err;
61
62     for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++) {
63         ctx->index_locks[i] = CRYPTO_THREAD_lock_new();
64         ctx->dyn_indexes[i] = -1;
65         if (ctx->index_locks[i] == NULL)
66             goto err;
67     }
68
69     /* OPENSSL_CTX is built on top of ex_data so we initialise that directly */
70     if (!do_ex_data_init(ctx))
71         goto err;
72     exdata_done = 1;
73
74     if (!crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
75                                &ctx->data)) {
76         crypto_cleanup_all_ex_data_int(ctx);
77         goto err;
78     }
79
80     /* Everything depends on properties, so we also pre-initialise that */
81     if (!ossl_property_parse_init(ctx))
82         goto err;
83
84     return 1;
85  err:
86     if (exdata_done)
87         crypto_cleanup_all_ex_data_int(ctx);
88     CRYPTO_THREAD_lock_free(ctx->oncelock);
89     CRYPTO_THREAD_lock_free(ctx->lock);
90     ctx->lock = NULL;
91     return 0;
92 }
93
94 static int context_deinit(OPENSSL_CTX *ctx)
95 {
96     struct openssl_ctx_onfree_list_st *tmp, *onfree;
97     int i;
98
99     if (ctx == NULL)
100         return 1;
101
102     ossl_ctx_thread_stop(ctx);
103
104     onfree = ctx->onfreelist;
105     while (onfree != NULL) {
106         onfree->fn(ctx);
107         tmp = onfree;
108         onfree = onfree->next;
109         OPENSSL_free(tmp);
110     }
111     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
112     crypto_cleanup_all_ex_data_int(ctx);
113     for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++)
114         CRYPTO_THREAD_lock_free(ctx->index_locks[i]);
115
116     CRYPTO_THREAD_lock_free(ctx->oncelock);
117     CRYPTO_THREAD_lock_free(ctx->lock);
118     ctx->lock = NULL;
119     return 1;
120 }
121
122 #ifndef FIPS_MODULE
123 void openssl_ctx_default_deinit(void)
124 {
125     context_deinit(default_context);
126 }
127
128 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
129 DEFINE_RUN_ONCE_STATIC(do_default_context_init)
130 {
131     if (context_init(&default_context_int))
132         default_context = &default_context_int;
133
134     return 1;
135 }
136 #endif
137
138 OPENSSL_CTX *OPENSSL_CTX_new(void)
139 {
140     OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
141
142     if (ctx != NULL && !context_init(ctx)) {
143         OPENSSL_CTX_free(ctx);
144         ctx = NULL;
145     }
146     return ctx;
147 }
148
149 #ifndef FIPS_MODULE
150 int OPENSSL_CTX_load_config(OPENSSL_CTX *ctx, const char *config_file)
151 {
152     return CONF_modules_load_file_with_libctx(ctx, config_file, NULL, 0) > 0;
153 }
154 #endif
155
156 void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
157 {
158     if (ctx != NULL)
159         context_deinit(ctx);
160     OPENSSL_free(ctx);
161 }
162
163 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx)
164 {
165 #ifndef FIPS_MODULE
166     if (ctx == NULL) {
167         if (!RUN_ONCE(&default_context_init, do_default_context_init))
168             return 0;
169         return default_context;
170     }
171 #endif
172     return ctx;
173 }
174
175 int openssl_ctx_is_default(OPENSSL_CTX *ctx)
176 {
177 #ifndef FIPS_MODULE
178     if (ctx == NULL || ctx == default_context)
179         return 1;
180 #endif
181     return 0;
182 }
183
184 static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
185                                     CRYPTO_EX_DATA *ad, int index,
186                                     long argl_ign, void *argp)
187 {
188     const OPENSSL_CTX_METHOD *meth = argp;
189     void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad));
190
191     if (ptr != NULL)
192         CRYPTO_set_ex_data(ad, index, ptr);
193 }
194 static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
195                                      CRYPTO_EX_DATA *ad, int index,
196                                      long argl_ign, void *argp)
197 {
198     const OPENSSL_CTX_METHOD *meth = argp;
199
200     meth->free_func(ptr);
201 }
202
203 /* Non-static so we can use it in context_internal_test */
204 static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index,
205                                   const OPENSSL_CTX_METHOD *meth)
206 {
207     int idx;
208
209     ctx = openssl_ctx_get_concrete(ctx);
210     if (ctx == NULL)
211         return 0;
212
213     idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0,
214                                      (void *)meth,
215                                      openssl_ctx_generic_new,
216                                      NULL, openssl_ctx_generic_free);
217     if (idx < 0)
218         return 0;
219
220     ctx->dyn_indexes[static_index] = idx;
221     return 1;
222 }
223
224 void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
225                            const OPENSSL_CTX_METHOD *meth)
226 {
227     void *data = NULL;
228     int dynidx;
229
230     ctx = openssl_ctx_get_concrete(ctx);
231     if (ctx == NULL)
232         return NULL;
233
234     CRYPTO_THREAD_read_lock(ctx->lock);
235     dynidx = ctx->dyn_indexes[index];
236     CRYPTO_THREAD_unlock(ctx->lock);
237
238     if (dynidx != -1) {
239         CRYPTO_THREAD_read_lock(ctx->index_locks[index]);
240         data = CRYPTO_get_ex_data(&ctx->data, dynidx);
241         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
242         return data;
243     }
244
245     CRYPTO_THREAD_write_lock(ctx->index_locks[index]);
246     CRYPTO_THREAD_write_lock(ctx->lock);
247
248     dynidx = ctx->dyn_indexes[index];
249     if (dynidx != -1) {
250         CRYPTO_THREAD_unlock(ctx->lock);
251         data = CRYPTO_get_ex_data(&ctx->data, dynidx);
252         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
253         return data;
254     }
255
256     if (!openssl_ctx_init_index(ctx, index, meth)) {
257         CRYPTO_THREAD_unlock(ctx->lock);
258         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
259         return NULL;
260     }
261
262     CRYPTO_THREAD_unlock(ctx->lock);
263
264     /* The alloc call ensures there's a value there */
265     if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
266                              &ctx->data, ctx->dyn_indexes[index]))
267         data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
268
269     CRYPTO_THREAD_unlock(ctx->index_locks[index]);
270
271     return data;
272 }
273
274 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)
275 {
276     ctx = openssl_ctx_get_concrete(ctx);
277     if (ctx == NULL)
278         return NULL;
279     return &ctx->global;
280 }
281
282 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
283                          openssl_ctx_run_once_fn run_once_fn)
284 {
285     int done = 0, ret = 0;
286
287     ctx = openssl_ctx_get_concrete(ctx);
288     if (ctx == NULL)
289         return 0;
290
291     CRYPTO_THREAD_read_lock(ctx->oncelock);
292     done = ctx->run_once_done[idx];
293     if (done)
294         ret = ctx->run_once_ret[idx];
295     CRYPTO_THREAD_unlock(ctx->oncelock);
296
297     if (done)
298         return ret;
299
300     CRYPTO_THREAD_write_lock(ctx->oncelock);
301     if (ctx->run_once_done[idx]) {
302         ret = ctx->run_once_ret[idx];
303         CRYPTO_THREAD_unlock(ctx->oncelock);
304         return ret;
305     }
306
307     ret = run_once_fn(ctx);
308     ctx->run_once_done[idx] = 1;
309     ctx->run_once_ret[idx] = ret;
310     CRYPTO_THREAD_unlock(ctx->oncelock);
311
312     return ret;
313 }
314
315 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
316 {
317     struct openssl_ctx_onfree_list_st *newonfree
318         = OPENSSL_malloc(sizeof(*newonfree));
319
320     if (newonfree == NULL)
321         return 0;
322
323     newonfree->fn = onfreefn;
324     newonfree->next = ctx->onfreelist;
325     ctx->onfreelist = newonfree;
326
327     return 1;
328 }