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