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