2 * Copyright 2016 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
10 #include <internal/cryptlib_int.h>
11 #include <openssl/err.h>
12 #include <internal/rand.h>
13 #include <internal/bio.h>
14 #include <openssl/evp.h>
15 #include <internal/evp_int.h>
16 #include <internal/conf.h>
17 #include <internal/async.h>
18 #include <internal/engine.h>
19 #include <internal/comp.h>
20 #include <internal/err.h>
21 #include <internal/err_int.h>
22 #include <internal/objects.h>
25 #include <internal/thread_once.h>
26 #include <internal/dso.h>
28 static int stopped = 0;
30 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
32 static CRYPTO_THREAD_LOCAL threadstopkey;
34 static void ossl_init_thread_stop_wrap(void *local)
36 ossl_init_thread_stop((struct thread_local_inits_st *)local);
39 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
41 struct thread_local_inits_st *local =
42 CRYPTO_THREAD_get_local(&threadstopkey);
44 if (local == NULL && alloc) {
45 local = OPENSSL_zalloc(sizeof *local);
46 CRYPTO_THREAD_set_local(&threadstopkey, local);
49 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
55 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
56 struct ossl_init_stop_st {
57 void (*handler)(void);
58 OPENSSL_INIT_STOP *next;
61 static OPENSSL_INIT_STOP *stop_handlers = NULL;
62 static CRYPTO_RWLOCK *init_lock = NULL;
64 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
65 static int base_inited = 0;
66 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
68 #ifdef OPENSSL_INIT_DEBUG
69 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
71 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
72 ossl_malloc_setup_failures();
75 * We use a dummy thread local key here. We use the destructor to detect
76 * when the thread is going to stop (where that feature is available)
78 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
79 #ifndef OPENSSL_SYS_UEFI
80 atexit(OPENSSL_cleanup);
82 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
84 OPENSSL_cpuid_setup();
88 * Everything needed to be initialized in this function before threads
89 * come along MUST happen before base_inited is set to 1, or we will
90 * see race conditions.
94 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
97 HMODULE handle = NULL;
100 /* We don't use the DSO route for WIN32 because there is a better way */
101 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
102 | GET_MODULE_HANDLE_EX_FLAG_PIN,
103 (void *)&base_inited, &handle);
105 return (ret == TRUE) ? 1 : 0;
109 * Deliberately leak a reference to ourselves. This will force the library
110 * to remain loaded until the atexit() handler is run a process exit.
115 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
124 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
125 static int load_crypto_strings_inited = 0;
126 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
128 /* Do nothing in this case */
132 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
136 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
137 * pulling in all the error strings during static linking
139 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
140 # ifdef OPENSSL_INIT_DEBUG
141 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
142 "err_load_crypto_strings_int()\n");
144 ret = err_load_crypto_strings_int();
145 load_crypto_strings_inited = 1;
150 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
151 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
154 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
155 * pulling in all the ciphers during static linking
157 #ifndef OPENSSL_NO_AUTOALGINIT
158 # ifdef OPENSSL_INIT_DEBUG
159 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
160 "openssl_add_all_ciphers_int()\n");
162 openssl_add_all_ciphers_int();
167 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
168 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
171 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
172 * pulling in all the ciphers during static linking
174 #ifndef OPENSSL_NO_AUTOALGINIT
175 # ifdef OPENSSL_INIT_DEBUG
176 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
177 "openssl_add_all_digests()\n");
179 openssl_add_all_digests_int();
184 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
190 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
191 static int config_inited = 0;
192 static const char *appname;
193 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
195 #ifdef OPENSSL_INIT_DEBUG
197 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
198 appname == NULL ? "NULL" : appname);
200 openssl_config_int(appname);
204 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
206 #ifdef OPENSSL_INIT_DEBUG
208 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
210 openssl_no_config_int();
215 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
216 static int async_inited = 0;
217 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
219 #ifdef OPENSSL_INIT_DEBUG
220 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
228 #ifndef OPENSSL_NO_ENGINE
229 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
230 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
232 # ifdef OPENSSL_INIT_DEBUG
233 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
234 "engine_load_openssl_int()\n");
236 engine_load_openssl_int();
239 # if !defined(OPENSSL_NO_HW) && \
240 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(HAVE_CRYPTODEV))
241 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
242 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
244 # ifdef OPENSSL_INIT_DEBUG
245 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
246 "engine_load_cryptodev_int()\n");
248 engine_load_cryptodev_int();
253 # ifndef OPENSSL_NO_RDRAND
254 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
255 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
257 # ifdef OPENSSL_INIT_DEBUG
258 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
259 "engine_load_rdrand_int()\n");
261 engine_load_rdrand_int();
265 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
266 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
268 # ifdef OPENSSL_INIT_DEBUG
269 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
270 "engine_load_dynamic_int()\n");
272 engine_load_dynamic_int();
275 # ifndef OPENSSL_NO_STATIC_ENGINE
276 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
277 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
278 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
280 # ifdef OPENSSL_INIT_DEBUG
281 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
282 "engine_load_padlock_int()\n");
284 engine_load_padlock_int();
288 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
289 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
290 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
292 # ifdef OPENSSL_INIT_DEBUG
293 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
294 "engine_load_capi_int()\n");
296 engine_load_capi_int();
300 # if !defined(OPENSSL_NO_AFALGENG)
301 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
302 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
304 # ifdef OPENSSL_INIT_DEBUG
305 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
306 "engine_load_afalg_int()\n");
308 engine_load_afalg_int();
315 #ifndef OPENSSL_NO_COMP
316 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
318 static int zlib_inited = 0;
319 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
321 /* Do nothing - we need to know about this for the later cleanup */
327 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
329 /* Can't do much about this */
334 #ifdef OPENSSL_INIT_DEBUG
335 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
336 "ASYNC_cleanup_thread()\n");
338 ASYNC_cleanup_thread();
341 if (locals->err_state) {
342 #ifdef OPENSSL_INIT_DEBUG
343 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
344 "err_delete_thread_state()\n");
346 err_delete_thread_state();
349 OPENSSL_free(locals);
352 void OPENSSL_thread_stop(void)
354 ossl_init_thread_stop(
355 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
358 int ossl_init_thread_start(uint64_t opts)
360 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
365 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
366 #ifdef OPENSSL_INIT_DEBUG
367 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
368 "marking thread for async\n");
373 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
374 #ifdef OPENSSL_INIT_DEBUG
375 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
376 "marking thread for err_state\n");
378 locals->err_state = 1;
384 void OPENSSL_cleanup(void)
386 OPENSSL_INIT_STOP *currhandler, *lasthandler;
388 /* If we've not been inited then no need to deinit */
392 /* Might be explicitly called and also by atexit */
398 * Thread stop may not get automatically called by the thread library for
399 * the very last thread in some situations, so call it directly.
401 ossl_init_thread_stop(ossl_init_get_thread_local(0));
403 currhandler = stop_handlers;
404 while (currhandler != NULL) {
405 currhandler->handler();
406 lasthandler = currhandler;
407 currhandler = currhandler->next;
408 OPENSSL_free(lasthandler);
410 stop_handlers = NULL;
412 CRYPTO_THREAD_lock_free(init_lock);
415 * We assume we are single-threaded for this function, i.e. no race
416 * conditions for the various "*_inited" vars below.
419 #ifndef OPENSSL_NO_COMP
421 #ifdef OPENSSL_INIT_DEBUG
422 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
423 "comp_zlib_cleanup_int()\n");
425 comp_zlib_cleanup_int();
430 # ifdef OPENSSL_INIT_DEBUG
431 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
437 if (load_crypto_strings_inited) {
438 #ifdef OPENSSL_INIT_DEBUG
439 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
440 "err_free_strings_int()\n");
442 err_free_strings_int();
445 CRYPTO_THREAD_cleanup_local(&threadstopkey);
447 #ifdef OPENSSL_INIT_DEBUG
448 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
449 "rand_cleanup_int()\n");
450 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
451 "conf_modules_free_int()\n");
452 #ifndef OPENSSL_NO_ENGINE
453 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
454 "engine_cleanup_int()\n");
456 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
457 "crypto_cleanup_all_ex_data_int()\n");
458 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
459 "bio_sock_cleanup_int()\n");
460 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
462 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
463 "evp_cleanup_int()\n");
464 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
465 "obj_cleanup_int()\n");
466 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
470 * Note that cleanup order is important:
471 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
472 * must be called before engine_cleanup_int()
473 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
474 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
475 * - conf_modules_free_int() can end up in ENGINE code so must be called
476 * before engine_cleanup_int()
477 * - ENGINEs and additional EVP algorithms might use added OIDs names so
478 * obj_cleanup_int() must be called last
481 conf_modules_free_int();
482 #ifndef OPENSSL_NO_ENGINE
483 engine_cleanup_int();
485 crypto_cleanup_all_ex_data_int();
495 * If this function is called with a non NULL settings value then it must be
496 * called prior to any threads making calls to any OpenSSL functions,
497 * i.e. passing a non-null settings value is assumed to be single-threaded.
499 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
501 static int stoperrset = 0;
506 * We only ever set this once to avoid getting into an infinite
507 * loop where the error system keeps trying to init and fails so
511 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
516 if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
519 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
520 && !RUN_ONCE(&load_crypto_strings,
521 ossl_init_no_load_crypto_strings))
524 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
525 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
528 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
529 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
532 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
533 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
536 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
537 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
540 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
541 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
544 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
545 && !RUN_ONCE(&config, ossl_init_no_config))
548 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
550 CRYPTO_THREAD_write_lock(init_lock);
551 appname = (settings == NULL) ? NULL : settings->appname;
552 ret = RUN_ONCE(&config, ossl_init_config);
553 CRYPTO_THREAD_unlock(init_lock);
558 if ((opts & OPENSSL_INIT_ASYNC)
559 && !RUN_ONCE(&async, ossl_init_async))
562 #ifndef OPENSSL_NO_ENGINE
563 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
564 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
566 # if !defined(OPENSSL_NO_HW) && \
567 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(HAVE_CRYPTODEV))
568 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
569 && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
572 # ifndef OPENSSL_NO_RDRAND
573 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
574 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
577 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
578 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
580 # ifndef OPENSSL_NO_STATIC_ENGINE
581 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
582 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
583 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
586 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
587 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
588 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
591 # if !defined(OPENSSL_NO_AFALGENG)
592 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
593 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
597 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
598 | OPENSSL_INIT_ENGINE_OPENSSL
599 | OPENSSL_INIT_ENGINE_AFALG)) {
600 ENGINE_register_all_complete();
604 #ifndef OPENSSL_NO_COMP
605 if ((opts & OPENSSL_INIT_ZLIB)
606 && !RUN_ONCE(&zlib, ossl_init_zlib))
613 int OPENSSL_atexit(void (*handler)(void))
615 OPENSSL_INIT_STOP *newhand;
617 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
624 handlersym.func = handler;
627 HMODULE handle = NULL;
631 * We don't use the DSO route for WIN32 because there is a better
634 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
635 | GET_MODULE_HANDLE_EX_FLAG_PIN,
636 handlersym.sym, &handle);
643 * Deliberately leak a reference to the handler. This will force the
644 * library/code containing the handler to remain loaded until we run the
645 * atexit handler. If -znodelete has been used then this is
651 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
658 newhand = OPENSSL_malloc(sizeof(*newhand));
662 newhand->handler = handler;
663 newhand->next = stop_handlers;
664 stop_handlers = newhand;