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