]>
Commit | Line | Data |
---|---|---|
72592b86 | 1 | /* |
4333b89f | 2 | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
72592b86 MC |
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 <openssl/crypto.h> | |
23c48d94 | 11 | #include <openssl/core_dispatch.h> |
25f2138b | 12 | #include "crypto/cryptlib.h" |
ddd21319 | 13 | #include "prov/providercommon.h" |
6913f5fe | 14 | #include "internal/thread_once.h" |
da747958 | 15 | |
f844f9eb | 16 | #ifdef FIPS_MODULE |
f6b72c7d MC |
17 | #include "prov/provider_ctx.h" |
18 | ||
da747958 MC |
19 | /* |
20 | * Thread aware code may want to be told about thread stop events. We register | |
21 | * to hear about those thread stop events when we see a new thread has started. | |
22 | * We call the ossl_init_thread_start function to do that. In the FIPS provider | |
23 | * we have our own copy of ossl_init_thread_start, which cascades notifications | |
24 | * about threads stopping from libcrypto to all the code in the FIPS provider | |
25 | * that needs to know about it. | |
4bd8b240 | 26 | * |
da747958 MC |
27 | * The FIPS provider tells libcrypto about which threads it is interested in |
28 | * by calling "c_thread_start" which is a function pointer created during | |
29 | * provider initialisation (i.e. OSSL_init_provider). | |
30 | */ | |
363b1e5d | 31 | extern OSSL_FUNC_core_thread_start_fn *c_thread_start; |
da747958 | 32 | #endif |
72592b86 MC |
33 | |
34 | typedef struct thread_event_handler_st THREAD_EVENT_HANDLER; | |
35 | struct thread_event_handler_st { | |
ec91f1ae | 36 | #ifndef FIPS_MODULE |
6913f5fe | 37 | const void *index; |
ec91f1ae | 38 | #endif |
da747958 MC |
39 | void *arg; |
40 | OSSL_thread_stop_handler_fn handfn; | |
72592b86 MC |
41 | THREAD_EVENT_HANDLER *next; |
42 | }; | |
43 | ||
f844f9eb | 44 | #ifndef FIPS_MODULE |
6913f5fe MC |
45 | DEFINE_SPECIAL_STACK_OF(THREAD_EVENT_HANDLER_PTR, THREAD_EVENT_HANDLER *) |
46 | ||
47 | typedef struct global_tevent_register_st GLOBAL_TEVENT_REGISTER; | |
48 | struct global_tevent_register_st { | |
49 | STACK_OF(THREAD_EVENT_HANDLER_PTR) *skhands; | |
50 | CRYPTO_RWLOCK *lock; | |
51 | }; | |
52 | ||
53 | static GLOBAL_TEVENT_REGISTER *glob_tevent_reg = NULL; | |
54 | ||
55 | static CRYPTO_ONCE tevent_register_runonce = CRYPTO_ONCE_STATIC_INIT; | |
56 | ||
57 | DEFINE_RUN_ONCE_STATIC(create_global_tevent_register) | |
58 | { | |
59 | glob_tevent_reg = OPENSSL_zalloc(sizeof(*glob_tevent_reg)); | |
60 | if (glob_tevent_reg == NULL) | |
61 | return 0; | |
62 | ||
63 | glob_tevent_reg->skhands = sk_THREAD_EVENT_HANDLER_PTR_new_null(); | |
64 | glob_tevent_reg->lock = CRYPTO_THREAD_lock_new(); | |
65 | if (glob_tevent_reg->skhands == NULL || glob_tevent_reg->lock == NULL) { | |
66 | sk_THREAD_EVENT_HANDLER_PTR_free(glob_tevent_reg->skhands); | |
67 | CRYPTO_THREAD_lock_free(glob_tevent_reg->lock); | |
68 | OPENSSL_free(glob_tevent_reg); | |
69 | glob_tevent_reg = NULL; | |
70 | return 0; | |
71 | } | |
72 | ||
73 | return 1; | |
74 | } | |
75 | ||
76 | static GLOBAL_TEVENT_REGISTER *get_global_tevent_register(void) | |
77 | { | |
78 | if (!RUN_ONCE(&tevent_register_runonce, create_global_tevent_register)) | |
79 | return NULL; | |
80 | return glob_tevent_reg; | |
81 | } | |
82 | #endif | |
83 | ||
f844f9eb | 84 | #ifndef FIPS_MODULE |
3b438ef9 DMSP |
85 | static int init_thread_push_handlers(THREAD_EVENT_HANDLER **hands); |
86 | static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin); | |
87 | static void init_thread_destructor(void *hands); | |
88 | static int init_thread_deregister(void *arg, int all); | |
89 | #endif | |
2be8c56a | 90 | static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands); |
da747958 MC |
91 | |
92 | static THREAD_EVENT_HANDLER ** | |
2be8c56a | 93 | init_get_thread_local(CRYPTO_THREAD_LOCAL *local, int alloc, int keep) |
da747958 MC |
94 | { |
95 | THREAD_EVENT_HANDLER **hands = CRYPTO_THREAD_get_local(local); | |
96 | ||
97 | if (alloc) { | |
6913f5fe | 98 | if (hands == NULL) { |
6913f5fe | 99 | |
3b438ef9 | 100 | if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL) |
6913f5fe | 101 | return NULL; |
6913f5fe | 102 | |
3b438ef9 | 103 | if (!CRYPTO_THREAD_set_local(local, hands)) { |
6913f5fe MC |
104 | OPENSSL_free(hands); |
105 | return NULL; | |
106 | } | |
3b438ef9 | 107 | |
f844f9eb | 108 | #ifndef FIPS_MODULE |
3b438ef9 DMSP |
109 | if (!init_thread_push_handlers(hands)) { |
110 | CRYPTO_THREAD_set_local(local, NULL); | |
6913f5fe | 111 | OPENSSL_free(hands); |
6913f5fe MC |
112 | return NULL; |
113 | } | |
6913f5fe | 114 | #endif |
da747958 MC |
115 | } |
116 | } else if (!keep) { | |
117 | CRYPTO_THREAD_set_local(local, NULL); | |
118 | } | |
119 | ||
120 | return hands; | |
121 | } | |
e41faf57 | 122 | |
f844f9eb | 123 | #ifndef FIPS_MODULE |
72592b86 MC |
124 | /* |
125 | * Since per-thread-specific-data destructors are not universally | |
126 | * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key | |
127 | * is assumed to have destructor associated. And then an effort is made | |
128 | * to call this single destructor on non-pthread platform[s]. | |
129 | * | |
130 | * Initial value is "impossible". It is used as guard value to shortcut | |
131 | * destructor for threads terminating before libcrypto is initialized or | |
132 | * after it's de-initialized. Access to the key doesn't have to be | |
133 | * serialized for the said threads, because they didn't use libcrypto | |
c2969ff6 | 134 | * and it doesn't matter if they pick "impossible" or dereference real |
72592b86 MC |
135 | * key value and pull NULL past initialization in the first thread that |
136 | * intends to use libcrypto. | |
137 | */ | |
138 | static union { | |
139 | long sane; | |
140 | CRYPTO_THREAD_LOCAL value; | |
141 | } destructor_key = { -1 }; | |
142 | ||
3b438ef9 DMSP |
143 | /* |
144 | * The thread event handler list is a thread specific linked list | |
145 | * of callback functions which are invoked in list order by the | |
146 | * current thread in case of certain events. (Currently, there is | |
147 | * only one type of event, the 'thread stop' event.) | |
148 | * | |
149 | * We also keep a global reference to that linked list, so that we | |
150 | * can deregister handlers if necessary before all the threads are | |
151 | * stopped. | |
152 | */ | |
153 | static int init_thread_push_handlers(THREAD_EVENT_HANDLER **hands) | |
154 | { | |
155 | int ret; | |
156 | GLOBAL_TEVENT_REGISTER *gtr; | |
157 | ||
158 | gtr = get_global_tevent_register(); | |
159 | if (gtr == NULL) | |
160 | return 0; | |
161 | ||
cd3f8c1b RS |
162 | if (!CRYPTO_THREAD_write_lock(gtr->lock)) |
163 | return 0; | |
3b438ef9 DMSP |
164 | ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0); |
165 | CRYPTO_THREAD_unlock(gtr->lock); | |
166 | ||
167 | return ret; | |
168 | } | |
169 | ||
6913f5fe MC |
170 | static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin) |
171 | { | |
172 | GLOBAL_TEVENT_REGISTER *gtr; | |
173 | int i; | |
174 | ||
175 | gtr = get_global_tevent_register(); | |
176 | if (gtr == NULL) | |
177 | return; | |
cd3f8c1b RS |
178 | if (!CRYPTO_THREAD_write_lock(gtr->lock)) |
179 | return; | |
6913f5fe MC |
180 | for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) { |
181 | THREAD_EVENT_HANDLER **hands | |
182 | = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i); | |
183 | ||
184 | if (hands == handsin) { | |
30742e8e | 185 | sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i); |
6913f5fe MC |
186 | CRYPTO_THREAD_unlock(gtr->lock); |
187 | return; | |
188 | } | |
189 | } | |
190 | CRYPTO_THREAD_unlock(gtr->lock); | |
191 | return; | |
192 | } | |
193 | ||
2be8c56a | 194 | static void init_thread_destructor(void *hands) |
72592b86 | 195 | { |
2be8c56a | 196 | init_thread_stop(NULL, (THREAD_EVENT_HANDLER **)hands); |
6913f5fe | 197 | init_thread_remove_handlers(hands); |
da747958 | 198 | OPENSSL_free(hands); |
72592b86 MC |
199 | } |
200 | ||
2be8c56a | 201 | int ossl_init_thread(void) |
72592b86 | 202 | { |
72592b86 | 203 | if (!CRYPTO_THREAD_init_local(&destructor_key.value, |
2be8c56a | 204 | init_thread_destructor)) |
72592b86 MC |
205 | return 0; |
206 | ||
207 | return 1; | |
208 | } | |
209 | ||
2be8c56a | 210 | void ossl_cleanup_thread(void) |
72592b86 | 211 | { |
6913f5fe | 212 | init_thread_deregister(NULL, 1); |
72592b86 MC |
213 | CRYPTO_THREAD_cleanup_local(&destructor_key.value); |
214 | destructor_key.sane = -1; | |
215 | } | |
216 | ||
b4250010 | 217 | void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx) |
52b18ce1 | 218 | { |
b4250010 | 219 | ctx = ossl_lib_ctx_get_concrete(ctx); |
52b18ce1 | 220 | /* |
703c4d66 MC |
221 | * It would be nice if we could figure out a way to do this on all threads |
222 | * that have used the OSSL_LIB_CTX when the context is freed. This is | |
223 | * currently not possible due to the use of thread local variables. | |
52b18ce1 MC |
224 | */ |
225 | ossl_ctx_thread_stop(ctx); | |
226 | } | |
227 | ||
da747958 | 228 | void OPENSSL_thread_stop(void) |
72592b86 | 229 | { |
da747958 MC |
230 | if (destructor_key.sane != -1) { |
231 | THREAD_EVENT_HANDLER **hands | |
2be8c56a MC |
232 | = init_get_thread_local(&destructor_key.value, 0, 0); |
233 | init_thread_stop(NULL, hands); | |
6913f5fe MC |
234 | |
235 | init_thread_remove_handlers(hands); | |
da747958 | 236 | OPENSSL_free(hands); |
72592b86 | 237 | } |
72592b86 MC |
238 | } |
239 | ||
da747958 | 240 | void ossl_ctx_thread_stop(void *arg) |
e41faf57 | 241 | { |
da747958 MC |
242 | if (destructor_key.sane != -1) { |
243 | THREAD_EVENT_HANDLER **hands | |
2be8c56a MC |
244 | = init_get_thread_local(&destructor_key.value, 0, 1); |
245 | init_thread_stop(arg, hands); | |
da747958 | 246 | } |
e41faf57 | 247 | } |
da747958 | 248 | |
e41faf57 | 249 | #else |
da747958 | 250 | |
b4250010 | 251 | static void *thread_event_ossl_ctx_new(OSSL_LIB_CTX *libctx) |
e41faf57 MC |
252 | { |
253 | THREAD_EVENT_HANDLER **hands = NULL; | |
da747958 | 254 | CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal)); |
e41faf57 MC |
255 | |
256 | if (tlocal == NULL) | |
257 | return NULL; | |
258 | ||
da747958 MC |
259 | if (!CRYPTO_THREAD_init_local(tlocal, NULL)) { |
260 | goto err; | |
261 | } | |
262 | ||
e41faf57 MC |
263 | hands = OPENSSL_zalloc(sizeof(*hands)); |
264 | if (hands == NULL) | |
265 | goto err; | |
266 | ||
267 | if (!CRYPTO_THREAD_set_local(tlocal, hands)) | |
268 | goto err; | |
269 | ||
270 | return tlocal; | |
271 | err: | |
272 | OPENSSL_free(hands); | |
273 | OPENSSL_free(tlocal); | |
274 | return NULL; | |
275 | } | |
276 | ||
da747958 | 277 | static void thread_event_ossl_ctx_free(void *tlocal) |
e41faf57 | 278 | { |
e41faf57 MC |
279 | OPENSSL_free(tlocal); |
280 | } | |
281 | ||
b4250010 | 282 | static const OSSL_LIB_CTX_METHOD thread_event_ossl_ctx_method = { |
a16d2174 | 283 | OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY, |
e41faf57 MC |
284 | thread_event_ossl_ctx_new, |
285 | thread_event_ossl_ctx_free, | |
286 | }; | |
287 | ||
da747958 | 288 | void ossl_ctx_thread_stop(void *arg) |
e41faf57 MC |
289 | { |
290 | THREAD_EVENT_HANDLER **hands; | |
f6b72c7d | 291 | OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(arg); |
da747958 | 292 | CRYPTO_THREAD_LOCAL *local |
b4250010 DMSP |
293 | = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX, |
294 | &thread_event_ossl_ctx_method); | |
e41faf57 | 295 | |
da747958 MC |
296 | if (local == NULL) |
297 | return; | |
2be8c56a | 298 | hands = init_get_thread_local(local, 0, 0); |
f6b72c7d | 299 | init_thread_stop(ctx, hands); |
da747958 | 300 | OPENSSL_free(hands); |
e41faf57 | 301 | } |
f844f9eb | 302 | #endif /* FIPS_MODULE */ |
e41faf57 | 303 | |
da747958 | 304 | |
2be8c56a | 305 | static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands) |
72592b86 | 306 | { |
2dd04ca8 | 307 | THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp; |
72592b86 MC |
308 | |
309 | /* Can't do much about this */ | |
310 | if (hands == NULL) | |
311 | return; | |
312 | ||
313 | curr = *hands; | |
314 | while (curr != NULL) { | |
da747958 | 315 | if (arg != NULL && curr->arg != arg) { |
2dd04ca8 | 316 | prev = curr; |
da747958 MC |
317 | curr = curr->next; |
318 | continue; | |
319 | } | |
320 | curr->handfn(curr->arg); | |
2dd04ca8 MC |
321 | if (prev == NULL) |
322 | *hands = curr->next; | |
323 | else | |
324 | prev->next = curr->next; | |
325 | ||
326 | tmp = curr; | |
72592b86 | 327 | curr = curr->next; |
2dd04ca8 MC |
328 | |
329 | OPENSSL_free(tmp); | |
72592b86 | 330 | } |
72592b86 MC |
331 | } |
332 | ||
6913f5fe MC |
333 | int ossl_init_thread_start(const void *index, void *arg, |
334 | OSSL_thread_stop_handler_fn handfn) | |
72592b86 MC |
335 | { |
336 | THREAD_EVENT_HANDLER **hands; | |
337 | THREAD_EVENT_HANDLER *hand; | |
f844f9eb | 338 | #ifdef FIPS_MODULE |
b4250010 | 339 | OSSL_LIB_CTX *ctx = arg; |
da747958 | 340 | |
e41faf57 MC |
341 | /* |
342 | * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination | |
b4250010 DMSP |
343 | * of OSSL_LIB_CTX and thread. This is because in FIPS mode each |
344 | * OSSL_LIB_CTX gets informed about thread stop events individually. | |
e41faf57 | 345 | */ |
da747958 | 346 | CRYPTO_THREAD_LOCAL *local |
b4250010 DMSP |
347 | = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX, |
348 | &thread_event_ossl_ctx_method); | |
e41faf57 MC |
349 | #else |
350 | /* | |
351 | * Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per | |
b4250010 | 352 | * thread, but may hold multiple OSSL_LIB_CTXs. We only get told about |
e41faf57 | 353 | * thread stop events globally, so we have to ensure all affected |
b4250010 | 354 | * OSSL_LIB_CTXs are informed. |
e41faf57 | 355 | */ |
da747958 | 356 | CRYPTO_THREAD_LOCAL *local = &destructor_key.value; |
e41faf57 | 357 | #endif |
72592b86 | 358 | |
2be8c56a | 359 | hands = init_get_thread_local(local, 1, 0); |
72592b86 MC |
360 | if (hands == NULL) |
361 | return 0; | |
362 | ||
f844f9eb | 363 | #ifdef FIPS_MODULE |
da747958 MC |
364 | if (*hands == NULL) { |
365 | /* | |
366 | * We've not yet registered any handlers for this thread. We need to get | |
367 | * libcrypto to tell us about later thread stop events. c_thread_start | |
368 | * is a callback to libcrypto defined in fipsprov.c | |
369 | */ | |
d40b42ab | 370 | if (!c_thread_start(FIPS_get_core_handle(ctx), ossl_ctx_thread_stop)) |
da747958 MC |
371 | return 0; |
372 | } | |
373 | #endif | |
374 | ||
72592b86 MC |
375 | hand = OPENSSL_malloc(sizeof(*hand)); |
376 | if (hand == NULL) | |
377 | return 0; | |
378 | ||
379 | hand->handfn = handfn; | |
da747958 | 380 | hand->arg = arg; |
ec91f1ae | 381 | #ifndef FIPS_MODULE |
6913f5fe | 382 | hand->index = index; |
ec91f1ae | 383 | #endif |
72592b86 MC |
384 | hand->next = *hands; |
385 | *hands = hand; | |
386 | ||
387 | return 1; | |
388 | } | |
6913f5fe | 389 | |
f844f9eb | 390 | #ifndef FIPS_MODULE |
6913f5fe MC |
391 | static int init_thread_deregister(void *index, int all) |
392 | { | |
393 | GLOBAL_TEVENT_REGISTER *gtr; | |
394 | int i; | |
395 | ||
396 | gtr = get_global_tevent_register(); | |
3478a210 P |
397 | if (gtr == NULL) |
398 | return 0; | |
cd3f8c1b RS |
399 | if (!all) { |
400 | if (!CRYPTO_THREAD_write_lock(gtr->lock)) | |
401 | return 0; | |
402 | } else { | |
3d7e7e7c | 403 | glob_tevent_reg = NULL; |
cd3f8c1b | 404 | } |
6913f5fe MC |
405 | for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) { |
406 | THREAD_EVENT_HANDLER **hands | |
407 | = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i); | |
59ed7339 | 408 | THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp; |
6913f5fe MC |
409 | |
410 | if (hands == NULL) { | |
411 | if (!all) | |
412 | CRYPTO_THREAD_unlock(gtr->lock); | |
413 | return 0; | |
414 | } | |
59ed7339 | 415 | curr = *hands; |
6913f5fe MC |
416 | while (curr != NULL) { |
417 | if (all || curr->index == index) { | |
418 | if (prev != NULL) | |
419 | prev->next = curr->next; | |
420 | else | |
421 | *hands = curr->next; | |
422 | tmp = curr; | |
423 | curr = curr->next; | |
424 | OPENSSL_free(tmp); | |
425 | continue; | |
426 | } | |
427 | prev = curr; | |
428 | curr = curr->next; | |
429 | } | |
430 | if (all) | |
431 | OPENSSL_free(hands); | |
432 | } | |
433 | if (all) { | |
434 | CRYPTO_THREAD_lock_free(gtr->lock); | |
435 | sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands); | |
436 | OPENSSL_free(gtr); | |
437 | } else { | |
438 | CRYPTO_THREAD_unlock(gtr->lock); | |
439 | } | |
440 | return 1; | |
441 | } | |
442 | ||
443 | int ossl_init_thread_deregister(void *index) | |
444 | { | |
445 | return init_thread_deregister(index, 0); | |
446 | } | |
447 | #endif |