2 * Copyright 2016-2017 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"
31 typedef struct global_lock_st {
34 struct global_lock_st *next;
37 static GLOBAL_LOCK *global_locks;
39 static int stopped = 0;
41 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
43 static CRYPTO_THREAD_LOCAL threadstopkey;
45 static void ossl_init_thread_stop_wrap(void *local)
47 ossl_init_thread_stop((struct thread_local_inits_st *)local);
50 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
52 struct thread_local_inits_st *local =
53 CRYPTO_THREAD_get_local(&threadstopkey);
55 if (local == NULL && alloc) {
56 local = OPENSSL_zalloc(sizeof *local);
57 if (local != NULL && !CRYPTO_THREAD_set_local(&threadstopkey, local)) {
63 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
69 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
70 struct ossl_init_stop_st {
71 void (*handler)(void);
72 OPENSSL_INIT_STOP *next;
75 static OPENSSL_INIT_STOP *stop_handlers = NULL;
76 static CRYPTO_RWLOCK *init_lock = NULL;
77 static CRYPTO_RWLOCK *glock_lock = NULL;
79 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
80 static int base_inited = 0;
81 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
83 #ifdef OPENSSL_INIT_DEBUG
84 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
86 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
87 ossl_malloc_setup_failures();
90 * We use a dummy thread local key here. We use the destructor to detect
91 * when the thread is going to stop (where that feature is available)
93 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
94 #ifndef OPENSSL_SYS_UEFI
95 atexit(OPENSSL_cleanup);
97 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL
98 || (glock_lock = CRYPTO_THREAD_lock_new()) == NULL)
100 OPENSSL_cpuid_setup();
104 * Everything needed to be initialized in this function before threads
105 * come along MUST happen before base_inited is set to 1, or we will
106 * see race conditions.
110 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
113 HMODULE handle = NULL;
116 /* We don't use the DSO route for WIN32 because there is a better way */
117 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
118 | GET_MODULE_HANDLE_EX_FLAG_PIN,
119 (void *)&base_inited, &handle);
121 return (ret == TRUE) ? 1 : 0;
125 * Deliberately leak a reference to ourselves. This will force the library
126 * to remain loaded until the atexit() handler is run at process exit.
132 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
142 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
143 static int load_crypto_strings_inited = 0;
144 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
146 /* Do nothing in this case */
150 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
154 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
155 * pulling in all the error strings during static linking
157 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
158 # ifdef OPENSSL_INIT_DEBUG
159 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
160 "err_load_crypto_strings_int()\n");
162 ret = err_load_crypto_strings_int();
163 load_crypto_strings_inited = 1;
168 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
169 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
172 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
173 * pulling in all the ciphers during static linking
175 #ifndef OPENSSL_NO_AUTOALGINIT
176 # ifdef OPENSSL_INIT_DEBUG
177 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
178 "openssl_add_all_ciphers_int()\n");
180 openssl_add_all_ciphers_int();
185 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
186 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
189 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
190 * pulling in all the ciphers during static linking
192 #ifndef OPENSSL_NO_AUTOALGINIT
193 # ifdef OPENSSL_INIT_DEBUG
194 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
195 "openssl_add_all_digests()\n");
197 openssl_add_all_digests_int();
202 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
208 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
209 static int config_inited = 0;
210 static const char *appname;
211 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
213 #ifdef OPENSSL_INIT_DEBUG
215 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
216 appname == NULL ? "NULL" : appname);
218 openssl_config_int(appname);
222 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
224 #ifdef OPENSSL_INIT_DEBUG
226 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
228 openssl_no_config_int();
233 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
234 static int async_inited = 0;
235 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
237 #ifdef OPENSSL_INIT_DEBUG
238 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
246 #ifndef OPENSSL_NO_ENGINE
247 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
248 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
250 # ifdef OPENSSL_INIT_DEBUG
251 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
252 "engine_load_openssl_int()\n");
254 engine_load_openssl_int();
257 # ifndef OPENSSL_NO_DEVCRYPTOENG
258 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
259 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
261 # ifdef OPENSSL_INIT_DEBUG
262 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
263 "engine_load_devcrypto_int()\n");
265 engine_load_devcrypto_int();
270 # ifndef OPENSSL_NO_RDRAND
271 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
272 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
274 # ifdef OPENSSL_INIT_DEBUG
275 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
276 "engine_load_rdrand_int()\n");
278 engine_load_rdrand_int();
282 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
283 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
285 # ifdef OPENSSL_INIT_DEBUG
286 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
287 "engine_load_dynamic_int()\n");
289 engine_load_dynamic_int();
292 # ifndef OPENSSL_NO_STATIC_ENGINE
293 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
294 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
295 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
297 # ifdef OPENSSL_INIT_DEBUG
298 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
299 "engine_load_padlock_int()\n");
301 engine_load_padlock_int();
305 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
306 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
307 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
309 # ifdef OPENSSL_INIT_DEBUG
310 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
311 "engine_load_capi_int()\n");
313 engine_load_capi_int();
317 # if !defined(OPENSSL_NO_AFALGENG)
318 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
319 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
321 # ifdef OPENSSL_INIT_DEBUG
322 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
323 "engine_load_afalg_int()\n");
325 engine_load_afalg_int();
332 #ifndef OPENSSL_NO_COMP
333 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
335 static int zlib_inited = 0;
336 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
338 /* Do nothing - we need to know about this for the later cleanup */
344 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
346 /* Can't do much about this */
351 #ifdef OPENSSL_INIT_DEBUG
352 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
353 "ASYNC_cleanup_thread()\n");
355 ASYNC_cleanup_thread();
358 if (locals->err_state) {
359 #ifdef OPENSSL_INIT_DEBUG
360 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
361 "err_delete_thread_state()\n");
363 err_delete_thread_state();
366 OPENSSL_free(locals);
369 void OPENSSL_thread_stop(void)
371 ossl_init_thread_stop(
372 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
375 int ossl_init_thread_start(uint64_t opts)
377 struct thread_local_inits_st *locals;
379 if (!OPENSSL_init_crypto(0, NULL))
382 locals = ossl_init_get_thread_local(1);
387 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
388 #ifdef OPENSSL_INIT_DEBUG
389 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
390 "marking thread for async\n");
395 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
396 #ifdef OPENSSL_INIT_DEBUG
397 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
398 "marking thread for err_state\n");
400 locals->err_state = 1;
406 void OPENSSL_cleanup(void)
408 OPENSSL_INIT_STOP *currhandler, *lasthandler;
410 /* If we've not been inited then no need to deinit */
414 /* Might be explicitly called and also by atexit */
419 /* Free list of global locks. */
420 while (global_locks != NULL) {
421 GLOBAL_LOCK *next = global_locks->next;
428 * Thread stop may not get automatically called by the thread library for
429 * the very last thread in some situations, so call it directly.
431 ossl_init_thread_stop(ossl_init_get_thread_local(0));
433 currhandler = stop_handlers;
434 while (currhandler != NULL) {
435 currhandler->handler();
436 lasthandler = currhandler;
437 currhandler = currhandler->next;
438 OPENSSL_free(lasthandler);
440 stop_handlers = NULL;
442 CRYPTO_THREAD_lock_free(init_lock);
444 CRYPTO_THREAD_lock_free(glock_lock);
448 * We assume we are single-threaded for this function, i.e. no race
449 * conditions for the various "*_inited" vars below.
452 #ifndef OPENSSL_NO_COMP
454 #ifdef OPENSSL_INIT_DEBUG
455 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
456 "comp_zlib_cleanup_int()\n");
458 comp_zlib_cleanup_int();
463 # ifdef OPENSSL_INIT_DEBUG
464 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
470 if (load_crypto_strings_inited) {
471 #ifdef OPENSSL_INIT_DEBUG
472 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
473 "err_free_strings_int()\n");
475 err_free_strings_int();
478 CRYPTO_THREAD_cleanup_local(&threadstopkey);
480 #ifdef OPENSSL_INIT_DEBUG
481 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
482 "rand_cleanup_int()\n");
483 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
484 "conf_modules_free_int()\n");
485 #ifndef OPENSSL_NO_ENGINE
486 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
487 "engine_cleanup_int()\n");
489 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
490 "crypto_cleanup_all_ex_data_int()\n");
491 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
492 "bio_sock_cleanup_int()\n");
493 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
495 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
496 "evp_cleanup_int()\n");
497 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
498 "obj_cleanup_int()\n");
499 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
503 * Note that cleanup order is important:
504 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
505 * must be called before engine_cleanup_int()
506 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
507 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
508 * - conf_modules_free_int() can end up in ENGINE code so must be called
509 * before engine_cleanup_int()
510 * - ENGINEs and additional EVP algorithms might use added OIDs names so
511 * obj_cleanup_int() must be called last
514 rand_cleanup_drbg_int();
515 conf_modules_free_int();
516 #ifndef OPENSSL_NO_ENGINE
517 engine_cleanup_int();
519 ossl_store_cleanup_int();
520 crypto_cleanup_all_ex_data_int();
530 * If this function is called with a non NULL settings value then it must be
531 * called prior to any threads making calls to any OpenSSL functions,
532 * i.e. passing a non-null settings value is assumed to be single-threaded.
534 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
536 static int stoperrset = 0;
541 * We only ever set this once to avoid getting into an infinite
542 * loop where the error system keeps trying to init and fails so
546 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
551 if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
554 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
555 && !RUN_ONCE(&load_crypto_strings,
556 ossl_init_no_load_crypto_strings))
559 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
560 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
563 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
564 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
567 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
568 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
571 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
572 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
575 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
576 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
579 if ((opts & OPENSSL_INIT_ATFORK)
580 && !openssl_init_fork_handlers())
583 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
584 && !RUN_ONCE(&config, ossl_init_no_config))
587 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
589 CRYPTO_THREAD_write_lock(init_lock);
590 appname = (settings == NULL) ? NULL : settings->appname;
591 ret = RUN_ONCE(&config, ossl_init_config);
592 CRYPTO_THREAD_unlock(init_lock);
597 if ((opts & OPENSSL_INIT_ASYNC)
598 && !RUN_ONCE(&async, ossl_init_async))
601 #ifndef OPENSSL_NO_ENGINE
602 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
603 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
605 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
606 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
607 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
610 # ifndef OPENSSL_NO_RDRAND
611 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
612 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
615 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
616 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
618 # ifndef OPENSSL_NO_STATIC_ENGINE
619 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
620 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
621 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
624 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
625 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
626 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
629 # if !defined(OPENSSL_NO_AFALGENG)
630 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
631 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
635 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
636 | OPENSSL_INIT_ENGINE_OPENSSL
637 | OPENSSL_INIT_ENGINE_AFALG)) {
638 ENGINE_register_all_complete();
642 #ifndef OPENSSL_NO_COMP
643 if ((opts & OPENSSL_INIT_ZLIB)
644 && !RUN_ONCE(&zlib, ossl_init_zlib))
651 int OPENSSL_atexit(void (*handler)(void))
653 OPENSSL_INIT_STOP *newhand;
655 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
662 handlersym.func = handler;
665 HMODULE handle = NULL;
669 * We don't use the DSO route for WIN32 because there is a better
672 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
673 | GET_MODULE_HANDLE_EX_FLAG_PIN,
674 handlersym.sym, &handle);
681 * Deliberately leak a reference to the handler. This will force the
682 * library/code containing the handler to remain loaded until we run the
683 * atexit handler. If -znodelete has been used then this is
690 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
698 newhand = OPENSSL_malloc(sizeof(*newhand));
702 newhand->handler = handler;
703 newhand->next = stop_handlers;
704 stop_handlers = newhand;
709 #ifndef OPENSSL_SYS_UNIX
710 CRYPTO_RWLOCK *CRYPTO_THREAD_glock_new(const char *name)
712 return CRYPTO_THREAD_lock_new();
718 * Create a new global lock, return NULL on error.
720 CRYPTO_RWLOCK *CRYPTO_THREAD_glock_new(const char *name)
722 GLOBAL_LOCK *newlock;
725 || glock_lock == NULL
726 || (newlock = malloc(sizeof(*newlock))) == NULL)
727 return CRYPTO_THREAD_lock_new();
728 CRYPTO_THREAD_write_lock(glock_lock);
729 newlock->name = name;
730 newlock->lock = CRYPTO_THREAD_lock_new();
731 newlock->next = global_locks;
732 global_locks = newlock->next;
733 CRYPTO_THREAD_unlock(glock_lock);
734 return newlock->lock;
738 * Unlock all global locks.
740 static void unlock_all(void)
744 CRYPTO_THREAD_write_lock(init_lock);
745 for (lp = global_locks; lp != NULL; lp = lp->next)
746 CRYPTO_THREAD_unlock(lp->lock);
747 CRYPTO_THREAD_unlock(init_lock);
751 * The following three functions are for OpenSSL developers. This is
752 * where we set/reset state across fork (called via pthread_atfork when
753 * it exists, or manually by the application when it doesn't).
755 * WARNING! If you put code in either OPENSSL_fork_parent or
756 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
757 * safe. See this link, for example:
758 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
761 void OPENSSL_fork_prepare(void)
765 CRYPTO_THREAD_write_lock(init_lock);
766 for (lp = global_locks; lp != NULL; lp = lp->next)
767 CRYPTO_THREAD_write_lock(lp->lock);
768 CRYPTO_THREAD_unlock(init_lock);
771 void OPENSSL_fork_parent(void)
776 void OPENSSL_fork_child(void)