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