init.c 17.5 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9 10 11
 */

#include <internal/cryptlib_int.h>
#include <openssl/err.h>
12
#include <internal/rand.h>
13
#include <internal/bio.h>
14 15 16 17 18
#include <openssl/evp.h>
#include <internal/evp_int.h>
#include <internal/conf.h>
#include <internal/async.h>
#include <internal/engine.h>
19
#include <internal/comp.h>
20
#include <internal/err.h>
21
#include <internal/err_int.h>
22
#include <internal/objects.h>
23
#include <stdlib.h>
R
Rich Salz 已提交
24 25 26
#include <assert.h>

static int stopped = 0;
27

M
Matt Caswell 已提交
28 29
static void ossl_init_thread_stop(struct thread_local_inits_st *locals);

30
static CRYPTO_THREAD_LOCAL threadstopkey;
31 32 33 34 35 36

static void ossl_init_thread_stop_wrap(void *local)
{
    ossl_init_thread_stop((struct thread_local_inits_st *)local);
}

37
static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
38
{
39 40
    struct thread_local_inits_st *local =
        CRYPTO_THREAD_get_local(&threadstopkey);
41 42 43

    if (local == NULL && alloc) {
        local = OPENSSL_zalloc(sizeof *local);
44
        CRYPTO_THREAD_set_local(&threadstopkey, local);
45
    }
46
    if (!alloc) {
47
        CRYPTO_THREAD_set_local(&threadstopkey, NULL);
48
    }
49 50 51 52

    return local;
}

R
Rich Salz 已提交
53
typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
54 55 56 57 58 59
struct ossl_init_stop_st {
    void (*handler)(void);
    OPENSSL_INIT_STOP *next;
};

static OPENSSL_INIT_STOP *stop_handlers = NULL;
60
static CRYPTO_RWLOCK *init_lock = NULL;
61

62
static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
63 64 65 66 67 68
static int base_inited = 0;
static void ossl_init_base(void)
{
#ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
#endif
69 70 71 72 73
    /*
     * We use a dummy thread local key here. We use the destructor to detect
     * when the thread is going to stop (where that feature is available)
     */
    CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
74
#ifndef OPENSSL_SYS_UEFI
75
    atexit(OPENSSL_cleanup);
76
#endif
77
    init_lock = CRYPTO_THREAD_lock_new();
78 79 80 81
    OPENSSL_cpuid_setup();
    base_inited = 1;
}

82
static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
83 84 85 86 87 88 89 90 91
static int load_crypto_strings_inited = 0;
static void ossl_init_no_load_crypto_strings(void)
{
    /* Do nothing in this case */
    return;
}

static void ossl_init_load_crypto_strings(void)
{
92 93 94 95 96
    /*
     * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
     * pulling in all the error strings during static linking
     */
#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
97 98
# ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
99
                    "err_load_crypto_strings_int()\n");
100
# endif
101
    err_load_crypto_strings_int();
102 103 104 105
#endif
    load_crypto_strings_inited = 1;
}

106
static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
107 108 109 110 111 112 113 114 115
static void ossl_init_add_all_ciphers(void)
{
    /*
     * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
     * pulling in all the ciphers during static linking
     */
#ifndef OPENSSL_NO_AUTOALGINIT
# ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
116
                    "openssl_add_all_ciphers_int()\n");
117
# endif
118
    openssl_add_all_ciphers_int();
119 120 121 122 123 124 125 126
# ifndef OPENSSL_NO_ENGINE
#  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
    ENGINE_setup_bsd_cryptodev();
#  endif
# endif
#endif
}

127
static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
128 129 130 131 132 133 134 135 136
static void ossl_init_add_all_digests(void)
{
    /*
     * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
     * pulling in all the ciphers during static linking
     */
#ifndef OPENSSL_NO_AUTOALGINIT
# ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
137
                    "openssl_add_all_digests()\n");
138
# endif
139
    openssl_add_all_digests_int();
140 141 142 143 144 145 146 147 148 149 150 151 152 153
# ifndef OPENSSL_NO_ENGINE
#  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
    ENGINE_setup_bsd_cryptodev();
#  endif
# endif
#endif
}

static void ossl_init_no_add_algs(void)
{
    /* Do nothing */
    return;
}

154
static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
155 156 157 158 159 160
static int config_inited = 0;
static const char *config_filename;
static void ossl_init_config(void)
{
#ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr,
161
            "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
162 163
            config_filename==NULL?"NULL":config_filename);
