Added API for external hashing of ServerKeyExchange, and signature algorithm identifi...
[BearSSL] / src / ssl / ssl_server_full_ec.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_server_init_full_ec(br_ssl_server_context *cc,
30 const br_x509_certificate *chain, size_t chain_len,
31 unsigned cert_issuer_key_type, const br_ec_private_key *sk)
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 * -- ChaCha20+Poly1305 is better than AES/GCM (faster, smaller).
42 * -- GCM is better than CBC.
43 * -- AES-128 is preferred over AES-256 (AES-128 is already
44 * strong enough, and AES-256 is 40% more expensive).
45 *
46 * Note that for ECDH suites, the list will be automatically
47 * filtered based on the issuing CA key type.
48 */
49 static const uint16_t suites[] = {
50 BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
51 BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
52 BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
53 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
54 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
55 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
56 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
57 BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
58 BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
59 BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
60 BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
61 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
62 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
63 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
64 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
65 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
66 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
67 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
68 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
69 BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
70 BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
71 BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
72 };
73
74 /*
75 * All hash functions are activated.
76 * Note: the X.509 validation engine will nonetheless refuse to
77 * validate signatures that use MD5 as hash function.
78 */
79 static const br_hash_class *hashes[] = {
80 &br_md5_vtable,
81 &br_sha1_vtable,
82 &br_sha224_vtable,
83 &br_sha256_vtable,
84 &br_sha384_vtable,
85 &br_sha512_vtable
86 };
87
88 int id;
89
90 /*
91 * Reset server context and set supported versions from TLS-1.0
92 * to TLS-1.2 (inclusive).
93 */
94 br_ssl_server_zero(cc);
95 br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
96
97 /*
98 * Set suites and elliptic curve implementation (for ECDHE).
99 */
100 br_ssl_engine_set_suites(&cc->eng, suites,
101 (sizeof suites) / (sizeof suites[0]));
102 br_ssl_engine_set_ec(&cc->eng, &br_ec_prime_i31);
103
104 /*
105 * Set the "server policy": handler for the certificate chain
106 * and private key operations.
107 */
108 br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
109 BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
110 cert_issuer_key_type,
111 &br_ec_prime_i31, br_ecdsa_i31_sign_asn1);
112
113 /*
114 * Set supported hash functions.
115 */
116 for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
117 const br_hash_class *hc;
118
119 hc = hashes[id - 1];
120 br_ssl_engine_set_hash(&cc->eng, id, hc);
121 }
122
123 /*
124 * Set the PRF implementations.
125 */
126 br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
127 br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
128 br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
129
130 /*
131 * Symmetric encryption. We use the "constant-time"
132 * implementations, which are the safest.
133 *
134 * On architectures detected as "64-bit", use the 64-bit
135 * versions (aes_ct64, ghash_ctmul64).
136 */
137 #if BR_64
138 br_ssl_engine_set_aes_cbc(&cc->eng,
139 &br_aes_ct64_cbcenc_vtable,
140 &br_aes_ct64_cbcdec_vtable);
141 br_ssl_engine_set_aes_ctr(&cc->eng,
142 &br_aes_ct64_ctr_vtable);
143 br_ssl_engine_set_ghash(&cc->eng,
144 &br_ghash_ctmul64);
145 #else
146 br_ssl_engine_set_aes_cbc(&cc->eng,
147 &br_aes_ct_cbcenc_vtable,
148 &br_aes_ct_cbcdec_vtable);
149 br_ssl_engine_set_aes_ctr(&cc->eng,
150 &br_aes_ct_ctr_vtable);
151 br_ssl_engine_set_ghash(&cc->eng,
152 &br_ghash_ctmul);
153 #endif
154 br_ssl_engine_set_des_cbc(&cc->eng,
155 &br_des_ct_cbcenc_vtable,
156 &br_des_ct_cbcdec_vtable);
157 br_ssl_engine_set_chacha20(&cc->eng,
158 &br_chacha20_ct_run);
159 br_ssl_engine_set_poly1305(&cc->eng,
160 &br_poly1305_ctmul_run);
161
162 /*
163 * Set the SSL record engines (CBC, GCM).
164 */
165 br_ssl_engine_set_cbc(&cc->eng,
166 &br_sslrec_in_cbc_vtable,
167 &br_sslrec_out_cbc_vtable);
168 br_ssl_engine_set_gcm(&cc->eng,
169 &br_sslrec_in_gcm_vtable,
170 &br_sslrec_out_gcm_vtable);
171 br_ssl_engine_set_chapol(&cc->eng,
172 &br_sslrec_in_chapol_vtable,
173 &br_sslrec_out_chapol_vtable);
174 }