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