2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include "internal/e_os.h"
14 #include "crypto/cryptlib.h"
15 #include <openssl/err.h>
16 #include "crypto/rand.h"
17 #include "internal/bio.h"
18 #include <openssl/evp.h>
19 #include "crypto/evp.h"
20 #include "internal/conf.h"
21 #include "crypto/async.h"
22 #include "crypto/engine.h"
23 #include "internal/comp.h"
24 #include "internal/err.h"
25 #include "crypto/err.h"
26 #include "crypto/objects.h"
29 #include "internal/thread_once.h"
30 #include "crypto/dso_conf.h"
31 #include "internal/dso.h"
32 #include "crypto/store.h"
33 #include <openssl/cmp_util.h> /* for OSSL_CMP_log_close() */
34 #include <openssl/trace.h>
35 #include "crypto/ctype.h"
37 static int stopped = 0;
38 static uint64_t optsdone = 0;
40 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
41 struct ossl_init_stop_st {
42 void (*handler)(void);
43 OPENSSL_INIT_STOP *next;
46 static OPENSSL_INIT_STOP *stop_handlers = NULL;
47 static CRYPTO_RWLOCK *init_lock = NULL;
48 static CRYPTO_THREAD_LOCAL in_init_config_local;
50 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
51 static int base_inited = 0;
52 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
54 /* no need to init trace */
56 OSSL_TRACE(INIT, "ossl_init_base: setting up stop handlers\n");
57 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
58 ossl_malloc_setup_failures();
61 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
63 OPENSSL_cpuid_setup();
65 if (!ossl_init_thread())
68 if (!CRYPTO_THREAD_init_local(&in_init_config_local, NULL))
75 OSSL_TRACE(INIT, "ossl_init_base failed!\n");
76 CRYPTO_THREAD_lock_free(init_lock);
82 static CRYPTO_ONCE register_atexit = CRYPTO_ONCE_STATIC_INIT;
83 #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
84 static int win32atexit(void)
91 DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit)
93 #ifdef OPENSSL_INIT_DEBUG
94 fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n");
96 #ifndef OPENSSL_SYS_UEFI
97 # if defined(_WIN32) && !defined(__BORLANDC__)
98 /* We use _onexit() in preference because it gets called on DLL unload */
99 if (_onexit(win32atexit) == NULL)
102 if (atexit(OPENSSL_cleanup) != 0)
110 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit,
111 ossl_init_register_atexit)
113 #ifdef OPENSSL_INIT_DEBUG
114 fprintf(stderr, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n");
116 /* Do nothing in this case */
120 static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
121 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
123 OSSL_TRACE(INIT, "ossl_init_load_crypto_nodelete()\n");
125 #if !defined(OPENSSL_USE_NODELETE) \
126 && !defined(OPENSSL_NO_PINSHARED)
127 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
129 HMODULE handle = NULL;
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);
138 "ossl_init_load_crypto_nodelete: "
139 "obtained DSO reference? %s\n",
140 (ret == TRUE ? "No!" : "Yes."));
141 return (ret == TRUE) ? 1 : 0;
143 # elif !defined(DSO_NONE)
145 * Deliberately leak a reference to ourselves. This will force the library
146 * to remain loaded until the atexit() handler is run at process exit.
152 if (!err_shelve_state(&err))
155 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
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
161 OSSL_TRACE1(INIT, "obtained DSO reference? %s\n",
162 (dso == NULL ? "No!" : "Yes."));
164 err_unshelve_state(err);
172 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
173 static int load_crypto_strings_inited = 0;
174 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
178 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
179 * pulling in all the error strings during static linking
181 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
182 OSSL_TRACE(INIT, "ossl_err_load_crypto_strings()\n");
183 ret = ossl_err_load_crypto_strings();
184 load_crypto_strings_inited = 1;
189 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
190 ossl_init_load_crypto_strings)
192 /* Do nothing in this case */
196 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
197 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
200 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
201 * pulling in all the ciphers during static linking
203 #ifndef OPENSSL_NO_AUTOALGINIT
204 OSSL_TRACE(INIT, "openssl_add_all_ciphers_int()\n");
205 openssl_add_all_ciphers_int();
210 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
211 ossl_init_add_all_ciphers)
217 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
218 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
221 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
222 * pulling in all the ciphers during static linking
224 #ifndef OPENSSL_NO_AUTOALGINIT
225 OSSL_TRACE(INIT, "openssl_add_all_digests()\n");
226 openssl_add_all_digests_int();
231 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
232 ossl_init_add_all_digests)
238 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
239 static int config_inited = 0;
240 static const OPENSSL_INIT_SETTINGS *conf_settings = NULL;
241 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
243 int ret = ossl_config_int(NULL);
248 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_config_settings, ossl_init_config)
250 int ret = ossl_config_int(conf_settings);
255 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
257 OSSL_TRACE(INIT, "ossl_no_config_int()\n");
258 ossl_no_config_int();
263 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
264 static int async_inited = 0;
265 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
267 OSSL_TRACE(INIT, "async_init()\n");
274 static CRYPTO_ONCE casecmp = CRYPTO_ONCE_STATIC_INIT;
275 static int casecmp_inited = 0;
276 DEFINE_RUN_ONCE_STATIC(ossl_init_casecmp)
278 int ret = ossl_init_casecmp_int();
283 #ifndef OPENSSL_NO_ENGINE
284 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
285 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
287 OSSL_TRACE(INIT, "engine_load_openssl_int()\n");
288 engine_load_openssl_int();
291 # ifndef OPENSSL_NO_RDRAND
292 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
293 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
295 OSSL_TRACE(INIT, "engine_load_rdrand_int()\n");
296 engine_load_rdrand_int();
300 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
301 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
303 OSSL_TRACE(INIT, "engine_load_dynamic_int()\n");
304 engine_load_dynamic_int();
307 # ifndef OPENSSL_NO_STATIC_ENGINE
308 # ifndef OPENSSL_NO_DEVCRYPTOENG
309 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
310 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
312 OSSL_TRACE(INIT, "engine_load_devcrypto_int()\n");
313 engine_load_devcrypto_int();
317 # if !defined(OPENSSL_NO_PADLOCKENG)
318 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
319 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
321 OSSL_TRACE(INIT, "engine_load_padlock_int()\n");
322 engine_load_padlock_int();
326 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
327 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
328 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
330 OSSL_TRACE(INIT, "engine_load_capi_int()\n");
331 engine_load_capi_int();
335 # if !defined(OPENSSL_NO_AFALGENG)
336 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
337 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
339 OSSL_TRACE(INIT, "engine_load_afalg_int()\n");
340 engine_load_afalg_int();
347 void OPENSSL_cleanup(void)
349 OPENSSL_INIT_STOP *currhandler, *lasthandler;
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.
356 /* If we've not been inited then no need to deinit */
360 /* Might be explicitly called and also by atexit */
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.
369 OPENSSL_thread_stop();
371 currhandler = stop_handlers;
372 while (currhandler != NULL) {
373 currhandler->handler();
374 lasthandler = currhandler;
375 currhandler = currhandler->next;
376 OPENSSL_free(lasthandler);
378 stop_handlers = NULL;
380 CRYPTO_THREAD_lock_free(init_lock);
383 CRYPTO_THREAD_cleanup_local(&in_init_config_local);
386 * We assume we are single-threaded for this function, i.e. no race
387 * conditions for the various "*_inited" vars below.
390 #ifndef OPENSSL_NO_COMP
391 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zlib_cleanup()\n");
392 ossl_comp_zlib_cleanup();
396 OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
400 if (load_crypto_strings_inited) {
401 OSSL_TRACE(INIT, "OPENSSL_cleanup: err_free_strings_int()\n");
402 err_free_strings_int();
406 * Note that cleanup order is important:
407 * - ossl_rand_cleanup_int could call an ENGINE's RAND cleanup function so
408 * must be called before engine_cleanup_int()
409 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
410 * before the ex data handlers are wiped during default ossl_lib_ctx deinit.
411 * - ossl_config_modules_free() can end up in ENGINE code so must be called
412 * before engine_cleanup_int()
413 * - ENGINEs and additional EVP algorithms might use added OIDs names so
414 * ossl_obj_cleanup_int() must be called last
416 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_rand_cleanup_int()\n");
417 ossl_rand_cleanup_int();
419 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_config_modules_free()\n");
420 ossl_config_modules_free();
422 #ifndef OPENSSL_NO_ENGINE
423 OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
424 engine_cleanup_int();
427 #ifndef OPENSSL_NO_DEPRECATED_3_0
428 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
429 ossl_store_cleanup_int();
432 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_lib_ctx_default_deinit()\n");
433 ossl_lib_ctx_default_deinit();
435 ossl_cleanup_thread();
437 OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
440 OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
443 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_obj_cleanup_int()\n");
444 ossl_obj_cleanup_int();
446 OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
449 OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
450 CRYPTO_secure_malloc_done();
452 #ifndef OPENSSL_NO_CMP
453 OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n");
454 OSSL_CMP_log_close();
457 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
458 ossl_trace_cleanup();
460 if (casecmp_inited) {
461 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_deinit_casecmp()\n");
462 ossl_deinit_casecmp();
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.
473 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
477 if (!RUN_ONCE(&casecmp, ossl_init_casecmp))
481 /* Applications depend on 0 being returned when cleanup was already done */
483 if (!(opts & OPENSSL_INIT_BASE_ONLY))
484 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL);
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.
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.
501 if (CRYPTO_atomic_load(&optsdone, &tmp, NULL)) {
502 if ((tmp & opts) == opts)
508 * At some point we should look at this function with a view to moving
509 * most/all of this into OSSL_LIB_CTX.
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.
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.
521 if (!RUN_ONCE(&base, ossl_init_base))
524 if (opts & OPENSSL_INIT_BASE_ONLY)
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
533 if (!CRYPTO_atomic_load(&optsdone, &tmp, init_lock))
535 if ((tmp & opts) == opts)
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
545 if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
546 if (!RUN_ONCE_ALT(®ister_atexit, ossl_init_no_register_atexit,
547 ossl_init_register_atexit))
549 } else if (!RUN_ONCE(®ister_atexit, ossl_init_register_atexit)) {
553 if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
556 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
557 && !RUN_ONCE_ALT(&load_crypto_strings,
558 ossl_init_no_load_crypto_strings,
559 ossl_init_load_crypto_strings))
562 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
563 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
566 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
567 && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
568 ossl_init_add_all_ciphers))
571 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
572 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
575 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
576 && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
577 ossl_init_add_all_digests))
580 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
581 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
584 if ((opts & OPENSSL_INIT_ATFORK)
585 && !openssl_init_fork_handlers())
588 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
589 && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
592 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
593 int loading = CRYPTO_THREAD_get_local(&in_init_config_local) != NULL;
595 /* If called recursively from OBJ_ calls, just skip it. */
599 if (!CRYPTO_THREAD_set_local(&in_init_config_local, (void *)-1))
601 if (settings == NULL) {
602 ret = RUN_ONCE(&config, ossl_init_config);
604 if (!CRYPTO_THREAD_write_lock(init_lock))
606 conf_settings = settings;
607 ret = RUN_ONCE_ALT(&config, ossl_init_config_settings,
609 conf_settings = NULL;
610 CRYPTO_THREAD_unlock(init_lock);
618 if ((opts & OPENSSL_INIT_ASYNC)
619 && !RUN_ONCE(&async, ossl_init_async))
622 #ifndef OPENSSL_NO_ENGINE
623 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
624 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
626 # ifndef OPENSSL_NO_RDRAND
627 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
628 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
631 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
632 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
634 # ifndef OPENSSL_NO_STATIC_ENGINE
635 # ifndef OPENSSL_NO_DEVCRYPTOENG
636 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
637 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
640 # if !defined(OPENSSL_NO_PADLOCKENG)
641 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
642 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
645 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
646 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
647 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
650 # if !defined(OPENSSL_NO_AFALGENG)
651 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
652 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
656 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
657 | OPENSSL_INIT_ENGINE_OPENSSL
658 | OPENSSL_INIT_ENGINE_AFALG)) {
659 ENGINE_register_all_complete();
663 if (!CRYPTO_atomic_or(&optsdone, opts, &tmp, init_lock))
669 int OPENSSL_atexit(void (*handler)(void))
671 OPENSSL_INIT_STOP *newhand;
673 #if !defined(OPENSSL_USE_NODELETE)\
674 && !defined(OPENSSL_NO_PINSHARED)
681 handlersym.func = handler;
682 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
684 HMODULE handle = NULL;
688 * We don't use the DSO route for WIN32 because there is a better
691 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
692 | GET_MODULE_HANDLE_EX_FLAG_PIN,
693 handlersym.sym, &handle);
698 # elif !defined(DSO_NONE)
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
709 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
710 /* See same code above in ossl_init_base() for an explanation. */
712 "atexit: obtained DSO reference? %s\n",
713 (dso == NULL ? "No!" : "Yes."));
721 if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
722 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
726 newhand->handler = handler;
727 newhand->next = stop_handlers;
728 stop_handlers = newhand;