Coverage Report

Created: 2026-09-12 06:55

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