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/cryptlib_int.h>
59 #include <openssl/err.h>
60 #include <openssl/rand.h>
61 #include <openssl/evp.h>
62 #include <internal/evp_int.h>
63 #include <internal/conf.h>
64 #include <internal/async.h>
65 #ifndef OPENSSL_NO_ENGINE
66 #include <internal/engine.h>
68 #include <openssl/comp.h>
69 #include <internal/err.h>
73 static int stopped = 0;
75 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
77 /* Implement "once" functionality */
78 #if !defined(OPENSSL_THREADS)
79 typedef int OPENSSL_INIT_ONCE;
80 # define OPENSSL_INIT_ONCE_STATIC_INIT 0
82 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
84 if (*once == OPENSSL_INIT_ONCE_STATIC_INIT) {
90 static int ossl_init_setup_thread_stop(void)
93 * There are no threads to stop. Do nothing.
98 static void ossl_init_thread_stop_cleanup(void)
102 static struct thread_local_inits_st *local = NULL;
103 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
105 struct thread_local_inits_st *tmp;
109 if (local == NULL && alloc)
110 tmp = local = OPENSSL_zalloc(sizeof(*local));
118 #elif defined(OPENSSL_SYS_WINDOWS)
120 # include <windows.h>
122 # if _WIN32_WINNT < 0x0600
125 * Versions before 0x0600 (Windows Vista, Windows Server 2008 or later) do not
126 * have InitOnceExecuteOnce, so we fall back to using a spinlock instead.
128 typedef LONG OPENSSL_INIT_ONCE;
129 # define OPENSSL_INIT_ONCE_STATIC_INIT 0
131 # define ONCE_UNINITED 0
132 # define ONCE_ININIT 1
135 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
137 LONG volatile *lock = (LONG *)once;
140 if (*lock == ONCE_DONE)
144 result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
145 if (result == ONCE_UNINITED) {
150 } while (result == ONCE_ININIT);
155 typedef INIT_ONCE OPENSSL_INIT_ONCE;
156 # define OPENSSL_INIT_ONCE_STATIC_INIT INIT_ONCE_STATIC_INIT
158 static BOOL CALLBACK once_cb(PINIT_ONCE once, PVOID initfp, PVOID *unused)
160 void (*init)(void) = initfp;
167 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
169 InitOnceExecuteOnce((INIT_ONCE *)once, once_cb, init, NULL);
173 static DWORD threadstopkey = TLS_OUT_OF_INDEXES;
175 static int ossl_init_setup_thread_stop(void)
178 * We use a dummy thread local key here. We use the destructor to detect
179 * when the thread is going to stop
181 threadstopkey = TlsAlloc();
182 if (threadstopkey == TLS_OUT_OF_INDEXES)
188 static void ossl_init_thread_stop_cleanup(void)
190 if (threadstopkey != TLS_OUT_OF_INDEXES) {
191 TlsFree(threadstopkey);
195 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
197 struct thread_local_inits_st *local = TlsGetValue(threadstopkey);
199 if (local == NULL && alloc) {
200 local = OPENSSL_zalloc(sizeof *local);
201 TlsSetValue(threadstopkey, local);
204 TlsSetValue(threadstopkey, NULL);
211 # include <pthread.h>
213 static pthread_key_t threadstopkey;
215 typedef pthread_once_t OPENSSL_INIT_ONCE;
216 # define OPENSSL_INIT_ONCE_STATIC_INIT PTHREAD_ONCE_INIT
218 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
220 pthread_once(once, init);
223 static void ossl_init_thread_stop_wrap(void *local)
225 ossl_init_thread_stop((struct thread_local_inits_st *)local);
228 static int ossl_init_setup_thread_stop(void)
231 * We use a dummy thread local key here. We use the destructor to detect
232 * when the thread is going to stop
234 return (pthread_key_create(&threadstopkey,
235 ossl_init_thread_stop_wrap) == 0);
238 static void ossl_init_thread_stop_cleanup(void)
242 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
244 struct thread_local_inits_st *local = pthread_getspecific(threadstopkey);
246 if (local == NULL && alloc) {
247 local = OPENSSL_zalloc(sizeof *local);
248 pthread_setspecific(threadstopkey, local);
251 pthread_setspecific(threadstopkey, NULL);
259 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
260 struct ossl_init_stop_st {
261 void (*handler)(void);
262 OPENSSL_INIT_STOP *next;
265 static OPENSSL_INIT_STOP *stop_handlers = NULL;
267 static OPENSSL_INIT_ONCE base = OPENSSL_INIT_ONCE_STATIC_INIT;
268 static int base_inited = 0;
269 static void ossl_init_base(void)
271 #ifdef OPENSSL_INIT_DEBUG
272 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
274 ossl_init_setup_thread_stop();
275 #ifndef OPENSSL_SYS_UEFI
276 atexit(OPENSSL_cleanup);
278 OPENSSL_cpuid_setup();
282 static OPENSSL_INIT_ONCE load_crypto_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
283 static int load_crypto_strings_inited = 0;
284 static void ossl_init_no_load_crypto_strings(void)
286 /* Do nothing in this case */
290 static void ossl_init_load_crypto_strings(void)
293 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
294 * pulling in all the error strings during static linking
296 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
297 # ifdef OPENSSL_INIT_DEBUG
298 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
299 "err_load_crypto_strings_intern()\n");
301 err_load_crypto_strings_intern();
303 load_crypto_strings_inited = 1;
306 static OPENSSL_INIT_ONCE add_all_ciphers = OPENSSL_INIT_ONCE_STATIC_INIT;
307 static void ossl_init_add_all_ciphers(void)
310 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
311 * pulling in all the ciphers during static linking
313 #ifndef OPENSSL_NO_AUTOALGINIT
314 # ifdef OPENSSL_INIT_DEBUG
315 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
316 "openssl_add_all_ciphers_internal()\n");
318 openssl_add_all_ciphers_internal();
319 # ifndef OPENSSL_NO_ENGINE
320 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
321 ENGINE_setup_bsd_cryptodev();
327 static OPENSSL_INIT_ONCE add_all_digests = OPENSSL_INIT_ONCE_STATIC_INIT;
328 static void ossl_init_add_all_digests(void)
331 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
332 * pulling in all the ciphers during static linking
334 #ifndef OPENSSL_NO_AUTOALGINIT
335 # ifdef OPENSSL_INIT_DEBUG
336 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
337 "openssl_add_all_digests_internal()\n");
339 openssl_add_all_digests_internal();
340 # ifndef OPENSSL_NO_ENGINE
341 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
342 ENGINE_setup_bsd_cryptodev();
348 static void ossl_init_no_add_algs(void)
354 static OPENSSL_INIT_ONCE config = OPENSSL_INIT_ONCE_STATIC_INIT;
355 static int config_inited = 0;
356 static const char *config_filename;
357 static void ossl_init_config(void)
359 #ifdef OPENSSL_INIT_DEBUG
361 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
362 config_filename==NULL?"NULL":config_filename);
364 openssl_config_internal(config_filename);
367 static void ossl_init_no_config(void)
369 #ifdef OPENSSL_INIT_DEBUG
371 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
373 openssl_no_config_internal();
377 #ifndef OPENSSL_NO_ASYNC
378 static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
379 static int async_inited = 0;
380 static void ossl_init_async(void)
382 #ifdef OPENSSL_INIT_DEBUG
383 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
390 #ifndef OPENSSL_NO_ENGINE
391 static int engine_inited = 0;
392 static OPENSSL_INIT_ONCE engine_openssl = OPENSSL_INIT_ONCE_STATIC_INIT;
393 static void ossl_init_engine_openssl(void)
395 # ifdef OPENSSL_INIT_DEBUG
396 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
397 "engine_load_openssl_internal()\n");
399 engine_load_openssl_internal();
402 # if !defined(OPENSSL_NO_HW) && \
403 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
404 static OPENSSL_INIT_ONCE engine_cryptodev = OPENSSL_INIT_ONCE_STATIC_INIT;
405 static void ossl_init_engine_cryptodev(void)
407 # ifdef OPENSSL_INIT_DEBUG
408 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
409 "engine_load_cryptodev_internal()\n");
411 engine_load_cryptodev_internal();
416 # ifndef OPENSSL_NO_RDRAND
417 static OPENSSL_INIT_ONCE engine_rdrand = OPENSSL_INIT_ONCE_STATIC_INIT;
418 static void ossl_init_engine_rdrand(void)
420 # ifdef OPENSSL_INIT_DEBUG
421 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
422 "engine_load_rdrand_internal()\n");
424 engine_load_rdrand_internal();
428 static OPENSSL_INIT_ONCE engine_dynamic = OPENSSL_INIT_ONCE_STATIC_INIT;
429 static void ossl_init_engine_dynamic(void)
431 # ifdef OPENSSL_INIT_DEBUG
432 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
433 "engine_load_dynamic_internal()\n");
435 engine_load_dynamic_internal();
438 # ifndef OPENSSL_NO_STATIC_ENGINE
439 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
440 static OPENSSL_INIT_ONCE engine_padlock = OPENSSL_INIT_ONCE_STATIC_INIT;
441 static void ossl_init_engine_padlock(void)
443 # ifdef OPENSSL_INIT_DEBUG
444 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
445 "engine_load_padlock_internal()\n");
447 engine_load_padlock_internal();
451 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
452 static OPENSSL_INIT_ONCE engine_capi = OPENSSL_INIT_ONCE_STATIC_INIT;
453 static void ossl_init_engine_capi(void)
455 # ifdef OPENSSL_INIT_DEBUG
456 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
457 "engine_load_capi_internal()\n");
459 engine_load_capi_internal();
463 static OPENSSL_INIT_ONCE engine_dasync = OPENSSL_INIT_ONCE_STATIC_INIT;
464 static void ossl_init_engine_dasync(void)
466 # ifdef OPENSSL_INIT_DEBUG
467 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
468 "engine_load_dasync_internal()\n");
470 engine_load_dasync_internal();
476 static OPENSSL_INIT_ONCE zlib = OPENSSL_INIT_ONCE_STATIC_INIT;
477 static int zlib_inited = 0;
478 static void ossl_init_zlib(void)
480 /* Do nothing - we need to know about this for the later cleanup */
484 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
486 /* Can't do much about this */
490 #ifndef OPENSSL_NO_ASYNC
492 #ifdef OPENSSL_INIT_DEBUG
493 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
494 "ASYNC_cleanup_thread()\n");
496 ASYNC_cleanup_thread();
500 if (locals->err_state) {
501 #ifdef OPENSSL_INIT_DEBUG
502 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
503 "ERR_remove_thread_state(NULL)\n");
505 ERR_remove_thread_state(NULL);
508 OPENSSL_free(locals);
511 void OPENSSL_thread_stop(void)
513 ossl_init_thread_stop(
514 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
517 int ossl_init_thread_start(uint64_t opts)
519 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
524 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
525 #ifdef OPENSSL_INIT_DEBUG
526 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
527 "marking thread for async\n");
532 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
533 #ifdef OPENSSL_INIT_DEBUG
534 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
535 "marking thread for err_state\n");
537 locals->err_state = 1;
543 void OPENSSL_cleanup(void)
545 OPENSSL_INIT_STOP *currhandler, *lasthandler;
547 /* If we've not been inited then no need to deinit */
551 /* Might be explicitly called and also by atexit */
557 * Thread stop may not get automatically called by the thread library for
558 * the very last thread in some situations, so call it directly.
560 ossl_init_thread_stop(ossl_init_get_thread_local(0));
562 currhandler = stop_handlers;
563 while (currhandler != NULL) {
564 currhandler->handler();
565 lasthandler = currhandler;
566 currhandler = currhandler->next;
567 OPENSSL_free(lasthandler);
569 stop_handlers = NULL;
571 * We assume we are single-threaded for this function, i.e. no race
572 * conditions for the various "*_inited" vars below.
576 #ifdef OPENSSL_INIT_DEBUG
577 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
578 "COMP_zlib_cleanup()\n");
583 #ifndef OPENSSL_NO_ASYNC
585 # ifdef OPENSSL_INIT_DEBUG
586 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
593 #ifndef OPENSSL_NO_ENGINE
595 # ifdef OPENSSL_INIT_DEBUG
596 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
597 "ENGINE_cleanup()\n");
603 if (load_crypto_strings_inited) {
604 #ifdef OPENSSL_INIT_DEBUG
605 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
606 "ERR_free_strings()\n");
611 ossl_init_thread_stop_cleanup();
613 #ifdef OPENSSL_INIT_DEBUG
614 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
615 "CRYPTO_cleanup_all_ex_data()\n");
616 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
618 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
619 "CONF_modules_free()\n");
620 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
623 CRYPTO_cleanup_all_ex_data();
631 * If this function is called with a non NULL settings value then it must be
632 * called prior to any threads making calls to any OpenSSL functions,
633 * i.e. passing a non-null settings value is assumed to be single-threaded.
635 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
637 static int stoperrset = 0;
642 * We only ever set this once to avoid getting into an infinite
643 * loop where the error system keeps trying to init and fails so
647 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
652 ossl_init_once_run(&base, ossl_init_base);
654 if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
655 ossl_init_once_run(&load_crypto_strings,
656 ossl_init_no_load_crypto_strings);
658 if (opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
659 ossl_init_once_run(&load_crypto_strings, ossl_init_load_crypto_strings);
661 if (opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
662 ossl_init_once_run(&add_all_ciphers, ossl_init_no_add_algs);
664 if (opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
665 ossl_init_once_run(&add_all_ciphers, ossl_init_add_all_ciphers);
667 if (opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
668 ossl_init_once_run(&add_all_digests, ossl_init_no_add_algs);
670 if (opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
671 ossl_init_once_run(&add_all_digests, ossl_init_add_all_digests);
673 if (opts & OPENSSL_INIT_NO_LOAD_CONFIG) {
674 ossl_init_once_run(&config, ossl_init_no_config);
677 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
678 CRYPTO_w_lock(CRYPTO_LOCK_INIT);
679 config_filename = (settings == NULL) ? NULL : settings->config_name;
680 ossl_init_once_run(&config, ossl_init_config);
681 CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
684 #ifndef OPENSSL_NO_ASYNC
685 if (opts & OPENSSL_INIT_ASYNC) {
686 ossl_init_once_run(&async, ossl_init_async);
689 #ifndef OPENSSL_NO_ENGINE
690 if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
691 ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
693 # if !defined(OPENSSL_NO_HW) && \
694 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
695 if (opts & OPENSSL_INIT_ENGINE_CRYPTODEV) {
696 ossl_init_once_run(&engine_cryptodev, ossl_init_engine_cryptodev);
699 # ifndef OPENSSL_NO_RDRAND
700 if (opts & OPENSSL_INIT_ENGINE_RDRAND) {
701 ossl_init_once_run(&engine_rdrand, ossl_init_engine_rdrand);
704 if (opts & OPENSSL_INIT_ENGINE_DYNAMIC) {
705 ossl_init_once_run(&engine_dynamic, ossl_init_engine_dynamic);
707 # ifndef OPENSSL_NO_STATIC_ENGINE
708 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
709 if (opts & OPENSSL_INIT_ENGINE_PADLOCK) {
710 ossl_init_once_run(&engine_padlock, ossl_init_engine_padlock);
713 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
714 if (opts & OPENSSL_INIT_ENGINE_CAPI) {
715 ossl_init_once_run(&engine_capi, ossl_init_engine_capi);
718 if (opts & OPENSSL_INIT_ENGINE_DASYNC) {
719 ossl_init_once_run(&engine_dasync, ossl_init_engine_dasync);
722 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
723 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL)) {
724 ENGINE_register_all_complete();
728 if (opts & OPENSSL_INIT_ZLIB) {
729 ossl_init_once_run(&zlib, ossl_init_zlib);
735 int OPENSSL_atexit(void (*handler)(void))
737 OPENSSL_INIT_STOP *newhand;
739 newhand = OPENSSL_malloc(sizeof(*newhand));
743 newhand->handler = handler;
744 newhand->next = stop_handlers;
745 stop_handlers = newhand;