Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
/* 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
275
{
59
    /* no need to init trace */
60
61
275
    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
275
    if ((optsdone_lock = CRYPTO_THREAD_lock_new()) == NULL
67
275
        || (init_lock = CRYPTO_THREAD_lock_new()) == NULL)
68
0
        goto err;
69
70
275
    OPENSSL_cpuid_setup();
71
72
275
    if (!ossl_init_thread())
73
0
        goto err;
74
75
275
    if (!CRYPTO_THREAD_init_local(&in_init_config_local, NULL))
76
0
        goto err;
77
78
275
    base_inited = 1;
79
275
    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
275
}
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
220
{
102
220
#ifndef OPENSSL_NO_ATEXIT
103
#ifdef OPENSSL_INIT_DEBUG
104
    fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n");
105
#endif
106
220
#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
220
    if (atexit(OPENSSL_cleanup) != 0)
113
0
        return 0;
114
220
#endif
115
220
#endif
116
220
#endif
117
118
220
    return 1;
119
220
}
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
220
{
134
220
    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
220
    return 1;
181
220
}
182
183
static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
184
185
DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
186
251
{
187
251
    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
251
#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
193
251
    void *err;
194
195
251
    if (!err_shelve_state(&err))
196
0
        return 0;
197
198
251
    OSSL_TRACE(INIT, "ossl_err_load_crypto_strings()\n");
199
251
    ret = ossl_err_load_crypto_strings();
200
201
251
    err_unshelve_state(err);
202
251
#endif
203
251
    return ret;
204
251
}
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
78
{
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
78
#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
222
78
    OSSL_TRACE(INIT, "ossl_init_load_ssl_strings: ossl_err_load_SSL_strings()\n");
223
78
    ossl_err_load_SSL_strings();
224
78
#endif
225
78
    return 1;
226
78
}
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
165
{
238
    /*
239
     * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
240
     * pulling in all the ciphers during static linking
241
     */
242
165
#ifndef OPENSSL_NO_AUTOALGINIT
243
165
    OSSL_TRACE(INIT, "openssl_add_all_ciphers_int()\n");
244
165
    openssl_add_all_ciphers_int();
245
165
#endif
246
165
    return 1;
247
165
}
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
165
{
259
    /*
260
     * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
261
     * pulling in all the ciphers during static linking
262
     */
263
165
#ifndef OPENSSL_NO_AUTOALGINIT
264
165
    OSSL_TRACE(INIT, "openssl_add_all_digests()\n");
265
165
    openssl_add_all_digests_int();
266
165
#endif
267
165
    return 1;
268
165
}
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
186
{
281
186
    int ret = ossl_config_int(NULL);
282
283
186
    return ret;
284
186
}
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
90
{
303
90
    OSSL_TRACE(INIT, "async_init()\n");
304
90
    if (!async_init())
305
0
        return 0;
306
90
    async_inited = 1;
307
90
    return 1;
308
90
}
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
220
{
376
220
    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
220
    if (!base_inited)
385
0
        return;
386
387
    /* Might be explicitly called and also by atexit */
388
220
    if (stopped)
389
0
        return;
390
220
    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
220
    OPENSSL_thread_stop();
397
398
220
    currhandler = stop_handlers;
399
244
    while (currhandler != NULL) {
400
24
        currhandler->handler();
401
24
        lasthandler = currhandler;
402
24
        currhandler = currhandler->next;
403
24
        OPENSSL_free(lasthandler);
404
24
    }
405
220
    stop_handlers = NULL;
406
407
220
    CRYPTO_THREAD_lock_free(optsdone_lock);
408
220
    optsdone_lock = NULL;
409
220
    CRYPTO_THREAD_lock_free(init_lock);
410
220
    init_lock = NULL;
411
412
220
    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
220
#ifndef OPENSSL_NO_COMP
420
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zlib_cleanup()\n");
421
220
    ossl_comp_zlib_cleanup();
422
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_brotli_cleanup()\n");
423
220
    ossl_comp_brotli_cleanup();
424
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zstd_cleanup()\n");
425
220
    ossl_comp_zstd_cleanup();
426
220
#endif
427
428
220
    if (async_inited) {
429
72
        OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
430
72
        async_deinit();
431
72
    }
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
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_rand_cleanup_int()\n");
445
220
    ossl_rand_cleanup_int();
446
447
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_config_modules_free()\n");
448
220
    ossl_config_modules_free();
449
450
220
#ifndef OPENSSL_NO_ENGINE
451
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
452
220
    engine_cleanup_int();
453
220
#endif
454
455
220
#ifndef OPENSSL_NO_DEPRECATED_3_0
456
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
457
220
    ossl_store_cleanup_int();
458
220
#endif
459
460
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_lib_ctx_default_deinit()\n");
461
220
    ossl_lib_ctx_default_deinit();
462
463
220
    ossl_cleanup_thread();
464
465
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
466
220
    bio_cleanup();
467
468
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
469
220
    evp_cleanup_int();
470
471
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_obj_cleanup_int()\n");
472
220
    ossl_obj_cleanup_int();
473
474
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
475
220
    err_cleanup();
476
477
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
478
220
    CRYPTO_secure_malloc_done();
479
480
220
#ifndef OPENSSL_NO_CMP
481
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n");
482
220
    OSSL_CMP_log_close();
483
220
#endif
484
485
220
    OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
486
220
    ossl_trace_cleanup();
487
488
220
    base_inited = 0;
489
220
}
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
1.16G
{
498
1.16G
    uint64_t tmp;
499
1.16G
    int aloaddone = 0;
500
501
    /* Applications depend on 0 being returned when cleanup was already done */
502
1.16G
    if (stopped) {
503
10
        if (!(opts & OPENSSL_INIT_BASE_ONLY))
504
10
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL);
505
10
        return 0;
506
10
    }
