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