Change the INSTALL documentation for unified builds
[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 <internal/threads.h>
61 #include <openssl/crypto.h>
62 #include <openssl/evp.h>
63 #include <assert.h>
64 #include "ssl_locl.h"
65
66 static int stopped;
67
68 static void ssl_library_stop(void);
69
70 static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
71 static int ssl_base_inited = 0;
72 static void ossl_init_ssl_base(void)
73 {
74 #ifdef OPENSSL_INIT_DEBUG
75     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
76                     "Adding SSL ciphers and digests\n");
77 #endif
78 #ifndef OPENSSL_NO_DES
79     EVP_add_cipher(EVP_des_cbc());
80     EVP_add_cipher(EVP_des_ede3_cbc());
81 #endif
82 #ifndef OPENSSL_NO_IDEA
83     EVP_add_cipher(EVP_idea_cbc());
84 #endif
85 #ifndef OPENSSL_NO_RC4
86     EVP_add_cipher(EVP_rc4());
87 # ifndef OPENSSL_NO_MD5
88     EVP_add_cipher(EVP_rc4_hmac_md5());
89 # endif
90 #endif
91 #ifndef OPENSSL_NO_RC2
92     EVP_add_cipher(EVP_rc2_cbc());
93     /*
94      * Not actually used for SSL/TLS but this makes PKCS#12 work if an
95      * application only calls SSL_library_init().
96      */
97     EVP_add_cipher(EVP_rc2_40_cbc());
98 #endif
99 #ifndef OPENSSL_NO_AES
100     EVP_add_cipher(EVP_aes_128_cbc());
101     EVP_add_cipher(EVP_aes_192_cbc());
102     EVP_add_cipher(EVP_aes_256_cbc());
103     EVP_add_cipher(EVP_aes_128_gcm());
104     EVP_add_cipher(EVP_aes_256_gcm());
105     EVP_add_cipher(EVP_aes_128_ccm());
106     EVP_add_cipher(EVP_aes_256_ccm());
107     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha1());
108     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha1());
109     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256());
110     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256());
111 #endif
112 #ifndef OPENSSL_NO_CAMELLIA
113     EVP_add_cipher(EVP_camellia_128_cbc());
114     EVP_add_cipher(EVP_camellia_256_cbc());
115 #endif
116 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
117     EVP_add_cipher(EVP_chacha20_poly1305());
118 #endif
119
120 #ifndef OPENSSL_NO_SEED
121     EVP_add_cipher(EVP_seed_cbc());
122 #endif
123
124 #ifndef OPENSSL_NO_MD5
125     EVP_add_digest(EVP_md5());
126     EVP_add_digest_alias(SN_md5, "ssl3-md5");
127 # ifndef OPENSSL_NO_SHA
128     EVP_add_digest(EVP_md5_sha1());
129 # endif
130 #endif
131     EVP_add_digest(EVP_sha1()); /* RSA with sha1 */
132     EVP_add_digest_alias(SN_sha1, "ssl3-sha1");
133     EVP_add_digest_alias(SN_sha1WithRSAEncryption, SN_sha1WithRSA);
134     EVP_add_digest(EVP_sha224());
135     EVP_add_digest(EVP_sha256());
136     EVP_add_digest(EVP_sha384());
137     EVP_add_digest(EVP_sha512());
138 #ifndef OPENSSL_NO_COMP
139 #ifdef OPENSSL_INIT_DEBUG
140     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
141                     "SSL_COMP_get_compression_methods()\n");
142 #endif
143     /*
144      * This will initialise the built-in compression algorithms. The value
145      * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
146      */
147     SSL_COMP_get_compression_methods();
148 #endif
149     /* initialize cipher/digest methods table */
150     ssl_load_ciphers();
151
152 #ifdef OPENSSL_INIT_DEBUG
153     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
154                     "SSL_add_ssl_module()\n");
155 #endif
156     SSL_add_ssl_module();
157     /*
158      * We ignore an error return here. Not much we can do - but not that bad
159      * either. We can still safely continue.
160      */
161     OPENSSL_atexit(ssl_library_stop);
162     ssl_base_inited = 1;
163 }
164
165 static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
166 static int ssl_strings_inited = 0;
167 static void ossl_init_load_ssl_strings(void)
168 {
169     /*
170      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
171      * pulling in all the error strings during static linking
172      */
173 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
174 # ifdef OPENSSL_INIT_DEBUG
175         fprintf(stderr, "OPENSSL_INIT: ossl_init_load_ssl_strings: "
176                         "ERR_load_SSL_strings()\n");
177 # endif
178     ERR_load_SSL_strings();
179 #endif
180     ssl_strings_inited = 1;
181 }
182
183 static void ossl_init_no_load_ssl_strings(void)
184 {
185     /* Do nothing in this case */
186     return;
187 }
188
189 static void ssl_library_stop(void)
190 {
191     /* Might be explicitly called and also by atexit */
192     if (stopped)
193         return;
194     stopped = 1;
195
196     if (ssl_base_inited) {
197 #ifndef OPENSSL_NO_COMP
198 #ifdef OPENSSL_INIT_DEBUG
199         fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
200                         "SSL_COMP_free_compression_methods()\n");
201 #endif
202         SSL_COMP_free_compression_methods();
203 #endif
204     }
205
206     if (ssl_strings_inited) {
207 #ifdef OPENSSL_INIT_DEBUG
208         fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
209                         "ERR_free_strings()\n");
210 #endif
211         /*
212          * If both crypto and ssl error strings are inited we will end up
213          * calling ERR_free_strings() twice - but that's ok. The second time
214          * will be a no-op. It's easier to do that than to try and track
215          * between the two libraries whether they have both been inited.
216          */
217         ERR_free_strings();
218     }
219 }
220
221
222 /*
223  * If this function is called with a non NULL settings value then it must be
224  * called prior to any threads making calls to any OpenSSL functions,
225  * i.e. passing a non-null settings value is assumed to be single-threaded.
226  */
227 int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
228 {
229     static int stoperrset = 0;
230
231     if (stopped) {
232         if (!stoperrset) {
233             /*
234              * We only ever set this once to avoid getting into an infinite
235              * loop where the error system keeps trying to init and fails so
236              * sets an error etc
237              */
238             stoperrset = 1;
239             SSLerr(SSL_F_OPENSSL_INIT_SSL, ERR_R_INIT_FAIL);
240         }
241         return 0;
242     }
243
244     if (!OPENSSL_init_crypto(opts | OPENSSL_INIT_ADD_ALL_CIPHERS
245                              | OPENSSL_INIT_ADD_ALL_DIGESTS, settings))
246         return 0;
247
248     if (!CRYPTO_THREAD_run_once(&ssl_base, ossl_init_ssl_base))
249         return 0;
250
251     if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
252             && !CRYPTO_THREAD_run_once(&ssl_strings,
253                                        ossl_init_no_load_ssl_strings))
254         return 0;
255
256     if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
257             && !CRYPTO_THREAD_run_once(&ssl_strings,
258                                        ossl_init_load_ssl_strings))
259         return 0;
260
261     return 1;
262 }
263