2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <internal/cryptlib_int.h>
11 #include <openssl/err.h>
12 #include <internal/rand.h>
13 #include <internal/bio.h>
14 #include <openssl/evp.h>
15 #include <internal/evp_int.h>
16 #include <internal/conf.h>
17 #include <internal/async.h>
18 #include <internal/engine.h>
19 #include <internal/comp.h>
20 #include <internal/err.h>
21 #include <internal/err_int.h>
22 #include <internal/objects.h>
25 #include <internal/thread_once.h>
26 #include <internal/dso.h>
28 static int stopped
= 0;
30 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
);
32 static CRYPTO_THREAD_LOCAL threadstopkey
;
34 static void ossl_init_thread_stop_wrap(void *local
)
36 ossl_init_thread_stop((struct thread_local_inits_st
*)local
);
39 static struct thread_local_inits_st
*ossl_init_get_thread_local(int alloc
)
41 struct thread_local_inits_st
*local
=
42 CRYPTO_THREAD_get_local(&threadstopkey
);
44 if (local
== NULL
&& alloc
) {
45 local
= OPENSSL_zalloc(sizeof *local
);
46 if (local
!= NULL
&& !CRYPTO_THREAD_set_local(&threadstopkey
, local
)) {
52 CRYPTO_THREAD_set_local(&threadstopkey
, NULL
);
58 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP
;
59 struct ossl_init_stop_st
{
60 void (*handler
)(void);
61 OPENSSL_INIT_STOP
*next
;
64 static OPENSSL_INIT_STOP
*stop_handlers
= NULL
;
65 static CRYPTO_RWLOCK
*init_lock
= NULL
;
67 static CRYPTO_ONCE base
= CRYPTO_ONCE_STATIC_INIT
;
68 static int base_inited
= 0;
69 DEFINE_RUN_ONCE_STATIC(ossl_init_base
)
71 #ifdef OPENSSL_INIT_DEBUG
72 fprintf(stderr
, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
74 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
75 ossl_malloc_setup_failures();
78 * We use a dummy thread local key here. We use the destructor to detect
79 * when the thread is going to stop (where that feature is available)
81 CRYPTO_THREAD_init_local(&threadstopkey
, ossl_init_thread_stop_wrap
);
82 #ifndef OPENSSL_SYS_UEFI
83 atexit(OPENSSL_cleanup
);
85 if ((init_lock
= CRYPTO_THREAD_lock_new()) == NULL
)
87 OPENSSL_cpuid_setup();
91 * Everything needed to be initialized in this function before threads
92 * come along MUST happen before base_inited is set to 1, or we will
93 * see race conditions.
97 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
100 HMODULE handle
= NULL
;
103 /* We don't use the DSO route for WIN32 because there is a better way */
104 ret
= GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
105 | GET_MODULE_HANDLE_EX_FLAG_PIN
,
106 (void *)&base_inited
, &handle
);
108 return (ret
== TRUE
) ? 1 : 0;
112 * Deliberately leak a reference to ourselves. This will force the library
113 * to remain loaded until the atexit() handler is run at process exit.
119 dso
= DSO_dsobyaddr(&base_inited
, DSO_FLAG_NO_UNLOAD_ON_FREE
);
129 static CRYPTO_ONCE load_crypto_strings
= CRYPTO_ONCE_STATIC_INIT
;
130 static int load_crypto_strings_inited
= 0;
131 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings
)
133 /* Do nothing in this case */
137 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings
)
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_int()\n");
149 ret
= err_load_crypto_strings_int();
150 load_crypto_strings_inited
= 1;
155 static CRYPTO_ONCE add_all_ciphers
= CRYPTO_ONCE_STATIC_INIT
;
156 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers
)
159 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
160 * pulling in all the ciphers during static linking
162 #ifndef OPENSSL_NO_AUTOALGINIT
163 # ifdef OPENSSL_INIT_DEBUG
164 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
165 "openssl_add_all_ciphers_int()\n");
167 openssl_add_all_ciphers_int();
172 static CRYPTO_ONCE add_all_digests
= CRYPTO_ONCE_STATIC_INIT
;
173 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests
)
176 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
177 * pulling in all the ciphers during static linking
179 #ifndef OPENSSL_NO_AUTOALGINIT
180 # ifdef OPENSSL_INIT_DEBUG
181 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_digests: "
182 "openssl_add_all_digests()\n");
184 openssl_add_all_digests_int();
189 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs
)
195 static CRYPTO_ONCE config
= CRYPTO_ONCE_STATIC_INIT
;
196 static int config_inited
= 0;
197 static const char *appname
;
198 DEFINE_RUN_ONCE_STATIC(ossl_init_config
)
200 #ifdef OPENSSL_INIT_DEBUG
202 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
203 appname
== NULL
? "NULL" : appname
);
205 openssl_config_int(appname
);
209 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config
)
211 #ifdef OPENSSL_INIT_DEBUG
213 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
215 openssl_no_config_int();
220 static CRYPTO_ONCE async
= CRYPTO_ONCE_STATIC_INIT
;
221 static int async_inited
= 0;
222 DEFINE_RUN_ONCE_STATIC(ossl_init_async
)
224 #ifdef OPENSSL_INIT_DEBUG
225 fprintf(stderr
, "OPENSSL_INIT: ossl_init_async: async_init()\n");
233 #ifndef OPENSSL_NO_ENGINE
234 static CRYPTO_ONCE engine_openssl
= CRYPTO_ONCE_STATIC_INIT
;
235 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl
)
237 # ifdef OPENSSL_INIT_DEBUG
238 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_openssl: "
239 "engine_load_openssl_int()\n");
241 engine_load_openssl_int();
245 # ifndef OPENSSL_NO_RDRAND
246 static CRYPTO_ONCE engine_rdrand
= CRYPTO_ONCE_STATIC_INIT
;
247 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand
)
249 # ifdef OPENSSL_INIT_DEBUG
250 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_rdrand: "
251 "engine_load_rdrand_int()\n");
253 engine_load_rdrand_int();
257 static CRYPTO_ONCE engine_dynamic
= CRYPTO_ONCE_STATIC_INIT
;
258 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic
)
260 # ifdef OPENSSL_INIT_DEBUG
261 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_dynamic: "
262 "engine_load_dynamic_int()\n");
264 engine_load_dynamic_int();
267 # ifndef OPENSSL_NO_STATIC_ENGINE
268 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
269 static CRYPTO_ONCE engine_padlock
= CRYPTO_ONCE_STATIC_INIT
;
270 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock
)
272 # ifdef OPENSSL_INIT_DEBUG
273 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_padlock: "
274 "engine_load_padlock_int()\n");
276 engine_load_padlock_int();
280 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
281 static CRYPTO_ONCE engine_capi
= CRYPTO_ONCE_STATIC_INIT
;
282 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi
)
284 # ifdef OPENSSL_INIT_DEBUG
285 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_capi: "
286 "engine_load_capi_int()\n");
288 engine_load_capi_int();
292 # if !defined(OPENSSL_NO_AFALGENG)
293 static CRYPTO_ONCE engine_afalg
= CRYPTO_ONCE_STATIC_INIT
;
294 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg
)
296 # ifdef OPENSSL_INIT_DEBUG
297 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_afalg: "
298 "engine_load_afalg_int()\n");
300 engine_load_afalg_int();
307 #ifndef OPENSSL_NO_COMP
308 static CRYPTO_ONCE zlib
= CRYPTO_ONCE_STATIC_INIT
;
310 static int zlib_inited
= 0;
311 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib
)
313 /* Do nothing - we need to know about this for the later cleanup */
319 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
)
321 /* Can't do much about this */
326 #ifdef OPENSSL_INIT_DEBUG
327 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
328 "ASYNC_cleanup_thread()\n");
330 ASYNC_cleanup_thread();
333 if (locals
->err_state
) {
334 #ifdef OPENSSL_INIT_DEBUG
335 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
336 "err_delete_thread_state()\n");
338 err_delete_thread_state();
341 OPENSSL_free(locals
);
344 void OPENSSL_thread_stop(void)
346 ossl_init_thread_stop(
347 (struct thread_local_inits_st
*)ossl_init_get_thread_local(0));
350 int ossl_init_thread_start(uint64_t opts
)
352 struct thread_local_inits_st
*locals
;
354 if (!OPENSSL_init_crypto(0, NULL
))
357 locals
= ossl_init_get_thread_local(1);
362 if (opts
& OPENSSL_INIT_THREAD_ASYNC
) {
363 #ifdef OPENSSL_INIT_DEBUG
364 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
365 "marking thread for async\n");
370 if (opts
& OPENSSL_INIT_THREAD_ERR_STATE
) {
371 #ifdef OPENSSL_INIT_DEBUG
372 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
373 "marking thread for err_state\n");
375 locals
->err_state
= 1;
381 void OPENSSL_cleanup(void)
383 OPENSSL_INIT_STOP
*currhandler
, *lasthandler
;
385 /* If we've not been inited then no need to deinit */
389 /* Might be explicitly called and also by atexit */
395 * Thread stop may not get automatically called by the thread library for
396 * the very last thread in some situations, so call it directly.
398 ossl_init_thread_stop(ossl_init_get_thread_local(0));
400 currhandler
= stop_handlers
;
401 while (currhandler
!= NULL
) {
402 currhandler
->handler();
403 lasthandler
= currhandler
;
404 currhandler
= currhandler
->next
;
405 OPENSSL_free(lasthandler
);
407 stop_handlers
= NULL
;
409 CRYPTO_THREAD_lock_free(init_lock
);
412 * We assume we are single-threaded for this function, i.e. no race
413 * conditions for the various "*_inited" vars below.
416 #ifndef OPENSSL_NO_COMP
418 #ifdef OPENSSL_INIT_DEBUG
419 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
420 "comp_zlib_cleanup_int()\n");
422 comp_zlib_cleanup_int();
427 # ifdef OPENSSL_INIT_DEBUG
428 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
434 if (load_crypto_strings_inited
) {
435 #ifdef OPENSSL_INIT_DEBUG
436 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
437 "err_free_strings_int()\n");
439 err_free_strings_int();
442 CRYPTO_THREAD_cleanup_local(&threadstopkey
);
444 #ifdef OPENSSL_INIT_DEBUG
445 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
446 "rand_cleanup_int()\n");
447 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
448 "conf_modules_free_int()\n");
449 #ifndef OPENSSL_NO_ENGINE
450 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
451 "engine_cleanup_int()\n");
453 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
454 "crypto_cleanup_all_ex_data_int()\n");
455 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
456 "bio_sock_cleanup_int()\n");
457 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
459 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
460 "evp_cleanup_int()\n");
461 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
462 "obj_cleanup_int()\n");
463 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
467 * Note that cleanup order is important:
468 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
469 * must be called before engine_cleanup_int()
470 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
471 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
472 * - conf_modules_free_int() can end up in ENGINE code so must be called
473 * before engine_cleanup_int()
474 * - ENGINEs and additional EVP algorithms might use added OIDs names so
475 * obj_cleanup_int() must be called last
478 conf_modules_free_int();
479 #ifndef OPENSSL_NO_ENGINE
480 engine_cleanup_int();
482 crypto_cleanup_all_ex_data_int();
492 * If this function is called with a non NULL settings value then it must be
493 * called prior to any threads making calls to any OpenSSL functions,
494 * i.e. passing a non-null settings value is assumed to be single-threaded.
496 int OPENSSL_init_crypto(uint64_t opts
, const OPENSSL_INIT_SETTINGS
*settings
)
498 static int stoperrset
= 0;
503 * We only ever set this once to avoid getting into an infinite
504 * loop where the error system keeps trying to init and fails so
508 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO
, ERR_R_INIT_FAIL
);
513 if (!base_inited
&& !RUN_ONCE(&base
, ossl_init_base
))
516 if ((opts
& OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS
)
517 && !RUN_ONCE(&load_crypto_strings
,
518 ossl_init_no_load_crypto_strings
))
521 if ((opts
& OPENSSL_INIT_LOAD_CRYPTO_STRINGS
)
522 && !RUN_ONCE(&load_crypto_strings
, ossl_init_load_crypto_strings
))
525 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_CIPHERS
)
526 && !RUN_ONCE(&add_all_ciphers
, ossl_init_no_add_algs
))
529 if ((opts
& OPENSSL_INIT_ADD_ALL_CIPHERS
)
530 && !RUN_ONCE(&add_all_ciphers
, ossl_init_add_all_ciphers
))
533 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_DIGESTS
)
534 && !RUN_ONCE(&add_all_digests
, ossl_init_no_add_algs
))
537 if ((opts
& OPENSSL_INIT_ADD_ALL_DIGESTS
)
538 && !RUN_ONCE(&add_all_digests
, ossl_init_add_all_digests
))
541 if ((opts
& OPENSSL_INIT_NO_LOAD_CONFIG
)
542 && !RUN_ONCE(&config
, ossl_init_no_config
))
545 if (opts
& OPENSSL_INIT_LOAD_CONFIG
) {
547 CRYPTO_THREAD_write_lock(init_lock
);
548 appname
= (settings
== NULL
) ? NULL
: settings
->appname
;
549 ret
= RUN_ONCE(&config
, ossl_init_config
);
550 CRYPTO_THREAD_unlock(init_lock
);
555 if ((opts
& OPENSSL_INIT_ASYNC
)
556 && !RUN_ONCE(&async
, ossl_init_async
))
559 #ifndef OPENSSL_NO_ENGINE
560 if ((opts
& OPENSSL_INIT_ENGINE_OPENSSL
)
561 && !RUN_ONCE(&engine_openssl
, ossl_init_engine_openssl
))
563 # ifndef OPENSSL_NO_RDRAND
564 if ((opts
& OPENSSL_INIT_ENGINE_RDRAND
)
565 && !RUN_ONCE(&engine_rdrand
, ossl_init_engine_rdrand
))
568 if ((opts
& OPENSSL_INIT_ENGINE_DYNAMIC
)
569 && !RUN_ONCE(&engine_dynamic
, ossl_init_engine_dynamic
))
571 # ifndef OPENSSL_NO_STATIC_ENGINE
572 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
573 if ((opts
& OPENSSL_INIT_ENGINE_PADLOCK
)
574 && !RUN_ONCE(&engine_padlock
, ossl_init_engine_padlock
))
577 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
578 if ((opts
& OPENSSL_INIT_ENGINE_CAPI
)
579 && !RUN_ONCE(&engine_capi
, ossl_init_engine_capi
))
582 # if !defined(OPENSSL_NO_AFALGENG)
583 if ((opts
& OPENSSL_INIT_ENGINE_AFALG
)
584 && !RUN_ONCE(&engine_afalg
, ossl_init_engine_afalg
))
588 if (opts
& (OPENSSL_INIT_ENGINE_ALL_BUILTIN
589 | OPENSSL_INIT_ENGINE_OPENSSL
590 | OPENSSL_INIT_ENGINE_AFALG
)) {
591 ENGINE_register_all_complete();
595 #ifndef OPENSSL_NO_COMP
596 if ((opts
& OPENSSL_INIT_ZLIB
)
597 && !RUN_ONCE(&zlib
, ossl_init_zlib
))
604 int OPENSSL_atexit(void (*handler
)(void))
606 OPENSSL_INIT_STOP
*newhand
;
608 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
615 handlersym
.func
= handler
;
618 HMODULE handle
= NULL
;
622 * We don't use the DSO route for WIN32 because there is a better
625 ret
= GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
626 | GET_MODULE_HANDLE_EX_FLAG_PIN
,
627 handlersym
.sym
, &handle
);
634 * Deliberately leak a reference to the handler. This will force the
635 * library/code containing the handler to remain loaded until we run the
636 * atexit handler. If -znodelete has been used then this is
643 dso
= DSO_dsobyaddr(handlersym
.sym
, DSO_FLAG_NO_UNLOAD_ON_FREE
);
651 newhand
= OPENSSL_malloc(sizeof(*newhand
));
655 newhand
->handler
= handler
;
656 newhand
->next
= stop_handlers
;
657 stop_handlers
= newhand
;