xref: /freebsd/contrib/bearssl/src/ssl/ssl_server_full_ec.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 CCM and CBC. CCM is better than CBC.
43 	 * -- CCM is better than CCM_8.
44 	 * -- AES-128 is preferred over AES-256 (AES-128 is already
45 	 *    strong enough, and AES-256 is 40% more expensive).
46 	 *
47 	 * Note that for ECDH suites, the list will be automatically
48 	 * filtered based on the issuing CA key type.
49 	 */
50 	static const uint16_t suites[] = {
51 		BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
52 		BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
53 		BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
54 		BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
55 		BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM,
56 		BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
57 		BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8,
58 		BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
59 		BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
60 		BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
61 		BR_TLS_ECDHE_ECDSA_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_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
75 		BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
76 		BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
77 	};
78 
79 	/*
80 	 * All hash functions are activated.
81 	 * Note: the X.509 validation engine will nonetheless refuse to
82 	 * validate signatures that use MD5 as hash function.
83 	 */
84 	static const br_hash_class *hashes[] = {
85 		&br_md5_vtable,
86 		&br_sha1_vtable,
87 		&br_sha224_vtable,
88 		&br_sha256_vtable,
89 		&br_sha384_vtable,
90 		&br_sha512_vtable
91 	};
92 
93 	int id;
94 
95 	/*
96 	 * Reset server context and set supported versions from TLS-1.0
97 	 * to TLS-1.2 (inclusive).
98 	 */
99 	br_ssl_server_zero(cc);
100 	br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
101 
102 	/*
103 	 * Set suites and elliptic curve implementation (for ECDHE).
104 	 */
105 	br_ssl_engine_set_suites(&cc->eng, suites,
106 		(sizeof suites) / (sizeof suites[0]));
107 	br_ssl_engine_set_default_ec(&cc->eng);
108 
109 	/*
110 	 * Set the "server policy": handler for the certificate chain
111 	 * and private key operations.
112 	 */
113 	br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
114 		BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
115 		cert_issuer_key_type,
116 		br_ssl_engine_get_ec(&cc->eng),
117 #if BR_LOMUL
118 		br_ecdsa_i15_sign_asn1
119 #else
120 		br_ecdsa_i31_sign_asn1
121 #endif
122 	);
123 
124 	/*
125 	 * Set supported hash functions.
126 	 */
127 	for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
128 		const br_hash_class *hc;
129 
130 		hc = hashes[id - 1];
131 		br_ssl_engine_set_hash(&cc->eng, id, hc);
132 	}
133 
134 	/*
135 	 * Set the PRF implementations.
136 	 */
137 	br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
138 	br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
139 	br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
140 
141 	/*
142 	 * Symmetric encryption.
143 	 */
144 	br_ssl_engine_set_default_aes_cbc(&cc->eng);
145 	br_ssl_engine_set_default_aes_ccm(&cc->eng);
146 	br_ssl_engine_set_default_aes_gcm(&cc->eng);
147 	br_ssl_engine_set_default_des_cbc(&cc->eng);
148 	br_ssl_engine_set_default_chapol(&cc->eng);
149 }
150