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