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