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