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