2 * Copyright 2016-2020 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
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>
36 static int stopped = 0;
37 static uint64_t optsdone = 0;
39 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
40 struct ossl_init_stop_st {
41 void (*handler)(void);
42 OPENSSL_INIT_STOP *next;
45 static OPENSSL_INIT_STOP *stop_handlers = NULL;
46 static CRYPTO_RWLOCK *init_lock = NULL;
48 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
49 static int base_inited = 0;
50 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
52 /* no need to init trace */
54 OSSL_TRACE(INIT, "ossl_init_base: setting up stop handlers\n");
55 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
56 ossl_malloc_setup_failures();
59 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
61 OPENSSL_cpuid_setup();
63 if (!ossl_init_thread())
70 OSSL_TRACE(INIT, "ossl_init_base failed!\n");
71 CRYPTO_THREAD_lock_free(init_lock);
77 static CRYPTO_ONCE register_atexit = CRYPTO_ONCE_STATIC_INIT;
78 #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
79 static int win32atexit(void)
86 DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit)
88 #ifdef OPENSSL_INIT_DEBUG
89 fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n");
91 #ifndef OPENSSL_SYS_UEFI
93 /* We use _onexit() in preference because it gets called on DLL unload */
94 if (_onexit(win32atexit) == NULL)
97 if (atexit(OPENSSL_cleanup) != 0)
105 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit,
106 ossl_init_register_atexit)
108 #ifdef OPENSSL_INIT_DEBUG
109 fprintf(stderr, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n");
111 /* Do nothing in this case */
115 static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
116 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
118 OSSL_TRACE(INIT, "ossl_init_load_crypto_nodelete()\n");
120 #if !defined(OPENSSL_USE_NODELETE) \
121 && !defined(OPENSSL_NO_PINSHARED)
122 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
124 HMODULE handle = NULL;
127 /* We don't use the DSO route for WIN32 because there is a better way */
128 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
129 | GET_MODULE_HANDLE_EX_FLAG_PIN,
130 (void *)&base_inited, &handle);
133 "ossl_init_load_crypto_nodelete: "
134 "obtained DSO reference? %s\n",
135 (ret == TRUE ? "No!" : "Yes."));
136 return (ret == TRUE) ? 1 : 0;
138 # elif !defined(DSO_NONE)
140 * Deliberately leak a reference to ourselves. This will force the library
141 * to remain loaded until the atexit() handler is run at process exit.
147 if (!err_shelve_state(&err))
150 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
152 * In case of No!, it is uncertain our exit()-handlers can still be
153 * called. After dlclose() the whole library might have been unloaded
156 OSSL_TRACE1(INIT, "obtained DSO reference? %s\n",
157 (dso == NULL ? "No!" : "Yes."));
159 err_unshelve_state(err);
167 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
168 static int load_crypto_strings_inited = 0;
169 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
173 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
174 * pulling in all the error strings during static linking
176 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
177 OSSL_TRACE(INIT, "err_load_crypto_strings_int()\n");
178 ret = err_load_crypto_strings_int();
179 load_crypto_strings_inited = 1;
184 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
185 ossl_init_load_crypto_strings)
187 /* Do nothing in this case */
191 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
192 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
195 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
196 * pulling in all the ciphers during static linking
198 #ifndef OPENSSL_NO_AUTOALGINIT
199 OSSL_TRACE(INIT, "openssl_add_all_ciphers_int()\n");
200 openssl_add_all_ciphers_int();
205 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
206 ossl_init_add_all_ciphers)
212 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
213 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
216 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
217 * pulling in all the ciphers during static linking
219 #ifndef OPENSSL_NO_AUTOALGINIT
220 OSSL_TRACE(INIT, "openssl_add_all_digests()\n");
221 openssl_add_all_digests_int();
226 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
227 ossl_init_add_all_digests)
233 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
234 static int config_inited = 0;
235 static const OPENSSL_INIT_SETTINGS *conf_settings = NULL;
236 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
238 int ret = openssl_config_int(NULL);
243 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_config_settings, ossl_init_config)
245 int ret = openssl_config_int(conf_settings);
250 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
252 OSSL_TRACE(INIT, "openssl_no_config_int()\n");
253 openssl_no_config_int();
258 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
259 static int async_inited = 0;
260 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
262 OSSL_TRACE(INIT, "async_init()\n");
269 #ifndef OPENSSL_NO_ENGINE
270 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
271 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
273 OSSL_TRACE(INIT, "engine_load_openssl_int()\n");
274 engine_load_openssl_int();
277 # ifndef OPENSSL_NO_RDRAND
278 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
279 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
281 OSSL_TRACE(INIT, "engine_load_rdrand_int()\n");
282 engine_load_rdrand_int();
286 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
287 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
289 OSSL_TRACE(INIT, "engine_load_dynamic_int()\n");
290 engine_load_dynamic_int();
293 # ifndef OPENSSL_NO_STATIC_ENGINE
294 # ifndef OPENSSL_NO_DEVCRYPTOENG
295 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
296 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
298 OSSL_TRACE(INIT, "engine_load_devcrypto_int()\n");
299 engine_load_devcrypto_int();
303 # if !defined(OPENSSL_NO_PADLOCKENG)
304 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
305 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
307 OSSL_TRACE(INIT, "engine_load_padlock_int()\n");
308 engine_load_padlock_int();
312 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
313 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
314 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
316 OSSL_TRACE(INIT, "engine_load_capi_int()\n");
317 engine_load_capi_int();
321 # if !defined(OPENSSL_NO_AFALGENG)
322 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
323 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
325 OSSL_TRACE(INIT, "engine_load_afalg_int()\n");
326 engine_load_afalg_int();
333 #ifndef OPENSSL_NO_COMP
334 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
336 static int zlib_inited = 0;
337 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
339 /* Do nothing - we need to know about this for the later cleanup */
345 void OPENSSL_cleanup(void)
347 OPENSSL_INIT_STOP *currhandler, *lasthandler;
350 * TODO(3.0): This function needs looking at with a view to moving most/all
351 * of this into onfree handlers in OSSL_LIB_CTX.
354 /* If we've not been inited then no need to deinit */
358 /* Might be explicitly called and also by atexit */
364 * Thread stop may not get automatically called by the thread library for
365 * the very last thread in some situations, so call it directly.
367 OPENSSL_thread_stop();
369 currhandler = stop_handlers;
370 while (currhandler != NULL) {
371 currhandler->handler();
372 lasthandler = currhandler;
373 currhandler = currhandler->next;
374 OPENSSL_free(lasthandler);
376 stop_handlers = NULL;
378 CRYPTO_THREAD_lock_free(init_lock);
382 * We assume we are single-threaded for this function, i.e. no race
383 * conditions for the various "*_inited" vars below.
386 #ifndef OPENSSL_NO_COMP
388 OSSL_TRACE(INIT, "OPENSSL_cleanup: comp_zlib_cleanup_int()\n");
389 comp_zlib_cleanup_int();
394 OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
398 if (load_crypto_strings_inited) {
399 OSSL_TRACE(INIT, "OPENSSL_cleanup: err_free_strings_int()\n");
400 err_free_strings_int();
404 * Note that cleanup order is important:
405 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
406 * must be called before engine_cleanup_int()
407 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
408 * before the ex data handlers are wiped during default ossl_lib_ctx deinit.
409 * - conf_modules_free_int() can end up in ENGINE code so must be called
410 * before engine_cleanup_int()
411 * - ENGINEs and additional EVP algorithms might use added OIDs names so
412 * obj_cleanup_int() must be called last
414 OSSL_TRACE(INIT, "OPENSSL_cleanup: rand_cleanup_int()\n");
417 OSSL_TRACE(INIT, "OPENSSL_cleanup: conf_modules_free_int()\n");
418 conf_modules_free_int();
420 #ifndef OPENSSL_NO_ENGINE
421 OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
422 engine_cleanup_int();
425 #ifndef OPENSSL_NO_DEPRECATED_3_0
426 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
427 ossl_store_cleanup_int();
430 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_lib_ctx_default_deinit()\n");
431 ossl_lib_ctx_default_deinit();
433 ossl_cleanup_thread();
435 OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
438 OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
441 OSSL_TRACE(INIT, "OPENSSL_cleanup: obj_cleanup_int()\n");
444 OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
447 OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
448 CRYPTO_secure_malloc_done();
450 #ifndef OPENSSL_NO_CMP
451 OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n");
452 OSSL_CMP_log_close();
455 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
456 ossl_trace_cleanup();
462 * If this function is called with a non NULL settings value then it must be
463 * called prior to any threads making calls to any OpenSSL functions,
464 * i.e. passing a non-null settings value is assumed to be single-threaded.
466 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
472 * We ignore failures from this function. It is probably because we are
473 * on a platform that doesn't support lockless atomic loads (we may not
474 * have created init_lock yet so we can't use it). This is just an
475 * optimisation to skip the full checks in this function if we don't need
476 * to, so we carry on regardless in the event of failure.
478 * There could be a race here with other threads, so that optsdone has not
479 * been updated yet, even though the options have in fact been initialised.
480 * This doesn't matter - it just means we will run the full function
481 * unnecessarily - but all the critical code is contained in RUN_ONCE
482 * functions anyway so we are safe.
484 if (CRYPTO_atomic_load(&optsdone, &tmp, NULL)) {
485 if ((tmp & opts) == opts)
491 * TODO(3.0): This function needs looking at with a view to moving most/all
492 * of this into OSSL_LIB_CTX.
496 if (!(opts & OPENSSL_INIT_BASE_ONLY))
497 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL);
502 * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
503 * *only* option specified. With that option we return immediately after
504 * doing the requested limited initialization. Note that
505 * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
506 * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
507 * base already initialized this is a harmless NOOP.
509 * If we remain the only caller of err_shelve_state() the recursion should
510 * perhaps be removed, but if in doubt, it can be left in place.
512 if (!RUN_ONCE(&base, ossl_init_base))
515 if (opts & OPENSSL_INIT_BASE_ONLY)
519 * init_lock should definitely be set up now, so we can now repeat the
520 * same check from above but be sure that it will work even on platforms
521 * without lockless CRYPTO_atomic_load
524 if (!CRYPTO_atomic_load(&optsdone, &tmp, init_lock))
526 if ((tmp & opts) == opts)
531 * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
532 * should not have the side-effect of setting up exit handlers, and
533 * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
536 if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
537 if (!RUN_ONCE_ALT(®ister_atexit, ossl_init_no_register_atexit,
538 ossl_init_register_atexit))
540 } else if (!RUN_ONCE(®ister_atexit, ossl_init_register_atexit)) {
544 if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
547 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
548 && !RUN_ONCE_ALT(&load_crypto_strings,
549 ossl_init_no_load_crypto_strings,
550 ossl_init_load_crypto_strings))
553 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
554 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
557 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
558 && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
559 ossl_init_add_all_ciphers))
562 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
563 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
566 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
567 && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
568 ossl_init_add_all_digests))
571 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
572 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
575 if ((opts & OPENSSL_INIT_ATFORK)
576 && !openssl_init_fork_handlers())
579 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
580 && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
583 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
586 if (settings == NULL) {
587 ret = RUN_ONCE(&config, ossl_init_config);
589 CRYPTO_THREAD_write_lock(init_lock);
590 conf_settings = settings;
591 ret = RUN_ONCE_ALT(&config, ossl_init_config_settings,
593 conf_settings = NULL;
594 CRYPTO_THREAD_unlock(init_lock);
601 if ((opts & OPENSSL_INIT_ASYNC)
602 && !RUN_ONCE(&async, ossl_init_async))
605 #ifndef OPENSSL_NO_ENGINE
606 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
607 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
609 # ifndef OPENSSL_NO_RDRAND
610 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
611 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
614 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
615 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
617 # ifndef OPENSSL_NO_STATIC_ENGINE
618 # ifndef OPENSSL_NO_DEVCRYPTOENG
619 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
620 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
623 # if !defined(OPENSSL_NO_PADLOCKENG)
624 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
625 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
628 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
629 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
630 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
633 # if !defined(OPENSSL_NO_AFALGENG)
634 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
635 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
639 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
640 | OPENSSL_INIT_ENGINE_OPENSSL
641 | OPENSSL_INIT_ENGINE_AFALG)) {
642 ENGINE_register_all_complete();
646 #ifndef OPENSSL_NO_COMP
647 if ((opts & OPENSSL_INIT_ZLIB)
648 && !RUN_ONCE(&zlib, ossl_init_zlib))
652 if (!CRYPTO_atomic_or(&optsdone, opts, &tmp, init_lock))
658 int OPENSSL_atexit(void (*handler)(void))
660 OPENSSL_INIT_STOP *newhand;
662 #if !defined(OPENSSL_USE_NODELETE)\
663 && !defined(OPENSSL_NO_PINSHARED)
670 handlersym.func = handler;
671 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
673 HMODULE handle = NULL;
677 * We don't use the DSO route for WIN32 because there is a better
680 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
681 | GET_MODULE_HANDLE_EX_FLAG_PIN,
682 handlersym.sym, &handle);
687 # elif !defined(DSO_NONE)
689 * Deliberately leak a reference to the handler. This will force the
690 * library/code containing the handler to remain loaded until we run the
691 * atexit handler. If -znodelete has been used then this is
698 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
699 /* See same code above in ossl_init_base() for an explanation. */
701 "atexit: obtained DSO reference? %s\n",
702 (dso == NULL ? "No!" : "Yes."));
710 if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
711 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
715 newhand->handler = handler;
716 newhand->next = stop_handlers;
717 stop_handlers = newhand;