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