Don't auto-instantiate a DRBG when trying to use it and it's not
[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     rand_cleanup_drbg_int();
492     conf_modules_free_int();
493 #ifndef OPENSSL_NO_ENGINE
494     engine_cleanup_int();
495 #endif
496     ossl_store_cleanup_int();
497     crypto_cleanup_all_ex_data_int();
498     bio_cleanup();
499     evp_cleanup_int();
500     obj_cleanup_int();
501     err_cleanup();
502
503     base_inited = 0;
504 }
505
506 /*
507  * If this function is called with a non NULL settings value then it must be
508  * called prior to any threads making calls to any OpenSSL functions,
509  * i.e. passing a non-null settings value is assumed to be single-threaded.
510  */
511 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
512 {
513     static int stoperrset = 0;
514
515     if (stopped) {
516         if (!stoperrset) {
517             /*
518              * We only ever set this once to avoid getting into an infinite
519              * loop where the error system keeps trying to init and fails so
520              * sets an error etc
521              */
522             stoperrset = 1;
523             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
524         }
525         return 0;
526     }
527
528     if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
529         return 0;
530
531     if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
532             && !RUN_ONCE(&load_crypto_strings,
533                          ossl_init_no_load_crypto_strings))
534         return 0;
535
536     if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
537             && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
538         return 0;
539
540     if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
541             && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
542         return 0;
543
544     if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
545             && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
546         return 0;
547
548     if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
549             && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
550         return 0;
551
552     if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
553             && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
554         return 0;
555
556     if ((opts & OPENSSL_INIT_ATFORK)
557             && !openssl_init_fork_handlers())
558         return 0;
559
560     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
561             && !RUN_ONCE(&config, ossl_init_no_config))
562         return 0;
563
564     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
565         int ret;
566         CRYPTO_THREAD_write_lock(init_lock);
567         appname = (settings == NULL) ? NULL : settings->appname;
568         ret = RUN_ONCE(&config, ossl_init_config);
569         CRYPTO_THREAD_unlock(init_lock);
570         if (!ret)
571             return 0;
572     }
573
574     if ((opts & OPENSSL_INIT_ASYNC)
575             && !RUN_ONCE(&async, ossl_init_async))
576         return 0;
577
578 #ifndef OPENSSL_NO_ENGINE
579     if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
580             && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
581         return 0;
582 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
583     if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
584             && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
585         return 0;
586 # endif
587 # ifndef OPENSSL_NO_RDRAND
588     if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
589             && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
590         return 0;
591 # endif
592     if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
593             && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
594         return 0;
595 # ifndef OPENSSL_NO_STATIC_ENGINE
596 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
597     if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
598             && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
599         return 0;
600 #  endif
601 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
602     if ((opts & OPENSSL_INIT_ENGINE_CAPI)
603             && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
604         return 0;
605 #  endif
606 #  if !defined(OPENSSL_NO_AFALGENG)
607     if ((opts & OPENSSL_INIT_ENGINE_AFALG)
608             && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
609         return 0;
610 #  endif
611 # endif
612     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
613                 | OPENSSL_INIT_ENGINE_OPENSSL
614                 | OPENSSL_INIT_ENGINE_AFALG)) {
615         ENGINE_register_all_complete();
616     }
617 #endif
618
619 #ifndef OPENSSL_NO_COMP
620     if ((opts & OPENSSL_INIT_ZLIB)
621             && !RUN_ONCE(&zlib, ossl_init_zlib))
622         return 0;
623 #endif
624
625     return 1;
626 }
627
628 int OPENSSL_atexit(void (*handler)(void))
629 {
630     OPENSSL_INIT_STOP *newhand;
631
632 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
633     {
634         union {
635             void *sym;
636             void (*func)(void);
637         } handlersym;
638
639         handlersym.func = handler;
640 # ifdef DSO_WIN32
641         {
642             HMODULE handle = NULL;
643             BOOL ret;
644
645             /*
646              * We don't use the DSO route for WIN32 because there is a better
647              * way
648              */
649             ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
650                                     | GET_MODULE_HANDLE_EX_FLAG_PIN,
651                                     handlersym.sym, &handle);
652
653             if (!ret)
654                 return 0;
655         }
656 # else
657         /*
658          * Deliberately leak a reference to the handler. This will force the
659          * library/code containing the handler to remain loaded until we run the
660          * atexit handler. If -znodelete has been used then this is
661          * unnecessary.
662          */
663         {
664             DSO *dso = NULL;
665
666             ERR_set_mark();
667             dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
668             DSO_free(dso);
669             ERR_pop_to_mark();
670         }
671 # endif
672     }
673 #endif
674
675     newhand = OPENSSL_malloc(sizeof(*newhand));
676     if (newhand == NULL)
677         return 0;
678
679     newhand->handler = handler;
680     newhand->next = stop_handlers;
681     stop_handlers = newhand;
682
683     return 1;
684 }
685
686 #ifdef OPENSSL_SYS_UNIX
687 /*
688  * The following three functions are for OpenSSL developers.  This is
689  * where we set/reset state across fork (called via pthread_atfork when
690  * it exists, or manually by the application when it doesn't).
691  *
692  * WARNING!  If you put code in either OPENSSL_fork_parent or
693  * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
694  * safe.  See this link, for example:
695  *      http://man7.org/linux/man-pages/man7/signal-safety.7.html
696  */
697
698 void OPENSSL_fork_prepare(void)
699 {
700 }
701
702 void OPENSSL_fork_parent(void)
703 {
704 }
705
706 void OPENSSL_fork_child(void)
707 {
708     rand_fork();
709 }
710 #endif