]>
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 RS |
26 | #include "internal/thread_once.h" |
27 | #include "internal/dso.h" | |
28 | #include "internal/store.h" | |
dd27f16e RS |
29 | |
30 | static int stopped = 0; | |
b184e3ef | 31 | |
71567a6f MC |
32 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals); |
33 | ||
a072ed0c | 34 | static CRYPTO_THREAD_LOCAL threadstopkey; |
b184e3ef | 35 | |
b184e3ef MC |
36 | static void ossl_init_thread_stop_wrap(void *local) |
37 | { | |
38 | ossl_init_thread_stop((struct thread_local_inits_st *)local); | |
39 | } | |
40 | ||
b7326ea7 | 41 | static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc) |
b184e3ef | 42 | { |
a072ed0c MC |
43 | struct thread_local_inits_st *local = |
44 | CRYPTO_THREAD_get_local(&threadstopkey); | |
b184e3ef MC |
45 | |
46 | if (local == NULL && alloc) { | |
cbe29648 | 47 | local = OPENSSL_zalloc(sizeof(*local)); |
3ac6d5ee BE |
48 | if (local != NULL && !CRYPTO_THREAD_set_local(&threadstopkey, local)) { |
49 | OPENSSL_free(local); | |
50 | return NULL; | |
51 | } | |
b184e3ef | 52 | } |
b7326ea7 | 53 | if (!alloc) { |
a072ed0c | 54 | CRYPTO_THREAD_set_local(&threadstopkey, NULL); |
b7326ea7 | 55 | } |
b184e3ef MC |
56 | |
57 | return local; | |
58 | } | |
59 | ||
7253fd55 | 60 | typedef struct ossl_init_stop_st OPENSSL_INIT_STOP; |
b184e3ef MC |
61 | struct ossl_init_stop_st { |
62 | void (*handler)(void); | |
63 | OPENSSL_INIT_STOP *next; | |
64 | }; | |
65 | ||
66 | static OPENSSL_INIT_STOP *stop_handlers = NULL; | |
c292b105 | 67 | static CRYPTO_RWLOCK *init_lock = NULL; |
b184e3ef | 68 | |
b1f1e7ae | 69 | static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 70 | static int base_inited = 0; |
c2e4e5d2 | 71 | DEFINE_RUN_ONCE_STATIC(ossl_init_base) |
b184e3ef MC |
72 | { |
73 | #ifdef OPENSSL_INIT_DEBUG | |
74 | fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n"); | |
f7edeced RS |
75 | #endif |
76 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG | |
77 | ossl_malloc_setup_failures(); | |
b184e3ef | 78 | #endif |
a072ed0c MC |
79 | /* |
80 | * We use a dummy thread local key here. We use the destructor to detect | |
81 | * when the thread is going to stop (where that feature is available) | |
82 | */ | |
83 | CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap); | |
c7b7938e | 84 | #ifndef OPENSSL_SYS_UEFI |
f672aee4 | 85 | atexit(OPENSSL_cleanup); |
c7b7938e | 86 | #endif |
75551e07 | 87 | if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL) |
c2e4e5d2 | 88 | return 0; |
b184e3ef | 89 | OPENSSL_cpuid_setup(); |
8aa9cf7e RL |
90 | |
91 | /* | |
92 | * BIG FAT WARNING! | |
93 | * Everything needed to be initialized in this function before threads | |
94 | * come along MUST happen before base_inited is set to 1, or we will | |
95 | * see race conditions. | |
96 | */ | |
b184e3ef | 97 | base_inited = 1; |
5836780f | 98 | |
6e290a25 | 99 | #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE) |
2b59d1be MC |
100 | # ifdef DSO_WIN32 |
101 | { | |
102 | HMODULE handle = NULL; | |
103 | BOOL ret; | |
104 | ||
105 | /* We don't use the DSO route for WIN32 because there is a better way */ | |
106 | ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
107 | | GET_MODULE_HANDLE_EX_FLAG_PIN, | |
108 | (void *)&base_inited, &handle); | |
109 | ||
110 | return (ret == TRUE) ? 1 : 0; | |
111 | } | |
112 | # else | |
5836780f MC |
113 | /* |
114 | * Deliberately leak a reference to ourselves. This will force the library | |
689f112d | 115 | * to remain loaded until the atexit() handler is run at process exit. |
5836780f MC |
116 | */ |
117 | { | |
118 | DSO *dso = NULL; | |
119 | ||
689f112d | 120 | ERR_set_mark(); |
5836780f MC |
121 | dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE); |
122 | DSO_free(dso); | |
689f112d | 123 | ERR_pop_to_mark(); |
5836780f | 124 | } |
2b59d1be | 125 | # endif |
b6d5ba1a | 126 | #endif |
5836780f | 127 | |
c2e4e5d2 | 128 | return 1; |
b184e3ef MC |
129 | } |
130 | ||
b1f1e7ae | 131 | static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 132 | static int load_crypto_strings_inited = 0; |
c2e4e5d2 | 133 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings) |
b184e3ef MC |
134 | { |
135 | /* Do nothing in this case */ | |
c2e4e5d2 | 136 | return 1; |
b184e3ef MC |
137 | } |
138 | ||
c2e4e5d2 | 139 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings) |
b184e3ef | 140 | { |
69588edb | 141 | int ret = 1; |
498abff0 MC |
142 | /* |
143 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time | |
144 | * pulling in all the error strings during static linking | |
145 | */ | |
146 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) | |
b184e3ef MC |
147 | # ifdef OPENSSL_INIT_DEBUG |
148 | fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: " | |
b3599dbb | 149 | "err_load_crypto_strings_int()\n"); |
b184e3ef | 150 | # endif |
69588edb | 151 | ret = err_load_crypto_strings_int(); |
b184e3ef | 152 | load_crypto_strings_inited = 1; |
bd91e3c8 | 153 | #endif |
69588edb | 154 | return ret; |
b184e3ef MC |
155 | } |
156 | ||
b1f1e7ae | 157 | static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 158 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers) |
b184e3ef MC |
159 | { |
160 | /* | |
161 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
162 | * pulling in all the ciphers during static linking | |
163 | */ | |
164 | #ifndef OPENSSL_NO_AUTOALGINIT | |
165 | # ifdef OPENSSL_INIT_DEBUG | |
166 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: " | |
b3599dbb | 167 | "openssl_add_all_ciphers_int()\n"); |
b184e3ef | 168 | # endif |
b3599dbb | 169 | openssl_add_all_ciphers_int(); |
b184e3ef | 170 | #endif |
c2e4e5d2 | 171 | return 1; |
b184e3ef MC |
172 | } |
173 | ||
b1f1e7ae | 174 | static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 175 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests) |
b184e3ef MC |
176 | { |
177 | /* | |
178 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
179 | * pulling in all the ciphers during static linking | |
180 | */ | |
181 | #ifndef OPENSSL_NO_AUTOALGINIT | |
182 | # ifdef OPENSSL_INIT_DEBUG | |
183 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: " | |
b3599dbb | 184 | "openssl_add_all_digests()\n"); |
b184e3ef | 185 | # endif |
b3599dbb | 186 | openssl_add_all_digests_int(); |
b184e3ef | 187 | #endif |
c2e4e5d2 | 188 | return 1; |
b184e3ef MC |
189 | } |
190 | ||
c2e4e5d2 | 191 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs) |
b184e3ef MC |
192 | { |
193 | /* Do nothing */ | |
c2e4e5d2 | 194 | return 1; |
b184e3ef MC |
195 | } |
196 | ||
b1f1e7ae | 197 | static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 198 | static int config_inited = 0; |
cda3ae5b | 199 | static const char *appname; |
c2e4e5d2 | 200 | DEFINE_RUN_ONCE_STATIC(ossl_init_config) |
b184e3ef MC |
201 | { |
202 | #ifdef OPENSSL_INIT_DEBUG | |
203 | fprintf(stderr, | |
b3599dbb | 204 | "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n", |
cda3ae5b | 205 | appname == NULL ? "NULL" : appname); |
b184e3ef | 206 | #endif |
cda3ae5b | 207 | openssl_config_int(appname); |
b184e3ef | 208 | config_inited = 1; |
c2e4e5d2 | 209 | return 1; |
b184e3ef | 210 | } |
c2e4e5d2 | 211 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_config) |
b184e3ef MC |
212 | { |
213 | #ifdef OPENSSL_INIT_DEBUG | |
214 | fprintf(stderr, | |
b3599dbb | 215 | "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n"); |
b184e3ef | 216 | #endif |
b3599dbb | 217 | openssl_no_config_int(); |
b184e3ef | 218 | config_inited = 1; |
c2e4e5d2 | 219 | return 1; |
b184e3ef MC |
220 | } |
221 | ||
b1f1e7ae | 222 | static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 223 | static int async_inited = 0; |
c2e4e5d2 | 224 | DEFINE_RUN_ONCE_STATIC(ossl_init_async) |
b184e3ef MC |
225 | { |
226 | #ifdef OPENSSL_INIT_DEBUG | |
227 | fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n"); | |
228 | #endif | |
c2e4e5d2 RL |
229 | if (!async_init()) |
230 | return 0; | |
b184e3ef | 231 | async_inited = 1; |
c2e4e5d2 | 232 | return 1; |
b184e3ef MC |
233 | } |
234 | ||
235 | #ifndef OPENSSL_NO_ENGINE | |
b1f1e7ae | 236 | static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 237 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl) |
b184e3ef MC |
238 | { |
239 | # ifdef OPENSSL_INIT_DEBUG | |
240 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: " | |
b3599dbb | 241 | "engine_load_openssl_int()\n"); |
b184e3ef | 242 | # endif |
b3599dbb | 243 | engine_load_openssl_int(); |
c2e4e5d2 | 244 | return 1; |
b184e3ef | 245 | } |
619eb33a RL |
246 | # ifndef OPENSSL_NO_DEVCRYPTOENG |
247 | static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT; | |
248 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto) | |
249 | { | |
250 | # ifdef OPENSSL_INIT_DEBUG | |
251 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: " | |
252 | "engine_load_devcrypto_int()\n"); | |
253 | # endif | |
254 | engine_load_devcrypto_int(); | |
255 | return 1; | |
256 | } | |
257 | # endif | |
b184e3ef MC |
258 | |
259 | # ifndef OPENSSL_NO_RDRAND | |
b1f1e7ae | 260 | static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 261 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand) |
b184e3ef MC |
262 | { |
263 | # ifdef OPENSSL_INIT_DEBUG | |
264 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: " | |
b3599dbb | 265 | "engine_load_rdrand_int()\n"); |
b184e3ef | 266 | # endif |
b3599dbb | 267 | engine_load_rdrand_int(); |
c2e4e5d2 | 268 | return 1; |
b184e3ef MC |
269 | } |
270 | # endif | |
b1f1e7ae | 271 | static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 272 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic) |
b184e3ef MC |
273 | { |
274 | # ifdef OPENSSL_INIT_DEBUG | |
275 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: " | |
b3599dbb | 276 | "engine_load_dynamic_int()\n"); |
b184e3ef | 277 | # endif |
b3599dbb | 278 | engine_load_dynamic_int(); |
c2e4e5d2 | 279 | return 1; |
b184e3ef MC |
280 | } |
281 | # ifndef OPENSSL_NO_STATIC_ENGINE | |
282 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) | |
b1f1e7ae | 283 | static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 284 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock) |
b184e3ef MC |
285 | { |
286 | # ifdef OPENSSL_INIT_DEBUG | |
287 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: " | |
b3599dbb | 288 | "engine_load_padlock_int()\n"); |
b184e3ef | 289 | # endif |
b3599dbb | 290 | engine_load_padlock_int(); |
c2e4e5d2 | 291 | return 1; |
b184e3ef MC |
292 | } |
293 | # endif | |
294 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 295 | static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 296 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi) |
b184e3ef MC |
297 | { |
298 | # ifdef OPENSSL_INIT_DEBUG | |
299 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: " | |
b3599dbb | 300 | "engine_load_capi_int()\n"); |
b184e3ef | 301 | # endif |
b3599dbb | 302 | engine_load_capi_int(); |
c2e4e5d2 | 303 | return 1; |
b184e3ef MC |
304 | } |
305 | # endif | |
6cba4a66 | 306 | # if !defined(OPENSSL_NO_AFALGENG) |
a4d8bcf1 | 307 | static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 308 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg) |
6cba4a66 | 309 | { |
310 | # ifdef OPENSSL_INIT_DEBUG | |
311 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: " | |
b3599dbb | 312 | "engine_load_afalg_int()\n"); |
6cba4a66 | 313 | # endif |
b3599dbb | 314 | engine_load_afalg_int(); |
c2e4e5d2 | 315 | return 1; |
6cba4a66 | 316 | } |
317 | # endif | |
b184e3ef MC |
318 | # endif |
319 | #endif | |
320 | ||
e4ad0763 | 321 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 322 | static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT; |
e4ad0763 | 323 | |
b184e3ef | 324 | static int zlib_inited = 0; |
c2e4e5d2 | 325 | DEFINE_RUN_ONCE_STATIC(ossl_init_zlib) |
b184e3ef MC |
326 | { |
327 | /* Do nothing - we need to know about this for the later cleanup */ | |
328 | zlib_inited = 1; | |
c2e4e5d2 | 329 | return 1; |
b184e3ef | 330 | } |
e4ad0763 | 331 | #endif |
b184e3ef | 332 | |
71567a6f | 333 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals) |
b184e3ef MC |
334 | { |
335 | /* Can't do much about this */ | |
336 | if (locals == NULL) | |
337 | return; | |
338 | ||
339 | if (locals->async) { | |
340 | #ifdef OPENSSL_INIT_DEBUG | |
341 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
342 | "ASYNC_cleanup_thread()\n"); | |
343 | #endif | |
344 | ASYNC_cleanup_thread(); | |
345 | } | |
346 | ||
347 | if (locals->err_state) { | |
348 | #ifdef OPENSSL_INIT_DEBUG | |
349 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
21e00174 | 350 | "err_delete_thread_state()\n"); |
b184e3ef | 351 | #endif |
21e00174 | 352 | err_delete_thread_state(); |
b184e3ef MC |
353 | } |
354 | ||
7caf122e KR |
355 | if (locals->rand) { |
356 | #ifdef OPENSSL_INIT_DEBUG | |
357 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
358 | "drbg_delete_thread_state()\n"); | |
359 | #endif | |
360 | drbg_delete_thread_state(); | |
361 | } | |
362 | ||
b184e3ef | 363 | OPENSSL_free(locals); |
b184e3ef MC |
364 | } |
365 | ||
f672aee4 | 366 | void OPENSSL_thread_stop(void) |
71567a6f MC |
367 | { |
368 | ossl_init_thread_stop( | |
369 | (struct thread_local_inits_st *)ossl_init_get_thread_local(0)); | |
370 | } | |
371 | ||
b184e3ef MC |
372 | int ossl_init_thread_start(uint64_t opts) |
373 | { | |
3ac6d5ee BE |
374 | struct thread_local_inits_st *locals; |
375 | ||
376 | if (!OPENSSL_init_crypto(0, NULL)) | |
377 | return 0; | |
378 | ||
379 | locals = ossl_init_get_thread_local(1); | |
b184e3ef MC |
380 | |
381 | if (locals == NULL) | |
382 | return 0; | |
383 | ||
384 | if (opts & OPENSSL_INIT_THREAD_ASYNC) { | |
385 | #ifdef OPENSSL_INIT_DEBUG | |
386 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
387 | "marking thread for async\n"); | |
388 | #endif | |
389 | locals->async = 1; | |
390 | } | |
391 | ||
392 | if (opts & OPENSSL_INIT_THREAD_ERR_STATE) { | |
393 | #ifdef OPENSSL_INIT_DEBUG | |
394 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
395 | "marking thread for err_state\n"); | |
396 | #endif | |
397 | locals->err_state = 1; | |
398 | } | |
399 | ||
7caf122e KR |
400 | if (opts & OPENSSL_INIT_THREAD_RAND) { |
401 | #ifdef OPENSSL_INIT_DEBUG | |
402 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
403 | "marking thread for rand\n"); | |
404 | #endif | |
405 | locals->rand = 1; | |
406 | } | |
407 | ||
b184e3ef MC |
408 | return 1; |
409 | } | |
410 | ||
f672aee4 | 411 | void OPENSSL_cleanup(void) |
b184e3ef MC |
412 | { |
413 | OPENSSL_INIT_STOP *currhandler, *lasthandler; | |
414 | ||
deca5df2 MC |
415 | /* If we've not been inited then no need to deinit */ |
416 | if (!base_inited) | |
417 | return; | |
418 | ||
dd27f16e RS |
419 | /* Might be explicitly called and also by atexit */ |
420 | if (stopped) | |
421 | return; | |
422 | stopped = 1; | |
423 | ||
b184e3ef MC |
424 | /* |
425 | * Thread stop may not get automatically called by the thread library for | |
426 | * the very last thread in some situations, so call it directly. | |
427 | */ | |
428 | ossl_init_thread_stop(ossl_init_get_thread_local(0)); | |
429 | ||
430 | currhandler = stop_handlers; | |
431 | while (currhandler != NULL) { | |
432 | currhandler->handler(); | |
433 | lasthandler = currhandler; | |
434 | currhandler = currhandler->next; | |
435 | OPENSSL_free(lasthandler); | |
436 | } | |
437 | stop_handlers = NULL; | |
c292b105 MC |
438 | |
439 | CRYPTO_THREAD_lock_free(init_lock); | |
adeb4bc7 | 440 | init_lock = NULL; |
c292b105 | 441 | |
b184e3ef MC |
442 | /* |
443 | * We assume we are single-threaded for this function, i.e. no race | |
444 | * conditions for the various "*_inited" vars below. | |
445 | */ | |
446 | ||
e4ad0763 | 447 | #ifndef OPENSSL_NO_COMP |
b184e3ef MC |
448 | if (zlib_inited) { |
449 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 450 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 451 | "comp_zlib_cleanup_int()\n"); |
b184e3ef | 452 | #endif |
b3599dbb | 453 | comp_zlib_cleanup_int(); |
b184e3ef | 454 | } |
e4ad0763 | 455 | #endif |
b184e3ef | 456 | |
ed49f43a MC |
457 | if (async_inited) { |
458 | # ifdef OPENSSL_INIT_DEBUG | |
459 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " | |
460 | "async_deinit()\n"); | |
461 | # endif | |
462 | async_deinit(); | |
463 | } | |
ed49f43a | 464 | |
b184e3ef MC |
465 | if (load_crypto_strings_inited) { |
466 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 467 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 468 | "err_free_strings_int()\n"); |
b184e3ef | 469 | #endif |
b3599dbb | 470 | err_free_strings_int(); |
b184e3ef MC |
471 | } |
472 | ||
a072ed0c | 473 | CRYPTO_THREAD_cleanup_local(&threadstopkey); |
6bc7bad0 | 474 | |
b184e3ef | 475 | #ifdef OPENSSL_INIT_DEBUG |
58a8fc25 | 476 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 477 | "rand_cleanup_int()\n"); |
58a8fc25 | 478 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 479 | "conf_modules_free_int()\n"); |
9749a07a | 480 | #ifndef OPENSSL_NO_ENGINE |
ae6412f3 | 481 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 482 | "engine_cleanup_int()\n"); |
9749a07a | 483 | #endif |
58a8fc25 | 484 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 485 | "crypto_cleanup_all_ex_data_int()\n"); |
58a8fc25 | 486 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 487 | "bio_sock_cleanup_int()\n"); |
ff234405 MC |
488 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
489 | "bio_cleanup()\n"); | |
58a8fc25 | 490 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 491 | "evp_cleanup_int()\n"); |
58a8fc25 | 492 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 493 | "obj_cleanup_int()\n"); |
ff234405 MC |
494 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
495 | "err_cleanup()\n"); | |
9749a07a | 496 | #endif |
58a8fc25 MC |
497 | /* |
498 | * Note that cleanup order is important: | |
a535fe12 | 499 | * - rand_cleanup_int could call an ENGINE's RAND cleanup function so |
b3599dbb | 500 | * must be called before engine_cleanup_int() |
58a8fc25 MC |
501 | * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up |
502 | * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data(). | |
b3599dbb MC |
503 | * - conf_modules_free_int() can end up in ENGINE code so must be called |
504 | * before engine_cleanup_int() | |
a535fe12 DSH |
505 | * - ENGINEs and additional EVP algorithms might use added OIDs names so |
506 | * obj_cleanup_int() must be called last | |
58a8fc25 | 507 | */ |
b3599dbb | 508 | rand_cleanup_int(); |
c16de9d8 | 509 | rand_drbg_cleanup_int(); |
b3599dbb | 510 | conf_modules_free_int(); |
773fd0ba | 511 | #ifndef OPENSSL_NO_ENGINE |
b3599dbb | 512 | engine_cleanup_int(); |
773fd0ba | 513 | #endif |
71a5516d | 514 | ossl_store_cleanup_int(); |
b3599dbb | 515 | crypto_cleanup_all_ex_data_int(); |
ff234405 | 516 | bio_cleanup(); |
b3599dbb MC |
517 | evp_cleanup_int(); |
518 | obj_cleanup_int(); | |
ff234405 MC |
519 | err_cleanup(); |
520 | ||
d7c402c4 DMSP |
521 | CRYPTO_secure_malloc_done(); |
522 | ||
deca5df2 | 523 | base_inited = 0; |
b184e3ef MC |
524 | } |
525 | ||
b184e3ef MC |
526 | /* |
527 | * If this function is called with a non NULL settings value then it must be | |
528 | * called prior to any threads making calls to any OpenSSL functions, | |
529 | * i.e. passing a non-null settings value is assumed to be single-threaded. | |
530 | */ | |
0fc32b07 | 531 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) |
b184e3ef | 532 | { |
302f7588 MC |
533 | static int stoperrset = 0; |
534 | ||
535 | if (stopped) { | |
536 | if (!stoperrset) { | |
537 | /* | |
538 | * We only ever set this once to avoid getting into an infinite | |
539 | * loop where the error system keeps trying to init and fails so | |
540 | * sets an error etc | |
541 | */ | |
542 | stoperrset = 1; | |
a4625290 | 543 | CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL); |
302f7588 | 544 | } |
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 MC |
687 | dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE); |
688 | DSO_free(dso); | |
689f112d | 689 | ERR_pop_to_mark(); |
2b59d1be MC |
690 | } |
691 | # endif | |
5836780f | 692 | } |
b6d5ba1a | 693 | #endif |
5836780f | 694 | |
b184e3ef MC |
695 | newhand = OPENSSL_malloc(sizeof(*newhand)); |
696 | if (newhand == NULL) | |
697 | return 0; | |
698 | ||
699 | newhand->handler = handler; | |
700 | newhand->next = stop_handlers; | |
701 | stop_handlers = newhand; | |
702 | ||
703 | return 1; | |
704 | } | |
2915fe19 | 705 | |
63ab5ea1 | 706 | #ifdef OPENSSL_SYS_UNIX |
2915fe19 RS |
707 | /* |
708 | * The following three functions are for OpenSSL developers. This is | |
709 | * where we set/reset state across fork (called via pthread_atfork when | |
710 | * it exists, or manually by the application when it doesn't). | |
711 | * | |
712 | * WARNING! If you put code in either OPENSSL_fork_parent or | |
713 | * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal- | |
714 | * safe. See this link, for example: | |
715 | * http://man7.org/linux/man-pages/man7/signal-safety.7.html | |
716 | */ | |
717 | ||
718 | void OPENSSL_fork_prepare(void) | |
719 | { | |
720 | } | |
721 | ||
722 | void OPENSSL_fork_parent(void) | |
723 | { | |
724 | } | |
725 | ||
726 | void OPENSSL_fork_child(void) | |
727 | { | |
a35f607c | 728 | rand_fork(); |
2915fe19 RS |
729 | } |
730 | #endif |