]>
Commit | Line | Data |
---|---|---|
b184e3ef | 1 | /* |
3c2bdd7d | 2 | * Copyright 2016-2021 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 | ||
e4468e6d P |
10 | /* We need to use some engine deprecated APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
07016a8a | 13 | #include "e_os.h" |
25f2138b | 14 | #include "crypto/cryptlib.h" |
b184e3ef | 15 | #include <openssl/err.h> |
25f2138b | 16 | #include "crypto/rand.h" |
176db6dc | 17 | #include "internal/bio.h" |
b184e3ef | 18 | #include <openssl/evp.h> |
25f2138b | 19 | #include "crypto/evp.h" |
176db6dc | 20 | #include "internal/conf.h" |
25f2138b DMSP |
21 | #include "crypto/async.h" |
22 | #include "crypto/engine.h" | |
176db6dc RS |
23 | #include "internal/comp.h" |
24 | #include "internal/err.h" | |
25f2138b DMSP |
25 | #include "crypto/err.h" |
26 | #include "crypto/objects.h" | |
b184e3ef | 27 | #include <stdlib.h> |
dd27f16e | 28 | #include <assert.h> |
176db6dc | 29 | #include "internal/thread_once.h" |
25f2138b | 30 | #include "crypto/dso_conf.h" |
176db6dc | 31 | #include "internal/dso.h" |
25f2138b | 32 | #include "crypto/store.h" |
7960dbec | 33 | #include <openssl/cmp_util.h> /* for OSSL_CMP_log_close() */ |
5c641735 | 34 | #include <openssl/trace.h> |
dd27f16e RS |
35 | |
36 | static int stopped = 0; | |
db6bcc81 | 37 | static uint64_t optsdone = 0; |
b184e3ef | 38 | |
7253fd55 | 39 | typedef struct ossl_init_stop_st OPENSSL_INIT_STOP; |
b184e3ef MC |
40 | struct ossl_init_stop_st { |
41 | void (*handler)(void); | |
42 | OPENSSL_INIT_STOP *next; | |
43 | }; | |
44 | ||
45 | static OPENSSL_INIT_STOP *stop_handlers = NULL; | |
c292b105 | 46 | static CRYPTO_RWLOCK *init_lock = NULL; |
b184e3ef | 47 | |
b1f1e7ae | 48 | static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 49 | static int base_inited = 0; |
c2e4e5d2 | 50 | DEFINE_RUN_ONCE_STATIC(ossl_init_base) |
b184e3ef | 51 | { |
cf0932cd | 52 | /* no need to init trace */ |
5c641735 RL |
53 | |
54 | OSSL_TRACE(INIT, "ossl_init_base: setting up stop handlers\n"); | |
f7edeced RS |
55 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
56 | ossl_malloc_setup_failures(); | |
b184e3ef | 57 | #endif |
72592b86 | 58 | |
eb2b9892 BE |
59 | if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL) |
60 | goto err; | |
b184e3ef | 61 | OPENSSL_cpuid_setup(); |
8aa9cf7e | 62 | |
2be8c56a | 63 | if (!ossl_init_thread()) |
72592b86 MC |
64 | return 0; |
65 | ||
b184e3ef | 66 | base_inited = 1; |
eb2b9892 BE |
67 | return 1; |
68 | ||
69 | err: | |
5c641735 | 70 | OSSL_TRACE(INIT, "ossl_init_base failed!\n"); |
eb2b9892 BE |
71 | CRYPTO_THREAD_lock_free(init_lock); |
72 | init_lock = NULL; | |
5836780f | 73 | |
eb2b9892 BE |
74 | return 0; |
75 | } | |
76 | ||
8f6a5c56 | 77 | static CRYPTO_ONCE register_atexit = CRYPTO_ONCE_STATIC_INIT; |
de2debc5 MC |
78 | #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32) |
79 | static int win32atexit(void) | |
80 | { | |
81 | OPENSSL_cleanup(); | |
82 | return 0; | |
83 | } | |
84 | #endif | |
85 | ||
8f6a5c56 MC |
86 | DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit) |
87 | { | |
de2debc5 | 88 | #ifdef OPENSSL_INIT_DEBUG |
8f6a5c56 | 89 | fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n"); |
de2debc5 | 90 | #endif |
8f6a5c56 | 91 | #ifndef OPENSSL_SYS_UEFI |
de2debc5 MC |
92 | # ifdef _WIN32 |
93 | /* We use _onexit() in preference because it gets called on DLL unload */ | |
94 | if (_onexit(win32atexit) == NULL) | |
95 | return 0; | |
96 | # else | |
8f6a5c56 MC |
97 | if (atexit(OPENSSL_cleanup) != 0) |
98 | return 0; | |
de2debc5 | 99 | # endif |
8f6a5c56 MC |
100 | #endif |
101 | ||
102 | return 1; | |
103 | } | |
104 | ||
105 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit, | |
106 | ossl_init_register_atexit) | |
107 | { | |
108 | #ifdef OPENSSL_INIT_DEBUG | |
109 | fprintf(stderr, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n"); | |
110 | #endif | |
111 | /* Do nothing in this case */ | |
112 | return 1; | |
113 | } | |
114 | ||
eb2b9892 BE |
115 | static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT; |
116 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete) | |
117 | { | |
5c641735 RL |
118 | OSSL_TRACE(INIT, "ossl_init_load_crypto_nodelete()\n"); |
119 | ||
31b6ed76 | 120 | #if !defined(OPENSSL_USE_NODELETE) \ |
41999e7d | 121 | && !defined(OPENSSL_NO_PINSHARED) |
9c98aa35 | 122 | # if defined(DSO_WIN32) && !defined(_WIN32_WCE) |
2b59d1be MC |
123 | { |
124 | HMODULE handle = NULL; | |
125 | BOOL ret; | |
126 | ||
127 | /* We don't use the DSO route for WIN32 because there is a better way */ | |
128 | ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
129 | | GET_MODULE_HANDLE_EX_FLAG_PIN, | |
130 | (void *)&base_inited, &handle); | |
131 | ||
5c641735 RL |
132 | OSSL_TRACE1(INIT, |
133 | "ossl_init_load_crypto_nodelete: " | |
134 | "obtained DSO reference? %s\n", | |
135 | (ret == TRUE ? "No!" : "Yes.")); | |
2b59d1be MC |
136 | return (ret == TRUE) ? 1 : 0; |
137 | } | |
31b6ed76 | 138 | # elif !defined(DSO_NONE) |
5836780f MC |
139 | /* |
140 | * Deliberately leak a reference to ourselves. This will force the library | |
689f112d | 141 | * to remain loaded until the atexit() handler is run at process exit. |
5836780f MC |
142 | */ |
143 | { | |
eb2b9892 BE |
144 | DSO *dso; |
145 | void *err; | |
146 | ||
147 | if (!err_shelve_state(&err)) | |
148 | return 0; | |
5836780f MC |
149 | |
150 | dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE); | |
4af14b7b MK |
151 | /* |
152 | * In case of No!, it is uncertain our exit()-handlers can still be | |
153 | * called. After dlclose() the whole library might have been unloaded | |
154 | * already. | |
155 | */ | |
5c641735 RL |
156 | OSSL_TRACE1(INIT, "obtained DSO reference? %s\n", |
157 | (dso == NULL ? "No!" : "Yes.")); | |
5836780f | 158 | DSO_free(dso); |
eb2b9892 | 159 | err_unshelve_state(err); |
5836780f | 160 | } |
2b59d1be | 161 | # endif |
b6d5ba1a | 162 | #endif |
5836780f | 163 | |
c2e4e5d2 | 164 | return 1; |
b184e3ef MC |
165 | } |
166 | ||
b1f1e7ae | 167 | static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 168 | static int load_crypto_strings_inited = 0; |
c2e4e5d2 | 169 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings) |
b184e3ef | 170 | { |
69588edb | 171 | int ret = 1; |
498abff0 MC |
172 | /* |
173 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time | |
174 | * pulling in all the error strings during static linking | |
175 | */ | |
176 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) | |
5c641735 | 177 | OSSL_TRACE(INIT, "err_load_crypto_strings_int()\n"); |
69588edb | 178 | ret = err_load_crypto_strings_int(); |
b184e3ef | 179 | load_crypto_strings_inited = 1; |
bd91e3c8 | 180 | #endif |
69588edb | 181 | return ret; |
b184e3ef MC |
182 | } |
183 | ||
660a1e04 MC |
184 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings, |
185 | ossl_init_load_crypto_strings) | |
186 | { | |
187 | /* Do nothing in this case */ | |
188 | return 1; | |
189 | } | |
190 | ||
b1f1e7ae | 191 | static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 192 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers) |
b184e3ef MC |
193 | { |
194 | /* | |
195 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
196 | * pulling in all the ciphers during static linking | |
197 | */ | |
198 | #ifndef OPENSSL_NO_AUTOALGINIT | |
5c641735 | 199 | OSSL_TRACE(INIT, "openssl_add_all_ciphers_int()\n"); |
b3599dbb | 200 | openssl_add_all_ciphers_int(); |
b184e3ef | 201 | #endif |
c2e4e5d2 | 202 | return 1; |
b184e3ef MC |
203 | } |
204 | ||
660a1e04 MC |
205 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers, |
206 | ossl_init_add_all_ciphers) | |
207 | { | |
208 | /* Do nothing */ | |
209 | return 1; | |
210 | } | |
211 | ||
b1f1e7ae | 212 | static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 213 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests) |
b184e3ef MC |
214 | { |
215 | /* | |
216 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
217 | * pulling in all the ciphers during static linking | |
218 | */ | |
219 | #ifndef OPENSSL_NO_AUTOALGINIT | |
5c641735 | 220 | OSSL_TRACE(INIT, "openssl_add_all_digests()\n"); |
b3599dbb | 221 | openssl_add_all_digests_int(); |
b184e3ef | 222 | #endif |
c2e4e5d2 | 223 | return 1; |
b184e3ef MC |
224 | } |
225 | ||
660a1e04 MC |
226 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests, |
227 | ossl_init_add_all_digests) | |
228 | { | |
229 | /* Do nothing */ | |
230 | return 1; | |
231 | } | |
232 | ||
b1f1e7ae | 233 | static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 234 | static int config_inited = 0; |
df1f538f | 235 | static const OPENSSL_INIT_SETTINGS *conf_settings = NULL; |
c2e4e5d2 | 236 | DEFINE_RUN_ONCE_STATIC(ossl_init_config) |
ae031148 | 237 | { |
f148f703 | 238 | int ret = ossl_config_int(NULL); |
ae031148 MC |
239 | |
240 | config_inited = 1; | |
241 | return ret; | |
242 | } | |
243 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_config_settings, ossl_init_config) | |
b184e3ef | 244 | { |
f148f703 | 245 | int ret = ossl_config_int(conf_settings); |
ae031148 | 246 | |
b184e3ef | 247 | config_inited = 1; |
df1f538f | 248 | return ret; |
b184e3ef | 249 | } |
660a1e04 | 250 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config) |
b184e3ef | 251 | { |
f148f703 SL |
252 | OSSL_TRACE(INIT, "ossl_no_config_int()\n"); |
253 | ossl_no_config_int(); | |
b184e3ef | 254 | config_inited = 1; |
c2e4e5d2 | 255 | return 1; |
b184e3ef MC |
256 | } |
257 | ||
b1f1e7ae | 258 | static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 259 | static int async_inited = 0; |
c2e4e5d2 | 260 | DEFINE_RUN_ONCE_STATIC(ossl_init_async) |
b184e3ef | 261 | { |
5c641735 | 262 | OSSL_TRACE(INIT, "async_init()\n"); |
c2e4e5d2 RL |
263 | if (!async_init()) |
264 | return 0; | |
b184e3ef | 265 | async_inited = 1; |
c2e4e5d2 | 266 | return 1; |
b184e3ef MC |
267 | } |
268 | ||
269 | #ifndef OPENSSL_NO_ENGINE | |
b1f1e7ae | 270 | static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 271 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl) |
b184e3ef | 272 | { |
5c641735 | 273 | OSSL_TRACE(INIT, "engine_load_openssl_int()\n"); |
b3599dbb | 274 | engine_load_openssl_int(); |
c2e4e5d2 | 275 | return 1; |
b184e3ef | 276 | } |
b184e3ef | 277 | # ifndef OPENSSL_NO_RDRAND |
b1f1e7ae | 278 | static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 279 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand) |
b184e3ef | 280 | { |
5c641735 | 281 | OSSL_TRACE(INIT, "engine_load_rdrand_int()\n"); |
b3599dbb | 282 | engine_load_rdrand_int(); |
c2e4e5d2 | 283 | return 1; |
b184e3ef MC |
284 | } |
285 | # endif | |
b1f1e7ae | 286 | static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 287 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic) |
b184e3ef | 288 | { |
5c641735 | 289 | OSSL_TRACE(INIT, "engine_load_dynamic_int()\n"); |
b3599dbb | 290 | engine_load_dynamic_int(); |
c2e4e5d2 | 291 | return 1; |
b184e3ef MC |
292 | } |
293 | # ifndef OPENSSL_NO_STATIC_ENGINE | |
2afebe0b EQ |
294 | # ifndef OPENSSL_NO_DEVCRYPTOENG |
295 | static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT; | |
296 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto) | |
297 | { | |
5c641735 | 298 | OSSL_TRACE(INIT, "engine_load_devcrypto_int()\n"); |
2afebe0b EQ |
299 | engine_load_devcrypto_int(); |
300 | return 1; | |
301 | } | |
302 | # endif | |
469ce8ff | 303 | # if !defined(OPENSSL_NO_PADLOCKENG) |
b1f1e7ae | 304 | static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 305 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock) |
b184e3ef | 306 | { |
5c641735 | 307 | OSSL_TRACE(INIT, "engine_load_padlock_int()\n"); |
b3599dbb | 308 | engine_load_padlock_int(); |
c2e4e5d2 | 309 | return 1; |
b184e3ef MC |
310 | } |
311 | # endif | |
312 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 313 | static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 314 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi) |
b184e3ef | 315 | { |
5c641735 | 316 | OSSL_TRACE(INIT, "engine_load_capi_int()\n"); |
b3599dbb | 317 | engine_load_capi_int(); |
c2e4e5d2 | 318 | return 1; |
b184e3ef MC |
319 | } |
320 | # endif | |
6cba4a66 | 321 | # if !defined(OPENSSL_NO_AFALGENG) |
a4d8bcf1 | 322 | static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 323 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg) |
6cba4a66 | 324 | { |
5c641735 | 325 | OSSL_TRACE(INIT, "engine_load_afalg_int()\n"); |
b3599dbb | 326 | engine_load_afalg_int(); |
c2e4e5d2 | 327 | return 1; |
6cba4a66 | 328 | } |
329 | # endif | |
b184e3ef MC |
330 | # endif |
331 | #endif | |
332 | ||
e4ad0763 | 333 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 334 | static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT; |
e4ad0763 | 335 | |
b184e3ef | 336 | static int zlib_inited = 0; |
c2e4e5d2 | 337 | DEFINE_RUN_ONCE_STATIC(ossl_init_zlib) |
b184e3ef MC |
338 | { |
339 | /* Do nothing - we need to know about this for the later cleanup */ | |
340 | zlib_inited = 1; | |
c2e4e5d2 | 341 | return 1; |
b184e3ef | 342 | } |
e4ad0763 | 343 | #endif |
b184e3ef | 344 | |
f672aee4 | 345 | void OPENSSL_cleanup(void) |
b184e3ef MC |
346 | { |
347 | OPENSSL_INIT_STOP *currhandler, *lasthandler; | |
348 | ||
65a1e917 | 349 | /* |
50864bd2 MC |
350 | * At some point we should consider looking at this function with a view to |
351 | * moving most/all of this into onfree handlers in OSSL_LIB_CTX. | |
65a1e917 MC |
352 | */ |
353 | ||
deca5df2 MC |
354 | /* If we've not been inited then no need to deinit */ |
355 | if (!base_inited) | |
356 | return; | |
357 | ||
dd27f16e RS |
358 | /* Might be explicitly called and also by atexit */ |
359 | if (stopped) | |
360 | return; | |
361 | stopped = 1; | |
362 | ||
b184e3ef MC |
363 | /* |
364 | * Thread stop may not get automatically called by the thread library for | |
365 | * the very last thread in some situations, so call it directly. | |
366 | */ | |
72592b86 | 367 | OPENSSL_thread_stop(); |
b184e3ef MC |
368 | |
369 | currhandler = stop_handlers; | |
370 | while (currhandler != NULL) { | |
371 | currhandler->handler(); | |
372 | lasthandler = currhandler; | |
373 | currhandler = currhandler->next; | |
374 | OPENSSL_free(lasthandler); | |
375 | } | |
376 | stop_handlers = NULL; | |
c292b105 MC |
377 | |
378 | CRYPTO_THREAD_lock_free(init_lock); | |
adeb4bc7 | 379 | init_lock = NULL; |
c292b105 | 380 | |
b184e3ef MC |
381 | /* |
382 | * We assume we are single-threaded for this function, i.e. no race | |
383 | * conditions for the various "*_inited" vars below. | |
384 | */ | |
385 | ||
e4ad0763 | 386 | #ifndef OPENSSL_NO_COMP |
b184e3ef | 387 | if (zlib_inited) { |
f148f703 SL |
388 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zlib_cleanup()\n"); |
389 | ossl_comp_zlib_cleanup(); | |
b184e3ef | 390 | } |
e4ad0763 | 391 | #endif |
b184e3ef | 392 | |
ed49f43a | 393 | if (async_inited) { |
5c641735 | 394 | OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n"); |
ed49f43a MC |
395 | async_deinit(); |
396 | } | |
ed49f43a | 397 | |
b184e3ef | 398 | if (load_crypto_strings_inited) { |
5c641735 | 399 | OSSL_TRACE(INIT, "OPENSSL_cleanup: err_free_strings_int()\n"); |
b3599dbb | 400 | err_free_strings_int(); |
b184e3ef MC |
401 | } |
402 | ||
58a8fc25 MC |
403 | /* |
404 | * Note that cleanup order is important: | |
1335ca4b | 405 | * - ossl_rand_cleanup_int could call an ENGINE's RAND cleanup function so |
b3599dbb | 406 | * must be called before engine_cleanup_int() |
58a8fc25 | 407 | * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up |
b4250010 | 408 | * before the ex data handlers are wiped during default ossl_lib_ctx deinit. |
f148f703 | 409 | * - ossl_config_modules_free() can end up in ENGINE code so must be called |
b3599dbb | 410 | * before engine_cleanup_int() |
a535fe12 | 411 | * - ENGINEs and additional EVP algorithms might use added OIDs names so |
f148f703 | 412 | * ossl_obj_cleanup_int() must be called last |
58a8fc25 | 413 | */ |
1335ca4b SL |
414 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_rand_cleanup_int()\n"); |
415 | ossl_rand_cleanup_int(); | |
5c641735 | 416 | |
f148f703 SL |
417 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_config_modules_free()\n"); |
418 | ossl_config_modules_free(); | |
1aedc35f | 419 | |
773fd0ba | 420 | #ifndef OPENSSL_NO_ENGINE |
5c641735 | 421 | OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n"); |
b3599dbb | 422 | engine_cleanup_int(); |
773fd0ba | 423 | #endif |
a1447076 RL |
424 | |
425 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
5c641735 | 426 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n"); |
71a5516d | 427 | ossl_store_cleanup_int(); |
a1447076 | 428 | #endif |
5c641735 | 429 | |
b4250010 DMSP |
430 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_lib_ctx_default_deinit()\n"); |
431 | ossl_lib_ctx_default_deinit(); | |
5c641735 | 432 | |
6913f5fe MC |
433 | ossl_cleanup_thread(); |
434 | ||
5c641735 | 435 | OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n"); |
ff234405 | 436 | bio_cleanup(); |
5c641735 RL |
437 | |
438 | OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n"); | |
b3599dbb | 439 | evp_cleanup_int(); |
5c641735 | 440 | |
f148f703 SL |
441 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_obj_cleanup_int()\n"); |
442 | ossl_obj_cleanup_int(); | |
5c641735 RL |
443 | |
444 | OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n"); | |
ff234405 MC |
445 | err_cleanup(); |
446 | ||
5c641735 | 447 | OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n"); |
d7c402c4 DMSP |
448 | CRYPTO_secure_malloc_done(); |
449 | ||
7960dbec DDO |
450 | #ifndef OPENSSL_NO_CMP |
451 | OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n"); | |
452 | OSSL_CMP_log_close(); | |
453 | #endif | |
454 | ||
5c641735 RL |
455 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n"); |
456 | ossl_trace_cleanup(); | |
457 | ||
deca5df2 | 458 | base_inited = 0; |
b184e3ef MC |
459 | } |
460 | ||
b184e3ef MC |
461 | /* |
462 | * If this function is called with a non NULL settings value then it must be | |
463 | * called prior to any threads making calls to any OpenSSL functions, | |
464 | * i.e. passing a non-null settings value is assumed to be single-threaded. | |
465 | */ | |
0fc32b07 | 466 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) |
b184e3ef | 467 | { |
db6bcc81 MC |
468 | uint64_t tmp; |
469 | int aloaddone = 0; | |
470 | ||
471 | /* | |
472 | * We ignore failures from this function. It is probably because we are | |
473 | * on a platform that doesn't support lockless atomic loads (we may not | |
474 | * have created init_lock yet so we can't use it). This is just an | |
475 | * optimisation to skip the full checks in this function if we don't need | |
476 | * to, so we carry on regardless in the event of failure. | |
477 | * | |
478 | * There could be a race here with other threads, so that optsdone has not | |
479 | * been updated yet, even though the options have in fact been initialised. | |
480 | * This doesn't matter - it just means we will run the full function | |
481 | * unnecessarily - but all the critical code is contained in RUN_ONCE | |
482 | * functions anyway so we are safe. | |
483 | */ | |
484 | if (CRYPTO_atomic_load(&optsdone, &tmp, NULL)) { | |
485 | if ((tmp & opts) == opts) | |
486 | return 1; | |
487 | aloaddone = 1; | |
488 | } | |
489 | ||
65a1e917 | 490 | /* |
50864bd2 MC |
491 | * At some point we should look at this function with a view to moving |
492 | * most/all of this into OSSL_LIB_CTX. | |
65a1e917 MC |
493 | */ |
494 | ||
302f7588 | 495 | if (stopped) { |
eb2b9892 | 496 | if (!(opts & OPENSSL_INIT_BASE_ONLY)) |
9311d0c4 | 497 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL); |
0fc32b07 | 498 | return 0; |
302f7588 | 499 | } |
dd27f16e | 500 | |
df1f538f VD |
501 | /* |
502 | * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the | |
503 | * *only* option specified. With that option we return immediately after | |
504 | * doing the requested limited initialization. Note that | |
505 | * err_shelve_state() called by us via ossl_init_load_crypto_nodelete() | |
506 | * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with | |
507 | * base already initialized this is a harmless NOOP. | |
508 | * | |
509 | * If we remain the only caller of err_shelve_state() the recursion should | |
510 | * perhaps be removed, but if in doubt, it can be left in place. | |
511 | */ | |
eb2b9892 BE |
512 | if (!RUN_ONCE(&base, ossl_init_base)) |
513 | return 0; | |
514 | ||
df1f538f VD |
515 | if (opts & OPENSSL_INIT_BASE_ONLY) |
516 | return 1; | |
517 | ||
db6bcc81 MC |
518 | /* |
519 | * init_lock should definitely be set up now, so we can now repeat the | |
520 | * same check from above but be sure that it will work even on platforms | |
521 | * without lockless CRYPTO_atomic_load | |
522 | */ | |
523 | if (!aloaddone) { | |
524 | if (!CRYPTO_atomic_load(&optsdone, &tmp, init_lock)) | |
525 | return 0; | |
526 | if ((tmp & opts) == opts) | |
527 | return 1; | |
528 | } | |
529 | ||
df1f538f VD |
530 | /* |
531 | * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls | |
532 | * should not have the side-effect of setting up exit handlers, and | |
533 | * therefore, this code block is below the INIT_BASE_ONLY-conditioned early | |
534 | * return above. | |
535 | */ | |
8f6a5c56 MC |
536 | if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) { |
537 | if (!RUN_ONCE_ALT(®ister_atexit, ossl_init_no_register_atexit, | |
538 | ossl_init_register_atexit)) | |
539 | return 0; | |
540 | } else if (!RUN_ONCE(®ister_atexit, ossl_init_register_atexit)) { | |
541 | return 0; | |
542 | } | |
543 | ||
df1f538f | 544 | if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete)) |
b1f1e7ae | 545 | return 0; |
b184e3ef | 546 | |
b1f1e7ae | 547 | if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS) |
660a1e04 MC |
548 | && !RUN_ONCE_ALT(&load_crypto_strings, |
549 | ossl_init_no_load_crypto_strings, | |
550 | ossl_init_load_crypto_strings)) | |
b1f1e7ae | 551 | return 0; |
b184e3ef | 552 | |
b1f1e7ae | 553 | if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS) |
c2e4e5d2 | 554 | && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings)) |
b1f1e7ae | 555 | return 0; |
b184e3ef | 556 | |
b1f1e7ae | 557 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS) |
660a1e04 MC |
558 | && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers, |
559 | ossl_init_add_all_ciphers)) | |
b1f1e7ae | 560 | return 0; |
b184e3ef | 561 | |
b1f1e7ae | 562 | if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS) |
c2e4e5d2 | 563 | && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers)) |
b1f1e7ae | 564 | return 0; |
b184e3ef | 565 | |
b1f1e7ae | 566 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS) |
660a1e04 MC |
567 | && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests, |
568 | ossl_init_add_all_digests)) | |
b1f1e7ae | 569 | return 0; |
b184e3ef | 570 | |
b1f1e7ae | 571 | if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS) |
c2e4e5d2 | 572 | && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests)) |
b1f1e7ae | 573 | return 0; |
b184e3ef | 574 | |
b5319bdb | 575 | if ((opts & OPENSSL_INIT_ATFORK) |
2915fe19 RS |
576 | && !openssl_init_fork_handlers()) |
577 | return 0; | |
578 | ||
b1f1e7ae | 579 | if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) |
660a1e04 | 580 | && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config)) |
b1f1e7ae | 581 | return 0; |
b184e3ef MC |
582 | |
583 | if (opts & OPENSSL_INIT_LOAD_CONFIG) { | |
b1f1e7ae | 584 | int ret; |
ae031148 MC |
585 | |
586 | if (settings == NULL) { | |
587 | ret = RUN_ONCE(&config, ossl_init_config); | |
588 | } else { | |
cd3f8c1b RS |
589 | if (!CRYPTO_THREAD_write_lock(init_lock)) |
590 | return 0; | |
ae031148 MC |
591 | conf_settings = settings; |
592 | ret = RUN_ONCE_ALT(&config, ossl_init_config_settings, | |
593 | ossl_init_config); | |
594 | conf_settings = NULL; | |
595 | CRYPTO_THREAD_unlock(init_lock); | |
596 | } | |
597 | ||
e3af453b | 598 | if (ret <= 0) |
b1f1e7ae | 599 | return 0; |
b184e3ef MC |
600 | } |
601 | ||
b1f1e7ae | 602 | if ((opts & OPENSSL_INIT_ASYNC) |
c2e4e5d2 | 603 | && !RUN_ONCE(&async, ossl_init_async)) |
b1f1e7ae | 604 | return 0; |
7626fbf2 | 605 | |
b184e3ef | 606 | #ifndef OPENSSL_NO_ENGINE |
b1f1e7ae | 607 | if ((opts & OPENSSL_INIT_ENGINE_OPENSSL) |
c2e4e5d2 | 608 | && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl)) |
b1f1e7ae | 609 | return 0; |
b184e3ef | 610 | # ifndef OPENSSL_NO_RDRAND |
b1f1e7ae | 611 | if ((opts & OPENSSL_INIT_ENGINE_RDRAND) |
c2e4e5d2 | 612 | && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand)) |
b1f1e7ae | 613 | return 0; |
b184e3ef | 614 | # endif |
b1f1e7ae | 615 | if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC) |
c2e4e5d2 | 616 | && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic)) |
b1f1e7ae | 617 | return 0; |
b184e3ef | 618 | # ifndef OPENSSL_NO_STATIC_ENGINE |
2afebe0b EQ |
619 | # ifndef OPENSSL_NO_DEVCRYPTOENG |
620 | if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV) | |
621 | && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto)) | |
622 | return 0; | |
623 | # endif | |
469ce8ff | 624 | # if !defined(OPENSSL_NO_PADLOCKENG) |
b1f1e7ae | 625 | if ((opts & OPENSSL_INIT_ENGINE_PADLOCK) |
c2e4e5d2 | 626 | && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock)) |
b1f1e7ae | 627 | return 0; |
b184e3ef MC |
628 | # endif |
629 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 630 | if ((opts & OPENSSL_INIT_ENGINE_CAPI) |
c2e4e5d2 | 631 | && !RUN_ONCE(&engine_capi, ossl_init_engine_capi)) |
b1f1e7ae | 632 | return 0; |
b184e3ef | 633 | # endif |
6cba4a66 | 634 | # if !defined(OPENSSL_NO_AFALGENG) |
b1f1e7ae | 635 | if ((opts & OPENSSL_INIT_ENGINE_AFALG) |
c2e4e5d2 | 636 | && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg)) |
b1f1e7ae | 637 | return 0; |
6cba4a66 | 638 | # endif |
b184e3ef MC |
639 | # endif |
640 | if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN | |
8d00e30f | 641 | | OPENSSL_INIT_ENGINE_OPENSSL |
6cba4a66 | 642 | | OPENSSL_INIT_ENGINE_AFALG)) { |
b184e3ef MC |
643 | ENGINE_register_all_complete(); |
644 | } | |
645 | #endif | |
646 | ||
e4ad0763 | 647 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 648 | if ((opts & OPENSSL_INIT_ZLIB) |
c2e4e5d2 | 649 | && !RUN_ONCE(&zlib, ossl_init_zlib)) |
b1f1e7ae | 650 | return 0; |
e4ad0763 | 651 | #endif |
0fc32b07 | 652 | |
db6bcc81 MC |
653 | if (!CRYPTO_atomic_or(&optsdone, opts, &tmp, init_lock)) |
654 | return 0; | |
655 | ||
0fc32b07 | 656 | return 1; |
b184e3ef MC |
657 | } |
658 | ||
f672aee4 | 659 | int OPENSSL_atexit(void (*handler)(void)) |
b184e3ef MC |
660 | { |
661 | OPENSSL_INIT_STOP *newhand; | |
662 | ||
31b6ed76 | 663 | #if !defined(OPENSSL_USE_NODELETE)\ |
41999e7d | 664 | && !defined(OPENSSL_NO_PINSHARED) |
5836780f | 665 | { |
5836780f MC |
666 | union { |
667 | void *sym; | |
668 | void (*func)(void); | |
669 | } handlersym; | |
670 | ||
671 | handlersym.func = handler; | |
9c98aa35 | 672 | # if defined(DSO_WIN32) && !defined(_WIN32_WCE) |
2b59d1be MC |
673 | { |
674 | HMODULE handle = NULL; | |
675 | BOOL ret; | |
5836780f | 676 | |
2b59d1be MC |
677 | /* |
678 | * We don't use the DSO route for WIN32 because there is a better | |
679 | * way | |
680 | */ | |
681 | ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
682 | | GET_MODULE_HANDLE_EX_FLAG_PIN, | |
683 | handlersym.sym, &handle); | |
684 | ||
685 | if (!ret) | |
686 | return 0; | |
687 | } | |
31b6ed76 | 688 | # elif !defined(DSO_NONE) |
2b59d1be MC |
689 | /* |
690 | * Deliberately leak a reference to the handler. This will force the | |
691 | * library/code containing the handler to remain loaded until we run the | |
692 | * atexit handler. If -znodelete has been used then this is | |
c9a41d7d | 693 | * unnecessary. |
2b59d1be MC |
694 | */ |
695 | { | |
696 | DSO *dso = NULL; | |
697 | ||
689f112d | 698 | ERR_set_mark(); |
2b59d1be | 699 | dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE); |
4af14b7b | 700 | /* See same code above in ossl_init_base() for an explanation. */ |
5c641735 RL |
701 | OSSL_TRACE1(INIT, |
702 | "atexit: obtained DSO reference? %s\n", | |
703 | (dso == NULL ? "No!" : "Yes.")); | |
2b59d1be | 704 | DSO_free(dso); |
689f112d | 705 | ERR_pop_to_mark(); |
2b59d1be MC |
706 | } |
707 | # endif | |
5836780f | 708 | } |
b6d5ba1a | 709 | #endif |
5836780f | 710 | |
cdb10bae | 711 | if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) { |
9311d0c4 | 712 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
b184e3ef | 713 | return 0; |
cdb10bae | 714 | } |
b184e3ef MC |
715 | |
716 | newhand->handler = handler; | |
717 | newhand->next = stop_handlers; | |
718 | stop_handlers = newhand; | |
719 | ||
720 | return 1; | |
721 | } | |
2915fe19 | 722 |