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