#endif
164
    openssl_config_int(config_filename);
165 166 167 168 169 170
    config_inited = 1;
}
static void ossl_init_no_config(void)
{
#ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr,
171
            "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
172
#endif
173
    openssl_no_config_int();
174 175 176
    config_inited = 1;
}

177
static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
178 179 180 181 182 183 184 185 186 187 188
static int async_inited = 0;
static void ossl_init_async(void)
{
#ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
#endif
    async_init();
    async_inited = 1;
}

#ifndef OPENSSL_NO_ENGINE
189
static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
190 191 192 193
static void ossl_init_engine_openssl(void)
{
# ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
194
                    "engine_load_openssl_int()\n");
195
# endif
196
    engine_load_openssl_int();
197 198 199
}
# if !defined(OPENSSL_NO_HW) && \
    (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
200
static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
201 202 203 204
static void ossl_init_engine_cryptodev(void)
{
#  ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
205
                    "engine_load_cryptodev_int()\n");
206
#  endif
207
    engine_load_cryptodev_int();
208 209 210 211
}
# endif

# ifndef OPENSSL_NO_RDRAND
212
static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
213 214 215 216
static void ossl_init_engine_rdrand(void)
{
#  ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
217
                    "engine_load_rdrand_int()\n");
218
#  endif
219
    engine_load_rdrand_int();
220 221
}
# endif
222
static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
223 224 225 226
static void ossl_init_engine_dynamic(void)
{
# ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
227
                    "engine_load_dynamic_int()\n");
228
# endif
229
    engine_load_dynamic_int();
230 231 232
}
# ifndef OPENSSL_NO_STATIC_ENGINE
#  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
233
static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
234 235 236 237
static void ossl_init_engine_padlock(void)
{
#   ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
238
                    "engine_load_padlock_int()\n");
239
#   endif
240
    engine_load_padlock_int();
241 242 243
}
#  endif
#  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
244
static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
245 246 247 248
static void ossl_init_engine_capi(void)
{
#   ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
249
                    "engine_load_capi_int()\n");
250
#   endif
251
    engine_load_capi_int();
252 253
}
#  endif
254
static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
255 256 257 258
static void ossl_init_engine_dasync(void)
{
# ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
259
                    "engine_load_dasync_int()\n");
260
# endif
261
    engine_load_dasync_int();
262
}
C
clucey 已提交
263
#  if !defined(OPENSSL_NO_AFALGENG)
R
Richard Levitte 已提交
264
static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
C
clucey 已提交
265 266 267 268
static void ossl_init_engine_afalg(void)
{
#   ifdef OPENSSL_INIT_DEBUG
    fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
269
                    "engine_load_afalg_int()\n");
C
clucey 已提交
270
#   endif
271
    engine_load_afalg_int();
C
clucey 已提交
272 273
}
#  endif
274 275 276
# endif
#endif

E
Emilia Kasper 已提交
277
#ifndef OPENSSL_NO_COMP
278
static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
E
Emilia Kasper 已提交
279

280 281 282 283 284 285
static int zlib_inited = 0;
static void ossl_init_zlib(void)
{
    /* Do nothing - we need to know about this for the later cleanup */
    zlib_inited = 1;
}
E
Emilia Kasper 已提交
286
#endif
287

M
Matt Caswell 已提交
288
static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
{
    /* Can't do much about this */
    if (locals == NULL)
        return;

    if (locals->async) {
#ifdef OPENSSL_INIT_DEBUG
        fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
                        "ASYNC_cleanup_thread()\n");
#endif
        ASYNC_cleanup_thread();
    }

    if (locals->err_state) {
#ifdef OPENSSL_INIT_DEBUG
        fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
305
                        "err_delete_thread_state()\n");
306
#endif
307
        err_delete_thread_state();
308 309 310 311 312
    }

    OPENSSL_free(locals);
}

313
void OPENSSL_thread_stop(void)
M
Matt Caswell 已提交
314 315 316 317 318
{
    ossl_init_thread_stop(
        (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
int ossl_init_thread_start(uint64_t opts)
{
    struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);

    if (locals == NULL)
        return 0;

    if (opts & OPENSSL_INIT_THREAD_ASYNC) {
#ifdef OPENSSL_INIT_DEBUG
        fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
                        "marking thread for async\n");
#endif
        locals->async = 1;
    }

    if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
#ifdef OPENSSL_INIT_DEBUG
        fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
                        "marking thread for err_state\n");
#endif
        locals->err_state = 1;
    }

    return 1;
}

