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 * Deliberately leak a reference to ourselves. This will force the library
86 * to remain loaded until the atexit() handler is run a process exit.
91 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
98 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
99 static int load_crypto_strings_inited = 0;
100 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
102 /* Do nothing in this case */
106 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
110 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
111 * pulling in all the error strings during static linking
113 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
114 # ifdef OPENSSL_INIT_DEBUG
115 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
116 "err_load_crypto_strings_int()\n");
118 ret = err_load_crypto_strings_int();
119 load_crypto_strings_inited = 1;
124 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
125 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
128 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
129 * pulling in all the ciphers during static linking
131 #ifndef OPENSSL_NO_AUTOALGINIT
132 # ifdef OPENSSL_INIT_DEBUG
133 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
134 "openssl_add_all_ciphers_int()\n");
136 openssl_add_all_ciphers_int();
141 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
142 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
145 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
146 * pulling in all the ciphers during static linking
148 #ifndef OPENSSL_NO_AUTOALGINIT
149 # ifdef OPENSSL_INIT_DEBUG
150 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
151 "openssl_add_all_digests()\n");
153 openssl_add_all_digests_int();
158 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
164 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
165 static int config_inited = 0;
166 static const char *appname;
167 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
169 #ifdef OPENSSL_INIT_DEBUG
171 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
172 appname == NULL ? "NULL" : appname);
174 openssl_config_int(appname);
178 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
180 #ifdef OPENSSL_INIT_DEBUG
182 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
184 openssl_no_config_int();
189 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
190 static int async_inited = 0;
191 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
193 #ifdef OPENSSL_INIT_DEBUG
194 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
202 #ifndef OPENSSL_NO_ENGINE
203 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
204 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
206 # ifdef OPENSSL_INIT_DEBUG
207 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
208 "engine_load_openssl_int()\n");
210 engine_load_openssl_int();
213 # if !defined(OPENSSL_NO_HW) && \
214 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
215 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
216 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
218 # ifdef OPENSSL_INIT_DEBUG
219 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
220 "engine_load_cryptodev_int()\n");
222 engine_load_cryptodev_int();
227 # ifndef OPENSSL_NO_RDRAND
228 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
229 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
231 # ifdef OPENSSL_INIT_DEBUG
232 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
233 "engine_load_rdrand_int()\n");
235 engine_load_rdrand_int();
239 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
240 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
242 # ifdef OPENSSL_INIT_DEBUG
243 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
244 "engine_load_dynamic_int()\n");
246 engine_load_dynamic_int();
249 # ifndef OPENSSL_NO_STATIC_ENGINE
250 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
251 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
252 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
254 # ifdef OPENSSL_INIT_DEBUG
255 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
256 "engine_load_padlock_int()\n");
258 engine_load_padlock_int();
262 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
263 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
264 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
266 # ifdef OPENSSL_INIT_DEBUG
267 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
268 "engine_load_capi_int()\n");
270 engine_load_capi_int();
274 # if !defined(OPENSSL_NO_AFALGENG)
275 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
276 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
278 # ifdef OPENSSL_INIT_DEBUG
279 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
280 "engine_load_afalg_int()\n");
282 engine_load_afalg_int();
289 #ifndef OPENSSL_NO_COMP
290 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
292 static int zlib_inited = 0;
293 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
295 /* Do nothing - we need to know about this for the later cleanup */
301 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
303 /* Can't do much about this */
308 #ifdef OPENSSL_INIT_DEBUG
309 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
310 "ASYNC_cleanup_thread()\n");
312 ASYNC_cleanup_thread();
315 if (locals->err_state) {
316 #ifdef OPENSSL_INIT_DEBUG
317 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
318 "err_delete_thread_state()\n");
320 err_delete_thread_state();
323 OPENSSL_free(locals);
326 void OPENSSL_thread_stop(void)
328 ossl_init_thread_stop(
329 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
332 int ossl_init_thread_start(uint64_t opts)
334 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
339 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
340 #ifdef OPENSSL_INIT_DEBUG
341 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
342 "marking thread for async\n");
347 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
348 #ifdef OPENSSL_INIT_DEBUG
349 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
350 "marking thread for err_state\n");
352 locals->err_state = 1;
358 void OPENSSL_cleanup(void)
360 OPENSSL_INIT_STOP *currhandler, *lasthandler;
362 /* If we've not been inited then no need to deinit */
366 /* Might be explicitly called and also by atexit */
372 * Thread stop may not get automatically called by the thread library for
373 * the very last thread in some situations, so call it directly.
375 ossl_init_thread_stop(ossl_init_get_thread_local(0));
377 currhandler = stop_handlers;
378 while (currhandler != NULL) {
379 currhandler->handler();
380 lasthandler = currhandler;
381 currhandler = currhandler->next;
382 OPENSSL_free(lasthandler);
384 stop_handlers = NULL;
386 CRYPTO_THREAD_lock_free(init_lock);
389 * We assume we are single-threaded for this function, i.e. no race
390 * conditions for the various "*_inited" vars below.
393 #ifndef OPENSSL_NO_COMP
395 #ifdef OPENSSL_INIT_DEBUG
396 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
397 "comp_zlib_cleanup_int()\n");
399 comp_zlib_cleanup_int();
404 # ifdef OPENSSL_INIT_DEBUG
405 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
411 if (load_crypto_strings_inited) {
412 #ifdef OPENSSL_INIT_DEBUG
413 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
414 "err_free_strings_int()\n");
416 err_free_strings_int();
419 CRYPTO_THREAD_cleanup_local(&threadstopkey);
421 #ifdef OPENSSL_INIT_DEBUG
422 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
423 "rand_cleanup_int()\n");
424 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
425 "conf_modules_free_int()\n");
426 #ifndef OPENSSL_NO_ENGINE
427 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
428 "engine_cleanup_int()\n");
430 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
431 "crypto_cleanup_all_ex_data_int()\n");
432 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
433 "bio_sock_cleanup_int()\n");
434 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
436 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
437 "evp_cleanup_int()\n");
438 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
439 "obj_cleanup_int()\n");
440 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
444 * Note that cleanup order is important:
445 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
446 * must be called before engine_cleanup_int()
447 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
448 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
449 * - conf_modules_free_int() can end up in ENGINE code so must be called
450 * before engine_cleanup_int()
451 * - ENGINEs and additional EVP algorithms might use added OIDs names so
452 * obj_cleanup_int() must be called last
455 conf_modules_free_int();
456 #ifndef OPENSSL_NO_ENGINE
457 engine_cleanup_int();
459 crypto_cleanup_all_ex_data_int();
469 * If this function is called with a non NULL settings value then it must be
470 * called prior to any threads making calls to any OpenSSL functions,
471 * i.e. passing a non-null settings value is assumed to be single-threaded.
473 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
475 static int stoperrset = 0;
480 * We only ever set this once to avoid getting into an infinite
481 * loop where the error system keeps trying to init and fails so
485 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
490 if (!RUN_ONCE(&base, ossl_init_base))
493 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
494 && !RUN_ONCE(&load_crypto_strings,
495 ossl_init_no_load_crypto_strings))
498 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
499 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
502 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
503 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
506 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
507 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
510 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
511 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
514 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
515 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
518 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
519 && !RUN_ONCE(&config, ossl_init_no_config))
522 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
524 CRYPTO_THREAD_write_lock(init_lock);
525 appname = (settings == NULL) ? NULL : settings->appname;
526 ret = RUN_ONCE(&config, ossl_init_config);
527 CRYPTO_THREAD_unlock(init_lock);
532 if ((opts & OPENSSL_INIT_ASYNC)
533 && !RUN_ONCE(&async, ossl_init_async))
536 #ifndef OPENSSL_NO_ENGINE
537 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
538 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
540 # if !defined(OPENSSL_NO_HW) && \
541 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
542 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
543 && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
546 # ifndef OPENSSL_NO_RDRAND
547 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
548 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
551 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
552 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
554 # ifndef OPENSSL_NO_STATIC_ENGINE
555 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
556 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
557 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
560 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
561 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
562 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
565 # if !defined(OPENSSL_NO_AFALGENG)
566 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
567 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
571 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
572 | OPENSSL_INIT_ENGINE_OPENSSL
573 | OPENSSL_INIT_ENGINE_AFALG)) {
574 ENGINE_register_all_complete();
578 #ifndef OPENSSL_NO_COMP
579 if ((opts & OPENSSL_INIT_ZLIB)
580 && !RUN_ONCE(&zlib, ossl_init_zlib))
587 int OPENSSL_atexit(void (*handler)(void))
589 OPENSSL_INIT_STOP *newhand;
592 * Deliberately leak a reference to the handler. This will force the
593 * library/code containing the handler to remain loaded until we run the
603 handlersym.func = handler;
605 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
609 newhand = OPENSSL_malloc(sizeof(*newhand));
613 newhand->handler = handler;
614 newhand->next = stop_handlers;
615 stop_handlers = newhand;