f1aa2c401afe3ca667be9db4624d652c2e3760a8
[openssl.git] / ssl / ssl_init.c
1 /*
2  * Written by Matt Caswell for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    openssl-core@openssl.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include "e_os.h"
59
60 #include <openssl/crypto.h>
61 #include <openssl/evp.h>
62 #include "ssl_locl.h"
63
64 /* Implement "once" functionality */
65 #if !defined(OPENSSL_THREADS)
66 typedef int OPENSSL_INIT_ONCE;
67 # define OPENSSL_INIT_ONCE_STATIC_INIT          0
68 # define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once)   (*(once) = 0)
69
70 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
71 {
72     if (*once == OPENSSL_INIT_ONCE_STATIC_INIT) {
73         *once = 1;
74         init();
75     }
76 }
77 #elif defined(OPENSSL_SYS_WINDOWS)
78 # include <windows.h>
79
80 # if _WIN32_WINNT < 0x0600
81
82 /*
83  * Versions before 0x0600 (Windows Vista, Windows Server 2008 or later) do not
84  * have InitOnceExecuteOnce, so we fall back to using a spinlock instead.
85  */
86 typedef LONG OPENSSL_INIT_ONCE;
87 #  define OPENSSL_INIT_ONCE_STATIC_INIT          0
88 #  define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once)   (*(once) = 0)
89
90 #  define ONCE_UNINITED     0
91 #  define ONCE_ININIT       1
92 #  define ONCE_DONE         2
93
94 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
95 {
96     LONG volatile *lock = (LONG *)once;
97     LONG result;
98
99     if (*lock == ONCE_DONE)
100         return;
101
102     do {
103         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
104         if (result == ONCE_UNINITED) {
105             init();
106             *lock = ONCE_DONE;
107             return;
108         }
109     } while (result == ONCE_ININIT);
110 }
111
112 # else
113
114 typedef INIT_ONCE OPENSSL_INIT_ONCE;
115 #  define OPENSSL_INIT_ONCE_STATIC_INIT          INIT_ONCE_STATIC_INIT
116 #  define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) \
117                 InitOnceInitialize((PINIT_ONCE)(once))
118
119 static BOOL CALLBACK once_cb(PINIT_ONCE once, PVOID initfp, PVOID *unused)
120 {
121     void (*init)(void) = initfp;
122
123     init();
124
125     return TRUE;
126 }
127
128 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
129 {
130     InitOnceExecuteOnce((INIT_ONCE *)once, once_cb, init, NULL);
131 }
132 # endif
133 #else /* pthreads */
134 # include <pthread.h>
135
136 typedef pthread_once_t OPENSSL_INIT_ONCE;
137 # define OPENSSL_INIT_ONCE_STATIC_INIT          PTHREAD_ONCE_INIT
138 # define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once)   (*(once) = PTHREAD_ONCE_INIT)
139
140 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
141 {
142     pthread_once(once, init);
143 }
144 #endif
145
146 static void ssl_library_stop(void);
147
148 static OPENSSL_INIT_ONCE ssl_base = OPENSSL_INIT_ONCE_STATIC_INIT;
149 static int ssl_base_inited = 0;
150 static void ossl_init_ssl_base(void)
151 {
152 #ifdef OPENSSL_INIT_DEBUG
153     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
154                     "Adding SSL ciphers and digests\n");
155 #endif
156 #ifndef OPENSSL_NO_DES
157     EVP_add_cipher(EVP_des_cbc());
158     EVP_add_cipher(EVP_des_ede3_cbc());
159 #endif
160 #ifndef OPENSSL_NO_IDEA
161     EVP_add_cipher(EVP_idea_cbc());
162 #endif
163 #ifndef OPENSSL_NO_RC4
164     EVP_add_cipher(EVP_rc4());
165 # ifndef OPENSSL_NO_MD5
166     EVP_add_cipher(EVP_rc4_hmac_md5());
167 # endif
168 #endif
169 #ifndef OPENSSL_NO_RC2
170     EVP_add_cipher(EVP_rc2_cbc());
171     /*
172      * Not actually used for SSL/TLS but this makes PKCS#12 work if an
173      * application only calls SSL_library_init().
174      */
175     EVP_add_cipher(EVP_rc2_40_cbc());
176 #endif
177 #ifndef OPENSSL_NO_AES
178     EVP_add_cipher(EVP_aes_128_cbc());
179     EVP_add_cipher(EVP_aes_192_cbc());
180     EVP_add_cipher(EVP_aes_256_cbc());
181     EVP_add_cipher(EVP_aes_128_gcm());
182     EVP_add_cipher(EVP_aes_256_gcm());
183     EVP_add_cipher(EVP_aes_128_ccm());
184     EVP_add_cipher(EVP_aes_256_ccm());
185     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha1());
186     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha1());
187     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256());
188     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256());
189 #endif
190 #ifndef OPENSSL_NO_CAMELLIA
191     EVP_add_cipher(EVP_camellia_128_cbc());
192     EVP_add_cipher(EVP_camellia_256_cbc());
193 #endif
194 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
195     EVP_add_cipher(EVP_chacha20_poly1305());
196 #endif
197
198 #ifndef OPENSSL_NO_SEED
199     EVP_add_cipher(EVP_seed_cbc());
200 #endif
201
202 #ifndef OPENSSL_NO_MD5
203     EVP_add_digest(EVP_md5());
204     EVP_add_digest_alias(SN_md5, "ssl3-md5");
205 # ifndef OPENSSL_NO_SHA
206     EVP_add_digest(EVP_md5_sha1());
207 # endif
208 #endif
209     EVP_add_digest(EVP_sha1()); /* RSA with sha1 */
210     EVP_add_digest_alias(SN_sha1, "ssl3-sha1");
211     EVP_add_digest_alias(SN_sha1WithRSAEncryption, SN_sha1WithRSA);
212     EVP_add_digest(EVP_sha224());
213     EVP_add_digest(EVP_sha256());
214     EVP_add_digest(EVP_sha384());
215     EVP_add_digest(EVP_sha512());
216 #ifndef OPENSSL_NO_COMP
217 #ifdef OPENSSL_INIT_DEBUG
218     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
219                     "SSL_COMP_get_compression_methods()\n");
220 #endif
221     /*
222      * This will initialise the built-in compression algorithms. The value
223      * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
224      */
225     SSL_COMP_get_compression_methods();
226 #endif
227     /* initialize cipher/digest methods table */
228     ssl_load_ciphers();
229
230 #ifdef OPENSSL_INIT_DEBUG
231     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
232                     "SSL_add_ssl_module()\n");
233 #endif
234     SSL_add_ssl_module();
235     /*
236      * We ignore an error return here. Not much we can do - but not that bad
237      * either. We can still safely continue.
238      */
239     OPENSSL_INIT_register_stop_handler(ssl_library_stop);
240     ssl_base_inited = 1;
241 }
242
243 static OPENSSL_INIT_ONCE ssl_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
244 static int ssl_strings_inited = 0;
245 static void ossl_init_load_ssl_strings(void)
246 {
247     /*
248      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
249      * pulling in all the error strings during static linking
250      */
251 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
252 # ifdef OPENSSL_INIT_DEBUG
253         fprintf(stderr, "OPENSSL_INIT: ossl_init_load_ssl_strings: "
254                         "ERR_load_SSL_strings()\n");
255 # endif
256     ERR_load_SSL_strings();
257 #endif
258     ssl_strings_inited = 1;
259 }
260
261 static void ossl_init_no_load_ssl_strings(void)
262 {
263     /* Do nothing in this case */
264     return;
265 }
266
267 static void ssl_library_stop(void)
268 {
269     if (ssl_base_inited) {
270 #ifndef OPENSSL_NO_COMP
271 #ifdef OPENSSL_INIT_DEBUG
272         fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
273                         "SSL_COMP_free_compression_methods()\n");
274 #endif
275         SSL_COMP_free_compression_methods();
276         ssl_base_inited = 0;
277         OPENSSL_INIT_ONCE_DYNAMIC_INIT(&ssl_base);
278 #endif
279     }
280
281     if (ssl_strings_inited) {
282 #ifdef OPENSSL_INIT_DEBUG
283         fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
284                         "ERR_free_strings()\n");
285 #endif
286         /*
287          * If both crypto and ssl error strings are inited we will end up
288          * calling ERR_free_strings() twice - but that's ok. The second time
289          * will be a no-op. It's easier to do that than to try and track
290          * between the two libraries whether they have both been inited.
291          */
292         ERR_free_strings();
293         ssl_strings_inited = 0;
294         OPENSSL_INIT_ONCE_DYNAMIC_INIT(&ssl_strings);
295     }
296 }
297
298 /*
299  * If this function is called with a non NULL settings value then it must be
300  * called prior to any threads making calls to any OpenSSL functions,
301  * i.e. passing a non-null settings value is assumed to be single-threaded.
302  */
303 void OPENSSL_INIT_ssl_library_start(uint64_t opts,
304                                  const OPENSSL_INIT_SETTINGS *settings)
305 {
306     OPENSSL_INIT_crypto_library_start(opts | OPENSSL_INIT_ADD_ALL_CIPHERS
307                                    | OPENSSL_INIT_ADD_ALL_DIGESTS, settings);
308
309     ossl_init_once_run(&ssl_base, ossl_init_ssl_base);
310
311     if (opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
312         ossl_init_once_run(&ssl_strings, ossl_init_no_load_ssl_strings);
313
314     if (opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
315         ossl_init_once_run(&ssl_strings, ossl_init_load_ssl_strings);
316 }
317