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