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