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