507
508
    /*
509
     * We ignore failures from this function. It is probably because we are
510
     * on a platform that doesn't support lockless atomic loads (we may not
511
     * have created optsdone_lock yet so we can't use it). This is just an
512
     * optimisation to skip the full checks in this function if we don't need
513
     * to, so we carry on regardless in the event of failure.
514
     *
515
     * There could be a race here with other threads, so that optsdone has not
516
     * been updated yet, even though the options have in fact been initialised.
517
     * This doesn't matter - it just means we will run the full function
518
     * unnecessarily - but all the critical code is contained in RUN_ONCE
519
     * functions anyway so we are safe.
520
     */
521
1.16G
    if (CRYPTO_atomic_load(&optsdone, &tmp, NULL)) {
522
1.16G
        if ((tmp & opts) == opts)
523
450M
            return 1;
524
719M
        aloaddone = 1;
525
719M
    }
526
527
    /*
528
     * At some point we should look at this function with a view to moving
529
     * most/all of this into OSSL_LIB_CTX.
530
     *
531
     * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
532
     * *only* option specified.  With that option we return immediately after
533
     * doing the requested limited initialization.  Note that
534
     * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
535
     * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
536
     * base already initialized this is a harmless NOOP.
537
     *
538
     * If we remain the only caller of err_shelve_state() the recursion should
539
     * perhaps be removed, but if in doubt, it can be left in place.
540
     */
541
719M
    if (!RUN_ONCE(&base, ossl_init_base))
542
0
        return 0;
543
544
719M
    if (opts & OPENSSL_INIT_BASE_ONLY)
545
719M
        return 1;
546
547
    /*
548
     * optsdone_lock should definitely be set up now, so we can now repeat the
549
     * same check from above but be sure that it will work even on platforms
550
     * without lockless CRYPTO_atomic_load
551
     */
552
265
    if (!aloaddone) {
553
0
        if (!CRYPTO_atomic_load(&optsdone, &tmp, optsdone_lock))
554
0
            return 0;
555
0
        if ((tmp & opts) == opts)
556
0
            return 1;
557
0
    }
558
559
    /*
560
     * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
561
     * should not have the side-effect of setting up exit handlers, and
562
     * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
563
     * return above.
564
     */
565
265
    if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
566
0
        if (!RUN_ONCE_ALT(&register_atexit, ossl_init_no_register_atexit,
567
0
                ossl_init_register_atexit))
568
0
            return 0;
569
265
    } else if (!RUN_ONCE(&register_atexit, ossl_init_register_atexit)) {
570
0
        return 0;
571
0
    }
572
573
265
    if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
574
0
        return 0;
575
576
265
    if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
577
0
        && !RUN_ONCE_ALT(&load_crypto_strings,
578
265
            ossl_init_no_load_crypto_strings,
579
265
            ossl_init_load_crypto_strings))
580
0
        return 0;
581
582
265
    if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
583
143
        && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
584
0
        return 0;
585
586
265
    if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
587
0
        && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings,
588
265
            ossl_init_load_ssl_strings))
589
0
        return 0;
590
591
265
    if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
592
38
        && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
593
0
        return 0;
594
595
265
    if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
596
0
        && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
597
265
            ossl_init_add_all_ciphers))
598
0
        return 0;
599
600
265
    if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
601
61
        && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
602
0
        return 0;
603
604
265
    if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
605
0
        && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
606
265
            ossl_init_add_all_digests))
607
0
        return 0;
608
609
265
    if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
610
61
        && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
611
0
        return 0;
612
613
265
    if ((opts & OPENSSL_INIT_ATFORK)
614
0
        && !openssl_init_fork_handlers())
615
0
        return 0;
616
617
265
    if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
618
0
        && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
