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