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