Add automatic initializations support for EVP_MAC objects
[openssl.git] / crypto / init.c
1 /*
2  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "e_os.h"
11 #include "internal/cryptlib_int.h"
12 #include <openssl/err.h>
13 #include "internal/rand_int.h"
14 #include "internal/bio.h"
15 #include <openssl/evp.h>
16 #include "internal/evp_int.h"
17 #include "internal/conf.h"
18 #include "internal/async.h"
19 #include "internal/engine.h"
20 #include "internal/comp.h"
21 #include "internal/err.h"
22 #include "internal/err_int.h"
23 #include "internal/objects.h"
24 #include <stdlib.h>
25 #include <assert.h>
26 #include "internal/thread_once.h"
27 #include "internal/dso_conf.h"
28 #include "internal/dso.h"
29 #include "internal/store.h"
30
31 static int stopped = 0;
32
33 /*
34  * Since per-thread-specific-data destructors are not universally
35  * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key
36  * is assumed to have destructor associated. And then an effort is made
37  * to call this single destructor on non-pthread platform[s].
38  *
39  * Initial value is "impossible". It is used as guard value to shortcut
40  * destructor for threads terminating before libcrypto is initialized or
41  * after it's de-initialized. Access to the key doesn't have to be
42  * serialized for the said threads, because they didn't use libcrypto
43  * and it doesn't matter if they pick "impossible" or derefernce real
44  * key value and pull NULL past initialization in the first thread that
45  * intends to use libcrypto.
46  */
47 static union {
48     long sane;
49     CRYPTO_THREAD_LOCAL value;
50 } destructor_key = { -1 };
51
52 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
53
54 static void ossl_init_thread_destructor(void *local)
55 {
56     ossl_init_thread_stop((struct thread_local_inits_st *)local);
57 }
58
59 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
60 {
61     struct thread_local_inits_st *local =
62         CRYPTO_THREAD_get_local(&destructor_key.value);
63
64     if (alloc) {
65         if (local == NULL
66             && (local = OPENSSL_zalloc(sizeof(*local))) != NULL
67             && !CRYPTO_THREAD_set_local(&destructor_key.value, local)) {
68             OPENSSL_free(local);
69             return NULL;
70         }
71     } else {
72         CRYPTO_THREAD_set_local(&destructor_key.value, NULL);
73     }
74
75     return local;
76 }
77
78 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
79 struct ossl_init_stop_st {
80     void (*handler)(void);
81     OPENSSL_INIT_STOP *next;
82 };
83
84 static OPENSSL_INIT_STOP *stop_handlers = NULL;
85 static CRYPTO_RWLOCK *init_lock = NULL;
86
87 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
88 static int base_inited = 0;
89 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
90 {
91     CRYPTO_THREAD_LOCAL key;
92
93 #ifdef OPENSSL_INIT_DEBUG
94     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
95 #endif
96 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
97     ossl_malloc_setup_failures();
98 #endif
99     if (!CRYPTO_THREAD_init_local(&key, ossl_init_thread_destructor))
100         return 0;
101     if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
102         goto err;
103 #ifndef OPENSSL_SYS_UEFI
104     if (atexit(OPENSSL_cleanup) != 0)
105         goto err;
106 #endif
107     OPENSSL_cpuid_setup();
108
109     destructor_key.value = key;
110     base_inited = 1;
111     return 1;
112
113 err:
114 #ifdef OPENSSL_INIT_DEBUG
115     fprintf(stderr, "OPENSSL_INIT: ossl_init_base not ok!\n");
116 #endif
117     CRYPTO_THREAD_lock_free(init_lock);
118     init_lock = NULL;
119
120     CRYPTO_THREAD_cleanup_local(&key);
121     return 0;
122 }
123
124 static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
125 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
126 {
127 #ifdef OPENSSL_INIT_DEBUG
128     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_nodelete()\n");
129 #endif
130 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
131 # ifdef DSO_WIN32
132     {
133         HMODULE handle = NULL;
134         BOOL ret;
135
136         /* We don't use the DSO route for WIN32 because there is a better way */
137         ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
138                                 | GET_MODULE_HANDLE_EX_FLAG_PIN,
139                                 (void *)&base_inited, &handle);
140
141 #  ifdef OPENSSL_INIT_DEBUG
142         fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
143                 (ret == TRUE ? "No!" : "Yes."));
144 #  endif
145         return (ret == TRUE) ? 1 : 0;
146     }
147 # else
148     /*
149      * Deliberately leak a reference to ourselves. This will force the library
150      * to remain loaded until the atexit() handler is run at process exit.
151      */
152     {
153         DSO *dso;
154         void *err;
155
156         if (!err_shelve_state(&err))
157             return 0;
158
159         dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
160 #  ifdef OPENSSL_INIT_DEBUG
161         fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
162                 (dso == NULL ? "No!" : "Yes."));
163         /*
164          * In case of No!, it is uncertain our exit()-handlers can still be
165          * called. After dlclose() the whole library might have been unloaded
166          * already.
167          */
168 #  endif
169         DSO_free(dso);
170         err_unshelve_state(err);
171     }
172 # endif
173 #endif
174
175     return 1;
176 }
177
178 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
179 static int load_crypto_strings_inited = 0;
180 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
181 {
182     /* Do nothing in this case */
183     return 1;
184 }
185
186 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
187 {
188     int ret = 1;
189     /*
190      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
191      * pulling in all the error strings during static linking
192      */
193 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
194 # ifdef OPENSSL_INIT_DEBUG
195     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
196                     "err_load_crypto_strings_int()\n");
197 # endif
198     ret = err_load_crypto_strings_int();
199     load_crypto_strings_inited = 1;
200 #endif
201     return ret;
202 }
203
204 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
205 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
206 {
207     /*
208      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
209      * pulling in all the ciphers during static linking
210      */
211 #ifndef OPENSSL_NO_AUTOALGINIT
212 # ifdef OPENSSL_INIT_DEBUG
213     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
214                     "openssl_add_all_ciphers_int()\n");
215 # endif
216     openssl_add_all_ciphers_int();
217 #endif
218     return 1;
219 }
220
221 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
222 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
223 {
224     /*
225      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
226      * pulling in all the ciphers during static linking
227      */
228 #ifndef OPENSSL_NO_AUTOALGINIT
229 # ifdef OPENSSL_INIT_DEBUG
230     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
231                     "openssl_add_all_digests()\n");
232 # endif
233     openssl_add_all_digests_int();
234 #endif
235     return 1;
236 }
237
238 static CRYPTO_ONCE add_all_macs = CRYPTO_ONCE_STATIC_INIT;
239 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_macs)
240 {
241     /*
242      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
243      * pulling in all the macs during static linking
244      */
245 #ifndef OPENSSL_NO_AUTOALGINIT
246 # ifdef OPENSSL_INIT_DEBUG
247     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_macs: "
248                     "openssl_add_all_macs_int()\n");
249 # endif
250     openssl_add_all_macs_int();
251 #endif
252     return 1;
253 }
254
255 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
256 {
257     /* Do nothing */
258     return 1;
259 }
260
261 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
262 static int config_inited = 0;
263 static const char *appname;
264 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
265 {
266 #ifdef OPENSSL_INIT_DEBUG
267     fprintf(stderr,
268             "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
269             appname == NULL ? "NULL" : appname);
270 #endif
271     openssl_config_int(appname);
272     config_inited = 1;
273     return 1;
274 }
275 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
276 {
277 #ifdef OPENSSL_INIT_DEBUG
278     fprintf(stderr,
279             "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
280 #endif
281     openssl_no_config_int();
282     config_inited = 1;
283     return 1;
284 }
285
286 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
287 static int async_inited = 0;
288 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
289 {
290 #ifdef OPENSSL_INIT_DEBUG
291     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
292 #endif
293     if (!async_init())
294         return 0;
295     async_inited = 1;
296     return 1;
297 }
298
299 #ifndef OPENSSL_NO_ENGINE
300 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
301 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
302 {
303 # ifdef OPENSSL_INIT_DEBUG
304     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
305                     "engine_load_openssl_int()\n");
306 # endif
307     engine_load_openssl_int();
308     return 1;
309 }
310 # ifndef OPENSSL_NO_DEVCRYPTOENG
311 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
312 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
313 {
314 #  ifdef OPENSSL_INIT_DEBUG
315     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
316                     "engine_load_devcrypto_int()\n");
317 #  endif
318     engine_load_devcrypto_int();
319     return 1;
320 }
321 # endif
322
323 # ifndef OPENSSL_NO_RDRAND
324 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
325 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
326 {
327 #  ifdef OPENSSL_INIT_DEBUG
328     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
329                     "engine_load_rdrand_int()\n");
330 #  endif
331     engine_load_rdrand_int();
332     return 1;
333 }
334 # endif
335 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
336 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
337 {
338 # ifdef OPENSSL_INIT_DEBUG
339     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
340                     "engine_load_dynamic_int()\n");
341 # endif
342     engine_load_dynamic_int();
343     return 1;
344 }
345 # ifndef OPENSSL_NO_STATIC_ENGINE
346 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
347 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
348 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
349 {
350 #   ifdef OPENSSL_INIT_DEBUG
351     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
352                     "engine_load_padlock_int()\n");
353 #   endif
354     engine_load_padlock_int();
355     return 1;
356 }
357 #  endif
358 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
359 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
360 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
361 {
362 #   ifdef OPENSSL_INIT_DEBUG
363     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
364                     "engine_load_capi_int()\n");
365 #   endif
366     engine_load_capi_int();
367     return 1;
368 }
369 #  endif
370 #  if !defined(OPENSSL_NO_AFALGENG)
371 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
372 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
373 {
374 #   ifdef OPENSSL_INIT_DEBUG
375     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
376                     "engine_load_afalg_int()\n");
377 #   endif
378     engine_load_afalg_int();
379     return 1;
380 }
381 #  endif
382 # endif
383 #endif
384
385 #ifndef OPENSSL_NO_COMP
386 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
387
388 static int zlib_inited = 0;
389 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
390 {
391     /* Do nothing - we need to know about this for the later cleanup */
392     zlib_inited = 1;
393     return 1;
394 }
395 #endif
396
397 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
398 {
399     /* Can't do much about this */
400     if (locals == NULL)
401         return;
402
403     if (locals->async) {
404 #ifdef OPENSSL_INIT_DEBUG
405         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
406                         "async_delete_thread_state()\n");
407 #endif
408         async_delete_thread_state();
409     }
410
411     if (locals->err_state) {
412 #ifdef OPENSSL_INIT_DEBUG
413         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
414                         "err_delete_thread_state()\n");
415 #endif
416         err_delete_thread_state();
417     }
418
419     if (locals->rand) {
420 #ifdef OPENSSL_INIT_DEBUG
421         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
422                         "drbg_delete_thread_state()\n");
423 #endif
424         drbg_delete_thread_state();
425     }
426
427     OPENSSL_free(locals);
428 }
429
430 void OPENSSL_thread_stop(void)
431 {
432     if (destructor_key.sane != -1)
433         ossl_init_thread_stop(ossl_init_get_thread_local(0));
434 }
435
436 int ossl_init_thread_start(uint64_t opts)
437 {
438     struct thread_local_inits_st *locals;
439
440     if (!OPENSSL_init_crypto(0, NULL))
441         return 0;
442
443     locals = ossl_init_get_thread_local(1);
444
445     if (locals == NULL)
446         return 0;
447
448     if (opts & OPENSSL_INIT_THREAD_ASYNC) {
449 #ifdef OPENSSL_INIT_DEBUG
450         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
451                         "marking thread for async\n");
452 #endif
453         locals->async = 1;
454     }
455
456     if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
457 #ifdef OPENSSL_INIT_DEBUG
458         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
459                         "marking thread for err_state\n");
460 #endif
461         locals->err_state = 1;
462     }
463
464     if (opts & OPENSSL_INIT_THREAD_RAND) {
465 #ifdef OPENSSL_INIT_DEBUG
466         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
467                         "marking thread for rand\n");
468 #endif
469         locals->rand = 1;
470     }
471
472     return 1;
473 }
474
475 void OPENSSL_cleanup(void)
476 {
477     OPENSSL_INIT_STOP *currhandler, *lasthandler;
478     CRYPTO_THREAD_LOCAL key;
479
480     /* If we've not been inited then no need to deinit */
481     if (!base_inited)
482         return;
483
484     /* Might be explicitly called and also by atexit */
485     if (stopped)
486         return;
487     stopped = 1;
488
489     /*
490      * Thread stop may not get automatically called by the thread library for
491      * the very last thread in some situations, so call it directly.
492      */
493     ossl_init_thread_stop(ossl_init_get_thread_local(0));
494
495     currhandler = stop_handlers;
496     while (currhandler != NULL) {
497         currhandler->handler();
498         lasthandler = currhandler;
499         currhandler = currhandler->next;
500         OPENSSL_free(lasthandler);
501     }
502     stop_handlers = NULL;
503
504     CRYPTO_THREAD_lock_free(init_lock);
505     init_lock = NULL;
506
507     /*
508      * We assume we are single-threaded for this function, i.e. no race
509      * conditions for the various "*_inited" vars below.
510      */
511
512 #ifndef OPENSSL_NO_COMP
513     if (zlib_inited) {
514 #ifdef OPENSSL_INIT_DEBUG
515         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
516                         "comp_zlib_cleanup_int()\n");
517 #endif
518         comp_zlib_cleanup_int();
519     }
520 #endif
521
522     if (async_inited) {
523 # ifdef OPENSSL_INIT_DEBUG
524         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
525                         "async_deinit()\n");
526 # endif
527         async_deinit();
528     }
529
530     if (load_crypto_strings_inited) {
531 #ifdef OPENSSL_INIT_DEBUG
532         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
533                         "err_free_strings_int()\n");
534 #endif
535         err_free_strings_int();
536     }
537
538     key = destructor_key.value;
539     destructor_key.sane = -1;
540     CRYPTO_THREAD_cleanup_local(&key);
541
542 #ifdef OPENSSL_INIT_DEBUG
543     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
544                     "rand_cleanup_int()\n");
545     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
546                     "conf_modules_free_int()\n");
547 #ifndef OPENSSL_NO_ENGINE
548     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
549                     "engine_cleanup_int()\n");
550 #endif
551     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
552                     "crypto_cleanup_all_ex_data_int()\n");
553     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
554                     "bio_sock_cleanup_int()\n");
555     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
556                     "bio_cleanup()\n");
557     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
558                     "evp_cleanup_int()\n");
559     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
560                     "obj_cleanup_int()\n");
561     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
562                     "err_cleanup()\n");
563 #endif
564     /*
565      * Note that cleanup order is important:
566      * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
567      * must be called before engine_cleanup_int()
568      * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
569      * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
570      * - conf_modules_free_int() can end up in ENGINE code so must be called
571      * before engine_cleanup_int()
572      * - ENGINEs and additional EVP algorithms might use added OIDs names so
573      * obj_cleanup_int() must be called last
574      */
575     rand_cleanup_int();
576     rand_drbg_cleanup_int();
577     conf_modules_free_int();
578 #ifndef OPENSSL_NO_ENGINE
579     engine_cleanup_int();
580 #endif
581     ossl_store_cleanup_int();
582     crypto_cleanup_all_ex_data_int();
583     bio_cleanup();
584     evp_cleanup_int();
585     obj_cleanup_int();
586     err_cleanup();
587
588     CRYPTO_secure_malloc_done();
589
590     base_inited = 0;
591 }
592
593 /*
594  * If this function is called with a non NULL settings value then it must be
595  * called prior to any threads making calls to any OpenSSL functions,
596  * i.e. passing a non-null settings value is assumed to be single-threaded.
597  */
598 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
599 {
600     if (stopped) {
601         if (!(opts & OPENSSL_INIT_BASE_ONLY))
602             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
603         return 0;
604     }
605
606     if (!RUN_ONCE(&base, ossl_init_base))
607         return 0;
608
609     if (!(opts & OPENSSL_INIT_BASE_ONLY)
610             && !RUN_ONCE(&load_crypto_nodelete,
611                          ossl_init_load_crypto_nodelete))
612         return 0;
613
614     if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
615             && !RUN_ONCE(&load_crypto_strings,
616                          ossl_init_no_load_crypto_strings))
617         return 0;
618
619     if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
620             && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
621         return 0;
622
623     if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
624             && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
625         return 0;
626
627     if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
628             && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
629         return 0;
630
631     if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
632             && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
633         return 0;
634
635     if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
636             && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
637         return 0;
638
639     if ((opts & OPENSSL_INIT_NO_ADD_ALL_MACS)
640             && !RUN_ONCE(&add_all_macs, ossl_init_no_add_algs))
641         return 0;
642
643     if ((opts & OPENSSL_INIT_ADD_ALL_MACS)
644             && !RUN_ONCE(&add_all_macs, ossl_init_add_all_macs))
645         return 0;
646
647     if ((opts & OPENSSL_INIT_ATFORK)
648             && !openssl_init_fork_handlers())
649         return 0;
650
651     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
652             && !RUN_ONCE(&config, ossl_init_no_config))
653         return 0;
654
655     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
656         int ret;
657         CRYPTO_THREAD_write_lock(init_lock);
658         appname = (settings == NULL) ? NULL : settings->appname;
659         ret = RUN_ONCE(&config, ossl_init_config);
660         CRYPTO_THREAD_unlock(init_lock);
661         if (!ret)
662             return 0;
663     }
664
665     if ((opts & OPENSSL_INIT_ASYNC)
666             && !RUN_ONCE(&async, ossl_init_async))
667         return 0;
668
669 #ifndef OPENSSL_NO_ENGINE
670     if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
671             && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
672         return 0;
673 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
674     if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
675             && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
676         return 0;
677 # endif
678 # ifndef OPENSSL_NO_RDRAND
679     if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
680             && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
681         return 0;
682 # endif
683     if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
684             && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
685         return 0;
686 # ifndef OPENSSL_NO_STATIC_ENGINE
687 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
688     if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
689             && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
690         return 0;
691 #  endif
692 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
693     if ((opts & OPENSSL_INIT_ENGINE_CAPI)
694             && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
695         return 0;
696 #  endif
697 #  if !defined(OPENSSL_NO_AFALGENG)
698     if ((opts & OPENSSL_INIT_ENGINE_AFALG)
699             && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
700         return 0;
701 #  endif
702 # endif
703     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
704                 | OPENSSL_INIT_ENGINE_OPENSSL
705                 | OPENSSL_INIT_ENGINE_AFALG)) {
706         ENGINE_register_all_complete();
707     }
708 #endif
709
710 #ifndef OPENSSL_NO_COMP
711     if ((opts & OPENSSL_INIT_ZLIB)
712             && !RUN_ONCE(&zlib, ossl_init_zlib))
713         return 0;
714 #endif
715
716     return 1;
717 }
718
719 int OPENSSL_atexit(void (*handler)(void))
720 {
721     OPENSSL_INIT_STOP *newhand;
722
723 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
724     {
725         union {
726             void *sym;
727             void (*func)(void);
728         } handlersym;
729
730         handlersym.func = handler;
731 # ifdef DSO_WIN32
732         {
733             HMODULE handle = NULL;
734             BOOL ret;
735
736             /*
737              * We don't use the DSO route for WIN32 because there is a better
738              * way
739              */
740             ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
741                                     | GET_MODULE_HANDLE_EX_FLAG_PIN,
742                                     handlersym.sym, &handle);
743
744             if (!ret)
745                 return 0;
746         }
747 # else
748         /*
749          * Deliberately leak a reference to the handler. This will force the
750          * library/code containing the handler to remain loaded until we run the
751          * atexit handler. If -znodelete has been used then this is
752          * unnecessary.
753          */
754         {
755             DSO *dso = NULL;
756
757             ERR_set_mark();
758             dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
759 #  ifdef OPENSSL_INIT_DEBUG
760             fprintf(stderr,
761                     "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
762                     (dso == NULL ? "No!" : "Yes."));
763             /* See same code above in ossl_init_base() for an explanation. */
764 #  endif
765             DSO_free(dso);
766             ERR_pop_to_mark();
767         }
768 # endif
769     }
770 #endif
771
772     if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
773         CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
774         return 0;
775     }
776
777     newhand->handler = handler;
778     newhand->next = stop_handlers;
779     stop_handlers = newhand;
780
781     return 1;
782 }
783
784 #ifdef OPENSSL_SYS_UNIX
785 /*
786  * The following three functions are for OpenSSL developers.  This is
787  * where we set/reset state across fork (called via pthread_atfork when
788  * it exists, or manually by the application when it doesn't).
789  *
790  * WARNING!  If you put code in either OPENSSL_fork_parent or
791  * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
792  * safe.  See this link, for example:
793  *      http://man7.org/linux/man-pages/man7/signal-safety.7.html
794  */
795
796 void OPENSSL_fork_prepare(void)
797 {
798 }
799
800 void OPENSSL_fork_parent(void)
801 {
802 }
803
804 void OPENSSL_fork_child(void)
805 {
806     rand_fork();
807 }
808 #endif