2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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"
31 static int stopped = 0;
33 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
35 static CRYPTO_THREAD_LOCAL threadstopkey;
37 static void ossl_init_thread_stop_wrap(void *local)
39 ossl_init_thread_stop((struct thread_local_inits_st *)local);
42 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
44 struct thread_local_inits_st *local =
45 CRYPTO_THREAD_get_local(&threadstopkey);
47 if (local == NULL && alloc) {
48 local = OPENSSL_zalloc(sizeof(*local));
49 if (local != NULL && !CRYPTO_THREAD_set_local(&threadstopkey, local)) {
55 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
61 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
62 struct ossl_init_stop_st {
63 void (*handler)(void);
64 OPENSSL_INIT_STOP *next;
67 static OPENSSL_INIT_STOP *stop_handlers = NULL;
68 static CRYPTO_RWLOCK *init_lock = NULL;
70 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
71 static int base_inited = 0;
72 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
74 #ifdef OPENSSL_INIT_DEBUG
75 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
77 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
78 ossl_malloc_setup_failures();
81 * We use a dummy thread local key here. We use the destructor to detect
82 * when the thread is going to stop (where that feature is available)
84 if (!CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap))
86 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
88 #ifndef OPENSSL_SYS_UEFI
89 if (atexit(OPENSSL_cleanup) != 0)
92 OPENSSL_cpuid_setup();
98 #ifdef OPENSSL_INIT_DEBUG
99 fprintf(stderr, "OPENSSL_INIT: ossl_init_base not ok!\n");
101 CRYPTO_THREAD_lock_free(init_lock);
104 CRYPTO_THREAD_cleanup_local(&threadstopkey);
108 static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
109 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
111 #ifdef OPENSSL_INIT_DEBUG
112 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_nodelete()\n");
114 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
117 HMODULE handle = NULL;
120 /* We don't use the DSO route for WIN32 because there is a better way */
121 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
122 | GET_MODULE_HANDLE_EX_FLAG_PIN,
123 (void *)&base_inited, &handle);
125 # ifdef OPENSSL_INIT_DEBUG
126 fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
127 (ret == TRUE ? "No!" : "Yes."));
129 return (ret == TRUE) ? 1 : 0;
133 * Deliberately leak a reference to ourselves. This will force the library
134 * to remain loaded until the atexit() handler is run at process exit.
140 if (!err_shelve_state(&err))
143 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
144 # ifdef OPENSSL_INIT_DEBUG
145 fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
146 (dso == NULL ? "No!" : "Yes."));
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
154 err_unshelve_state(err);
162 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
163 static int load_crypto_strings_inited = 0;
164 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
166 /* Do nothing in this case */
170 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
174 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
175 * pulling in all the error strings during static linking
177 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
178 # ifdef OPENSSL_INIT_DEBUG
179 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
180 "err_load_crypto_strings_int()\n");
182 ret = err_load_crypto_strings_int();
183 load_crypto_strings_inited = 1;
188 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
189 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
192 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
193 * pulling in all the ciphers during static linking
195 #ifndef OPENSSL_NO_AUTOALGINIT
196 # ifdef OPENSSL_INIT_DEBUG
197 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
198 "openssl_add_all_ciphers_int()\n");
200 openssl_add_all_ciphers_int();
205 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
206 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
209 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
210 * pulling in all the ciphers during static linking
212 #ifndef OPENSSL_NO_AUTOALGINIT
213 # ifdef OPENSSL_INIT_DEBUG
214 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
215 "openssl_add_all_digests()\n");
217 openssl_add_all_digests_int();
222 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
228 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
229 static int config_inited = 0;
230 static const char *appname;
231 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
233 #ifdef OPENSSL_INIT_DEBUG
235 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
236 appname == NULL ? "NULL" : appname);
238 openssl_config_int(appname);
242 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
244 #ifdef OPENSSL_INIT_DEBUG
246 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
248 openssl_no_config_int();
253 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
254 static int async_inited = 0;
255 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
257 #ifdef OPENSSL_INIT_DEBUG
258 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
266 #ifndef OPENSSL_NO_ENGINE
267 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
268 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
270 # ifdef OPENSSL_INIT_DEBUG
271 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
272 "engine_load_openssl_int()\n");
274 engine_load_openssl_int();
277 # ifndef OPENSSL_NO_DEVCRYPTOENG
278 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
279 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
281 # ifdef OPENSSL_INIT_DEBUG
282 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
283 "engine_load_devcrypto_int()\n");
285 engine_load_devcrypto_int();
290 # ifndef OPENSSL_NO_RDRAND
291 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
292 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
294 # ifdef OPENSSL_INIT_DEBUG
295 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
296 "engine_load_rdrand_int()\n");
298 engine_load_rdrand_int();
302 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
303 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
305 # ifdef OPENSSL_INIT_DEBUG
306 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
307 "engine_load_dynamic_int()\n");
309 engine_load_dynamic_int();
312 # ifndef OPENSSL_NO_STATIC_ENGINE
313 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
314 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
315 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
317 # ifdef OPENSSL_INIT_DEBUG
318 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
319 "engine_load_padlock_int()\n");
321 engine_load_padlock_int();
325 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
326 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
327 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
329 # ifdef OPENSSL_INIT_DEBUG
330 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
331 "engine_load_capi_int()\n");
333 engine_load_capi_int();
337 # if !defined(OPENSSL_NO_AFALGENG)
338 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
339 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
341 # ifdef OPENSSL_INIT_DEBUG
342 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
343 "engine_load_afalg_int()\n");
345 engine_load_afalg_int();
352 #ifndef OPENSSL_NO_COMP
353 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
355 static int zlib_inited = 0;
356 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
358 /* Do nothing - we need to know about this for the later cleanup */
364 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
366 /* Can't do much about this */
371 #ifdef OPENSSL_INIT_DEBUG
372 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
373 "async_delete_thread_state()\n");
375 async_delete_thread_state();
378 if (locals->err_state) {
379 #ifdef OPENSSL_INIT_DEBUG
380 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
381 "err_delete_thread_state()\n");
383 err_delete_thread_state();
387 #ifdef OPENSSL_INIT_DEBUG
388 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
389 "drbg_delete_thread_state()\n");
391 drbg_delete_thread_state();
394 OPENSSL_free(locals);
397 void OPENSSL_thread_stop(void)
399 ossl_init_thread_stop(
400 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
403 int ossl_init_thread_start(uint64_t opts)
405 struct thread_local_inits_st *locals;
407 if (!OPENSSL_init_crypto(0, NULL))
410 locals = ossl_init_get_thread_local(1);
415 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
416 #ifdef OPENSSL_INIT_DEBUG
417 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
418 "marking thread for async\n");
423 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
424 #ifdef OPENSSL_INIT_DEBUG
425 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
426 "marking thread for err_state\n");
428 locals->err_state = 1;
431 if (opts & OPENSSL_INIT_THREAD_RAND) {
432 #ifdef OPENSSL_INIT_DEBUG
433 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
434 "marking thread for rand\n");
442 void OPENSSL_cleanup(void)
444 OPENSSL_INIT_STOP *currhandler, *lasthandler;
446 /* If we've not been inited then no need to deinit */
450 /* Might be explicitly called and also by atexit */
456 * Thread stop may not get automatically called by the thread library for
457 * the very last thread in some situations, so call it directly.
459 ossl_init_thread_stop(ossl_init_get_thread_local(0));
461 currhandler = stop_handlers;
462 while (currhandler != NULL) {
463 currhandler->handler();
464 lasthandler = currhandler;
465 currhandler = currhandler->next;
466 OPENSSL_free(lasthandler);
468 stop_handlers = NULL;
470 CRYPTO_THREAD_lock_free(init_lock);
474 * We assume we are single-threaded for this function, i.e. no race
475 * conditions for the various "*_inited" vars below.
478 #ifndef OPENSSL_NO_COMP
480 #ifdef OPENSSL_INIT_DEBUG
481 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
482 "comp_zlib_cleanup_int()\n");
484 comp_zlib_cleanup_int();
489 # ifdef OPENSSL_INIT_DEBUG
490 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
496 if (load_crypto_strings_inited) {
497 #ifdef OPENSSL_INIT_DEBUG
498 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
499 "err_free_strings_int()\n");
501 err_free_strings_int();
504 CRYPTO_THREAD_cleanup_local(&threadstopkey);
506 #ifdef OPENSSL_INIT_DEBUG
507 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
508 "rand_cleanup_int()\n");
509 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
510 "conf_modules_free_int()\n");
511 #ifndef OPENSSL_NO_ENGINE
512 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
513 "engine_cleanup_int()\n");
515 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
516 "crypto_cleanup_all_ex_data_int()\n");
517 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
518 "bio_sock_cleanup_int()\n");
519 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
521 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
522 "evp_cleanup_int()\n");
523 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
524 "obj_cleanup_int()\n");
525 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
529 * Note that cleanup order is important:
530 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
531 * must be called before engine_cleanup_int()
532 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
533 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
534 * - conf_modules_free_int() can end up in ENGINE code so must be called
535 * before engine_cleanup_int()
536 * - ENGINEs and additional EVP algorithms might use added OIDs names so
537 * obj_cleanup_int() must be called last
540 rand_drbg_cleanup_int();
541 conf_modules_free_int();
542 #ifndef OPENSSL_NO_ENGINE
543 engine_cleanup_int();
545 ossl_store_cleanup_int();
546 crypto_cleanup_all_ex_data_int();
552 CRYPTO_secure_malloc_done();
558 * If this function is called with a non NULL settings value then it must be
559 * called prior to any threads making calls to any OpenSSL functions,
560 * i.e. passing a non-null settings value is assumed to be single-threaded.
562 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
565 if (!(opts & OPENSSL_INIT_BASE_ONLY))
566 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
570 if (!RUN_ONCE(&base, ossl_init_base))
573 if (!(opts & OPENSSL_INIT_BASE_ONLY)
574 && !RUN_ONCE(&load_crypto_nodelete,
575 ossl_init_load_crypto_nodelete))
578 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
579 && !RUN_ONCE(&load_crypto_strings,
580 ossl_init_no_load_crypto_strings))
583 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
584 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
587 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
588 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
591 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
592 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
595 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
596 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
599 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
600 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
603 if ((opts & OPENSSL_INIT_ATFORK)
604 && !openssl_init_fork_handlers())
607 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
608 && !RUN_ONCE(&config, ossl_init_no_config))
611 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
613 CRYPTO_THREAD_write_lock(init_lock);
614 appname = (settings == NULL) ? NULL : settings->appname;
615 ret = RUN_ONCE(&config, ossl_init_config);
616 CRYPTO_THREAD_unlock(init_lock);
621 if ((opts & OPENSSL_INIT_ASYNC)
622 && !RUN_ONCE(&async, ossl_init_async))
625 #ifndef OPENSSL_NO_ENGINE
626 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
627 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
629 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
630 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
631 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
634 # ifndef OPENSSL_NO_RDRAND
635 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
636 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
639 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
640 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
642 # ifndef OPENSSL_NO_STATIC_ENGINE
643 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
644 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
645 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
648 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
649 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
650 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
653 # if !defined(OPENSSL_NO_AFALGENG)
654 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
655 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
659 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
660 | OPENSSL_INIT_ENGINE_OPENSSL
661 | OPENSSL_INIT_ENGINE_AFALG)) {
662 ENGINE_register_all_complete();
666 #ifndef OPENSSL_NO_COMP
667 if ((opts & OPENSSL_INIT_ZLIB)
668 && !RUN_ONCE(&zlib, ossl_init_zlib))
675 int OPENSSL_atexit(void (*handler)(void))
677 OPENSSL_INIT_STOP *newhand;
679 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
686 handlersym.func = handler;
689 HMODULE handle = NULL;
693 * We don't use the DSO route for WIN32 because there is a better
696 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
697 | GET_MODULE_HANDLE_EX_FLAG_PIN,
698 handlersym.sym, &handle);
705 * Deliberately leak a reference to the handler. This will force the
706 * library/code containing the handler to remain loaded until we run the
707 * atexit handler. If -znodelete has been used then this is
714 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
715 # ifdef OPENSSL_INIT_DEBUG
717 "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
718 (dso == NULL ? "No!" : "Yes."));
719 /* See same code above in ossl_init_base() for an explanation. */
728 if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
729 CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
733 newhand->handler = handler;
734 newhand->next = stop_handlers;
735 stop_handlers = newhand;
740 #ifdef OPENSSL_SYS_UNIX
742 * The following three functions are for OpenSSL developers. This is
743 * where we set/reset state across fork (called via pthread_atfork when
744 * it exists, or manually by the application when it doesn't).
746 * WARNING! If you put code in either OPENSSL_fork_parent or
747 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
748 * safe. See this link, for example:
749 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
752 void OPENSSL_fork_prepare(void)
756 void OPENSSL_fork_parent(void)
760 void OPENSSL_fork_child(void)