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