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