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