]>
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 | ||
10 | #include "internal/cryptlib.h" | |
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 | |
1aedc35f | 83 | onfree = ctx->onfreelist; |
b8fe36fe MC |
84 | while (onfree != NULL) { |
85 | onfree->fn(ctx); | |
86 | tmp = onfree; | |
87 | onfree = onfree->next; | |
88 | OPENSSL_free(tmp); | |
89 | } | |
d64b6299 | 90 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data); |
1aedc35f MC |
91 | crypto_cleanup_all_ex_data_int(ctx); |
92 | CRYPTO_THREAD_lock_free(ctx->oncelock); | |
d64b6299 | 93 | CRYPTO_THREAD_lock_free(ctx->lock); |
1aedc35f | 94 | ctx->lock = NULL; |
d64b6299 RL |
95 | return 1; |
96 | } | |
97 | ||
1aedc35f MC |
98 | #ifndef FIPS_MODE |
99 | void openssl_ctx_default_deinit(void) | |
d64b6299 | 100 | { |
1aedc35f | 101 | context_deinit(default_context); |
d64b6299 | 102 | } |
1aedc35f MC |
103 | |
104 | static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT; | |
d64b6299 RL |
105 | DEFINE_RUN_ONCE_STATIC(do_default_context_init) |
106 | { | |
1aedc35f MC |
107 | if (context_init(&default_context_int)) |
108 | default_context = &default_context_int; | |
109 | ||
110 | return 1; | |
d64b6299 | 111 | } |
1aedc35f | 112 | #endif |
d64b6299 RL |
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 | ||
d4c051ce MC |
132 | OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx) |
133 | { | |
134 | #ifndef FIPS_MODE | |
135 | if (ctx == NULL) { | |
136 | if (!RUN_ONCE(&default_context_init, do_default_context_init)) | |
137 | return 0; | |
138 | return default_context; | |
139 | } | |
140 | #endif | |
141 | return ctx; | |
142 | } | |
143 | ||
d64b6299 RL |
144 | static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign, |
145 | CRYPTO_EX_DATA *ad, int index, | |
146 | long argl_ign, void *argp) | |
147 | { | |
148 | const OPENSSL_CTX_METHOD *meth = argp; | |
1aedc35f | 149 | void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad)); |
d64b6299 RL |
150 | |
151 | if (ptr != NULL) | |
152 | CRYPTO_set_ex_data(ad, index, ptr); | |
153 | } | |
154 | static void openssl_ctx_generic_free(void *parent_ign, void *ptr, | |
155 | CRYPTO_EX_DATA *ad, int index, | |
156 | long argl_ign, void *argp) | |
157 | { | |
158 | const OPENSSL_CTX_METHOD *meth = argp; | |
159 | ||
160 | meth->free_func(ptr); | |
161 | } | |
1aedc35f MC |
162 | |
163 | /* Non-static so we can use it in context_internal_test */ | |
164 | static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index, | |
165 | const OPENSSL_CTX_METHOD *meth) | |
d64b6299 | 166 | { |
1aedc35f MC |
167 | int idx; |
168 | ||
d4c051ce | 169 | ctx = openssl_ctx_get_concrete(ctx); |
1aedc35f MC |
170 | if (ctx == NULL) |
171 | return 0; | |
172 | ||
173 | idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0, | |
174 | (void *)meth, | |
175 | openssl_ctx_generic_new, | |
176 | NULL, openssl_ctx_generic_free); | |
177 | if (idx < 0) | |
178 | return 0; | |
179 | ||
180 | ctx->dyn_indexes[static_index] = idx; | |
181 | return 1; | |
d64b6299 RL |
182 | } |
183 | ||
1aedc35f MC |
184 | void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index, |
185 | const OPENSSL_CTX_METHOD *meth) | |
d64b6299 RL |
186 | { |
187 | void *data = NULL; | |
188 | ||
d4c051ce | 189 | ctx = openssl_ctx_get_concrete(ctx); |
b8fe36fe MC |
190 | if (ctx == NULL) |
191 | return NULL; | |
d64b6299 RL |
192 | |
193 | CRYPTO_THREAD_read_lock(ctx->lock); | |
194 | ||
1aedc35f MC |
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 | ||
d64b6299 RL |
201 | /* The alloc call ensures there's a value there */ |
202 | if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, | |
1aedc35f MC |
203 | &ctx->data, ctx->dyn_indexes[index])) |
204 | data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]); | |
d64b6299 RL |
205 | |
206 | CRYPTO_THREAD_unlock(ctx->lock); | |
207 | ||
208 | return data; | |
209 | } | |
210 | ||
1aedc35f MC |
211 | OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx) |
212 | { | |
d4c051ce | 213 | ctx = openssl_ctx_get_concrete(ctx); |
1aedc35f MC |
214 | if (ctx == NULL) |
215 | return NULL; | |
216 | return &ctx->global; | |
217 | } | |
218 | ||
b8fe36fe MC |
219 | int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx, |
220 | openssl_ctx_run_once_fn run_once_fn) | |
221 | { | |
222 | int done = 0, ret = 0; | |
223 | ||
d4c051ce | 224 | ctx = openssl_ctx_get_concrete(ctx); |
b8fe36fe MC |
225 | if (ctx == NULL) |
226 | return 0; | |
227 | ||
1aedc35f | 228 | CRYPTO_THREAD_read_lock(ctx->oncelock); |
b8fe36fe MC |
229 | done = ctx->run_once_done[idx]; |
230 | if (done) | |
231 | ret = ctx->run_once_ret[idx]; | |
1aedc35f | 232 | CRYPTO_THREAD_unlock(ctx->oncelock); |
b8fe36fe MC |
233 | |
234 | if (done) | |
235 | return ret; | |
236 | ||
1aedc35f | 237 | CRYPTO_THREAD_write_lock(ctx->oncelock); |
b8fe36fe MC |
238 | if (ctx->run_once_done[idx]) { |
239 | ret = ctx->run_once_ret[idx]; | |
1aedc35f | 240 | CRYPTO_THREAD_unlock(ctx->oncelock); |
b8fe36fe MC |
241 | return ret; |
242 | } | |
243 | ||
244 | ret = run_once_fn(ctx); | |
245 | ctx->run_once_done[idx] = 1; | |
246 | ctx->run_once_ret[idx] = ret; | |
1aedc35f | 247 | CRYPTO_THREAD_unlock(ctx->oncelock); |
b8fe36fe MC |
248 | |
249 | return ret; | |
250 | } | |
251 | ||
252 | int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn) | |
253 | { | |
254 | struct openssl_ctx_onfree_list_st *newonfree | |
255 | = OPENSSL_malloc(sizeof(*newonfree)); | |
256 | ||
257 | if (newonfree == NULL) | |
258 | return 0; | |
259 | ||
260 | newonfree->fn = onfreefn; | |
261 | newonfree->next = ctx->onfreelist; | |
262 | ctx->onfreelist = newonfree; | |
263 | ||
264 | return 1; | |
265 | } |