]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/init.c
Fix windows thread stop code
[thirdparty/openssl.git] / crypto / init.c
CommitLineData
b184e3ef
MC
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
b184e3ef
MC
58#include <internal/cryptlib_int.h>
59#include <openssl/err.h>
31305cdf 60#include <openssl/rand.h>
b184e3ef 61#include <openssl/evp.h>
b184e3ef
MC
62#include <internal/evp_int.h>
63#include <internal/conf.h>
64#include <internal/async.h>
1288f26f 65#ifndef OPENSSL_NO_ENGINE
b184e3ef 66#include <internal/engine.h>
1288f26f 67#endif
b184e3ef 68#include <openssl/comp.h>
b184e3ef 69#include <internal/err.h>
b184e3ef 70#include <stdlib.h>
dd27f16e
RS
71#include <assert.h>
72
73static int stopped = 0;
b184e3ef 74
71567a6f
MC
75static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
76
b184e3ef
MC
77/* Implement "once" functionality */
78#if !defined(OPENSSL_THREADS)
79typedef int OPENSSL_INIT_ONCE;
80# define OPENSSL_INIT_ONCE_STATIC_INIT 0
b184e3ef
MC
81
82static 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
90static int ossl_init_setup_thread_stop(void)
91{
92 /*
93 * There are no threads to stop. Do nothing.
94 */
95 return 1;
96}
97
98static void ossl_init_thread_stop_cleanup(void)
99{
100}
101
102static struct thread_local_inits_st *local = NULL;
b7326ea7 103static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
b184e3ef 104{
8bd8221b 105 struct thread_local_inits_st *tmp;
b7326ea7
MC
106
107 tmp = local;
108
b184e3ef 109 if (local == NULL && alloc)
b7326ea7
MC
110 tmp = local = OPENSSL_zalloc(sizeof(*local));
111
112 if (!alloc)
113 local = NULL;
114
115 return tmp;
b184e3ef
MC
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 */
128typedef LONG OPENSSL_INIT_ONCE;
129# define OPENSSL_INIT_ONCE_STATIC_INIT 0
b184e3ef
MC
130
131# define ONCE_UNINITED 0
132# define ONCE_ININIT 1
133# define ONCE_DONE 2
134
135static 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
155typedef INIT_ONCE OPENSSL_INIT_ONCE;
156# define OPENSSL_INIT_ONCE_STATIC_INIT INIT_ONCE_STATIC_INIT
b184e3ef
MC
157
158static 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
167static 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
1ffa8a96 173static DWORD threadstopkey = TLS_OUT_OF_INDEXES;
b184e3ef
MC
174
175static 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
188static void ossl_init_thread_stop_cleanup(void)
189{
190 if (threadstopkey != TLS_OUT_OF_INDEXES) {
191 TlsFree(threadstopkey);
192 }
193}
194
b7326ea7 195static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
b184e3ef
MC
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 }
b7326ea7
MC
203 if (!alloc) {
204 TlsSetValue(threadstopkey, NULL);
205 }
b184e3ef
MC
206
207 return local;
208}
209
210#else /* pthreads */
211# include <pthread.h>
212
1ffa8a96 213static pthread_key_t threadstopkey;
b184e3ef
MC
214
215typedef pthread_once_t OPENSSL_INIT_ONCE;
216# define OPENSSL_INIT_ONCE_STATIC_INIT PTHREAD_ONCE_INIT
b184e3ef
MC
217
218static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
219{
220 pthread_once(once, init);
221}
222
223static void ossl_init_thread_stop_wrap(void *local)
224{
225 ossl_init_thread_stop((struct thread_local_inits_st *)local);
226}
227
228static 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
238static void ossl_init_thread_stop_cleanup(void)
239{
240}
241
b7326ea7 242static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
b184e3ef
MC
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 }
b7326ea7
MC
250 if (!alloc) {
251 pthread_setspecific(threadstopkey, NULL);
252 }
b184e3ef
MC
253
254 return local;
255}
256
257#endif
258
7253fd55 259typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
b184e3ef
MC
260struct ossl_init_stop_st {
261 void (*handler)(void);
262 OPENSSL_INIT_STOP *next;
263};
264
265static OPENSSL_INIT_STOP *stop_handlers = NULL;
266
267static OPENSSL_INIT_ONCE base = OPENSSL_INIT_ONCE_STATIC_INIT;
268static int base_inited = 0;
269static 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();
c7b7938e 275#ifndef OPENSSL_SYS_UEFI
f672aee4 276 atexit(OPENSSL_cleanup);
c7b7938e 277#endif
b184e3ef
MC
278 OPENSSL_cpuid_setup();
279 base_inited = 1;
280}
281
282static OPENSSL_INIT_ONCE load_crypto_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
283static int load_crypto_strings_inited = 0;
284static void ossl_init_no_load_crypto_strings(void)
285{
286 /* Do nothing in this case */
287 return;
288}
289
290static void ossl_init_load_crypto_strings(void)
291{
498abff0
MC
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)
b184e3ef
MC
297# ifdef OPENSSL_INIT_DEBUG
298 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
299 "err_load_crypto_strings_intern()\n");
300# endif
b184e3ef 301 err_load_crypto_strings_intern();
b184e3ef
MC
302#endif
303 load_crypto_strings_inited = 1;
304}
305
306static OPENSSL_INIT_ONCE add_all_ciphers = OPENSSL_INIT_ONCE_STATIC_INIT;
307static 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
b184e3ef 318 openssl_add_all_ciphers_internal();
b184e3ef
MC
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
327static OPENSSL_INIT_ONCE add_all_digests = OPENSSL_INIT_ONCE_STATIC_INIT;
328static 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
b184e3ef 339 openssl_add_all_digests_internal();
b184e3ef
MC
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
348static void ossl_init_no_add_algs(void)
349{
350 /* Do nothing */
351 return;
352}
353
354static OPENSSL_INIT_ONCE config = OPENSSL_INIT_ONCE_STATIC_INIT;
355static int config_inited = 0;
356static const char *config_filename;
357static 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
b184e3ef 364 openssl_config_internal(config_filename);
b184e3ef
MC
365 config_inited = 1;
366}
367static 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
b184e3ef 373 openssl_no_config_internal();
b184e3ef
MC
374 config_inited = 1;
375}
376
1288f26f 377#ifndef OPENSSL_NO_ASYNC
b184e3ef
MC
378static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
379static int async_inited = 0;
380static void ossl_init_async(void)
381{
382#ifdef OPENSSL_INIT_DEBUG
383 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
384#endif
b184e3ef 385 async_init();
b184e3ef
MC
386 async_inited = 1;
387}
1288f26f 388#endif
b184e3ef
MC
389
390#ifndef OPENSSL_NO_ENGINE
391static int engine_inited = 0;
392static OPENSSL_INIT_ONCE engine_openssl = OPENSSL_INIT_ONCE_STATIC_INIT;
393static 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
b184e3ef 399 engine_load_openssl_internal();
b184e3ef
MC
400 engine_inited = 1;
401}
402# if !defined(OPENSSL_NO_HW) && \
403 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
404static OPENSSL_INIT_ONCE engine_cryptodev = OPENSSL_INIT_ONCE_STATIC_INIT;
405static 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
b184e3ef 411 engine_load_cryptodev_internal();
b184e3ef
MC
412 engine_inited = 1;
413}
414# endif
415
416# ifndef OPENSSL_NO_RDRAND
417static OPENSSL_INIT_ONCE engine_rdrand = OPENSSL_INIT_ONCE_STATIC_INIT;
418static 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
b184e3ef 424 engine_load_rdrand_internal();
b184e3ef
MC
425 engine_inited = 1;
426}
427# endif
428static OPENSSL_INIT_ONCE engine_dynamic = OPENSSL_INIT_ONCE_STATIC_INIT;
429static 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
b184e3ef 435 engine_load_dynamic_internal();
b184e3ef
MC
436 engine_inited = 1;
437}
438# ifndef OPENSSL_NO_STATIC_ENGINE
439# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
440static OPENSSL_INIT_ONCE engine_padlock = OPENSSL_INIT_ONCE_STATIC_INIT;
441static 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
b184e3ef 447 engine_load_padlock_internal();
b184e3ef
MC
448 engine_inited = 1;
449}
450# endif
451# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
452static OPENSSL_INIT_ONCE engine_capi = OPENSSL_INIT_ONCE_STATIC_INIT;
453static 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
b184e3ef 459 engine_load_capi_internal();
b184e3ef
MC
460 engine_inited = 1;
461}
462# endif
463static OPENSSL_INIT_ONCE engine_dasync = OPENSSL_INIT_ONCE_STATIC_INIT;
464static 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
b184e3ef 470 engine_load_dasync_internal();
b184e3ef
MC
471 engine_inited = 1;
472}
473# endif
474#endif
475
476static OPENSSL_INIT_ONCE zlib = OPENSSL_INIT_ONCE_STATIC_INIT;
477static int zlib_inited = 0;
478static void ossl_init_zlib(void)
479{
480 /* Do nothing - we need to know about this for the later cleanup */
481 zlib_inited = 1;
482}
483
71567a6f 484static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
b184e3ef
MC
485{
486 /* Can't do much about this */
487 if (locals == NULL)
488 return;
489
1288f26f 490#ifndef OPENSSL_NO_ASYNC
b184e3ef
MC
491 if (locals->async) {
492#ifdef OPENSSL_INIT_DEBUG
493 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
494 "ASYNC_cleanup_thread()\n");
495#endif
496 ASYNC_cleanup_thread();
497 }
1288f26f 498#endif
b184e3ef
MC
499
500 if (locals->err_state) {
501#ifdef OPENSSL_INIT_DEBUG
502 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
503 "ERR_remove_thread_state(NULL)\n");
504#endif
505 ERR_remove_thread_state(NULL);
506 }
507
508 OPENSSL_free(locals);
b184e3ef
MC
509}
510
f672aee4 511void OPENSSL_thread_stop(void)
71567a6f
MC
512{
513 ossl_init_thread_stop(
514 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
515}
516
b184e3ef
MC
517int ossl_init_thread_start(uint64_t opts)
518{
519 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
520
521 if (locals == NULL)
522 return 0;
523
524 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
525#ifdef OPENSSL_INIT_DEBUG
526 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
527 "marking thread for async\n");
528#endif
529 locals->async = 1;
530 }
531
532 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
533#ifdef OPENSSL_INIT_DEBUG
534 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
535 "marking thread for err_state\n");
536#endif
537 locals->err_state = 1;
538 }
539
540 return 1;
541}
542
f672aee4 543void OPENSSL_cleanup(void)
b184e3ef
MC
544{
545 OPENSSL_INIT_STOP *currhandler, *lasthandler;
546
deca5df2
MC
547 /* If we've not been inited then no need to deinit */
548 if (!base_inited)
549 return;
550
dd27f16e
RS
551 /* Might be explicitly called and also by atexit */
552 if (stopped)
553 return;
554 stopped = 1;
555
b184e3ef
MC
556 /*
557 * Thread stop may not get automatically called by the thread library for
558 * the very last thread in some situations, so call it directly.
559 */
560 ossl_init_thread_stop(ossl_init_get_thread_local(0));
561
562 currhandler = stop_handlers;
563 while (currhandler != NULL) {
564 currhandler->handler();
565 lasthandler = currhandler;
566 currhandler = currhandler->next;
567 OPENSSL_free(lasthandler);
568 }
569 stop_handlers = NULL;
570 /*
571 * We assume we are single-threaded for this function, i.e. no race
572 * conditions for the various "*_inited" vars below.
573 */
574
575 if (zlib_inited) {
576#ifdef OPENSSL_INIT_DEBUG
f672aee4 577 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b184e3ef
MC
578 "COMP_zlib_cleanup()\n");
579#endif
580 COMP_zlib_cleanup();
b184e3ef
MC
581 }
582
583#ifndef OPENSSL_NO_ENGINE
584 if (engine_inited) {
585# ifdef OPENSSL_INIT_DEBUG
f672aee4 586 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b184e3ef
MC
587 "ENGINE_cleanup()\n");
588# endif
589 ENGINE_cleanup();
b184e3ef
MC
590 }
591#endif
592
b184e3ef
MC
593 if (load_crypto_strings_inited) {
594#ifdef OPENSSL_INIT_DEBUG
f672aee4 595 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b184e3ef
MC
596 "ERR_free_strings()\n");
597#endif
598 ERR_free_strings();
b184e3ef
MC
599 }
600
6bc7bad0
MC
601 ossl_init_thread_stop_cleanup();
602
b184e3ef 603#ifdef OPENSSL_INIT_DEBUG
deca5df2
MC
604 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
605 "CRYPTO_cleanup_all_ex_data()\n");
606 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
607 "EVP_cleanup()\n");
608 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
609 "CONF_modules_free()\n");
610 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
611 "RAND_cleanup()\n");
b184e3ef 612#endif
deca5df2
MC
613 CRYPTO_cleanup_all_ex_data();
614 EVP_cleanup();
615 CONF_modules_free();
616 RAND_cleanup();
617 base_inited = 0;
b184e3ef
MC
618}
619
b184e3ef
MC
620/*
621 * If this function is called with a non NULL settings value then it must be
622 * called prior to any threads making calls to any OpenSSL functions,
623 * i.e. passing a non-null settings value is assumed to be single-threaded.
624 */
0fc32b07 625int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
b184e3ef 626{
302f7588
MC
627 static int stoperrset = 0;
628
629 if (stopped) {
630 if (!stoperrset) {
631 /*
632 * We only ever set this once to avoid getting into an infinite
633 * loop where the error system keeps trying to init and fails so
634 * sets an error etc
635 */
636 stoperrset = 1;
a4625290 637 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
302f7588 638 }
0fc32b07 639 return 0;
302f7588 640 }
dd27f16e 641
b184e3ef
MC
642 ossl_init_once_run(&base, ossl_init_base);
643
644 if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
645 ossl_init_once_run(&load_crypto_strings,
646 ossl_init_no_load_crypto_strings);
647
648 if (opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
649 ossl_init_once_run(&load_crypto_strings, ossl_init_load_crypto_strings);
650
651 if (opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
652 ossl_init_once_run(&add_all_ciphers, ossl_init_no_add_algs);
653
654 if (opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
655 ossl_init_once_run(&add_all_ciphers, ossl_init_add_all_ciphers);
656
657 if (opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
658 ossl_init_once_run(&add_all_digests, ossl_init_no_add_algs);
659
660 if (opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
661 ossl_init_once_run(&add_all_digests, ossl_init_add_all_digests);
662
663 if (opts & OPENSSL_INIT_NO_LOAD_CONFIG) {
664 ossl_init_once_run(&config, ossl_init_no_config);
665 }
666
667 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
bf24111b 668 CRYPTO_w_lock(CRYPTO_LOCK_INIT);
7253fd55 669 config_filename = (settings == NULL) ? NULL : settings->config_name;
b184e3ef 670 ossl_init_once_run(&config, ossl_init_config);
bf24111b 671 CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
b184e3ef
MC
672 }
673
1288f26f 674#ifndef OPENSSL_NO_ASYNC
b184e3ef
MC
675 if (opts & OPENSSL_INIT_ASYNC) {
676 ossl_init_once_run(&async, ossl_init_async);
677 }
1288f26f 678#endif
b184e3ef
MC
679#ifndef OPENSSL_NO_ENGINE
680 if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
681 ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
682 }
683# if !defined(OPENSSL_NO_HW) && \
684 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
685 if (opts & OPENSSL_INIT_ENGINE_CRYPTODEV) {
686 ossl_init_once_run(&engine_cryptodev, ossl_init_engine_cryptodev);
687 }
688# endif
689# ifndef OPENSSL_NO_RDRAND
690 if (opts & OPENSSL_INIT_ENGINE_RDRAND) {
691 ossl_init_once_run(&engine_rdrand, ossl_init_engine_rdrand);
692 }
693# endif
694 if (opts & OPENSSL_INIT_ENGINE_DYNAMIC) {
695 ossl_init_once_run(&engine_dynamic, ossl_init_engine_dynamic);
696 }
697# ifndef OPENSSL_NO_STATIC_ENGINE
698# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
699 if (opts & OPENSSL_INIT_ENGINE_PADLOCK) {
700 ossl_init_once_run(&engine_padlock, ossl_init_engine_padlock);
701 }
702# endif
703# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
704 if (opts & OPENSSL_INIT_ENGINE_CAPI) {
705 ossl_init_once_run(&engine_capi, ossl_init_engine_capi);
706 }
707# endif
708 if (opts & OPENSSL_INIT_ENGINE_DASYNC) {
709 ossl_init_once_run(&engine_dasync, ossl_init_engine_dasync);
710 }
711# endif
712 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
713 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL)) {
714 ENGINE_register_all_complete();
715 }
716#endif
717
718 if (opts & OPENSSL_INIT_ZLIB) {
719 ossl_init_once_run(&zlib, ossl_init_zlib);
720 }
0fc32b07
MC
721
722 return 1;
b184e3ef
MC
723}
724
f672aee4 725int OPENSSL_atexit(void (*handler)(void))
b184e3ef
MC
726{
727 OPENSSL_INIT_STOP *newhand;
728
729 newhand = OPENSSL_malloc(sizeof(*newhand));
730 if (newhand == NULL)
731 return 0;
732
733 newhand->handler = handler;
734 newhand->next = stop_handlers;
735 stop_handlers = newhand;
736
737 return 1;
738}
739
740