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 #ifndef OPENSSL_NO_COMP
70 #include <openssl/comp.h>
72 #include <internal/err.h>
76 static int stopped = 0;
78 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
80 static CRYPTO_THREAD_LOCAL threadstopkey;
82 static void ossl_init_thread_stop_wrap(void *local)
84 ossl_init_thread_stop((struct thread_local_inits_st *)local);
87 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
89 struct thread_local_inits_st *local =
90 CRYPTO_THREAD_get_local(&threadstopkey);
92 if (local == NULL && alloc) {
93 local = OPENSSL_zalloc(sizeof *local);
94 CRYPTO_THREAD_set_local(&threadstopkey, local);
97 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
103 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
104 struct ossl_init_stop_st {
105 void (*handler)(void);
106 OPENSSL_INIT_STOP *next;
109 static OPENSSL_INIT_STOP *stop_handlers = NULL;
110 static CRYPTO_RWLOCK *init_lock = NULL;
112 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
113 static int base_inited = 0;
114 static void ossl_init_base(void)
116 #ifdef OPENSSL_INIT_DEBUG
117 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
120 * We use a dummy thread local key here. We use the destructor to detect
121 * when the thread is going to stop (where that feature is available)
123 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
124 #ifndef OPENSSL_SYS_UEFI
125 atexit(OPENSSL_cleanup);
127 init_lock = CRYPTO_THREAD_lock_new();
128 OPENSSL_cpuid_setup();
132 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
133 static int load_crypto_strings_inited = 0;
134 static void ossl_init_no_load_crypto_strings(void)
136 /* Do nothing in this case */
140 static void ossl_init_load_crypto_strings(void)
143 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
144 * pulling in all the error strings during static linking
146 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
147 # ifdef OPENSSL_INIT_DEBUG
148 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
149 "err_load_crypto_strings_intern()\n");
151 err_load_crypto_strings_intern();
153 load_crypto_strings_inited = 1;
156 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
157 static void ossl_init_add_all_ciphers(void)
160 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
161 * pulling in all the ciphers during static linking
163 #ifndef OPENSSL_NO_AUTOALGINIT
164 # ifdef OPENSSL_INIT_DEBUG
165 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
166 "openssl_add_all_ciphers_internal()\n");
168 openssl_add_all_ciphers_internal();
169 # ifndef OPENSSL_NO_ENGINE
170 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
171 ENGINE_setup_bsd_cryptodev();
177 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
178 static void ossl_init_add_all_digests(void)
181 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
182 * pulling in all the ciphers during static linking
184 #ifndef OPENSSL_NO_AUTOALGINIT
185 # ifdef OPENSSL_INIT_DEBUG
186 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
187 "openssl_add_all_digests_internal()\n");
189 openssl_add_all_digests_internal();
190 # ifndef OPENSSL_NO_ENGINE
191 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
192 ENGINE_setup_bsd_cryptodev();
198 static void ossl_init_no_add_algs(void)
204 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
205 static int config_inited = 0;
206 static const char *config_filename;
207 static void ossl_init_config(void)
209 #ifdef OPENSSL_INIT_DEBUG
211 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
212 config_filename==NULL?"NULL":config_filename);
214 openssl_config_internal(config_filename);
217 static void ossl_init_no_config(void)
219 #ifdef OPENSSL_INIT_DEBUG
221 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
223 openssl_no_config_internal();
227 #ifndef OPENSSL_NO_ASYNC
228 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
229 static int async_inited = 0;
230 static void ossl_init_async(void)
232 #ifdef OPENSSL_INIT_DEBUG
233 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
240 #ifndef OPENSSL_NO_ENGINE
241 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
242 static void ossl_init_engine_openssl(void)
244 # ifdef OPENSSL_INIT_DEBUG
245 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
246 "engine_load_openssl_internal()\n");
248 engine_load_openssl_internal();
250 # if !defined(OPENSSL_NO_HW) && \
251 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
252 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
253 static void ossl_init_engine_cryptodev(void)
255 # ifdef OPENSSL_INIT_DEBUG
256 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
257 "engine_load_cryptodev_internal()\n");
259 engine_load_cryptodev_internal();
263 # ifndef OPENSSL_NO_RDRAND
264 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
265 static void ossl_init_engine_rdrand(void)
267 # ifdef OPENSSL_INIT_DEBUG
268 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
269 "engine_load_rdrand_internal()\n");
271 engine_load_rdrand_internal();
274 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
275 static void ossl_init_engine_dynamic(void)
277 # ifdef OPENSSL_INIT_DEBUG
278 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
279 "engine_load_dynamic_internal()\n");
281 engine_load_dynamic_internal();
283 # ifndef OPENSSL_NO_STATIC_ENGINE
284 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
285 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
286 static void ossl_init_engine_padlock(void)
288 # ifdef OPENSSL_INIT_DEBUG
289 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
290 "engine_load_padlock_internal()\n");
292 engine_load_padlock_internal();
295 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
296 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
297 static void ossl_init_engine_capi(void)
299 # ifdef OPENSSL_INIT_DEBUG
300 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
301 "engine_load_capi_internal()\n");
303 engine_load_capi_internal();
306 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
307 static void ossl_init_engine_dasync(void)
309 # ifdef OPENSSL_INIT_DEBUG
310 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
311 "engine_load_dasync_internal()\n");
313 engine_load_dasync_internal();
315 # if !defined(OPENSSL_NO_AFALGENG)
316 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
317 static void ossl_init_engine_afalg(void)
319 # ifdef OPENSSL_INIT_DEBUG
320 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
321 "engine_load_afalg_internal()\n");
323 engine_load_afalg_internal();
329 #ifndef OPENSSL_NO_COMP
330 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
332 static int zlib_inited = 0;
333 static void ossl_init_zlib(void)
335 /* Do nothing - we need to know about this for the later cleanup */
340 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
342 /* Can't do much about this */
346 #ifndef OPENSSL_NO_ASYNC
348 #ifdef OPENSSL_INIT_DEBUG
349 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
350 "ASYNC_cleanup_thread()\n");
352 ASYNC_cleanup_thread();
356 if (locals->err_state) {
357 #ifdef OPENSSL_INIT_DEBUG
358 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
359 "ERR_remove_thread_state()\n");
361 ERR_remove_thread_state();
364 OPENSSL_free(locals);
367 void OPENSSL_thread_stop(void)
369 ossl_init_thread_stop(
370 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
373 int ossl_init_thread_start(uint64_t opts)
375 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
380 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
381 #ifdef OPENSSL_INIT_DEBUG
382 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
383 "marking thread for async\n");
388 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
389 #ifdef OPENSSL_INIT_DEBUG
390 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
391 "marking thread for err_state\n");
393 locals->err_state = 1;
399 void OPENSSL_cleanup(void)
401 OPENSSL_INIT_STOP *currhandler, *lasthandler;
403 /* If we've not been inited then no need to deinit */
407 /* Might be explicitly called and also by atexit */
413 * Thread stop may not get automatically called by the thread library for
414 * the very last thread in some situations, so call it directly.
416 ossl_init_thread_stop(ossl_init_get_thread_local(0));
418 currhandler = stop_handlers;
419 while (currhandler != NULL) {
420 currhandler->handler();
421 lasthandler = currhandler;
422 currhandler = currhandler->next;
423 OPENSSL_free(lasthandler);
425 stop_handlers = NULL;
427 CRYPTO_THREAD_lock_free(init_lock);
430 * We assume we are single-threaded for this function, i.e. no race
431 * conditions for the various "*_inited" vars below.
434 #ifndef OPENSSL_NO_COMP
436 #ifdef OPENSSL_INIT_DEBUG
437 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
438 "COMP_zlib_cleanup()\n");
444 #ifndef OPENSSL_NO_ASYNC
446 # ifdef OPENSSL_INIT_DEBUG
447 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
454 if (load_crypto_strings_inited) {
455 #ifdef OPENSSL_INIT_DEBUG
456 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
457 "ERR_free_strings()\n");
462 CRYPTO_THREAD_cleanup_local(&threadstopkey);
464 #ifdef OPENSSL_INIT_DEBUG
465 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
467 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
468 "CONF_modules_free()\n");
469 #ifndef OPENSSL_NO_ENGINE
470 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
471 "ENGINE_cleanup()\n");
473 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
474 "CRYPTO_cleanup_all_ex_data()\n");
475 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
476 "BIO_sock_cleanup()\n");
477 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
479 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
483 * Note that cleanup order is important:
484 * - RAND_cleanup could call an ENINGE's RAND cleanup function so must be
485 * called before ENGINE_cleanup()
486 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
487 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
488 * - CONF_modules_free() can end up in ENGINE code so must be called before
493 #ifndef OPENSSL_NO_ENGINE
496 CRYPTO_cleanup_all_ex_data();
504 * If this function is called with a non NULL settings value then it must be
505 * called prior to any threads making calls to any OpenSSL functions,
506 * i.e. passing a non-null settings value is assumed to be single-threaded.
508 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
510 static int stoperrset = 0;
515 * We only ever set this once to avoid getting into an infinite
516 * loop where the error system keeps trying to init and fails so
520 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
525 if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
528 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
529 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
530 ossl_init_no_load_crypto_strings))
533 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
534 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
535 ossl_init_load_crypto_strings))
538 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
539 && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
542 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
543 && !CRYPTO_THREAD_run_once(&add_all_ciphers,
544 ossl_init_add_all_ciphers))
547 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
548 && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
551 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
552 && !CRYPTO_THREAD_run_once(&add_all_digests,
553 ossl_init_add_all_digests))
556 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
557 && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
560 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
562 CRYPTO_THREAD_write_lock(init_lock);
563 config_filename = (settings == NULL) ? NULL : settings->config_name;
564 ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
565 CRYPTO_THREAD_unlock(init_lock);
570 #ifndef OPENSSL_NO_ASYNC
571 if ((opts & OPENSSL_INIT_ASYNC)
572 && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
575 #ifndef OPENSSL_NO_ENGINE
576 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
577 && !CRYPTO_THREAD_run_once(&engine_openssl,
578 ossl_init_engine_openssl))
580 # if !defined(OPENSSL_NO_HW) && \
581 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
582 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
583 && !CRYPTO_THREAD_run_once(&engine_cryptodev,
584 ossl_init_engine_cryptodev))
587 # ifndef OPENSSL_NO_RDRAND
588 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
589 && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
592 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
593 && !CRYPTO_THREAD_run_once(&engine_dynamic,
594 ossl_init_engine_dynamic))
596 # ifndef OPENSSL_NO_STATIC_ENGINE
597 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
598 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
599 && !CRYPTO_THREAD_run_once(&engine_padlock,
600 ossl_init_engine_padlock))
603 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
604 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
605 && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
608 if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
609 && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
611 # if !defined(OPENSSL_NO_AFALGENG)
612 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
613 && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
617 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
618 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
619 | OPENSSL_INIT_ENGINE_AFALG)) {
620 ENGINE_register_all_complete();
624 #ifndef OPENSSL_NO_COMP
625 if ((opts & OPENSSL_INIT_ZLIB)
626 && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
633 int OPENSSL_atexit(void (*handler)(void))
635 OPENSSL_INIT_STOP *newhand;
637 newhand = OPENSSL_malloc(sizeof(*newhand));
641 newhand->handler = handler;
642 newhand->next = stop_handlers;
643 stop_handlers = newhand;