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 #ifndef OPENSSL_NO_ENGINE
67 #include <internal/engine.h>
69 #include <openssl/comp.h>
70 #include <internal/err.h>
74 static int stopped = 0;
76 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
78 /* Implement "once" functionality */
79 #if !defined(OPENSSL_THREADS)
81 static int ossl_init_setup_thread_stop(void)
84 * There are no threads to stop. Do nothing.
89 static void ossl_init_thread_stop_cleanup(void)
93 static struct thread_local_inits_st *local = NULL;
94 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
96 struct thread_local_inits_st *tmp;
100 if (local == NULL && alloc)
101 tmp = local = OPENSSL_zalloc(sizeof(*local));
109 #elif defined(OPENSSL_SYS_WINDOWS)
111 # include <windows.h>
113 static DWORD threadstopkey = TLS_OUT_OF_INDEXES;
115 static int ossl_init_setup_thread_stop(void)
118 * We use a dummy thread local key here. We use the destructor to detect
119 * when the thread is going to stop
121 threadstopkey = TlsAlloc();
122 if (threadstopkey == TLS_OUT_OF_INDEXES)
128 static void ossl_init_thread_stop_cleanup(void)
130 if (threadstopkey != TLS_OUT_OF_INDEXES) {
131 TlsFree(threadstopkey);
135 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
137 struct thread_local_inits_st *local = TlsGetValue(threadstopkey);
139 if (local == NULL && alloc) {
140 local = OPENSSL_zalloc(sizeof *local);
141 TlsSetValue(threadstopkey, local);
144 TlsSetValue(threadstopkey, NULL);
151 # include <pthread.h>
153 static pthread_key_t threadstopkey;
155 static void ossl_init_thread_stop_wrap(void *local)
157 ossl_init_thread_stop((struct thread_local_inits_st *)local);
160 static int ossl_init_setup_thread_stop(void)
163 * We use a dummy thread local key here. We use the destructor to detect
164 * when the thread is going to stop
166 return (pthread_key_create(&threadstopkey,
167 ossl_init_thread_stop_wrap) == 0);
170 static void ossl_init_thread_stop_cleanup(void)
174 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
176 struct thread_local_inits_st *local = pthread_getspecific(threadstopkey);
178 if (local == NULL && alloc) {
179 local = OPENSSL_zalloc(sizeof *local);
180 pthread_setspecific(threadstopkey, local);
183 pthread_setspecific(threadstopkey, NULL);
191 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
192 struct ossl_init_stop_st {
193 void (*handler)(void);
194 OPENSSL_INIT_STOP *next;
197 static OPENSSL_INIT_STOP *stop_handlers = NULL;
199 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
200 static int base_inited = 0;
201 static void ossl_init_base(void)
203 #ifdef OPENSSL_INIT_DEBUG
204 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
206 ossl_init_setup_thread_stop();
207 #ifndef OPENSSL_SYS_UEFI
208 atexit(OPENSSL_cleanup);
210 OPENSSL_cpuid_setup();
214 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
215 static int load_crypto_strings_inited = 0;
216 static void ossl_init_no_load_crypto_strings(void)
218 /* Do nothing in this case */
222 static void ossl_init_load_crypto_strings(void)
225 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
226 * pulling in all the error strings during static linking
228 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
229 # ifdef OPENSSL_INIT_DEBUG
230 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
231 "err_load_crypto_strings_intern()\n");
233 err_load_crypto_strings_intern();
235 load_crypto_strings_inited = 1;
238 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
239 static void ossl_init_add_all_ciphers(void)
242 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
243 * pulling in all the ciphers during static linking
245 #ifndef OPENSSL_NO_AUTOALGINIT
246 # ifdef OPENSSL_INIT_DEBUG
247 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
248 "openssl_add_all_ciphers_internal()\n");
250 openssl_add_all_ciphers_internal();
251 # ifndef OPENSSL_NO_ENGINE
252 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
253 ENGINE_setup_bsd_cryptodev();
259 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
260 static void ossl_init_add_all_digests(void)
263 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
264 * pulling in all the ciphers during static linking
266 #ifndef OPENSSL_NO_AUTOALGINIT
267 # ifdef OPENSSL_INIT_DEBUG
268 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
269 "openssl_add_all_digests_internal()\n");
271 openssl_add_all_digests_internal();
272 # ifndef OPENSSL_NO_ENGINE
273 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
274 ENGINE_setup_bsd_cryptodev();
280 static void ossl_init_no_add_algs(void)
286 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
287 static int config_inited = 0;
288 static const char *config_filename;
289 static void ossl_init_config(void)
291 #ifdef OPENSSL_INIT_DEBUG
293 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
294 config_filename==NULL?"NULL":config_filename);
296 openssl_config_internal(config_filename);
299 static void ossl_init_no_config(void)
301 #ifdef OPENSSL_INIT_DEBUG
303 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
305 openssl_no_config_internal();
309 #ifndef OPENSSL_NO_ASYNC
310 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
311 static int async_inited = 0;
312 static void ossl_init_async(void)
314 #ifdef OPENSSL_INIT_DEBUG
315 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
322 #ifndef OPENSSL_NO_ENGINE
323 static int engine_inited = 0;
324 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
325 static void ossl_init_engine_openssl(void)
327 # ifdef OPENSSL_INIT_DEBUG
328 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
329 "engine_load_openssl_internal()\n");
331 engine_load_openssl_internal();
334 # if !defined(OPENSSL_NO_HW) && \
335 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
336 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
337 static void ossl_init_engine_cryptodev(void)
339 # ifdef OPENSSL_INIT_DEBUG
340 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
341 "engine_load_cryptodev_internal()\n");
343 engine_load_cryptodev_internal();
348 # ifndef OPENSSL_NO_RDRAND
349 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
350 static void ossl_init_engine_rdrand(void)
352 # ifdef OPENSSL_INIT_DEBUG
353 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
354 "engine_load_rdrand_internal()\n");
356 engine_load_rdrand_internal();
360 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
361 static void ossl_init_engine_dynamic(void)
363 # ifdef OPENSSL_INIT_DEBUG
364 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
365 "engine_load_dynamic_internal()\n");
367 engine_load_dynamic_internal();
370 # ifndef OPENSSL_NO_STATIC_ENGINE
371 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
372 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
373 static void ossl_init_engine_padlock(void)
375 # ifdef OPENSSL_INIT_DEBUG
376 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
377 "engine_load_padlock_internal()\n");
379 engine_load_padlock_internal();
383 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
384 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
385 static void ossl_init_engine_capi(void)
387 # ifdef OPENSSL_INIT_DEBUG
388 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
389 "engine_load_capi_internal()\n");
391 engine_load_capi_internal();
395 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
396 static void ossl_init_engine_dasync(void)
398 # ifdef OPENSSL_INIT_DEBUG
399 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
400 "engine_load_dasync_internal()\n");
402 engine_load_dasync_internal();
405 # if !defined(OPENSSL_NO_AFALGENG)
406 static OPENSSL_INIT_ONCE engine_afalg = OPENSSL_INIT_ONCE_STATIC_INIT;
407 static void ossl_init_engine_afalg(void)
409 # ifdef OPENSSL_INIT_DEBUG
410 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
411 "engine_load_afalg_internal()\n");
413 engine_load_afalg_internal();
420 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
421 static int zlib_inited = 0;
422 static void ossl_init_zlib(void)
424 /* Do nothing - we need to know about this for the later cleanup */
428 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
430 /* Can't do much about this */
434 #ifndef OPENSSL_NO_ASYNC
436 #ifdef OPENSSL_INIT_DEBUG
437 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
438 "ASYNC_cleanup_thread()\n");
440 ASYNC_cleanup_thread();
444 if (locals->err_state) {
445 #ifdef OPENSSL_INIT_DEBUG
446 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
447 "ERR_remove_thread_state(NULL)\n");
449 ERR_remove_thread_state(NULL);
452 OPENSSL_free(locals);
455 void OPENSSL_thread_stop(void)
457 ossl_init_thread_stop(
458 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
461 int ossl_init_thread_start(uint64_t opts)
463 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
468 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
469 #ifdef OPENSSL_INIT_DEBUG
470 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
471 "marking thread for async\n");
476 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
477 #ifdef OPENSSL_INIT_DEBUG
478 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
479 "marking thread for err_state\n");
481 locals->err_state = 1;
487 void OPENSSL_cleanup(void)
489 OPENSSL_INIT_STOP *currhandler, *lasthandler;
491 /* If we've not been inited then no need to deinit */
495 /* Might be explicitly called and also by atexit */
501 * Thread stop may not get automatically called by the thread library for
502 * the very last thread in some situations, so call it directly.
504 ossl_init_thread_stop(ossl_init_get_thread_local(0));
506 currhandler = stop_handlers;
507 while (currhandler != NULL) {
508 currhandler->handler();
509 lasthandler = currhandler;
510 currhandler = currhandler->next;
511 OPENSSL_free(lasthandler);
513 stop_handlers = NULL;
515 * We assume we are single-threaded for this function, i.e. no race
516 * conditions for the various "*_inited" vars below.
520 #ifdef OPENSSL_INIT_DEBUG
521 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
522 "COMP_zlib_cleanup()\n");
527 #ifndef OPENSSL_NO_ASYNC
529 # ifdef OPENSSL_INIT_DEBUG
530 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
537 #ifndef OPENSSL_NO_ENGINE
539 # ifdef OPENSSL_INIT_DEBUG
540 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
541 "ENGINE_cleanup()\n");
547 if (load_crypto_strings_inited) {
548 #ifdef OPENSSL_INIT_DEBUG
549 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
550 "ERR_free_strings()\n");
555 ossl_init_thread_stop_cleanup();
557 #ifdef OPENSSL_INIT_DEBUG
558 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
559 "CRYPTO_cleanup_all_ex_data()\n");
560 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
562 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
563 "CONF_modules_free()\n");
564 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
567 CRYPTO_cleanup_all_ex_data();
575 * If this function is called with a non NULL settings value then it must be
576 * called prior to any threads making calls to any OpenSSL functions,
577 * i.e. passing a non-null settings value is assumed to be single-threaded.
579 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
581 static int stoperrset = 0;
586 * We only ever set this once to avoid getting into an infinite
587 * loop where the error system keeps trying to init and fails so
591 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
596 if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
599 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
600 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
601 ossl_init_no_load_crypto_strings))
604 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
605 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
606 ossl_init_load_crypto_strings))
609 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
610 && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
613 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
614 && !CRYPTO_THREAD_run_once(&add_all_ciphers,
615 ossl_init_add_all_ciphers))
618 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
619 && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
622 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
623 && !CRYPTO_THREAD_run_once(&add_all_digests,
624 ossl_init_add_all_digests))
627 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
628 && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
631 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
633 CRYPTO_w_lock(CRYPTO_LOCK_INIT);
634 config_filename = (settings == NULL) ? NULL : settings->config_name;
635 ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
636 CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
641 #ifndef OPENSSL_NO_ASYNC
642 if ((opts & OPENSSL_INIT_ASYNC)
643 && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
646 #ifndef OPENSSL_NO_ENGINE
647 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
648 && !CRYPTO_THREAD_run_once(&engine_openssl,
649 ossl_init_engine_openssl))
651 # if !defined(OPENSSL_NO_HW) && \
652 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
653 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
654 && !CRYPTO_THREAD_run_once(&engine_cryptodev,
655 ossl_init_engine_cryptodev))
658 # ifndef OPENSSL_NO_RDRAND
659 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
660 && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
663 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
664 && !CRYPTO_THREAD_run_once(&engine_dynamic,
665 ossl_init_engine_dynamic))
667 # ifndef OPENSSL_NO_STATIC_ENGINE
668 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
669 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
670 && CRYPTO_THREAD_run_once(&engine_padlock,
671 ossl_init_engine_padlock))
674 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
675 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
676 && CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
679 if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
680 && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
682 # if !defined(OPENSSL_NO_AFALGENG)
683 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
684 && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
688 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
689 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
690 | OPENSSL_INIT_ENGINE_AFALG)) {
691 ENGINE_register_all_complete();
695 if ((opts & OPENSSL_INIT_ZLIB)
696 && CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
702 int OPENSSL_atexit(void (*handler)(void))
704 OPENSSL_INIT_STOP *newhand;
706 newhand = OPENSSL_malloc(sizeof(*newhand));
710 newhand->handler = handler;
711 newhand->next = stop_handlers;
712 stop_handlers = newhand;