f20a12f069aa14f08bb5c5968d213efad6aa873b
[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_load_crypto_strings)
181 {
182     int ret = 1;
183     /*
184      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
185      * pulling in all the error strings during static linking
186      */
187 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
188 # ifdef OPENSSL_INIT_DEBUG
189     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
190                     "err_load_crypto_strings_int()\n");
191 # endif
192     ret = err_load_crypto_strings_int();
193     load_crypto_strings_inited = 1;
194 #endif
195     return ret;
196 }
197
198 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
199                            ossl_init_load_crypto_strings)
200 {
201     /* Do nothing in this case */
202     return 1;
203 }
204
205 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
206 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
207 {
208     /*
209      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
210      * pulling in all the ciphers during static linking
211      */
212 #ifndef OPENSSL_NO_AUTOALGINIT
213 # ifdef OPENSSL_INIT_DEBUG
214     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
215                     "openssl_add_all_ciphers_int()\n");
216 # endif
217     openssl_add_all_ciphers_int();
218 #endif
219     return 1;
220 }
221
222 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
223                            ossl_init_add_all_ciphers)
224 {
225     /* Do nothing */
226     return 1;
227 }
228
229 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
230 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
231 {
232     /*
233      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
234      * pulling in all the ciphers during static linking
235      */
236 #ifndef OPENSSL_NO_AUTOALGINIT
237 # ifdef OPENSSL_INIT_DEBUG
238     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
239                     "openssl_add_all_digests()\n");
240 # endif
241     openssl_add_all_digests_int();
242 #endif
243     return 1;
244 }
245
246 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
247                            ossl_init_add_all_digests)
248 {
249     /* Do nothing */
250     return 1;
251 }
252
253 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
254 static int config_inited = 0;
255 static const char *appname;
256 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
257 {
258 #ifdef OPENSSL_INIT_DEBUG
259     fprintf(stderr,
260             "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
261             appname == NULL ? "NULL" : appname);
262 #endif
263     openssl_config_int(appname);
264     config_inited = 1;
265     return 1;
266 }
267 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
268 {
269 #ifdef OPENSSL_INIT_DEBUG
270     fprintf(stderr,
271             "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
272 #endif
273     openssl_no_config_int();
274     config_inited = 1;
275     return 1;
276 }
277
278 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
279 static int async_inited = 0;
280 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
281 {
282 #ifdef OPENSSL_INIT_DEBUG
283     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
284 #endif
285     if (!async_init())
286         return 0;
287     async_inited = 1;
288     return 1;
289 }
290
291 #ifndef OPENSSL_NO_ENGINE
292 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
293 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
294 {
295 # ifdef OPENSSL_INIT_DEBUG
296     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
297                     "engine_load_openssl_int()\n");
298 # endif
299     engine_load_openssl_int();
300     return 1;
301 }
302 # ifndef OPENSSL_NO_DEVCRYPTOENG
303 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
304 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
305 {
306 #  ifdef OPENSSL_INIT_DEBUG
307     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
308                     "engine_load_devcrypto_int()\n");
309 #  endif
310     engine_load_devcrypto_int();
311     return 1;
312 }
313 # endif
314
315 # ifndef OPENSSL_NO_RDRAND
316 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
317 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
318 {
319 #  ifdef OPENSSL_INIT_DEBUG
320     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
321                     "engine_load_rdrand_int()\n");
322 #  endif
323     engine_load_rdrand_int();
324     return 1;
325 }
326 # endif
327 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
328 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
329 {
330 # ifdef OPENSSL_INIT_DEBUG
331     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
332                     "engine_load_dynamic_int()\n");
333 # endif
334     engine_load_dynamic_int();
335     return 1;
336 }
337 # ifndef OPENSSL_NO_STATIC_ENGINE
338 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
339 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
340 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
341 {
342 #   ifdef OPENSSL_INIT_DEBUG
343     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
344                     "engine_load_padlock_int()\n");
345 #   endif
346     engine_load_padlock_int();
347     return 1;
348 }
349 #  endif
350 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
351 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
352 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
353 {
354 #   ifdef OPENSSL_INIT_DEBUG
355     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
356                     "engine_load_capi_int()\n");
357 #   endif
358     engine_load_capi_int();
359     return 1;
360 }
361 #  endif
362 #  if !defined(OPENSSL_NO_AFALGENG)
363 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
364 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
365 {
366 #   ifdef OPENSSL_INIT_DEBUG
367     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
368                     "engine_load_afalg_int()\n");
369 #   endif
370     engine_load_afalg_int();
371     return 1;
372 }
373 #  endif
374 # endif
375 #endif
376
377 #ifndef OPENSSL_NO_COMP
378 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
379
380 static int zlib_inited = 0;
381 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
382 {
383     /* Do nothing - we need to know about this for the later cleanup */
384     zlib_inited = 1;
385     return 1;
386 }
387 #endif
388
389 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
390 {
391     /* Can't do much about this */
392     if (locals == NULL)
393         return;
394
395     if (locals->async) {
396 #ifdef OPENSSL_INIT_DEBUG
397         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
398                         "async_delete_thread_state()\n");
399 #endif
400         async_delete_thread_state();
401     }
402
403     if (locals->err_state) {
404 #ifdef OPENSSL_INIT_DEBUG
405         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
406                         "err_delete_thread_state()\n");
407 #endif
408         err_delete_thread_state();
409     }
410
411     if (locals->rand) {
412 #ifdef OPENSSL_INIT_DEBUG
413         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
414                         "drbg_delete_thread_state()\n");
415 #endif
416         drbg_delete_thread_state();
417     }
418
419     OPENSSL_free(locals);
420 }
421
422 void OPENSSL_thread_stop(void)
423 {
424     if (destructor_key.sane != -1)
425         ossl_init_thread_stop(ossl_init_get_thread_local(0));
426 }
427
428 int ossl_init_thread_start(uint64_t opts)
429 {
430     struct thread_local_inits_st *locals;
431
432     if (!OPENSSL_init_crypto(0, NULL))
433         return 0;
434
435     locals = ossl_init_get_thread_local(1);
436
437     if (locals == NULL)
438         return 0;
439
440     if (opts & OPENSSL_INIT_THREAD_ASYNC) {
441 #ifdef OPENSSL_INIT_DEBUG
442         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
443                         "marking thread for async\n");
444 #endif
445         locals->async = 1;
446     }
447
448     if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
449 #ifdef OPENSSL_INIT_DEBUG
450         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
451                         "marking thread for err_state\n");
452 #endif
453         locals->err_state = 1;
454     }
455
456     if (opts & OPENSSL_INIT_THREAD_RAND) {
457 #ifdef OPENSSL_INIT_DEBUG
458         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
459                         "marking thread for rand\n");
460 #endif
461         locals->rand = 1;
462     }
463
464     return 1;
465 }
466
467 void OPENSSL_cleanup(void)
468 {
469     OPENSSL_INIT_STOP *currhandler, *lasthandler;
470     CRYPTO_THREAD_LOCAL key;
471
472     /* If we've not been inited then no need to deinit */
473     if (!base_inited)
474         return;
475
476     /* Might be explicitly called and also by atexit */
477     if (stopped)
478         return;
479     stopped = 1;
480
481     /*
482      * Thread stop may not get automatically called by the thread library for
483      * the very last thread in some situations, so call it directly.
484      */
485     ossl_init_thread_stop(ossl_init_get_thread_local(0));
486
487     currhandler = stop_handlers;
488     while (currhandler != NULL) {
489         currhandler->handler();
490         lasthandler = currhandler;
491         currhandler = currhandler->next;
492         OPENSSL_free(lasthandler);
493     }
494     stop_handlers = NULL;
495
496     CRYPTO_THREAD_lock_free(init_lock);
497     init_lock = NULL;
498
499     /*
500      * We assume we are single-threaded for this function, i.e. no race
501      * conditions for the various "*_inited" vars below.
502      */
503
504 #ifndef OPENSSL_NO_COMP
505     if (zlib_inited) {
506 #ifdef OPENSSL_INIT_DEBUG
507         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
508                         "comp_zlib_cleanup_int()\n");
509 #endif
510         comp_zlib_cleanup_int();
511     }
512 #endif
513
514     if (async_inited) {
515 # ifdef OPENSSL_INIT_DEBUG
516         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
517                         "async_deinit()\n");
518 # endif
519         async_deinit();
520     }
521
522     if (load_crypto_strings_inited) {
523 #ifdef OPENSSL_INIT_DEBUG
524         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
525                         "err_free_strings_int()\n");
526 #endif
527         err_free_strings_int();
528     }
529
530     key = destructor_key.value;
531     destructor_key.sane = -1;
532     CRYPTO_THREAD_cleanup_local(&key);
533
534 #ifdef OPENSSL_INIT_DEBUG
535     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
536                     "rand_cleanup_int()\n");
537     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
538                     "conf_modules_free_int()\n");
539 #ifndef OPENSSL_NO_ENGINE
540     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
541                     "engine_cleanup_int()\n");
542 #endif
543     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
544                     "crypto_cleanup_all_ex_data_int()\n");
545     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
546                     "bio_sock_cleanup_int()\n");
547     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
548                     "bio_cleanup()\n");
549     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
550                     "evp_cleanup_int()\n");
551     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
552                     "obj_cleanup_int()\n");
553     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
554                     "err_cleanup()\n");
555 #endif
556     /*
557      * Note that cleanup order is important:
558      * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
559      * must be called before engine_cleanup_int()
560      * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
561      * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
562      * - conf_modules_free_int() can end up in ENGINE code so must be called
563      * before engine_cleanup_int()
564      * - ENGINEs and additional EVP algorithms might use added OIDs names so
565      * obj_cleanup_int() must be called last
566      */
567     rand_cleanup_int();
568     rand_drbg_cleanup_int();
569     conf_modules_free_int();
570 #ifndef OPENSSL_NO_ENGINE
571     engine_cleanup_int();
572 #endif
573     ossl_store_cleanup_int();
574     crypto_cleanup_all_ex_data_int();
575     bio_cleanup();
576     evp_cleanup_int();
577     obj_cleanup_int();
578     err_cleanup();
579
580     CRYPTO_secure_malloc_done();
581
582     base_inited = 0;
583 }
584
585 /*
586  * If this function is called with a non NULL settings value then it must be
587  * called prior to any threads making calls to any OpenSSL functions,
588  * i.e. passing a non-null settings value is assumed to be single-threaded.
589  */
590 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
591 {
592     if (stopped) {
593         if (!(opts & OPENSSL_INIT_BASE_ONLY))
594             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
595         return 0;
596     }
597
598     if (!RUN_ONCE(&base, ossl_init_base))
599         return 0;
600
601     if (!(opts & OPENSSL_INIT_BASE_ONLY)
602             && !RUN_ONCE(&load_crypto_nodelete,
603                          ossl_init_load_crypto_nodelete))
604         return 0;
605
606     if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
607             && !RUN_ONCE_ALT(&load_crypto_strings,
608                              ossl_init_no_load_crypto_strings,
609                              ossl_init_load_crypto_strings))
610         return 0;
611
612     if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
613             && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
614         return 0;
615
616     if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
617             && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
618                              ossl_init_add_all_ciphers))
619         return 0;
620
621     if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
622             && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
623         return 0;
624
625     if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
626             && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
627                              ossl_init_add_all_digests))
628         return 0;
629
630     if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
631             && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
632         return 0;
633
634     if ((opts & OPENSSL_INIT_ATFORK)
635             && !openssl_init_fork_handlers())
636         return 0;
637
638     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
639             && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
640         return 0;
641
642     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
643         int ret;
644         CRYPTO_THREAD_write_lock(init_lock);
645         appname = (settings == NULL) ? NULL : settings->appname;
646         ret = RUN_ONCE(&config, ossl_init_config);
647         CRYPTO_THREAD_unlock(init_lock);
648         if (!ret)
649             return 0;
650     }
651
652     if ((opts & OPENSSL_INIT_ASYNC)
653             && !RUN_ONCE(&async, ossl_init_async))
654         return 0;
655
656 #ifndef OPENSSL_NO_ENGINE
657     if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
658             && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
659         return 0;
660 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
661     if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
662             && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
663         return 0;
664 # endif
665 # ifndef OPENSSL_NO_RDRAND
666     if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
667             && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
668         return 0;
669 # endif
670     if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
671             && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
672         return 0;
673 # ifndef OPENSSL_NO_STATIC_ENGINE
674 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
675     if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
676             && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
677         return 0;
678 #  endif
679 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
680     if ((opts & OPENSSL_INIT_ENGINE_CAPI)
681             && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
682         return 0;
683 #  endif
684 #  if !defined(OPENSSL_NO_AFALGENG)
685     if ((opts & OPENSSL_INIT_ENGINE_AFALG)
686             && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
687         return 0;
688 #  endif
689 # endif
690     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
691                 | OPENSSL_INIT_ENGINE_OPENSSL
692                 | OPENSSL_INIT_ENGINE_AFALG)) {
693         ENGINE_register_all_complete();
694     }
695 #endif
696
697 #ifndef OPENSSL_NO_COMP
698     if ((opts & OPENSSL_INIT_ZLIB)
699             && !RUN_ONCE(&zlib, ossl_init_zlib))
700         return 0;
701 #endif
702
703     return 1;
704 }
705
706 int OPENSSL_atexit(void (*handler)(void))
707 {
708     OPENSSL_INIT_STOP *newhand;
709
710 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
711     {
712         union {
713             void *sym;
714             void (*func)(void);
715         } handlersym;
716
717         handlersym.func = handler;
718 # ifdef DSO_WIN32
719         {
720             HMODULE handle = NULL;
721             BOOL ret;
722
723             /*
724              * We don't use the DSO route for WIN32 because there is a better
725              * way
726              */
727             ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
728                                     | GET_MODULE_HANDLE_EX_FLAG_PIN,
729                                     handlersym.sym, &handle);
730
731             if (!ret)
732                 return 0;
733         }
734 # else
735         /*
736          * Deliberately leak a reference to the handler. This will force the
737          * library/code containing the handler to remain loaded until we run the
738          * atexit handler. If -znodelete has been used then this is
739          * unnecessary.
740          */
741         {
742             DSO *dso = NULL;
743
744             ERR_set_mark();
745             dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
746 #  ifdef OPENSSL_INIT_DEBUG
747             fprintf(stderr,
748                     "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
749                     (dso == NULL ? "No!" : "Yes."));
750             /* See same code above in ossl_init_base() for an explanation. */
751 #  endif
752             DSO_free(dso);
753             ERR_pop_to_mark();
754         }
755 # endif
756     }
757 #endif
758
759     if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
760         CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
761         return 0;
762     }
763
764     newhand->handler = handler;
765     newhand->next = stop_handlers;
766     stop_handlers = newhand;
767
768     return 1;
769 }
770
771 #ifdef OPENSSL_SYS_UNIX
772 /*
773  * The following three functions are for OpenSSL developers.  This is
774  * where we set/reset state across fork (called via pthread_atfork when
775  * it exists, or manually by the application when it doesn't).
776  *
777  * WARNING!  If you put code in either OPENSSL_fork_parent or
778  * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
779  * safe.  See this link, for example:
780  *      http://man7.org/linux/man-pages/man7/signal-safety.7.html
781  */
782
783 void OPENSSL_fork_prepare(void)
784 {
785 }
786
787 void OPENSSL_fork_parent(void)
788 {
789 }
790
791 void OPENSSL_fork_child(void)
792 {
793     rand_fork();
794 }
795 #endif