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