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>
26 static int stopped = 0;
28 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
30 static CRYPTO_THREAD_LOCAL threadstopkey;
32 static void ossl_init_thread_stop_wrap(void *local)
34 ossl_init_thread_stop((struct thread_local_inits_st *)local);
37 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
39 struct thread_local_inits_st *local =
40 CRYPTO_THREAD_get_local(&threadstopkey);
42 if (local == NULL && alloc) {
43 local = OPENSSL_zalloc(sizeof *local);
44 CRYPTO_THREAD_set_local(&threadstopkey, local);
47 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
53 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
54 struct ossl_init_stop_st {
55 void (*handler)(void);
56 OPENSSL_INIT_STOP *next;
59 static OPENSSL_INIT_STOP *stop_handlers = NULL;
60 static CRYPTO_RWLOCK *init_lock = NULL;
62 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
63 static int base_inited = 0;
64 static void ossl_init_base(void)
66 #ifdef OPENSSL_INIT_DEBUG
67 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
70 * We use a dummy thread local key here. We use the destructor to detect
71 * when the thread is going to stop (where that feature is available)
73 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
74 #ifndef OPENSSL_SYS_UEFI
75 atexit(OPENSSL_cleanup);
77 init_lock = CRYPTO_THREAD_lock_new();
78 OPENSSL_cpuid_setup();
82 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
83 static int load_crypto_strings_inited = 0;
84 static void ossl_init_no_load_crypto_strings(void)
86 /* Do nothing in this case */
90 static void ossl_init_load_crypto_strings(void)
93 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
94 * pulling in all the error strings during static linking
96 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
97 # ifdef OPENSSL_INIT_DEBUG
98 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
99 "err_load_crypto_strings_int()\n");
101 err_load_crypto_strings_int();
103 load_crypto_strings_inited = 1;
106 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
107 static void ossl_init_add_all_ciphers(void)
110 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
111 * pulling in all the ciphers during static linking
113 #ifndef OPENSSL_NO_AUTOALGINIT
114 # ifdef OPENSSL_INIT_DEBUG
115 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
116 "openssl_add_all_ciphers_int()\n");
118 openssl_add_all_ciphers_int();
122 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
123 static void ossl_init_add_all_digests(void)
126 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
127 * pulling in all the ciphers during static linking
129 #ifndef OPENSSL_NO_AUTOALGINIT
130 # ifdef OPENSSL_INIT_DEBUG
131 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
132 "openssl_add_all_digests()\n");
134 openssl_add_all_digests_int();
138 static void ossl_init_no_add_algs(void)
144 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
145 static int config_inited = 0;
146 static const char *appname;
147 static void ossl_init_config(void)
149 #ifdef OPENSSL_INIT_DEBUG
151 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
152 appname == NULL ? "NULL" : appname);
154 openssl_config_int(appname);
157 static void ossl_init_no_config(void)
159 #ifdef OPENSSL_INIT_DEBUG
161 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
163 openssl_no_config_int();
167 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
168 static int async_inited = 0;
169 static void ossl_init_async(void)
171 #ifdef OPENSSL_INIT_DEBUG
172 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
178 #ifndef OPENSSL_NO_ENGINE
179 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
180 static void ossl_init_engine_openssl(void)
182 # ifdef OPENSSL_INIT_DEBUG
183 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
184 "engine_load_openssl_int()\n");
186 engine_load_openssl_int();
188 # if !defined(OPENSSL_NO_HW) && \
189 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
190 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
191 static void ossl_init_engine_cryptodev(void)
193 # ifdef OPENSSL_INIT_DEBUG
194 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
195 "engine_load_cryptodev_int()\n");
197 engine_load_cryptodev_int();
201 # ifndef OPENSSL_NO_RDRAND
202 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
203 static void ossl_init_engine_rdrand(void)
205 # ifdef OPENSSL_INIT_DEBUG
206 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
207 "engine_load_rdrand_int()\n");
209 engine_load_rdrand_int();
212 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
213 static void ossl_init_engine_dynamic(void)
215 # ifdef OPENSSL_INIT_DEBUG
216 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
217 "engine_load_dynamic_int()\n");
219 engine_load_dynamic_int();
221 # ifndef OPENSSL_NO_STATIC_ENGINE
222 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
223 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
224 static void ossl_init_engine_padlock(void)
226 # ifdef OPENSSL_INIT_DEBUG
227 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
228 "engine_load_padlock_int()\n");
230 engine_load_padlock_int();
233 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
234 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
235 static void ossl_init_engine_capi(void)
237 # ifdef OPENSSL_INIT_DEBUG
238 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
239 "engine_load_capi_int()\n");
241 engine_load_capi_int();
244 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
245 static void ossl_init_engine_dasync(void)
247 # ifdef OPENSSL_INIT_DEBUG
248 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
249 "engine_load_dasync_int()\n");
251 engine_load_dasync_int();
253 # if !defined(OPENSSL_NO_AFALGENG)
254 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
255 static void ossl_init_engine_afalg(void)
257 # ifdef OPENSSL_INIT_DEBUG
258 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
259 "engine_load_afalg_int()\n");
261 engine_load_afalg_int();
267 #ifndef OPENSSL_NO_COMP
268 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
270 static int zlib_inited = 0;
271 static void ossl_init_zlib(void)
273 /* Do nothing - we need to know about this for the later cleanup */
278 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
280 /* Can't do much about this */
285 #ifdef OPENSSL_INIT_DEBUG
286 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
287 "ASYNC_cleanup_thread()\n");
289 ASYNC_cleanup_thread();
292 if (locals->err_state) {
293 #ifdef OPENSSL_INIT_DEBUG
294 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
295 "err_delete_thread_state()\n");
297 err_delete_thread_state();
300 OPENSSL_free(locals);
303 void OPENSSL_thread_stop(void)
305 ossl_init_thread_stop(
306 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
309 int ossl_init_thread_start(uint64_t opts)
311 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
316 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
317 #ifdef OPENSSL_INIT_DEBUG
318 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
319 "marking thread for async\n");
324 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
325 #ifdef OPENSSL_INIT_DEBUG
326 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
327 "marking thread for err_state\n");
329 locals->err_state = 1;
335 void OPENSSL_cleanup(void)
337 OPENSSL_INIT_STOP *currhandler, *lasthandler;
339 /* If we've not been inited then no need to deinit */
343 /* Might be explicitly called and also by atexit */
349 * Thread stop may not get automatically called by the thread library for
350 * the very last thread in some situations, so call it directly.
352 ossl_init_thread_stop(ossl_init_get_thread_local(0));
354 currhandler = stop_handlers;
355 while (currhandler != NULL) {
356 currhandler->handler();
357 lasthandler = currhandler;
358 currhandler = currhandler->next;
359 OPENSSL_free(lasthandler);
361 stop_handlers = NULL;
363 CRYPTO_THREAD_lock_free(init_lock);
366 * We assume we are single-threaded for this function, i.e. no race
367 * conditions for the various "*_inited" vars below.
370 #ifndef OPENSSL_NO_COMP
372 #ifdef OPENSSL_INIT_DEBUG
373 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
374 "comp_zlib_cleanup_int()\n");
376 comp_zlib_cleanup_int();
381 # ifdef OPENSSL_INIT_DEBUG
382 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
388 if (load_crypto_strings_inited) {
389 #ifdef OPENSSL_INIT_DEBUG
390 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
391 "err_free_strings_int()\n");
393 err_free_strings_int();
396 CRYPTO_THREAD_cleanup_local(&threadstopkey);
398 #ifdef OPENSSL_INIT_DEBUG
399 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
400 "rand_cleanup_int()\n");
401 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
402 "conf_modules_free_int()\n");
403 #ifndef OPENSSL_NO_ENGINE
404 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
405 "engine_cleanup_int()\n");
407 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
408 "crypto_cleanup_all_ex_data_int()\n");
409 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
410 "bio_sock_cleanup_int()\n");
411 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
413 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
414 "evp_cleanup_int()\n");
415 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
416 "obj_cleanup_int()\n");
417 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
421 * Note that cleanup order is important:
422 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
423 * must be called before engine_cleanup_int()
424 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
425 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
426 * - conf_modules_free_int() can end up in ENGINE code so must be called
427 * before engine_cleanup_int()
428 * - ENGINEs and additional EVP algorithms might use added OIDs names so
429 * obj_cleanup_int() must be called last
432 conf_modules_free_int();
433 #ifndef OPENSSL_NO_ENGINE
434 engine_cleanup_int();
436 crypto_cleanup_all_ex_data_int();
446 * If this function is called with a non NULL settings value then it must be
447 * called prior to any threads making calls to any OpenSSL functions,
448 * i.e. passing a non-null settings value is assumed to be single-threaded.
450 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
452 static int stoperrset = 0;
457 * We only ever set this once to avoid getting into an infinite
458 * loop where the error system keeps trying to init and fails so
462 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
467 if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
470 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
471 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
472 ossl_init_no_load_crypto_strings))
475 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
476 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
477 ossl_init_load_crypto_strings))
480 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
481 && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
484 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
485 && !CRYPTO_THREAD_run_once(&add_all_ciphers,
486 ossl_init_add_all_ciphers))
489 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
490 && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
493 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
494 && !CRYPTO_THREAD_run_once(&add_all_digests,
495 ossl_init_add_all_digests))
498 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
499 && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
502 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
504 CRYPTO_THREAD_write_lock(init_lock);
505 appname = (settings == NULL) ? NULL : settings->appname;
506 ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
507 CRYPTO_THREAD_unlock(init_lock);
512 if ((opts & OPENSSL_INIT_ASYNC)
513 && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
516 #ifndef OPENSSL_NO_ENGINE
517 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
518 && !CRYPTO_THREAD_run_once(&engine_openssl,
519 ossl_init_engine_openssl))
521 # if !defined(OPENSSL_NO_HW) && \
522 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
523 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
524 && !CRYPTO_THREAD_run_once(&engine_cryptodev,
525 ossl_init_engine_cryptodev))
528 # ifndef OPENSSL_NO_RDRAND
529 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
530 && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
533 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
534 && !CRYPTO_THREAD_run_once(&engine_dynamic,
535 ossl_init_engine_dynamic))
537 # ifndef OPENSSL_NO_STATIC_ENGINE
538 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
539 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
540 && !CRYPTO_THREAD_run_once(&engine_padlock,
541 ossl_init_engine_padlock))
544 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
545 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
546 && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
549 if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
550 && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
552 # if !defined(OPENSSL_NO_AFALGENG)
553 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
554 && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
558 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
559 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
560 | OPENSSL_INIT_ENGINE_AFALG)) {
561 ENGINE_register_all_complete();
565 #ifndef OPENSSL_NO_COMP
566 if ((opts & OPENSSL_INIT_ZLIB)
567 && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
574 int OPENSSL_atexit(void (*handler)(void))
576 OPENSSL_INIT_STOP *newhand;
578 newhand = OPENSSL_malloc(sizeof(*newhand));
582 newhand->handler = handler;
583 newhand->next = stop_handlers;
584 stop_handlers = newhand;