2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include "internal/cryptlib_int.h"
12 #include <openssl/err.h>
13 #include "internal/rand_int.h"
14 #include "internal/bio.h"
15 #include <openssl/evp.h>
16 #include "internal/evp_int.h"
17 #include "internal/conf.h"
18 #include "internal/async.h"
19 #include "internal/engine.h"
20 #include "internal/comp.h"
21 #include "internal/err.h"
22 #include "internal/err_int.h"
23 #include "internal/objects.h"
26 #include "internal/thread_once.h"
27 #include "internal/dso_conf.h"
28 #include "internal/dso.h"
29 #include "internal/store.h"
31 static int stopped
= 0;
34 * Since per-thread-specific-data destructors are not universally
35 * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key
36 * is assumed to have destructor associated. And then an effort is made
37 * to call this single destructor on non-pthread platform[s].
39 * Initial value is "impossible". It is used as guard value to shortcut
40 * destructor for threads terminating before libcrypto is initialized or
41 * after it's de-initialized. Access to the key doesn't have to be
42 * serialized for the said threads, because they didn't use libcrypto
43 * and it doesn't matter if they pick "impossible" or derefernce real
44 * key value and pull NULL past initialization in the first thread that
45 * intends to use libcrypto.
49 CRYPTO_THREAD_LOCAL value
;
50 } destructor_key
= { -1 };
52 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
);
54 static void ossl_init_thread_destructor(void *local
)
56 ossl_init_thread_stop((struct thread_local_inits_st
*)local
);
59 static struct thread_local_inits_st
*ossl_init_get_thread_local(int alloc
)
61 struct thread_local_inits_st
*local
=
62 CRYPTO_THREAD_get_local(&destructor_key
.value
);
66 && (local
= OPENSSL_zalloc(sizeof(*local
))) != NULL
67 && !CRYPTO_THREAD_set_local(&destructor_key
.value
, local
)) {
72 CRYPTO_THREAD_set_local(&destructor_key
.value
, NULL
);
78 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP
;
79 struct ossl_init_stop_st
{
80 void (*handler
)(void);
81 OPENSSL_INIT_STOP
*next
;
84 static OPENSSL_INIT_STOP
*stop_handlers
= NULL
;
85 static CRYPTO_RWLOCK
*init_lock
= NULL
;
87 static CRYPTO_ONCE base
= CRYPTO_ONCE_STATIC_INIT
;
88 static int base_inited
= 0;
89 DEFINE_RUN_ONCE_STATIC(ossl_init_base
)
91 CRYPTO_THREAD_LOCAL key
;
93 #ifdef OPENSSL_INIT_DEBUG
94 fprintf(stderr
, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
96 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
97 ossl_malloc_setup_failures();
99 if (!CRYPTO_THREAD_init_local(&key
, ossl_init_thread_destructor
))
101 if ((init_lock
= CRYPTO_THREAD_lock_new()) == NULL
)
103 OPENSSL_cpuid_setup();
105 destructor_key
.value
= key
;
110 #ifdef OPENSSL_INIT_DEBUG
111 fprintf(stderr
, "OPENSSL_INIT: ossl_init_base not ok!\n");
113 CRYPTO_THREAD_lock_free(init_lock
);
116 CRYPTO_THREAD_cleanup_local(&key
);
120 static CRYPTO_ONCE register_atexit
= CRYPTO_ONCE_STATIC_INIT
;
121 #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
122 static int win32atexit(void)
129 DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit
)
131 #ifdef OPENSSL_INIT_DEBUG
132 fprintf(stderr
, "OPENSSL_INIT: ossl_init_register_atexit()\n");
134 #ifndef OPENSSL_SYS_UEFI
136 /* We use _onexit() in preference because it gets called on DLL unload */
137 if (_onexit(win32atexit
) == NULL
)
140 if (atexit(OPENSSL_cleanup
) != 0)
148 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit
,
149 ossl_init_register_atexit
)
151 #ifdef OPENSSL_INIT_DEBUG
152 fprintf(stderr
, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n");
154 /* Do nothing in this case */
158 static CRYPTO_ONCE load_crypto_nodelete
= CRYPTO_ONCE_STATIC_INIT
;
159 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete
)
161 #ifdef OPENSSL_INIT_DEBUG
162 fprintf(stderr
, "OPENSSL_INIT: ossl_init_load_crypto_nodelete()\n");
164 #if !defined(OPENSSL_NO_DSO) \
165 && !defined(OPENSSL_USE_NODELETE) \
166 && !defined(OPENSSL_NO_PINSHARED)
169 HMODULE handle
= NULL
;
172 /* We don't use the DSO route for WIN32 because there is a better way */
173 ret
= GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
174 | GET_MODULE_HANDLE_EX_FLAG_PIN
,
175 (void *)&base_inited
, &handle
);
177 # ifdef OPENSSL_INIT_DEBUG
178 fprintf(stderr
, "OPENSSL_INIT: obtained DSO reference? %s\n",
179 (ret
== TRUE
? "No!" : "Yes."));
181 return (ret
== TRUE
) ? 1 : 0;
185 * Deliberately leak a reference to ourselves. This will force the library
186 * to remain loaded until the atexit() handler is run at process exit.
192 if (!err_shelve_state(&err
))
195 dso
= DSO_dsobyaddr(&base_inited
, DSO_FLAG_NO_UNLOAD_ON_FREE
);
196 # ifdef OPENSSL_INIT_DEBUG
197 fprintf(stderr
, "OPENSSL_INIT: obtained DSO reference? %s\n",
198 (dso
== NULL
? "No!" : "Yes."));
200 * In case of No!, it is uncertain our exit()-handlers can still be
201 * called. After dlclose() the whole library might have been unloaded
206 err_unshelve_state(err
);
214 static CRYPTO_ONCE load_crypto_strings
= CRYPTO_ONCE_STATIC_INIT
;
215 static int load_crypto_strings_inited
= 0;
216 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings
)
220 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
221 * pulling in all the error strings during static linking
223 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
224 # ifdef OPENSSL_INIT_DEBUG
225 fprintf(stderr
, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
226 "err_load_crypto_strings_int()\n");
228 ret
= err_load_crypto_strings_int();
229 load_crypto_strings_inited
= 1;
234 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings
,
235 ossl_init_load_crypto_strings
)
237 /* Do nothing in this case */
241 static CRYPTO_ONCE add_all_ciphers
= CRYPTO_ONCE_STATIC_INIT
;
242 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers
)
245 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
246 * pulling in all the ciphers during static linking
248 #ifndef OPENSSL_NO_AUTOALGINIT
249 # ifdef OPENSSL_INIT_DEBUG
250 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
251 "openssl_add_all_ciphers_int()\n");
253 openssl_add_all_ciphers_int();
258 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers
,
259 ossl_init_add_all_ciphers
)
265 static CRYPTO_ONCE add_all_digests
= CRYPTO_ONCE_STATIC_INIT
;
266 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests
)
269 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
270 * pulling in all the ciphers during static linking
272 #ifndef OPENSSL_NO_AUTOALGINIT
273 # ifdef OPENSSL_INIT_DEBUG
274 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_digests: "
275 "openssl_add_all_digests()\n");
277 openssl_add_all_digests_int();
282 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests
,
283 ossl_init_add_all_digests
)
289 static CRYPTO_ONCE add_all_macs
= CRYPTO_ONCE_STATIC_INIT
;
290 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_macs
)
293 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
294 * pulling in all the macs during static linking
296 #ifndef OPENSSL_NO_AUTOALGINIT
297 # ifdef OPENSSL_INIT_DEBUG
298 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_macs: "
299 "openssl_add_all_macs_int()\n");
301 openssl_add_all_macs_int();
306 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_macs
, ossl_init_add_all_macs
)
312 static CRYPTO_ONCE config
= CRYPTO_ONCE_STATIC_INIT
;
313 static int config_inited
= 0;
314 static const OPENSSL_INIT_SETTINGS
*conf_settings
= NULL
;
315 DEFINE_RUN_ONCE_STATIC(ossl_init_config
)
317 int ret
= openssl_config_int(conf_settings
);
321 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config
, ossl_init_config
)
323 #ifdef OPENSSL_INIT_DEBUG
325 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
327 openssl_no_config_int();
332 static CRYPTO_ONCE async
= CRYPTO_ONCE_STATIC_INIT
;
333 static int async_inited
= 0;
334 DEFINE_RUN_ONCE_STATIC(ossl_init_async
)
336 #ifdef OPENSSL_INIT_DEBUG
337 fprintf(stderr
, "OPENSSL_INIT: ossl_init_async: async_init()\n");
345 #ifndef OPENSSL_NO_ENGINE
346 static CRYPTO_ONCE engine_openssl
= CRYPTO_ONCE_STATIC_INIT
;
347 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl
)
349 # ifdef OPENSSL_INIT_DEBUG
350 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_openssl: "
351 "engine_load_openssl_int()\n");
353 engine_load_openssl_int();
357 # ifndef OPENSSL_NO_RDRAND
358 static CRYPTO_ONCE engine_rdrand
= CRYPTO_ONCE_STATIC_INIT
;
359 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand
)
361 # ifdef OPENSSL_INIT_DEBUG
362 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_rdrand: "
363 "engine_load_rdrand_int()\n");
365 engine_load_rdrand_int();
369 static CRYPTO_ONCE engine_dynamic
= CRYPTO_ONCE_STATIC_INIT
;
370 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic
)
372 # ifdef OPENSSL_INIT_DEBUG
373 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_dynamic: "
374 "engine_load_dynamic_int()\n");
376 engine_load_dynamic_int();
379 # ifndef OPENSSL_NO_STATIC_ENGINE
380 # ifndef OPENSSL_NO_DEVCRYPTOENG
381 static CRYPTO_ONCE engine_devcrypto
= CRYPTO_ONCE_STATIC_INIT
;
382 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto
)
384 # ifdef OPENSSL_INIT_DEBUG
385 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
386 "engine_load_devcrypto_int()\n");
388 engine_load_devcrypto_int();
392 # if !defined(OPENSSL_NO_PADLOCKENG)
393 static CRYPTO_ONCE engine_padlock
= CRYPTO_ONCE_STATIC_INIT
;
394 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock
)
396 # ifdef OPENSSL_INIT_DEBUG
397 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_padlock: "
398 "engine_load_padlock_int()\n");
400 engine_load_padlock_int();
404 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
405 static CRYPTO_ONCE engine_capi
= CRYPTO_ONCE_STATIC_INIT
;
406 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi
)
408 # ifdef OPENSSL_INIT_DEBUG
409 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_capi: "
410 "engine_load_capi_int()\n");
412 engine_load_capi_int();
416 # if !defined(OPENSSL_NO_AFALGENG)
417 static CRYPTO_ONCE engine_afalg
= CRYPTO_ONCE_STATIC_INIT
;
418 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg
)
420 # ifdef OPENSSL_INIT_DEBUG
421 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_afalg: "
422 "engine_load_afalg_int()\n");
424 engine_load_afalg_int();
431 #ifndef OPENSSL_NO_COMP
432 static CRYPTO_ONCE zlib
= CRYPTO_ONCE_STATIC_INIT
;
434 static int zlib_inited
= 0;
435 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib
)
437 /* Do nothing - we need to know about this for the later cleanup */
443 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
)
445 /* Can't do much about this */
450 #ifdef OPENSSL_INIT_DEBUG
451 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
452 "async_delete_thread_state()\n");
454 async_delete_thread_state();
457 if (locals
->err_state
) {
458 #ifdef OPENSSL_INIT_DEBUG
459 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
460 "err_delete_thread_state()\n");
462 err_delete_thread_state();
466 #ifdef OPENSSL_INIT_DEBUG
467 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
468 "drbg_delete_thread_state()\n");
470 drbg_delete_thread_state();
473 OPENSSL_free(locals
);
476 void OPENSSL_thread_stop(void)
478 if (destructor_key
.sane
!= -1)
479 ossl_init_thread_stop(ossl_init_get_thread_local(0));
482 int ossl_init_thread_start(uint64_t opts
)
484 struct thread_local_inits_st
*locals
;
486 if (!OPENSSL_init_crypto(0, NULL
))
489 locals
= ossl_init_get_thread_local(1);
494 if (opts
& OPENSSL_INIT_THREAD_ASYNC
) {
495 #ifdef OPENSSL_INIT_DEBUG
496 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
497 "marking thread for async\n");
502 if (opts
& OPENSSL_INIT_THREAD_ERR_STATE
) {
503 #ifdef OPENSSL_INIT_DEBUG
504 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
505 "marking thread for err_state\n");
507 locals
->err_state
= 1;
510 if (opts
& OPENSSL_INIT_THREAD_RAND
) {
511 #ifdef OPENSSL_INIT_DEBUG
512 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
513 "marking thread for rand\n");
521 void OPENSSL_cleanup(void)
523 OPENSSL_INIT_STOP
*currhandler
, *lasthandler
;
524 CRYPTO_THREAD_LOCAL key
;
526 /* If we've not been inited then no need to deinit */
530 /* Might be explicitly called and also by atexit */
536 * Thread stop may not get automatically called by the thread library for
537 * the very last thread in some situations, so call it directly.
539 ossl_init_thread_stop(ossl_init_get_thread_local(0));
541 currhandler
= stop_handlers
;
542 while (currhandler
!= NULL
) {
543 currhandler
->handler();
544 lasthandler
= currhandler
;
545 currhandler
= currhandler
->next
;
546 OPENSSL_free(lasthandler
);
548 stop_handlers
= NULL
;
550 CRYPTO_THREAD_lock_free(init_lock
);
554 * We assume we are single-threaded for this function, i.e. no race
555 * conditions for the various "*_inited" vars below.
558 #ifndef OPENSSL_NO_COMP
560 #ifdef OPENSSL_INIT_DEBUG
561 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
562 "comp_zlib_cleanup_int()\n");
564 comp_zlib_cleanup_int();
569 # ifdef OPENSSL_INIT_DEBUG
570 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
576 if (load_crypto_strings_inited
) {
577 #ifdef OPENSSL_INIT_DEBUG
578 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
579 "err_free_strings_int()\n");
581 err_free_strings_int();
584 key
= destructor_key
.value
;
585 destructor_key
.sane
= -1;
586 CRYPTO_THREAD_cleanup_local(&key
);
588 #ifdef OPENSSL_INIT_DEBUG
589 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
590 "rand_cleanup_int()\n");
591 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
592 "conf_modules_free_int()\n");
593 #ifndef OPENSSL_NO_ENGINE
594 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
595 "engine_cleanup_int()\n");
597 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
598 "crypto_cleanup_all_ex_data_int()\n");
599 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
600 "bio_sock_cleanup_int()\n");
601 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
603 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
604 "evp_cleanup_int()\n");
605 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
606 "obj_cleanup_int()\n");
607 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_cleanup: "
611 * Note that cleanup order is important:
612 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
613 * must be called before engine_cleanup_int()
614 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
615 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
616 * - conf_modules_free_int() can end up in ENGINE code so must be called
617 * before engine_cleanup_int()
618 * - ENGINEs and additional EVP algorithms might use added OIDs names so
619 * obj_cleanup_int() must be called last
622 rand_drbg_cleanup_int();
623 conf_modules_free_int();
624 #ifndef OPENSSL_NO_ENGINE
625 engine_cleanup_int();
627 ossl_store_cleanup_int();
628 crypto_cleanup_all_ex_data_int();
634 CRYPTO_secure_malloc_done();
640 * If this function is called with a non NULL settings value then it must be
641 * called prior to any threads making calls to any OpenSSL functions,
642 * i.e. passing a non-null settings value is assumed to be single-threaded.
644 int OPENSSL_init_crypto(uint64_t opts
, const OPENSSL_INIT_SETTINGS
*settings
)
647 if (!(opts
& OPENSSL_INIT_BASE_ONLY
))
648 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO
, ERR_R_INIT_FAIL
);
653 * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
654 * *only* option specified. With that option we return immediately after
655 * doing the requested limited initialization. Note that
656 * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
657 * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
658 * base already initialized this is a harmless NOOP.
660 * If we remain the only caller of err_shelve_state() the recursion should
661 * perhaps be removed, but if in doubt, it can be left in place.
663 if (!RUN_ONCE(&base
, ossl_init_base
))
666 if (opts
& OPENSSL_INIT_BASE_ONLY
)
670 * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
671 * should not have the side-effect of setting up exit handlers, and
672 * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
675 if ((opts
& OPENSSL_INIT_NO_ATEXIT
) != 0) {
676 if (!RUN_ONCE_ALT(®ister_atexit
, ossl_init_no_register_atexit
,
677 ossl_init_register_atexit
))
679 } else if (!RUN_ONCE(®ister_atexit
, ossl_init_register_atexit
)) {
683 if (!RUN_ONCE(&load_crypto_nodelete
, ossl_init_load_crypto_nodelete
))
686 if ((opts
& OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS
)
687 && !RUN_ONCE_ALT(&load_crypto_strings
,
688 ossl_init_no_load_crypto_strings
,
689 ossl_init_load_crypto_strings
))
692 if ((opts
& OPENSSL_INIT_LOAD_CRYPTO_STRINGS
)
693 && !RUN_ONCE(&load_crypto_strings
, ossl_init_load_crypto_strings
))
696 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_CIPHERS
)
697 && !RUN_ONCE_ALT(&add_all_ciphers
, ossl_init_no_add_all_ciphers
,
698 ossl_init_add_all_ciphers
))
701 if ((opts
& OPENSSL_INIT_ADD_ALL_CIPHERS
)
702 && !RUN_ONCE(&add_all_ciphers
, ossl_init_add_all_ciphers
))
705 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_DIGESTS
)
706 && !RUN_ONCE_ALT(&add_all_digests
, ossl_init_no_add_all_digests
,
707 ossl_init_add_all_digests
))
710 if ((opts
& OPENSSL_INIT_ADD_ALL_DIGESTS
)
711 && !RUN_ONCE(&add_all_digests
, ossl_init_add_all_digests
))
714 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_MACS
)
715 && !RUN_ONCE_ALT(&add_all_macs
, ossl_init_no_add_all_macs
,
716 ossl_init_add_all_macs
))
719 if ((opts
& OPENSSL_INIT_ADD_ALL_MACS
)
720 && !RUN_ONCE(&add_all_macs
, ossl_init_add_all_macs
))
723 if ((opts
& OPENSSL_INIT_ATFORK
)
724 && !openssl_init_fork_handlers())
727 if ((opts
& OPENSSL_INIT_NO_LOAD_CONFIG
)
728 && !RUN_ONCE_ALT(&config
, ossl_init_no_config
, ossl_init_config
))
731 if (opts
& OPENSSL_INIT_LOAD_CONFIG
) {
733 CRYPTO_THREAD_write_lock(init_lock
);
734 conf_settings
= settings
;
735 ret
= RUN_ONCE(&config
, ossl_init_config
);
736 conf_settings
= NULL
;
737 CRYPTO_THREAD_unlock(init_lock
);
742 if ((opts
& OPENSSL_INIT_ASYNC
)
743 && !RUN_ONCE(&async
, ossl_init_async
))
746 #ifndef OPENSSL_NO_ENGINE
747 if ((opts
& OPENSSL_INIT_ENGINE_OPENSSL
)
748 && !RUN_ONCE(&engine_openssl
, ossl_init_engine_openssl
))
750 # ifndef OPENSSL_NO_RDRAND
751 if ((opts
& OPENSSL_INIT_ENGINE_RDRAND
)
752 && !RUN_ONCE(&engine_rdrand
, ossl_init_engine_rdrand
))
755 if ((opts
& OPENSSL_INIT_ENGINE_DYNAMIC
)
756 && !RUN_ONCE(&engine_dynamic
, ossl_init_engine_dynamic
))
758 # ifndef OPENSSL_NO_STATIC_ENGINE
759 # ifndef OPENSSL_NO_DEVCRYPTOENG
760 if ((opts
& OPENSSL_INIT_ENGINE_CRYPTODEV
)
761 && !RUN_ONCE(&engine_devcrypto
, ossl_init_engine_devcrypto
))
764 # if !defined(OPENSSL_NO_PADLOCKENG)
765 if ((opts
& OPENSSL_INIT_ENGINE_PADLOCK
)
766 && !RUN_ONCE(&engine_padlock
, ossl_init_engine_padlock
))
769 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
770 if ((opts
& OPENSSL_INIT_ENGINE_CAPI
)
771 && !RUN_ONCE(&engine_capi
, ossl_init_engine_capi
))
774 # if !defined(OPENSSL_NO_AFALGENG)
775 if ((opts
& OPENSSL_INIT_ENGINE_AFALG
)
776 && !RUN_ONCE(&engine_afalg
, ossl_init_engine_afalg
))
780 if (opts
& (OPENSSL_INIT_ENGINE_ALL_BUILTIN
781 | OPENSSL_INIT_ENGINE_OPENSSL
782 | OPENSSL_INIT_ENGINE_AFALG
)) {
783 ENGINE_register_all_complete();
787 #ifndef OPENSSL_NO_COMP
788 if ((opts
& OPENSSL_INIT_ZLIB
)
789 && !RUN_ONCE(&zlib
, ossl_init_zlib
))
796 int OPENSSL_atexit(void (*handler
)(void))
798 OPENSSL_INIT_STOP
*newhand
;
800 #if !defined(OPENSSL_NO_DSO) \
801 && !defined(OPENSSL_USE_NODELETE)\
802 && !defined(OPENSSL_NO_PINSHARED)
809 handlersym
.func
= handler
;
812 HMODULE handle
= NULL
;
816 * We don't use the DSO route for WIN32 because there is a better
819 ret
= GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
820 | GET_MODULE_HANDLE_EX_FLAG_PIN
,
821 handlersym
.sym
, &handle
);
828 * Deliberately leak a reference to the handler. This will force the
829 * library/code containing the handler to remain loaded until we run the
830 * atexit handler. If -znodelete has been used then this is
837 dso
= DSO_dsobyaddr(handlersym
.sym
, DSO_FLAG_NO_UNLOAD_ON_FREE
);
838 # ifdef OPENSSL_INIT_DEBUG
840 "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
841 (dso
== NULL
? "No!" : "Yes."));
842 /* See same code above in ossl_init_base() for an explanation. */
851 if ((newhand
= OPENSSL_malloc(sizeof(*newhand
))) == NULL
) {
852 CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT
, ERR_R_MALLOC_FAILURE
);
856 newhand
->handler
= handler
;
857 newhand
->next
= stop_handlers
;
858 stop_handlers
= newhand
;
863 #ifdef OPENSSL_SYS_UNIX
865 * The following three functions are for OpenSSL developers. This is
866 * where we set/reset state across fork (called via pthread_atfork when
867 * it exists, or manually by the application when it doesn't).
869 * WARNING! If you put code in either OPENSSL_fork_parent or
870 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
871 * safe. See this link, for example:
872 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
875 void OPENSSL_fork_prepare(void)
879 void OPENSSL_fork_parent(void)
883 void OPENSSL_fork_child(void)