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