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.h"
28 #include "internal/store.h"
30 static int stopped = 0;
32 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
34 static CRYPTO_THREAD_LOCAL threadstopkey;
36 static void ossl_init_thread_stop_wrap(void *local)
38 ossl_init_thread_stop((struct thread_local_inits_st *)local);
41 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
43 struct thread_local_inits_st *local =
44 CRYPTO_THREAD_get_local(&threadstopkey);
46 if (local == NULL && alloc) {
47 local = OPENSSL_zalloc(sizeof(*local));
48 if (local != NULL && !CRYPTO_THREAD_set_local(&threadstopkey, local)) {
54 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
60 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
61 struct ossl_init_stop_st {
62 void (*handler)(void);
63 OPENSSL_INIT_STOP *next;
66 static OPENSSL_INIT_STOP *stop_handlers = NULL;
67 static CRYPTO_RWLOCK *init_lock = NULL;
69 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
70 static int base_inited = 0;
71 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
73 #ifdef OPENSSL_INIT_DEBUG
74 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
76 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
77 ossl_malloc_setup_failures();
80 * We use a dummy thread local key here. We use the destructor to detect
81 * when the thread is going to stop (where that feature is available)
83 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
84 #ifndef OPENSSL_SYS_UEFI
85 atexit(OPENSSL_cleanup);
87 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
89 OPENSSL_cpuid_setup();
93 * Everything needed to be initialized in this function before threads
94 * come along MUST happen before base_inited is set to 1, or we will
95 * see race conditions.
99 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
102 HMODULE handle = NULL;
105 /* We don't use the DSO route for WIN32 because there is a better way */
106 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
107 | GET_MODULE_HANDLE_EX_FLAG_PIN,
108 (void *)&base_inited, &handle);
110 return (ret == TRUE) ? 1 : 0;
114 * Deliberately leak a reference to ourselves. This will force the library
115 * to remain loaded until the atexit() handler is run at process exit.
121 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
131 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
132 static int load_crypto_strings_inited = 0;
133 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
135 /* Do nothing in this case */
139 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
143 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
144 * pulling in all the error strings during static linking
146 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
147 # ifdef OPENSSL_INIT_DEBUG
148 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
149 "err_load_crypto_strings_int()\n");
151 ret = err_load_crypto_strings_int();
152 load_crypto_strings_inited = 1;
157 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
158 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
161 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
162 * pulling in all the ciphers during static linking
164 #ifndef OPENSSL_NO_AUTOALGINIT
165 # ifdef OPENSSL_INIT_DEBUG
166 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
167 "openssl_add_all_ciphers_int()\n");
169 openssl_add_all_ciphers_int();
174 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
175 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
178 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
179 * pulling in all the ciphers during static linking
181 #ifndef OPENSSL_NO_AUTOALGINIT
182 # ifdef OPENSSL_INIT_DEBUG
183 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
184 "openssl_add_all_digests()\n");
186 openssl_add_all_digests_int();
191 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
197 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
198 static int config_inited = 0;
199 static const char *appname;
200 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
202 #ifdef OPENSSL_INIT_DEBUG
204 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
205 appname == NULL ? "NULL" : appname);
207 openssl_config_int(appname);
211 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
213 #ifdef OPENSSL_INIT_DEBUG
215 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
217 openssl_no_config_int();
222 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
223 static int async_inited = 0;
224 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
226 #ifdef OPENSSL_INIT_DEBUG
227 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
235 #ifndef OPENSSL_NO_ENGINE
236 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
237 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
239 # ifdef OPENSSL_INIT_DEBUG
240 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
241 "engine_load_openssl_int()\n");
243 engine_load_openssl_int();
246 # ifndef OPENSSL_NO_DEVCRYPTOENG
247 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
248 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
250 # ifdef OPENSSL_INIT_DEBUG
251 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
252 "engine_load_devcrypto_int()\n");
254 engine_load_devcrypto_int();
259 # ifndef OPENSSL_NO_RDRAND
260 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
261 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
263 # ifdef OPENSSL_INIT_DEBUG
264 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
265 "engine_load_rdrand_int()\n");
267 engine_load_rdrand_int();
271 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
272 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
274 # ifdef OPENSSL_INIT_DEBUG
275 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
276 "engine_load_dynamic_int()\n");
278 engine_load_dynamic_int();
281 # ifndef OPENSSL_NO_STATIC_ENGINE
282 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
283 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
284 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
286 # ifdef OPENSSL_INIT_DEBUG
287 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
288 "engine_load_padlock_int()\n");
290 engine_load_padlock_int();
294 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
295 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
296 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
298 # ifdef OPENSSL_INIT_DEBUG
299 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
300 "engine_load_capi_int()\n");
302 engine_load_capi_int();
306 # if !defined(OPENSSL_NO_AFALGENG)
307 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
308 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
310 # ifdef OPENSSL_INIT_DEBUG
311 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
312 "engine_load_afalg_int()\n");
314 engine_load_afalg_int();
321 #ifndef OPENSSL_NO_COMP
322 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
324 static int zlib_inited = 0;
325 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
327 /* Do nothing - we need to know about this for the later cleanup */
333 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
335 /* Can't do much about this */
340 #ifdef OPENSSL_INIT_DEBUG
341 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
342 "ASYNC_cleanup_thread()\n");
344 ASYNC_cleanup_thread();
347 if (locals->err_state) {
348 #ifdef OPENSSL_INIT_DEBUG
349 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
350 "err_delete_thread_state()\n");
352 err_delete_thread_state();
356 #ifdef OPENSSL_INIT_DEBUG
357 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
358 "drbg_delete_thread_state()\n");
360 drbg_delete_thread_state();
363 OPENSSL_free(locals);
366 void OPENSSL_thread_stop(void)
368 ossl_init_thread_stop(
369 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
372 int ossl_init_thread_start(uint64_t opts)
374 struct thread_local_inits_st *locals;
376 if (!OPENSSL_init_crypto(0, NULL))
379 locals = ossl_init_get_thread_local(1);
384 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
385 #ifdef OPENSSL_INIT_DEBUG
386 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
387 "marking thread for async\n");
392 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
393 #ifdef OPENSSL_INIT_DEBUG
394 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
395 "marking thread for err_state\n");
397 locals->err_state = 1;
400 if (opts & OPENSSL_INIT_THREAD_RAND) {
401 #ifdef OPENSSL_INIT_DEBUG
402 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
403 "marking thread for rand\n");
411 void OPENSSL_cleanup(void)
413 OPENSSL_INIT_STOP *currhandler, *lasthandler;
415 /* If we've not been inited then no need to deinit */
419 /* Might be explicitly called and also by atexit */
425 * Thread stop may not get automatically called by the thread library for
426 * the very last thread in some situations, so call it directly.
428 ossl_init_thread_stop(ossl_init_get_thread_local(0));
430 currhandler = stop_handlers;
431 while (currhandler != NULL) {
432 currhandler->handler();
433 lasthandler = currhandler;
434 currhandler = currhandler->next;
435 OPENSSL_free(lasthandler);
437 stop_handlers = NULL;
439 CRYPTO_THREAD_lock_free(init_lock);
443 * We assume we are single-threaded for this function, i.e. no race
444 * conditions for the various "*_inited" vars below.
447 #ifndef OPENSSL_NO_COMP
449 #ifdef OPENSSL_INIT_DEBUG
450 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
451 "comp_zlib_cleanup_int()\n");
453 comp_zlib_cleanup_int();
458 # ifdef OPENSSL_INIT_DEBUG
459 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
465 if (load_crypto_strings_inited) {
466 #ifdef OPENSSL_INIT_DEBUG
467 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
468 "err_free_strings_int()\n");
470 err_free_strings_int();
473 CRYPTO_THREAD_cleanup_local(&threadstopkey);
475 #ifdef OPENSSL_INIT_DEBUG
476 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
477 "rand_cleanup_int()\n");
478 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
479 "conf_modules_free_int()\n");
480 #ifndef OPENSSL_NO_ENGINE
481 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
482 "engine_cleanup_int()\n");
484 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
485 "crypto_cleanup_all_ex_data_int()\n");
486 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
487 "bio_sock_cleanup_int()\n");
488 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
490 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
491 "evp_cleanup_int()\n");
492 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
493 "obj_cleanup_int()\n");
494 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
498 * Note that cleanup order is important:
499 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
500 * must be called before engine_cleanup_int()
501 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
502 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
503 * - conf_modules_free_int() can end up in ENGINE code so must be called
504 * before engine_cleanup_int()
505 * - ENGINEs and additional EVP algorithms might use added OIDs names so
506 * obj_cleanup_int() must be called last
509 rand_drbg_cleanup_int();
510 conf_modules_free_int();
511 #ifndef OPENSSL_NO_ENGINE
512 engine_cleanup_int();
514 ossl_store_cleanup_int();
515 crypto_cleanup_all_ex_data_int();
521 CRYPTO_secure_malloc_done();
527 * If this function is called with a non NULL settings value then it must be
528 * called prior to any threads making calls to any OpenSSL functions,
529 * i.e. passing a non-null settings value is assumed to be single-threaded.
531 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
533 static int stoperrset = 0;
538 * We only ever set this once to avoid getting into an infinite
539 * loop where the error system keeps trying to init and fails so
543 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
548 if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
551 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
552 && !RUN_ONCE(&load_crypto_strings,
553 ossl_init_no_load_crypto_strings))
556 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
557 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
560 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
561 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
564 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
565 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
568 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
569 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
572 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
573 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
576 if ((opts & OPENSSL_INIT_ATFORK)
577 && !openssl_init_fork_handlers())
580 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
581 && !RUN_ONCE(&config, ossl_init_no_config))
584 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
586 CRYPTO_THREAD_write_lock(init_lock);
587 appname = (settings == NULL) ? NULL : settings->appname;
588 ret = RUN_ONCE(&config, ossl_init_config);
589 CRYPTO_THREAD_unlock(init_lock);
594 if ((opts & OPENSSL_INIT_ASYNC)
595 && !RUN_ONCE(&async, ossl_init_async))
598 #ifndef OPENSSL_NO_ENGINE
599 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
600 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
602 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
603 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
604 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
607 # ifndef OPENSSL_NO_RDRAND
608 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
609 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
612 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
613 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
615 # ifndef OPENSSL_NO_STATIC_ENGINE
616 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
617 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
618 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
621 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
622 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
623 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
626 # if !defined(OPENSSL_NO_AFALGENG)
627 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
628 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
632 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
633 | OPENSSL_INIT_ENGINE_OPENSSL
634 | OPENSSL_INIT_ENGINE_AFALG)) {
635 ENGINE_register_all_complete();
639 #ifndef OPENSSL_NO_COMP
640 if ((opts & OPENSSL_INIT_ZLIB)
641 && !RUN_ONCE(&zlib, ossl_init_zlib))
648 int OPENSSL_atexit(void (*handler)(void))
650 OPENSSL_INIT_STOP *newhand;
652 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
659 handlersym.func = handler;
662 HMODULE handle = NULL;
666 * We don't use the DSO route for WIN32 because there is a better
669 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
670 | GET_MODULE_HANDLE_EX_FLAG_PIN,
671 handlersym.sym, &handle);
678 * Deliberately leak a reference to the handler. This will force the
679 * library/code containing the handler to remain loaded until we run the
680 * atexit handler. If -znodelete has been used then this is
687 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
695 newhand = OPENSSL_malloc(sizeof(*newhand));
699 newhand->handler = handler;
700 newhand->next = stop_handlers;
701 stop_handlers = newhand;
706 #ifdef OPENSSL_SYS_UNIX
708 * The following three functions are for OpenSSL developers. This is
709 * where we set/reset state across fork (called via pthread_atfork when
710 * it exists, or manually by the application when it doesn't).
712 * WARNING! If you put code in either OPENSSL_fork_parent or
713 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
714 * safe. See this link, for example:
715 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
718 void OPENSSL_fork_prepare(void)
722 void OPENSSL_fork_parent(void)
726 void OPENSSL_fork_child(void)