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");
72 * We use a dummy thread local key here. We use the destructor to detect
73 * when the thread is going to stop (where that feature is available)
75 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
76 #ifndef OPENSSL_SYS_UEFI
77 atexit(OPENSSL_cleanup);
79 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
81 OPENSSL_cpuid_setup();
85 * Everything needed to be initialized in this function before threads
86 * come along MUST happen before base_inited is set to 1, or we will
87 * see race conditions.
91 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
94 HMODULE handle = NULL;
97 /* We don't use the DSO route for WIN32 because there is a better way */
98 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
99 | GET_MODULE_HANDLE_EX_FLAG_PIN,
100 (void *)&base_inited, &handle);
102 return (ret == TRUE) ? 1 : 0;
106 * Deliberately leak a reference to ourselves. This will force the library
107 * to remain loaded until the atexit() handler is run at process exit.
113 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
123 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
124 static int load_crypto_strings_inited = 0;
125 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
127 /* Do nothing in this case */
131 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
135 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
136 * pulling in all the error strings during static linking
138 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
139 # ifdef OPENSSL_INIT_DEBUG
140 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
141 "err_load_crypto_strings_int()\n");
143 ret = err_load_crypto_strings_int();
144 load_crypto_strings_inited = 1;
149 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
150 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
153 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
154 * pulling in all the ciphers during static linking
156 #ifndef OPENSSL_NO_AUTOALGINIT
157 # ifdef OPENSSL_INIT_DEBUG
158 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
159 "openssl_add_all_ciphers_int()\n");
161 openssl_add_all_ciphers_int();
166 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
167 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
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_digests: "
176 "openssl_add_all_digests()\n");
178 openssl_add_all_digests_int();
183 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
189 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
190 static int config_inited = 0;
191 static const char *appname;
192 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
194 #ifdef OPENSSL_INIT_DEBUG
196 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
197 appname == NULL ? "NULL" : appname);
199 openssl_config_int(appname);
203 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
205 #ifdef OPENSSL_INIT_DEBUG
207 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
209 openssl_no_config_int();
214 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
215 static int async_inited = 0;
216 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
218 #ifdef OPENSSL_INIT_DEBUG
219 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
227 #ifndef OPENSSL_NO_ENGINE
228 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
229 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
231 # ifdef OPENSSL_INIT_DEBUG
232 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
233 "engine_load_openssl_int()\n");
235 engine_load_openssl_int();
238 # if !defined(OPENSSL_NO_HW) && \
239 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
240 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
241 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
243 # ifdef OPENSSL_INIT_DEBUG
244 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
245 "engine_load_cryptodev_int()\n");
247 engine_load_cryptodev_int();
252 # ifndef OPENSSL_NO_RDRAND
253 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
254 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
256 # ifdef OPENSSL_INIT_DEBUG
257 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
258 "engine_load_rdrand_int()\n");
260 engine_load_rdrand_int();
264 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
265 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
267 # ifdef OPENSSL_INIT_DEBUG
268 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
269 "engine_load_dynamic_int()\n");
271 engine_load_dynamic_int();
274 # ifndef OPENSSL_NO_STATIC_ENGINE
275 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
276 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
277 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
279 # ifdef OPENSSL_INIT_DEBUG
280 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
281 "engine_load_padlock_int()\n");
283 engine_load_padlock_int();
287 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
288 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
289 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
291 # ifdef OPENSSL_INIT_DEBUG
292 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
293 "engine_load_capi_int()\n");
295 engine_load_capi_int();
299 # if !defined(OPENSSL_NO_AFALGENG)
300 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
301 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
303 # ifdef OPENSSL_INIT_DEBUG
304 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
305 "engine_load_afalg_int()\n");
307 engine_load_afalg_int();
314 #ifndef OPENSSL_NO_COMP
315 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
317 static int zlib_inited = 0;
318 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
320 /* Do nothing - we need to know about this for the later cleanup */
326 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
328 /* Can't do much about this */
333 #ifdef OPENSSL_INIT_DEBUG
334 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
335 "ASYNC_cleanup_thread()\n");
337 ASYNC_cleanup_thread();
340 if (locals->err_state) {
341 #ifdef OPENSSL_INIT_DEBUG
342 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
343 "err_delete_thread_state()\n");
345 err_delete_thread_state();
348 OPENSSL_free(locals);
351 void OPENSSL_thread_stop(void)
353 ossl_init_thread_stop(
354 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
357 int ossl_init_thread_start(uint64_t opts)
359 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
364 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
365 #ifdef OPENSSL_INIT_DEBUG
366 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
367 "marking thread for async\n");
372 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
373 #ifdef OPENSSL_INIT_DEBUG
374 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
375 "marking thread for err_state\n");
377 locals->err_state = 1;
383 void OPENSSL_cleanup(void)
385 OPENSSL_INIT_STOP *currhandler, *lasthandler;
387 /* If we've not been inited then no need to deinit */
391 /* Might be explicitly called and also by atexit */
397 * Thread stop may not get automatically called by the thread library for
398 * the very last thread in some situations, so call it directly.
400 ossl_init_thread_stop(ossl_init_get_thread_local(0));
402 currhandler = stop_handlers;
403 while (currhandler != NULL) {
404 currhandler->handler();
405 lasthandler = currhandler;
406 currhandler = currhandler->next;
407 OPENSSL_free(lasthandler);
409 stop_handlers = NULL;
411 CRYPTO_THREAD_lock_free(init_lock);
414 * We assume we are single-threaded for this function, i.e. no race
415 * conditions for the various "*_inited" vars below.
418 #ifndef OPENSSL_NO_COMP
420 #ifdef OPENSSL_INIT_DEBUG
421 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
422 "comp_zlib_cleanup_int()\n");
424 comp_zlib_cleanup_int();
429 # ifdef OPENSSL_INIT_DEBUG
430 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
436 if (load_crypto_strings_inited) {
437 #ifdef OPENSSL_INIT_DEBUG
438 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
439 "err_free_strings_int()\n");
441 err_free_strings_int();
444 CRYPTO_THREAD_cleanup_local(&threadstopkey);
446 #ifdef OPENSSL_INIT_DEBUG
447 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
448 "rand_cleanup_int()\n");
449 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
450 "conf_modules_free_int()\n");
451 #ifndef OPENSSL_NO_ENGINE
452 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
453 "engine_cleanup_int()\n");
455 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
456 "crypto_cleanup_all_ex_data_int()\n");
457 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
458 "bio_sock_cleanup_int()\n");
459 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
461 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
462 "evp_cleanup_int()\n");
463 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
464 "obj_cleanup_int()\n");
465 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
469 * Note that cleanup order is important:
470 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
471 * must be called before engine_cleanup_int()
472 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
473 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
474 * - conf_modules_free_int() can end up in ENGINE code so must be called
475 * before engine_cleanup_int()
476 * - ENGINEs and additional EVP algorithms might use added OIDs names so
477 * obj_cleanup_int() must be called last
480 conf_modules_free_int();
481 #ifndef OPENSSL_NO_ENGINE
482 engine_cleanup_int();
484 crypto_cleanup_all_ex_data_int();
494 * If this function is called with a non NULL settings value then it must be
495 * called prior to any threads making calls to any OpenSSL functions,
496 * i.e. passing a non-null settings value is assumed to be single-threaded.
498 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
500 static int stoperrset = 0;
505 * We only ever set this once to avoid getting into an infinite
506 * loop where the error system keeps trying to init and fails so
510 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
515 if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
518 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
519 && !RUN_ONCE(&load_crypto_strings,
520 ossl_init_no_load_crypto_strings))
523 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
524 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
527 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
528 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
531 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
532 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
535 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
536 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
539 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
540 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
543 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
544 && !RUN_ONCE(&config, ossl_init_no_config))
547 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
549 CRYPTO_THREAD_write_lock(init_lock);
550 appname = (settings == NULL) ? NULL : settings->appname;
551 ret = RUN_ONCE(&config, ossl_init_config);
552 CRYPTO_THREAD_unlock(init_lock);
557 if ((opts & OPENSSL_INIT_ASYNC)
558 && !RUN_ONCE(&async, ossl_init_async))
561 #ifndef OPENSSL_NO_ENGINE
562 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
563 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
565 # if !defined(OPENSSL_NO_HW) && \
566 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
567 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
568 && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
571 # ifndef OPENSSL_NO_RDRAND
572 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
573 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
576 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
577 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
579 # ifndef OPENSSL_NO_STATIC_ENGINE
580 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
581 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
582 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
585 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
586 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
587 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
590 # if !defined(OPENSSL_NO_AFALGENG)
591 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
592 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
596 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
597 | OPENSSL_INIT_ENGINE_OPENSSL
598 | OPENSSL_INIT_ENGINE_AFALG)) {
599 ENGINE_register_all_complete();
603 #ifndef OPENSSL_NO_COMP
604 if ((opts & OPENSSL_INIT_ZLIB)
605 && !RUN_ONCE(&zlib, ossl_init_zlib))
612 int OPENSSL_atexit(void (*handler)(void))
614 OPENSSL_INIT_STOP *newhand;
616 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
623 handlersym.func = handler;
626 HMODULE handle = NULL;
630 * We don't use the DSO route for WIN32 because there is a better
633 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
634 | GET_MODULE_HANDLE_EX_FLAG_PIN,
635 handlersym.sym, &handle);
642 * Deliberately leak a reference to the handler. This will force the
643 * library/code containing the handler to remain loaded until we run the
644 * atexit handler. If -znodelete has been used then this is
651 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
659 newhand = OPENSSL_malloc(sizeof(*newhand));
663 newhand->handler = handler;
664 newhand->next = stop_handlers;
665 stop_handlers = newhand;