/src/openssl/ssl/ssl_init.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | #include "e_os.h" |
11 | | |
12 | | #include "internal/err.h" |
13 | | #include <openssl/crypto.h> |
14 | | #include <openssl/evp.h> |
15 | | #include "ssl_locl.h" |
16 | | #include "internal/thread_once.h" |
17 | | |
18 | | static int stopped; |
19 | | |
20 | | static void ssl_library_stop(void); |
21 | | |
22 | | static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT; |
23 | | static int ssl_base_inited = 0; |
24 | | DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base) |
25 | 0 | { |
26 | | #ifdef OPENSSL_INIT_DEBUG |
27 | | fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: " |
28 | | "Adding SSL ciphers and digests\n"); |
29 | | #endif |
30 | | #ifndef OPENSSL_NO_DES |
31 | 0 | EVP_add_cipher(EVP_des_cbc()); |
32 | 0 | EVP_add_cipher(EVP_des_ede3_cbc()); |
33 | 0 | #endif |
34 | 0 | #ifndef OPENSSL_NO_IDEA |
35 | 0 | EVP_add_cipher(EVP_idea_cbc()); |
36 | 0 | #endif |
37 | 0 | #ifndef OPENSSL_NO_RC4 |
38 | 0 | EVP_add_cipher(EVP_rc4()); |
39 | 0 | # ifndef OPENSSL_NO_MD5 |
40 | 0 | EVP_add_cipher(EVP_rc4_hmac_md5()); |
41 | 0 | # endif |
42 | 0 | #endif |
43 | 0 | #ifndef OPENSSL_NO_RC2 |
44 | 0 | EVP_add_cipher(EVP_rc2_cbc()); |
45 | 0 | /* |
46 | 0 | * Not actually used for SSL/TLS but this makes PKCS#12 work if an |
47 | 0 | * application only calls SSL_library_init(). |
48 | 0 | */ |
49 | 0 | EVP_add_cipher(EVP_rc2_40_cbc()); |
50 | 0 | #endif |
51 | 0 | EVP_add_cipher(EVP_aes_128_cbc()); |
52 | 0 | EVP_add_cipher(EVP_aes_192_cbc()); |
53 | 0 | EVP_add_cipher(EVP_aes_256_cbc()); |
54 | 0 | EVP_add_cipher(EVP_aes_128_gcm()); |
55 | 0 | EVP_add_cipher(EVP_aes_256_gcm()); |
56 | 0 | EVP_add_cipher(EVP_aes_128_ccm()); |
57 | 0 | EVP_add_cipher(EVP_aes_256_ccm()); |
58 | 0 | EVP_add_cipher(EVP_aes_128_cbc_hmac_sha1()); |
59 | 0 | EVP_add_cipher(EVP_aes_256_cbc_hmac_sha1()); |
60 | 0 | EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256()); |
61 | 0 | EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256()); |
62 | 0 | #ifndef OPENSSL_NO_ARIA |
63 | 0 | EVP_add_cipher(EVP_aria_128_gcm()); |
64 | 0 | EVP_add_cipher(EVP_aria_256_gcm()); |
65 | 0 | #endif |
66 | 0 | #ifndef OPENSSL_NO_CAMELLIA |
67 | 0 | EVP_add_cipher(EVP_camellia_128_cbc()); |
68 | 0 | EVP_add_cipher(EVP_camellia_256_cbc()); |
69 | 0 | #endif |
70 | 0 | #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) |
71 | 0 | EVP_add_cipher(EVP_chacha20_poly1305()); |
72 | 0 | #endif |
73 | 0 |
|
74 | 0 | #ifndef OPENSSL_NO_SEED |
75 | 0 | EVP_add_cipher(EVP_seed_cbc()); |
76 | 0 | #endif |
77 | 0 |
|
78 | 0 | #ifndef OPENSSL_NO_MD5 |
79 | 0 | EVP_add_digest(EVP_md5()); |
80 | 0 | EVP_add_digest_alias(SN_md5, "ssl3-md5"); |
81 | 0 | EVP_add_digest(EVP_md5_sha1()); |
82 | 0 | #endif |
83 | 0 | EVP_add_digest(EVP_sha1()); /* RSA with sha1 */ |
84 | 0 | EVP_add_digest_alias(SN_sha1, "ssl3-sha1"); |
85 | 0 | EVP_add_digest_alias(SN_sha1WithRSAEncryption, SN_sha1WithRSA); |
86 | 0 | EVP_add_digest(EVP_sha224()); |
87 | 0 | EVP_add_digest(EVP_sha256()); |
88 | 0 | EVP_add_digest(EVP_sha384()); |
89 | 0 | EVP_add_digest(EVP_sha512()); |
90 | 0 | #ifndef OPENSSL_NO_COMP |
91 | | # ifdef OPENSSL_INIT_DEBUG |
92 | | fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: " |
93 | | "SSL_COMP_get_compression_methods()\n"); |
94 | | # endif |
95 | | /* |
96 | 0 | * This will initialise the built-in compression algorithms. The value |
97 | 0 | * returned is a STACK_OF(SSL_COMP), but that can be discarded safely |
98 | 0 | */ |
99 | 0 | SSL_COMP_get_compression_methods(); |
100 | 0 | #endif |
101 | 0 | /* initialize cipher/digest methods table */ |
102 | 0 | if (!ssl_load_ciphers()) |
103 | 0 | return 0; |
104 | 0 | |
105 | | #ifdef OPENSSL_INIT_DEBUG |
106 | | fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: " |
107 | | "SSL_add_ssl_module()\n"); |
108 | | #endif |
109 | | /* |
110 | 0 | * We ignore an error return here. Not much we can do - but not that bad |
111 | 0 | * either. We can still safely continue. |
112 | 0 | */ |
113 | 0 | OPENSSL_atexit(ssl_library_stop); |
114 | 0 | ssl_base_inited = 1; |
115 | 0 | return 1; |
116 | 0 | } |
117 | | |
118 | | static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT; |
119 | | static int ssl_strings_inited = 0; |
120 | | DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings) |
121 | 0 | { |
122 | 0 | /* |
123 | 0 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time |
124 | 0 | * pulling in all the error strings during static linking |
125 | 0 | */ |
126 | 0 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) |
127 | | # ifdef OPENSSL_INIT_DEBUG |
128 | | fprintf(stderr, "OPENSSL_INIT: ossl_init_load_ssl_strings: " |
129 | | "ERR_load_SSL_strings()\n"); |
130 | | # endif |
131 | | ERR_load_SSL_strings(); |
132 | 0 | ssl_strings_inited = 1; |
133 | 0 | #endif |
134 | 0 | return 1; |
135 | 0 | } |
136 | | |
137 | | DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_ssl_strings) |
138 | 0 | { |
139 | 0 | /* Do nothing in this case */ |
140 | 0 | return 1; |
141 | 0 | } |
142 | | |
143 | | static void ssl_library_stop(void) |
144 | 0 | { |
145 | 0 | /* Might be explicitly called and also by atexit */ |
146 | 0 | if (stopped) |
147 | 0 | return; |
148 | 0 | stopped = 1; |
149 | 0 |
|
150 | 0 | if (ssl_base_inited) { |
151 | 0 | #ifndef OPENSSL_NO_COMP |
152 | | # ifdef OPENSSL_INIT_DEBUG |
153 | | fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: " |
154 | | "ssl_comp_free_compression_methods_int()\n"); |
155 | | # endif |
156 | | ssl_comp_free_compression_methods_int(); |
157 | 0 | #endif |
158 | 0 | } |
159 | 0 |
|
160 | 0 | if (ssl_strings_inited) { |
161 | | #ifdef OPENSSL_INIT_DEBUG |
162 | | fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: " |
163 | | "err_free_strings_int()\n"); |
164 | | #endif |
165 | | /* |
166 | 0 | * If both crypto and ssl error strings are inited we will end up |
167 | 0 | * calling err_free_strings_int() twice - but that's ok. The second |
168 | 0 | * time will be a no-op. It's easier to do that than to try and track |
169 | 0 | * between the two libraries whether they have both been inited. |
170 | 0 | */ |
171 | 0 | err_free_strings_int(); |
172 | 0 | } |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * If this function is called with a non NULL settings value then it must be |
177 | | * called prior to any threads making calls to any OpenSSL functions, |
178 | | * i.e. passing a non-null settings value is assumed to be single-threaded. |
179 | | */ |
180 | | int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings) |
181 | 0 | { |
182 | 0 | static int stoperrset = 0; |
183 | 0 |
|
184 | 0 | if (stopped) { |
185 | 0 | if (!stoperrset) { |
186 | 0 | /* |
187 | 0 | * We only ever set this once to avoid getting into an infinite |
188 | 0 | * loop where the error system keeps trying to init and fails so |
189 | 0 | * sets an error etc |
190 | 0 | */ |
191 | 0 | stoperrset = 1; |
192 | 0 | SSLerr(SSL_F_OPENSSL_INIT_SSL, ERR_R_INIT_FAIL); |
193 | 0 | } |
194 | 0 | return 0; |
195 | 0 | } |
196 | 0 |
|
197 | 0 | if (!OPENSSL_init_crypto(opts |
198 | 0 | #ifndef OPENSSL_NO_AUTOLOAD_CONFIG |
199 | 0 | | OPENSSL_INIT_LOAD_CONFIG |
200 | 0 | #endif |
201 | 0 | | OPENSSL_INIT_ADD_ALL_CIPHERS |
202 | 0 | | OPENSSL_INIT_ADD_ALL_DIGESTS, |
203 | 0 | settings)) |
204 | 0 | return 0; |
205 | 0 | |
206 | 0 | if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base)) |
207 | 0 | return 0; |
208 | 0 | |
209 | 0 | if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS) |
210 | 0 | && !RUN_ONCE(&ssl_strings, ossl_init_no_load_ssl_strings)) |
211 | 0 | return 0; |
212 | 0 | |
213 | 0 | if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS) |
214 | 0 | && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings)) |
215 | 0 | return 0; |
216 | 0 | |
217 | 0 | return 1; |
218 | 0 | } |