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