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