345
void OPENSSL_cleanup(void)
346 347 348
{
    OPENSSL_INIT_STOP *currhandler, *lasthandler;

349 350 351 352
    /* If we've not been inited then no need to deinit */
    if (!base_inited)
        return;

R
Rich Salz 已提交
353 354 355 356 357
    /* Might be explicitly called and also by atexit */
    if (stopped)
        return;
    stopped = 1;

358 359 360 361 362 363 364 365 366 367 368 369 370 371
    /*
     * Thread stop may not get automatically called by the thread library for
     * the very last thread in some situations, so call it directly.
     */
    ossl_init_thread_stop(ossl_init_get_thread_local(0));

    currhandler = stop_handlers;
    while (currhandler != NULL) {
        currhandler->handler();
        lasthandler = currhandler;
        currhandler = currhandler->next;
        OPENSSL_free(lasthandler);
    }
    stop_handlers = NULL;
372 373 374

    CRYPTO_THREAD_lock_free(init_lock);

375 376 377 378 379
    /*
     * We assume we are single-threaded for this function, i.e. no race
     * conditions for the various "*_inited" vars below.
     */

E
Emilia Kasper 已提交
380
#ifndef OPENSSL_NO_COMP
381 382
    if (zlib_inited) {
#ifdef OPENSSL_INIT_DEBUG
383
        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
384
                        "comp_zlib_cleanup_int()\n");
385
#endif
386
        comp_zlib_cleanup_int();
387
    }
E
Emilia Kasper 已提交
388
#endif
389

M
Matt Caswell 已提交
390 391 392 393 394 395 396 397
    if (async_inited) {
# ifdef OPENSSL_INIT_DEBUG
        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
                        "async_deinit()\n");
# endif
        async_deinit();
    }

398 399
    if (load_crypto_strings_inited) {
#ifdef OPENSSL_INIT_DEBUG
400
        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
401
                        "err_free_strings_int()\n");
402
#endif
403
        err_free_strings_int();
404 405
    }

406
    CRYPTO_THREAD_cleanup_local(&threadstopkey);
M
Matt Caswell 已提交
407

408
#ifdef OPENSSL_INIT_DEBUG
M
Matt Caswell 已提交
409
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
410
                    "rand_cleanup_int()\n");
M
Matt Caswell 已提交
411
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
412
                    "conf_modules_free_int()\n");
413
#ifndef OPENSSL_NO_ENGINE
414
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
415
                    "engine_cleanup_int()\n");
416
#endif
M
Matt Caswell 已提交
417
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
418
                    "crypto_cleanup_all_ex_data_int()\n");
M
Matt Caswell 已提交
419
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
420
                    "bio_sock_cleanup_int()\n");
421 422
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
                    "bio_cleanup()\n");
M
Matt Caswell 已提交
423
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
424
                    "evp_cleanup_int()\n");
M
Matt Caswell 已提交
425
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
426
                    "obj_cleanup_int()\n");
427 428
    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
                    "err_cleanup()\n");
429
#endif
M
Matt Caswell 已提交
430 431
    /*
     * Note that cleanup order is important:
D
Dr. Stephen Henson 已提交
432
     * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
433
     * must be called before engine_cleanup_int()
M
Matt Caswell 已提交
434 435
     * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
     * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
436 437
     * - conf_modules_free_int() can end up in ENGINE code so must be called
     * before engine_cleanup_int()
D
Dr. Stephen Henson 已提交
438 439
     * - ENGINEs and additional EVP algorithms might use added OIDs names so
     * obj_cleanup_int() must be called last
M
Matt Caswell 已提交
440
     */
441 442
    rand_cleanup_int();
    conf_modules_free_int();
443
#ifndef OPENSSL_NO_ENGINE
444
    engine_cleanup_int();
445
#endif
446
    crypto_cleanup_all_ex_data_int();
447
    bio_cleanup();
448 449
    evp_cleanup_int();
    obj_cleanup_int();
450 451
    err_cleanup();

452
    base_inited = 0;
453 454 455 456 457 458 459
}

/*
 * If this function is called with a non NULL settings value then it must be
 * called prior to any threads making calls to any OpenSSL functions,
 * i.e. passing a non-null settings value is assumed to be single-threaded.
 */
