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