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