Fix build-break
[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 <assert.h>
63 #include "ssl_locl.h"
64
65 static int stopped;
66
67 /* Implement "once" functionality */
68 #if !defined(OPENSSL_THREADS)
69 typedef int OPENSSL_INIT_ONCE;
70 # define OPENSSL_INIT_ONCE_STATIC_INIT          0
71
72 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
73 {
74     if (*once == OPENSSL_INIT_ONCE_STATIC_INIT) {
75         *once = 1;
76         init();
77     }
78 }
79 #elif defined(OPENSSL_SYS_WINDOWS)
80 # include <windows.h>
81
82 # if _WIN32_WINNT < 0x0600
83
84 /*
85  * Versions before 0x0600 (Windows Vista, Windows Server 2008 or later) do not
86  * have InitOnceExecuteOnce, so we fall back to using a spinlock instead.
87  */
88 typedef LONG OPENSSL_INIT_ONCE;
89 #  define OPENSSL_INIT_ONCE_STATIC_INIT          0
90
91 #  define ONCE_UNINITED     0
92 #  define ONCE_ININIT       1
93 #  define ONCE_DONE         2
94
95 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
96 {
97     LONG volatile *lock = (LONG *)once;
98     LONG result;
99
100     if (*lock == ONCE_DONE)
101         return;
102
103     do {
104         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
105         if (result == ONCE_UNINITED) {
106             init();
107             *lock = ONCE_DONE;
108             return;
109         }
110     } while (result == ONCE_ININIT);
111 }
112
113 # else
114
115 typedef INIT_ONCE OPENSSL_INIT_ONCE;
116 #  define OPENSSL_INIT_ONCE_STATIC_INIT          INIT_ONCE_STATIC_INIT
117
118 static BOOL CALLBACK once_cb(PINIT_ONCE once, PVOID initfp, PVOID *unused)
119 {
120     void (*init)(void) = initfp;
121
122     init();
123
124     return TRUE;
125 }
126
127 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
128 {
129     InitOnceExecuteOnce((INIT_ONCE *)once, once_cb, init, NULL);
130 }
131 # endif
132 #else /* pthreads */
133 # include <pthread.h>
134
135 typedef pthread_once_t OPENSSL_INIT_ONCE;
136 # define OPENSSL_INIT_ONCE_STATIC_INIT          PTHREAD_ONCE_INIT
137
138 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
139 {
140     pthread_once(once, init);
141 }
142 #endif
143
144 static void ssl_library_stop(void);
145
146 static OPENSSL_INIT_ONCE ssl_base = OPENSSL_INIT_ONCE_STATIC_INIT;
147 static int ssl_base_inited = 0;
148 static void ossl_init_ssl_base(void)
149 {
150 #ifdef OPENSSL_INIT_DEBUG
151     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
152                     "Adding SSL ciphers and digests\n");
153 #endif
154 #ifndef OPENSSL_NO_DES
155     EVP_add_cipher(EVP_des_cbc());
156     EVP_add_cipher(EVP_des_ede3_cbc());
157 #endif
158 #ifndef OPENSSL_NO_IDEA
159     EVP_add_cipher(EVP_idea_cbc());
160 #endif
161 #ifndef OPENSSL_NO_RC4
162     EVP_add_cipher(EVP_rc4());
163 # ifndef OPENSSL_NO_MD5
164     EVP_add_cipher(EVP_rc4_hmac_md5());
165 # endif
166 #endif
167 #ifndef OPENSSL_NO_RC2
168     EVP_add_cipher(EVP_rc2_cbc());
169     /*
170      * Not actually used for SSL/TLS but this makes PKCS#12 work if an
171      * application only calls SSL_library_init().
172      */
173     EVP_add_cipher(EVP_rc2_40_cbc());
174 #endif
175 #ifndef OPENSSL_NO_AES
176     EVP_add_cipher(EVP_aes_128_cbc());
177     EVP_add_cipher(EVP_aes_192_cbc());
178     EVP_add_cipher(EVP_aes_256_cbc());
179     EVP_add_cipher(EVP_aes_128_gcm());
180     EVP_add_cipher(EVP_aes_256_gcm());
181     EVP_add_cipher(EVP_aes_128_ccm());
182     EVP_add_cipher(EVP_aes_256_ccm());
183     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha1());
184     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha1());
185     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256());
186     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256());
187 #endif
188 #ifndef OPENSSL_NO_CAMELLIA
189     EVP_add_cipher(EVP_camellia_128_cbc());
190     EVP_add_cipher(EVP_camellia_256_cbc());
191 #endif
192 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
193     EVP_add_cipher(EVP_chacha20_poly1305());
194 #endif
195
196 #ifndef OPENSSL_NO_SEED
197     EVP_add_cipher(EVP_seed_cbc());
198 #endif
199
200 #ifndef OPENSSL_NO_MD5
201     EVP_add_digest(EVP_md5());
202     EVP_add_digest_alias(SN_md5, "ssl3-md5");
203 # ifndef OPENSSL_NO_SHA
204     EVP_add_digest(EVP_md5_sha1());
205 # endif
206 #endif
207     EVP_add_digest(EVP_sha1()); /* RSA with sha1 */
208     EVP_add_digest_alias(SN_sha1, "ssl3-sha1");
209     EVP_add_digest_alias(SN_sha1WithRSAEncryption, SN_sha1WithRSA);
210     EVP_add_digest(EVP_sha224());
211     EVP_add_digest(EVP_sha256());
212     EVP_add_digest(EVP_sha384());
213     EVP_add_digest(EVP_sha512());
214 #ifndef OPENSSL_NO_COMP
215 #ifdef OPENSSL_INIT_DEBUG
216     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
217                     "SSL_COMP_get_compression_methods()\n");
218 #endif
219     /*
220      * This will initialise the built-in compression algorithms. The value
221      * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
222      */
223     SSL_COMP_get_compression_methods();
224 #endif
225     /* initialize cipher/digest methods table */
226     ssl_load_ciphers();
227
228 #ifdef OPENSSL_INIT_DEBUG
229     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
230                     "SSL_add_ssl_module()\n");
231 #endif
232     SSL_add_ssl_module();
233     /*
234      * We ignore an error return here. Not much we can do - but not that bad
235      * either. We can still safely continue.
236      */
237     OPENSSL_atexit(ssl_library_stop);
238     ssl_base_inited = 1;
239 }
240
241 static OPENSSL_INIT_ONCE ssl_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
242 static int ssl_strings_inited = 0;
243 static void ossl_init_load_ssl_strings(void)
244 {
245     /*
246      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
247      * pulling in all the error strings during static linking
248      */
249 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
250 # ifdef OPENSSL_INIT_DEBUG
251         fprintf(stderr, "OPENSSL_INIT: ossl_init_load_ssl_strings: "
252                         "ERR_load_SSL_strings()\n");
253 # endif
254     ERR_load_SSL_strings();
255 #endif
256     ssl_strings_inited = 1;
257 }
258
259 static void ossl_init_no_load_ssl_strings(void)
260 {
261     /* Do nothing in this case */
262     return;
263 }
264
265 static void ssl_library_stop(void)
266 {
267     /* Might be explicitly called and also by atexit */
268     if (stopped)
269         return;
270     stopped = 1;
271
272     if (ssl_base_inited) {
273 #ifndef OPENSSL_NO_COMP
274 #ifdef OPENSSL_INIT_DEBUG
275         fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
276                         "SSL_COMP_free_compression_methods()\n");
277 #endif
278         SSL_COMP_free_compression_methods();
279 #endif
280     }
281
282     if (ssl_strings_inited) {
283 #ifdef OPENSSL_INIT_DEBUG
284         fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
285                         "ERR_free_strings()\n");
286 #endif
287         /*
288          * If both crypto and ssl error strings are inited we will end up
289          * calling ERR_free_strings() twice - but that's ok. The second time
290          * will be a no-op. It's easier to do that than to try and track
291          * between the two libraries whether they have both been inited.
292          */
293         ERR_free_strings();
294     }
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 int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
304 {
305     static int stoperrset = 0;
306
307     if (stopped) {
308         if (!stoperrset) {
309             /*
310              * We only ever set this once to avoid getting into an infinite
311              * loop where the error system keeps trying to init and fails so
312              * sets an error etc
313              */
314             stoperrset = 1;
315             SSLerr(SSL_F_OPENSSL_INIT_SSL, ERR_R_INIT_FAIL);
316         }
317         return 0;
318     }
319
320     if (!OPENSSL_init_crypto(opts | OPENSSL_INIT_ADD_ALL_CIPHERS
321                              | OPENSSL_INIT_ADD_ALL_DIGESTS, settings))
322         return 0;
323
324     ossl_init_once_run(&ssl_base, ossl_init_ssl_base);
325
326     if (opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
327         ossl_init_once_run(&ssl_strings, ossl_init_no_load_ssl_strings);
328
329     if (opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
330         ossl_init_once_run(&ssl_strings, ossl_init_load_ssl_strings);
331
332     return 1;
333 }
334