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)
97 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
98 * pulling in all the error strings during static linking
100 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
101 # ifdef OPENSSL_INIT_DEBUG
102 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
103 "err_load_crypto_strings_int()\n");
105 ret = err_load_crypto_strings_int();
107 load_crypto_strings_inited = 1;
111 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
112 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
115 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
116 * pulling in all the ciphers during static linking
118 #ifndef OPENSSL_NO_AUTOALGINIT
119 # ifdef OPENSSL_INIT_DEBUG
120 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
121 "openssl_add_all_ciphers_int()\n");
123 openssl_add_all_ciphers_int();
128 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
129 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
132 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
133 * pulling in all the ciphers during static linking
135 #ifndef OPENSSL_NO_AUTOALGINIT
136 # ifdef OPENSSL_INIT_DEBUG
137 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
138 "openssl_add_all_digests()\n");
140 openssl_add_all_digests_int();
145 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
151 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
152 static int config_inited = 0;
153 static const char *appname;
154 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
156 #ifdef OPENSSL_INIT_DEBUG
158 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
159 appname == NULL ? "NULL" : appname);
161 openssl_config_int(appname);
165 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
167 #ifdef OPENSSL_INIT_DEBUG
169 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
171 openssl_no_config_int();
176 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
177 static int async_inited = 0;
178 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
180 #ifdef OPENSSL_INIT_DEBUG
181 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
189 #ifndef OPENSSL_NO_ENGINE
190 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
191 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
193 # ifdef OPENSSL_INIT_DEBUG
194 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
195 "engine_load_openssl_int()\n");
197 engine_load_openssl_int();
200 # if !defined(OPENSSL_NO_HW) && \
201 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
202 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
203 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
205 # ifdef OPENSSL_INIT_DEBUG
206 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
207 "engine_load_cryptodev_int()\n");
209 engine_load_cryptodev_int();
214 # ifndef OPENSSL_NO_RDRAND
215 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
216 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
218 # ifdef OPENSSL_INIT_DEBUG
219 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
220 "engine_load_rdrand_int()\n");
222 engine_load_rdrand_int();
226 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
227 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
229 # ifdef OPENSSL_INIT_DEBUG
230 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
231 "engine_load_dynamic_int()\n");
233 engine_load_dynamic_int();
236 # ifndef OPENSSL_NO_STATIC_ENGINE
237 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
238 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
239 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
241 # ifdef OPENSSL_INIT_DEBUG
242 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
243 "engine_load_padlock_int()\n");
245 engine_load_padlock_int();
249 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
250 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
251 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
253 # ifdef OPENSSL_INIT_DEBUG
254 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
255 "engine_load_capi_int()\n");
257 engine_load_capi_int();
261 # if !defined(OPENSSL_NO_AFALGENG)
262 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
263 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
265 # ifdef OPENSSL_INIT_DEBUG
266 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
267 "engine_load_afalg_int()\n");
269 engine_load_afalg_int();
276 #ifndef OPENSSL_NO_COMP
277 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
279 static int zlib_inited = 0;
280 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
282 /* Do nothing - we need to know about this for the later cleanup */
288 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
290 /* Can't do much about this */
295 #ifdef OPENSSL_INIT_DEBUG
296 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
297 "ASYNC_cleanup_thread()\n");
299 ASYNC_cleanup_thread();
302 if (locals->err_state) {
303 #ifdef OPENSSL_INIT_DEBUG
304 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
305 "err_delete_thread_state()\n");
307 err_delete_thread_state();
310 OPENSSL_free(locals);
313 void OPENSSL_thread_stop(void)
315 ossl_init_thread_stop(
316 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
319 int ossl_init_thread_start(uint64_t opts)
321 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
326 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
327 #ifdef OPENSSL_INIT_DEBUG
328 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
329 "marking thread for async\n");
334 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
335 #ifdef OPENSSL_INIT_DEBUG
336 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
337 "marking thread for err_state\n");
339 locals->err_state = 1;
345 void OPENSSL_cleanup(void)
347 OPENSSL_INIT_STOP *currhandler, *lasthandler;
349 /* If we've not been inited then no need to deinit */
353 /* Might be explicitly called and also by atexit */
359 * Thread stop may not get automatically called by the thread library for
360 * the very last thread in some situations, so call it directly.
362 ossl_init_thread_stop(ossl_init_get_thread_local(0));
364 currhandler = stop_handlers;
365 while (currhandler != NULL) {
366 currhandler->handler();
367 lasthandler = currhandler;
368 currhandler = currhandler->next;
369 OPENSSL_free(lasthandler);
371 stop_handlers = NULL;
373 CRYPTO_THREAD_lock_free(init_lock);
376 * We assume we are single-threaded for this function, i.e. no race
377 * conditions for the various "*_inited" vars below.
380 #ifndef OPENSSL_NO_COMP
382 #ifdef OPENSSL_INIT_DEBUG
383 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
384 "comp_zlib_cleanup_int()\n");
386 comp_zlib_cleanup_int();
391 # ifdef OPENSSL_INIT_DEBUG
392 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
398 if (load_crypto_strings_inited) {
399 #ifdef OPENSSL_INIT_DEBUG
400 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
401 "err_free_strings_int()\n");
403 err_free_strings_int();
406 CRYPTO_THREAD_cleanup_local(&threadstopkey);
408 #ifdef OPENSSL_INIT_DEBUG
409 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
410 "rand_cleanup_int()\n");
411 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
412 "conf_modules_free_int()\n");
413 #ifndef OPENSSL_NO_ENGINE
414 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
415 "engine_cleanup_int()\n");
417 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
418 "crypto_cleanup_all_ex_data_int()\n");
419 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
420 "bio_sock_cleanup_int()\n");
421 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
423 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
424 "evp_cleanup_int()\n");
425 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
426 "obj_cleanup_int()\n");
427 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
431 * Note that cleanup order is important:
432 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
433 * must be called before engine_cleanup_int()
434 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
435 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
436 * - conf_modules_free_int() can end up in ENGINE code so must be called
437 * before engine_cleanup_int()
438 * - ENGINEs and additional EVP algorithms might use added OIDs names so
439 * obj_cleanup_int() must be called last
442 conf_modules_free_int();
443 #ifndef OPENSSL_NO_ENGINE
444 engine_cleanup_int();
446 crypto_cleanup_all_ex_data_int();
456 * If this function is called with a non NULL settings value then it must be
457 * called prior to any threads making calls to any OpenSSL functions,
458 * i.e. passing a non-null settings value is assumed to be single-threaded.
460 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
462 static int stoperrset = 0;
467 * We only ever set this once to avoid getting into an infinite
468 * loop where the error system keeps trying to init and fails so
472 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
477 if (!RUN_ONCE(&base, ossl_init_base))
480 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
481 && !RUN_ONCE(&load_crypto_strings,
482 ossl_init_no_load_crypto_strings))
485 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
486 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
489 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
490 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
493 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
494 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
497 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
498 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
501 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
502 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
505 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
506 && !RUN_ONCE(&config, ossl_init_no_config))
509 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
511 CRYPTO_THREAD_write_lock(init_lock);
512 appname = (settings == NULL) ? NULL : settings->appname;
513 ret = RUN_ONCE(&config, ossl_init_config);
514 CRYPTO_THREAD_unlock(init_lock);
519 if ((opts & OPENSSL_INIT_ASYNC)
520 && !RUN_ONCE(&async, ossl_init_async))
523 #ifndef OPENSSL_NO_ENGINE
524 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
525 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
527 # if !defined(OPENSSL_NO_HW) && \
528 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
529 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
530 && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
533 # ifndef OPENSSL_NO_RDRAND
534 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
535 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
538 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
539 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
541 # ifndef OPENSSL_NO_STATIC_ENGINE
542 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
543 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
544 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
547 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
548 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
549 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
552 # if !defined(OPENSSL_NO_AFALGENG)
553 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
554 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
558 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
559 | 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 && !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;