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);
122 # ifdef OPENSSL_INIT_DEBUG
123 fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
124 (dso == NULL ? "No!" : "Yes."));
126 * In case of No!, it is uncertain our exit()-handlers can still be
127 * called. After dlclose() the whole library might have been unloaded
140 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
141 static int load_crypto_strings_inited = 0;
142 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
144 /* Do nothing in this case */
148 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
152 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
153 * pulling in all the error strings during static linking
155 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
156 # ifdef OPENSSL_INIT_DEBUG
157 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
158 "err_load_crypto_strings_int()\n");
160 ret = err_load_crypto_strings_int();
161 load_crypto_strings_inited = 1;
166 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
167 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
170 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
171 * pulling in all the ciphers during static linking
173 #ifndef OPENSSL_NO_AUTOALGINIT
174 # ifdef OPENSSL_INIT_DEBUG
175 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
176 "openssl_add_all_ciphers_int()\n");
178 openssl_add_all_ciphers_int();
183 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
184 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
187 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
188 * pulling in all the ciphers during static linking
190 #ifndef OPENSSL_NO_AUTOALGINIT
191 # ifdef OPENSSL_INIT_DEBUG
192 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
193 "openssl_add_all_digests()\n");
195 openssl_add_all_digests_int();
200 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
206 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
207 static int config_inited = 0;
208 static const char *appname;
209 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
211 #ifdef OPENSSL_INIT_DEBUG
213 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
214 appname == NULL ? "NULL" : appname);
216 openssl_config_int(appname);
220 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
222 #ifdef OPENSSL_INIT_DEBUG
224 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
226 openssl_no_config_int();
231 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
232 static int async_inited = 0;
233 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
235 #ifdef OPENSSL_INIT_DEBUG
236 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
244 #ifndef OPENSSL_NO_ENGINE
245 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
246 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
248 # ifdef OPENSSL_INIT_DEBUG
249 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
250 "engine_load_openssl_int()\n");
252 engine_load_openssl_int();
255 # ifndef OPENSSL_NO_DEVCRYPTOENG
256 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
257 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
259 # ifdef OPENSSL_INIT_DEBUG
260 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
261 "engine_load_devcrypto_int()\n");
263 engine_load_devcrypto_int();
268 # ifndef OPENSSL_NO_RDRAND
269 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
270 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
272 # ifdef OPENSSL_INIT_DEBUG
273 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
274 "engine_load_rdrand_int()\n");
276 engine_load_rdrand_int();
280 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
281 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
283 # ifdef OPENSSL_INIT_DEBUG
284 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
285 "engine_load_dynamic_int()\n");
287 engine_load_dynamic_int();
290 # ifndef OPENSSL_NO_STATIC_ENGINE
291 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
292 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
293 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
295 # ifdef OPENSSL_INIT_DEBUG
296 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
297 "engine_load_padlock_int()\n");
299 engine_load_padlock_int();
303 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
304 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
305 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
307 # ifdef OPENSSL_INIT_DEBUG
308 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
309 "engine_load_capi_int()\n");
311 engine_load_capi_int();
315 # if !defined(OPENSSL_NO_AFALGENG)
316 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
317 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
319 # ifdef OPENSSL_INIT_DEBUG
320 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
321 "engine_load_afalg_int()\n");
323 engine_load_afalg_int();
330 #ifndef OPENSSL_NO_COMP
331 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
333 static int zlib_inited = 0;
334 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
336 /* Do nothing - we need to know about this for the later cleanup */
342 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
344 /* Can't do much about this */
349 #ifdef OPENSSL_INIT_DEBUG
350 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
351 "ASYNC_cleanup_thread()\n");
353 ASYNC_cleanup_thread();
356 if (locals->err_state) {
357 #ifdef OPENSSL_INIT_DEBUG
358 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
359 "err_delete_thread_state()\n");
361 err_delete_thread_state();
365 #ifdef OPENSSL_INIT_DEBUG
366 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
367 "drbg_delete_thread_state()\n");
369 drbg_delete_thread_state();
372 OPENSSL_free(locals);
375 void OPENSSL_thread_stop(void)
377 ossl_init_thread_stop(
378 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
381 int ossl_init_thread_start(uint64_t opts)
383 struct thread_local_inits_st *locals;
385 if (!OPENSSL_init_crypto(0, NULL))
388 locals = ossl_init_get_thread_local(1);
393 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
394 #ifdef OPENSSL_INIT_DEBUG
395 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
396 "marking thread for async\n");
401 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
402 #ifdef OPENSSL_INIT_DEBUG
403 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
404 "marking thread for err_state\n");
406 locals->err_state = 1;
409 if (opts & OPENSSL_INIT_THREAD_RAND) {
410 #ifdef OPENSSL_INIT_DEBUG
411 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
412 "marking thread for rand\n");
420 void OPENSSL_cleanup(void)
422 OPENSSL_INIT_STOP *currhandler, *lasthandler;
424 /* If we've not been inited then no need to deinit */
428 /* Might be explicitly called and also by atexit */
434 * Thread stop may not get automatically called by the thread library for
435 * the very last thread in some situations, so call it directly.
437 ossl_init_thread_stop(ossl_init_get_thread_local(0));
439 currhandler = stop_handlers;
440 while (currhandler != NULL) {
441 currhandler->handler();
442 lasthandler = currhandler;
443 currhandler = currhandler->next;
444 OPENSSL_free(lasthandler);
446 stop_handlers = NULL;
448 CRYPTO_THREAD_lock_free(init_lock);
452 * We assume we are single-threaded for this function, i.e. no race
453 * conditions for the various "*_inited" vars below.
456 #ifndef OPENSSL_NO_COMP
458 #ifdef OPENSSL_INIT_DEBUG
459 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
460 "comp_zlib_cleanup_int()\n");
462 comp_zlib_cleanup_int();
467 # ifdef OPENSSL_INIT_DEBUG
468 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
474 if (load_crypto_strings_inited) {
475 #ifdef OPENSSL_INIT_DEBUG
476 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
477 "err_free_strings_int()\n");
479 err_free_strings_int();
482 CRYPTO_THREAD_cleanup_local(&threadstopkey);
484 #ifdef OPENSSL_INIT_DEBUG
485 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
486 "rand_cleanup_int()\n");
487 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
488 "conf_modules_free_int()\n");
489 #ifndef OPENSSL_NO_ENGINE
490 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
491 "engine_cleanup_int()\n");
493 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
494 "crypto_cleanup_all_ex_data_int()\n");
495 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
496 "bio_sock_cleanup_int()\n");
497 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
499 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
500 "evp_cleanup_int()\n");
501 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
502 "obj_cleanup_int()\n");
503 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
507 * Note that cleanup order is important:
508 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
509 * must be called before engine_cleanup_int()
510 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
511 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
512 * - conf_modules_free_int() can end up in ENGINE code so must be called
513 * before engine_cleanup_int()
514 * - ENGINEs and additional EVP algorithms might use added OIDs names so
515 * obj_cleanup_int() must be called last
518 rand_drbg_cleanup_int();
519 conf_modules_free_int();
520 #ifndef OPENSSL_NO_ENGINE
521 engine_cleanup_int();
523 ossl_store_cleanup_int();
524 crypto_cleanup_all_ex_data_int();
530 CRYPTO_secure_malloc_done();
536 * If this function is called with a non NULL settings value then it must be
537 * called prior to any threads making calls to any OpenSSL functions,
538 * i.e. passing a non-null settings value is assumed to be single-threaded.
540 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
542 static int stoperrset = 0;
547 * We only ever set this once to avoid getting into an infinite
548 * loop where the error system keeps trying to init and fails so
552 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
557 if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
560 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
561 && !RUN_ONCE(&load_crypto_strings,
562 ossl_init_no_load_crypto_strings))
565 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
566 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
569 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
570 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
573 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
574 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
577 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
578 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
581 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
582 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
585 if ((opts & OPENSSL_INIT_ATFORK)
586 && !openssl_init_fork_handlers())
589 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
590 && !RUN_ONCE(&config, ossl_init_no_config))
593 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
595 CRYPTO_THREAD_write_lock(init_lock);
596 appname = (settings == NULL) ? NULL : settings->appname;
597 ret = RUN_ONCE(&config, ossl_init_config);
598 CRYPTO_THREAD_unlock(init_lock);
603 if ((opts & OPENSSL_INIT_ASYNC)
604 && !RUN_ONCE(&async, ossl_init_async))
607 #ifndef OPENSSL_NO_ENGINE
608 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
609 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
611 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
612 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
613 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
616 # ifndef OPENSSL_NO_RDRAND
617 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
618 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
621 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
622 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
624 # ifndef OPENSSL_NO_STATIC_ENGINE
625 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
626 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
627 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
630 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
631 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
632 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
635 # if !defined(OPENSSL_NO_AFALGENG)
636 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
637 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
641 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
642 | OPENSSL_INIT_ENGINE_OPENSSL
643 | OPENSSL_INIT_ENGINE_AFALG)) {
644 ENGINE_register_all_complete();
648 #ifndef OPENSSL_NO_COMP
649 if ((opts & OPENSSL_INIT_ZLIB)
650 && !RUN_ONCE(&zlib, ossl_init_zlib))
657 int OPENSSL_atexit(void (*handler)(void))
659 OPENSSL_INIT_STOP *newhand;
661 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
668 handlersym.func = handler;
671 HMODULE handle = NULL;
675 * We don't use the DSO route for WIN32 because there is a better
678 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
679 | GET_MODULE_HANDLE_EX_FLAG_PIN,
680 handlersym.sym, &handle);
687 * Deliberately leak a reference to the handler. This will force the
688 * library/code containing the handler to remain loaded until we run the
689 * atexit handler. If -znodelete has been used then this is
696 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
697 # ifdef OPENSSL_INIT_DEBUG
699 "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
700 (dso == NULL ? "No!" : "Yes."));
701 /* See same code above in ossl_init_base() for an explanation. */
710 newhand = OPENSSL_malloc(sizeof(*newhand));
714 newhand->handler = handler;
715 newhand->next = stop_handlers;
716 stop_handlers = newhand;
721 #ifdef OPENSSL_SYS_UNIX
723 * The following three functions are for OpenSSL developers. This is
724 * where we set/reset state across fork (called via pthread_atfork when
725 * it exists, or manually by the application when it doesn't).
727 * WARNING! If you put code in either OPENSSL_fork_parent or
728 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
729 * safe. See this link, for example:
730 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
733 void OPENSSL_fork_prepare(void)
737 void OPENSSL_fork_parent(void)
741 void OPENSSL_fork_child(void)