460
int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
461
{
462 463 464 465 466 467 468 469 470 471
    static int stoperrset = 0;

    if (stopped) {
        if (!stoperrset) {
            /*
             * We only ever set this once to avoid getting into an infinite
             * loop where the error system keeps trying to init and fails so
             * sets an error etc
             */
            stoperrset = 1;
R
Rich Salz 已提交
472
            CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
473
        }
474
        return 0;
475
    }
R
Rich Salz 已提交
476

477 478
    if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
        return 0;
479

480 481 482 483
    if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
            && !CRYPTO_THREAD_run_once(&load_crypto_strings,
                                       ossl_init_no_load_crypto_strings))
        return 0;
484

485 486 487 488
    if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
            && !CRYPTO_THREAD_run_once(&load_crypto_strings,
                                       ossl_init_load_crypto_strings))
        return 0;
489

490 491 492
    if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
            && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
        return 0;
493

494 495 496 497
    if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
            && !CRYPTO_THREAD_run_once(&add_all_ciphers,
                                       ossl_init_add_all_ciphers))
        return 0;
498

499 500 501
    if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
            && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
        return 0;
502

503 504 505 506
    if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
            && !CRYPTO_THREAD_run_once(&add_all_digests,
                                       ossl_init_add_all_digests))
        return 0;
507

508 509 510
    if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
            && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
        return 0;
511 512

    if (opts & OPENSSL_INIT_LOAD_CONFIG) {
513
        int ret;
514
        CRYPTO_THREAD_write_lock(init_lock);
R
Rich Salz 已提交
515
        config_filename = (settings == NULL) ? NULL : settings->config_name;
516
        ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
517
        CRYPTO_THREAD_unlock(init_lock);
518 519
        if (!ret)
            return 0;
520 521
    }

522 523 524
    if ((opts & OPENSSL_INIT_ASYNC)
            && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
        return 0;
525

526
#ifndef OPENSSL_NO_ENGINE
527 528 529 530
    if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
            && !CRYPTO_THREAD_run_once(&engine_openssl,
                                       ossl_init_engine_openssl))
        return 0;
531 532
# if !defined(OPENSSL_NO_HW) && \
    (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
533 534 535 536
    if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
            && !CRYPTO_THREAD_run_once(&engine_cryptodev,
                                       ossl_init_engine_cryptodev))
        return 0;
537 538
# endif
# ifndef OPENSSL_NO_RDRAND
539 540 541
    if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
            && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
        return 0;
542
# endif
543 544 545 546
    if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
            && !CRYPTO_THREAD_run_once(&engine_dynamic,
                                       ossl_init_engine_dynamic))
        return 0;
547 548
# ifndef OPENSSL_NO_STATIC_ENGINE
#  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
549
    if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
550 551
            && !CRYPTO_THREAD_run_once(&engine_padlock,
                                       ossl_init_engine_padlock))
552
        return 0;
553 554
#  endif
#  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
555
    if ((opts & OPENSSL_INIT_ENGINE_CAPI)
556
            && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
557
        return 0;
558
#  endif
559 560 561
    if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
            && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
        return 0;
C
clucey 已提交
562
#  if !defined(OPENSSL_NO_AFALGENG)
563 564 565
    if ((opts & OPENSSL_INIT_ENGINE_AFALG)
            && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
        return 0;
C
clucey 已提交
566
#  endif
567 568
# endif
    if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
C
clucey 已提交
569 570
                | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
                | OPENSSL_INIT_ENGINE_AFALG)) {
571 572 573 574
        ENGINE_register_all_complete();
    }
#endif

E
Emilia Kasper 已提交
575
#ifndef OPENSSL_NO_COMP
576
    if ((opts & OPENSSL_INIT_ZLIB)
577
            && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
578
        return 0;
E
Emilia Kasper 已提交
579
#endif
580 581

    return 1;
582 583
}

584
int OPENSSL_atexit(void (*handler)(void))
585 586 587 588 589 590 591 592 593 594 595 596 597
{
    OPENSSL_INIT_STOP *newhand;

    newhand = OPENSSL_malloc(sizeof(*newhand));
    if (newhand == NULL)
        return 0;

    newhand->handler = handler;
    newhand->next = stop_handlers;
    stop_handlers = newhand;

    return 1;
}