Debian Codegraph @

crt/s2n/tests/unit/s2n_cipher_suite_match_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <string.h>
17
18 #include "crypto/s2n_ecc_evp.h"
19 #include "crypto/s2n_pq.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22 #include "tls/s2n_cipher_suites.h"
23 #include "tls/s2n_connection.h"
24 #include "tls/s2n_security_policies.h"
25
26 static s2n_result s2n_conn_set_chosen_psk(struct s2n_connection *conn)
27 {
28 RESULT_ENSURE_REF(conn);
29
30 uint8_t psk_identity[] = "psk identity";
31 RESULT_GUARD(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &conn->psk_params.chosen_psk));
32 RESULT_ENSURE_REF(conn->psk_params.chosen_psk);
33 RESULT_GUARD(s2n_psk_init(conn->psk_params.chosen_psk, S2N_PSK_TYPE_EXTERNAL));
34 RESULT_GUARD_POSIX(s2n_psk_set_identity(conn->psk_params.chosen_psk, psk_identity, sizeof(psk_identity)));
35
36 return S2N_RESULT_OK;
37 }
38
39 int main(int argc, char **argv)
40 {
41 BEGIN_TEST();
42 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
43
44 char dhparams_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
45 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem,
46 S2N_MAX_TEST_PEM_SIZE));
47
48 /* Test client cipher selection */
49 {
50 /* Setup connections */
51 struct s2n_connection *conn = NULL;
52 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
53
54 /* Setup config */
55 struct s2n_cert_chain_and_key *chain_and_key = NULL;
56 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
57 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
58 struct s2n_config *config = NULL;
59 EXPECT_NOT_NULL(config = s2n_config_new());
60 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
61 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
62
63 /* Test that the client allows the server to select ciphers that were offered in ClientHello */
64 {
65 conn->client_protocol_version = S2N_TLS13;
66 conn->actual_protocol_version = S2N_TLS13;
67 conn->server_protocol_version = S2N_TLS13;
68
69 /* The client will offer the default tls13 ciphersuites */
70 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
71
72 /* The server will send a TLS13 cipher over the wire */
73 uint8_t valid_wire_ciphers[] = {
74 TLS_AES_128_GCM_SHA256
75 };
76
77 /* We expect to succeed because the cipher was offered by the client */
78 EXPECT_SUCCESS(s2n_set_cipher_as_client(conn, valid_wire_ciphers));
79
80 EXPECT_SUCCESS(s2n_connection_wipe(conn));
81 };
82
83 /* Test that the client rejects a cipher that was not originally offered in ClientHello */
84 {
85 conn->client_protocol_version = S2N_TLS13;
86 conn->actual_protocol_version = S2N_TLS13;
87 conn->server_protocol_version = S2N_TLS13;
88
89 /* The client will offer the default tls13 ciphersuites */
90 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all_tls13"));
91
92 /* The server will send a TLS12 cipher over the wire */
93 uint8_t invalid_wire_ciphers[] = {
94 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
95 };
96
97 /* We expect to fail because the cipher was not offered by the client */
98 EXPECT_FAILURE_WITH_ERRNO(s2n_set_cipher_as_client(conn, invalid_wire_ciphers), S2N_ERR_CIPHER_NOT_SUPPORTED);
99
100 EXPECT_SUCCESS(s2n_connection_wipe(conn));
101 };
102
103 /** Clients MUST verify
104 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.11
105 *= type=test
106 *# that the server selected a cipher suite
107 *# indicating a Hash associated with the PSK
108 **/
109 {
110 /* If chosen PSK is set, test error case for incorrect hash match */
111 {
112 s2n_connection_set_cipher_preferences(conn, "default_tls13");
113
114 EXPECT_OK(s2n_conn_set_chosen_psk(conn));
115
116 uint8_t valid_tls13_wire_ciphers[] = {
117 TLS_AES_128_GCM_SHA256,
118 };
119
120 /* S2N_HMAC_SHA1 is not a matching hmac algorithm */
121 conn->psk_params.chosen_psk->hmac_alg = S2N_HMAC_SHA1;
122 EXPECT_FAILURE_WITH_ERRNO(s2n_set_cipher_as_client(conn, valid_tls13_wire_ciphers),
123 S2N_ERR_CIPHER_NOT_SUPPORTED);
124 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_null_cipher_suite);
125
126 EXPECT_SUCCESS(s2n_connection_wipe(conn));
127 };
128
129 /* If chosen PSK is set, test success case for matching hash algorithm */
130 {
131 s2n_connection_set_cipher_preferences(conn, "default_tls13");
132
133 EXPECT_OK(s2n_conn_set_chosen_psk(conn));
134
135 uint8_t valid_tls13_wire_ciphers[] = {
136 TLS_AES_128_GCM_SHA256,
137 };
138
139 /* S2N_HMAC_SHA256 is a matching hmac algorithm for the cipher suite present in valid_tls13_wire_ciphers */
140 conn->psk_params.chosen_psk->hmac_alg = S2N_HMAC_SHA256;
141 EXPECT_SUCCESS(s2n_set_cipher_as_client(conn, valid_tls13_wire_ciphers));
142 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_tls13_aes_128_gcm_sha256);
143
144 EXPECT_SUCCESS(s2n_connection_wipe(conn));
145 };
146 };
147
148 EXPECT_SUCCESS(s2n_connection_free(conn));
149 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
150 EXPECT_SUCCESS(s2n_config_free(config));
151 };
152
153 /* Test server cipher selection and scsv detection */
154 {
155 struct s2n_connection *conn = NULL;
156 struct s2n_config *server_config = NULL;
157 char *rsa_cert_chain_pem = NULL, *rsa_private_key_pem = NULL, *ecdsa_cert_chain_pem = NULL, *ecdsa_private_key_pem = NULL;
158 struct s2n_cert_chain_and_key *rsa_cert = NULL, *ecdsa_cert = NULL;
159 /* Allocate all of the objects and PEMs we'll need for this test. */
160 EXPECT_NOT_NULL(rsa_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
161 EXPECT_NOT_NULL(rsa_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
162 EXPECT_NOT_NULL(ecdsa_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
163 EXPECT_NOT_NULL(ecdsa_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
164 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
165 EXPECT_NOT_NULL(rsa_cert = s2n_cert_chain_and_key_new());
166 EXPECT_NOT_NULL(ecdsa_cert = s2n_cert_chain_and_key_new());
167 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, rsa_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
168 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, rsa_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
169 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, ecdsa_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
170 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, ecdsa_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
171 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(rsa_cert, rsa_cert_chain_pem, rsa_private_key_pem));
172 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(ecdsa_cert, ecdsa_cert_chain_pem, ecdsa_private_key_pem));
173
174 uint8_t wire_ciphers[] = {
175 TLS_RSA_WITH_RC4_128_MD5,
176 TLS_RSA_WITH_RC4_128_SHA,
177 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
178 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
179 TLS_RSA_WITH_AES_128_CBC_SHA,
180 TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
181 TLS_RSA_WITH_AES_256_CBC_SHA,
182 TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
183 TLS_RSA_WITH_AES_128_CBC_SHA256,
184 TLS_RSA_WITH_AES_256_CBC_SHA256,
185 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
186 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
187 TLS_RSA_WITH_AES_128_GCM_SHA256,
188 TLS_RSA_WITH_AES_256_GCM_SHA384,
189 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
190 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
191 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
192 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
193 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
194 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
195 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
196 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
197 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
198 };
199 const uint8_t cipher_count = sizeof(wire_ciphers) / S2N_TLS_CIPHER_SUITE_LEN;
200
201 uint8_t wire_ciphers_fallback[] = {
202 TLS_RSA_WITH_RC4_128_MD5,
203 TLS_RSA_WITH_RC4_128_SHA,
204 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
205 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
206 TLS_RSA_WITH_AES_128_CBC_SHA,
207 TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
208 TLS_RSA_WITH_AES_256_CBC_SHA,
209 TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
210 TLS_RSA_WITH_AES_128_CBC_SHA256,
211 TLS_RSA_WITH_AES_256_CBC_SHA256,
212 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
213 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
214 TLS_RSA_WITH_AES_128_GCM_SHA256,
215 TLS_RSA_WITH_AES_256_GCM_SHA384,
216 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
217 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
218 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
219 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
220 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
221 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
222 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
223 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
224 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
225 TLS_FALLBACK_SCSV, /* At the end to verify it isn't missed */
226 };
227 const uint8_t cipher_count_fallback = sizeof(wire_ciphers_fallback) / S2N_TLS_CIPHER_SUITE_LEN;
228
229 uint8_t wire_ciphers_renegotiation[] = {
230 TLS_RSA_WITH_RC4_128_MD5,
231 TLS_RSA_WITH_RC4_128_SHA,
232 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
233 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
234 TLS_RSA_WITH_AES_128_CBC_SHA,
235 TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
236 TLS_RSA_WITH_AES_256_CBC_SHA,
237 TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
238 TLS_RSA_WITH_AES_128_CBC_SHA256,
239 TLS_RSA_WITH_AES_256_CBC_SHA256,
240 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
241 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
242 TLS_RSA_WITH_AES_128_GCM_SHA256,
243 TLS_RSA_WITH_AES_256_GCM_SHA384,
244 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
245 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
246 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
247 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
248 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
249 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
250 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
251 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
252 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
253 TLS_EMPTY_RENEGOTIATION_INFO_SCSV, /* At the end to verify it isn't missed */
254 };
255 const uint8_t cipher_count_renegotiation = sizeof(wire_ciphers_renegotiation) / S2N_TLS_CIPHER_SUITE_LEN;
256
257 /* Only two ciphers for testing RSA vs ECDSA. */
258 uint8_t wire_ciphers_with_ecdsa[] = {
259 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
260 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
261 };
262 const uint8_t cipher_count_ecdsa = sizeof(wire_ciphers_with_ecdsa) / S2N_TLS_CIPHER_SUITE_LEN;
263
264 /* Only ECDSA ciphers */
265 uint8_t wire_ciphers_only_ecdsa[] = {
266 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
267 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
268 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
269 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
270 };
271 const uint8_t cipher_count_only_ecdsa = sizeof(wire_ciphers_only_ecdsa) / S2N_TLS_CIPHER_SUITE_LEN;
272
273 uint8_t wire_ciphers_rsa_fallback[] = {
274 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
275 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
276 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
277 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
278 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
279 TLS_RSA_WITH_AES_256_GCM_SHA384,
280 };
281 const uint8_t cipher_count_rsa_fallback = sizeof(wire_ciphers_rsa_fallback) / S2N_TLS_CIPHER_SUITE_LEN;
282
283 EXPECT_NOT_NULL(server_config = s2n_config_new());
284 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, rsa_cert));
285 /* Security policy must allow all test cipher suites */
286 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "test_all"));
287 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
288
289 /* TEST RSA */
290 conn->actual_protocol_version = S2N_TLS10;
291 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers, cipher_count));
292 EXPECT_EQUAL(conn->secure_renegotiation, 0);
293 EXPECT_EQUAL(1, s2n_connection_is_valid_for_cipher_preferences(conn, "test_all"));
294 EXPECT_EQUAL(0, s2n_connection_is_valid_for_cipher_preferences(conn, "null"));
295 EXPECT_SUCCESS(s2n_connection_wipe(conn));
296
297 /* TEST RENEGOTIATION
298 *
299 *= https://www.rfc-editor.org/rfc/rfc5746#3.6
300 *= type=test
301 *# o When a ClientHello is received, the server MUST check if it
302 *# includes the TLS_EMPTY_RENEGOTIATION_INFO_SCSV SCSV. If it does,
303 *# set the secure_renegotiation flag to TRUE.
304 */
305 conn->actual_protocol_version = S2N_TLS12;
306 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_renegotiation, cipher_count_renegotiation));
307 EXPECT_EQUAL(conn->secure_renegotiation, 1);
308 EXPECT_EQUAL(1, s2n_connection_is_valid_for_cipher_preferences(conn, "test_all"));
309 EXPECT_EQUAL(-1, s2n_connection_is_valid_for_cipher_preferences(conn, "not_exist"));
310 EXPECT_SUCCESS(s2n_connection_wipe(conn));
311
312 /* Simulate a TLSv11 client to trigger the fallback error */
313 conn->actual_protocol_version = S2N_TLS11;
314 EXPECT_FAILURE(s2n_set_cipher_as_tls_server(conn, wire_ciphers_fallback, cipher_count_fallback));
315 EXPECT_EQUAL(conn->secure_renegotiation, 0);
316 EXPECT_EQUAL(1, s2n_connection_is_valid_for_cipher_preferences(conn, "null"));
317 EXPECT_EQUAL(0, s2n_connection_is_valid_for_cipher_preferences(conn, "CloudFront-TLS-1-2-2018"));
318 EXPECT_EQUAL(0, s2n_connection_is_valid_for_cipher_preferences(conn, "CloudFront-TLS-1-2-2019"));
319 EXPECT_SUCCESS(s2n_connection_wipe(conn));
320
321 /* TEST RSA cipher chosen when ECDSA cipher is at top */
322 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_ecdsa_priority"));
323
324 const struct s2n_ecc_preferences *ecc_pref = NULL;
325 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
326 EXPECT_NOT_NULL(ecc_pref);
327
328 /* Assume default for negotiated curve. */
329 /* Shouldn't be necessary unless the test fails, but we want the failure to be obvious. */
330 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
331 conn->actual_protocol_version = conn->server_protocol_version;
332 const struct s2n_cipher_suite *expected_rsa_wire_choice = &s2n_ecdhe_rsa_with_aes_128_cbc_sha;
333 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
334 EXPECT_EQUAL(conn->secure_renegotiation, 0);
335 EXPECT_EQUAL(conn->secure->cipher_suite, expected_rsa_wire_choice);
336 EXPECT_SUCCESS(s2n_connection_wipe(conn));
337
338 /* Clean+free to setup for ECDSA tests */
339 EXPECT_SUCCESS(s2n_config_free(server_config));
340
341 /* Set ECDSA CERT in s2n_config */
342 EXPECT_NOT_NULL(server_config = s2n_config_new());
343 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_cert));
344 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
345
346 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
347 EXPECT_NOT_NULL(ecc_pref);
348
349 /* TEST ECDSA */
350 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all_ecdsa"));
351 const struct s2n_cipher_suite *expected_ecdsa_wire_choice = &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256;
352 /* Assume default for negotiated curve. */
353 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
354 conn->actual_protocol_version = conn->server_protocol_version;
355 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
356 EXPECT_EQUAL(conn->secure_renegotiation, 0);
357 EXPECT_EQUAL(conn->secure->cipher_suite, expected_ecdsa_wire_choice);
358 EXPECT_SUCCESS(s2n_connection_wipe(conn));
359
360 /* TEST ECDSA cipher chosen when RSA cipher is at top */
361 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
362 /* Assume default for negotiated curve. */
363 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
364 conn->actual_protocol_version = conn->server_protocol_version;
365 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
366 EXPECT_EQUAL(conn->secure_renegotiation, 0);
367 EXPECT_EQUAL(conn->secure->cipher_suite, expected_ecdsa_wire_choice);
368 EXPECT_SUCCESS(s2n_connection_wipe(conn));
369 EXPECT_SUCCESS(s2n_config_free(server_config));
370
371 /* TEST two certificates. Use two certs with different key types(RSA, ECDSA) and add them to a single
372 * s2n_config.
373 */
374 EXPECT_NOT_NULL(server_config = s2n_config_new());
375 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, rsa_cert));
376 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_cert));
377 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
378
379 /* Client sends RSA and ECDSA ciphers, server prioritizes ECDSA, ECDSA + RSA cert is configured */
380 {
381 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256;
382 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_ecdsa_priority"));
383 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
384 conn->actual_protocol_version = conn->server_protocol_version;
385 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
386 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
387 EXPECT_EQUAL(conn->secure_renegotiation, 0);
388 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
389 EXPECT_SUCCESS(s2n_connection_wipe(conn));
390 };
391
392 /* Client sends RSA and ECDSA ciphers, server prioritizes RSA, ECDSA + RSA cert is configured */
393 {
394 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_rsa_with_aes_128_cbc_sha;
395 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
396 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
397 conn->actual_protocol_version = conn->server_protocol_version;
398 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
399 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
400 EXPECT_EQUAL(conn->secure_renegotiation, 0);
401 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
402 EXPECT_SUCCESS(s2n_connection_wipe(conn));
403 };
404
405 /* Client sends both RSA and ECDSA ciphers, server only configures RSA ciphers,
406 * ECDSA + RSA cert is configured.
407 */
408 {
409 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_rsa_with_aes_128_cbc_sha;
410 /* 20170328 only supports RSA ciphers */
411 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "20170328"));
412 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
413 conn->actual_protocol_version = conn->server_protocol_version;
414 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
415 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
416 EXPECT_EQUAL(conn->secure_renegotiation, 0);
417 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
418 EXPECT_SUCCESS(s2n_connection_wipe(conn));
419 };
420
421 /* Client sends both RSA and ECDSA ciphers, server only configures ECDSA ciphers, ECDSA + RSA cert is
422 * configured.
423 */
424 {
425 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256;
426 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all_ecdsa"));
427 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
428 conn->actual_protocol_version = conn->server_protocol_version;
429 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
430 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
431 EXPECT_EQUAL(conn->secure_renegotiation, 0);
432 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
433 EXPECT_SUCCESS(s2n_connection_wipe(conn));
434 };
435
436 /* Client only sends RSA ciphers, server prioritizes ECDSA ciphers, ECDSA + RSA cert is
437 * configured.
438 */
439 {
440 const struct s2n_cipher_suite *expected_wire_choice = &s2n_rsa_with_rc4_128_md5;
441 if (!expected_wire_choice->available) {
442 expected_wire_choice = &s2n_rsa_with_3des_ede_cbc_sha;
443 }
444 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_ecdsa_priority"));
445 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
446 conn->actual_protocol_version = conn->server_protocol_version;
447 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
448 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers, cipher_count));
449 EXPECT_EQUAL(conn->secure_renegotiation, 0);
450 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
451 EXPECT_SUCCESS(s2n_connection_wipe(conn));
452 };
453
454 /* Client only sends ECDSA ciphers, server prioritizes ECDSA ciphers, ECDSA + RSA cert is
455 * configured.
456 */
457 {
458 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha;
459 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_ecdsa_priority"));
460 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
461 conn->actual_protocol_version = conn->server_protocol_version;
462 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
463 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_only_ecdsa, cipher_count_only_ecdsa));
464 EXPECT_EQUAL(conn->secure_renegotiation, 0);
465 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
466 EXPECT_SUCCESS(s2n_connection_wipe(conn));
467 };
468
469 /* Client sends ECDHE-ECDSA, RSA, ECDHE-RSA ciphers. Server prioritizes ECDSA but also supports RSA.
470 * No mutually supported elliptic curves between client and server. ECDSA + RSA cert is configured.
471 */
472 {
473 /* If there are no shared elliptic curves, we must fall through to a cipher that supports RSA kx.
474 * This is the first RSA kx cipher that CloudFront-Upstream supports.
475 */
476 const struct s2n_cipher_suite *expected_wire_choice = &s2n_rsa_with_aes_256_gcm_sha384;
477 /* Selecting this preference list because it prioritizes ECDHE-ECDSA and ECDHE-RSA over plain RSA kx. */
478 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "CloudFront-Upstream"));
479 /* No shared curve */
480 conn->kex_params.server_ecc_evp_params.negotiated_curve = NULL;
481 conn->actual_protocol_version = conn->server_protocol_version;
482 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
483 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_rsa_fallback, cipher_count_rsa_fallback));
484 EXPECT_EQUAL(conn->secure_renegotiation, 0);
485 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
486 EXPECT_SUCCESS(s2n_connection_wipe(conn));
487 };
488 EXPECT_SUCCESS(s2n_config_free(server_config));
489
490 EXPECT_NOT_NULL(server_config = s2n_config_new());
491 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, rsa_cert));
492 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_cert));
493 /* Override auto-chosen defaults with only RSA cert default. ECDSA still loaded, but not default. */
494 EXPECT_SUCCESS(s2n_config_set_cert_chain_and_key_defaults(server_config, &rsa_cert, 1));
495 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
496
497 /* Client sends RSA and ECDSA ciphers, server prioritizes ECDSA, ECDSA + RSA cert is configured,
498 * only RSA is default. Expect default RSA used instead of previous test that expects ECDSA for this case. */
499 {
500 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_rsa_with_aes_128_cbc_sha;
501 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_ecdsa_priority"));
502 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
503 conn->actual_protocol_version = conn->server_protocol_version;
504 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
505 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
506 EXPECT_EQUAL(conn->secure_renegotiation, 0);
507 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
508 EXPECT_SUCCESS(s2n_connection_wipe(conn));
509 };
510
511 /* Override auto-chosen defaults with only ECDSA cert default. RSA still loaded, but not default. */
512 EXPECT_SUCCESS(s2n_config_set_cert_chain_and_key_defaults(server_config, &ecdsa_cert, 1));
513
514 /* Client sends RSA and ECDSA ciphers, server prioritizes RSA, ECDSA + RSA cert is configured,
515 * only ECDSA is default. Expect default ECDSA used instead of previous test that expects RSA for this case. */
516 {
517 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256;
518 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
519 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
520 conn->actual_protocol_version = conn->server_protocol_version;
521 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
522 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
523 EXPECT_EQUAL(conn->secure_renegotiation, 0);
524 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
525 EXPECT_SUCCESS(s2n_connection_wipe(conn));
526 };
527
528 /* Test override back to both RSA and ECDSA defaults. */
529 struct s2n_cert_chain_and_key *certs_list[] = { rsa_cert, ecdsa_cert };
530 EXPECT_SUCCESS(s2n_config_set_cert_chain_and_key_defaults(server_config, certs_list, 2));
531
532 /* Client sends RSA and ECDSA ciphers, server prioritizes ECDSA, ECDSA + RSA cert is configured */
533 {
534 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256;
535 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_ecdsa_priority"));
536 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
537 conn->actual_protocol_version = conn->server_protocol_version;
538 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
539 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
540 EXPECT_EQUAL(conn->secure_renegotiation, 0);
541 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
542 EXPECT_SUCCESS(s2n_connection_wipe(conn));
543 };
544
545 /* Test that defaults are not overriden after failures to set new default certificates */
546 EXPECT_FAILURE_WITH_ERRNO_NO_RESET(s2n_config_set_cert_chain_and_key_defaults(server_config, NULL, 0), S2N_ERR_NULL);
547 EXPECT_EQUAL(strcmp(s2n_strerror_name(s2n_errno), "S2N_ERR_NULL"), 0);
548 EXPECT_FAILURE_WITH_ERRNO_NO_RESET(s2n_config_set_cert_chain_and_key_defaults(server_config, &rsa_cert, 0),
549 S2N_ERR_NUM_DEFAULT_CERTIFICATES);
550 EXPECT_EQUAL(strcmp(s2n_strerror_name(s2n_errno), "S2N_ERR_NUM_DEFAULT_CERTIFICATES"), 0);
551 struct s2n_cert_chain_and_key *rsa_certs_list[] = { rsa_cert, rsa_cert };
552 EXPECT_FAILURE_WITH_ERRNO_NO_RESET(s2n_config_set_cert_chain_and_key_defaults(server_config, rsa_certs_list, 2),
553 S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE);
554 EXPECT_EQUAL(strcmp(s2n_strerror_name(s2n_errno), "S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE"), 0);
555
556 /* Client sends RSA and ECDSA ciphers, server prioritizes RSA, ECDSA + RSA cert is configured.
557 * RSA default certificate should be chosen. */
558 {
559 const struct s2n_cipher_suite *expected_wire_choice = &s2n_ecdhe_rsa_with_aes_128_cbc_sha;
560 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
561 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
562 conn->actual_protocol_version = conn->server_protocol_version;
563 EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
564 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_ecdsa, cipher_count_ecdsa));
565 EXPECT_EQUAL(conn->secure_renegotiation, 0);
566 EXPECT_EQUAL(conn->secure->cipher_suite, expected_wire_choice);
567 EXPECT_SUCCESS(s2n_connection_wipe(conn));
568 };
569
570 struct s2n_cipher_suite *tls12_cipher_suite = cipher_preferences_20170210.suites[cipher_preferences_20170210.count - 1];
571 uint8_t wire_ciphers_with_tls13[] = {
572 TLS_AES_128_GCM_SHA256,
573 TLS_AES_256_GCM_SHA384,
574 TLS_CHACHA20_POLY1305_SHA256,
575 tls12_cipher_suite->iana_value[0], tls12_cipher_suite->iana_value[1]
576 };
577 const uint8_t cipher_count_tls13 = sizeof(wire_ciphers_with_tls13) / S2N_TLS_CIPHER_SUITE_LEN;
578
579 /* Client sends TLS1.3 cipher suites, but server does not support TLS1.3 */
580 {
581 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
582 conn->client_protocol_version = S2N_TLS13;
583 conn->actual_protocol_version = S2N_TLS12;
584 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_tls13, cipher_count_tls13));
585 EXPECT_EQUAL(conn->secure->cipher_suite, tls12_cipher_suite);
586 EXPECT_SUCCESS(s2n_connection_wipe(conn));
587 };
588
589 /* Client sends TLS1.3 cipher suites, server selects correct TLS1.3 ciphersuite */
590 if (s2n_is_tls13_fully_supported()) {
591 struct test_case {
592 const char *cipher_pref;
593 const struct s2n_cipher_suite *expected_cipher_wire;
594 };
595
596 struct test_case test_cases[] = {
597 { .cipher_pref = "default_tls13", .expected_cipher_wire = &s2n_tls13_aes_128_gcm_sha256 },
598 { .cipher_pref = "test_all", .expected_cipher_wire = &s2n_tls13_aes_128_gcm_sha256 },
599 { .cipher_pref = "test_all_tls13", .expected_cipher_wire = &s2n_tls13_aes_128_gcm_sha256 },
600 };
601
602 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
603 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, test_cases[i].cipher_pref));
604 conn->client_protocol_version = S2N_TLS13;
605 conn->actual_protocol_version = S2N_TLS13;
606 conn->server_protocol_version = S2N_TLS13;
607 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_tls13, cipher_count_tls13));
608 EXPECT_EQUAL(conn->secure->cipher_suite, test_cases[i].expected_cipher_wire);
609 EXPECT_SUCCESS(s2n_connection_wipe(conn));
610 }
611 }
612
613 /* Check wire's cipher suites with preferred tls12 ordering does not affect tls13 selection */
614 {
615 uint8_t wire_ciphers2[] = {
616 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
617 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
618 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* tls 1.2 */
619 TLS_CHACHA20_POLY1305_SHA256, /* tls 1.3 */
620 };
621
622 const uint8_t count = sizeof(wire_ciphers2) / S2N_TLS_CIPHER_SUITE_LEN;
623 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
624 conn->client_protocol_version = S2N_TLS13;
625 conn->actual_protocol_version = S2N_TLS13;
626 conn->server_protocol_version = S2N_TLS13;
627
628 if (s2n_chacha20_poly1305.is_available()) {
629 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers2, count));
630 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_tls13_chacha20_poly1305_sha256);
631 } else {
632 EXPECT_FAILURE(s2n_set_cipher_as_tls_server(conn, wire_ciphers2, count));
633 }
634 EXPECT_SUCCESS(s2n_connection_wipe(conn));
635 };
636
637 /* Test cipher suite with a required version higher than what connection supports should not be selected */
638 {
639 uint8_t test_wire_ciphers[] = {
640 TLS_AES_128_GCM_SHA256, /* tls 1.3 */
641 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* tls 1.2 */
642 };
643
644 const uint8_t count = sizeof(test_wire_ciphers) / S2N_TLS_CIPHER_SUITE_LEN;
645 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
646 conn->actual_protocol_version = S2N_TLS12;
647 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
648
649 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, test_wire_ciphers, count));
650 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_ecdhe_rsa_with_aes_128_gcm_sha256);
651 EXPECT_SUCCESS(s2n_connection_wipe(conn));
652 };
653
654 /* We should skip cipher suites with a minimum protocol version unsupported by the connection.
655 * If no valid cipher suite is found, we should fall back to a cipher suite with a higher protocol version,
656 * but we should NEVER use a TLS1.3 suite on a pre-TLS1.3 connection or vice versa. */
657 {
658 /* Skip but fall back to cipher suite with protocol version higher than connection */
659 {
660 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
661 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
662
663 uint8_t test_wire_ciphers[] = {
664 TLS_AES_128_GCM_SHA256, /* tls 1.3 */
665 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* tls 1.2 */
666 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, /* ssl v3 */
667 };
668
669 conn->actual_protocol_version = S2N_TLS10;
670
671 /* If a match exists, skip the invalid cipher and choose it */
672 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, test_wire_ciphers, 3));
673 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_ecdhe_rsa_with_aes_128_cbc_sha);
674
675 /* If a match does not exist, choose the invalid cipher */
676 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, test_wire_ciphers, 2));
677 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_ecdhe_rsa_with_aes_128_gcm_sha256);
678
679 EXPECT_SUCCESS(s2n_connection_wipe(conn));
680 };
681
682 /* Skip and do NOT fall back to a TLS1.3 cipher suite if using TLS1.2 */
683 {
684 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
685 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
686
687 uint8_t test_wire_ciphers[] = {
688 TLS_AES_128_GCM_SHA256, /* tls 1.3 */
689 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* tls 1.2 */
690 };
691
692 conn->actual_protocol_version = S2N_TLS12;
693
694 /* If a match exists, skip the invalid cipher and choose it */
695 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, test_wire_ciphers, 2));
696 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_ecdhe_rsa_with_aes_128_gcm_sha256);
697
698 /* If a match does not exist, fail to negotiate a cipher suite.
699 * We cannot fall back to the TLS1.3 choice. */
700 EXPECT_FAILURE_WITH_ERRNO(s2n_set_cipher_as_tls_server(conn, test_wire_ciphers, 1),
701 S2N_ERR_CIPHER_NOT_SUPPORTED);
702
703 EXPECT_SUCCESS(s2n_connection_wipe(conn));
704 }
705
706 /* Skip and do NOT fall back to a TLS1.2 cipher suite if using TLS1.3 */
707 {
708 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all"));
709 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
710
711 uint8_t test_wire_ciphers[] = {
712 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* tls 1.2 */
713 TLS_AES_128_GCM_SHA256, /* tls 1.3 */
714 };
715
716 conn->actual_protocol_version = S2N_TLS13;
717
718 /* If a match exists, skip the invalid cipher and choose it */
719 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, test_wire_ciphers, 2));
720 EXPECT_EQUAL(conn->secure->cipher_suite, &s2n_tls13_aes_128_gcm_sha256);
721
722 /* If a match does not exist, fail to negotiate a cipher suite.
723 * We cannot fall back to the TLS1.2 choice. */
724 EXPECT_FAILURE_WITH_ERRNO(s2n_set_cipher_as_tls_server(conn, test_wire_ciphers, 1),
725 S2N_ERR_CIPHER_NOT_SUPPORTED);
726
727 EXPECT_SUCCESS(s2n_connection_wipe(conn));
728 }
729 };
730
731 /* If a PSK is being used, then the cipher suite must match the PSK's HMAC algorithm.
732 *
733 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.11
734 *= type=test
735 *# The server MUST ensure that it selects a compatible PSK
736 *# (if any) and cipher suite.
737 **/
738 {
739 /* If chosen PSK is set, a cipher suite with matching HMAC algorithm must be selected */
740 {
741 s2n_connection_set_cipher_preferences(conn, "test_all");
742 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
743 conn->actual_protocol_version = S2N_TLS13;
744
745 EXPECT_OK(s2n_conn_set_chosen_psk(conn));
746
747 conn->psk_params.chosen_psk->hmac_alg = S2N_HMAC_SHA256;
748 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_tls13, cipher_count_tls13));
749 EXPECT_EQUAL(conn->secure->cipher_suite->prf_alg, conn->psk_params.chosen_psk->hmac_alg);
750
751 conn->psk_params.chosen_psk->hmac_alg = S2N_HMAC_SHA384;
752 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_tls13, cipher_count_tls13));
753 EXPECT_EQUAL(conn->secure->cipher_suite->prf_alg, conn->psk_params.chosen_psk->hmac_alg);
754
755 EXPECT_SUCCESS(s2n_connection_wipe(conn));
756 };
757
758 /* If chosen PSK is set but there is no matching cipher, the server MUST fail to set a cipher */
759 {
760 s2n_connection_set_cipher_preferences(conn, "test_all");
761 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
762 conn->actual_protocol_version = S2N_TLS13;
763
764 /* S2N_HMAC_SHA1 is not a matching HMAC algorithm for any TLS1.3 cipher */
765 EXPECT_OK(s2n_conn_set_chosen_psk(conn));
766 conn->psk_params.chosen_psk->hmac_alg = S2N_HMAC_SHA1;
767
768 EXPECT_FAILURE_WITH_ERRNO(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_tls13, cipher_count_tls13),
769 S2N_ERR_CIPHER_NOT_SUPPORTED);
770
771 EXPECT_SUCCESS(s2n_connection_wipe(conn));
772 };
773 };
774
775 /* Client sends cipher which is not in the configured suite */
776 {
777 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
778 uint8_t invalid_cipher_pref[] = {
779 TLS_NULL_WITH_NULL_NULL
780 };
781
782 const uint8_t invalid_cipher_count = sizeof(invalid_cipher_pref) / S2N_TLS_CIPHER_SUITE_LEN;
783 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
784 conn->client_protocol_version = S2N_TLS13;
785 conn->actual_protocol_version = S2N_TLS13;
786 EXPECT_FAILURE_WITH_ERRNO(s2n_set_cipher_as_tls_server(conn, invalid_cipher_pref, invalid_cipher_count), S2N_ERR_CIPHER_NOT_SUPPORTED);
787 EXPECT_SUCCESS(s2n_connection_wipe(conn));
788 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
789 };
790
791 /* Client sends cipher that requires DH params */
792 {
793 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
794 s2n_config_ptr_free);
795 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
796 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, rsa_cert));
797
798 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
799 s2n_connection_ptr_free);
800 EXPECT_NOT_NULL(server);
801 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
802
803 /* The client only offers one cipher suite, which requires dh kex */
804 uint8_t wire[] = { TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 };
805
806 /* By default, the server does not accept cipher suites with dh kex. */
807 EXPECT_FAILURE_WITH_ERRNO(s2n_set_cipher_as_tls_server(server, wire, 1),
808 S2N_ERR_CIPHER_NOT_SUPPORTED);
809
810 /* With dh params configured, the server accepts cipher suites with dh kex. */
811 EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams_pem));
812 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(server, wire, 1));
813 EXPECT_EQUAL(server->secure->cipher_suite, &s2n_dhe_rsa_with_aes_128_gcm_sha256);
814 };
815
816 EXPECT_SUCCESS(s2n_config_free(server_config));
817 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(rsa_cert));
818 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_cert));
819 free(ecdsa_cert_chain_pem);
820 free(ecdsa_private_key_pem);
821 free(rsa_cert_chain_pem);
822 free(rsa_private_key_pem);
823 EXPECT_SUCCESS(s2n_connection_free(conn));
824 };
825
826 /* Test chacha20 boosting behaviour */
827 {
828 /* Setup cipher preferences + security policy */
829 struct s2n_cipher_preferences cipher_preferences = { 0 };
830 struct s2n_security_policy security_policy = {
831 .minimum_protocol_version = S2N_SSLv2,
832 .cipher_preferences = &cipher_preferences,
833 .kem_preferences = &kem_preferences_null,
834 .signature_preferences = &s2n_signature_preferences_20201021,
835 .ecc_preferences = &s2n_ecc_preferences_test_all,
836 };
837
838 /* Initialise config and relevant certs */
839 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
840 struct s2n_cert_chain_and_key *rsa_cert = NULL;
841 struct s2n_cert_chain_and_key *ecdsa_cert = NULL;
842 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&rsa_cert,
843 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
844 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_cert,
845 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
846
847 EXPECT_NOT_NULL(rsa_cert);
848 EXPECT_NOT_NULL(ecdsa_cert);
849 EXPECT_NOT_NULL(server_config);
850 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, rsa_cert));
851 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_cert));
852 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "test_all"));
853
854 if (s2n_chacha20_poly1305.is_available()) {
855 /* Test chacha20 boosting when ciphersuites fail auth validation */
856 {
857 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
858 EXPECT_NOT_NULL(connection);
859 connection->security_policy_override = &security_policy;
860 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
861 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS12));
862 DEFER_CLEANUP(struct s2n_config *rsa_only_config = s2n_config_new(), s2n_config_ptr_free);
863 EXPECT_NOT_NULL(rsa_only_config);
864 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(rsa_only_config, rsa_cert));
865 /* Connection only supports rsa auth. */
866 EXPECT_SUCCESS(s2n_connection_set_config(connection, rsa_only_config));
867
868 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
869 /* Not negotiated because invalid (ecdsa) */
870 &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256,
871 /* Only negotiated if chacha20 boosting is disabled */
872 &s2n_ecdhe_rsa_with_aes_256_gcm_sha384,
873 /* First valid chacha20 cipher suite and is negotiated */
874 &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256,
875 };
876
877 cipher_preferences = (struct s2n_cipher_preferences){
878 .count = s2n_array_len(test_cipher_suite_list),
879 .suites = test_cipher_suite_list,
880 .allow_chacha20_boosting = true,
881 };
882
883 uint8_t wire[] = {
884 /* Client signalled chacha20 boosting; valid chacha20 ciphersuite and is negotiated */
885 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
886 /* Not negotiated because invalid (ecdsa) */
887 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
888 /* Negotiated if chacha20 boosting is off */
889 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
890 };
891 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
892
893 /* Verify chacha20 RSA ciphersuite chosen with chacha20 boosting enabled */
894 cipher_preferences.allow_chacha20_boosting = true;
895 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
896 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256);
897
898 /* Sanity check: non-chacha20 RSA ciphersuite chosen without chacha20 boosting enabled */
899 /* cppcheck-suppress redundantAssignment */
900 cipher_preferences.allow_chacha20_boosting = false;
901 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
902 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_rsa_with_aes_256_gcm_sha384);
903 };
904
905 /* Server is able to negotiate its most preferred chacha20 ciphersuite */
906 {
907 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
908 EXPECT_NOT_NULL(connection);
909 connection->security_policy_override = &security_policy;
910 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
911 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS12));
912 EXPECT_SUCCESS(s2n_connection_set_config(connection, server_config));
913
914 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
915 /* Skipped because it is not a chacha20 ciphersuite. Is negotiated if chacha20 boosting is disabled. */
916 &s2n_ecdhe_rsa_with_aes_256_cbc_sha384,
917 /* First chacha20 ciphersuite and is negotiated */
918 &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256,
919 /* Second chacha20 ciphersuite and is not negotiated (not server's most preferred chacha20 ciphersuite) */
920 &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256,
921 };
922
923 cipher_preferences = (struct s2n_cipher_preferences){
924 .count = s2n_array_len(test_cipher_suite_list),
925 .suites = test_cipher_suite_list,
926 .allow_chacha20_boosting = true
927 };
928
929 uint8_t wire[] = {
930 /* Client signalled chacha20 boosting: not negotiated because it's not server's most preferred chacha20 ciphersuite */
931 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
932 /* Negotiated if chacha20 boosting is on */
933 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
934 /* Negotiated if chacha20 boosting is off */
935 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
936 };
937 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
938
939 /* Verify most preferred chacha20 ciphersuite is chosen with chacha20 boosting enabled */
940 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
941 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256);
942
943 /* Sanity check: server's most preferred cipehrsuite is chosen when chacha20 boosting is off */
944 cipher_preferences.allow_chacha20_boosting = false;
945 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
946 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_rsa_with_aes_256_cbc_sha384);
947 };
948
949 /* Server's most preferred chacha20 is not offered by the client */
950 {
951 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
952 EXPECT_NOT_NULL(connection);
953 connection->security_policy_override = &security_policy;
954 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
955 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS12));
956 EXPECT_SUCCESS(s2n_connection_set_config(connection, server_config));
957
958 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
959 /* Server's most preferred chacha20 ciphersuite; not offered by the client */
960 &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256,
961 /* Negotiated if chacha20 boosting is off */
962 &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256,
963 /* First valid chacha20 ciphersuite; negotiated if chacha20 boosting is on */
964 &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256,
965 };
966
967 uint8_t wire[] = {
968 /* Client signalled chacha20 boosting; negotiated if chacha20 boosting is on */
969 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
970 /* Negotiated if chacha20 boosting is off */
971 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
972 };
973 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
974
975 cipher_preferences = (struct s2n_cipher_preferences){
976 .count = s2n_array_len(test_cipher_suite_list),
977 .suites = test_cipher_suite_list,
978 .allow_chacha20_boosting = true,
979 };
980
981 /* Verify server selects its second most preferred chacha20 ciphersuite */
982 cipher_preferences.allow_chacha20_boosting = true;
983 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
984 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256);
985
986 /* Sanity check: server's most preferred ciphersuite is chosen without chacha20 boosting enabled */
987 /* cppcheck-suppress redundantAssignment */
988 cipher_preferences.allow_chacha20_boosting = false;
989 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
990 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256);
991 };
992
993 /* Server does not negotiate the client's most preferred chacha20 ciphersuite */
994 {
995 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
996 EXPECT_NOT_NULL(connection);
997 connection->security_policy_override = &security_policy;
998 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
999 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS12));
1000 EXPECT_SUCCESS(s2n_connection_set_config(connection, server_config));
1001
1002 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1003 /* Skipped because not a chacha20 ciphersuite. If chacha20 boosting is off then this is negotiated.*/
1004 &s2n_ecdhe_rsa_with_aes_256_cbc_sha384,
1005 /* First chacha20 ciphersuite and is negotiated (client's second preferred ciphersuite) */
1006 &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256,
1007 /* Second chacha20 ciphersuite and is the client's most preferred ciphersuite. Not negotiated. */
1008 &s2n_dhe_rsa_with_chacha20_poly1305_sha256,
1009 };
1010
1011 cipher_preferences = (struct s2n_cipher_preferences){
1012 .count = s2n_array_len(test_cipher_suite_list),
1013 .suites = test_cipher_suite_list,
1014 .allow_chacha20_boosting = true,
1015 };
1016
1017 uint8_t wire[] = {
1018 /* Client signalled chacha20 boosting. This is not negotiated as it's not the server's most preferred chacha20 ciphersuite */
1019 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
1020 /* Negotiated if chacha20 boosting is on */
1021 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
1022 /* Negotiated if chacha20 boosting is off */
1023 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
1024 };
1025 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
1026
1027 /* Verify client's second preferred (ecdhe_rsa_chacha20) is negotiated when chacha20 boosting enabled */
1028 cipher_preferences.allow_chacha20_boosting = true;
1029 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1030 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256);
1031
1032 /* Sanity check: server's most preferred ciphersuite is chosen without chacha20 boosting enabled */
1033 /* cppcheck-suppress redundantAssignment */
1034 cipher_preferences.allow_chacha20_boosting = false;
1035 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1036 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_rsa_with_aes_256_cbc_sha384);
1037 };
1038
1039 /* Chacha20 boosting is disabled when client did not indicate chacha20 preference */
1040 {
1041 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1042 EXPECT_NOT_NULL(connection);
1043
1044 connection->security_policy_override = &security_policy;
1045 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
1046 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS12));
1047
1048 EXPECT_SUCCESS(s2n_connection_set_config(connection, server_config));
1049
1050 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1051 /* Negotiated because client did not signal chacha20 boosting and it is present in client wire */
1052 &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256,
1053 /* Never considered; if client did signal chacha20 boosting we expect this to be negotiated */
1054 &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256,
1055 };
1056
1057 cipher_preferences = (struct s2n_cipher_preferences){
1058 .count = s2n_array_len(test_cipher_suite_list),
1059 .suites = test_cipher_suite_list,
1060 .allow_chacha20_boosting = true,
1061 };
1062
1063 uint8_t wire[] = {
1064 /* Chacha20 boosting is off. This ciphersuite is negotiated. */
1065 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
1066 /* Not negotiated */
1067 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
1068 };
1069 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
1070
1071 /* Verify that client negotiates non-chacha20 ciphersuite when chacha20 boosting is not signalled by client */
1072 cipher_preferences.allow_chacha20_boosting = true;
1073 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1074 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256);
1075 };
1076
1077 /* Server negotiates its most preferred chacha20 ciphersuite for tls 1.3 */
1078 {
1079 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1080 EXPECT_NOT_NULL(connection);
1081
1082 connection->security_policy_override = &security_policy;
1083 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
1084 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS13));
1085
1086 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1087 /* Most preferred ciphersuite. If chacha20 boosting behaviour is on then this can't be negotiated. */
1088 &s2n_tls13_aes_128_gcm_sha256,
1089 /* Second preferred ciphersuite. If chacha20 boosting behaviour is on then this can't be negotiated. */
1090 &s2n_tls13_aes_256_gcm_sha384,
1091 /* Negotiated if chacha20 boosting behaviour is on. Otherwise, one of the two ciphersuites above is choosen. */
1092 &s2n_tls13_chacha20_poly1305_sha256,
1093 };
1094
1095 cipher_preferences = (struct s2n_cipher_preferences){
1096 .count = s2n_array_len(test_cipher_suite_list),
1097 .suites = test_cipher_suite_list,
1098 .allow_chacha20_boosting = true,
1099 };
1100
1101 uint8_t test_wire_1[] = {
1102 /* Client signalled chacha20 boosting. Negotiated. */
1103 TLS_CHACHA20_POLY1305_SHA256,
1104 /* Not negotiated even when chacha20 boosting is off. Server prefers aes 128 gcm. */
1105 TLS_AES_256_GCM_SHA384,
1106 /* Negotiated if chacha20 boosting is off. This is the server's most preferred ciphersuite. */
1107 TLS_AES_128_GCM_SHA256,
1108 };
1109 uint8_t count = sizeof(test_wire_1) / S2N_TLS_CIPHER_SUITE_LEN;
1110
1111 cipher_preferences.allow_chacha20_boosting = true;
1112 /* Verify that client negotiates chacha20 when chacha20 boosting is on */
1113 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, test_wire_1, count));
1114 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_tls13_chacha20_poly1305_sha256);
1115
1116 /* Sanity check: server's most preferred ciphersuite is chosen without chacha20 boosting enabled */
1117 /* cppcheck-suppress redundantAssignment */
1118 cipher_preferences.allow_chacha20_boosting = false;
1119 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, test_wire_1, count));
1120 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_tls13_aes_128_gcm_sha256);
1121
1122 /*
1123 * Client did not signal chacha20 boosting.
1124 * TLS_AES_256_GCM_SHA384 > TLS_CHACHA20_POLY1305_SHA256
1125 */
1126 uint8_t test_wire_2[] = {
1127 /* Client did not signal chacha20 boosting. Negotiated if chacha20 boosting is off. */
1128 TLS_AES_256_GCM_SHA384,
1129 /* Not negotiated since the server prefers aes 256 gcm over chacha20 */
1130 TLS_CHACHA20_POLY1305_SHA256,
1131 };
1132 count = sizeof(test_wire_2) / S2N_TLS_CIPHER_SUITE_LEN;
1133
1134 /* Verify that client negotiates server preferred non-chacha20 aes_256 when client did not signal */
1135 /* cppcheck-suppress redundantAssignment */
1136 cipher_preferences.allow_chacha20_boosting = true;
1137 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, test_wire_2, count));
1138 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_tls13_aes_256_gcm_sha384);
1139 };
1140
1141 /* Server can negotiate chacha20 ciphersuite, even when boosting is disabled */
1142 {
1143 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1144 EXPECT_NOT_NULL(connection);
1145
1146 connection->security_policy_override = &security_policy;
1147 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
1148 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS13));
1149
1150 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1151 /* Negotiated (only available option) */
1152 &s2n_tls13_chacha20_poly1305_sha256,
1153 };
1154
1155 cipher_preferences = (struct s2n_cipher_preferences){
1156 .count = s2n_array_len(test_cipher_suite_list),
1157 .suites = test_cipher_suite_list,
1158 .allow_chacha20_boosting = false,
1159 };
1160
1161 uint8_t wire[] = {
1162 /* Client signalled chacha20 boosting. Negotiated as this is the only negotiable option. */
1163 TLS_CHACHA20_POLY1305_SHA256,
1164 };
1165 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
1166
1167 /* Verify server can still negotiate chacha20 if it's the only option even when chacha20 boosting is off */
1168 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1169 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_tls13_chacha20_poly1305_sha256);
1170 };
1171
1172 /*
1173 * sslv2 server correctly negotiates ciphersuite when chacha20 boosting is enabled.
1174 */
1175 {
1176 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1177 EXPECT_NOT_NULL(connection);
1178
1179 connection->security_policy_override = &security_policy;
1180 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
1181 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_SSLv2));
1182 EXPECT_SUCCESS(s2n_connection_set_config(connection, server_config));
1183
1184 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1185 /* First 'valid' ciphersuite that is saved as a higher_vers_match and is negotiated */
1186 &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha,
1187 /* Not valid for connection (does not meet minimum version requirement and higher_vers_match already found) */
1188 &s2n_dhe_rsa_with_chacha20_poly1305_sha256,
1189 /* Not valid for connection (does not meet minimum version requirement and higher_vers_match already found) */
1190 &s2n_dhe_rsa_with_aes_256_gcm_sha384
1191 };
1192
1193 /* cppcheck-suppress redundantAssignment */
1194 cipher_preferences = (struct s2n_cipher_preferences){
1195 .count = s2n_array_len(test_cipher_suite_list),
1196 .suites = test_cipher_suite_list,
1197 .allow_chacha20_boosting = true,
1198 };
1199
1200 uint8_t wire[] = {
1201 /* Client signalled chacha20 boosting. Not negotiated as it's not offered by the server. */
1202 0x00,
1203 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
1204 /* Not saved as a higher_vers_match as ecdhe_ecdsa_aes_128 was already saved as one; not negotiated */
1205 0x00,
1206 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
1207 /* Saved by server as higher_vers_match; negotiated as a 'fall-back' ciphersuite */
1208 0x00,
1209 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
1210 };
1211 uint8_t count = sizeof(wire) / S2N_SSLv2_CIPHER_SUITE_LEN;
1212
1213 /* Verify server negotiate its higher_vers_match ecdhe_ecdsa_aes_128_cbc instead of a chacha20 ciphersuite */
1214 cipher_preferences.allow_chacha20_boosting = true;
1215 /*
1216 * For an sslv2 connection, all of the ciphersuites in test_cipher_suite_list will not meet the minimum
1217 * required tls version validation (they all require at least sslv3). This means that the server will save this
1218 * cipher as a higher_vers_match and will use this ciphersuite as a fallback if no other ciphersuite can be identified.
1219 * When this logic happens, we bypass chacha20 boosting altogether; therefore because the first ciphersuite
1220 * ecdhe_ecdsa_with_aes_128_cbc is offered by both the client and server and only fails the minimum required version check,
1221 * then we save this ciphersuite as a higher_vers_match and continue.
1222 */
1223 EXPECT_SUCCESS(s2n_set_cipher_as_sslv2_server(connection, wire, count));
1224 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha);
1225 };
1226
1227 /* Server is able to negotiate a ciphersuite even without chacha20 in its cipher pref */
1228 {
1229 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1230 EXPECT_NOT_NULL(connection);
1231 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
1232
1233 connection->security_policy_override = &security_policy;
1234 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
1235 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS13));
1236
1237 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1238 /* Negotiated because it is the server's most preferred */
1239 &s2n_tls13_aes_128_gcm_sha256,
1240 /* Not considered */
1241 &s2n_tls13_aes_256_gcm_sha384,
1242 };
1243
1244 cipher_preferences = (struct s2n_cipher_preferences){
1245 .count = s2n_array_len(test_cipher_suite_list),
1246 .suites = test_cipher_suite_list,
1247 .allow_chacha20_boosting = true,
1248 };
1249
1250 uint8_t wire[] = {
1251 /* Client signalled chacha20 boosting; not negotiated as server does not have any chacha20 ciphersuites */
1252 TLS_CHACHA20_POLY1305_SHA256,
1253 /* Not negotiated as the server prefers aes 128 over aes 256 */
1254 TLS_AES_256_GCM_SHA384,
1255 /* Negotiated */
1256 TLS_AES_128_GCM_SHA256,
1257 };
1258 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
1259
1260 /* Verify that server is able to negotiate aes_128 even without any chacha20 ciphersuites in its preferences */
1261 cipher_preferences.allow_chacha20_boosting = false;
1262 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1263 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_tls13_aes_128_gcm_sha256);
1264 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
1265 };
1266
1267 /* Test chacha20 boosting when the most preferred ciphersuite fails version validation */
1268 {
1269 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1270 EXPECT_NOT_NULL(connection);
1271
1272 connection->security_policy_override = &security_policy;
1273 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
1274 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS12));
1275
1276 EXPECT_SUCCESS(s2n_connection_set_config(connection, server_config));
1277
1278 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1279 /* Only negotiated if chacha20 boosting is off */
1280 &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256,
1281 /* Invalid; never use tls 1.3 ciphers on pre-tls 1.3 connections */
1282 &s2n_tls13_chacha20_poly1305_sha256,
1283 /* Negotiated (if chacha20 boosting is on) */
1284 &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256,
1285 };
1286
1287 cipher_preferences = (struct s2n_cipher_preferences){
1288 .count = s2n_array_len(test_cipher_suite_list),
1289 .suites = test_cipher_suite_list,
1290 .allow_chacha20_boosting = true,
1291 };
1292
1293 uint8_t wire[] = {
1294 /* Client signalled chacha20 boosting. Not negotiated by server since it is invalid for the connection. */
1295 TLS_CHACHA20_POLY1305_SHA256,
1296 /* Negotiated if chacha20 boosting is off */
1297 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
1298 /* Negotiated if chacha20 boosting is on */
1299 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
1300 };
1301 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
1302
1303 /* Verify server negotiates second preferred chacha20 ciphersuite ecdhe_rsa_chacha20 */
1304 cipher_preferences.allow_chacha20_boosting = true;
1305 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1306 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256);
1307
1308 /* Sanity check: server's most preferred ciphersuite is chosen without chacha20 boosting enabled */
1309 /* cppcheck-suppress redundantAssignment */
1310 cipher_preferences.allow_chacha20_boosting = false;
1311 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1312 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256);
1313 };
1314 }
1315
1316 if (!s2n_chacha20_poly1305.is_available()) {
1317 /* Chacha20 can't be negotiated when it's not available in libcrypto */
1318 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1319 EXPECT_NOT_NULL(connection);
1320 connection->security_policy_override = &security_policy;
1321 connection->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
1322 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(connection, S2N_TLS12));
1323 EXPECT_SUCCESS(s2n_connection_set_config(connection, server_config));
1324
1325 static struct s2n_cipher_suite *test_cipher_suite_list[] = {
1326 /* Invalid (no libcrypto) */
1327 &s2n_dhe_rsa_with_chacha20_poly1305_sha256,
1328 /* Negotiated (only valid option) */
1329 &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256,
1330 /* Not considered + invalid (no libcrypto) */
1331 &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256,
1332 };
1333
1334 cipher_preferences = (struct s2n_cipher_preferences){
1335 .count = s2n_array_len(test_cipher_suite_list),
1336 .suites = test_cipher_suite_list,
1337 .allow_chacha20_boosting = true,
1338 };
1339
1340 uint8_t wire[] = {
1341 /* Client signalled chacha20 boosting. Not negotiated because of missing libcrypto for chacha20 */
1342 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
1343 /* Not negotiated because of missing libcrypto for chacha20 */
1344 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
1345 /* Negotiated whether chacha20 boosting is enabled or not by server */
1346 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
1347 };
1348 uint8_t count = sizeof(wire) / S2N_TLS_CIPHER_SUITE_LEN;
1349
1350 /* Verify that server negotiated non-chacha20 ciphersuite ecdhe_ecdsa_aes_128 */
1351 cipher_preferences.allow_chacha20_boosting = true;
1352 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(connection, wire, count));
1353 EXPECT_EQUAL(connection->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256);
1354 }
1355
1356 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(rsa_cert));
1357 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_cert));
1358 };
1359
1360 END_TEST();
1361 }