]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/init.c
Fix a RUN_ONCE bug
[thirdparty/openssl.git] / crypto / init.c
CommitLineData
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"
dd27f16e
RS
30
31static int stopped = 0;
b184e3ef 32
80ae7285
AP
33/*
34 * Since per-thread-specific-data destructors are not universally
35 * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key
36 * is assumed to have destructor associated. And then an effort is made
37 * to call this single destructor on non-pthread platform[s].
38 *
39 * Initial value is "impossible". It is used as guard value to shortcut
40 * destructor for threads terminating before libcrypto is initialized or
41 * after it's de-initialized. Access to the key doesn't have to be
42 * serialized for the said threads, because they didn't use libcrypto
43 * and it doesn't matter if they pick "impossible" or derefernce real
44 * key value and pull NULL past initialization in the first thread that
45 * intends to use libcrypto.
46 */
0b1319ba
AP
47static union {
48 long sane;
49 CRYPTO_THREAD_LOCAL value;
50} destructor_key = { -1 };
71567a6f 51
80ae7285 52static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
b184e3ef 53
80ae7285 54static void ossl_init_thread_destructor(void *local)
b184e3ef
MC
55{
56 ossl_init_thread_stop((struct thread_local_inits_st *)local);
57}
58
b7326ea7 59static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
b184e3ef 60{
a072ed0c 61 struct thread_local_inits_st *local =
0b1319ba 62 CRYPTO_THREAD_get_local(&destructor_key.value);
b184e3ef 63
80ae7285
AP
64 if (alloc) {
65 if (local == NULL
66 && (local = OPENSSL_zalloc(sizeof(*local))) != NULL
0b1319ba 67 && !CRYPTO_THREAD_set_local(&destructor_key.value, local)) {
3ac6d5ee
BE
68 OPENSSL_free(local);
69 return NULL;
70 }
80ae7285 71 } else {
0b1319ba 72 CRYPTO_THREAD_set_local(&destructor_key.value, NULL);
b7326ea7 73 }
b184e3ef
MC
74
75 return local;
76}
77
7253fd55 78typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
b184e3ef
MC
79struct ossl_init_stop_st {
80 void (*handler)(void);
81 OPENSSL_INIT_STOP *next;
82};
83
84static OPENSSL_INIT_STOP *stop_handlers = NULL;
c292b105 85static CRYPTO_RWLOCK *init_lock = NULL;
b184e3ef 86
b1f1e7ae 87static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
b184e3ef 88static int base_inited = 0;
c2e4e5d2 89DEFINE_RUN_ONCE_STATIC(ossl_init_base)
b184e3ef 90{
80ae7285
AP
91 CRYPTO_THREAD_LOCAL key;
92
b184e3ef
MC
93#ifdef OPENSSL_INIT_DEBUG
94 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
f7edeced
RS
95#endif
96#ifndef OPENSSL_NO_CRYPTO_MDEBUG
97 ossl_malloc_setup_failures();
b184e3ef 98#endif
80ae7285 99 if (!CRYPTO_THREAD_init_local(&key, ossl_init_thread_destructor))
eb2b9892
BE
100 return 0;
101 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
102 goto err;
c7b7938e 103#ifndef OPENSSL_SYS_UEFI
eb2b9892
BE
104 if (atexit(OPENSSL_cleanup) != 0)
105 goto err;
c7b7938e 106#endif
b184e3ef 107 OPENSSL_cpuid_setup();
8aa9cf7e 108
0b1319ba 109 destructor_key.value = key;
b184e3ef 110 base_inited = 1;
eb2b9892
BE
111 return 1;
112
113err:
114#ifdef OPENSSL_INIT_DEBUG
115 fprintf(stderr, "OPENSSL_INIT: ossl_init_base not ok!\n");
116#endif
117 CRYPTO_THREAD_lock_free(init_lock);
118 init_lock = NULL;
5836780f 119
80ae7285 120 CRYPTO_THREAD_cleanup_local(&key);
eb2b9892
BE
121 return 0;
122}
123
124static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
125DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
126{
127#ifdef OPENSSL_INIT_DEBUG
128 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_nodelete()\n");
129#endif
6e290a25 130#if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
2b59d1be
MC
131# ifdef DSO_WIN32
132 {
133 HMODULE handle = NULL;
134 BOOL ret;
135
136 /* We don't use the DSO route for WIN32 because there is a better way */
137 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
138 | GET_MODULE_HANDLE_EX_FLAG_PIN,
139 (void *)&base_inited, &handle);
140
eb2b9892
BE
141# ifdef OPENSSL_INIT_DEBUG
142 fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
143 (ret == TRUE ? "No!" : "Yes."));
144# endif
2b59d1be
MC
145 return (ret == TRUE) ? 1 : 0;
146 }
147# else
5836780f
MC
148 /*
149 * Deliberately leak a reference to ourselves. This will force the library
689f112d 150 * to remain loaded until the atexit() handler is run at process exit.
5836780f
MC
151 */
152 {
eb2b9892
BE
153 DSO *dso;
154 void *err;
155
156 if (!err_shelve_state(&err))
157 return 0;
5836780f
MC
158
159 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
4af14b7b
MK
160# ifdef OPENSSL_INIT_DEBUG
161 fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
162 (dso == NULL ? "No!" : "Yes."));
163 /*
164 * In case of No!, it is uncertain our exit()-handlers can still be
165 * called. After dlclose() the whole library might have been unloaded
166 * already.
167 */
168# endif
5836780f 169 DSO_free(dso);
eb2b9892 170 err_unshelve_state(err);
5836780f 171 }
2b59d1be 172# endif
b6d5ba1a 173#endif
5836780f 174
c2e4e5d2 175 return 1;
b184e3ef
MC
176}
177
b1f1e7ae 178static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
b184e3ef 179static int load_crypto_strings_inited = 0;
c2e4e5d2 180DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
b184e3ef 181{
69588edb 182 int ret = 1;
498abff0
MC
183 /*
184 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
185 * pulling in all the error strings during static linking
186 */
187#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
b184e3ef
MC
188# ifdef OPENSSL_INIT_DEBUG
189 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
b3599dbb 190 "err_load_crypto_strings_int()\n");
b184e3ef 191# endif
69588edb 192 ret = err_load_crypto_strings_int();
b184e3ef 193 load_crypto_strings_inited = 1;
bd91e3c8 194#endif
69588edb 195 return ret;
b184e3ef
MC
196}
197
660a1e04
MC
198DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
199 ossl_init_load_crypto_strings)
200{
201 /* Do nothing in this case */
202 return 1;
203}
204
b1f1e7ae 205static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 206DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
b184e3ef
MC
207{
208 /*
209 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
210 * pulling in all the ciphers during static linking
211 */
212#ifndef OPENSSL_NO_AUTOALGINIT
213# ifdef OPENSSL_INIT_DEBUG
214 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
b3599dbb 215 "openssl_add_all_ciphers_int()\n");
b184e3ef 216# endif
b3599dbb 217 openssl_add_all_ciphers_int();
b184e3ef 218#endif
c2e4e5d2 219 return 1;
b184e3ef
MC
220}
221
660a1e04
MC
222DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
223 ossl_init_add_all_ciphers)
224{
225 /* Do nothing */
226 return 1;
227}
228
b1f1e7ae 229static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 230DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
b184e3ef
MC
231{
232 /*
233 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
234 * pulling in all the ciphers during static linking
235 */
236#ifndef OPENSSL_NO_AUTOALGINIT
237# ifdef OPENSSL_INIT_DEBUG
238 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
b3599dbb 239 "openssl_add_all_digests()\n");
b184e3ef 240# endif
b3599dbb 241 openssl_add_all_digests_int();
b184e3ef 242#endif
c2e4e5d2 243 return 1;
b184e3ef
MC
244}
245
660a1e04
MC
246DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
247 ossl_init_add_all_digests)
248{
249 /* Do nothing */
250 return 1;
251}
252
0145dd32
RL
253static CRYPTO_ONCE add_all_macs = CRYPTO_ONCE_STATIC_INIT;
254DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_macs)
255{
256 /*
257 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
258 * pulling in all the macs during static linking
259 */
260#ifndef OPENSSL_NO_AUTOALGINIT
261# ifdef OPENSSL_INIT_DEBUG
262 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_macs: "
263 "openssl_add_all_macs_int()\n");
264# endif
265 openssl_add_all_macs_int();
266#endif
267 return 1;
268}
269
660a1e04 270DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_macs, ossl_init_add_all_macs)
b184e3ef
MC
271{
272 /* Do nothing */
c2e4e5d2 273 return 1;
b184e3ef
MC
274}
275
b1f1e7ae 276static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
b184e3ef 277static int config_inited = 0;
cda3ae5b 278static const char *appname;
c2e4e5d2 279DEFINE_RUN_ONCE_STATIC(ossl_init_config)
b184e3ef
MC
280{
281#ifdef OPENSSL_INIT_DEBUG
282 fprintf(stderr,
b3599dbb 283 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
cda3ae5b 284 appname == NULL ? "NULL" : appname);
b184e3ef 285#endif
cda3ae5b 286 openssl_config_int(appname);
b184e3ef 287 config_inited = 1;
c2e4e5d2 288 return 1;
b184e3ef 289}
660a1e04 290DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
b184e3ef
MC
291{
292#ifdef OPENSSL_INIT_DEBUG
293 fprintf(stderr,
b3599dbb 294 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
b184e3ef 295#endif
b3599dbb 296 openssl_no_config_int();
b184e3ef 297 config_inited = 1;
c2e4e5d2 298 return 1;
b184e3ef
MC
299}
300
b1f1e7ae 301static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
b184e3ef 302static int async_inited = 0;
c2e4e5d2 303DEFINE_RUN_ONCE_STATIC(ossl_init_async)
b184e3ef
MC
304{
305#ifdef OPENSSL_INIT_DEBUG
306 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
307#endif
c2e4e5d2
RL
308 if (!async_init())
309 return 0;
b184e3ef 310 async_inited = 1;
c2e4e5d2 311 return 1;
b184e3ef
MC
312}
313
314#ifndef OPENSSL_NO_ENGINE
b1f1e7ae 315static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 316DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
b184e3ef
MC
317{
318# ifdef OPENSSL_INIT_DEBUG
319 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
b3599dbb 320 "engine_load_openssl_int()\n");
b184e3ef 321# endif
b3599dbb 322 engine_load_openssl_int();
c2e4e5d2 323 return 1;
b184e3ef 324}
619eb33a
RL
325# ifndef OPENSSL_NO_DEVCRYPTOENG
326static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
327DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
328{
329# ifdef OPENSSL_INIT_DEBUG
330 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
331 "engine_load_devcrypto_int()\n");
332# endif
333 engine_load_devcrypto_int();
334 return 1;
335}
336# endif
b184e3ef
MC
337
338# ifndef OPENSSL_NO_RDRAND
b1f1e7ae 339static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 340DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
b184e3ef
MC
341{
342# ifdef OPENSSL_INIT_DEBUG
343 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
b3599dbb 344 "engine_load_rdrand_int()\n");
b184e3ef 345# endif
b3599dbb 346 engine_load_rdrand_int();
c2e4e5d2 347 return 1;
b184e3ef
MC
348}
349# endif
b1f1e7ae 350static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 351DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
b184e3ef
MC
352{
353# ifdef OPENSSL_INIT_DEBUG
354 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
b3599dbb 355 "engine_load_dynamic_int()\n");
b184e3ef 356# endif
b3599dbb 357 engine_load_dynamic_int();
c2e4e5d2 358 return 1;
b184e3ef
MC
359}
360# ifndef OPENSSL_NO_STATIC_ENGINE
361# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
b1f1e7ae 362static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 363DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
b184e3ef
MC
364{
365# ifdef OPENSSL_INIT_DEBUG
366 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
b3599dbb 367 "engine_load_padlock_int()\n");
b184e3ef 368# endif
b3599dbb 369 engine_load_padlock_int();
c2e4e5d2 370 return 1;
b184e3ef
MC
371}
372# endif
373# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
b1f1e7ae 374static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 375DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
b184e3ef
MC
376{
377# ifdef OPENSSL_INIT_DEBUG
378 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
b3599dbb 379 "engine_load_capi_int()\n");
b184e3ef 380# endif
b3599dbb 381 engine_load_capi_int();
c2e4e5d2 382 return 1;
b184e3ef
MC
383}
384# endif
6cba4a66 385# if !defined(OPENSSL_NO_AFALGENG)
a4d8bcf1 386static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
c2e4e5d2 387DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
6cba4a66 388{
389# ifdef OPENSSL_INIT_DEBUG
390 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
b3599dbb 391 "engine_load_afalg_int()\n");
6cba4a66 392# endif
b3599dbb 393 engine_load_afalg_int();
c2e4e5d2 394 return 1;
6cba4a66 395}
396# endif
b184e3ef
MC
397# endif
398#endif
399
e4ad0763 400#ifndef OPENSSL_NO_COMP
b1f1e7ae 401static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
e4ad0763 402
b184e3ef 403static int zlib_inited = 0;
c2e4e5d2 404DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
b184e3ef
MC
405{
406 /* Do nothing - we need to know about this for the later cleanup */
407 zlib_inited = 1;
c2e4e5d2 408 return 1;
b184e3ef 409}
e4ad0763 410#endif
b184e3ef 411
71567a6f 412static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
b184e3ef
MC
413{
414 /* Can't do much about this */
415 if (locals == NULL)
416 return;
417
418 if (locals->async) {
419#ifdef OPENSSL_INIT_DEBUG
420 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
74a8acbd 421 "async_delete_thread_state()\n");
b184e3ef 422#endif
74a8acbd 423 async_delete_thread_state();
b184e3ef
MC
424 }
425
426 if (locals->err_state) {
427#ifdef OPENSSL_INIT_DEBUG
428 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
21e00174 429 "err_delete_thread_state()\n");
b184e3ef 430#endif
21e00174 431 err_delete_thread_state();
b184e3ef
MC
432 }
433
7caf122e
KR
434 if (locals->rand) {
435#ifdef OPENSSL_INIT_DEBUG
436 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
437 "drbg_delete_thread_state()\n");
438#endif
439 drbg_delete_thread_state();
440 }
441
b184e3ef 442 OPENSSL_free(locals);
b184e3ef
MC
443}
444
f672aee4 445void OPENSSL_thread_stop(void)
71567a6f 446{
0b1319ba 447 if (destructor_key.sane != -1)
80ae7285 448 ossl_init_thread_stop(ossl_init_get_thread_local(0));
71567a6f
MC
449}
450
b184e3ef
MC
451int ossl_init_thread_start(uint64_t opts)
452{
3ac6d5ee
BE
453 struct thread_local_inits_st *locals;
454
455 if (!OPENSSL_init_crypto(0, NULL))
456 return 0;
457
458 locals = ossl_init_get_thread_local(1);
b184e3ef
MC
459
460 if (locals == NULL)
461 return 0;
462
463 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
464#ifdef OPENSSL_INIT_DEBUG
465 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
466 "marking thread for async\n");
467#endif
468 locals->async = 1;
469 }
470
471 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
472#ifdef OPENSSL_INIT_DEBUG
473 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
474 "marking thread for err_state\n");
475#endif
476 locals->err_state = 1;
477 }
478
7caf122e
KR
479 if (opts & OPENSSL_INIT_THREAD_RAND) {
480#ifdef OPENSSL_INIT_DEBUG
481 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
482 "marking thread for rand\n");
483#endif
484 locals->rand = 1;
485 }
486
b184e3ef
MC
487 return 1;
488}
489
f672aee4 490void OPENSSL_cleanup(void)
b184e3ef
MC
491{
492 OPENSSL_INIT_STOP *currhandler, *lasthandler;
80ae7285 493 CRYPTO_THREAD_LOCAL key;
b184e3ef 494
deca5df2
MC
495 /* If we've not been inited then no need to deinit */
496 if (!base_inited)
497 return;
498
dd27f16e
RS
499 /* Might be explicitly called and also by atexit */
500 if (stopped)
501 return;
502 stopped = 1;
503
b184e3ef
MC
504 /*
505 * Thread stop may not get automatically called by the thread library for
506 * the very last thread in some situations, so call it directly.
507 */
508 ossl_init_thread_stop(ossl_init_get_thread_local(0));
509
510 currhandler = stop_handlers;
511 while (currhandler != NULL) {
512 currhandler->handler();
513 lasthandler = currhandler;
514 currhandler = currhandler->next;
515 OPENSSL_free(lasthandler);
516 }
517 stop_handlers = NULL;
c292b105
MC
518
519 CRYPTO_THREAD_lock_free(init_lock);
adeb4bc7 520 init_lock = NULL;
c292b105 521
b184e3ef
MC
522 /*
523 * We assume we are single-threaded for this function, i.e. no race
524 * conditions for the various "*_inited" vars below.
525 */
526
e4ad0763 527#ifndef OPENSSL_NO_COMP
b184e3ef
MC
528 if (zlib_inited) {
529#ifdef OPENSSL_INIT_DEBUG
f672aee4 530 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 531 "comp_zlib_cleanup_int()\n");
b184e3ef 532#endif
b3599dbb 533 comp_zlib_cleanup_int();
b184e3ef 534 }
e4ad0763 535#endif
b184e3ef 536
ed49f43a
MC
537 if (async_inited) {
538# ifdef OPENSSL_INIT_DEBUG
539 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
540 "async_deinit()\n");
541# endif
542 async_deinit();
543 }
ed49f43a 544
b184e3ef
MC
545 if (load_crypto_strings_inited) {
546#ifdef OPENSSL_INIT_DEBUG
f672aee4 547 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 548 "err_free_strings_int()\n");
b184e3ef 549#endif
b3599dbb 550 err_free_strings_int();
b184e3ef
MC
551 }
552
0b1319ba
AP
553 key = destructor_key.value;
554 destructor_key.sane = -1;
80ae7285 555 CRYPTO_THREAD_cleanup_local(&key);
6bc7bad0 556
b184e3ef 557#ifdef OPENSSL_INIT_DEBUG
58a8fc25 558 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 559 "rand_cleanup_int()\n");
58a8fc25 560 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 561 "conf_modules_free_int()\n");
9749a07a 562#ifndef OPENSSL_NO_ENGINE
ae6412f3 563 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 564 "engine_cleanup_int()\n");
9749a07a 565#endif
58a8fc25 566 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 567 "crypto_cleanup_all_ex_data_int()\n");
58a8fc25 568 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 569 "bio_sock_cleanup_int()\n");
ff234405
MC
570 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
571 "bio_cleanup()\n");
58a8fc25 572 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 573 "evp_cleanup_int()\n");
58a8fc25 574 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 575 "obj_cleanup_int()\n");
ff234405
MC
576 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
577 "err_cleanup()\n");
9749a07a 578#endif
58a8fc25
MC
579 /*
580 * Note that cleanup order is important:
a535fe12 581 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
b3599dbb 582 * must be called before engine_cleanup_int()
58a8fc25
MC
583 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
584 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
b3599dbb
MC
585 * - conf_modules_free_int() can end up in ENGINE code so must be called
586 * before engine_cleanup_int()
a535fe12
DSH
587 * - ENGINEs and additional EVP algorithms might use added OIDs names so
588 * obj_cleanup_int() must be called last
58a8fc25 589 */
b3599dbb 590 rand_cleanup_int();
c16de9d8 591 rand_drbg_cleanup_int();
b3599dbb 592 conf_modules_free_int();
773fd0ba 593#ifndef OPENSSL_NO_ENGINE
b3599dbb 594 engine_cleanup_int();
773fd0ba 595#endif
71a5516d 596 ossl_store_cleanup_int();
b3599dbb 597 crypto_cleanup_all_ex_data_int();
ff234405 598 bio_cleanup();
b3599dbb
MC
599 evp_cleanup_int();
600 obj_cleanup_int();
ff234405
MC
601 err_cleanup();
602
d7c402c4
DMSP
603 CRYPTO_secure_malloc_done();
604
deca5df2 605 base_inited = 0;
b184e3ef
MC
606}
607
b184e3ef
MC
608/*
609 * If this function is called with a non NULL settings value then it must be
610 * called prior to any threads making calls to any OpenSSL functions,
611 * i.e. passing a non-null settings value is assumed to be single-threaded.
612 */
0fc32b07 613int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
b184e3ef 614{
302f7588 615 if (stopped) {
eb2b9892
BE
616 if (!(opts & OPENSSL_INIT_BASE_ONLY))
617 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
0fc32b07 618 return 0;
302f7588 619 }
dd27f16e 620
eb2b9892
BE
621 if (!RUN_ONCE(&base, ossl_init_base))
622 return 0;
623
624 if (!(opts & OPENSSL_INIT_BASE_ONLY)
625 && !RUN_ONCE(&load_crypto_nodelete,
626 ossl_init_load_crypto_nodelete))
b1f1e7ae 627 return 0;
b184e3ef 628
b1f1e7ae 629 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
660a1e04
MC
630 && !RUN_ONCE_ALT(&load_crypto_strings,
631 ossl_init_no_load_crypto_strings,
632 ossl_init_load_crypto_strings))
b1f1e7ae 633 return 0;
b184e3ef 634
b1f1e7ae 635 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
c2e4e5d2 636 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
b1f1e7ae 637 return 0;
b184e3ef 638
b1f1e7ae 639 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
660a1e04
MC
640 && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
641 ossl_init_add_all_ciphers))
b1f1e7ae 642 return 0;
b184e3ef 643
b1f1e7ae 644 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
c2e4e5d2 645 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
b1f1e7ae 646 return 0;
b184e3ef 647
b1f1e7ae 648 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
660a1e04
MC
649 && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
650 ossl_init_add_all_digests))
b1f1e7ae 651 return 0;
b184e3ef 652
b1f1e7ae 653 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
c2e4e5d2 654 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
b1f1e7ae 655 return 0;
b184e3ef 656
0145dd32 657 if ((opts & OPENSSL_INIT_NO_ADD_ALL_MACS)
660a1e04
MC
658 && !RUN_ONCE_ALT(&add_all_macs, ossl_init_no_add_all_macs,
659 ossl_init_add_all_macs))
0145dd32
RL
660 return 0;
661
662 if ((opts & OPENSSL_INIT_ADD_ALL_MACS)
663 && !RUN_ONCE(&add_all_macs, ossl_init_add_all_macs))
664 return 0;
665
b5319bdb 666 if ((opts & OPENSSL_INIT_ATFORK)
2915fe19
RS
667 && !openssl_init_fork_handlers())
668 return 0;
669
b1f1e7ae 670 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
660a1e04 671 && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
b1f1e7ae 672 return 0;
b184e3ef
MC
673
674 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
b1f1e7ae 675 int ret;
c292b105 676 CRYPTO_THREAD_write_lock(init_lock);
cda3ae5b 677 appname = (settings == NULL) ? NULL : settings->appname;
c2e4e5d2 678 ret = RUN_ONCE(&config, ossl_init_config);
c292b105 679 CRYPTO_THREAD_unlock(init_lock);
b1f1e7ae
MC
680 if (!ret)
681 return 0;
b184e3ef
MC
682 }
683
b1f1e7ae 684 if ((opts & OPENSSL_INIT_ASYNC)
c2e4e5d2 685 && !RUN_ONCE(&async, ossl_init_async))
b1f1e7ae 686 return 0;
7626fbf2 687
b184e3ef 688#ifndef OPENSSL_NO_ENGINE
b1f1e7ae 689 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
c2e4e5d2 690 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
b1f1e7ae 691 return 0;
619eb33a
RL
692# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
693 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
694 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
695 return 0;
696# endif
b184e3ef 697# ifndef OPENSSL_NO_RDRAND
b1f1e7ae 698 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
c2e4e5d2 699 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
b1f1e7ae 700 return 0;
b184e3ef 701# endif
b1f1e7ae 702 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
c2e4e5d2 703 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
b1f1e7ae 704 return 0;
b184e3ef
MC
705# ifndef OPENSSL_NO_STATIC_ENGINE
706# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
b1f1e7ae 707 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
c2e4e5d2 708 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
b1f1e7ae 709 return 0;
b184e3ef
MC
710# endif
711# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
b1f1e7ae 712 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
c2e4e5d2 713 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
b1f1e7ae 714 return 0;
b184e3ef 715# endif
6cba4a66 716# if !defined(OPENSSL_NO_AFALGENG)
b1f1e7ae 717 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
c2e4e5d2 718 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
b1f1e7ae 719 return 0;
6cba4a66 720# endif
b184e3ef
MC
721# endif
722 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
8d00e30f 723 | OPENSSL_INIT_ENGINE_OPENSSL
6cba4a66 724 | OPENSSL_INIT_ENGINE_AFALG)) {
b184e3ef
MC
725 ENGINE_register_all_complete();
726 }
727#endif
728
e4ad0763 729#ifndef OPENSSL_NO_COMP
b1f1e7ae 730 if ((opts & OPENSSL_INIT_ZLIB)
c2e4e5d2 731 && !RUN_ONCE(&zlib, ossl_init_zlib))
b1f1e7ae 732 return 0;
e4ad0763 733#endif
0fc32b07
MC
734
735 return 1;
b184e3ef
MC
736}
737
f672aee4 738int OPENSSL_atexit(void (*handler)(void))
b184e3ef
MC
739{
740 OPENSSL_INIT_STOP *newhand;
741
6e290a25 742#if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
5836780f 743 {
5836780f
MC
744 union {
745 void *sym;
746 void (*func)(void);
747 } handlersym;
748
749 handlersym.func = handler;
2b59d1be
MC
750# ifdef DSO_WIN32
751 {
752 HMODULE handle = NULL;
753 BOOL ret;
5836780f 754
2b59d1be
MC
755 /*
756 * We don't use the DSO route for WIN32 because there is a better
757 * way
758 */
759 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
760 | GET_MODULE_HANDLE_EX_FLAG_PIN,
761 handlersym.sym, &handle);
762
763 if (!ret)
764 return 0;
765 }
766# else
767 /*
768 * Deliberately leak a reference to the handler. This will force the
769 * library/code containing the handler to remain loaded until we run the
770 * atexit handler. If -znodelete has been used then this is
c9a41d7d 771 * unnecessary.
2b59d1be
MC
772 */
773 {
774 DSO *dso = NULL;
775
689f112d 776 ERR_set_mark();
2b59d1be 777 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
4af14b7b
MK
778# ifdef OPENSSL_INIT_DEBUG
779 fprintf(stderr,
780 "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
781 (dso == NULL ? "No!" : "Yes."));
782 /* See same code above in ossl_init_base() for an explanation. */
783# endif
2b59d1be 784 DSO_free(dso);
689f112d 785 ERR_pop_to_mark();
2b59d1be
MC
786 }
787# endif
5836780f 788 }
b6d5ba1a 789#endif
5836780f 790
cdb10bae
RS
791 if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
792 CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
b184e3ef 793 return 0;
cdb10bae 794 }
b184e3ef
MC
795
796 newhand->handler = handler;
797 newhand->next = stop_handlers;
798 stop_handlers = newhand;
799
800 return 1;
801}
2915fe19 802
63ab5ea1 803#ifdef OPENSSL_SYS_UNIX
2915fe19
RS
804/*
805 * The following three functions are for OpenSSL developers. This is
806 * where we set/reset state across fork (called via pthread_atfork when
807 * it exists, or manually by the application when it doesn't).
808 *
809 * WARNING! If you put code in either OPENSSL_fork_parent or
810 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
811 * safe. See this link, for example:
812 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
813 */
814
815void OPENSSL_fork_prepare(void)
816{
817}
818
819void OPENSSL_fork_parent(void)
820{
821}
822
823void OPENSSL_fork_child(void)
824{
a35f607c 825 rand_fork();
2915fe19
RS
826}
827#endif