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