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