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