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();
84 #ifndef OPENSSL_USE_NODELETE
87 HMODULE handle = NULL;
90 /* We don't use the DSO route for WIN32 because there is a better way */
91 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
92 | GET_MODULE_HANDLE_EX_FLAG_PIN,
93 (void *)&base_inited, &handle);
95 return (ret == TRUE) ? 1 : 0;
99 * Deliberately leak a reference to ourselves. This will force the library
100 * to remain loaded until the atexit() handler is run a process exit.
105 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
114 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
115 static int load_crypto_strings_inited = 0;
116 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
118 /* Do nothing in this case */
122 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
126 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
127 * pulling in all the error strings during static linking
129 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
130 # ifdef OPENSSL_INIT_DEBUG
131 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
132 "err_load_crypto_strings_int()\n");
134 ret = err_load_crypto_strings_int();
135 load_crypto_strings_inited = 1;
140 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
141 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
144 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
145 * pulling in all the ciphers during static linking
147 #ifndef OPENSSL_NO_AUTOALGINIT
148 # ifdef OPENSSL_INIT_DEBUG
149 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
150 "openssl_add_all_ciphers_int()\n");
152 openssl_add_all_ciphers_int();
157 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
158 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
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_digests: "
167 "openssl_add_all_digests()\n");
169 openssl_add_all_digests_int();
174 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
180 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
181 static int config_inited = 0;
182 static const char *appname;
183 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
185 #ifdef OPENSSL_INIT_DEBUG
187 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
188 appname == NULL ? "NULL" : appname);
190 openssl_config_int(appname);
194 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
196 #ifdef OPENSSL_INIT_DEBUG
198 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
200 openssl_no_config_int();
205 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
206 static int async_inited = 0;
207 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
209 #ifdef OPENSSL_INIT_DEBUG
210 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
218 #ifndef OPENSSL_NO_ENGINE
219 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
220 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
222 # ifdef OPENSSL_INIT_DEBUG
223 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
224 "engine_load_openssl_int()\n");
226 engine_load_openssl_int();
229 # if !defined(OPENSSL_NO_HW) && \
230 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
231 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
232 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
234 # ifdef OPENSSL_INIT_DEBUG
235 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
236 "engine_load_cryptodev_int()\n");
238 engine_load_cryptodev_int();
243 # ifndef OPENSSL_NO_RDRAND
244 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
245 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
247 # ifdef OPENSSL_INIT_DEBUG
248 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
249 "engine_load_rdrand_int()\n");
251 engine_load_rdrand_int();
255 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
256 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
258 # ifdef OPENSSL_INIT_DEBUG
259 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
260 "engine_load_dynamic_int()\n");
262 engine_load_dynamic_int();
265 # ifndef OPENSSL_NO_STATIC_ENGINE
266 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
267 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
268 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
270 # ifdef OPENSSL_INIT_DEBUG
271 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
272 "engine_load_padlock_int()\n");
274 engine_load_padlock_int();
278 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
279 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
280 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
282 # ifdef OPENSSL_INIT_DEBUG
283 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
284 "engine_load_capi_int()\n");
286 engine_load_capi_int();
290 # if !defined(OPENSSL_NO_AFALGENG)
291 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
292 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
294 # ifdef OPENSSL_INIT_DEBUG
295 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
296 "engine_load_afalg_int()\n");
298 engine_load_afalg_int();
305 #ifndef OPENSSL_NO_COMP
306 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
308 static int zlib_inited = 0;
309 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
311 /* Do nothing - we need to know about this for the later cleanup */
317 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
319 /* Can't do much about this */
324 #ifdef OPENSSL_INIT_DEBUG
325 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
326 "ASYNC_cleanup_thread()\n");
328 ASYNC_cleanup_thread();
331 if (locals->err_state) {
332 #ifdef OPENSSL_INIT_DEBUG
333 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
334 "err_delete_thread_state()\n");
336 err_delete_thread_state();
339 OPENSSL_free(locals);
342 void OPENSSL_thread_stop(void)
344 ossl_init_thread_stop(
345 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
348 int ossl_init_thread_start(uint64_t opts)
350 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
355 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
356 #ifdef OPENSSL_INIT_DEBUG
357 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
358 "marking thread for async\n");
363 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
364 #ifdef OPENSSL_INIT_DEBUG
365 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
366 "marking thread for err_state\n");
368 locals->err_state = 1;
374 void OPENSSL_cleanup(void)
376 OPENSSL_INIT_STOP *currhandler, *lasthandler;
378 /* If we've not been inited then no need to deinit */
382 /* Might be explicitly called and also by atexit */
388 * Thread stop may not get automatically called by the thread library for
389 * the very last thread in some situations, so call it directly.
391 ossl_init_thread_stop(ossl_init_get_thread_local(0));
393 currhandler = stop_handlers;
394 while (currhandler != NULL) {
395 currhandler->handler();
396 lasthandler = currhandler;
397 currhandler = currhandler->next;
398 OPENSSL_free(lasthandler);
400 stop_handlers = NULL;
402 CRYPTO_THREAD_lock_free(init_lock);
405 * We assume we are single-threaded for this function, i.e. no race
406 * conditions for the various "*_inited" vars below.
409 #ifndef OPENSSL_NO_COMP
411 #ifdef OPENSSL_INIT_DEBUG
412 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
413 "comp_zlib_cleanup_int()\n");
415 comp_zlib_cleanup_int();
420 # ifdef OPENSSL_INIT_DEBUG
421 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
427 if (load_crypto_strings_inited) {
428 #ifdef OPENSSL_INIT_DEBUG
429 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
430 "err_free_strings_int()\n");
432 err_free_strings_int();
435 CRYPTO_THREAD_cleanup_local(&threadstopkey);
437 #ifdef OPENSSL_INIT_DEBUG
438 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
439 "rand_cleanup_int()\n");
440 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
441 "conf_modules_free_int()\n");
442 #ifndef OPENSSL_NO_ENGINE
443 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
444 "engine_cleanup_int()\n");
446 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
447 "crypto_cleanup_all_ex_data_int()\n");
448 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
449 "bio_sock_cleanup_int()\n");
450 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
452 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
453 "evp_cleanup_int()\n");
454 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
455 "obj_cleanup_int()\n");
456 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
460 * Note that cleanup order is important:
461 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
462 * must be called before engine_cleanup_int()
463 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
464 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
465 * - conf_modules_free_int() can end up in ENGINE code so must be called
466 * before engine_cleanup_int()
467 * - ENGINEs and additional EVP algorithms might use added OIDs names so
468 * obj_cleanup_int() must be called last
471 conf_modules_free_int();
472 #ifndef OPENSSL_NO_ENGINE
473 engine_cleanup_int();
475 crypto_cleanup_all_ex_data_int();
485 * If this function is called with a non NULL settings value then it must be
486 * called prior to any threads making calls to any OpenSSL functions,
487 * i.e. passing a non-null settings value is assumed to be single-threaded.
489 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
491 static int stoperrset = 0;
496 * We only ever set this once to avoid getting into an infinite
497 * loop where the error system keeps trying to init and fails so
501 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
506 if (!RUN_ONCE(&base, ossl_init_base))
509 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
510 && !RUN_ONCE(&load_crypto_strings,
511 ossl_init_no_load_crypto_strings))
514 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
515 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
518 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
519 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
522 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
523 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
526 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
527 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
530 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
531 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
534 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
535 && !RUN_ONCE(&config, ossl_init_no_config))
538 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
540 CRYPTO_THREAD_write_lock(init_lock);
541 appname = (settings == NULL) ? NULL : settings->appname;
542 ret = RUN_ONCE(&config, ossl_init_config);
543 CRYPTO_THREAD_unlock(init_lock);
548 if ((opts & OPENSSL_INIT_ASYNC)
549 && !RUN_ONCE(&async, ossl_init_async))
552 #ifndef OPENSSL_NO_ENGINE
553 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
554 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
556 # if !defined(OPENSSL_NO_HW) && \
557 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
558 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
559 && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
562 # ifndef OPENSSL_NO_RDRAND
563 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
564 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
567 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
568 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
570 # ifndef OPENSSL_NO_STATIC_ENGINE
571 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
572 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
573 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
576 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
577 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
578 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
581 # if !defined(OPENSSL_NO_AFALGENG)
582 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
583 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
587 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
588 | OPENSSL_INIT_ENGINE_OPENSSL
589 | OPENSSL_INIT_ENGINE_AFALG)) {
590 ENGINE_register_all_complete();
594 #ifndef OPENSSL_NO_COMP
595 if ((opts & OPENSSL_INIT_ZLIB)
596 && !RUN_ONCE(&zlib, ossl_init_zlib))
603 int OPENSSL_atexit(void (*handler)(void))
605 OPENSSL_INIT_STOP *newhand;
607 #ifndef OPENSSL_USE_NODELETE
614 handlersym.func = handler;
617 HMODULE handle = NULL;
621 * We don't use the DSO route for WIN32 because there is a better
624 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
625 | GET_MODULE_HANDLE_EX_FLAG_PIN,
626 handlersym.sym, &handle);
633 * Deliberately leak a reference to the handler. This will force the
634 * library/code containing the handler to remain loaded until we run the
635 * atexit handler. If -znodelete has been used then this is
641 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
648 newhand = OPENSSL_malloc(sizeof(*newhand));
652 newhand->handler = handler;
653 newhand->next = stop_handlers;
654 stop_handlers = newhand;