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