]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/init.c
Have [.VMS]openssl_{startup,shutdown}.com depend on respective *.in
[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
b1f1e7ae 58#include <internal/threads.h>
b184e3ef
MC
59#include <internal/cryptlib_int.h>
60#include <openssl/err.h>
f3cd81d6 61#include <internal/rand.h>
62d876ad 62#include <internal/bio.h>
b184e3ef 63#include <openssl/evp.h>
b184e3ef
MC
64#include <internal/evp_int.h>
65#include <internal/conf.h>
66#include <internal/async.h>
67#include <internal/engine.h>
02a247e0 68#include <internal/comp.h>
b184e3ef 69#include <internal/err.h>
13524b11 70#include <internal/err_int.h>
7b8cc9b3 71#include <internal/objects.h>
b184e3ef 72#include <stdlib.h>
dd27f16e
RS
73#include <assert.h>
74
75static int stopped = 0;
b184e3ef 76
71567a6f
MC
77static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
78
a072ed0c 79static CRYPTO_THREAD_LOCAL threadstopkey;
b184e3ef 80
b184e3ef
MC
81static void ossl_init_thread_stop_wrap(void *local)
82{
83 ossl_init_thread_stop((struct thread_local_inits_st *)local);
84}
85
b7326ea7 86static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
b184e3ef 87{
a072ed0c
MC
88 struct thread_local_inits_st *local =
89 CRYPTO_THREAD_get_local(&threadstopkey);
b184e3ef
MC
90
91 if (local == NULL && alloc) {
92 local = OPENSSL_zalloc(sizeof *local);
a072ed0c 93 CRYPTO_THREAD_set_local(&threadstopkey, local);
b184e3ef 94 }
b7326ea7 95 if (!alloc) {
a072ed0c 96 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
b7326ea7 97 }
b184e3ef
MC
98
99 return local;
100}
101
7253fd55 102typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
b184e3ef
MC
103struct ossl_init_stop_st {
104 void (*handler)(void);
105 OPENSSL_INIT_STOP *next;
106};
107
108static OPENSSL_INIT_STOP *stop_handlers = NULL;
c292b105 109static CRYPTO_RWLOCK *init_lock = NULL;
b184e3ef 110
b1f1e7ae 111static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
112static int base_inited = 0;
113static void ossl_init_base(void)
114{
115#ifdef OPENSSL_INIT_DEBUG
116 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
117#endif
a072ed0c
MC
118 /*
119 * We use a dummy thread local key here. We use the destructor to detect
120 * when the thread is going to stop (where that feature is available)
121 */
122 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
c7b7938e 123#ifndef OPENSSL_SYS_UEFI
f672aee4 124 atexit(OPENSSL_cleanup);
c7b7938e 125#endif
c292b105 126 init_lock = CRYPTO_THREAD_lock_new();
b184e3ef
MC
127 OPENSSL_cpuid_setup();
128 base_inited = 1;
129}
130
b1f1e7ae 131static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
132static int load_crypto_strings_inited = 0;
133static void ossl_init_no_load_crypto_strings(void)
134{
135 /* Do nothing in this case */
136 return;
137}
138
139static void ossl_init_load_crypto_strings(void)
140{
498abff0
MC
141 /*
142 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
143 * pulling in all the error strings during static linking
144 */
145#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
b184e3ef
MC
146# ifdef OPENSSL_INIT_DEBUG
147 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
b3599dbb 148 "err_load_crypto_strings_int()\n");
b184e3ef 149# endif
b3599dbb 150 err_load_crypto_strings_int();
b184e3ef
MC
151#endif
152 load_crypto_strings_inited = 1;
153}
154
b1f1e7ae 155static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
156static void ossl_init_add_all_ciphers(void)
157{
158 /*
159 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
160 * pulling in all the ciphers during static linking
161 */
162#ifndef OPENSSL_NO_AUTOALGINIT
163# ifdef OPENSSL_INIT_DEBUG
164 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
b3599dbb 165 "openssl_add_all_ciphers_int()\n");
b184e3ef 166# endif
b3599dbb 167 openssl_add_all_ciphers_int();
b184e3ef
MC
168# ifndef OPENSSL_NO_ENGINE
169# if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
170 ENGINE_setup_bsd_cryptodev();
171# endif
172# endif
173#endif
174}
175
b1f1e7ae 176static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
177static void ossl_init_add_all_digests(void)
178{
179 /*
180 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
181 * pulling in all the ciphers during static linking
182 */
183#ifndef OPENSSL_NO_AUTOALGINIT
184# ifdef OPENSSL_INIT_DEBUG
185 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
b3599dbb 186 "openssl_add_all_digests()\n");
b184e3ef 187# endif
b3599dbb 188 openssl_add_all_digests_int();
b184e3ef
MC
189# ifndef OPENSSL_NO_ENGINE
190# if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
191 ENGINE_setup_bsd_cryptodev();
192# endif
193# endif
194#endif
195}
196
197static void ossl_init_no_add_algs(void)
198{
199 /* Do nothing */
200 return;
201}
202
b1f1e7ae 203static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
204static int config_inited = 0;
205static const char *config_filename;
206static void ossl_init_config(void)
207{
208#ifdef OPENSSL_INIT_DEBUG
209 fprintf(stderr,
b3599dbb 210 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
b184e3ef
MC
211 config_filename==NULL?"NULL":config_filename);
212#endif
b3599dbb 213 openssl_config_int(config_filename);
b184e3ef
MC
214 config_inited = 1;
215}
216static void ossl_init_no_config(void)
217{
218#ifdef OPENSSL_INIT_DEBUG
219 fprintf(stderr,
b3599dbb 220 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
b184e3ef 221#endif
b3599dbb 222 openssl_no_config_int();
b184e3ef
MC
223 config_inited = 1;
224}
225
b1f1e7ae 226static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
227static int async_inited = 0;
228static void ossl_init_async(void)
229{
230#ifdef OPENSSL_INIT_DEBUG
231 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
232#endif
b184e3ef 233 async_init();
b184e3ef
MC
234 async_inited = 1;
235}
236
237#ifndef OPENSSL_NO_ENGINE
b1f1e7ae 238static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
239static void ossl_init_engine_openssl(void)
240{
241# ifdef OPENSSL_INIT_DEBUG
242 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
b3599dbb 243 "engine_load_openssl_int()\n");
b184e3ef 244# endif
b3599dbb 245 engine_load_openssl_int();
b184e3ef
MC
246}
247# if !defined(OPENSSL_NO_HW) && \
248 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
b1f1e7ae 249static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
250static void ossl_init_engine_cryptodev(void)
251{
252# ifdef OPENSSL_INIT_DEBUG
253 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
b3599dbb 254 "engine_load_cryptodev_int()\n");
b184e3ef 255# endif
b3599dbb 256 engine_load_cryptodev_int();
b184e3ef
MC
257}
258# endif
259
260# ifndef OPENSSL_NO_RDRAND
b1f1e7ae 261static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
262static void ossl_init_engine_rdrand(void)
263{
264# ifdef OPENSSL_INIT_DEBUG
265 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
b3599dbb 266 "engine_load_rdrand_int()\n");
b184e3ef 267# endif
b3599dbb 268 engine_load_rdrand_int();
b184e3ef
MC
269}
270# endif
b1f1e7ae 271static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
272static void ossl_init_engine_dynamic(void)
273{
274# ifdef OPENSSL_INIT_DEBUG
275 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
b3599dbb 276 "engine_load_dynamic_int()\n");
b184e3ef 277# endif
b3599dbb 278 engine_load_dynamic_int();
b184e3ef
MC
279}
280# ifndef OPENSSL_NO_STATIC_ENGINE
281# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
b1f1e7ae 282static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
283static void ossl_init_engine_padlock(void)
284{
285# ifdef OPENSSL_INIT_DEBUG
286 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
b3599dbb 287 "engine_load_padlock_int()\n");
b184e3ef 288# endif
b3599dbb 289 engine_load_padlock_int();
b184e3ef
MC
290}
291# endif
292# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
b1f1e7ae 293static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
294static void ossl_init_engine_capi(void)
295{
296# ifdef OPENSSL_INIT_DEBUG
297 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
b3599dbb 298 "engine_load_capi_int()\n");
b184e3ef 299# endif
b3599dbb 300 engine_load_capi_int();
b184e3ef
MC
301}
302# endif
b1f1e7ae 303static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
b184e3ef
MC
304static void ossl_init_engine_dasync(void)
305{
306# ifdef OPENSSL_INIT_DEBUG
307 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
b3599dbb 308 "engine_load_dasync_int()\n");
b184e3ef 309# endif
b3599dbb 310 engine_load_dasync_int();
b184e3ef 311}
6cba4a66 312# if !defined(OPENSSL_NO_AFALGENG)
a4d8bcf1 313static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
6cba4a66 314static void ossl_init_engine_afalg(void)
315{
316# ifdef OPENSSL_INIT_DEBUG
317 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
b3599dbb 318 "engine_load_afalg_int()\n");
6cba4a66 319# endif
b3599dbb 320 engine_load_afalg_int();
6cba4a66 321}
322# endif
b184e3ef
MC
323# endif
324#endif
325
e4ad0763 326#ifndef OPENSSL_NO_COMP
b1f1e7ae 327static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
e4ad0763 328
b184e3ef
MC
329static int zlib_inited = 0;
330static void ossl_init_zlib(void)
331{
332 /* Do nothing - we need to know about this for the later cleanup */
333 zlib_inited = 1;
334}
e4ad0763 335#endif
b184e3ef 336
71567a6f 337static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
b184e3ef
MC
338{
339 /* Can't do much about this */
340 if (locals == NULL)
341 return;
342
343 if (locals->async) {
344#ifdef OPENSSL_INIT_DEBUG
345 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
346 "ASYNC_cleanup_thread()\n");
347#endif
348 ASYNC_cleanup_thread();
349 }
350
351 if (locals->err_state) {
352#ifdef OPENSSL_INIT_DEBUG
353 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
8509dcc9 354 "ERR_remove_thread_state()\n");
b184e3ef 355#endif
8509dcc9 356 ERR_remove_thread_state();
b184e3ef
MC
357 }
358
359 OPENSSL_free(locals);
b184e3ef
MC
360}
361
f672aee4 362void OPENSSL_thread_stop(void)
71567a6f
MC
363{
364 ossl_init_thread_stop(
365 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
366}
367
b184e3ef
MC
368int ossl_init_thread_start(uint64_t opts)
369{
370 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
371
372 if (locals == NULL)
373 return 0;
374
375 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
376#ifdef OPENSSL_INIT_DEBUG
377 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
378 "marking thread for async\n");
379#endif
380 locals->async = 1;
381 }
382
383 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
384#ifdef OPENSSL_INIT_DEBUG
385 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
386 "marking thread for err_state\n");
387#endif
388 locals->err_state = 1;
389 }
390
391 return 1;
392}
393
f672aee4 394void OPENSSL_cleanup(void)
b184e3ef
MC
395{
396 OPENSSL_INIT_STOP *currhandler, *lasthandler;
397
deca5df2
MC
398 /* If we've not been inited then no need to deinit */
399 if (!base_inited)
400 return;
401
dd27f16e
RS
402 /* Might be explicitly called and also by atexit */
403 if (stopped)
404 return;
405 stopped = 1;
406
b184e3ef
MC
407 /*
408 * Thread stop may not get automatically called by the thread library for
409 * the very last thread in some situations, so call it directly.
410 */
411 ossl_init_thread_stop(ossl_init_get_thread_local(0));
412
413 currhandler = stop_handlers;
414 while (currhandler != NULL) {
415 currhandler->handler();
416 lasthandler = currhandler;
417 currhandler = currhandler->next;
418 OPENSSL_free(lasthandler);
419 }
420 stop_handlers = NULL;
c292b105
MC
421
422 CRYPTO_THREAD_lock_free(init_lock);
423
b184e3ef
MC
424 /*
425 * We assume we are single-threaded for this function, i.e. no race
426 * conditions for the various "*_inited" vars below.
427 */
428
e4ad0763 429#ifndef OPENSSL_NO_COMP
b184e3ef
MC
430 if (zlib_inited) {
431#ifdef OPENSSL_INIT_DEBUG
f672aee4 432 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 433 "comp_zlib_cleanup_int()\n");
b184e3ef 434#endif
b3599dbb 435 comp_zlib_cleanup_int();
b184e3ef 436 }
e4ad0763 437#endif
b184e3ef 438
ed49f43a
MC
439 if (async_inited) {
440# ifdef OPENSSL_INIT_DEBUG
441 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
442 "async_deinit()\n");
443# endif
444 async_deinit();
445 }
ed49f43a 446
b184e3ef
MC
447 if (load_crypto_strings_inited) {
448#ifdef OPENSSL_INIT_DEBUG
f672aee4 449 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 450 "err_free_strings_int()\n");
b184e3ef 451#endif
b3599dbb 452 err_free_strings_int();
b184e3ef
MC
453 }
454
a072ed0c 455 CRYPTO_THREAD_cleanup_local(&threadstopkey);
6bc7bad0 456
b184e3ef 457#ifdef OPENSSL_INIT_DEBUG
58a8fc25 458 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 459 "rand_cleanup_int()\n");
58a8fc25 460 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 461 "conf_modules_free_int()\n");
9749a07a 462#ifndef OPENSSL_NO_ENGINE
ae6412f3 463 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 464 "engine_cleanup_int()\n");
9749a07a 465#endif
58a8fc25 466 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 467 "crypto_cleanup_all_ex_data_int()\n");
58a8fc25 468 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 469 "bio_sock_cleanup_int()\n");
ff234405
MC
470 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
471 "bio_cleanup()\n");
58a8fc25 472 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 473 "evp_cleanup_int()\n");
58a8fc25 474 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
b3599dbb 475 "obj_cleanup_int()\n");
ff234405
MC
476 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
477 "err_cleanup()\n");
9749a07a 478#endif
58a8fc25
MC
479 /*
480 * Note that cleanup order is important:
a535fe12 481 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
b3599dbb 482 * must be called before engine_cleanup_int()
58a8fc25
MC
483 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
484 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
b3599dbb
MC
485 * - conf_modules_free_int() can end up in ENGINE code so must be called
486 * before engine_cleanup_int()
a535fe12
DSH
487 * - ENGINEs and additional EVP algorithms might use added OIDs names so
488 * obj_cleanup_int() must be called last
58a8fc25 489 */
b3599dbb
MC
490 rand_cleanup_int();
491 conf_modules_free_int();
773fd0ba 492#ifndef OPENSSL_NO_ENGINE
b3599dbb 493 engine_cleanup_int();
773fd0ba 494#endif
b3599dbb 495 crypto_cleanup_all_ex_data_int();
ff234405 496 bio_cleanup();
b3599dbb
MC
497 evp_cleanup_int();
498 obj_cleanup_int();
ff234405
MC
499 err_cleanup();
500
deca5df2 501 base_inited = 0;
b184e3ef
MC
502}
503
b184e3ef
MC
504/*
505 * If this function is called with a non NULL settings value then it must be
506 * called prior to any threads making calls to any OpenSSL functions,
507 * i.e. passing a non-null settings value is assumed to be single-threaded.
508 */
0fc32b07 509int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
b184e3ef 510{
302f7588
MC
511 static int stoperrset = 0;
512
513 if (stopped) {
514 if (!stoperrset) {
515 /*
516 * We only ever set this once to avoid getting into an infinite
517 * loop where the error system keeps trying to init and fails so
518 * sets an error etc
519 */
520 stoperrset = 1;
a4625290 521 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
302f7588 522 }
0fc32b07 523 return 0;
302f7588 524 }
dd27f16e 525
b1f1e7ae
MC
526 if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
527 return 0;
b184e3ef 528
b1f1e7ae
MC
529 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
530 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
531 ossl_init_no_load_crypto_strings))
532 return 0;
b184e3ef 533
b1f1e7ae
MC
534 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
535 && !CRYPTO_THREAD_run_once(&load_crypto_strings,
536 ossl_init_load_crypto_strings))
537 return 0;
b184e3ef 538
b1f1e7ae
MC
539 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
540 && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
541 return 0;
b184e3ef 542
b1f1e7ae
MC
543 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
544 && !CRYPTO_THREAD_run_once(&add_all_ciphers,
545 ossl_init_add_all_ciphers))
546 return 0;
b184e3ef 547
b1f1e7ae
MC
548 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
549 && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
550 return 0;
b184e3ef 551
b1f1e7ae
MC
552 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
553 && !CRYPTO_THREAD_run_once(&add_all_digests,
554 ossl_init_add_all_digests))
555 return 0;
b184e3ef 556
b1f1e7ae
MC
557 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
558 && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
559 return 0;
b184e3ef
MC
560
561 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
b1f1e7ae 562 int ret;
c292b105 563 CRYPTO_THREAD_write_lock(init_lock);
7253fd55 564 config_filename = (settings == NULL) ? NULL : settings->config_name;
b1f1e7ae 565 ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
c292b105 566 CRYPTO_THREAD_unlock(init_lock);
b1f1e7ae
MC
567 if (!ret)
568 return 0;
b184e3ef
MC
569 }
570
b1f1e7ae
MC
571 if ((opts & OPENSSL_INIT_ASYNC)
572 && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
573 return 0;
7626fbf2 574
b184e3ef 575#ifndef OPENSSL_NO_ENGINE
b1f1e7ae
MC
576 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
577 && !CRYPTO_THREAD_run_once(&engine_openssl,
578 ossl_init_engine_openssl))
579 return 0;
b184e3ef
MC
580# if !defined(OPENSSL_NO_HW) && \
581 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
b1f1e7ae
MC
582 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
583 && !CRYPTO_THREAD_run_once(&engine_cryptodev,
584 ossl_init_engine_cryptodev))
585 return 0;
b184e3ef
MC
586# endif
587# ifndef OPENSSL_NO_RDRAND
b1f1e7ae
MC
588 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
589 && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
590 return 0;
b184e3ef 591# endif
b1f1e7ae
MC
592 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
593 && !CRYPTO_THREAD_run_once(&engine_dynamic,
594 ossl_init_engine_dynamic))
595 return 0;
b184e3ef
MC
596# ifndef OPENSSL_NO_STATIC_ENGINE
597# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
b1f1e7ae 598 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
3ddd1d04
EK
599 && !CRYPTO_THREAD_run_once(&engine_padlock,
600 ossl_init_engine_padlock))
b1f1e7ae 601 return 0;
b184e3ef
MC
602# endif
603# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
b1f1e7ae 604 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
3ddd1d04 605 && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
b1f1e7ae 606 return 0;
b184e3ef 607# endif
b1f1e7ae
MC
608 if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
609 && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
610 return 0;
6cba4a66 611# if !defined(OPENSSL_NO_AFALGENG)
b1f1e7ae
MC
612 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
613 && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
614 return 0;
6cba4a66 615# endif
b184e3ef
MC
616# endif
617 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
6cba4a66 618 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
619 | OPENSSL_INIT_ENGINE_AFALG)) {
b184e3ef
MC
620 ENGINE_register_all_complete();
621 }
622#endif
623
e4ad0763 624#ifndef OPENSSL_NO_COMP
b1f1e7ae 625 if ((opts & OPENSSL_INIT_ZLIB)
3ddd1d04 626 && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
b1f1e7ae 627 return 0;
e4ad0763 628#endif
0fc32b07
MC
629
630 return 1;
b184e3ef
MC
631}
632
f672aee4 633int OPENSSL_atexit(void (*handler)(void))
b184e3ef
MC
634{
635 OPENSSL_INIT_STOP *newhand;
636
637 newhand = OPENSSL_malloc(sizeof(*newhand));
638 if (newhand == NULL)
639 return 0;
640
641 newhand->handler = handler;
642 newhand->next = stop_handlers;
643 stop_handlers = newhand;
644
645 return 1;
646}