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