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