Added support for client certificates (both client-side and server-side, but still...
[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 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 * -- GCM is better than CBC.
46 * -- AES-128 is preferred over AES-256 (AES-128 is already
47 * strong enough, and AES-256 is 40% more expensive).
48 */
49 static const uint16_t suites[] = {
50 BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
51 BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
52 BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
53 BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
54 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
55 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
56 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
57 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
58 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
59 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
60 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
61 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
62 BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
63 BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
64 BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
65 BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
66 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
67 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
68 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
69 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
70 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
71 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
72 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
73 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
74 BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
75 BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
76 BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
77 BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
78 BR_TLS_RSA_WITH_AES_128_CBC_SHA,
79 BR_TLS_RSA_WITH_AES_256_CBC_SHA,
80 BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
81 BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
82 BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
83 BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
84 BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
85 };
86
87 /*
88 * All hash functions are activated.
89 * Note: the X.509 validation engine will nonetheless refuse to
90 * validate signatures that use MD5 as hash function.
91 */
92 static const br_hash_class *hashes[] = {
93 &br_md5_vtable,
94 &br_sha1_vtable,
95 &br_sha224_vtable,
96 &br_sha256_vtable,
97 &br_sha384_vtable,
98 &br_sha512_vtable
99 };
100
101 int id;
102
103 /*
104 * Reset client context and set supported versions from TLS-1.0
105 * to TLS-1.2 (inclusive).
106 */
107 br_ssl_client_zero(cc);
108 br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
109
110 /*
111 * X.509 engine uses SHA-256 to hash certificate DN (for
112 * comparisons).
113 */
114 br_x509_minimal_init(xc, &br_sha256_vtable,
115 trust_anchors, trust_anchors_num);
116
117 /*
118 * Set suites and asymmetric crypto implementations. We use the
119 * "i31" code for RSA (it is somewhat faster than the "i32"
120 * implementation).
121 * TODO: change that when better implementations are made available.
122 */
123 br_ssl_engine_set_suites(&cc->eng, suites,
124 (sizeof suites) / (sizeof suites[0]));
125 br_ssl_client_set_rsapub(cc, &br_rsa_i31_public);
126 br_ssl_engine_set_rsavrfy(&cc->eng, &br_rsa_i31_pkcs1_vrfy);
127 br_ssl_engine_set_ec(&cc->eng, &br_ec_prime_i31);
128 br_ssl_engine_set_ecdsa(&cc->eng, &br_ecdsa_i31_vrfy_asn1);
129 br_x509_minimal_set_rsa(xc, &br_rsa_i31_pkcs1_vrfy);
130 br_x509_minimal_set_ecdsa(xc,
131 &br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
132
133 /*
134 * Set supported hash functions, for the SSL engine and for the
135 * X.509 engine.
136 */
137 for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
138 const br_hash_class *hc;
139
140 hc = hashes[id - 1];
141 br_ssl_engine_set_hash(&cc->eng, id, hc);
142 br_x509_minimal_set_hash(xc, id, hc);
143 }
144
145 /*
146 * Link the X.509 engine in the SSL engine.
147 */
148 br_ssl_engine_set_x509(&cc->eng, &xc->vtable);
149
150 /*
151 * Set the PRF implementations.
152 */
153 br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
154 br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
155 br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
156
157 /*
158 * Symmetric encryption. We use the "constant-time"
159 * implementations, which are the safest.
160 *
161 * On architectures detected as "64-bit", use the 64-bit
162 * versions (aes_ct64, ghash_ctmul64).
163 */
164 #if BR_64
165 br_ssl_engine_set_aes_cbc(&cc->eng,
166 &br_aes_ct64_cbcenc_vtable,
167 &br_aes_ct64_cbcdec_vtable);
168 br_ssl_engine_set_aes_ctr(&cc->eng,
169 &br_aes_ct64_ctr_vtable);
170 br_ssl_engine_set_ghash(&cc->eng,
171 &br_ghash_ctmul64);
172 #else
173 br_ssl_engine_set_aes_cbc(&cc->eng,
174 &br_aes_ct_cbcenc_vtable,
175 &br_aes_ct_cbcdec_vtable);
176 br_ssl_engine_set_aes_ctr(&cc->eng,
177 &br_aes_ct_ctr_vtable);
178 br_ssl_engine_set_ghash(&cc->eng,
179 &br_ghash_ctmul);
180 #endif
181 br_ssl_engine_set_des_cbc(&cc->eng,
182 &br_des_ct_cbcenc_vtable,
183 &br_des_ct_cbcdec_vtable);
184
185 /*
186 * Set the SSL record engines (CBC, GCM).
187 */
188 br_ssl_engine_set_cbc(&cc->eng,
189 &br_sslrec_in_cbc_vtable,
190 &br_sslrec_out_cbc_vtable);
191 br_ssl_engine_set_gcm(&cc->eng,
192 &br_sslrec_in_gcm_vtable,
193 &br_sslrec_out_gcm_vtable);
194 }