RT4315: Fix UEFI build in crypto/init.c
[openssl.git] / crypto / init.c
1 /*
2  * Written by Matt Caswell for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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/)"
23  *
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.
28  *
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.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
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  * ====================================================================
51  *
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).
55  *
56  */
57
58 #include <internal/cryptlib_int.h>
59 #include <openssl/err.h>
60 #include <openssl/rand.h>
61 #include <openssl/evp.h>
62 #include <internal/evp_int.h>
63 #include <internal/conf.h>
64 #include <internal/async.h>
65 #include <internal/engine.h>
66 #include <openssl/comp.h>
67 #include <internal/err.h>
68 #include <stdlib.h>
69 #include <assert.h>
70
71 static int stopped = 0;
72
73 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
74
75 /* Implement "once" functionality */
76 #if !defined(OPENSSL_THREADS)
77 typedef int OPENSSL_INIT_ONCE;
78 # define OPENSSL_INIT_ONCE_STATIC_INIT          0
79
80 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
81 {
82     if (*once == OPENSSL_INIT_ONCE_STATIC_INIT) {
83         *once = 1;
84         init();
85     }
86 }
87
88 static int ossl_init_setup_thread_stop(void)
89 {
90     /*
91      * There are no threads to stop. Do nothing.
92      */
93     return 1;
94 }
95
96 static void ossl_init_thread_stop_cleanup(void)
97 {
98 }
99
100 static struct thread_local_inits_st *local = NULL;
101 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
102 {
103     struct thread_local_inits_st *tmp;
104
105     tmp = local;
106
107     if (local == NULL && alloc)
108         tmp = local = OPENSSL_zalloc(sizeof(*local));
109
110     if (!alloc)
111         local = NULL;
112
113     return tmp;
114 }
115
116 #elif defined(OPENSSL_SYS_WINDOWS)
117
118 # include <windows.h>
119
120 # if _WIN32_WINNT < 0x0600
121
122 /*
123  * Versions before 0x0600 (Windows Vista, Windows Server 2008 or later) do not
124  * have InitOnceExecuteOnce, so we fall back to using a spinlock instead.
125  */
126 typedef LONG OPENSSL_INIT_ONCE;
127 #  define OPENSSL_INIT_ONCE_STATIC_INIT          0
128
129 #  define ONCE_UNINITED     0
130 #  define ONCE_ININIT       1
131 #  define ONCE_DONE         2
132
133 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
134 {
135     LONG volatile *lock = (LONG *)once;
136     LONG result;
137
138     if (*lock == ONCE_DONE)
139         return;
140
141     do {
142         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
143         if (result == ONCE_UNINITED) {
144             init();
145             *lock = ONCE_DONE;
146             return;
147         }
148     } while (result == ONCE_ININIT);
149 }
150
151 # else
152
153 typedef INIT_ONCE OPENSSL_INIT_ONCE;
154 #  define OPENSSL_INIT_ONCE_STATIC_INIT          INIT_ONCE_STATIC_INIT
155
156 static BOOL CALLBACK once_cb(PINIT_ONCE once, PVOID initfp, PVOID *unused)
157 {
158     void (*init)(void) = initfp;
159
160     init();
161
162     return TRUE;
163 }
164
165 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
166 {
167     InitOnceExecuteOnce((INIT_ONCE *)once, once_cb, init, NULL);
168 }
169 # endif
170
171 static DWORD threadstopkey = TLS_OUT_OF_INDEXES;
172
173 static int ossl_init_setup_thread_stop(void)
174 {
175     /*
176      * We use a dummy thread local key here. We use the destructor to detect
177      * when the thread is going to stop
178      */
179     threadstopkey = TlsAlloc();
180     if (threadstopkey == TLS_OUT_OF_INDEXES)
181         return 0;
182
183     return 1;
184 }
185
186 static void ossl_init_thread_stop_cleanup(void)
187 {
188     if (threadstopkey != TLS_OUT_OF_INDEXES) {
189         TlsFree(threadstopkey);
190     }
191 }
192
193 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
194 {
195     struct thread_local_inits_st *local = TlsGetValue(threadstopkey);
196
197     if (local == NULL && alloc) {
198         local = OPENSSL_zalloc(sizeof *local);
199         TlsSetValue(threadstopkey, local);
200     }
201     if (!alloc) {
202         TlsSetValue(threadstopkey, NULL);
203     }
204
205     return local;
206 }
207
208 #else /* pthreads */
209 # include <pthread.h>
210
211 static pthread_key_t threadstopkey;
212
213 typedef pthread_once_t OPENSSL_INIT_ONCE;
214 # define OPENSSL_INIT_ONCE_STATIC_INIT          PTHREAD_ONCE_INIT
215
216 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
217 {
218     pthread_once(once, init);
219 }
220
221 static void ossl_init_thread_stop_wrap(void *local)
222 {
223     ossl_init_thread_stop((struct thread_local_inits_st *)local);
224 }
225
226 static int ossl_init_setup_thread_stop(void)
227 {
228     /*
229      * We use a dummy thread local key here. We use the destructor to detect
230      * when the thread is going to stop
231      */
232     return (pthread_key_create(&threadstopkey,
233                                ossl_init_thread_stop_wrap) == 0);
234 }
235
236 static void ossl_init_thread_stop_cleanup(void)
237 {
238 }
239
240 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
241 {
242     struct thread_local_inits_st *local = pthread_getspecific(threadstopkey);
243
244     if (local == NULL && alloc) {
245         local = OPENSSL_zalloc(sizeof *local);
246         pthread_setspecific(threadstopkey, local);
247     }
248     if (!alloc) {
249         pthread_setspecific(threadstopkey, NULL);
250     }
251
252     return local;
253 }
254
255 #endif
256
257 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
258 struct ossl_init_stop_st {
259     void (*handler)(void);
260     OPENSSL_INIT_STOP *next;
261 };
262
263 static OPENSSL_INIT_STOP *stop_handlers = NULL;
264
265 static OPENSSL_INIT_ONCE base = OPENSSL_INIT_ONCE_STATIC_INIT;
266 static int base_inited = 0;
267 static void ossl_init_base(void)
268 {
269 #ifdef OPENSSL_INIT_DEBUG
270     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
271 #endif
272     ossl_init_setup_thread_stop();
273 #ifndef OPENSSL_SYS_UEFI
274     atexit(OPENSSL_cleanup);
275 #endif
276     OPENSSL_cpuid_setup();
277     base_inited = 1;
278 }
279
280 static OPENSSL_INIT_ONCE load_crypto_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
281 static int load_crypto_strings_inited = 0;
282 static void ossl_init_no_load_crypto_strings(void)
283 {
284     /* Do nothing in this case */
285     return;
286 }
287
288 static void ossl_init_load_crypto_strings(void)
289 {
290     /*
291      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
292      * pulling in all the error strings during static linking
293      */
294 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
295 # ifdef OPENSSL_INIT_DEBUG
296     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
297                     "err_load_crypto_strings_intern()\n");
298 # endif
299     err_load_crypto_strings_intern();
300 #endif
301     load_crypto_strings_inited = 1;
302 }
303
304 static OPENSSL_INIT_ONCE add_all_ciphers = OPENSSL_INIT_ONCE_STATIC_INIT;
305 static void ossl_init_add_all_ciphers(void)
306 {
307     /*
308      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
309      * pulling in all the ciphers during static linking
310      */
311 #ifndef OPENSSL_NO_AUTOALGINIT
312 # ifdef OPENSSL_INIT_DEBUG
313     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
314                     "openssl_add_all_ciphers_internal()\n");
315 # endif
316     openssl_add_all_ciphers_internal();
317 # ifndef OPENSSL_NO_ENGINE
318 #  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
319     ENGINE_setup_bsd_cryptodev();
320 #  endif
321 # endif
322 #endif
323 }
324
325 static OPENSSL_INIT_ONCE add_all_digests = OPENSSL_INIT_ONCE_STATIC_INIT;
326 static void ossl_init_add_all_digests(void)
327 {
328     /*
329      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
330      * pulling in all the ciphers during static linking
331      */
332 #ifndef OPENSSL_NO_AUTOALGINIT
333 # ifdef OPENSSL_INIT_DEBUG
334     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
335                     "openssl_add_all_digests_internal()\n");
336 # endif
337     openssl_add_all_digests_internal();
338 # ifndef OPENSSL_NO_ENGINE
339 #  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
340     ENGINE_setup_bsd_cryptodev();
341 #  endif
342 # endif
343 #endif
344 }
345
346 static void ossl_init_no_add_algs(void)
347 {
348     /* Do nothing */
349     return;
350 }
351
352 static OPENSSL_INIT_ONCE config = OPENSSL_INIT_ONCE_STATIC_INIT;
353 static int config_inited = 0;
354 static const char *config_filename;
355 static void ossl_init_config(void)
356 {
357 #ifdef OPENSSL_INIT_DEBUG
358     fprintf(stderr,
359             "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
360             config_filename==NULL?"NULL":config_filename);
361 #endif
362     openssl_config_internal(config_filename);
363     config_inited = 1;
364 }
365 static void ossl_init_no_config(void)
366 {
367 #ifdef OPENSSL_INIT_DEBUG
368     fprintf(stderr,
369             "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
370 #endif
371     openssl_no_config_internal();
372     config_inited = 1;
373 }
374
375 static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
376 static int async_inited = 0;
377 static void ossl_init_async(void)
378 {
379 #ifdef OPENSSL_INIT_DEBUG
380     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
381 #endif
382     async_init();
383     async_inited = 1;
384 }
385
386 #ifndef OPENSSL_NO_ENGINE
387 static int engine_inited = 0;
388 static OPENSSL_INIT_ONCE engine_openssl = OPENSSL_INIT_ONCE_STATIC_INIT;
389 static void ossl_init_engine_openssl(void)
390 {
391 # ifdef OPENSSL_INIT_DEBUG
392     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
393                     "engine_load_openssl_internal()\n");
394 # endif
395     engine_load_openssl_internal();
396     engine_inited = 1;
397 }
398 # if !defined(OPENSSL_NO_HW) && \
399     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
400 static OPENSSL_INIT_ONCE engine_cryptodev = OPENSSL_INIT_ONCE_STATIC_INIT;
401 static void ossl_init_engine_cryptodev(void)
402 {
403 #  ifdef OPENSSL_INIT_DEBUG
404     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
405                     "engine_load_cryptodev_internal()\n");
406 #  endif
407     engine_load_cryptodev_internal();
408     engine_inited = 1;
409 }
410 # endif
411
412 # ifndef OPENSSL_NO_RDRAND
413 static OPENSSL_INIT_ONCE engine_rdrand = OPENSSL_INIT_ONCE_STATIC_INIT;
414 static void ossl_init_engine_rdrand(void)
415 {
416 #  ifdef OPENSSL_INIT_DEBUG
417     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
418                     "engine_load_rdrand_internal()\n");
419 #  endif
420     engine_load_rdrand_internal();
421     engine_inited = 1;
422 }
423 # endif
424 static OPENSSL_INIT_ONCE engine_dynamic = OPENSSL_INIT_ONCE_STATIC_INIT;
425 static void ossl_init_engine_dynamic(void)
426 {
427 # ifdef OPENSSL_INIT_DEBUG
428     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
429                     "engine_load_dynamic_internal()\n");
430 # endif
431     engine_load_dynamic_internal();
432     engine_inited = 1;
433 }
434 # ifndef OPENSSL_NO_STATIC_ENGINE
435 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
436 static OPENSSL_INIT_ONCE engine_padlock = OPENSSL_INIT_ONCE_STATIC_INIT;
437 static void ossl_init_engine_padlock(void)
438 {
439 #   ifdef OPENSSL_INIT_DEBUG
440     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
441                     "engine_load_padlock_internal()\n");
442 #   endif
443     engine_load_padlock_internal();
444     engine_inited = 1;
445 }
446 #  endif
447 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
448 static OPENSSL_INIT_ONCE engine_capi = OPENSSL_INIT_ONCE_STATIC_INIT;
449 static void ossl_init_engine_capi(void)
450 {
451 #   ifdef OPENSSL_INIT_DEBUG
452     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
453                     "engine_load_capi_internal()\n");
454 #   endif
455     engine_load_capi_internal();
456     engine_inited = 1;
457 }
458 #  endif
459 static OPENSSL_INIT_ONCE engine_dasync = OPENSSL_INIT_ONCE_STATIC_INIT;
460 static void ossl_init_engine_dasync(void)
461 {
462 # ifdef OPENSSL_INIT_DEBUG
463     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
464                     "engine_load_dasync_internal()\n");
465 # endif
466     engine_load_dasync_internal();
467     engine_inited = 1;
468 }
469 # endif
470 #endif
471
472 static OPENSSL_INIT_ONCE zlib = OPENSSL_INIT_ONCE_STATIC_INIT;
473 static int zlib_inited = 0;
474 static void ossl_init_zlib(void)
475 {
476     /* Do nothing - we need to know about this for the later cleanup */
477     zlib_inited = 1;
478 }
479
480 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
481 {
482     /* Can't do much about this */
483     if (locals == NULL)
484         return;
485
486     if (locals->async) {
487 #ifdef OPENSSL_INIT_DEBUG
488         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
489                         "ASYNC_cleanup_thread()\n");
490 #endif
491         ASYNC_cleanup_thread();
492     }
493
494     if (locals->err_state) {
495 #ifdef OPENSSL_INIT_DEBUG
496         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
497                         "ERR_remove_thread_state(NULL)\n");
498 #endif
499         ERR_remove_thread_state(NULL);
500     }
501
502     OPENSSL_free(locals);
503     ossl_init_thread_stop_cleanup();
504 }
505
506 void OPENSSL_thread_stop(void)
507 {
508     ossl_init_thread_stop(
509         (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
510 }
511
512 int ossl_init_thread_start(uint64_t opts)
513 {
514     struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
515
516     if (locals == NULL)
517         return 0;
518
519     if (opts & OPENSSL_INIT_THREAD_ASYNC) {
520 #ifdef OPENSSL_INIT_DEBUG
521         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
522                         "marking thread for async\n");
523 #endif
524         locals->async = 1;
525     }
526
527     if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
528 #ifdef OPENSSL_INIT_DEBUG
529         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
530                         "marking thread for err_state\n");
531 #endif
532         locals->err_state = 1;
533     }
534
535     return 1;
536 }
537
538 void OPENSSL_cleanup(void)
539 {
540     OPENSSL_INIT_STOP *currhandler, *lasthandler;
541
542     /* If we've not been inited then no need to deinit */
543     if (!base_inited)
544         return;
545
546     /* Might be explicitly called and also by atexit */
547     if (stopped)
548         return;
549     stopped = 1;
550
551     /*
552      * Thread stop may not get automatically called by the thread library for
553      * the very last thread in some situations, so call it directly.
554      */
555     ossl_init_thread_stop(ossl_init_get_thread_local(0));
556
557     currhandler = stop_handlers;
558     while (currhandler != NULL) {
559         currhandler->handler();
560         lasthandler = currhandler;
561         currhandler = currhandler->next;
562         OPENSSL_free(lasthandler);
563     }
564     stop_handlers = NULL;
565     /*
566      * We assume we are single-threaded for this function, i.e. no race
567      * conditions for the various "*_inited" vars below.
568      */
569
570     if (zlib_inited) {
571 #ifdef OPENSSL_INIT_DEBUG
572         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
573                         "COMP_zlib_cleanup()\n");
574 #endif
575         COMP_zlib_cleanup();
576     }
577
578 #ifndef OPENSSL_NO_ENGINE
579     if (engine_inited) {
580 # ifdef OPENSSL_INIT_DEBUG
581         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
582                         "ENGINE_cleanup()\n");
583 # endif
584         ENGINE_cleanup();
585     }
586 #endif
587
588     if (load_crypto_strings_inited) {
589 #ifdef OPENSSL_INIT_DEBUG
590         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
591                         "ERR_free_strings()\n");
592 #endif
593         ERR_free_strings();
594     }
595
596 #ifdef OPENSSL_INIT_DEBUG
597     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
598                     "CRYPTO_cleanup_all_ex_data()\n");
599     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
600                     "EVP_cleanup()\n");
601     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
602                     "CONF_modules_free()\n");
603     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
604                     "RAND_cleanup()\n");
605 #endif
606     CRYPTO_cleanup_all_ex_data();
607     EVP_cleanup();
608     CONF_modules_free();
609     RAND_cleanup();
610     base_inited = 0;
611 }
612
613 /*
614  * If this function is called with a non NULL settings value then it must be
615  * called prior to any threads making calls to any OpenSSL functions,
616  * i.e. passing a non-null settings value is assumed to be single-threaded.
617  */
618 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
619 {
620     static int stoperrset = 0;
621
622     if (stopped) {
623         if (!stoperrset) {
624             /*
625              * We only ever set this once to avoid getting into an infinite
626              * loop where the error system keeps trying to init and fails so
627              * sets an error etc
628              */
629             stoperrset = 1;
630             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
631         }
632         return 0;
633     }
634
635     ossl_init_once_run(&base, ossl_init_base);
636
637     if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
638         ossl_init_once_run(&load_crypto_strings,
639                            ossl_init_no_load_crypto_strings);
640
641     if (opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
642         ossl_init_once_run(&load_crypto_strings, ossl_init_load_crypto_strings);
643
644     if (opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
645         ossl_init_once_run(&add_all_ciphers, ossl_init_no_add_algs);
646
647     if (opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
648         ossl_init_once_run(&add_all_ciphers, ossl_init_add_all_ciphers);
649
650     if (opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
651         ossl_init_once_run(&add_all_digests, ossl_init_no_add_algs);
652
653     if (opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
654         ossl_init_once_run(&add_all_digests, ossl_init_add_all_digests);
655
656     if (opts & OPENSSL_INIT_NO_LOAD_CONFIG) {
657         ossl_init_once_run(&config, ossl_init_no_config);
658     }
659
660     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
661         CRYPTO_w_lock(CRYPTO_LOCK_INIT);
662         config_filename = (settings == NULL) ? NULL : settings->config_name;
663         ossl_init_once_run(&config, ossl_init_config);
664         CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
665     }
666
667     if (opts & OPENSSL_INIT_ASYNC) {
668         ossl_init_once_run(&async, ossl_init_async);
669     }
670
671 #ifndef OPENSSL_NO_ENGINE
672     if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
673         ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
674     }
675 # if !defined(OPENSSL_NO_HW) && \
676     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
677     if (opts & OPENSSL_INIT_ENGINE_CRYPTODEV) {
678         ossl_init_once_run(&engine_cryptodev, ossl_init_engine_cryptodev);
679     }
680 # endif
681 # ifndef OPENSSL_NO_RDRAND
682     if (opts & OPENSSL_INIT_ENGINE_RDRAND) {
683         ossl_init_once_run(&engine_rdrand, ossl_init_engine_rdrand);
684     }
685 # endif
686     if (opts & OPENSSL_INIT_ENGINE_DYNAMIC) {
687         ossl_init_once_run(&engine_dynamic, ossl_init_engine_dynamic);
688     }
689 # ifndef OPENSSL_NO_STATIC_ENGINE
690 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
691     if (opts & OPENSSL_INIT_ENGINE_PADLOCK) {
692         ossl_init_once_run(&engine_padlock, ossl_init_engine_padlock);
693     }
694 #  endif
695 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
696     if (opts & OPENSSL_INIT_ENGINE_CAPI) {
697         ossl_init_once_run(&engine_capi, ossl_init_engine_capi);
698     }
699 #  endif
700     if (opts & OPENSSL_INIT_ENGINE_DASYNC) {
701         ossl_init_once_run(&engine_dasync, ossl_init_engine_dasync);
702     }
703 # endif
704     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
705                 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL)) {
706         ENGINE_register_all_complete();
707     }
708 #endif
709
710     if (opts & OPENSSL_INIT_ZLIB) {
711         ossl_init_once_run(&zlib, ossl_init_zlib);
712     }
713
714     return 1;
715 }
716
717 int OPENSSL_atexit(void (*handler)(void))
718 {
719     OPENSSL_INIT_STOP *newhand;
720
721     newhand = OPENSSL_malloc(sizeof(*newhand));
722     if (newhand == NULL)
723         return 0;
724
725     newhand->handler = handler;
726     newhand->next = stop_handlers;
727     stop_handlers = newhand;
728
729     return 1;
730 }
731
732