Constify various mostly X509-related parameter types in crypto/ and apps/
[oweals/openssl.git] / crypto / context.c
1 /*
2  * Copyright 2019 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_MODE
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_MODE
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_MODE
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_MODE
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 static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
176                                     CRYPTO_EX_DATA *ad, int index,
177                                     long argl_ign, void *argp)
178 {
179     const OPENSSL_CTX_METHOD *meth = argp;
180     void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad));
181
182     if (ptr != NULL)
183         CRYPTO_set_ex_data(ad, index, ptr);
184 }
185 static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
186                                      CRYPTO_EX_DATA *ad, int index,
187                                      long argl_ign, void *argp)
188 {
189     const OPENSSL_CTX_METHOD *meth = argp;
190
191     meth->free_func(ptr);
192 }
193
194 /* Non-static so we can use it in context_internal_test */
195 static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index,
196                                   const OPENSSL_CTX_METHOD *meth)
197 {
198     int idx;
199
200     ctx = openssl_ctx_get_concrete(ctx);
201     if (ctx == NULL)
202         return 0;
203
204     idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0,
205                                      (void *)meth,
206                                      openssl_ctx_generic_new,
207                                      NULL, openssl_ctx_generic_free);
208     if (idx < 0)
209         return 0;
210
211     ctx->dyn_indexes[static_index] = idx;
212     return 1;
213 }
214
215 void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
216                            const OPENSSL_CTX_METHOD *meth)
217 {
218     void *data = NULL;
219     int dynidx;
220
221     ctx = openssl_ctx_get_concrete(ctx);
222     if (ctx == NULL)
223         return NULL;
224
225     CRYPTO_THREAD_read_lock(ctx->lock);
226     dynidx = ctx->dyn_indexes[index];
227     CRYPTO_THREAD_unlock(ctx->lock);
228
229     if (dynidx != -1) {
230         CRYPTO_THREAD_read_lock(ctx->index_locks[index]);
231         data = CRYPTO_get_ex_data(&ctx->data, dynidx);
232         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
233         return data;
234     }
235
236     CRYPTO_THREAD_write_lock(ctx->index_locks[index]);
237     CRYPTO_THREAD_write_lock(ctx->lock);
238
239     dynidx = ctx->dyn_indexes[index];
240     if (dynidx != -1) {
241         CRYPTO_THREAD_unlock(ctx->lock);
242         data = CRYPTO_get_ex_data(&ctx->data, dynidx);
243         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
244         return data;
245     }
246
247     if (!openssl_ctx_init_index(ctx, index, meth)) {
248         CRYPTO_THREAD_unlock(ctx->lock);
249         CRYPTO_THREAD_unlock(ctx->index_locks[index]);
250         return NULL;
251     }
252
253     CRYPTO_THREAD_unlock(ctx->lock);
254
255     /* The alloc call ensures there's a value there */
256     if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
257                              &ctx->data, ctx->dyn_indexes[index]))
258         data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
259
260     CRYPTO_THREAD_unlock(ctx->index_locks[index]);
261
262     return data;
263 }
264
265 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)
266 {
267     ctx = openssl_ctx_get_concrete(ctx);
268     if (ctx == NULL)
269         return NULL;
270     return &ctx->global;
271 }
272
273 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
274                          openssl_ctx_run_once_fn run_once_fn)
275 {
276     int done = 0, ret = 0;
277
278     ctx = openssl_ctx_get_concrete(ctx);
279     if (ctx == NULL)
280         return 0;
281
282     CRYPTO_THREAD_read_lock(ctx->oncelock);
283     done = ctx->run_once_done[idx];
284     if (done)
285         ret = ctx->run_once_ret[idx];
286     CRYPTO_THREAD_unlock(ctx->oncelock);
287
288     if (done)
289         return ret;
290
291     CRYPTO_THREAD_write_lock(ctx->oncelock);
292     if (ctx->run_once_done[idx]) {
293         ret = ctx->run_once_ret[idx];
294         CRYPTO_THREAD_unlock(ctx->oncelock);
295         return ret;
296     }
297
298     ret = run_once_fn(ctx);
299     ctx->run_once_done[idx] = 1;
300     ctx->run_once_ret[idx] = ret;
301     CRYPTO_THREAD_unlock(ctx->oncelock);
302
303     return ret;
304 }
305
306 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
307 {
308     struct openssl_ctx_onfree_list_st *newonfree
309         = OPENSSL_malloc(sizeof(*newonfree));
310
311     if (newonfree == NULL)
312         return 0;
313
314     newonfree->fn = onfreefn;
315     newonfree->next = ctx->onfreelist;
316     ctx->onfreelist = newonfree;
317
318     return 1;
319 }