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