]>
Commit | Line | Data |
---|---|---|
d64b6299 RL |
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 | ||
da747958 | 10 | #include "internal/cryptlib_int.h" |
d64b6299 RL |
11 | #include "internal/thread_once.h" |
12 | ||
b8fe36fe | 13 | struct openssl_ctx_onfree_list_st { |
1aedc35f | 14 | openssl_ctx_onfree_fn *fn; |
b8fe36fe MC |
15 | struct openssl_ctx_onfree_list_st *next; |
16 | }; | |
17 | ||
d64b6299 RL |
18 | struct openssl_ctx_st { |
19 | CRYPTO_RWLOCK *lock; | |
20 | CRYPTO_EX_DATA data; | |
1aedc35f MC |
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]; | |
b8fe36fe | 34 | struct openssl_ctx_onfree_list_st *onfreelist; |
d64b6299 RL |
35 | }; |
36 | ||
1aedc35f MC |
37 | #ifndef FIPS_MODE |
38 | static OPENSSL_CTX default_context_int; | |
1aedc35f MC |
39 | |
40 | /* Always points at default_context_int if it has been initialised */ | |
41 | static OPENSSL_CTX *default_context = NULL; | |
3593266d | 42 | #endif |
d64b6299 RL |
43 | |
44 | static int context_init(OPENSSL_CTX *ctx) | |
45 | { | |
1aedc35f MC |
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; | |
d64b6299 RL |
74 | } |
75 | ||
76 | static int context_deinit(OPENSSL_CTX *ctx) | |
77 | { | |
1aedc35f MC |
78 | struct openssl_ctx_onfree_list_st *tmp, *onfree; |
79 | ||
80 | if (ctx == NULL) | |
81 | return 1; | |
b8fe36fe | 82 | |
da747958 MC |
83 | ossl_ctx_thread_stop(ctx); |
84 | ||
1aedc35f | 85 | onfree = ctx->onfreelist; |
b8fe36fe MC |
86 | while (onfree != NULL) { |
87 | onfree->fn(ctx); | |
88 | tmp = onfree; | |
89 | onfree = onfree->next; | |
90 | OPENSSL_free(tmp); | |
91 | } | |
d64b6299 | 92 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data); |
1aedc35f MC |
93 | crypto_cleanup_all_ex_data_int(ctx); |
94 | CRYPTO_THREAD_lock_free(ctx->oncelock); | |
d64b6299 | 95 | CRYPTO_THREAD_lock_free(ctx->lock); |
1aedc35f | 96 | ctx->lock = NULL; |
d64b6299 RL |
97 | return 1; |
98 | } | |
99 | ||
1aedc35f MC |
100 | #ifndef FIPS_MODE |
101 | void openssl_ctx_default_deinit(void) | |
d64b6299 | 102 | { |
1aedc35f | 103 | context_deinit(default_context); |
d64b6299 | 104 | } |
1aedc35f MC |
105 | |
106 | static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT; | |
d64b6299 RL |
107 | DEFINE_RUN_ONCE_STATIC(do_default_context_init) |
108 | { | |
1aedc35f MC |
109 | if (context_init(&default_context_int)) |
110 | default_context = &default_context_int; | |
111 | ||
112 | return 1; | |
d64b6299 | 113 | } |
1aedc35f | 114 | #endif |
d64b6299 RL |
115 | |
116 | OPENSSL_CTX *OPENSSL_CTX_new(void) | |
117 | { | |
118 | OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
119 | ||
120 | if (ctx != NULL && !context_init(ctx)) { | |
121 | OPENSSL_CTX_free(ctx); | |
122 | ctx = NULL; | |
123 | } | |
124 | return ctx; | |
125 | } | |
126 | ||
127 | void OPENSSL_CTX_free(OPENSSL_CTX *ctx) | |
128 | { | |
129 | if (ctx != NULL) | |
130 | context_deinit(ctx); | |
131 | OPENSSL_free(ctx); | |
132 | } | |
133 | ||
d4c051ce MC |
134 | OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx) |
135 | { | |
136 | #ifndef FIPS_MODE | |
137 | if (ctx == NULL) { | |
138 | if (!RUN_ONCE(&default_context_init, do_default_context_init)) | |
139 | return 0; | |
140 | return default_context; | |
141 | } | |
142 | #endif | |
143 | return ctx; | |
144 | } | |
145 | ||
d64b6299 RL |
146 | static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign, |
147 | CRYPTO_EX_DATA *ad, int index, | |
148 | long argl_ign, void *argp) | |
149 | { | |
150 | const OPENSSL_CTX_METHOD *meth = argp; | |
1aedc35f | 151 | void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad)); |
d64b6299 RL |
152 | |
153 | if (ptr != NULL) | |
154 | CRYPTO_set_ex_data(ad, index, ptr); | |
155 | } | |
156 | static void openssl_ctx_generic_free(void *parent_ign, void *ptr, | |
157 | CRYPTO_EX_DATA *ad, int index, | |
158 | long argl_ign, void *argp) | |
159 | { | |
160 | const OPENSSL_CTX_METHOD *meth = argp; | |
161 | ||
162 | meth->free_func(ptr); | |
163 | } | |
1aedc35f MC |
164 | |
165 | /* Non-static so we can use it in context_internal_test */ | |
166 | static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index, | |
167 | const OPENSSL_CTX_METHOD *meth) | |
d64b6299 | 168 | { |
1aedc35f MC |
169 | int idx; |
170 | ||
d4c051ce | 171 | ctx = openssl_ctx_get_concrete(ctx); |
1aedc35f MC |
172 | if (ctx == NULL) |
173 | return 0; | |
174 | ||
175 | idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0, | |
176 | (void *)meth, | |
177 | openssl_ctx_generic_new, | |
178 | NULL, openssl_ctx_generic_free); | |
179 | if (idx < 0) | |
180 | return 0; | |
181 | ||
182 | ctx->dyn_indexes[static_index] = idx; | |
183 | return 1; | |
d64b6299 RL |
184 | } |
185 | ||
1aedc35f MC |
186 | void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index, |
187 | const OPENSSL_CTX_METHOD *meth) | |
d64b6299 RL |
188 | { |
189 | void *data = NULL; | |
190 | ||
d4c051ce | 191 | ctx = openssl_ctx_get_concrete(ctx); |
b8fe36fe MC |
192 | if (ctx == NULL) |
193 | return NULL; | |
d64b6299 RL |
194 | |
195 | CRYPTO_THREAD_read_lock(ctx->lock); | |
196 | ||
1aedc35f MC |
197 | if (ctx->dyn_indexes[index] == -1 |
198 | && !openssl_ctx_init_index(ctx, index, meth)) { | |
199 | CRYPTO_THREAD_unlock(ctx->lock); | |
200 | return NULL; | |
201 | } | |
202 | ||
d64b6299 RL |
203 | /* The alloc call ensures there's a value there */ |
204 | if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, | |
1aedc35f MC |
205 | &ctx->data, ctx->dyn_indexes[index])) |
206 | data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]); | |
d64b6299 RL |
207 | |
208 | CRYPTO_THREAD_unlock(ctx->lock); | |
209 | ||
210 | return data; | |
211 | } | |
212 | ||
1aedc35f MC |
213 | OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx) |
214 | { | |
d4c051ce | 215 | ctx = openssl_ctx_get_concrete(ctx); |
1aedc35f MC |
216 | if (ctx == NULL) |
217 | return NULL; | |
218 | return &ctx->global; | |
219 | } | |
220 | ||
b8fe36fe MC |
221 | int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx, |
222 | openssl_ctx_run_once_fn run_once_fn) | |
223 | { | |
224 | int done = 0, ret = 0; | |
225 | ||
d4c051ce | 226 | ctx = openssl_ctx_get_concrete(ctx); |
b8fe36fe MC |
227 | if (ctx == NULL) |
228 | return 0; | |
229 | ||
1aedc35f | 230 | CRYPTO_THREAD_read_lock(ctx->oncelock); |
b8fe36fe MC |
231 | done = ctx->run_once_done[idx]; |
232 | if (done) | |
233 | ret = ctx->run_once_ret[idx]; | |
1aedc35f | 234 | CRYPTO_THREAD_unlock(ctx->oncelock); |
b8fe36fe MC |
235 | |
236 | if (done) | |
237 | return ret; | |
238 | ||
1aedc35f | 239 | CRYPTO_THREAD_write_lock(ctx->oncelock); |
b8fe36fe MC |
240 | if (ctx->run_once_done[idx]) { |
241 | ret = ctx->run_once_ret[idx]; | |
1aedc35f | 242 | CRYPTO_THREAD_unlock(ctx->oncelock); |
b8fe36fe MC |
243 | return ret; |
244 | } | |
245 | ||
246 | ret = run_once_fn(ctx); | |
247 | ctx->run_once_done[idx] = 1; | |
248 | ctx->run_once_ret[idx] = ret; | |
1aedc35f | 249 | CRYPTO_THREAD_unlock(ctx->oncelock); |
b8fe36fe MC |
250 | |
251 | return ret; | |
252 | } | |
253 | ||
254 | int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn) | |
255 | { | |
256 | struct openssl_ctx_onfree_list_st *newonfree | |
257 | = OPENSSL_malloc(sizeof(*newonfree)); | |
258 | ||
259 | if (newonfree == NULL) | |
260 | return 0; | |
261 | ||
262 | newonfree->fn = onfreefn; | |
263 | newonfree->next = ctx->onfreelist; | |
264 | ctx->onfreelist = newonfree; | |
265 | ||
266 | return 1; | |
267 | } |