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/evp.h>
61 #include <internal/evp_int.h>
62 #include <internal/conf.h>
63 #include <internal/async.h>
64 #include <internal/engine.h>
65 #include <openssl/comp.h>
66 #include <internal/err.h>
70 static int stopped = 0;
72 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
74 /* Implement "once" functionality */
75 #if !defined(OPENSSL_THREADS)
76 typedef int OPENSSL_INIT_ONCE;
77 # define OPENSSL_INIT_ONCE_STATIC_INIT 0
79 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
81 if (*once == OPENSSL_INIT_ONCE_STATIC_INIT) {
87 static int ossl_init_setup_thread_stop(void)
90 * There are no threads to stop. Do nothing.
95 static void ossl_init_thread_stop_cleanup(void)
99 static struct thread_local_inits_st *local = NULL;
100 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
102 struct thread_local_inits_st *tmp;
106 if (local == NULL && alloc)
107 tmp = local = OPENSSL_zalloc(sizeof(*local));
115 #elif defined(OPENSSL_SYS_WINDOWS)
117 # include <windows.h>
119 # if _WIN32_WINNT < 0x0600
122 * Versions before 0x0600 (Windows Vista, Windows Server 2008 or later) do not
123 * have InitOnceExecuteOnce, so we fall back to using a spinlock instead.
125 typedef LONG OPENSSL_INIT_ONCE;
126 # define OPENSSL_INIT_ONCE_STATIC_INIT 0
128 # define ONCE_UNINITED 0
129 # define ONCE_ININIT 1
132 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
134 LONG volatile *lock = (LONG *)once;
137 if (*lock == ONCE_DONE)
141 result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
142 if (result == ONCE_UNINITED) {
147 } while (result == ONCE_ININIT);
152 typedef INIT_ONCE OPENSSL_INIT_ONCE;
153 # define OPENSSL_INIT_ONCE_STATIC_INIT INIT_ONCE_STATIC_INIT
155 static BOOL CALLBACK once_cb(PINIT_ONCE once, PVOID initfp, PVOID *unused)
157 void (*init)(void) = initfp;
164 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
166 InitOnceExecuteOnce((INIT_ONCE *)once, once_cb, init, NULL);
170 static DWORD threadstopkey = TLS_OUT_OF_INDEXES;
172 static int ossl_init_setup_thread_stop(void)
175 * We use a dummy thread local key here. We use the destructor to detect
176 * when the thread is going to stop
178 threadstopkey = TlsAlloc();
179 if (threadstopkey == TLS_OUT_OF_INDEXES)
185 static void ossl_init_thread_stop_cleanup(void)
187 if (threadstopkey != TLS_OUT_OF_INDEXES) {
188 TlsFree(threadstopkey);
192 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
194 struct thread_local_inits_st *local = TlsGetValue(threadstopkey);
196 if (local == NULL && alloc) {
197 local = OPENSSL_zalloc(sizeof *local);
198 TlsSetValue(threadstopkey, local);
201 TlsSetValue(threadstopkey, NULL);
208 # include <pthread.h>
210 static pthread_key_t threadstopkey;
212 typedef pthread_once_t OPENSSL_INIT_ONCE;
213 # define OPENSSL_INIT_ONCE_STATIC_INIT PTHREAD_ONCE_INIT
215 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
217 pthread_once(once, init);
220 static void ossl_init_thread_stop_wrap(void *local)
222 ossl_init_thread_stop((struct thread_local_inits_st *)local);
225 static int ossl_init_setup_thread_stop(void)
228 * We use a dummy thread local key here. We use the destructor to detect
229 * when the thread is going to stop
231 return (pthread_key_create(&threadstopkey,
232 ossl_init_thread_stop_wrap) == 0);
235 static void ossl_init_thread_stop_cleanup(void)
239 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
241 struct thread_local_inits_st *local = pthread_getspecific(threadstopkey);
243 if (local == NULL && alloc) {
244 local = OPENSSL_zalloc(sizeof *local);
245 pthread_setspecific(threadstopkey, local);
248 pthread_setspecific(threadstopkey, NULL);
256 struct ossl_init_stop_st {
257 void (*handler)(void);
258 OPENSSL_INIT_STOP *next;
261 static OPENSSL_INIT_STOP *stop_handlers = NULL;
263 static OPENSSL_INIT_ONCE base = OPENSSL_INIT_ONCE_STATIC_INIT;
264 static int base_inited = 0;
265 static void ossl_init_base(void)
267 #ifdef OPENSSL_INIT_DEBUG
268 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
270 ossl_init_setup_thread_stop();
271 atexit(OPENSSL_cleanup);
272 OPENSSL_cpuid_setup();
276 static OPENSSL_INIT_ONCE load_crypto_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
277 static int load_crypto_strings_inited = 0;
278 static void ossl_init_no_load_crypto_strings(void)
280 /* Do nothing in this case */
284 static void ossl_init_load_crypto_strings(void)
287 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
288 * pulling in all the error strings during static linking
290 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
291 # ifdef OPENSSL_INIT_DEBUG
292 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
293 "err_load_crypto_strings_intern()\n");
295 err_load_crypto_strings_intern();
297 load_crypto_strings_inited = 1;
300 static OPENSSL_INIT_ONCE add_all_ciphers = OPENSSL_INIT_ONCE_STATIC_INIT;
301 static void ossl_init_add_all_ciphers(void)
304 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
305 * pulling in all the ciphers during static linking
307 #ifndef OPENSSL_NO_AUTOALGINIT
308 # ifdef OPENSSL_INIT_DEBUG
309 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
310 "openssl_add_all_ciphers_internal()\n");
312 openssl_add_all_ciphers_internal();
313 # ifndef OPENSSL_NO_ENGINE
314 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
315 ENGINE_setup_bsd_cryptodev();
321 static OPENSSL_INIT_ONCE add_all_digests = OPENSSL_INIT_ONCE_STATIC_INIT;
322 static void ossl_init_add_all_digests(void)
325 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
326 * pulling in all the ciphers during static linking
328 #ifndef OPENSSL_NO_AUTOALGINIT
329 # ifdef OPENSSL_INIT_DEBUG
330 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
331 "openssl_add_all_digests_internal()\n");
333 openssl_add_all_digests_internal();
334 # ifndef OPENSSL_NO_ENGINE
335 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
336 ENGINE_setup_bsd_cryptodev();
342 static void ossl_init_no_add_algs(void)
348 static OPENSSL_INIT_ONCE config = OPENSSL_INIT_ONCE_STATIC_INIT;
349 static int config_inited = 0;
350 static const char *config_filename;
351 static void ossl_init_config(void)
353 #ifdef OPENSSL_INIT_DEBUG
355 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
356 config_filename==NULL?"NULL":config_filename);
358 openssl_config_internal(config_filename);
361 static void ossl_init_no_config(void)
363 #ifdef OPENSSL_INIT_DEBUG
365 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
367 openssl_no_config_internal();
371 static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
372 static int async_inited = 0;
373 static void ossl_init_async(void)
375 #ifdef OPENSSL_INIT_DEBUG
376 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
382 #ifndef OPENSSL_NO_ENGINE
383 static int engine_inited = 0;
384 static OPENSSL_INIT_ONCE engine_openssl = OPENSSL_INIT_ONCE_STATIC_INIT;
385 static void ossl_init_engine_openssl(void)
387 # ifdef OPENSSL_INIT_DEBUG
388 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
389 "engine_load_openssl_internal()\n");
391 engine_load_openssl_internal();
394 # if !defined(OPENSSL_NO_HW) && \
395 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
396 static OPENSSL_INIT_ONCE engine_cryptodev = OPENSSL_INIT_ONCE_STATIC_INIT;
397 static void ossl_init_engine_cryptodev(void)
399 # ifdef OPENSSL_INIT_DEBUG
400 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
401 "engine_load_cryptodev_internal()\n");
403 engine_load_cryptodev_internal();
408 # ifndef OPENSSL_NO_RDRAND
409 static OPENSSL_INIT_ONCE engine_rdrand = OPENSSL_INIT_ONCE_STATIC_INIT;
410 static void ossl_init_engine_rdrand(void)
412 # ifdef OPENSSL_INIT_DEBUG
413 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
414 "engine_load_rdrand_internal()\n");
416 engine_load_rdrand_internal();
420 static OPENSSL_INIT_ONCE engine_dynamic = OPENSSL_INIT_ONCE_STATIC_INIT;
421 static void ossl_init_engine_dynamic(void)
423 # ifdef OPENSSL_INIT_DEBUG
424 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
425 "engine_load_dynamic_internal()\n");
427 engine_load_dynamic_internal();
430 # ifndef OPENSSL_NO_STATIC_ENGINE
431 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
432 static OPENSSL_INIT_ONCE engine_padlock = OPENSSL_INIT_ONCE_STATIC_INIT;
433 static void ossl_init_engine_padlock(void)
435 # ifdef OPENSSL_INIT_DEBUG
436 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
437 "engine_load_padlock_internal()\n");
439 engine_load_padlock_internal();
443 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
444 static OPENSSL_INIT_ONCE engine_capi = OPENSSL_INIT_ONCE_STATIC_INIT;
445 static void ossl_init_engine_capi(void)
447 # ifdef OPENSSL_INIT_DEBUG
448 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
449 "engine_load_capi_internal()\n");
451 engine_load_capi_internal();
455 static OPENSSL_INIT_ONCE engine_dasync = OPENSSL_INIT_ONCE_STATIC_INIT;
456 static void ossl_init_engine_dasync(void)
458 # ifdef OPENSSL_INIT_DEBUG
459 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
460 "engine_load_dasync_internal()\n");
462 engine_load_dasync_internal();
468 static OPENSSL_INIT_ONCE zlib = OPENSSL_INIT_ONCE_STATIC_INIT;
469 static int zlib_inited = 0;
470 static void ossl_init_zlib(void)
472 /* Do nothing - we need to know about this for the later cleanup */
476 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
478 /* Can't do much about this */
483 #ifdef OPENSSL_INIT_DEBUG
484 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
485 "ASYNC_cleanup_thread()\n");
487 ASYNC_cleanup_thread();
490 if (locals->err_state) {
491 #ifdef OPENSSL_INIT_DEBUG
492 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
493 "ERR_remove_thread_state(NULL)\n");
495 ERR_remove_thread_state(NULL);
498 OPENSSL_free(locals);
499 ossl_init_thread_stop_cleanup();
502 void OPENSSL_thread_stop(void)
504 ossl_init_thread_stop(
505 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
508 int ossl_init_thread_start(uint64_t opts)
510 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
515 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
516 #ifdef OPENSSL_INIT_DEBUG
517 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
518 "marking thread for async\n");
523 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
524 #ifdef OPENSSL_INIT_DEBUG
525 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
526 "marking thread for err_state\n");
528 locals->err_state = 1;
534 void OPENSSL_cleanup(void)
536 OPENSSL_INIT_STOP *currhandler, *lasthandler;
538 /* If we've not been inited then no need to deinit */
542 /* Might be explicitly called and also by atexit */
548 * Thread stop may not get automatically called by the thread library for
549 * the very last thread in some situations, so call it directly.
551 ossl_init_thread_stop(ossl_init_get_thread_local(0));
553 currhandler = stop_handlers;
554 while (currhandler != NULL) {
555 currhandler->handler();
556 lasthandler = currhandler;
557 currhandler = currhandler->next;
558 OPENSSL_free(lasthandler);
560 stop_handlers = NULL;
562 * We assume we are single-threaded for this function, i.e. no race
563 * conditions for the various "*_inited" vars below.
567 #ifdef OPENSSL_INIT_DEBUG
568 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
569 "COMP_zlib_cleanup()\n");
574 #ifndef OPENSSL_NO_ENGINE
576 # ifdef OPENSSL_INIT_DEBUG
577 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
578 "ENGINE_cleanup()\n");
584 if (load_crypto_strings_inited) {
585 #ifdef OPENSSL_INIT_DEBUG
586 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
587 "ERR_free_strings()\n");
592 #ifdef OPENSSL_INIT_DEBUG
593 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
594 "CRYPTO_cleanup_all_ex_data()\n");
595 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
597 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
598 "CONF_modules_free()\n");
599 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
602 CRYPTO_cleanup_all_ex_data();
609 static const OPENSSL_INIT_SETTINGS *ossl_init_get_setting(
610 const OPENSSL_INIT_SETTINGS *settings, int name)
612 if (settings == NULL)
615 while (settings->name != OPENSSL_INIT_SET_END) {
616 if (settings->name == name)
625 * If this function is called with a non NULL settings value then it must be
626 * called prior to any threads making calls to any OpenSSL functions,
627 * i.e. passing a non-null settings value is assumed to be single-threaded.
629 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
634 ossl_init_once_run(&base, ossl_init_base);
636 if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
637 ossl_init_once_run(&load_crypto_strings,
638 ossl_init_no_load_crypto_strings);
640 if (opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
641 ossl_init_once_run(&load_crypto_strings, ossl_init_load_crypto_strings);
643 if (opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
644 ossl_init_once_run(&add_all_ciphers, ossl_init_no_add_algs);
646 if (opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
647 ossl_init_once_run(&add_all_ciphers, ossl_init_add_all_ciphers);
649 if (opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
650 ossl_init_once_run(&add_all_digests, ossl_init_no_add_algs);
652 if (opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
653 ossl_init_once_run(&add_all_digests, ossl_init_add_all_digests);
655 if (opts & OPENSSL_INIT_NO_LOAD_CONFIG) {
656 ossl_init_once_run(&config, ossl_init_no_config);
659 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
660 CRYPTO_w_lock(CRYPTO_LOCK_INIT);
661 if (settings != NULL) {
662 const OPENSSL_INIT_SETTINGS *curr;
663 curr = ossl_init_get_setting(settings,
664 OPENSSL_INIT_SET_CONF_FILENAME);
665 config_filename = (curr == NULL) ? NULL : curr->value.type_string;
667 config_filename = NULL;
669 ossl_init_once_run(&config, ossl_init_config);
670 CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
673 if (opts & OPENSSL_INIT_ASYNC) {
674 ossl_init_once_run(&async, ossl_init_async);
677 #ifndef OPENSSL_NO_ENGINE
678 if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
679 ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
681 # if !defined(OPENSSL_NO_HW) && \
682 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
683 if (opts & OPENSSL_INIT_ENGINE_CRYPTODEV) {
684 ossl_init_once_run(&engine_cryptodev, ossl_init_engine_cryptodev);
687 # ifndef OPENSSL_NO_RDRAND
688 if (opts & OPENSSL_INIT_ENGINE_RDRAND) {
689 ossl_init_once_run(&engine_rdrand, ossl_init_engine_rdrand);
692 if (opts & OPENSSL_INIT_ENGINE_DYNAMIC) {
693 ossl_init_once_run(&engine_dynamic, ossl_init_engine_dynamic);
695 # ifndef OPENSSL_NO_STATIC_ENGINE
696 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
697 if (opts & OPENSSL_INIT_ENGINE_PADLOCK) {
698 ossl_init_once_run(&engine_padlock, ossl_init_engine_padlock);
701 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
702 if (opts & OPENSSL_INIT_ENGINE_CAPI) {
703 ossl_init_once_run(&engine_capi, ossl_init_engine_capi);
706 if (opts & OPENSSL_INIT_ENGINE_DASYNC) {
707 ossl_init_once_run(&engine_dasync, ossl_init_engine_dasync);
710 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
711 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL)) {
712 ENGINE_register_all_complete();
716 if (opts & OPENSSL_INIT_ZLIB) {
717 ossl_init_once_run(&zlib, ossl_init_zlib);
723 int OPENSSL_atexit(void (*handler)(void))
725 OPENSSL_INIT_STOP *newhand;
727 newhand = OPENSSL_malloc(sizeof(*newhand));
731 newhand->handler = handler;
732 newhand->next = stop_handlers;
733 stop_handlers = newhand;