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