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>
27 static int stopped = 0;
29 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
31 static CRYPTO_THREAD_LOCAL threadstopkey;
33 static void ossl_init_thread_stop_wrap(void *local)
35 ossl_init_thread_stop((struct thread_local_inits_st *)local);
38 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
40 struct thread_local_inits_st *local =
41 CRYPTO_THREAD_get_local(&threadstopkey);
43 if (local == NULL && alloc) {
44 local = OPENSSL_zalloc(sizeof *local);
45 CRYPTO_THREAD_set_local(&threadstopkey, local);
48 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
54 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
55 struct ossl_init_stop_st {
56 void (*handler)(void);
57 OPENSSL_INIT_STOP *next;
60 static OPENSSL_INIT_STOP *stop_handlers = NULL;
61 static CRYPTO_RWLOCK *init_lock = NULL;
63 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
64 static int base_inited = 0;
65 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
67 #ifdef OPENSSL_INIT_DEBUG
68 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
71 * We use a dummy thread local key here. We use the destructor to detect
72 * when the thread is going to stop (where that feature is available)
74 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
75 #ifndef OPENSSL_SYS_UEFI
76 atexit(OPENSSL_cleanup);
78 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
80 OPENSSL_cpuid_setup();
85 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
86 static int load_crypto_strings_inited = 0;
87 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
89 /* Do nothing in this case */
93 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
96 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
97 * pulling in all the error strings during static linking
99 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
100 # ifdef OPENSSL_INIT_DEBUG
101 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
102 "err_load_crypto_strings_int()\n");
104 err_load_crypto_strings_int();
106 load_crypto_strings_inited = 1;
110 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
111 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
114 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
115 * pulling in all the ciphers during static linking
117 #ifndef OPENSSL_NO_AUTOALGINIT
118 # ifdef OPENSSL_INIT_DEBUG
119 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
120 "openssl_add_all_ciphers_int()\n");
122 openssl_add_all_ciphers_int();
127 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
128 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
131 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
132 * pulling in all the ciphers during static linking
134 #ifndef OPENSSL_NO_AUTOALGINIT
135 # ifdef OPENSSL_INIT_DEBUG
136 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
137 "openssl_add_all_digests()\n");
139 openssl_add_all_digests_int();
144 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
150 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
151 static int config_inited = 0;
152 static const char *appname;
153 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
155 #ifdef OPENSSL_INIT_DEBUG
157 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
158 appname == NULL ? "NULL" : appname);
160 openssl_config_int(appname);
164 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
166 #ifdef OPENSSL_INIT_DEBUG
168 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
170 openssl_no_config_int();
175 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
176 static int async_inited = 0;
177 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
179 #ifdef OPENSSL_INIT_DEBUG
180 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
188 #ifndef OPENSSL_NO_ENGINE
189 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
190 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
192 # ifdef OPENSSL_INIT_DEBUG
193 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
194 "engine_load_openssl_int()\n");
196 engine_load_openssl_int();
199 # if !defined(OPENSSL_NO_HW) && \
200 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
201 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
202 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
204 # ifdef OPENSSL_INIT_DEBUG
205 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
206 "engine_load_cryptodev_int()\n");
208 engine_load_cryptodev_int();
213 # ifndef OPENSSL_NO_RDRAND
214 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
215 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
217 # ifdef OPENSSL_INIT_DEBUG
218 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
219 "engine_load_rdrand_int()\n");
221 engine_load_rdrand_int();
225 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
226 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
228 # ifdef OPENSSL_INIT_DEBUG
229 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
230 "engine_load_dynamic_int()\n");
232 engine_load_dynamic_int();
235 # ifndef OPENSSL_NO_STATIC_ENGINE
236 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
237 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
238 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
240 # ifdef OPENSSL_INIT_DEBUG
241 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
242 "engine_load_padlock_int()\n");
244 engine_load_padlock_int();
248 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
249 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
250 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
252 # ifdef OPENSSL_INIT_DEBUG
253 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
254 "engine_load_capi_int()\n");
256 engine_load_capi_int();
260 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
261 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dasync)
263 # ifdef OPENSSL_INIT_DEBUG
264 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
265 "engine_load_dasync_int()\n");
267 engine_load_dasync_int();
270 # if !defined(OPENSSL_NO_AFALGENG)
271 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
272 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
274 # ifdef OPENSSL_INIT_DEBUG
275 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
276 "engine_load_afalg_int()\n");
278 engine_load_afalg_int();
285 #ifndef OPENSSL_NO_COMP
286 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
288 static int zlib_inited = 0;
289 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
291 /* Do nothing - we need to know about this for the later cleanup */
297 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
299 /* Can't do much about this */
304 #ifdef OPENSSL_INIT_DEBUG
305 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
306 "ASYNC_cleanup_thread()\n");
308 ASYNC_cleanup_thread();
311 if (locals->err_state) {
312 #ifdef OPENSSL_INIT_DEBUG
313 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
314 "err_delete_thread_state()\n");
316 err_delete_thread_state();
319 OPENSSL_free(locals);
322 void OPENSSL_thread_stop(void)
324 ossl_init_thread_stop(
325 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
328 int ossl_init_thread_start(uint64_t opts)
330 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
335 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
336 #ifdef OPENSSL_INIT_DEBUG
337 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
338 "marking thread for async\n");
343 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
344 #ifdef OPENSSL_INIT_DEBUG
345 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
346 "marking thread for err_state\n");
348 locals->err_state = 1;
354 void OPENSSL_cleanup(void)
356 OPENSSL_INIT_STOP *currhandler, *lasthandler;
358 /* If we've not been inited then no need to deinit */
362 /* Might be explicitly called and also by atexit */
368 * Thread stop may not get automatically called by the thread library for
369 * the very last thread in some situations, so call it directly.
371 ossl_init_thread_stop(ossl_init_get_thread_local(0));
373 currhandler = stop_handlers;
374 while (currhandler != NULL) {
375 currhandler->handler();
376 lasthandler = currhandler;
377 currhandler = currhandler->next;
378 OPENSSL_free(lasthandler);
380 stop_handlers = NULL;
382 CRYPTO_THREAD_lock_free(init_lock);
385 * We assume we are single-threaded for this function, i.e. no race
386 * conditions for the various "*_inited" vars below.
389 #ifndef OPENSSL_NO_COMP
391 #ifdef OPENSSL_INIT_DEBUG
392 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
393 "comp_zlib_cleanup_int()\n");
395 comp_zlib_cleanup_int();
400 # ifdef OPENSSL_INIT_DEBUG
401 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
407 if (load_crypto_strings_inited) {
408 #ifdef OPENSSL_INIT_DEBUG
409 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
410 "err_free_strings_int()\n");
412 err_free_strings_int();
415 CRYPTO_THREAD_cleanup_local(&threadstopkey);
417 #ifdef OPENSSL_INIT_DEBUG
418 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
419 "rand_cleanup_int()\n");
420 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
421 "conf_modules_free_int()\n");
422 #ifndef OPENSSL_NO_ENGINE
423 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
424 "engine_cleanup_int()\n");
426 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
427 "crypto_cleanup_all_ex_data_int()\n");
428 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
429 "bio_sock_cleanup_int()\n");
430 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
432 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
433 "evp_cleanup_int()\n");
434 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
435 "obj_cleanup_int()\n");
436 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
440 * Note that cleanup order is important:
441 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
442 * must be called before engine_cleanup_int()
443 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
444 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
445 * - conf_modules_free_int() can end up in ENGINE code so must be called
446 * before engine_cleanup_int()
447 * - ENGINEs and additional EVP algorithms might use added OIDs names so
448 * obj_cleanup_int() must be called last
451 conf_modules_free_int();
452 #ifndef OPENSSL_NO_ENGINE
453 engine_cleanup_int();
455 crypto_cleanup_all_ex_data_int();
465 * If this function is called with a non NULL settings value then it must be
466 * called prior to any threads making calls to any OpenSSL functions,
467 * i.e. passing a non-null settings value is assumed to be single-threaded.
469 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
471 static int stoperrset = 0;
476 * We only ever set this once to avoid getting into an infinite
477 * loop where the error system keeps trying to init and fails so
481 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
486 if (!RUN_ONCE(&base, ossl_init_base))
489 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
490 && !RUN_ONCE(&load_crypto_strings,
491 ossl_init_no_load_crypto_strings))
494 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
495 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
498 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
499 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
502 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
503 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
506 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
507 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
510 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
511 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
514 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
515 && !RUN_ONCE(&config, ossl_init_no_config))
518 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
520 CRYPTO_THREAD_write_lock(init_lock);
521 appname = (settings == NULL) ? NULL : settings->appname;
522 ret = RUN_ONCE(&config, ossl_init_config);
523 CRYPTO_THREAD_unlock(init_lock);
528 if ((opts & OPENSSL_INIT_ASYNC)
529 && !RUN_ONCE(&async, ossl_init_async))
532 #ifndef OPENSSL_NO_ENGINE
533 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
534 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
536 # if !defined(OPENSSL_NO_HW) && \
537 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
538 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
539 && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
542 # ifndef OPENSSL_NO_RDRAND
543 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
544 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
547 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
548 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
550 # ifndef OPENSSL_NO_STATIC_ENGINE
551 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
552 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
553 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
556 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
557 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
558 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
561 if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
562 && !RUN_ONCE(&engine_dasync, ossl_init_engine_dasync))
564 # if !defined(OPENSSL_NO_AFALGENG)
565 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
566 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
570 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
571 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
572 | OPENSSL_INIT_ENGINE_AFALG)) {
573 ENGINE_register_all_complete();
577 #ifndef OPENSSL_NO_COMP
578 if ((opts & OPENSSL_INIT_ZLIB)
579 && !RUN_ONCE(&zlib, ossl_init_zlib))
586 int OPENSSL_atexit(void (*handler)(void))
588 OPENSSL_INIT_STOP *newhand;
590 newhand = OPENSSL_malloc(sizeof(*newhand));
594 newhand->handler = handler;
595 newhand->next = stop_handlers;
596 stop_handlers = newhand;