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