2 * Written by Matt Caswell for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2016 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
58 #include <internal/threads.h>
59 #include <internal/cryptlib_int.h>
60 #include <openssl/err.h>
61 #include <openssl/rand.h>
62 #include <openssl/evp.h>
63 #include <internal/evp_int.h>
64 #include <internal/conf.h>
65 #include <internal/async.h>
66 #include <internal/engine.h>
67 #include <internal/comp.h>
68 #include <internal/err.h>
72 static int stopped = 0;
74 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
76 static CRYPTO_THREAD_LOCAL threadstopkey;
78 static void ossl_init_thread_stop_wrap(void *local)
80 ossl_init_thread_stop((struct thread_local_inits_st *)local);
83 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
85 struct thread_local_inits_st *local =
86 CRYPTO_THREAD_get_local(&threadstopkey);
88 if (local == NULL && alloc) {
89 local = OPENSSL_zalloc(sizeof *local);
90 CRYPTO_THREAD_set_local(&threadstopkey, local);
93 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
99 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
100 struct ossl_init_stop_st {
101 void (*handler)(void);
102 OPENSSL_INIT_STOP *next;
105 static OPENSSL_INIT_STOP *stop_handlers = NULL;
106 static CRYPTO_RWLOCK *init_lock = NULL;
108 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
109 static int base_inited = 0;
110 static void ossl_init_base(void)
112 #ifdef OPENSSL_INIT_DEBUG
113 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
116 * We use a dummy thread local key here. We use the destructor to detect
117 * when the thread is going to stop (where that feature is available)
119 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
120 #ifndef OPENSSL_SYS_UEFI
121 atexit(OPENSSL_cleanup);
123 init_lock = CRYPTO_THREAD_lock_new();
124 OPENSSL_cpuid_setup();
128 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
129 static int load_crypto_strings_inited = 0;
130 static void ossl_init_no_load_crypto_strings(void)
132 /* Do nothing in this case */
136 static void ossl_init_load_crypto_strings(void)
139 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
140 * pulling in all the error strings during static linking
142 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
143 # ifdef OPENSSL_INIT_DEBUG
144 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
145 "err_load_crypto_strings_intern()\n");
147 err_load_crypto_strings_intern();
149 load_crypto_strings_inited = 1;
152 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
153 static void ossl_init_add_all_ciphers(void)
156 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
157 * pulling in all the ciphers during static linking
159 #ifndef OPENSSL_NO_AUTOALGINIT
160 # ifdef OPENSSL_INIT_DEBUG
161 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
162 "openssl_add_all_ciphers_internal()\n");
164 openssl_add_all_ciphers_internal();
165 # ifndef OPENSSL_NO_ENGINE
166 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
167 ENGINE_setup_bsd_cryptodev();
173 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
174 static void ossl_init_add_all_digests(void)
177 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
178 * pulling in all the ciphers during static linking
180 #ifndef OPENSSL_NO_AUTOALGINIT
181 # ifdef OPENSSL_INIT_DEBUG
182 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
183 "openssl_add_all_digests_internal()\n");
185 openssl_add_all_digests_internal();
186 # ifndef OPENSSL_NO_ENGINE
187 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
188 ENGINE_setup_bsd_cryptodev();
194 static void ossl_init_no_add_algs(void)
200 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
201 static int config_inited = 0;
202 static const char *config_filename;
203 static void ossl_init_config(void)
205 #ifdef OPENSSL_INIT_DEBUG
207 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
208 config_filename==NULL?"NULL":config_filename);
210 openssl_config_internal(config_filename);
213 static void ossl_init_no_config(void)
215 #ifdef OPENSSL_INIT_DEBUG
217 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
219 openssl_no_config_internal();
223 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
224 static int async_inited = 0;
225 static void ossl_init_async(void)
227 #ifdef OPENSSL_INIT_DEBUG
228 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
234 #ifndef OPENSSL_NO_ENGINE
235 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
236 static void ossl_init_engine_openssl(void)
238 # ifdef OPENSSL_INIT_DEBUG
239 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
240 "engine_load_openssl_internal()\n");
242 engine_load_openssl_internal();
244 # if !defined(OPENSSL_NO_HW) && \
245 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
246 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
247 static void ossl_init_engine_cryptodev(void)
249 # ifdef OPENSSL_INIT_DEBUG
250 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
251 "engine_load_cryptodev_internal()\n");
253 engine_load_cryptodev_internal();
257 # ifndef OPENSSL_NO_RDRAND
258 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
259 static void ossl_init_engine_rdrand(void)
261 # ifdef OPENSSL_INIT_DEBUG
262 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
263 "engine_load_rdrand_internal()\n");
265 engine_load_rdrand_internal();
268 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
269 static void ossl_init_engine_dynamic(void)
271 # ifdef OPENSSL_INIT_DEBUG
272 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
273 "engine_load_dynamic_internal()\n");
275 engine_load_dynamic_internal();
277 # ifndef OPENSSL_NO_STATIC_ENGINE
278 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
279 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
280 static void ossl_init_engine_padlock(void)
282 # ifdef OPENSSL_INIT_DEBUG
283 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
284 "engine_load_padlock_internal()\n");
286 engine_load_padlock_internal();
289 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
290 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
291 static void ossl_init_engine_capi(void)
293 # ifdef OPENSSL_INIT_DEBUG
294 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
295 "engine_load_capi_internal()\n");
297 engine_load_capi_internal();
300 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
301 static void ossl_init_engine_dasync(void)
303 # ifdef OPENSSL_INIT_DEBUG
304 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
305 "engine_load_dasync_internal()\n");
307 engine_load_dasync_internal();
309 # if !defined(OPENSSL_NO_AFALGENG)
310 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
311 static void ossl_init_engine_afalg(void)
313 # ifdef OPENSSL_INIT_DEBUG
314 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
315 "engine_load_afalg_internal()\n");
317 engine_load_afalg_internal();
323 #ifndef OPENSSL_NO_COMP
324 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
326 static int zlib_inited = 0;
327 static void ossl_init_zlib(void)
329 /* Do nothing - we need to know about this for the later cleanup */
334 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
336 /* Can't do much about this */
341 #ifdef OPENSSL_INIT_DEBUG
342 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
343 "ASYNC_cleanup_thread()\n");
345 ASYNC_cleanup_thread();
348 if (locals->err_state) {
349 #ifdef OPENSSL_INIT_DEBUG
350 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
351 "ERR_remove_thread_state()\n");
353 ERR_remove_thread_state();
356 OPENSSL_free(locals);
359 void OPENSSL_thread_stop(void)
361 ossl_init_thread_stop(
362 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
365 int ossl_init_thread_start(uint64_t opts)
367 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
372 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
373 #ifdef OPENSSL_INIT_DEBUG
374 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
375 "marking thread for async\n");
380 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
381 #ifdef OPENSSL_INIT_DEBUG
382 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
383 "marking thread for err_state\n");
385 locals->err_state = 1;
391 void OPENSSL_cleanup(void)
393 OPENSSL_INIT_STOP *currhandler, *lasthandler;
395 /* If we've not been inited then no need to deinit */
399 /* Might be explicitly called and also by atexit */
405 * Thread stop may not get automatically called by the thread library for
406 * the very last thread in some situations, so call it directly.
408 ossl_init_thread_stop(ossl_init_get_thread_local(0));
410 currhandler = stop_handlers;
411 while (currhandler != NULL) {
412 currhandler->handler();
413 lasthandler = currhandler;
414 currhandler = currhandler->next;
415 OPENSSL_free(lasthandler);
417 stop_handlers = NULL;
419 CRYPTO_THREAD_lock_free(init_lock);
422 * We assume we are single-threaded for this function, i.e. no race
423 * conditions for the various "*_inited" vars below.
426 #ifndef OPENSSL_NO_COMP
428 #ifdef OPENSSL_INIT_DEBUG
429 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
430 "comp_zlib_cleanup_internal()\n");
432 comp_zlib_cleanup_internal();
437 # ifdef OPENSSL_INIT_DEBUG
438 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
444 if (load_crypto_strings_inited) {
445 #ifdef OPENSSL_INIT_DEBUG
446 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
447 "err_free_strings_intern()\n");
449 err_free_strings_intern();
452 CRYPTO_THREAD_cleanup_local(&threadstopkey);
454 #ifdef OPENSSL_INIT_DEBUG
455 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
457 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
458 "CONF_modules_free()\n");
459 #ifndef OPENSSL_NO_ENGINE
460 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
461 "ENGINE_cleanup()\n");
463 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
464 "CRYPTO_cleanup_all_ex_data()\n");
465 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
466 "BIO_sock_cleanup()\n");
467 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
469 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
473 * Note that cleanup order is important:
474 * - RAND_cleanup could call an ENINGE's RAND cleanup function so must be
475 * called before ENGINE_cleanup()
476 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
477 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
478 * - CONF_modules_free() can end up in ENGINE code so must be called before
483 #ifndef OPENSSL_NO_ENGINE
486 CRYPTO_cleanup_all_ex_data();
487 #ifndef OPENSSL_NO_SOCK
496 * If this function is called with a non NULL settings value then it must be
497 * called prior to any threads making calls to any OpenSSL functions,
498 * i.e. passing a non-null settings value is assumed to be single-threaded.
500 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
502 static int stoperrset = 0;
507 * We only ever set this once to avoid getting into an infinite
508 * loop where the error system keeps trying to init and fails so
512 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
517 if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
520 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
521 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
522 ossl_init_no_load_crypto_strings))
525 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
526 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
527 ossl_init_load_crypto_strings))
530 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
531 && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
534 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
535 && !CRYPTO_THREAD_run_once(&add_all_ciphers,
536 ossl_init_add_all_ciphers))
539 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
540 && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
543 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
544 && !CRYPTO_THREAD_run_once(&add_all_digests,
545 ossl_init_add_all_digests))
548 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
549 && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
552 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
554 CRYPTO_THREAD_write_lock(init_lock);
555 config_filename = (settings == NULL) ? NULL : settings->config_name;
556 ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
557 CRYPTO_THREAD_unlock(init_lock);
562 if ((opts & OPENSSL_INIT_ASYNC)
563 && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
566 #ifndef OPENSSL_NO_ENGINE
567 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
568 && !CRYPTO_THREAD_run_once(&engine_openssl,
569 ossl_init_engine_openssl))
571 # if !defined(OPENSSL_NO_HW) && \
572 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
573 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
574 && !CRYPTO_THREAD_run_once(&engine_cryptodev,
575 ossl_init_engine_cryptodev))
578 # ifndef OPENSSL_NO_RDRAND
579 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
580 && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
583 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
584 && !CRYPTO_THREAD_run_once(&engine_dynamic,
585 ossl_init_engine_dynamic))
587 # ifndef OPENSSL_NO_STATIC_ENGINE
588 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
589 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
590 && !CRYPTO_THREAD_run_once(&engine_padlock,
591 ossl_init_engine_padlock))
594 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
595 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
596 && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
599 if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
600 && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
602 # if !defined(OPENSSL_NO_AFALGENG)
603 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
604 && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
608 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
609 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
610 | OPENSSL_INIT_ENGINE_AFALG)) {
611 ENGINE_register_all_complete();
615 #ifndef OPENSSL_NO_COMP
616 if ((opts & OPENSSL_INIT_ZLIB)
617 && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
624 int OPENSSL_atexit(void (*handler)(void))
626 OPENSSL_INIT_STOP *newhand;
628 newhand = OPENSSL_malloc(sizeof(*newhand));
632 newhand->handler = handler;
633 newhand->next = stop_handlers;
634 stop_handlers = newhand;