Coverage Report

Created: 2025-12-31 06:58

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