Workaround for compiler bug (GCC 4.8 and 4.9 when targetting 32-bit x86).
[BearSSL] / src / ssl / ssl_client_full.c
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 /* see bearssl_ssl.h */
28 void
29 br_ssl_client_init_full(br_ssl_client_context *cc,
30 br_x509_minimal_context *xc,
31 const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num)
32 {
33 /*
34 * The "full" profile supports all implemented cipher suites.
35 *
36 * Rationale for suite order, from most important to least
37 * important rule:
38 *
39 * -- Don't use 3DES if AES or ChaCha20 is available.
40 * -- Try to have Forward Secrecy (ECDHE suite) if possible.
41 * -- When not using Forward Secrecy, ECDH key exchange is
42 * better than RSA key exchange (slightly more expensive on the
43 * client, but much cheaper on the server, and it implies smaller
44 * messages).
45 * -- ChaCha20+Poly1305 is better than AES/GCM (faster, smaller code).
46 * -- GCM is better than CBC.
47 * -- AES-128 is preferred over AES-256 (AES-128 is already
48 * strong enough, and AES-256 is 40% more expensive).
49 */
50 static const uint16_t suites[] = {
51 BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
52 BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
53 BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
54 BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
55 BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
56 BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
57 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
58 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
59 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
60 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
61 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
62 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
63 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
64 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
65 BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
66 BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
67 BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
68 BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
69 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
70 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
71 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
72 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
73 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
74 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
75 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
76 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
77 BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
78 BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
79 BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
80 BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
81 BR_TLS_RSA_WITH_AES_128_CBC_SHA,
82 BR_TLS_RSA_WITH_AES_256_CBC_SHA,
83 BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
84 BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
85 BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
86 BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
87 BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
88 };
89
90 /*
91 * All hash functions are activated.
92 * Note: the X.509 validation engine will nonetheless refuse to
93 * validate signatures that use MD5 as hash function.
94 */
95 static const br_hash_class *hashes[] = {
96 &br_md5_vtable,
97 &br_sha1_vtable,
98 &br_sha224_vtable,
99 &br_sha256_vtable,
100 &br_sha384_vtable,
101 &br_sha512_vtable
102 };
103
104 int id;
105
106 /*
107 * Reset client context and set supported versions from TLS-1.0
108 * to TLS-1.2 (inclusive).
109 */
110 br_ssl_client_zero(cc);
111 br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
112
113 /*
114 * X.509 engine uses SHA-256 to hash certificate DN (for
115 * comparisons).
116 */
117 br_x509_minimal_init(xc, &br_sha256_vtable,
118 trust_anchors, trust_anchors_num);
119
120 /*
121 * Set suites and asymmetric crypto implementations. We use the
122 * "i31" code for RSA (it is somewhat faster than the "i32"
123 * implementation).
124 * TODO: change that when better implementations are made available.
125 */
126 br_ssl_engine_set_suites(&cc->eng, suites,
127 (sizeof suites) / (sizeof suites[0]));
128 br_ssl_client_set_default_rsapub(cc);
129 br_ssl_engine_set_default_rsavrfy(&cc->eng);
130 br_ssl_engine_set_default_ecdsa(&cc->eng);
131 br_x509_minimal_set_rsa(xc, br_ssl_engine_get_rsavrfy(&cc->eng));
132 br_x509_minimal_set_ecdsa(xc,
133 br_ssl_engine_get_ec(&cc->eng),
134 br_ssl_engine_get_ecdsa(&cc->eng));
135
136 /*
137 * Set supported hash functions, for the SSL engine and for the
138 * X.509 engine.
139 */
140 for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
141 const br_hash_class *hc;
142
143 hc = hashes[id - 1];
144 br_ssl_engine_set_hash(&cc->eng, id, hc);
145 br_x509_minimal_set_hash(xc, id, hc);
146 }
147
148 /*
149 * Link the X.509 engine in the SSL engine.
150 */
151 br_ssl_engine_set_x509(&cc->eng, &xc->vtable);
152
153 /*
154 * Set the PRF implementations.
155 */
156 br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
157 br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
158 br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
159
160 /*
161 * Symmetric encryption. We use the "default" implementations
162 * (fastest among constant-time implementations).
163 */
164 br_ssl_engine_set_default_aes_cbc(&cc->eng);
165 br_ssl_engine_set_default_aes_gcm(&cc->eng);
166 br_ssl_engine_set_default_des_cbc(&cc->eng);
167 br_ssl_engine_set_default_chapol(&cc->eng);
168 }