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