]>
Commit | Line | Data |
---|---|---|
b184e3ef | 1 | /* |
556009c5 | 2 | * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. |
b184e3ef | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
b184e3ef MC |
8 | */ |
9 | ||
d5f9166b | 10 | #include "internal/e_os.h" |
b184e3ef | 11 | |
6827cb36 | 12 | #include "internal/err.h" |
b184e3ef MC |
13 | #include <openssl/crypto.h> |
14 | #include <openssl/evp.h> | |
5c641735 | 15 | #include <openssl/trace.h> |
706457b7 | 16 | #include "ssl_local.h" |
f5a46ed7 | 17 | #include "sslerr.h" |
c2e4e5d2 | 18 | #include "internal/thread_once.h" |
b184e3ef | 19 | |
dd27f16e RS |
20 | static int stopped; |
21 | ||
b1f1e7ae | 22 | static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 23 | static int ssl_base_inited = 0; |
c2e4e5d2 | 24 | DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base) |
b184e3ef | 25 | { |
b184e3ef | 26 | #ifndef OPENSSL_NO_COMP |
5c641735 RL |
27 | OSSL_TRACE(INIT, "ossl_init_ssl_base: " |
28 | "SSL_COMP_get_compression_methods()\n"); | |
b184e3ef MC |
29 | /* |
30 | * This will initialise the built-in compression algorithms. The value | |
31 | * returned is a STACK_OF(SSL_COMP), but that can be discarded safely | |
32 | */ | |
33 | SSL_COMP_get_compression_methods(); | |
34 | #endif | |
c8f6c28a | 35 | ssl_sort_cipher_list(); |
1287dabd | 36 | OSSL_TRACE(INIT, "ossl_init_ssl_base: SSL_add_ssl_module()\n"); |
b184e3ef | 37 | ssl_base_inited = 1; |
c2e4e5d2 | 38 | return 1; |
b184e3ef MC |
39 | } |
40 | ||
b1f1e7ae | 41 | static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT; |
1c8787d5 | 42 | |
c2e4e5d2 | 43 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings) |
b184e3ef | 44 | { |
498abff0 MC |
45 | /* |
46 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time | |
47 | * pulling in all the error strings during static linking | |
48 | */ | |
49 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) | |
b93f6c2d P |
50 | OSSL_TRACE(INIT, "ossl_init_load_ssl_strings: ossl_err_load_SSL_strings()\n"); |
51 | ossl_err_load_SSL_strings(); | |
10281e83 | 52 | #endif |
c2e4e5d2 | 53 | return 1; |
b184e3ef MC |
54 | } |
55 | ||
660a1e04 MC |
56 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings, |
57 | ossl_init_load_ssl_strings) | |
b184e3ef MC |
58 | { |
59 | /* Do nothing in this case */ | |
c2e4e5d2 | 60 | return 1; |
b184e3ef MC |
61 | } |
62 | ||
b184e3ef MC |
63 | /* |
64 | * If this function is called with a non NULL settings value then it must be | |
65 | * called prior to any threads making calls to any OpenSSL functions, | |
66 | * i.e. passing a non-null settings value is assumed to be single-threaded. | |
67 | */ | |
bbaeadb0 | 68 | int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) |
b184e3ef | 69 | { |
302f7588 MC |
70 | static int stoperrset = 0; |
71 | ||
72 | if (stopped) { | |
73 | if (!stoperrset) { | |
74 | /* | |
75 | * We only ever set this once to avoid getting into an infinite | |
76 | * loop where the error system keeps trying to init and fails so | |
77 | * sets an error etc | |
78 | */ | |
79 | stoperrset = 1; | |
6849b73c | 80 | ERR_raise(ERR_LIB_SSL, ERR_R_INIT_FAIL); |
302f7588 | 81 | } |
0fc32b07 | 82 | return 0; |
302f7588 | 83 | } |
dd27f16e | 84 | |
df1f538f | 85 | opts |= OPENSSL_INIT_ADD_ALL_CIPHERS |
e74bd290 | 86 | | OPENSSL_INIT_ADD_ALL_DIGESTS; |
dbabc862 | 87 | #ifndef OPENSSL_NO_AUTOLOAD_CONFIG |
df1f538f VD |
88 | if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0) |
89 | opts |= OPENSSL_INIT_LOAD_CONFIG; | |
dbabc862 | 90 | #endif |
df1f538f VD |
91 | |
92 | if (!OPENSSL_init_crypto(opts, settings)) | |
0fc32b07 | 93 | return 0; |
b184e3ef | 94 | |
d8f031e8 | 95 | if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base)) |
b1f1e7ae | 96 | return 0; |
b184e3ef | 97 | |
b1f1e7ae | 98 | if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS) |
660a1e04 MC |
99 | && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings, |
100 | ossl_init_load_ssl_strings)) | |
b1f1e7ae | 101 | return 0; |
b184e3ef | 102 | |
b1f1e7ae | 103 | if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS) |
a230b26e | 104 | && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings)) |
b1f1e7ae | 105 | return 0; |
0fc32b07 MC |
106 | |
107 | return 1; | |
b184e3ef | 108 | } |