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