]>
Commit | Line | Data |
---|---|---|
b184e3ef | 1 | /* |
48e5119a | 2 | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
b184e3ef | 3 | * |
2039c421 RS |
4 | * Licensed under the OpenSSL license (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 | |
b184e3ef MC |
8 | */ |
9 | ||
07016a8a | 10 | #include "e_os.h" |
176db6dc | 11 | #include "internal/cryptlib_int.h" |
b184e3ef | 12 | #include <openssl/err.h> |
176db6dc RS |
13 | #include "internal/rand_int.h" |
14 | #include "internal/bio.h" | |
b184e3ef | 15 | #include <openssl/evp.h> |
176db6dc RS |
16 | #include "internal/evp_int.h" |
17 | #include "internal/conf.h" | |
18 | #include "internal/async.h" | |
19 | #include "internal/engine.h" | |
20 | #include "internal/comp.h" | |
21 | #include "internal/err.h" | |
22 | #include "internal/err_int.h" | |
23 | #include "internal/objects.h" | |
b184e3ef | 24 | #include <stdlib.h> |
dd27f16e | 25 | #include <assert.h> |
176db6dc | 26 | #include "internal/thread_once.h" |
b71fa7b3 | 27 | #include "internal/dso_conf.h" |
176db6dc RS |
28 | #include "internal/dso.h" |
29 | #include "internal/store.h" | |
dd27f16e RS |
30 | |
31 | static int stopped = 0; | |
b184e3ef | 32 | |
71567a6f MC |
33 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals); |
34 | ||
a072ed0c | 35 | static CRYPTO_THREAD_LOCAL threadstopkey; |
b184e3ef | 36 | |
b184e3ef MC |
37 | static void ossl_init_thread_stop_wrap(void *local) |
38 | { | |
39 | ossl_init_thread_stop((struct thread_local_inits_st *)local); | |
40 | } | |
41 | ||
b7326ea7 | 42 | static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc) |
b184e3ef | 43 | { |
a072ed0c MC |
44 | struct thread_local_inits_st *local = |
45 | CRYPTO_THREAD_get_local(&threadstopkey); | |
b184e3ef MC |
46 | |
47 | if (local == NULL && alloc) { | |
cbe29648 | 48 | local = OPENSSL_zalloc(sizeof(*local)); |
3ac6d5ee BE |
49 | if (local != NULL && !CRYPTO_THREAD_set_local(&threadstopkey, local)) { |
50 | OPENSSL_free(local); | |
51 | return NULL; | |
52 | } | |
b184e3ef | 53 | } |
b7326ea7 | 54 | if (!alloc) { |
a072ed0c | 55 | CRYPTO_THREAD_set_local(&threadstopkey, NULL); |
b7326ea7 | 56 | } |
b184e3ef MC |
57 | |
58 | return local; | |
59 | } | |
60 | ||
7253fd55 | 61 | typedef struct ossl_init_stop_st OPENSSL_INIT_STOP; |
b184e3ef MC |
62 | struct ossl_init_stop_st { |
63 | void (*handler)(void); | |
64 | OPENSSL_INIT_STOP *next; | |
65 | }; | |
66 | ||
67 | static OPENSSL_INIT_STOP *stop_handlers = NULL; | |
c292b105 | 68 | static CRYPTO_RWLOCK *init_lock = NULL; |
b184e3ef | 69 | |
b1f1e7ae | 70 | static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 71 | static int base_inited = 0; |
c2e4e5d2 | 72 | DEFINE_RUN_ONCE_STATIC(ossl_init_base) |
b184e3ef MC |
73 | { |
74 | #ifdef OPENSSL_INIT_DEBUG | |
75 | fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n"); | |
f7edeced RS |
76 | #endif |
77 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG | |
78 | ossl_malloc_setup_failures(); | |
b184e3ef | 79 | #endif |
a072ed0c MC |
80 | /* |
81 | * We use a dummy thread local key here. We use the destructor to detect | |
82 | * when the thread is going to stop (where that feature is available) | |
83 | */ | |
84 | CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap); | |
c7b7938e | 85 | #ifndef OPENSSL_SYS_UEFI |
f672aee4 | 86 | atexit(OPENSSL_cleanup); |
c7b7938e | 87 | #endif |
75551e07 | 88 | if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL) |
c2e4e5d2 | 89 | return 0; |
b184e3ef | 90 | OPENSSL_cpuid_setup(); |
8aa9cf7e RL |
91 | |
92 | /* | |
93 | * BIG FAT WARNING! | |
94 | * Everything needed to be initialized in this function before threads | |
95 | * come along MUST happen before base_inited is set to 1, or we will | |
96 | * see race conditions. | |
97 | */ | |
b184e3ef | 98 | base_inited = 1; |
5836780f | 99 | |
6e290a25 | 100 | #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE) |
2b59d1be MC |
101 | # ifdef DSO_WIN32 |
102 | { | |
103 | HMODULE handle = NULL; | |
104 | BOOL ret; | |
105 | ||
106 | /* We don't use the DSO route for WIN32 because there is a better way */ | |
107 | ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
108 | | GET_MODULE_HANDLE_EX_FLAG_PIN, | |
109 | (void *)&base_inited, &handle); | |
110 | ||
111 | return (ret == TRUE) ? 1 : 0; | |
112 | } | |
113 | # else | |
5836780f MC |
114 | /* |
115 | * Deliberately leak a reference to ourselves. This will force the library | |
689f112d | 116 | * to remain loaded until the atexit() handler is run at process exit. |
5836780f MC |
117 | */ |
118 | { | |
119 | DSO *dso = NULL; | |
120 | ||
689f112d | 121 | ERR_set_mark(); |
5836780f | 122 | dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE); |
4af14b7b MK |
123 | # ifdef OPENSSL_INIT_DEBUG |
124 | fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n", | |
125 | (dso == NULL ? "No!" : "Yes.")); | |
126 | /* | |
127 | * In case of No!, it is uncertain our exit()-handlers can still be | |
128 | * called. After dlclose() the whole library might have been unloaded | |
129 | * already. | |
130 | */ | |
131 | # endif | |
5836780f | 132 | DSO_free(dso); |
689f112d | 133 | ERR_pop_to_mark(); |
5836780f | 134 | } |
2b59d1be | 135 | # endif |
b6d5ba1a | 136 | #endif |
5836780f | 137 | |
c2e4e5d2 | 138 | return 1; |
b184e3ef MC |
139 | } |
140 | ||
b1f1e7ae | 141 | static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 142 | static int load_crypto_strings_inited = 0; |
c2e4e5d2 | 143 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings) |
b184e3ef MC |
144 | { |
145 | /* Do nothing in this case */ | |
c2e4e5d2 | 146 | return 1; |
b184e3ef MC |
147 | } |
148 | ||
c2e4e5d2 | 149 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings) |
b184e3ef | 150 | { |
69588edb | 151 | int ret = 1; |
498abff0 MC |
152 | /* |
153 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time | |
154 | * pulling in all the error strings during static linking | |
155 | */ | |
156 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) | |
b184e3ef MC |
157 | # ifdef OPENSSL_INIT_DEBUG |
158 | fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: " | |
b3599dbb | 159 | "err_load_crypto_strings_int()\n"); |
b184e3ef | 160 | # endif |
69588edb | 161 | ret = err_load_crypto_strings_int(); |
b184e3ef | 162 | load_crypto_strings_inited = 1; |
bd91e3c8 | 163 | #endif |
69588edb | 164 | return ret; |
b184e3ef MC |
165 | } |
166 | ||
b1f1e7ae | 167 | static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 168 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers) |
b184e3ef MC |
169 | { |
170 | /* | |
171 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
172 | * pulling in all the ciphers during static linking | |
173 | */ | |
174 | #ifndef OPENSSL_NO_AUTOALGINIT | |
175 | # ifdef OPENSSL_INIT_DEBUG | |
176 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: " | |
b3599dbb | 177 | "openssl_add_all_ciphers_int()\n"); |
b184e3ef | 178 | # endif |
b3599dbb | 179 | openssl_add_all_ciphers_int(); |
b184e3ef | 180 | #endif |
c2e4e5d2 | 181 | return 1; |
b184e3ef MC |
182 | } |
183 | ||
b1f1e7ae | 184 | static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 185 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests) |
b184e3ef MC |
186 | { |
187 | /* | |
188 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
189 | * pulling in all the ciphers during static linking | |
190 | */ | |
191 | #ifndef OPENSSL_NO_AUTOALGINIT | |
192 | # ifdef OPENSSL_INIT_DEBUG | |
193 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: " | |
b3599dbb | 194 | "openssl_add_all_digests()\n"); |
b184e3ef | 195 | # endif |
b3599dbb | 196 | openssl_add_all_digests_int(); |
b184e3ef | 197 | #endif |
c2e4e5d2 | 198 | return 1; |
b184e3ef MC |
199 | } |
200 | ||
c2e4e5d2 | 201 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs) |
b184e3ef MC |
202 | { |
203 | /* Do nothing */ | |
c2e4e5d2 | 204 | return 1; |
b184e3ef MC |
205 | } |
206 | ||
b1f1e7ae | 207 | static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 208 | static int config_inited = 0; |
cda3ae5b | 209 | static const char *appname; |
c2e4e5d2 | 210 | DEFINE_RUN_ONCE_STATIC(ossl_init_config) |
b184e3ef MC |
211 | { |
212 | #ifdef OPENSSL_INIT_DEBUG | |
213 | fprintf(stderr, | |
b3599dbb | 214 | "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n", |
cda3ae5b | 215 | appname == NULL ? "NULL" : appname); |
b184e3ef | 216 | #endif |
cda3ae5b | 217 | openssl_config_int(appname); |
b184e3ef | 218 | config_inited = 1; |
c2e4e5d2 | 219 | return 1; |
b184e3ef | 220 | } |
c2e4e5d2 | 221 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_config) |
b184e3ef MC |
222 | { |
223 | #ifdef OPENSSL_INIT_DEBUG | |
224 | fprintf(stderr, | |
b3599dbb | 225 | "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n"); |
b184e3ef | 226 | #endif |
b3599dbb | 227 | openssl_no_config_int(); |
b184e3ef | 228 | config_inited = 1; |
c2e4e5d2 | 229 | return 1; |
b184e3ef MC |
230 | } |
231 | ||
b1f1e7ae | 232 | static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 233 | static int async_inited = 0; |
c2e4e5d2 | 234 | DEFINE_RUN_ONCE_STATIC(ossl_init_async) |
b184e3ef MC |
235 | { |
236 | #ifdef OPENSSL_INIT_DEBUG | |
237 | fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n"); | |
238 | #endif | |
c2e4e5d2 RL |
239 | if (!async_init()) |
240 | return 0; | |
b184e3ef | 241 | async_inited = 1; |
c2e4e5d2 | 242 | return 1; |
b184e3ef MC |
243 | } |
244 | ||
245 | #ifndef OPENSSL_NO_ENGINE | |
b1f1e7ae | 246 | static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 247 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl) |
b184e3ef MC |
248 | { |
249 | # ifdef OPENSSL_INIT_DEBUG | |
250 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: " | |
b3599dbb | 251 | "engine_load_openssl_int()\n"); |
b184e3ef | 252 | # endif |
b3599dbb | 253 | engine_load_openssl_int(); |
c2e4e5d2 | 254 | return 1; |
b184e3ef | 255 | } |
619eb33a RL |
256 | # ifndef OPENSSL_NO_DEVCRYPTOENG |
257 | static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT; | |
258 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto) | |
259 | { | |
260 | # ifdef OPENSSL_INIT_DEBUG | |
261 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: " | |
262 | "engine_load_devcrypto_int()\n"); | |
263 | # endif | |
264 | engine_load_devcrypto_int(); | |
265 | return 1; | |
266 | } | |
267 | # endif | |
b184e3ef MC |
268 | |
269 | # ifndef OPENSSL_NO_RDRAND | |
b1f1e7ae | 270 | static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 271 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand) |
b184e3ef MC |
272 | { |
273 | # ifdef OPENSSL_INIT_DEBUG | |
274 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: " | |
b3599dbb | 275 | "engine_load_rdrand_int()\n"); |
b184e3ef | 276 | # endif |
b3599dbb | 277 | engine_load_rdrand_int(); |
c2e4e5d2 | 278 | return 1; |
b184e3ef MC |
279 | } |
280 | # endif | |
b1f1e7ae | 281 | static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 282 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic) |
b184e3ef MC |
283 | { |
284 | # ifdef OPENSSL_INIT_DEBUG | |
285 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: " | |
b3599dbb | 286 | "engine_load_dynamic_int()\n"); |
b184e3ef | 287 | # endif |
b3599dbb | 288 | engine_load_dynamic_int(); |
c2e4e5d2 | 289 | return 1; |
b184e3ef MC |
290 | } |
291 | # ifndef OPENSSL_NO_STATIC_ENGINE | |
292 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) | |
b1f1e7ae | 293 | static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 294 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock) |
b184e3ef MC |
295 | { |
296 | # ifdef OPENSSL_INIT_DEBUG | |
297 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: " | |
b3599dbb | 298 | "engine_load_padlock_int()\n"); |
b184e3ef | 299 | # endif |
b3599dbb | 300 | engine_load_padlock_int(); |
c2e4e5d2 | 301 | return 1; |
b184e3ef MC |
302 | } |
303 | # endif | |
304 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 305 | static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 306 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi) |
b184e3ef MC |
307 | { |
308 | # ifdef OPENSSL_INIT_DEBUG | |
309 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: " | |
b3599dbb | 310 | "engine_load_capi_int()\n"); |
b184e3ef | 311 | # endif |
b3599dbb | 312 | engine_load_capi_int(); |
c2e4e5d2 | 313 | return 1; |
b184e3ef MC |
314 | } |
315 | # endif | |
6cba4a66 | 316 | # if !defined(OPENSSL_NO_AFALGENG) |
a4d8bcf1 | 317 | static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 318 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg) |
6cba4a66 | 319 | { |
320 | # ifdef OPENSSL_INIT_DEBUG | |
321 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: " | |
b3599dbb | 322 | "engine_load_afalg_int()\n"); |
6cba4a66 | 323 | # endif |
b3599dbb | 324 | engine_load_afalg_int(); |
c2e4e5d2 | 325 | return 1; |
6cba4a66 | 326 | } |
327 | # endif | |
b184e3ef MC |
328 | # endif |
329 | #endif | |
330 | ||
e4ad0763 | 331 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 332 | static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT; |
e4ad0763 | 333 | |
b184e3ef | 334 | static int zlib_inited = 0; |
c2e4e5d2 | 335 | DEFINE_RUN_ONCE_STATIC(ossl_init_zlib) |
b184e3ef MC |
336 | { |
337 | /* Do nothing - we need to know about this for the later cleanup */ | |
338 | zlib_inited = 1; | |
c2e4e5d2 | 339 | return 1; |
b184e3ef | 340 | } |
e4ad0763 | 341 | #endif |
b184e3ef | 342 | |
71567a6f | 343 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals) |
b184e3ef MC |
344 | { |
345 | /* Can't do much about this */ | |
346 | if (locals == NULL) | |
347 | return; | |
348 | ||
349 | if (locals->async) { | |
350 | #ifdef OPENSSL_INIT_DEBUG | |
351 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
352 | "ASYNC_cleanup_thread()\n"); | |
353 | #endif | |
354 | ASYNC_cleanup_thread(); | |
355 | } | |
356 | ||
357 | if (locals->err_state) { | |
358 | #ifdef OPENSSL_INIT_DEBUG | |
359 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
21e00174 | 360 | "err_delete_thread_state()\n"); |
b184e3ef | 361 | #endif |
21e00174 | 362 | err_delete_thread_state(); |
b184e3ef MC |
363 | } |
364 | ||
7caf122e KR |
365 | if (locals->rand) { |
366 | #ifdef OPENSSL_INIT_DEBUG | |
367 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
368 | "drbg_delete_thread_state()\n"); | |
369 | #endif | |
370 | drbg_delete_thread_state(); | |
371 | } | |
372 | ||
b184e3ef | 373 | OPENSSL_free(locals); |
b184e3ef MC |
374 | } |
375 | ||
f672aee4 | 376 | void OPENSSL_thread_stop(void) |
71567a6f MC |
377 | { |
378 | ossl_init_thread_stop( | |
379 | (struct thread_local_inits_st *)ossl_init_get_thread_local(0)); | |
380 | } | |
381 | ||
b184e3ef MC |
382 | int ossl_init_thread_start(uint64_t opts) |
383 | { | |
3ac6d5ee BE |
384 | struct thread_local_inits_st *locals; |
385 | ||
386 | if (!OPENSSL_init_crypto(0, NULL)) | |
387 | return 0; | |
388 | ||
389 | locals = ossl_init_get_thread_local(1); | |
b184e3ef MC |
390 | |
391 | if (locals == NULL) | |
392 | return 0; | |
393 | ||
394 | if (opts & OPENSSL_INIT_THREAD_ASYNC) { | |
395 | #ifdef OPENSSL_INIT_DEBUG | |
396 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
397 | "marking thread for async\n"); | |
398 | #endif | |
399 | locals->async = 1; | |
400 | } | |
401 | ||
402 | if (opts & OPENSSL_INIT_THREAD_ERR_STATE) { | |
403 | #ifdef OPENSSL_INIT_DEBUG | |
404 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
405 | "marking thread for err_state\n"); | |
406 | #endif | |
407 | locals->err_state = 1; | |
408 | } | |
409 | ||
7caf122e KR |
410 | if (opts & OPENSSL_INIT_THREAD_RAND) { |
411 | #ifdef OPENSSL_INIT_DEBUG | |
412 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
413 | "marking thread for rand\n"); | |
414 | #endif | |
415 | locals->rand = 1; | |
416 | } | |
417 | ||
b184e3ef MC |
418 | return 1; |
419 | } | |
420 | ||
f672aee4 | 421 | void OPENSSL_cleanup(void) |
b184e3ef MC |
422 | { |
423 | OPENSSL_INIT_STOP *currhandler, *lasthandler; | |
424 | ||
deca5df2 MC |
425 | /* If we've not been inited then no need to deinit */ |
426 | if (!base_inited) | |
427 | return; | |
428 | ||
dd27f16e RS |
429 | /* Might be explicitly called and also by atexit */ |
430 | if (stopped) | |
431 | return; | |
432 | stopped = 1; | |
433 | ||
b184e3ef MC |
434 | /* |
435 | * Thread stop may not get automatically called by the thread library for | |
436 | * the very last thread in some situations, so call it directly. | |
437 | */ | |
438 | ossl_init_thread_stop(ossl_init_get_thread_local(0)); | |
439 | ||
440 | currhandler = stop_handlers; | |
441 | while (currhandler != NULL) { | |
442 | currhandler->handler(); | |
443 | lasthandler = currhandler; | |
444 | currhandler = currhandler->next; | |
445 | OPENSSL_free(lasthandler); | |
446 | } | |
447 | stop_handlers = NULL; | |
c292b105 MC |
448 | |
449 | CRYPTO_THREAD_lock_free(init_lock); | |
adeb4bc7 | 450 | init_lock = NULL; |
c292b105 | 451 | |
b184e3ef MC |
452 | /* |
453 | * We assume we are single-threaded for this function, i.e. no race | |
454 | * conditions for the various "*_inited" vars below. | |
455 | */ | |
456 | ||
e4ad0763 | 457 | #ifndef OPENSSL_NO_COMP |
b184e3ef MC |
458 | if (zlib_inited) { |
459 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 460 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 461 | "comp_zlib_cleanup_int()\n"); |
b184e3ef | 462 | #endif |
b3599dbb | 463 | comp_zlib_cleanup_int(); |
b184e3ef | 464 | } |
e4ad0763 | 465 | #endif |
b184e3ef | 466 | |
ed49f43a MC |
467 | if (async_inited) { |
468 | # ifdef OPENSSL_INIT_DEBUG | |
469 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " | |
470 | "async_deinit()\n"); | |
471 | # endif | |
472 | async_deinit(); | |
473 | } | |
ed49f43a | 474 | |
b184e3ef MC |
475 | if (load_crypto_strings_inited) { |
476 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 477 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 478 | "err_free_strings_int()\n"); |
b184e3ef | 479 | #endif |
b3599dbb | 480 | err_free_strings_int(); |
b184e3ef MC |
481 | } |
482 | ||
a072ed0c | 483 | CRYPTO_THREAD_cleanup_local(&threadstopkey); |
6bc7bad0 | 484 | |
b184e3ef | 485 | #ifdef OPENSSL_INIT_DEBUG |
58a8fc25 | 486 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 487 | "rand_cleanup_int()\n"); |
58a8fc25 | 488 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 489 | "conf_modules_free_int()\n"); |
9749a07a | 490 | #ifndef OPENSSL_NO_ENGINE |
ae6412f3 | 491 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 492 | "engine_cleanup_int()\n"); |
9749a07a | 493 | #endif |
58a8fc25 | 494 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 495 | "crypto_cleanup_all_ex_data_int()\n"); |
58a8fc25 | 496 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 497 | "bio_sock_cleanup_int()\n"); |
ff234405 MC |
498 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
499 | "bio_cleanup()\n"); | |
58a8fc25 | 500 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 501 | "evp_cleanup_int()\n"); |
58a8fc25 | 502 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 503 | "obj_cleanup_int()\n"); |
ff234405 MC |
504 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
505 | "err_cleanup()\n"); | |
9749a07a | 506 | #endif |
58a8fc25 MC |
507 | /* |
508 | * Note that cleanup order is important: | |
a535fe12 | 509 | * - rand_cleanup_int could call an ENGINE's RAND cleanup function so |
b3599dbb | 510 | * must be called before engine_cleanup_int() |
58a8fc25 MC |
511 | * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up |
512 | * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data(). | |
b3599dbb MC |
513 | * - conf_modules_free_int() can end up in ENGINE code so must be called |
514 | * before engine_cleanup_int() | |
a535fe12 DSH |
515 | * - ENGINEs and additional EVP algorithms might use added OIDs names so |
516 | * obj_cleanup_int() must be called last | |
58a8fc25 | 517 | */ |
b3599dbb | 518 | rand_cleanup_int(); |
c16de9d8 | 519 | rand_drbg_cleanup_int(); |
b3599dbb | 520 | conf_modules_free_int(); |
773fd0ba | 521 | #ifndef OPENSSL_NO_ENGINE |
b3599dbb | 522 | engine_cleanup_int(); |
773fd0ba | 523 | #endif |
71a5516d | 524 | ossl_store_cleanup_int(); |
b3599dbb | 525 | crypto_cleanup_all_ex_data_int(); |
ff234405 | 526 | bio_cleanup(); |
b3599dbb MC |
527 | evp_cleanup_int(); |
528 | obj_cleanup_int(); | |
ff234405 MC |
529 | err_cleanup(); |
530 | ||
d7c402c4 DMSP |
531 | CRYPTO_secure_malloc_done(); |
532 | ||
deca5df2 | 533 | base_inited = 0; |
b184e3ef MC |
534 | } |
535 | ||
b184e3ef MC |
536 | /* |
537 | * If this function is called with a non NULL settings value then it must be | |
538 | * called prior to any threads making calls to any OpenSSL functions, | |
539 | * i.e. passing a non-null settings value is assumed to be single-threaded. | |
540 | */ | |
0fc32b07 | 541 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) |
b184e3ef | 542 | { |
302f7588 | 543 | if (stopped) { |
6b49b308 | 544 | CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL); |
0fc32b07 | 545 | return 0; |
302f7588 | 546 | } |
dd27f16e | 547 | |
b7a7f39a | 548 | if (!base_inited && !RUN_ONCE(&base, ossl_init_base)) |
b1f1e7ae | 549 | return 0; |
b184e3ef | 550 | |
b1f1e7ae | 551 | if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS) |
c2e4e5d2 RL |
552 | && !RUN_ONCE(&load_crypto_strings, |
553 | ossl_init_no_load_crypto_strings)) | |
b1f1e7ae | 554 | return 0; |
b184e3ef | 555 | |
b1f1e7ae | 556 | if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS) |
c2e4e5d2 | 557 | && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings)) |
b1f1e7ae | 558 | return 0; |
b184e3ef | 559 | |
b1f1e7ae | 560 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS) |
c2e4e5d2 | 561 | && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs)) |
b1f1e7ae | 562 | return 0; |
b184e3ef | 563 | |
b1f1e7ae | 564 | if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS) |
c2e4e5d2 | 565 | && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers)) |
b1f1e7ae | 566 | return 0; |
b184e3ef | 567 | |
b1f1e7ae | 568 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS) |
c2e4e5d2 | 569 | && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs)) |
b1f1e7ae | 570 | return 0; |
b184e3ef | 571 | |
b1f1e7ae | 572 | if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS) |
c2e4e5d2 | 573 | && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests)) |
b1f1e7ae | 574 | return 0; |
b184e3ef | 575 | |
b5319bdb | 576 | if ((opts & OPENSSL_INIT_ATFORK) |
2915fe19 RS |
577 | && !openssl_init_fork_handlers()) |
578 | return 0; | |
579 | ||
b1f1e7ae | 580 | if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) |
c2e4e5d2 | 581 | && !RUN_ONCE(&config, ossl_init_no_config)) |
b1f1e7ae | 582 | return 0; |
b184e3ef MC |
583 | |
584 | if (opts & OPENSSL_INIT_LOAD_CONFIG) { | |
b1f1e7ae | 585 | int ret; |
c292b105 | 586 | CRYPTO_THREAD_write_lock(init_lock); |
cda3ae5b | 587 | appname = (settings == NULL) ? NULL : settings->appname; |
c2e4e5d2 | 588 | ret = RUN_ONCE(&config, ossl_init_config); |
c292b105 | 589 | CRYPTO_THREAD_unlock(init_lock); |
b1f1e7ae MC |
590 | if (!ret) |
591 | return 0; | |
b184e3ef MC |
592 | } |
593 | ||
b1f1e7ae | 594 | if ((opts & OPENSSL_INIT_ASYNC) |
c2e4e5d2 | 595 | && !RUN_ONCE(&async, ossl_init_async)) |
b1f1e7ae | 596 | return 0; |
7626fbf2 | 597 | |
b184e3ef | 598 | #ifndef OPENSSL_NO_ENGINE |
b1f1e7ae | 599 | if ((opts & OPENSSL_INIT_ENGINE_OPENSSL) |
c2e4e5d2 | 600 | && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl)) |
b1f1e7ae | 601 | return 0; |
619eb33a RL |
602 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG) |
603 | if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV) | |
604 | && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto)) | |
605 | return 0; | |
606 | # endif | |
b184e3ef | 607 | # ifndef OPENSSL_NO_RDRAND |
b1f1e7ae | 608 | if ((opts & OPENSSL_INIT_ENGINE_RDRAND) |
c2e4e5d2 | 609 | && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand)) |
b1f1e7ae | 610 | return 0; |
b184e3ef | 611 | # endif |
b1f1e7ae | 612 | if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC) |
c2e4e5d2 | 613 | && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic)) |
b1f1e7ae | 614 | return 0; |
b184e3ef MC |
615 | # ifndef OPENSSL_NO_STATIC_ENGINE |
616 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) | |
b1f1e7ae | 617 | if ((opts & OPENSSL_INIT_ENGINE_PADLOCK) |
c2e4e5d2 | 618 | && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock)) |
b1f1e7ae | 619 | return 0; |
b184e3ef MC |
620 | # endif |
621 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 622 | if ((opts & OPENSSL_INIT_ENGINE_CAPI) |
c2e4e5d2 | 623 | && !RUN_ONCE(&engine_capi, ossl_init_engine_capi)) |
b1f1e7ae | 624 | return 0; |
b184e3ef | 625 | # endif |
6cba4a66 | 626 | # if !defined(OPENSSL_NO_AFALGENG) |
b1f1e7ae | 627 | if ((opts & OPENSSL_INIT_ENGINE_AFALG) |
c2e4e5d2 | 628 | && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg)) |
b1f1e7ae | 629 | return 0; |
6cba4a66 | 630 | # endif |
b184e3ef MC |
631 | # endif |
632 | if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN | |
8d00e30f | 633 | | OPENSSL_INIT_ENGINE_OPENSSL |
6cba4a66 | 634 | | OPENSSL_INIT_ENGINE_AFALG)) { |
b184e3ef MC |
635 | ENGINE_register_all_complete(); |
636 | } | |
637 | #endif | |
638 | ||
e4ad0763 | 639 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 640 | if ((opts & OPENSSL_INIT_ZLIB) |
c2e4e5d2 | 641 | && !RUN_ONCE(&zlib, ossl_init_zlib)) |
b1f1e7ae | 642 | return 0; |
e4ad0763 | 643 | #endif |
0fc32b07 MC |
644 | |
645 | return 1; | |
b184e3ef MC |
646 | } |
647 | ||
f672aee4 | 648 | int OPENSSL_atexit(void (*handler)(void)) |
b184e3ef MC |
649 | { |
650 | OPENSSL_INIT_STOP *newhand; | |
651 | ||
6e290a25 | 652 | #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE) |
5836780f | 653 | { |
5836780f MC |
654 | union { |
655 | void *sym; | |
656 | void (*func)(void); | |
657 | } handlersym; | |
658 | ||
659 | handlersym.func = handler; | |
2b59d1be MC |
660 | # ifdef DSO_WIN32 |
661 | { | |
662 | HMODULE handle = NULL; | |
663 | BOOL ret; | |
5836780f | 664 | |
2b59d1be MC |
665 | /* |
666 | * We don't use the DSO route for WIN32 because there is a better | |
667 | * way | |
668 | */ | |
669 | ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
670 | | GET_MODULE_HANDLE_EX_FLAG_PIN, | |
671 | handlersym.sym, &handle); | |
672 | ||
673 | if (!ret) | |
674 | return 0; | |
675 | } | |
676 | # else | |
677 | /* | |
678 | * Deliberately leak a reference to the handler. This will force the | |
679 | * library/code containing the handler to remain loaded until we run the | |
680 | * atexit handler. If -znodelete has been used then this is | |
c9a41d7d | 681 | * unnecessary. |
2b59d1be MC |
682 | */ |
683 | { | |
684 | DSO *dso = NULL; | |
685 | ||
689f112d | 686 | ERR_set_mark(); |
2b59d1be | 687 | dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE); |
4af14b7b MK |
688 | # ifdef OPENSSL_INIT_DEBUG |
689 | fprintf(stderr, | |
690 | "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n", | |
691 | (dso == NULL ? "No!" : "Yes.")); | |
692 | /* See same code above in ossl_init_base() for an explanation. */ | |
693 | # endif | |
2b59d1be | 694 | DSO_free(dso); |
689f112d | 695 | ERR_pop_to_mark(); |
2b59d1be MC |
696 | } |
697 | # endif | |
5836780f | 698 | } |
b6d5ba1a | 699 | #endif |
5836780f | 700 | |
cdb10bae RS |
701 | if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) { |
702 | CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE); | |
b184e3ef | 703 | return 0; |
cdb10bae | 704 | } |
b184e3ef MC |
705 | |
706 | newhand->handler = handler; | |
707 | newhand->next = stop_handlers; | |
708 | stop_handlers = newhand; | |
709 | ||
710 | return 1; | |
711 | } | |
2915fe19 | 712 | |
63ab5ea1 | 713 | #ifdef OPENSSL_SYS_UNIX |
2915fe19 RS |
714 | /* |
715 | * The following three functions are for OpenSSL developers. This is | |
716 | * where we set/reset state across fork (called via pthread_atfork when | |
717 | * it exists, or manually by the application when it doesn't). | |
718 | * | |
719 | * WARNING! If you put code in either OPENSSL_fork_parent or | |
720 | * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal- | |
721 | * safe. See this link, for example: | |
722 | * http://man7.org/linux/man-pages/man7/signal-safety.7.html | |
723 | */ | |
724 | ||
725 | void OPENSSL_fork_prepare(void) | |
726 | { | |
727 | } | |
728 | ||
729 | void OPENSSL_fork_parent(void) | |
730 | { | |
731 | } | |
732 | ||
733 | void OPENSSL_fork_child(void) | |
734 | { | |
a35f607c | 735 | rand_fork(); |
2915fe19 RS |
736 | } |
737 | #endif |