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