RT4310: Fix varous no-XXX builds
[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     ossl_init_thread_stop_cleanup();
510 }
511
512 void OPENSSL_thread_stop(void)
513 {
514     ossl_init_thread_stop(
515         (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
516 }
517
518 int ossl_init_thread_start(uint64_t opts)
519 {
520     struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
521
522     if (locals == NULL)
523         return 0;
524
525     if (opts & OPENSSL_INIT_THREAD_ASYNC) {
526 #ifdef OPENSSL_INIT_DEBUG
527         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
528                         "marking thread for async\n");
529 #endif
530         locals->async = 1;
531     }
532
533     if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
534 #ifdef OPENSSL_INIT_DEBUG
535         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
536                         "marking thread for err_state\n");
537 #endif
538         locals->err_state = 1;
539     }
540
541     return 1;
542 }
543
544 void OPENSSL_cleanup(void)
545 {
546     OPENSSL_INIT_STOP *currhandler, *lasthandler;
547
548     /* If we've not been inited then no need to deinit */
549     if (!base_inited)
550         return;
551
552     /* Might be explicitly called and also by atexit */
553     if (stopped)
554         return;
555     stopped = 1;
556
557     /*
558      * Thread stop may not get automatically called by the thread library for
559      * the very last thread in some situations, so call it directly.
560      */
561     ossl_init_thread_stop(ossl_init_get_thread_local(0));
562
563     currhandler = stop_handlers;
564     while (currhandler != NULL) {
565         currhandler->handler();
566         lasthandler = currhandler;
567         currhandler = currhandler->next;
568         OPENSSL_free(lasthandler);
569     }
570     stop_handlers = NULL;
571     /*
572      * We assume we are single-threaded for this function, i.e. no race
573      * conditions for the various "*_inited" vars below.
574      */
575
576     if (zlib_inited) {
577 #ifdef OPENSSL_INIT_DEBUG
578         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
579                         "COMP_zlib_cleanup()\n");
580 #endif
581         COMP_zlib_cleanup();
582     }
583
584 #ifndef OPENSSL_NO_ENGINE
585     if (engine_inited) {
586 # ifdef OPENSSL_INIT_DEBUG
587         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
588                         "ENGINE_cleanup()\n");
589 # endif
590         ENGINE_cleanup();
591     }
592 #endif
593
594     if (load_crypto_strings_inited) {
595 #ifdef OPENSSL_INIT_DEBUG
596         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
597                         "ERR_free_strings()\n");
598 #endif
599         ERR_free_strings();
600     }
601
602 #ifdef OPENSSL_INIT_DEBUG
603     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
604                     "CRYPTO_cleanup_all_ex_data()\n");
605     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
606                     "EVP_cleanup()\n");
607     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
608                     "CONF_modules_free()\n");
609     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
610                     "RAND_cleanup()\n");
611 #endif
612     CRYPTO_cleanup_all_ex_data();
613     EVP_cleanup();
614     CONF_modules_free();
615     RAND_cleanup();
616     base_inited = 0;
617 }
618
619 /*
620  * If this function is called with a non NULL settings value then it must be
621  * called prior to any threads making calls to any OpenSSL functions,
622  * i.e. passing a non-null settings value is assumed to be single-threaded.
623  */
624 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
625 {
626     static int stoperrset = 0;
627
628     if (stopped) {
629         if (!stoperrset) {
630             /*
631              * We only ever set this once to avoid getting into an infinite
632              * loop where the error system keeps trying to init and fails so
633              * sets an error etc
634              */
635             stoperrset = 1;
636             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
637         }
638         return 0;
639     }
640
641     ossl_init_once_run(&base, ossl_init_base);
642
643     if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
644         ossl_init_once_run(&load_crypto_strings,
645                            ossl_init_no_load_crypto_strings);
646
647     if (opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
648         ossl_init_once_run(&load_crypto_strings, ossl_init_load_crypto_strings);
649
650     if (opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
651         ossl_init_once_run(&add_all_ciphers, ossl_init_no_add_algs);
652
653     if (opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
654         ossl_init_once_run(&add_all_ciphers, ossl_init_add_all_ciphers);
655
656     if (opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
657         ossl_init_once_run(&add_all_digests, ossl_init_no_add_algs);
658
659     if (opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
660         ossl_init_once_run(&add_all_digests, ossl_init_add_all_digests);
661
662     if (opts & OPENSSL_INIT_NO_LOAD_CONFIG) {
663         ossl_init_once_run(&config, ossl_init_no_config);
664     }
665
666     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
667         CRYPTO_w_lock(CRYPTO_LOCK_INIT);
668         config_filename = (settings == NULL) ? NULL : settings->config_name;
669         ossl_init_once_run(&config, ossl_init_config);
670         CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
671     }
672
673 #ifndef OPENSSL_NO_ASYNC
674     if (opts & OPENSSL_INIT_ASYNC) {
675         ossl_init_once_run(&async, ossl_init_async);
676     }
677 #endif
678 #ifndef OPENSSL_NO_ENGINE
679     if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
680         ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
681     }
682 # if !defined(OPENSSL_NO_HW) && \
683     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
684     if (opts & OPENSSL_INIT_ENGINE_CRYPTODEV) {
685         ossl_init_once_run(&engine_cryptodev, ossl_init_engine_cryptodev);
686     }
687 # endif
688 # ifndef OPENSSL_NO_RDRAND
689     if (opts & OPENSSL_INIT_ENGINE_RDRAND) {
690         ossl_init_once_run(&engine_rdrand, ossl_init_engine_rdrand);
691     }
692 # endif
693     if (opts & OPENSSL_INIT_ENGINE_DYNAMIC) {
694         ossl_init_once_run(&engine_dynamic, ossl_init_engine_dynamic);
695     }
696 # ifndef OPENSSL_NO_STATIC_ENGINE
697 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
698     if (opts & OPENSSL_INIT_ENGINE_PADLOCK) {
699         ossl_init_once_run(&engine_padlock, ossl_init_engine_padlock);
700     }
701 #  endif
702 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
703     if (opts & OPENSSL_INIT_ENGINE_CAPI) {
704         ossl_init_once_run(&engine_capi, ossl_init_engine_capi);
705     }
706 #  endif
707     if (opts & OPENSSL_INIT_ENGINE_DASYNC) {
708         ossl_init_once_run(&engine_dasync, ossl_init_engine_dasync);
709     }
710 # endif
711     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
712                 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL)) {
713         ENGINE_register_all_complete();
714     }
715 #endif
716
717     if (opts & OPENSSL_INIT_ZLIB) {
718         ossl_init_once_run(&zlib, ossl_init_zlib);
719     }
720
721     return 1;
722 }
723
724 int OPENSSL_atexit(void (*handler)(void))
725 {
726     OPENSSL_INIT_STOP *newhand;
727
728     newhand = OPENSSL_malloc(sizeof(*newhand));
729     if (newhand == NULL)
730         return 0;
731
732     newhand->handler = handler;
733     newhand->next = stop_handlers;
734     stop_handlers = newhand;
735
736     return 1;
737 }
738
739