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