2 * Copyright 2016-2018 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
11 #include "internal/cryptlib_int.h"
12 #include <openssl/err.h>
13 #include "internal/rand_int.h"
14 #include "internal/bio.h"
15 #include <openssl/evp.h>
16 #include "internal/evp_int.h"
17 #include "internal/conf.h"
18 #include "internal/async.h"
19 #include "internal/engine.h"
20 #include "internal/comp.h"
21 #include "internal/err.h"
22 #include "internal/err_int.h"
23 #include "internal/objects.h"
26 #include "internal/thread_once.h"
27 #include "internal/dso_conf.h"
28 #include "internal/dso.h"
29 #include "internal/store.h"
30 #include <openssl/trace.h>
32 static int stopped = 0;
34 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
35 struct ossl_init_stop_st {
36 void (*handler)(void);
37 OPENSSL_INIT_STOP *next;
40 static OPENSSL_INIT_STOP *stop_handlers = NULL;
41 static CRYPTO_RWLOCK *init_lock = NULL;
43 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
44 static int base_inited = 0;
45 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
47 if (ossl_trace_init() == 0)
50 OSSL_TRACE(INIT, "ossl_init_base: setting up stop handlers\n");
51 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
52 ossl_malloc_setup_failures();
55 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
57 OPENSSL_cpuid_setup();
59 if (!ossl_init_thread())
66 OSSL_TRACE(INIT, "ossl_init_base failed!\n");
67 CRYPTO_THREAD_lock_free(init_lock);
73 static CRYPTO_ONCE register_atexit = CRYPTO_ONCE_STATIC_INIT;
74 #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
75 static int win32atexit(void)
82 DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit)
84 #ifdef OPENSSL_INIT_DEBUG
85 fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n");
87 #ifndef OPENSSL_SYS_UEFI
89 /* We use _onexit() in preference because it gets called on DLL unload */
90 if (_onexit(win32atexit) == NULL)
93 if (atexit(OPENSSL_cleanup) != 0)
101 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit,
102 ossl_init_register_atexit)
104 #ifdef OPENSSL_INIT_DEBUG
105 fprintf(stderr, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n");
107 /* Do nothing in this case */
111 static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
112 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
114 OSSL_TRACE(INIT, "ossl_init_load_crypto_nodelete()\n");
116 #if !defined(OPENSSL_USE_NODELETE) \
117 && !defined(OPENSSL_NO_PINSHARED)
118 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
120 HMODULE handle = NULL;
123 /* We don't use the DSO route for WIN32 because there is a better way */
124 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
125 | GET_MODULE_HANDLE_EX_FLAG_PIN,
126 (void *)&base_inited, &handle);
129 "ossl_init_load_crypto_nodelete: "
130 "obtained DSO reference? %s\n",
131 (ret == TRUE ? "No!" : "Yes."));
132 return (ret == TRUE) ? 1 : 0;
134 # elif !defined(DSO_NONE)
136 * Deliberately leak a reference to ourselves. This will force the library
137 * to remain loaded until the atexit() handler is run at process exit.
143 if (!err_shelve_state(&err))
146 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
148 * In case of No!, it is uncertain our exit()-handlers can still be
149 * called. After dlclose() the whole library might have been unloaded
152 OSSL_TRACE1(INIT, "obtained DSO reference? %s\n",
153 (dso == NULL ? "No!" : "Yes."));
155 err_unshelve_state(err);
163 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
164 static int load_crypto_strings_inited = 0;
165 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
169 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
170 * pulling in all the error strings during static linking
172 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
173 OSSL_TRACE(INIT, "err_load_crypto_strings_int()\n");
174 ret = err_load_crypto_strings_int();
175 load_crypto_strings_inited = 1;
180 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
181 ossl_init_load_crypto_strings)
183 /* Do nothing in this case */
187 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
188 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
191 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
192 * pulling in all the ciphers during static linking
194 #ifndef OPENSSL_NO_AUTOALGINIT
195 OSSL_TRACE(INIT, "openssl_add_all_ciphers_int()\n");
196 openssl_add_all_ciphers_int();
201 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
202 ossl_init_add_all_ciphers)
208 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
209 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
212 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
213 * pulling in all the ciphers during static linking
215 #ifndef OPENSSL_NO_AUTOALGINIT
216 OSSL_TRACE(INIT, "openssl_add_all_digests()\n");
217 openssl_add_all_digests_int();
222 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
223 ossl_init_add_all_digests)
229 static CRYPTO_ONCE add_all_macs = CRYPTO_ONCE_STATIC_INIT;
230 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_macs)
233 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
234 * pulling in all the macs during static linking
236 #ifndef OPENSSL_NO_AUTOALGINIT
237 OSSL_TRACE(INIT, "openssl_add_all_macs_int()\n");
238 openssl_add_all_macs_int();
243 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_macs, ossl_init_add_all_macs)
249 static CRYPTO_ONCE add_all_kdfs = CRYPTO_ONCE_STATIC_INIT;
250 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_kdfs)
253 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
254 * pulling in all the macs during static linking
256 #ifndef OPENSSL_NO_AUTOALGINIT
257 OSSL_TRACE(INIT, "openssl_add_all_kdfs_int()\n");
258 openssl_add_all_kdfs_int();
263 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_kdfs, ossl_init_add_all_kdfs)
269 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
270 static int config_inited = 0;
271 static const OPENSSL_INIT_SETTINGS *conf_settings = NULL;
272 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
274 int ret = openssl_config_int(conf_settings);
278 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
280 OSSL_TRACE(INIT, "openssl_no_config_int()\n");
281 openssl_no_config_int();
286 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
287 static int async_inited = 0;
288 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
290 OSSL_TRACE(INIT, "async_init()\n");
297 #ifndef OPENSSL_NO_ENGINE
298 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
299 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
301 OSSL_TRACE(INIT, "engine_load_openssl_int()\n");
302 engine_load_openssl_int();
305 # ifndef OPENSSL_NO_RDRAND
306 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
307 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
309 OSSL_TRACE(INIT, "engine_load_rdrand_int()\n");
310 engine_load_rdrand_int();
314 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
315 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
317 OSSL_TRACE(INIT, "engine_load_dynamic_int()\n");
318 engine_load_dynamic_int();
321 # ifndef OPENSSL_NO_STATIC_ENGINE
322 # ifndef OPENSSL_NO_DEVCRYPTOENG
323 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
324 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
326 OSSL_TRACE(INIT, "engine_load_devcrypto_int()\n");
327 engine_load_devcrypto_int();
331 # if !defined(OPENSSL_NO_PADLOCKENG)
332 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
333 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
335 OSSL_TRACE(INIT, "engine_load_padlock_int()\n");
336 engine_load_padlock_int();
340 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
341 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
342 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
344 OSSL_TRACE(INIT, "engine_load_capi_int()\n");
345 engine_load_capi_int();
349 # if !defined(OPENSSL_NO_AFALGENG)
350 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
351 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
353 OSSL_TRACE(INIT, "engine_load_afalg_int()\n");
354 engine_load_afalg_int();
361 #ifndef OPENSSL_NO_COMP
362 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
364 static int zlib_inited = 0;
365 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
367 /* Do nothing - we need to know about this for the later cleanup */
373 void OPENSSL_cleanup(void)
375 OPENSSL_INIT_STOP *currhandler, *lasthandler;
378 * TODO(3.0): This function needs looking at with a view to moving most/all
379 * of this into onfree handlers in OPENSSL_CTX.
382 /* If we've not been inited then no need to deinit */
386 /* Might be explicitly called and also by atexit */
392 * Thread stop may not get automatically called by the thread library for
393 * the very last thread in some situations, so call it directly.
395 OPENSSL_thread_stop();
397 currhandler = stop_handlers;
398 while (currhandler != NULL) {
399 currhandler->handler();
400 lasthandler = currhandler;
401 currhandler = currhandler->next;
402 OPENSSL_free(lasthandler);
404 stop_handlers = NULL;
406 CRYPTO_THREAD_lock_free(init_lock);
410 * We assume we are single-threaded for this function, i.e. no race
411 * conditions for the various "*_inited" vars below.
414 #ifndef OPENSSL_NO_COMP
416 OSSL_TRACE(INIT, "OPENSSL_cleanup: comp_zlib_cleanup_int()\n");
417 comp_zlib_cleanup_int();
422 OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
426 if (load_crypto_strings_inited) {
427 OSSL_TRACE(INIT, "OPENSSL_cleanup: err_free_strings_int()\n");
428 err_free_strings_int();
432 * Note that cleanup order is important:
433 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
434 * must be called before engine_cleanup_int()
435 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
436 * before the ex data handlers are wiped during default openssl_ctx deinit.
437 * - conf_modules_free_int() can end up in ENGINE code so must be called
438 * before engine_cleanup_int()
439 * - ENGINEs and additional EVP algorithms might use added OIDs names so
440 * obj_cleanup_int() must be called last
442 OSSL_TRACE(INIT, "OPENSSL_cleanup: rand_cleanup_int()\n");
445 OSSL_TRACE(INIT, "OPENSSL_cleanup: conf_modules_free_int()\n");
446 conf_modules_free_int();
448 #ifndef OPENSSL_NO_ENGINE
449 OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
450 engine_cleanup_int();
452 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
453 ossl_store_cleanup_int();
455 OSSL_TRACE(INIT, "OPENSSL_cleanup: openssl_ctx_default_deinit()\n");
456 openssl_ctx_default_deinit();
458 ossl_cleanup_thread();
460 OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
463 OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
466 OSSL_TRACE(INIT, "OPENSSL_cleanup: obj_cleanup_int()\n");
469 OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
472 OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
473 CRYPTO_secure_malloc_done();
475 OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
476 ossl_trace_cleanup();
482 * If this function is called with a non NULL settings value then it must be
483 * called prior to any threads making calls to any OpenSSL functions,
484 * i.e. passing a non-null settings value is assumed to be single-threaded.
486 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
489 * TODO(3.0): This function needs looking at with a view to moving most/all
490 * of this into OPENSSL_CTX.
494 if (!(opts & OPENSSL_INIT_BASE_ONLY))
495 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
500 * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
501 * *only* option specified. With that option we return immediately after
502 * doing the requested limited initialization. Note that
503 * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
504 * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
505 * base already initialized this is a harmless NOOP.
507 * If we remain the only caller of err_shelve_state() the recursion should
508 * perhaps be removed, but if in doubt, it can be left in place.
510 if (!RUN_ONCE(&base, ossl_init_base))
513 if (opts & OPENSSL_INIT_BASE_ONLY)
517 * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
518 * should not have the side-effect of setting up exit handlers, and
519 * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
522 if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
523 if (!RUN_ONCE_ALT(®ister_atexit, ossl_init_no_register_atexit,
524 ossl_init_register_atexit))
526 } else if (!RUN_ONCE(®ister_atexit, ossl_init_register_atexit)) {
530 if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
533 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
534 && !RUN_ONCE_ALT(&load_crypto_strings,
535 ossl_init_no_load_crypto_strings,
536 ossl_init_load_crypto_strings))
539 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
540 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
543 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
544 && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
545 ossl_init_add_all_ciphers))
548 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
549 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
552 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
553 && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
554 ossl_init_add_all_digests))
557 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
558 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
561 if ((opts & OPENSSL_INIT_NO_ADD_ALL_MACS)
562 && !RUN_ONCE_ALT(&add_all_macs, ossl_init_no_add_all_macs,
563 ossl_init_add_all_macs))
566 if ((opts & OPENSSL_INIT_ADD_ALL_MACS)
567 && !RUN_ONCE(&add_all_macs, ossl_init_add_all_macs))
570 if ((opts & OPENSSL_INIT_NO_ADD_ALL_KDFS)
571 && !RUN_ONCE_ALT(&add_all_kdfs, ossl_init_no_add_all_kdfs,
572 ossl_init_add_all_kdfs))
575 if ((opts & OPENSSL_INIT_ADD_ALL_KDFS)
576 && !RUN_ONCE(&add_all_kdfs, ossl_init_add_all_kdfs))
579 if ((opts & OPENSSL_INIT_ATFORK)
580 && !openssl_init_fork_handlers())
583 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
584 && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
587 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
589 CRYPTO_THREAD_write_lock(init_lock);
590 conf_settings = settings;
591 ret = RUN_ONCE(&config, ossl_init_config);
592 conf_settings = NULL;
593 CRYPTO_THREAD_unlock(init_lock);
598 if ((opts & OPENSSL_INIT_ASYNC)
599 && !RUN_ONCE(&async, ossl_init_async))
602 #ifndef OPENSSL_NO_ENGINE
603 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
604 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
606 # ifndef OPENSSL_NO_RDRAND
607 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
608 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
611 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
612 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
614 # ifndef OPENSSL_NO_STATIC_ENGINE
615 # ifndef OPENSSL_NO_DEVCRYPTOENG
616 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
617 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
620 # if !defined(OPENSSL_NO_PADLOCKENG)
621 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
622 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
625 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
626 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
627 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
630 # if !defined(OPENSSL_NO_AFALGENG)
631 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
632 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
636 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
637 | OPENSSL_INIT_ENGINE_OPENSSL
638 | OPENSSL_INIT_ENGINE_AFALG)) {
639 ENGINE_register_all_complete();
643 #ifndef OPENSSL_NO_COMP
644 if ((opts & OPENSSL_INIT_ZLIB)
645 && !RUN_ONCE(&zlib, ossl_init_zlib))
652 int OPENSSL_atexit(void (*handler)(void))
654 OPENSSL_INIT_STOP *newhand;
656 #if !defined(OPENSSL_USE_NODELETE)\
657 && !defined(OPENSSL_NO_PINSHARED)
664 handlersym.func = handler;
665 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
667 HMODULE handle = NULL;
671 * We don't use the DSO route for WIN32 because there is a better
674 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
675 | GET_MODULE_HANDLE_EX_FLAG_PIN,
676 handlersym.sym, &handle);
681 # elif !defined(DSO_NONE)
683 * Deliberately leak a reference to the handler. This will force the
684 * library/code containing the handler to remain loaded until we run the
685 * atexit handler. If -znodelete has been used then this is
692 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
693 /* See same code above in ossl_init_base() for an explanation. */
695 "atexit: obtained DSO reference? %s\n",
696 (dso == NULL ? "No!" : "Yes."));
704 if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
705 CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
709 newhand->handler = handler;
710 newhand->next = stop_handlers;
711 stop_handlers = newhand;
716 #ifdef OPENSSL_SYS_UNIX
718 * The following three functions are for OpenSSL developers. This is
719 * where we set/reset state across fork (called via pthread_atfork when
720 * it exists, or manually by the application when it doesn't).
722 * WARNING! If you put code in either OPENSSL_fork_parent or
723 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
724 * safe. See this link, for example:
725 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
728 void OPENSSL_fork_prepare(void)
732 void OPENSSL_fork_parent(void)
736 void OPENSSL_fork_child(void)
739 /* TODO(3.0): Inform all providers about a fork event */