619
0
        return 0;
620
621
265
    if (opts & OPENSSL_INIT_LOAD_CONFIG) {
622
107
        int loading = CRYPTO_THREAD_get_local(&in_init_config_local) != NULL;
623
624
        /* If called recursively from OBJ_ calls, just skip it. */
625
107
        if (!loading) {
626
69
            int ret;
627
628
69
            if (!CRYPTO_THREAD_set_local(&in_init_config_local, (void *)-1))
629
0
                return 0;
630
69
            if (settings == NULL) {
631
69
                ret = RUN_ONCE(&config, ossl_init_config);
632
69
            } else {
633
0
                if (!CRYPTO_THREAD_write_lock(init_lock))
634
0
                    return 0;
635
0
                conf_settings = settings;
636
0
                ret = RUN_ONCE_ALT(&config, ossl_init_config_settings,
637
0
                    ossl_init_config);
638
0
                conf_settings = NULL;
639
0
                CRYPTO_THREAD_unlock(init_lock);
640
0
            }
641
642
69
            if (ret <= 0)
643
0
                return 0;
644
69
        }
645
107
    }
646
647
265
    if ((opts & OPENSSL_INIT_ASYNC)
648
34
        && !RUN_ONCE(&async, ossl_init_async))
649
0
        return 0;
650
651
265
#ifndef OPENSSL_NO_ENGINE
652
265
    if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
653
0
        && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
654
0
        return 0;
655
265
#ifndef OPENSSL_NO_RDRAND
656
265
    if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
657
0
        && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
658
0
        return 0;
659
265
#endif
660
265
    if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
661
0
        && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
662
0
        return 0;
663
265
#ifndef OPENSSL_NO_STATIC_ENGINE
664
#ifndef OPENSSL_NO_DEVCRYPTOENG
665
    if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
666
        && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
667
        return 0;
668
#endif
669
265
#if !defined(OPENSSL_NO_PADLOCKENG)
670
265
    if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
671
0
        && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
672
0
        return 0;
673
265
#endif
674
#if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
675
    if ((opts & OPENSSL_INIT_ENGINE_CAPI)
676
        && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
677
        return 0;
678
#endif
679
265
#if !defined(OPENSSL_NO_AFALGENG)
680
265
    if ((opts & OPENSSL_INIT_ENGINE_AFALG)
681
0
        && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
682
0
        return 0;
683
265
#endif
684
265
#endif
685
265
    if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN | OPENSSL_INIT_ENGINE_OPENSSL | OPENSSL_INIT_ENGINE_AFALG)) {
686
0
        ENGINE_register_all_complete();
687
0
    }
688
265
#endif
689
690
265
    if (!CRYPTO_atomic_or(&optsdone, opts, &tmp, optsdone_lock))
691
0
        return 0;
692
693
265
    return 1;
694
265
}
695
696
int OPENSSL_atexit(void (*handler)(void))
697
24
{
698
24
    OPENSSL_INIT_STOP *newhand;
699
700
#if !defined(OPENSSL_USE_NODELETE) \
701
    && !defined(OPENSSL_NO_PINSHARED)
702
    {
703
#if defined(DSO_WIN32) && !defined(_WIN32_WCE)
704
        HMODULE handle = NULL;
705
        BOOL ret;
706
        union {
707
            void *sym;
708
            void (*func)(void);
709
        } handlersym;
710
711
        handlersym.func = handler;
712
713
        /*
714
         * We don't use the DSO route for WIN32 because there is a better
715
         * way
716
         */
717
        ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
718
                | GET_MODULE_HANDLE_EX_FLAG_PIN,
719
            handlersym.sym, &handle);
720
721
        if (!ret)
722
            return 0;
723
#elif !defined(DSO_NONE)
724
        /*
725
         * Deliberately leak a reference to the handler. This will force the
726
         * library/code containing the handler to remain loaded until we run the
727
         * atexit handler. If -znodelete has been used then this is
728
         * unnecessary.
729
         */
730
        DSO *dso = NULL;
731
        union {
732
            void *sym;
733
            void (*func)(void);
734
        } handlersym;
735
736
        handlersym.func = handler;
737
738
        ERR_set_mark();
739
        dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
740
        /* See same code above in ossl_init_base() for an explanation. */
741
        OSSL_TRACE1(INIT,
742
            "atexit: obtained DSO reference? %s\n",
743
            (dso == NULL ? "No!" : "Yes."));
744
        DSO_free(dso);
745
        ERR_pop_to_mark();
746
#endif
747
    }
748
#endif
749
750
24
    if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL)
751
0
        return 0;
752
753
24
    newhand->handler = handler;
754
24
    newhand->next = stop_handlers;
755
24
    stop_handlers = newhand;
756
757
24
    return 1;
758
24
}