Debian Codegraph @

crt/s2n/tests/unit/s2n_server_extensions_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 "tls/s2n_server_extensions.h"
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/extensions/s2n_cert_status_response.h"
22 #include "tls/extensions/s2n_ec_point_format.h"
23 #include "tls/extensions/s2n_server_key_share.h"
24 #include "tls/extensions/s2n_server_psk.h"
25 #include "tls/extensions/s2n_server_supported_versions.h"
26 #include "tls/s2n_cipher_preferences.h"
27 #include "tls/s2n_security_policies.h"
28 #include "tls/s2n_tls.h"
29 #include "tls/s2n_tls13.h"
30 #include "utils/s2n_bitmap.h"
31 #include "utils/s2n_safety.h"
32
33 const uint8_t EXTENSION_LEN = 2;
34 const uint8_t SECURE_RENEGOTIATION_SIZE = 5;
35 const uint8_t NEW_SESSION_TICKET_SIZE = 4;
36
37 const uint8_t SUPPORTED_VERSION_SIZE = 6;
38 const uint8_t P256_KEYSHARE_SIZE = (32 * 2) + 1 + 8;
39 const uint8_t MIN_TLS13_EXTENSION_SIZE = (32 * 2) + 1 + 8 + 6; /* expanded from
40 P256_KEYSHARE_SIZE + SUPPORTED_VERSION_SIZE because gcc... */
41
42 /* set up minimum parameters for a tls13 connection so server extensions can work */
43 static int configure_tls13_connection(struct s2n_connection *conn)
44 {
45 conn->actual_protocol_version = S2N_TLS13;
46
47 const struct s2n_ecc_preferences *ecc_pref = NULL;
48 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
49 EXPECT_NOT_NULL(ecc_pref);
50
51 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
52 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
53 POSIX_GUARD(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
54 POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
55 POSIX_GUARD(s2n_connection_allow_all_response_extensions(conn));
56
57 return 0;
58 }
59
60 int main(int argc, char **argv)
61 {
62 BEGIN_TEST();
63 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
64
65 struct s2n_cert_chain_and_key *chain_and_key = NULL;
66 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
67 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
68
69 /* s2n_server_extensions_send */
70 {
71 struct s2n_config *config = NULL;
72 EXPECT_NOT_NULL(config = s2n_config_new());
73 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
74
75 /* Test Server Extensions Send - No extensions */
76 {
77 struct s2n_connection *conn = NULL;
78 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
79 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
80 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
81 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
82 EXPECT_EQUAL(s2n_stuffer_data_available(hello_stuffer), 0);
83 EXPECT_SUCCESS(s2n_server_extensions_recv(conn, hello_stuffer));
84 EXPECT_EQUAL(s2n_stuffer_data_available(hello_stuffer), 0);
85 EXPECT_SUCCESS(s2n_connection_free(conn));
86 };
87
88 /* Test Server Extensions Send - Server Name */
89 {
90 struct s2n_connection *conn = NULL;
91 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
92 s2n_extension_type_id extension_id = 0;
93 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_SERVER_NAME, &extension_id));
94 S2N_CBIT_SET(conn->extension_requests_received, extension_id);
95 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
96 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
97
98 /* server name size */
99 const uint8_t size = 4;
100
101 /* server name is sent when used */
102 conn->server_name_used = 1;
103 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
104 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, size + EXTENSION_LEN);
105
106 /* server name is not sent when not used */
107 conn->server_name_used = 0;
108 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
109 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, 0);
110
111 /* TLS 1.3: server name extension is not sent here */
112 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
113 EXPECT_SUCCESS(configure_tls13_connection(conn));
114 conn->server_name_used = 1;
115 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
116 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, MIN_TLS13_EXTENSION_SIZE + EXTENSION_LEN);
117 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
118
119 EXPECT_SUCCESS(s2n_connection_free(conn));
120 };
121
122 /* Test Server Extensions Send - Application Protocol */
123 {
124 struct s2n_connection *conn = NULL;
125 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
126 s2n_extension_type_id extension_id = 0;
127 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_ALPN, &extension_id));
128 S2N_CBIT_SET(conn->extension_requests_received, extension_id);
129 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
130 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
131 strcpy(conn->application_protocol, "h2");
132 const uint8_t application_protocol_len = strlen(conn->application_protocol);
133
134 const uint8_t ALPN_LEN = 7 + application_protocol_len;
135 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
136 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, ALPN_LEN + EXTENSION_LEN);
137
138 strcpy(conn->application_protocol, "");
139 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
140 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, 0);
141
142 /* TLS 1.3: extension is not sent here */
143 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
144 EXPECT_SUCCESS(configure_tls13_connection(conn));
145 strcpy(conn->application_protocol, "h2");
146 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
147 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, MIN_TLS13_EXTENSION_SIZE + EXTENSION_LEN);
148 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
149
150 EXPECT_SUCCESS(s2n_connection_free(conn));
151 };
152
153 /* Test Server Extensions Send - Maximum Fragment Length (MFL) */
154 {
155 struct s2n_connection *conn = NULL;
156 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
157 s2n_extension_type_id extension_id = 0;
158 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_MAX_FRAG_LEN, &extension_id));
159 S2N_CBIT_SET(conn->extension_requests_received, extension_id);
160 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
161 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
162
163 conn->negotiated_mfl_code = 0;
164 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
165 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, 0);
166
167 const uint8_t MFL_EXT_SIZE = 2 + 2 + 1;
168 conn->negotiated_mfl_code = S2N_TLS_MAX_FRAG_LEN_1024;
169 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
170 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, MFL_EXT_SIZE + EXTENSION_LEN);
171
172 /* TLS 1.3: extension is not sent here */
173 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
174 EXPECT_SUCCESS(configure_tls13_connection(conn));
175 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
176 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, MIN_TLS13_EXTENSION_SIZE + EXTENSION_LEN);
177 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
178
179 EXPECT_SUCCESS(s2n_connection_free(conn));
180 };
181
182 /* Test Server Extensions Send - Signed Certificate Timestamp extension */
183 {
184 struct s2n_connection *conn = NULL;
185 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
186 s2n_extension_type_id extension_id = 0;
187 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_SCT_LIST, &extension_id));
188 S2N_CBIT_SET(conn->extension_requests_received, extension_id);
189 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
190 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
191
192 struct s2n_cert_chain_and_key fake_chain_and_key = { 0 };
193 static uint8_t sct_list[] = { 0xff, 0xff, 0xff };
194 EXPECT_SUCCESS(s2n_blob_init(&fake_chain_and_key.sct_list, sct_list, sizeof(sct_list)));
195
196 conn->ct_level_requested = S2N_CT_SUPPORT_REQUEST;
197 conn->handshake_params.our_chain_and_key = &fake_chain_and_key;
198 const uint8_t size = 4 + sizeof(sct_list);
199 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
200 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, size + EXTENSION_LEN);
201
202 /* TLS 1.3: extension is not sent here */
203 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
204 EXPECT_SUCCESS(configure_tls13_connection(conn));
205 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
206 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, MIN_TLS13_EXTENSION_SIZE + EXTENSION_LEN);
207 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
208
209 EXPECT_SUCCESS(s2n_connection_free(conn));
210 };
211
212 /* Test Server Extensions Send - OCSP Status Request */
213 {
214 struct s2n_connection *conn = NULL;
215 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
216 s2n_extension_type_id extension_id = 0;
217 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_STATUS_REQUEST, &extension_id));
218 S2N_CBIT_SET(conn->extension_requests_received, extension_id);
219 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
220 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
221
222 struct s2n_cert_chain_and_key fake_chain_and_key = { 0 };
223 static uint8_t fake_ocsp[] = { 0xff, 0xff, 0xff };
224 EXPECT_SUCCESS(s2n_blob_init(&fake_chain_and_key.ocsp_status, fake_ocsp, sizeof(fake_ocsp)));
225
226 conn->status_type = S2N_STATUS_REQUEST_OCSP;
227 conn->handshake_params.our_chain_and_key = &fake_chain_and_key;
228
229 const uint8_t size = 4;
230 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
231 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, size + EXTENSION_LEN);
232
233 /* TLS 1.3: extension is not sent here */
234 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
235 EXPECT_SUCCESS(configure_tls13_connection(conn));
236 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
237 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, MIN_TLS13_EXTENSION_SIZE + EXTENSION_LEN);
238 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
239
240 EXPECT_SUCCESS(s2n_connection_free(conn));
241 };
242
243 /* Test Server Extensions Send - Secure Negotiation */
244 {
245 struct s2n_connection *conn = NULL;
246 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
247 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
248 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
249
250 conn->secure_renegotiation = 1;
251 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
252 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, SECURE_RENEGOTIATION_SIZE + EXTENSION_LEN);
253
254 conn->secure_renegotiation = 0;
255 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
256 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, 0);
257 EXPECT_SUCCESS(s2n_connection_free(conn));
258 };
259
260 /* Test Server Extensions Send - New Session Ticket */
261 {
262 struct s2n_connection *conn = NULL;
263 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
264 s2n_extension_type_id extension_id = 0;
265 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_SESSION_TICKET, &extension_id));
266 S2N_CBIT_SET(conn->extension_requests_received, extension_id);
267 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
268 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
269
270 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
271 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, 0);
272
273 conn->config->use_tickets = 1;
274 conn->session_ticket_status = S2N_NEW_TICKET;
275 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
276 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, NEW_SESSION_TICKET_SIZE + EXTENSION_LEN);
277 EXPECT_SUCCESS(s2n_connection_free(conn));
278 };
279
280 /* Test TLS13 Extensions */
281 {
282 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
283 struct s2n_connection *conn = NULL;
284 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
285 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
286
287 EXPECT_SUCCESS(s2n_connection_allow_response_extension(conn, s2n_server_key_share_extension.iana_value));
288
289 const struct s2n_ecc_preferences *ecc_pref = NULL;
290 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
291 EXPECT_NOT_NULL(ecc_pref);
292
293 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
294 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
295
296 /* Test that s2n_server_extensions_send() only works when protocol version is TLS13 */
297 conn->actual_protocol_version = S2N_TLS13;
298
299 /* key_share_send() requires a negotiated_curve */
300 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
301
302 EXPECT_EQUAL(s2n_extensions_server_key_share_send_size(conn), P256_KEYSHARE_SIZE);
303
304 EXPECT_FAILURE(s2n_server_extensions_send(conn, hello_stuffer));
305
306 EXPECT_SUCCESS(s2n_stuffer_wipe(hello_stuffer));
307 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
308
309 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
310 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, P256_KEYSHARE_SIZE + EXTENSION_LEN);
311
312 /* Test that s2n_server_extensions_send() do not send extension < TLS13 */
313 conn->actual_protocol_version = S2N_TLS12;
314
315 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, s2n_stuffer_data_available(hello_stuffer)));
316 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
317 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, 0);
318 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
319 EXPECT_SUCCESS(s2n_connection_free(conn));
320 };
321
322 /* Test Secure Negotiation server_hello extension not sent with TLS13 or higher */
323 {
324 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
325 struct s2n_connection *conn = NULL;
326 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
327 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
328
329 EXPECT_SUCCESS(s2n_connection_allow_response_extension(conn, s2n_server_key_share_extension.iana_value));
330
331 const struct s2n_ecc_preferences *ecc_pref = NULL;
332 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
333 EXPECT_NOT_NULL(ecc_pref);
334
335 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
336 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
337 /* secure renegotiation is requested */
338 conn->secure_renegotiation = 1;
339 /* Test that s2n_server_extensions_send() only works when protocol version is TLS13 */
340 conn->actual_protocol_version = S2N_TLS13;
341
342 /* key_share_send() requires a negotiated_curve */
343 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
344 /* secure_renegotiation extension not send >=TLS13*/
345 uint8_t size = s2n_extensions_server_key_share_send_size(conn);
346
347 EXPECT_FAILURE(s2n_server_extensions_send(conn, hello_stuffer));
348
349 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
350
351 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, s2n_stuffer_data_available(hello_stuffer)));
352 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
353 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, size + EXTENSION_LEN);
354
355 /* Only sending secure_renegotiation(if it is requested) < TLS13 */
356 conn->actual_protocol_version = S2N_TLS12;
357 uint8_t tls12_server_extension_size = SECURE_RENEGOTIATION_SIZE + EXTENSION_LEN;
358 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, s2n_stuffer_data_available(hello_stuffer)));
359 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
360 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, tls12_server_extension_size);
361 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
362 EXPECT_SUCCESS(s2n_connection_free(conn));
363 };
364
365 /* Test New Session Ticket server_hello extension not sent with TLS13 or higher */
366 {
367 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
368 struct s2n_connection *conn = NULL;
369 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
370 s2n_extension_type_id extension_id = 0;
371 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_SESSION_TICKET, &extension_id));
372 S2N_CBIT_SET(conn->extension_requests_received, extension_id);
373 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
374
375 EXPECT_SUCCESS(s2n_connection_allow_response_extension(conn, s2n_server_key_share_extension.iana_value));
376
377 const struct s2n_ecc_preferences *ecc_pref = NULL;
378 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
379 EXPECT_NOT_NULL(ecc_pref);
380
381 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
382 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
383
384 /* New Session Ticket Requested*/
385 conn->config->use_tickets = 1;
386 conn->session_ticket_status = S2N_NEW_TICKET;
387
388 /* Test that s2n_server_extensions_send() only works when protocol version is TLS13 */
389 conn->actual_protocol_version = S2N_TLS13;
390
391 /* key_share_send() requires a negotiated_curve */
392 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
393
394 /* nst extension not send >=TLS13*/
395 uint8_t size = s2n_extensions_server_key_share_send_size(conn);
396
397 EXPECT_FAILURE(s2n_server_extensions_send(conn, hello_stuffer));
398
399 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
400
401 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, s2n_stuffer_data_available(hello_stuffer)));
402 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
403 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, size + EXTENSION_LEN);
404
405 /* Sending nst (if it is requested) < TLS13 */
406 conn->actual_protocol_version = S2N_TLS12;
407
408 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, s2n_stuffer_data_available(hello_stuffer)));
409 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
410 uint8_t tls12_server_extension_size = NEW_SESSION_TICKET_SIZE + EXTENSION_LEN;
411 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, tls12_server_extension_size);
412 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
413 EXPECT_SUCCESS(s2n_connection_free(conn));
414 };
415
416 /* Test TLS13 Extensions with null key exchange alg cipher suites */
417 {
418 struct s2n_cipher_suite *tls12_cipher_suite = cipher_preferences_20170210.suites[cipher_preferences_20170210.count - 1];
419 uint8_t wire_ciphers_with_tls13[] = {
420 TLS_AES_128_GCM_SHA256,
421 TLS_AES_256_GCM_SHA384,
422 TLS_CHACHA20_POLY1305_SHA256,
423 tls12_cipher_suite->iana_value[0], tls12_cipher_suite->iana_value[1]
424 };
425 const uint8_t cipher_count_tls13 = sizeof(wire_ciphers_with_tls13) / S2N_TLS_CIPHER_SUITE_LEN;
426
427 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
428 struct s2n_connection *conn = NULL;
429 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
430 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
431
432 EXPECT_SUCCESS(s2n_connection_allow_response_extension(conn, s2n_server_key_share_extension.iana_value));
433
434 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all_tls13"));
435
436 const struct s2n_ecc_preferences *ecc_pref = NULL;
437 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
438 EXPECT_NOT_NULL(ecc_pref);
439 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
440 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
441
442 /* Test that s2n_server_extensions_send() only works when protocol version is TLS13 */
443 conn->client_protocol_version = S2N_TLS13;
444 conn->actual_protocol_version = S2N_TLS13;
445 EXPECT_SUCCESS(s2n_set_cipher_as_tls_server(conn, wire_ciphers_with_tls13, cipher_count_tls13));
446
447 /* key_share_send() requires a negotiated_curve */
448 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
449
450 uint8_t size = s2n_extensions_server_key_share_send_size(conn);
451
452 EXPECT_FAILURE(s2n_server_extensions_send(conn, hello_stuffer));
453
454 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
455
456 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, s2n_stuffer_data_available(hello_stuffer)));
457 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
458 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, size + EXTENSION_LEN);
459
460 /* Test that s2n_server_extensions_send() do not send extension < TLS13 */
461 conn->actual_protocol_version = S2N_TLS12;
462
463 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, s2n_stuffer_data_available(hello_stuffer)));
464 EXPECT_SUCCESS(s2n_server_extensions_send(conn, hello_stuffer));
465 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, 0);
466 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
467 EXPECT_SUCCESS(s2n_connection_free(conn));
468 };
469
470 /* Test that some TLS1.3 extensions (like PSK) not sent on a HRR request */
471 {
472 s2n_extension_type_id psk_extension_id = 0;
473 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(s2n_server_psk_extension.iana_value, &psk_extension_id));
474
475 struct s2n_psk psk = { 0 };
476
477 for (size_t is_hrr = 0; is_hrr < 2; is_hrr++) {
478 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
479 EXPECT_NOT_NULL(conn);
480 EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(conn));
481 conn->actual_protocol_version = S2N_TLS13;
482 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
483
484 struct s2n_stuffer *io_stuffer = &conn->handshake.io;
485
486 /* Setup required for PSK extension */
487 conn->psk_params.chosen_psk = &psk;
488 S2N_CBIT_CLR(conn->extension_requests_sent, psk_extension_id);
489 EXPECT_TRUE(s2n_server_psk_extension.should_send(conn));
490
491 /* Setup required for other server extensions */
492 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
493 conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
494 conn->kex_params.client_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
495 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
496
497 if (is_hrr) {
498 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(conn));
499 }
500
501 EXPECT_SUCCESS(s2n_server_extensions_send(conn, io_stuffer));
502
503 s2n_parsed_extensions_list parsed_extensions = { 0 };
504 EXPECT_SUCCESS(s2n_extension_list_parse(io_stuffer, &parsed_extensions));
505
506 bool psk_extension_sent = (parsed_extensions.parsed_extensions[psk_extension_id].extension_type
507 == s2n_server_psk_extension.iana_value);
508 EXPECT_NOT_EQUAL(psk_extension_sent, is_hrr);
509
510 EXPECT_SUCCESS(s2n_connection_free(conn));
511 }
512 };
513
514 EXPECT_SUCCESS(s2n_config_free(config));
515 };
516
517 /* Test ec_point_format extension */
518 {
519 struct s2n_connection *conn = NULL;
520 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
521 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha;
522
523 struct s2n_stuffer stuffer = { 0 };
524 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
525
526 EXPECT_SUCCESS(s2n_server_extensions_send(conn, &stuffer));
527 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
528
529 EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(conn));
530
531 EXPECT_SUCCESS(s2n_server_extensions_send(conn, &stuffer));
532 EXPECT_NOT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
533
534 EXPECT_SUCCESS(s2n_server_extensions_recv(conn, &stuffer));
535 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
536
537 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
538 EXPECT_SUCCESS(s2n_connection_free(conn));
539 };
540
541 /* Test supported_versions extension can change extensions processed.
542 * In TLS1.2, we receive status_request on the ServerHello. TLS1.3 expects it on the Certificate. */
543 {
544 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
545
546 struct s2n_connection *server_conn = NULL;
547 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
548 EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(server_conn));
549
550 struct s2n_cert_chain_and_key fake_chain_and_key = { 0 };
551 static uint8_t fake_ocsp[] = { 0xff, 0xff, 0xff };
552 EXPECT_SUCCESS(s2n_blob_init(&fake_chain_and_key.ocsp_status, fake_ocsp, sizeof(fake_ocsp)));
553
554 /* For our test status_request extension */
555 server_conn->status_type = S2N_STATUS_REQUEST_OCSP;
556 server_conn->handshake_params.our_chain_and_key = &fake_chain_and_key;
557
558 /* supported_versions not included - should NOT use TLS1.3 extensions,
559 * so should accept the status_request without issue. */
560 {
561 server_conn->actual_protocol_version = S2N_TLS12;
562 server_conn->server_protocol_version = S2N_TLS12;
563
564 struct s2n_connection *client_conn = NULL;
565 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
566 EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(client_conn));
567
568 DEFER_CLEANUP(struct s2n_stuffer stuffer, s2n_stuffer_free);
569 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
570
571 /* Write extensions - just status_request */
572 struct s2n_stuffer_reservation extension_list_size = { 0 };
573 EXPECT_SUCCESS(s2n_stuffer_reserve_uint16(&stuffer, &extension_list_size));
574 EXPECT_SUCCESS(s2n_extension_send(&s2n_cert_status_response_extension,
575 server_conn, &stuffer));
576 EXPECT_SUCCESS(s2n_stuffer_write_vector_size(&extension_list_size));
577
578 EXPECT_EQUAL(client_conn->status_type, S2N_STATUS_REQUEST_NONE);
579 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_UNKNOWN_PROTOCOL_VERSION);
580 EXPECT_SUCCESS(s2n_server_extensions_recv(client_conn, &stuffer));
581 EXPECT_EQUAL(client_conn->status_type, S2N_STATUS_REQUEST_OCSP);
582 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_UNKNOWN_PROTOCOL_VERSION);
583
584 EXPECT_SUCCESS(s2n_connection_free(client_conn));
585 };
586
587 /* supported_versions included - should use TLS1.3 extensions,
588 * so should reject the status_request bc it does not belong here. */
589 {
590 server_conn->actual_protocol_version = S2N_TLS13;
591 server_conn->server_protocol_version = S2N_TLS13;
592
593 struct s2n_connection *client_conn = NULL;
594 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
595 EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(client_conn));
596
597 DEFER_CLEANUP(struct s2n_stuffer stuffer, s2n_stuffer_free);
598 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
599
600 /* Write extensions - supported_versions + status_request */
601 struct s2n_stuffer_reservation extension_list_size = { 0 };
602 EXPECT_SUCCESS(s2n_stuffer_reserve_uint16(&stuffer, &extension_list_size));
603 EXPECT_SUCCESS(s2n_extension_send(&s2n_server_supported_versions_extension,
604 server_conn, &stuffer));
605 EXPECT_SUCCESS(s2n_extension_send(&s2n_cert_status_response_extension,
606 server_conn, &stuffer));
607 EXPECT_SUCCESS(s2n_stuffer_write_vector_size(&extension_list_size));
608
609 EXPECT_EQUAL(client_conn->status_type, S2N_STATUS_REQUEST_NONE);
610 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_UNKNOWN_PROTOCOL_VERSION);
611 EXPECT_SUCCESS(s2n_server_extensions_recv(client_conn, &stuffer));
612 EXPECT_EQUAL(client_conn->status_type, S2N_STATUS_REQUEST_NONE);
613 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_TLS13);
614
615 EXPECT_SUCCESS(s2n_connection_free(client_conn));
616 };
617
618 /* TLS1.3 HRR handshake - should use HRR TLS1.3 extensions,
619 * so should reject the PSK extension */
620 {
621 const uint8_t test_wire_index = 5;
622 struct s2n_psk empty_psk = { 0 };
623
624 server_conn->actual_protocol_version = S2N_TLS13;
625 server_conn->server_protocol_version = S2N_TLS13;
626 EXPECT_OK(s2n_conn_choose_state_machine(server_conn, S2N_TLS13));
627 server_conn->psk_params.chosen_psk = &empty_psk;
628 server_conn->psk_params.chosen_psk_wire_index = test_wire_index;
629
630 DEFER_CLEANUP(struct s2n_stuffer stuffer, s2n_stuffer_free);
631 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
632
633 /* Write extensions - supported_versions + PSK */
634 struct s2n_stuffer_reservation extension_list_size = { 0 };
635 EXPECT_SUCCESS(s2n_stuffer_reserve_uint16(&stuffer, &extension_list_size));
636 EXPECT_SUCCESS(s2n_extension_send(&s2n_server_supported_versions_extension,
637 server_conn, &stuffer));
638 EXPECT_SUCCESS(s2n_extension_send(&s2n_server_psk_extension,
639 server_conn, &stuffer));
640 EXPECT_SUCCESS(s2n_stuffer_write_vector_size(&extension_list_size));
641
642 for (size_t is_hrr = 0; is_hrr < 2; is_hrr++) {
643 struct s2n_connection *client_conn = NULL;
644 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
645 EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(client_conn));
646 client_conn->actual_protocol_version = S2N_TLS13;
647 EXPECT_OK(s2n_conn_choose_state_machine(client_conn, S2N_TLS13));
648
649 EXPECT_SUCCESS(s2n_connection_mark_extension_received(client_conn, s2n_server_key_share_extension.iana_value));
650
651 for (size_t i = 0; i <= test_wire_index; i++) {
652 struct s2n_psk *psk = NULL;
653 EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &psk));
654 }
655
656 if (is_hrr) {
657 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(client_conn));
658 }
659
660 EXPECT_EQUAL(client_conn->psk_params.chosen_psk_wire_index, 0);
661 EXPECT_SUCCESS(s2n_server_extensions_recv(client_conn, &stuffer));
662
663 if (is_hrr) {
664 EXPECT_EQUAL(client_conn->psk_params.chosen_psk_wire_index, 0);
665 } else {
666 EXPECT_EQUAL(client_conn->psk_params.chosen_psk_wire_index, test_wire_index);
667 }
668
669 EXPECT_SUCCESS(s2n_connection_free(client_conn));
670 EXPECT_SUCCESS(s2n_stuffer_reread(&stuffer));
671 }
672 };
673
674 EXPECT_SUCCESS(s2n_connection_free(server_conn));
675 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
676 };
677
678 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
679
680 END_TEST();
681 }