Coverage Report

Created: 2025-12-08 06:22

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