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>
69 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
);
71 /* Implement "once" functionality */
72 #if !defined(OPENSSL_THREADS)
73 typedef int OPENSSL_INIT_ONCE
;
74 # define OPENSSL_INIT_ONCE_STATIC_INIT 0
75 # define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) (*(once) = 0)
77 static void ossl_init_once_run(OPENSSL_INIT_ONCE
*once
, void (*init
)(void))
79 if (*once
== OPENSSL_INIT_ONCE_STATIC_INIT
) {
85 static int ossl_init_setup_thread_stop(void)
88 * There are no threads to stop. Do nothing.
93 static void ossl_init_thread_stop_cleanup(void)
97 static struct thread_local_inits_st
*local
= NULL
;
98 static struct thread_local_inits_st
*ossl_init_get_thread_local(int alloc
)
100 static struct thread_local_inits_st
*tmp
;
104 if (local
== NULL
&& alloc
)
105 tmp
= local
= OPENSSL_zalloc(sizeof(*local
));
113 #elif defined(OPENSSL_SYS_WINDOWS)
115 # include <windows.h>
117 # if _WIN32_WINNT < 0x0600
120 * Versions before 0x0600 (Windows Vista, Windows Server 2008 or later) do not
121 * have InitOnceExecuteOnce, so we fall back to using a spinlock instead.
123 typedef LONG OPENSSL_INIT_ONCE
;
124 # define OPENSSL_INIT_ONCE_STATIC_INIT 0
125 # define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) (*(once) = 0)
127 # define ONCE_UNINITED 0
128 # define ONCE_ININIT 1
131 static void ossl_init_once_run(OPENSSL_INIT_ONCE
*once
, void (*init
)(void))
133 LONG
volatile *lock
= (LONG
*)once
;
136 if (*lock
== ONCE_DONE
)
140 result
= InterlockedCompareExchange(lock
, ONCE_ININIT
, ONCE_UNINITED
);
141 if (result
== ONCE_UNINITED
) {
146 } while (result
== ONCE_ININIT
);
151 typedef INIT_ONCE OPENSSL_INIT_ONCE
;
152 # define OPENSSL_INIT_ONCE_STATIC_INIT INIT_ONCE_STATIC_INIT
153 # define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) \
154 InitOnceInitialize((PINIT_ONCE)(once))
156 static BOOL CALLBACK
once_cb(PINIT_ONCE once
, PVOID initfp
, PVOID
*unused
)
158 void (*init
)(void) = initfp
;
165 static void ossl_init_once_run(OPENSSL_INIT_ONCE
*once
, void (*init
)(void))
167 InitOnceExecuteOnce((INIT_ONCE
*)once
, once_cb
, init
, NULL
);
171 static DWORD threadstopkey
= TLS_OUT_OF_INDEXES
;
173 static int ossl_init_setup_thread_stop(void)
176 * We use a dummy thread local key here. We use the destructor to detect
177 * when the thread is going to stop
179 threadstopkey
= TlsAlloc();
180 if (threadstopkey
== TLS_OUT_OF_INDEXES
)
186 static void ossl_init_thread_stop_cleanup(void)
188 if (threadstopkey
!= TLS_OUT_OF_INDEXES
) {
189 TlsFree(threadstopkey
);
193 static struct thread_local_inits_st
*ossl_init_get_thread_local(int alloc
)
195 struct thread_local_inits_st
*local
= TlsGetValue(threadstopkey
);
197 if (local
== NULL
&& alloc
) {
198 local
= OPENSSL_zalloc(sizeof *local
);
199 TlsSetValue(threadstopkey
, local
);
202 TlsSetValue(threadstopkey
, NULL
);
209 # include <pthread.h>
211 static pthread_key_t threadstopkey
;
213 typedef pthread_once_t OPENSSL_INIT_ONCE
;
214 # define OPENSSL_INIT_ONCE_STATIC_INIT PTHREAD_ONCE_INIT
215 # define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) (*(once) = PTHREAD_ONCE_INIT)
217 static void ossl_init_once_run(OPENSSL_INIT_ONCE
*once
, void (*init
)(void))
219 pthread_once(once
, init
);
222 static void ossl_init_thread_stop_wrap(void *local
)
224 ossl_init_thread_stop((struct thread_local_inits_st
*)local
);
227 static int ossl_init_setup_thread_stop(void)
230 * We use a dummy thread local key here. We use the destructor to detect
231 * when the thread is going to stop
233 return (pthread_key_create(&threadstopkey
,
234 ossl_init_thread_stop_wrap
) == 0);
237 static void ossl_init_thread_stop_cleanup(void)
241 static struct thread_local_inits_st
*ossl_init_get_thread_local(int alloc
)
243 struct thread_local_inits_st
*local
= pthread_getspecific(threadstopkey
);
245 if (local
== NULL
&& alloc
) {
246 local
= OPENSSL_zalloc(sizeof *local
);
247 pthread_setspecific(threadstopkey
, local
);
250 pthread_setspecific(threadstopkey
, NULL
);
258 struct ossl_init_stop_st
{
259 void (*handler
)(void);
260 OPENSSL_INIT_STOP
*next
;
263 static OPENSSL_INIT_STOP
*stop_handlers
= NULL
;
265 static OPENSSL_INIT_ONCE base
= OPENSSL_INIT_ONCE_STATIC_INIT
;
266 static int base_inited
= 0;
267 static void ossl_init_base(void)
269 #ifdef OPENSSL_INIT_DEBUG
270 fprintf(stderr
, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
272 ossl_init_setup_thread_stop();
273 atexit(OPENSSL_INIT_library_stop
);
274 OPENSSL_cpuid_setup();
278 static OPENSSL_INIT_ONCE load_crypto_strings
= OPENSSL_INIT_ONCE_STATIC_INIT
;
279 static int load_crypto_strings_inited
= 0;
280 static void ossl_init_no_load_crypto_strings(void)
282 /* Do nothing in this case */
286 static void ossl_init_load_crypto_strings(void)
289 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
290 * pulling in all the error strings during static linking
292 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
293 # ifdef OPENSSL_INIT_DEBUG
294 fprintf(stderr
, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
295 "err_load_crypto_strings_intern()\n");
297 err_load_crypto_strings_intern();
299 load_crypto_strings_inited
= 1;
302 static OPENSSL_INIT_ONCE add_all_ciphers
= OPENSSL_INIT_ONCE_STATIC_INIT
;
303 static void ossl_init_add_all_ciphers(void)
306 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
307 * pulling in all the ciphers during static linking
309 #ifndef OPENSSL_NO_AUTOALGINIT
310 # ifdef OPENSSL_INIT_DEBUG
311 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
312 "openssl_add_all_ciphers_internal()\n");
314 openssl_add_all_ciphers_internal();
315 # ifndef OPENSSL_NO_ENGINE
316 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
317 ENGINE_setup_bsd_cryptodev();
323 static OPENSSL_INIT_ONCE add_all_digests
= OPENSSL_INIT_ONCE_STATIC_INIT
;
324 static void ossl_init_add_all_digests(void)
327 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
328 * pulling in all the ciphers during static linking
330 #ifndef OPENSSL_NO_AUTOALGINIT
331 # ifdef OPENSSL_INIT_DEBUG
332 fprintf(stderr
, "OPENSSL_INIT: ossl_init_add_all_digests: "
333 "openssl_add_all_digests_internal()\n");
335 openssl_add_all_digests_internal();
336 # ifndef OPENSSL_NO_ENGINE
337 # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
338 ENGINE_setup_bsd_cryptodev();
344 static void ossl_init_no_add_algs(void)
350 static OPENSSL_INIT_ONCE config
= OPENSSL_INIT_ONCE_STATIC_INIT
;
351 static int config_inited
= 0;
352 static const char *config_filename
;
353 static void ossl_init_config(void)
355 #ifdef OPENSSL_INIT_DEBUG
357 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
358 config_filename
==NULL
?"NULL":config_filename
);
360 openssl_config_internal(config_filename
);
363 static void ossl_init_no_config(void)
365 #ifdef OPENSSL_INIT_DEBUG
367 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
369 openssl_no_config_internal();
373 static OPENSSL_INIT_ONCE async
= OPENSSL_INIT_ONCE_STATIC_INIT
;
374 static int async_inited
= 0;
375 static void ossl_init_async(void)
377 #ifdef OPENSSL_INIT_DEBUG
378 fprintf(stderr
, "OPENSSL_INIT: ossl_init_async: async_init()\n");
384 #ifndef OPENSSL_NO_ENGINE
385 static int engine_inited
= 0;
386 static OPENSSL_INIT_ONCE engine_openssl
= OPENSSL_INIT_ONCE_STATIC_INIT
;
387 static void ossl_init_engine_openssl(void)
389 # ifdef OPENSSL_INIT_DEBUG
390 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_openssl: "
391 "engine_load_openssl_internal()\n");
393 engine_load_openssl_internal();
396 # if !defined(OPENSSL_NO_HW) && \
397 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
398 static OPENSSL_INIT_ONCE engine_cryptodev
= OPENSSL_INIT_ONCE_STATIC_INIT
;
399 static void ossl_init_engine_cryptodev(void)
401 # ifdef OPENSSL_INIT_DEBUG
402 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
403 "engine_load_cryptodev_internal()\n");
405 engine_load_cryptodev_internal();
410 # ifndef OPENSSL_NO_RDRAND
411 static OPENSSL_INIT_ONCE engine_rdrand
= OPENSSL_INIT_ONCE_STATIC_INIT
;
412 static void ossl_init_engine_rdrand(void)
414 # ifdef OPENSSL_INIT_DEBUG
415 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_rdrand: "
416 "engine_load_rdrand_internal()\n");
418 engine_load_rdrand_internal();
422 static OPENSSL_INIT_ONCE engine_dynamic
= OPENSSL_INIT_ONCE_STATIC_INIT
;
423 static void ossl_init_engine_dynamic(void)
425 # ifdef OPENSSL_INIT_DEBUG
426 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_dynamic: "
427 "engine_load_dynamic_internal()\n");
429 engine_load_dynamic_internal();
432 # ifndef OPENSSL_NO_STATIC_ENGINE
433 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
434 static OPENSSL_INIT_ONCE engine_padlock
= OPENSSL_INIT_ONCE_STATIC_INIT
;
435 static void ossl_init_engine_padlock(void)
437 # ifdef OPENSSL_INIT_DEBUG
438 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_padlock: "
439 "engine_load_padlock_internal()\n");
441 engine_load_padlock_internal();
445 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
446 static OPENSSL_INIT_ONCE engine_capi
= OPENSSL_INIT_ONCE_STATIC_INIT
;
447 static void ossl_init_engine_capi(void)
449 # ifdef OPENSSL_INIT_DEBUG
450 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_capi: "
451 "engine_load_capi_internal()\n");
453 engine_load_capi_internal();
457 static OPENSSL_INIT_ONCE engine_dasync
= OPENSSL_INIT_ONCE_STATIC_INIT
;
458 static void ossl_init_engine_dasync(void)
460 # ifdef OPENSSL_INIT_DEBUG
461 fprintf(stderr
, "OPENSSL_INIT: ossl_init_engine_dasync: "
462 "engine_load_dasync_internal()\n");
464 engine_load_dasync_internal();
470 static OPENSSL_INIT_ONCE zlib
= OPENSSL_INIT_ONCE_STATIC_INIT
;
471 static int zlib_inited
= 0;
472 static void ossl_init_zlib(void)
474 /* Do nothing - we need to know about this for the later cleanup */
478 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
)
480 /* Can't do much about this */
485 #ifdef OPENSSL_INIT_DEBUG
486 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
487 "ASYNC_cleanup_thread()\n");
489 ASYNC_cleanup_thread();
492 if (locals
->err_state
) {
493 #ifdef OPENSSL_INIT_DEBUG
494 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_stop: "
495 "ERR_remove_thread_state(NULL)\n");
497 ERR_remove_thread_state(NULL
);
500 OPENSSL_free(locals
);
501 ossl_init_thread_stop_cleanup();
504 void OPENSSL_INIT_thread_stop(void)
506 ossl_init_thread_stop(
507 (struct thread_local_inits_st
*)ossl_init_get_thread_local(0));
510 int ossl_init_thread_start(uint64_t opts
)
512 struct thread_local_inits_st
*locals
= ossl_init_get_thread_local(1);
517 if (opts
& OPENSSL_INIT_THREAD_ASYNC
) {
518 #ifdef OPENSSL_INIT_DEBUG
519 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
520 "marking thread for async\n");
525 if (opts
& OPENSSL_INIT_THREAD_ERR_STATE
) {
526 #ifdef OPENSSL_INIT_DEBUG
527 fprintf(stderr
, "OPENSSL_INIT: ossl_init_thread_start: "
528 "marking thread for err_state\n");
530 locals
->err_state
= 1;
536 void OPENSSL_INIT_library_stop(void)
538 OPENSSL_INIT_STOP
*currhandler
, *lasthandler
;
540 /* If we've not been inited then no need to deinit */
545 * Thread stop may not get automatically called by the thread library for
546 * the very last thread in some situations, so call it directly.
548 ossl_init_thread_stop(ossl_init_get_thread_local(0));
550 currhandler
= stop_handlers
;
551 while (currhandler
!= NULL
) {
552 currhandler
->handler();
553 lasthandler
= currhandler
;
554 currhandler
= currhandler
->next
;
555 OPENSSL_free(lasthandler
);
557 stop_handlers
= NULL
;
559 * We assume we are single-threaded for this function, i.e. no race
560 * conditions for the various "*_inited" vars below.
564 #ifdef OPENSSL_INIT_DEBUG
565 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
566 "COMP_zlib_cleanup()\n");
570 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&zlib
);
573 #ifndef OPENSSL_NO_ENGINE
575 # ifdef OPENSSL_INIT_DEBUG
576 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
577 "ENGINE_cleanup()\n");
581 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_openssl
);
582 # if !defined(OPENSSL_NO_HW) && \
583 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
584 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_cryptodev
);
586 # ifndef OPENSSL_NO_RDRAND
587 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_rdrand
);
589 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_dynamic
);
590 # ifndef OPENSSL_NO_STATIC_ENGINE
591 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
592 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_padlock
);
594 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
595 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_capi
);
597 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_dasync
);
603 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&async
);
606 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&config
);
607 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&add_all_ciphers
);
608 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&add_all_digests
);
610 if (load_crypto_strings_inited
) {
611 #ifdef OPENSSL_INIT_DEBUG
612 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
613 "ERR_free_strings()\n");
616 load_crypto_strings_inited
= 0;
617 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&load_crypto_strings
);
620 #ifdef OPENSSL_INIT_DEBUG
621 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
622 "CRYPTO_cleanup_all_ex_data()\n");
623 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
625 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
626 "CONF_modules_free()\n");
627 fprintf(stderr
, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
630 CRYPTO_cleanup_all_ex_data();
635 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&base
);
638 static const OPENSSL_INIT_SETTINGS
*ossl_init_get_setting(
639 const OPENSSL_INIT_SETTINGS
*settings
, int name
)
641 if (settings
== NULL
)
644 while (settings
->name
!= OPENSSL_INIT_SET_END
) {
645 if (settings
->name
== name
)
654 * If this function is called with a non NULL settings value then it must be
655 * called prior to any threads making calls to any OpenSSL functions,
656 * i.e. passing a non-null settings value is assumed to be single-threaded.
658 void OPENSSL_INIT_crypto_library_start(uint64_t opts
,
659 const OPENSSL_INIT_SETTINGS
*settings
)
661 ossl_init_once_run(&base
, ossl_init_base
);
663 if (opts
& OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS
)
664 ossl_init_once_run(&load_crypto_strings
,
665 ossl_init_no_load_crypto_strings
);
667 if (opts
& OPENSSL_INIT_LOAD_CRYPTO_STRINGS
)
668 ossl_init_once_run(&load_crypto_strings
, ossl_init_load_crypto_strings
);
670 if (opts
& OPENSSL_INIT_NO_ADD_ALL_CIPHERS
)
671 ossl_init_once_run(&add_all_ciphers
, ossl_init_no_add_algs
);
673 if (opts
& OPENSSL_INIT_ADD_ALL_CIPHERS
)
674 ossl_init_once_run(&add_all_ciphers
, ossl_init_add_all_ciphers
);
676 if (opts
& OPENSSL_INIT_NO_ADD_ALL_DIGESTS
)
677 ossl_init_once_run(&add_all_digests
, ossl_init_no_add_algs
);
679 if (opts
& OPENSSL_INIT_ADD_ALL_DIGESTS
)
680 ossl_init_once_run(&add_all_digests
, ossl_init_add_all_digests
);
682 if (opts
& OPENSSL_INIT_NO_LOAD_CONFIG
) {
683 ossl_init_once_run(&config
, ossl_init_no_config
);
686 if (opts
& OPENSSL_INIT_LOAD_CONFIG
) {
687 CRYPTO_w_lock(CRYPTO_LOCK_INIT
);
688 if (settings
!= NULL
) {
689 const OPENSSL_INIT_SETTINGS
*curr
;
690 curr
= ossl_init_get_setting(settings
,
691 OPENSSL_INIT_SET_CONF_FILENAME
);
692 config_filename
= (curr
== NULL
) ? NULL
: curr
->value
.type_string
;
694 config_filename
= NULL
;
696 ossl_init_once_run(&config
, ossl_init_config
);
697 CRYPTO_w_unlock(CRYPTO_LOCK_INIT
);
700 if (opts
& OPENSSL_INIT_ASYNC
) {
701 ossl_init_once_run(&async
, ossl_init_async
);
704 #ifndef OPENSSL_NO_ENGINE
705 if (opts
& OPENSSL_INIT_ENGINE_OPENSSL
) {
706 ossl_init_once_run(&engine_openssl
, ossl_init_engine_openssl
);
708 # if !defined(OPENSSL_NO_HW) && \
709 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
710 if (opts
& OPENSSL_INIT_ENGINE_CRYPTODEV
) {
711 ossl_init_once_run(&engine_cryptodev
, ossl_init_engine_cryptodev
);
714 # ifndef OPENSSL_NO_RDRAND
715 if (opts
& OPENSSL_INIT_ENGINE_RDRAND
) {
716 ossl_init_once_run(&engine_rdrand
, ossl_init_engine_rdrand
);
719 if (opts
& OPENSSL_INIT_ENGINE_DYNAMIC
) {
720 ossl_init_once_run(&engine_dynamic
, ossl_init_engine_dynamic
);
722 # ifndef OPENSSL_NO_STATIC_ENGINE
723 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
724 if (opts
& OPENSSL_INIT_ENGINE_PADLOCK
) {
725 ossl_init_once_run(&engine_padlock
, ossl_init_engine_padlock
);
728 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
729 if (opts
& OPENSSL_INIT_ENGINE_CAPI
) {
730 ossl_init_once_run(&engine_capi
, ossl_init_engine_capi
);
733 if (opts
& OPENSSL_INIT_ENGINE_DASYNC
) {
734 ossl_init_once_run(&engine_dasync
, ossl_init_engine_dasync
);
737 if (opts
& (OPENSSL_INIT_ENGINE_ALL_BUILTIN
738 | OPENSSL_INIT_ENGINE_DASYNC
| OPENSSL_INIT_ENGINE_OPENSSL
)) {
739 ENGINE_register_all_complete();
743 if (opts
& OPENSSL_INIT_ZLIB
) {
744 ossl_init_once_run(&zlib
, ossl_init_zlib
);
748 int OPENSSL_INIT_register_stop_handler(void (*handler
)(void))
750 OPENSSL_INIT_STOP
*newhand
;
752 newhand
= OPENSSL_malloc(sizeof(*newhand
));
756 newhand
->handler
= handler
;
757 newhand
->next
= stop_handlers
;
758 stop_handlers
= newhand
;