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