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