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 static CRYPTO_THREAD_LOCAL threadstopkey
;
80 static void ossl_init_thread_stop_wrap(void *local
)
82 ossl_init_thread_stop((struct thread_local_inits_st
*)local
);
85 static struct thread_local_inits_st
*ossl_init_get_thread_local(int alloc
)
87 struct thread_local_inits_st
*local
=
88 CRYPTO_THREAD_get_local(&threadstopkey
);
90 if (local
== NULL
&& alloc
) {
91 local
= OPENSSL_zalloc(sizeof *local
);
92 CRYPTO_THREAD_set_local(&threadstopkey
, local
);
95 CRYPTO_THREAD_set_local(&threadstopkey
, NULL
);
101 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP
;
102 struct ossl_init_stop_st
{
103 void (*handler
)(void);
104 OPENSSL_INIT_STOP
*next
;
107 static OPENSSL_INIT_STOP
*stop_handlers
= NULL
;
108 static CRYPTO_RWLOCK
*init_lock
= NULL
;
110 static CRYPTO_ONCE base
= CRYPTO_ONCE_STATIC_INIT
;
111 static int base_inited
= 0;
112 static void ossl_init_base(void)
114 #ifdef OPENSSL_INIT_DEBUG
115 fprintf(stderr
, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
118 * We use a dummy thread local key here. We use the destructor to detect
119 * when the thread is going to stop (where that feature is available)
121 CRYPTO_THREAD_init_local(&threadstopkey
, ossl_init_thread_stop_wrap
);
122 #ifndef OPENSSL_SYS_UEFI
123 atexit(OPENSSL_cleanup
);
125 init_lock
= CRYPTO_THREAD_lock_new();
126 OPENSSL_cpuid_setup();
130 static CRYPTO_ONCE load_crypto_strings
= CRYPTO_ONCE_STATIC_INIT
;
131 static int load_crypto_strings_inited
= 0;
132 static void ossl_init_no_load_crypto_strings(void)
134 /* Do nothing in this case */
138 static void ossl_init_load_crypto_strings(void)
141 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
142 * pulling in all the error strings during static linking
144 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
145 # ifdef OPENSSL_INIT_DEBUG
146 fprintf(stderr
, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
147 "err_load_crypto_strings_intern()\n");
149 err_load_crypto_strings_intern();
151 load_crypto_strings_inited
= 1;
154 static CRYPTO_ONCE add_all_ciphers
= CRYPTO_ONCE_STATIC_INIT
;
155 static void ossl_init_add_all_ciphers(void)
158 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
159 * pulling in all the ciphers during static linking
161 #ifndef OPENSSL_NO_AUTOALGINIT
162 # ifdef OPENSSL_INIT_DEBUG
163 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
164 "openssl_add_all_ciphers_internal()\n");
166 openssl_add_all_ciphers_internal();
167 # ifndef OPENSSL_NO_ENGINE
168 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
169 ENGINE_setup_bsd_cryptodev();
175 static CRYPTO_ONCE add_all_digests
= CRYPTO_ONCE_STATIC_INIT
;
176 static void ossl_init_add_all_digests(void)
179 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
180 * pulling in all the ciphers during static linking
182 #ifndef OPENSSL_NO_AUTOALGINIT
183 # ifdef OPENSSL_INIT_DEBUG
184 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_digests: "
185 "openssl_add_all_digests_internal()\n");
187 openssl_add_all_digests_internal();
188 # ifndef OPENSSL_NO_ENGINE
189 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
190 ENGINE_setup_bsd_cryptodev();
196 static void ossl_init_no_add_algs(void)
202 static CRYPTO_ONCE config
= CRYPTO_ONCE_STATIC_INIT
;
203 static int config_inited
= 0;
204 static const char *config_filename
;
205 static void ossl_init_config(void)
207 #ifdef OPENSSL_INIT_DEBUG
209 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
210 config_filename
==NULL
?"NULL":config_filename
);
212 openssl_config_internal(config_filename
);
215 static void ossl_init_no_config(void)
217 #ifdef OPENSSL_INIT_DEBUG
219 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
221 openssl_no_config_internal();
225 #ifndef OPENSSL_NO_ASYNC
226 static CRYPTO_ONCE async
= CRYPTO_ONCE_STATIC_INIT
;
227 static int async_inited
= 0;
228 static void ossl_init_async(void)
230 #ifdef OPENSSL_INIT_DEBUG
231 fprintf(stderr
, "OPENSSL_INIT: ossl_init_async: async_init()\n");
238 #ifndef OPENSSL_NO_ENGINE
239 static CRYPTO_ONCE engine_openssl
= CRYPTO_ONCE_STATIC_INIT
;
240 static void ossl_init_engine_openssl(void)
242 # ifdef OPENSSL_INIT_DEBUG
243 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_openssl: "
244 "engine_load_openssl_internal()\n");
246 engine_load_openssl_internal();
248 # if !defined(OPENSSL_NO_HW) && \
249 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
250 static CRYPTO_ONCE engine_cryptodev
= CRYPTO_ONCE_STATIC_INIT
;
251 static void ossl_init_engine_cryptodev(void)
253 # ifdef OPENSSL_INIT_DEBUG
254 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
255 "engine_load_cryptodev_internal()\n");
257 engine_load_cryptodev_internal();
261 # ifndef OPENSSL_NO_RDRAND
262 static CRYPTO_ONCE engine_rdrand
= CRYPTO_ONCE_STATIC_INIT
;
263 static void ossl_init_engine_rdrand(void)
265 # ifdef OPENSSL_INIT_DEBUG
266 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_rdrand: "
267 "engine_load_rdrand_internal()\n");
269 engine_load_rdrand_internal();
272 static CRYPTO_ONCE engine_dynamic
= CRYPTO_ONCE_STATIC_INIT
;
273 static void ossl_init_engine_dynamic(void)
275 # ifdef OPENSSL_INIT_DEBUG
276 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_dynamic: "
277 "engine_load_dynamic_internal()\n");
279 engine_load_dynamic_internal();
281 # ifndef OPENSSL_NO_STATIC_ENGINE
282 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
283 static CRYPTO_ONCE engine_padlock
= CRYPTO_ONCE_STATIC_INIT
;
284 static void ossl_init_engine_padlock(void)
286 # ifdef OPENSSL_INIT_DEBUG
287 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_padlock: "
288 "engine_load_padlock_internal()\n");
290 engine_load_padlock_internal();
293 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
294 static CRYPTO_ONCE engine_capi
= CRYPTO_ONCE_STATIC_INIT
;
295 static void ossl_init_engine_capi(void)
297 # ifdef OPENSSL_INIT_DEBUG
298 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_capi: "
299 "engine_load_capi_internal()\n");
301 engine_load_capi_internal();
304 static CRYPTO_ONCE engine_dasync
= CRYPTO_ONCE_STATIC_INIT
;
305 static void ossl_init_engine_dasync(void)
307 # ifdef OPENSSL_INIT_DEBUG
308 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_dasync: "
309 "engine_load_dasync_internal()\n");
311 engine_load_dasync_internal();
313 # if !defined(OPENSSL_NO_AFALGENG)
314 static CRYPTO_ONCE engine_afalg
= CRYPTO_ONCE_STATIC_INIT
;
315 static void ossl_init_engine_afalg(void)
317 # ifdef OPENSSL_INIT_DEBUG
318 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_afalg: "
319 "engine_load_afalg_internal()\n");
321 engine_load_afalg_internal();
327 #ifndef OPENSSL_NO_COMP
328 static CRYPTO_ONCE zlib
= CRYPTO_ONCE_STATIC_INIT
;
330 static int zlib_inited
= 0;
331 static void ossl_init_zlib(void)
333 /* Do nothing - we need to know about this for the later cleanup */
338 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
)
340 /* Can't do much about this */
344 #ifndef OPENSSL_NO_ASYNC
346 #ifdef OPENSSL_INIT_DEBUG
347 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
348 "ASYNC_cleanup_thread()\n");
350 ASYNC_cleanup_thread();
354 if (locals
->err_state
) {
355 #ifdef OPENSSL_INIT_DEBUG
356 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
357 "ERR_remove_thread_state()\n");
359 ERR_remove_thread_state();
362 OPENSSL_free(locals
);
365 void OPENSSL_thread_stop(void)
367 ossl_init_thread_stop(
368 (struct thread_local_inits_st
*)ossl_init_get_thread_local(0));
371 int ossl_init_thread_start(uint64_t opts
)
373 struct thread_local_inits_st
*locals
= ossl_init_get_thread_local(1);
378 if (opts
& OPENSSL_INIT_THREAD_ASYNC
) {
379 #ifdef OPENSSL_INIT_DEBUG
380 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
381 "marking thread for async\n");
386 if (opts
& OPENSSL_INIT_THREAD_ERR_STATE
) {
387 #ifdef OPENSSL_INIT_DEBUG
388 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
389 "marking thread for err_state\n");
391 locals
->err_state
= 1;
397 void OPENSSL_cleanup(void)
399 OPENSSL_INIT_STOP
*currhandler
, *lasthandler
;
401 /* If we've not been inited then no need to deinit */
405 /* Might be explicitly called and also by atexit */
411 * Thread stop may not get automatically called by the thread library for
412 * the very last thread in some situations, so call it directly.
414 ossl_init_thread_stop(ossl_init_get_thread_local(0));
416 currhandler
= stop_handlers
;
417 while (currhandler
!= NULL
) {
418 currhandler
->handler();
419 lasthandler
= currhandler
;
420 currhandler
= currhandler
->next
;
421 OPENSSL_free(lasthandler
);
423 stop_handlers
= NULL
;
425 CRYPTO_THREAD_lock_free(init_lock
);
428 * We assume we are single-threaded for this function, i.e. no race
429 * conditions for the various "*_inited" vars below.
432 #ifndef OPENSSL_NO_COMP
434 #ifdef OPENSSL_INIT_DEBUG
435 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
436 "COMP_zlib_cleanup()\n");
442 #ifndef OPENSSL_NO_ASYNC
444 # ifdef OPENSSL_INIT_DEBUG
445 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
452 if (load_crypto_strings_inited
) {
453 #ifdef OPENSSL_INIT_DEBUG
454 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
455 "ERR_free_strings()\n");
460 CRYPTO_THREAD_cleanup_local(&threadstopkey
);
462 #ifdef OPENSSL_INIT_DEBUG
463 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
465 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
466 "CONF_modules_free()\n");
467 #ifndef OPENSSL_NO_ENGINE
468 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
469 "ENGINE_cleanup()\n");
471 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
472 "CRYPTO_cleanup_all_ex_data()\n");
473 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
474 "BIO_sock_cleanup()\n");
475 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
477 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
481 * Note that cleanup order is important:
482 * - RAND_cleanup could call an ENINGE's RAND cleanup function so must be
483 * called before ENGINE_cleanup()
484 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
485 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
486 * - CONF_modules_free() can end up in ENGINE code so must be called before
491 #ifndef OPENSSL_NO_ENGINE
494 CRYPTO_cleanup_all_ex_data();
502 * If this function is called with a non NULL settings value then it must be
503 * called prior to any threads making calls to any OpenSSL functions,
504 * i.e. passing a non-null settings value is assumed to be single-threaded.
506 int OPENSSL_init_crypto(uint64_t opts
, const OPENSSL_INIT_SETTINGS
*settings
)
508 static int stoperrset
= 0;
513 * We only ever set this once to avoid getting into an infinite
514 * loop where the error system keeps trying to init and fails so
518 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO
, ERR_R_INIT_FAIL
);
523 if (!CRYPTO_THREAD_run_once(&base
, ossl_init_base
))
526 if ((opts
& OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS
)
527 && !CRYPTO_THREAD_run_once(&load_crypto_strings
,
528 ossl_init_no_load_crypto_strings
))
531 if ((opts
& OPENSSL_INIT_LOAD_CRYPTO_STRINGS
)
532 && !CRYPTO_THREAD_run_once(&load_crypto_strings
,
533 ossl_init_load_crypto_strings
))
536 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_CIPHERS
)
537 && !CRYPTO_THREAD_run_once(&add_all_ciphers
, ossl_init_no_add_algs
))
540 if ((opts
& OPENSSL_INIT_ADD_ALL_CIPHERS
)
541 && !CRYPTO_THREAD_run_once(&add_all_ciphers
,
542 ossl_init_add_all_ciphers
))
545 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_DIGESTS
)
546 && !CRYPTO_THREAD_run_once(&add_all_digests
, ossl_init_no_add_algs
))
549 if ((opts
& OPENSSL_INIT_ADD_ALL_DIGESTS
)
550 && !CRYPTO_THREAD_run_once(&add_all_digests
,
551 ossl_init_add_all_digests
))
554 if ((opts
& OPENSSL_INIT_NO_LOAD_CONFIG
)
555 && !CRYPTO_THREAD_run_once(&config
, ossl_init_no_config
))
558 if (opts
& OPENSSL_INIT_LOAD_CONFIG
) {
560 CRYPTO_THREAD_write_lock(init_lock
);
561 config_filename
= (settings
== NULL
) ? NULL
: settings
->config_name
;
562 ret
= CRYPTO_THREAD_run_once(&config
, ossl_init_config
);
563 CRYPTO_THREAD_unlock(init_lock
);
568 #ifndef OPENSSL_NO_ASYNC
569 if ((opts
& OPENSSL_INIT_ASYNC
)
570 && !CRYPTO_THREAD_run_once(&async
, ossl_init_async
))
573 #ifndef OPENSSL_NO_ENGINE
574 if ((opts
& OPENSSL_INIT_ENGINE_OPENSSL
)
575 && !CRYPTO_THREAD_run_once(&engine_openssl
,
576 ossl_init_engine_openssl
))
578 # if !defined(OPENSSL_NO_HW) && \
579 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
580 if ((opts
& OPENSSL_INIT_ENGINE_CRYPTODEV
)
581 && !CRYPTO_THREAD_run_once(&engine_cryptodev
,
582 ossl_init_engine_cryptodev
))
585 # ifndef OPENSSL_NO_RDRAND
586 if ((opts
& OPENSSL_INIT_ENGINE_RDRAND
)
587 && !CRYPTO_THREAD_run_once(&engine_rdrand
, ossl_init_engine_rdrand
))
590 if ((opts
& OPENSSL_INIT_ENGINE_DYNAMIC
)
591 && !CRYPTO_THREAD_run_once(&engine_dynamic
,
592 ossl_init_engine_dynamic
))
594 # ifndef OPENSSL_NO_STATIC_ENGINE
595 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
596 if ((opts
& OPENSSL_INIT_ENGINE_PADLOCK
)
597 && CRYPTO_THREAD_run_once(&engine_padlock
,
598 ossl_init_engine_padlock
))
601 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
602 if ((opts
& OPENSSL_INIT_ENGINE_CAPI
)
603 && CRYPTO_THREAD_run_once(&engine_capi
, ossl_init_engine_capi
))
606 if ((opts
& OPENSSL_INIT_ENGINE_DASYNC
)
607 && !CRYPTO_THREAD_run_once(&engine_dasync
, ossl_init_engine_dasync
))
609 # if !defined(OPENSSL_NO_AFALGENG)
610 if ((opts
& OPENSSL_INIT_ENGINE_AFALG
)
611 && !CRYPTO_THREAD_run_once(&engine_afalg
, ossl_init_engine_afalg
))
615 if (opts
& (OPENSSL_INIT_ENGINE_ALL_BUILTIN
616 | OPENSSL_INIT_ENGINE_DASYNC
| OPENSSL_INIT_ENGINE_OPENSSL
617 | OPENSSL_INIT_ENGINE_AFALG
)) {
618 ENGINE_register_all_complete();
622 #ifndef OPENSSL_NO_COMP
623 if ((opts
& OPENSSL_INIT_ZLIB
)
624 && CRYPTO_THREAD_run_once(&zlib
, ossl_init_zlib
))
631 int OPENSSL_atexit(void (*handler
)(void))
633 OPENSSL_INIT_STOP
*newhand
;
635 newhand
= OPENSSL_malloc(sizeof(*newhand
));
639 newhand
->handler
= handler
;
640 newhand
->next
= stop_handlers
;
641 stop_handlers
= newhand
;