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