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 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
257 struct ossl_init_stop_st {
258 void (*handler)(void);
259 OPENSSL_INIT_STOP *next;
262 static OPENSSL_INIT_STOP *stop_handlers = NULL;
264 static OPENSSL_INIT_ONCE base = OPENSSL_INIT_ONCE_STATIC_INIT;
265 static int base_inited = 0;
266 static void ossl_init_base(void)
268 #ifdef OPENSSL_INIT_DEBUG
269 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
271 ossl_init_setup_thread_stop();
272 atexit(OPENSSL_cleanup);
273 OPENSSL_cpuid_setup();
277 static OPENSSL_INIT_ONCE load_crypto_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
278 static int load_crypto_strings_inited = 0;
279 static void ossl_init_no_load_crypto_strings(void)
281 /* Do nothing in this case */
285 static void ossl_init_load_crypto_strings(void)
288 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
289 * pulling in all the error strings during static linking
291 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
292 # ifdef OPENSSL_INIT_DEBUG
293 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
294 "err_load_crypto_strings_intern()\n");
296 err_load_crypto_strings_intern();
298 load_crypto_strings_inited = 1;
301 static OPENSSL_INIT_ONCE add_all_ciphers = OPENSSL_INIT_ONCE_STATIC_INIT;
302 static void ossl_init_add_all_ciphers(void)
305 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
306 * pulling in all the ciphers during static linking
308 #ifndef OPENSSL_NO_AUTOALGINIT
309 # ifdef OPENSSL_INIT_DEBUG
310 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
311 "openssl_add_all_ciphers_internal()\n");
313 openssl_add_all_ciphers_internal();
314 # ifndef OPENSSL_NO_ENGINE
315 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
316 ENGINE_setup_bsd_cryptodev();
322 static OPENSSL_INIT_ONCE add_all_digests = OPENSSL_INIT_ONCE_STATIC_INIT;
323 static void ossl_init_add_all_digests(void)
326 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
327 * pulling in all the ciphers during static linking
329 #ifndef OPENSSL_NO_AUTOALGINIT
330 # ifdef OPENSSL_INIT_DEBUG
331 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
332 "openssl_add_all_digests_internal()\n");
334 openssl_add_all_digests_internal();
335 # ifndef OPENSSL_NO_ENGINE
336 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
337 ENGINE_setup_bsd_cryptodev();
343 static void ossl_init_no_add_algs(void)
349 static OPENSSL_INIT_ONCE config = OPENSSL_INIT_ONCE_STATIC_INIT;
350 static int config_inited = 0;
351 static const char *config_filename;
352 static void ossl_init_config(void)
354 #ifdef OPENSSL_INIT_DEBUG
356 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
357 config_filename==NULL?"NULL":config_filename);
359 openssl_config_internal(config_filename);
362 static void ossl_init_no_config(void)
364 #ifdef OPENSSL_INIT_DEBUG
366 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
368 openssl_no_config_internal();
372 static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
373 static int async_inited = 0;
374 static void ossl_init_async(void)
376 #ifdef OPENSSL_INIT_DEBUG
377 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
383 #ifndef OPENSSL_NO_ENGINE
384 static int engine_inited = 0;
385 static OPENSSL_INIT_ONCE engine_openssl = OPENSSL_INIT_ONCE_STATIC_INIT;
386 static void ossl_init_engine_openssl(void)
388 # ifdef OPENSSL_INIT_DEBUG
389 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
390 "engine_load_openssl_internal()\n");
392 engine_load_openssl_internal();
395 # if !defined(OPENSSL_NO_HW) && \
396 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
397 static OPENSSL_INIT_ONCE engine_cryptodev = OPENSSL_INIT_ONCE_STATIC_INIT;
398 static void ossl_init_engine_cryptodev(void)
400 # ifdef OPENSSL_INIT_DEBUG
401 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
402 "engine_load_cryptodev_internal()\n");
404 engine_load_cryptodev_internal();
409 # ifndef OPENSSL_NO_RDRAND
410 static OPENSSL_INIT_ONCE engine_rdrand = OPENSSL_INIT_ONCE_STATIC_INIT;
411 static void ossl_init_engine_rdrand(void)
413 # ifdef OPENSSL_INIT_DEBUG
414 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
415 "engine_load_rdrand_internal()\n");
417 engine_load_rdrand_internal();
421 static OPENSSL_INIT_ONCE engine_dynamic = OPENSSL_INIT_ONCE_STATIC_INIT;
422 static void ossl_init_engine_dynamic(void)
424 # ifdef OPENSSL_INIT_DEBUG
425 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
426 "engine_load_dynamic_internal()\n");
428 engine_load_dynamic_internal();
431 # ifndef OPENSSL_NO_STATIC_ENGINE
432 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
433 static OPENSSL_INIT_ONCE engine_padlock = OPENSSL_INIT_ONCE_STATIC_INIT;
434 static void ossl_init_engine_padlock(void)
436 # ifdef OPENSSL_INIT_DEBUG
437 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
438 "engine_load_padlock_internal()\n");
440 engine_load_padlock_internal();
444 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
445 static OPENSSL_INIT_ONCE engine_capi = OPENSSL_INIT_ONCE_STATIC_INIT;
446 static void ossl_init_engine_capi(void)
448 # ifdef OPENSSL_INIT_DEBUG
449 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
450 "engine_load_capi_internal()\n");
452 engine_load_capi_internal();
456 static OPENSSL_INIT_ONCE engine_dasync = OPENSSL_INIT_ONCE_STATIC_INIT;
457 static void ossl_init_engine_dasync(void)
459 # ifdef OPENSSL_INIT_DEBUG
460 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
461 "engine_load_dasync_internal()\n");
463 engine_load_dasync_internal();
469 static OPENSSL_INIT_ONCE zlib = OPENSSL_INIT_ONCE_STATIC_INIT;
470 static int zlib_inited = 0;
471 static void ossl_init_zlib(void)
473 /* Do nothing - we need to know about this for the later cleanup */
477 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
479 /* Can't do much about this */
484 #ifdef OPENSSL_INIT_DEBUG
485 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
486 "ASYNC_cleanup_thread()\n");
488 ASYNC_cleanup_thread();
491 if (locals->err_state) {
492 #ifdef OPENSSL_INIT_DEBUG
493 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
494 "ERR_remove_thread_state(NULL)\n");
496 ERR_remove_thread_state(NULL);
499 OPENSSL_free(locals);
500 ossl_init_thread_stop_cleanup();
503 void OPENSSL_thread_stop(void)
505 ossl_init_thread_stop(
506 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
509 int ossl_init_thread_start(uint64_t opts)
511 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
516 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
517 #ifdef OPENSSL_INIT_DEBUG
518 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
519 "marking thread for async\n");
524 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
525 #ifdef OPENSSL_INIT_DEBUG
526 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
527 "marking thread for err_state\n");
529 locals->err_state = 1;
535 void OPENSSL_cleanup(void)
537 OPENSSL_INIT_STOP *currhandler, *lasthandler;
539 /* If we've not been inited then no need to deinit */
543 /* Might be explicitly called and also by atexit */
549 * Thread stop may not get automatically called by the thread library for
550 * the very last thread in some situations, so call it directly.
552 ossl_init_thread_stop(ossl_init_get_thread_local(0));
554 currhandler = stop_handlers;
555 while (currhandler != NULL) {
556 currhandler->handler();
557 lasthandler = currhandler;
558 currhandler = currhandler->next;
559 OPENSSL_free(lasthandler);
561 stop_handlers = NULL;
563 * We assume we are single-threaded for this function, i.e. no race
564 * conditions for the various "*_inited" vars below.
568 #ifdef OPENSSL_INIT_DEBUG
569 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
570 "COMP_zlib_cleanup()\n");
575 #ifndef OPENSSL_NO_ENGINE
577 # ifdef OPENSSL_INIT_DEBUG
578 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
579 "ENGINE_cleanup()\n");
585 if (load_crypto_strings_inited) {
586 #ifdef OPENSSL_INIT_DEBUG
587 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
588 "ERR_free_strings()\n");
593 #ifdef OPENSSL_INIT_DEBUG
594 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
595 "CRYPTO_cleanup_all_ex_data()\n");
596 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
598 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
599 "CONF_modules_free()\n");
600 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
603 CRYPTO_cleanup_all_ex_data();
611 * If this function is called with a non NULL settings value then it must be
612 * called prior to any threads making calls to any OpenSSL functions,
613 * i.e. passing a non-null settings value is assumed to be single-threaded.
615 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
617 static int stoperrset = 0;
622 * We only ever set this once to avoid getting into an infinite
623 * loop where the error system keeps trying to init and fails so
627 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
632 ossl_init_once_run(&base, ossl_init_base);
634 if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
635 ossl_init_once_run(&load_crypto_strings,
636 ossl_init_no_load_crypto_strings);
638 if (opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
639 ossl_init_once_run(&load_crypto_strings, ossl_init_load_crypto_strings);
641 if (opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
642 ossl_init_once_run(&add_all_ciphers, ossl_init_no_add_algs);
644 if (opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
645 ossl_init_once_run(&add_all_ciphers, ossl_init_add_all_ciphers);
647 if (opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
648 ossl_init_once_run(&add_all_digests, ossl_init_no_add_algs);
650 if (opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
651 ossl_init_once_run(&add_all_digests, ossl_init_add_all_digests);
653 if (opts & OPENSSL_INIT_NO_LOAD_CONFIG) {
654 ossl_init_once_run(&config, ossl_init_no_config);
657 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
658 CRYPTO_w_lock(CRYPTO_LOCK_INIT);
659 config_filename = (settings == NULL) ? NULL : settings->config_name;
660 ossl_init_once_run(&config, ossl_init_config);
661 CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
664 if (opts & OPENSSL_INIT_ASYNC) {
665 ossl_init_once_run(&async, ossl_init_async);
668 #ifndef OPENSSL_NO_ENGINE
669 if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
670 ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
672 # if !defined(OPENSSL_NO_HW) && \
673 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
674 if (opts & OPENSSL_INIT_ENGINE_CRYPTODEV) {
675 ossl_init_once_run(&engine_cryptodev, ossl_init_engine_cryptodev);
678 # ifndef OPENSSL_NO_RDRAND
679 if (opts & OPENSSL_INIT_ENGINE_RDRAND) {
680 ossl_init_once_run(&engine_rdrand, ossl_init_engine_rdrand);
683 if (opts & OPENSSL_INIT_ENGINE_DYNAMIC) {
684 ossl_init_once_run(&engine_dynamic, ossl_init_engine_dynamic);
686 # ifndef OPENSSL_NO_STATIC_ENGINE
687 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
688 if (opts & OPENSSL_INIT_ENGINE_PADLOCK) {
689 ossl_init_once_run(&engine_padlock, ossl_init_engine_padlock);
692 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
693 if (opts & OPENSSL_INIT_ENGINE_CAPI) {
694 ossl_init_once_run(&engine_capi, ossl_init_engine_capi);
697 if (opts & OPENSSL_INIT_ENGINE_DASYNC) {
698 ossl_init_once_run(&engine_dasync, ossl_init_engine_dasync);
701 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
702 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL)) {
703 ENGINE_register_all_complete();
707 if (opts & OPENSSL_INIT_ZLIB) {
708 ossl_init_once_run(&zlib, ossl_init_zlib);
714 int OPENSSL_atexit(void (*handler)(void))
716 OPENSSL_INIT_STOP *newhand;
718 newhand = OPENSSL_malloc(sizeof(*newhand));
722 newhand->handler = handler;
723 newhand->next = stop_handlers;
724 stop_handlers = newhand;