xref: /freebsd/contrib/bearssl/src/ssl/ssl_server_full_rsa.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
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
br_ssl_server_init_full_rsa(br_ssl_server_context * cc,const br_x509_certificate * chain,size_t chain_len,const br_rsa_private_key * sk)29 br_ssl_server_init_full_rsa(br_ssl_server_context *cc,
30 	const br_x509_certificate *chain, size_t chain_len,
31 	const br_rsa_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 	static const uint16_t suites[] = {
47 		BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
48 		BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
49 		BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
50 		BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
51 		BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
52 		BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
53 		BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
54 		BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
55 		BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
56 		BR_TLS_RSA_WITH_AES_128_CCM,
57 		BR_TLS_RSA_WITH_AES_256_CCM,
58 		BR_TLS_RSA_WITH_AES_128_CCM_8,
59 		BR_TLS_RSA_WITH_AES_256_CCM_8,
60 		BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
61 		BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
62 		BR_TLS_RSA_WITH_AES_128_CBC_SHA,
63 		BR_TLS_RSA_WITH_AES_256_CBC_SHA,
64 		BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
65 		BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
66 	};
67 
68 	/*
69 	 * All hash functions are activated.
70 	 * Note: the X.509 validation engine will nonetheless refuse to
71 	 * validate signatures that use MD5 as hash function.
72 	 */
73 	static const br_hash_class *hashes[] = {
74 		&br_md5_vtable,
75 		&br_sha1_vtable,
76 		&br_sha224_vtable,
77 		&br_sha256_vtable,
78 		&br_sha384_vtable,
79 		&br_sha512_vtable
80 	};
81 
82 	int id;
83 
84 	/*
85 	 * Reset server context and set supported versions from TLS-1.0
86 	 * to TLS-1.2 (inclusive).
87 	 */
88 	br_ssl_server_zero(cc);
89 	br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
90 
91 	/*
92 	 * Set suites and elliptic curve implementation (for ECDHE).
93 	 */
94 	br_ssl_engine_set_suites(&cc->eng, suites,
95 		(sizeof suites) / (sizeof suites[0]));
96 	br_ssl_engine_set_default_ec(&cc->eng);
97 
98 	/*
99 	 * Set the "server policy": handler for the certificate chain
100 	 * and private key operations.
101 	 */
102 	br_ssl_server_set_single_rsa(cc, chain, chain_len, sk,
103 		BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
104 		br_rsa_private_get_default(),
105 		br_rsa_pkcs1_sign_get_default());
106 
107 	/*
108 	 * Set supported hash functions.
109 	 */
110 	for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
111 		const br_hash_class *hc;
112 
113 		hc = hashes[id - 1];
114 		br_ssl_engine_set_hash(&cc->eng, id, hc);
115 	}
116 
117 	/*
118 	 * Set the PRF implementations.
119 	 */
120 	br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
121 	br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
122 	br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
123 
124 	/*
125 	 * Symmetric encryption.
126 	 */
127 	br_ssl_engine_set_default_aes_cbc(&cc->eng);
128 	br_ssl_engine_set_default_aes_ccm(&cc->eng);
129 	br_ssl_engine_set_default_aes_gcm(&cc->eng);
130 	br_ssl_engine_set_default_des_cbc(&cc->eng);
131 	br_ssl_engine_set_default_chapol(&cc->eng);
132 }
133