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