Debian Codegraph @

crt/s2n/tests/unit/s2n_cookie_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/extensions/s2n_cookie.h"
17
18 #include "s2n_test.h"
19 #include "testlib/s2n_testlib.h"
20 #include "tls/s2n_handshake_type.h"
21 #include "utils/s2n_random.h"
22 #include "utils/s2n_safety.h"
23
24 #define TEST_COOKIE_COUNT 5
25
26 int main()
27 {
28 BEGIN_TEST();
29
30 if (!s2n_is_tls13_fully_supported()) {
31 END_TEST();
32 }
33
34 uint16_t test_cookie_sizes[TEST_COOKIE_COUNT] = { 1, UINT8_MAX, UINT8_MAX + 1, UINT16_MAX - 1, UINT16_MAX };
35 struct s2n_blob test_cookies[TEST_COOKIE_COUNT] = { 0 };
36 for (size_t i = 0; i < TEST_COOKIE_COUNT; i++) {
37 EXPECT_SUCCESS(s2n_alloc(&test_cookies[i], test_cookie_sizes[i]));
38 EXPECT_OK(s2n_get_public_random_data(&test_cookies[i]));
39 }
40
41 /**
42 * Test: client only sends extension if cookie present
43 *
44 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.2
45 *= type=test
46 *# - Including a "cookie" extension if one was provided in the
47 *# HelloRetryRequest.
48 **/
49 {
50 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
51 s2n_connection_ptr_free);
52 EXPECT_NOT_NULL(client_conn);
53
54 /* Not sent without a cookie */
55 EXPECT_FALSE(s2n_client_cookie_extension.should_send(client_conn));
56
57 /* Sent with a cookie */
58 EXPECT_SUCCESS(s2n_dup(&test_cookies[0], &client_conn->cookie));
59 EXPECT_TRUE(s2n_client_cookie_extension.should_send(client_conn));
60 };
61
62 /* Test: server only sends extension if cookie present
63 * (cookie will never be present in production)
64 */
65 {
66 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
67 s2n_connection_ptr_free);
68 EXPECT_NOT_NULL(server_conn);
69
70 /* Not sent without a cookie */
71 EXPECT_FALSE(s2n_server_cookie_extension.should_send(server_conn));
72
73 /* Sent with a cookie */
74 EXPECT_SUCCESS(s2n_dup(&test_cookies[0], &server_conn->cookie));
75 EXPECT_TRUE(s2n_server_cookie_extension.should_send(server_conn));
76 };
77
78 /* Test: client can parse server cookie extension */
79 for (size_t i = 0; i < TEST_COOKIE_COUNT; i++) {
80 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
81 s2n_connection_ptr_free);
82 EXPECT_NOT_NULL(server_conn);
83
84 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
85 s2n_connection_ptr_free);
86 EXPECT_NOT_NULL(server_conn);
87
88 DEFER_CLEANUP(struct s2n_stuffer server_extension = { 0 }, s2n_stuffer_free);
89 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_extension, 0));
90
91 /* Server sends extension with test cookie */
92 EXPECT_SUCCESS(s2n_dup(&test_cookies[i], &server_conn->cookie));
93 EXPECT_SUCCESS(s2n_server_cookie_extension.send(server_conn, &server_extension));
94
95 /* Client doesn't parse extension if no retry */
96 EXPECT_FAILURE_WITH_ERRNO(s2n_server_cookie_extension.recv(client_conn, &server_extension),
97 S2N_ERR_UNSUPPORTED_EXTENSION);
98
99 /* Client parses extension if retry */
100 client_conn->handshake.handshake_type = HELLO_RETRY_REQUEST;
101 EXPECT_SUCCESS(s2n_server_cookie_extension.recv(client_conn, &server_extension));
102 S2N_BLOB_EXPECT_EQUAL(test_cookies[i], client_conn->cookie);
103 }
104
105 /* Test: client sends correctly formatted extension */
106 for (size_t i = 0; i < TEST_COOKIE_COUNT; i++) {
107 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
108 s2n_connection_ptr_free);
109 EXPECT_NOT_NULL(server_conn);
110
111 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
112 s2n_connection_ptr_free);
113 EXPECT_NOT_NULL(server_conn);
114
115 DEFER_CLEANUP(struct s2n_stuffer client_extension = { 0 }, s2n_stuffer_free);
116 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_extension, 0));
117
118 /* Client sends extension with test cookie */
119 EXPECT_SUCCESS(s2n_dup(&test_cookies[i], &client_conn->cookie));
120 EXPECT_SUCCESS(s2n_client_cookie_extension.send(client_conn, &client_extension));
121
122 /* Sanity check: Server rejects incorrectly sized cookie */
123 EXPECT_SUCCESS(s2n_dup(&test_cookies[i], &server_conn->cookie));
124 server_conn->cookie.size--;
125 EXPECT_FAILURE_WITH_ERRNO(s2n_client_cookie_extension.recv(server_conn, &client_extension),
126 S2N_ERR_BAD_MESSAGE);
127 EXPECT_SUCCESS(s2n_free(&server_conn->cookie));
128 EXPECT_SUCCESS(s2n_stuffer_reread(&client_extension));
129
130 /* Sanity check: Server rejects incorrect cookie data */
131 EXPECT_SUCCESS(s2n_dup(&test_cookies[i], &server_conn->cookie));
132 server_conn->cookie.data[0] = server_conn->cookie.data[0] + 1;
133 EXPECT_FAILURE_WITH_ERRNO(s2n_client_cookie_extension.recv(server_conn, &client_extension),
134 S2N_ERR_BAD_MESSAGE);
135 EXPECT_SUCCESS(s2n_free(&server_conn->cookie));
136 EXPECT_SUCCESS(s2n_stuffer_reread(&client_extension));
137
138 /* Server accepts correct cookie data */
139 EXPECT_SUCCESS(s2n_dup(&test_cookies[i], &server_conn->cookie));
140 EXPECT_SUCCESS(s2n_client_cookie_extension.recv(server_conn, &client_extension));
141 }
142
143 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
144 s2n_cert_chain_and_key_ptr_free);
145 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
146 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
147
148 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
149 s2n_config_ptr_free);
150 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
151 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
152 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
153
154 /* Although the cookie is *technically* allowed to be UINT16_MAX,
155 * in reality it has to share a uint16_t extensions list length
156 * with other extensions.
157 *
158 * So for the self-talk tests, reduce the size of any large test cookies.
159 */
160 for (size_t i = 0; i < TEST_COOKIE_COUNT; i++) {
161 test_cookies[i].size = S2N_MIN(test_cookies[i].size, UINT16_MAX / 2);
162 }
163
164 /* Sanity check: server fails if client does not provide expected cookie */
165 {
166 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
167 s2n_connection_ptr_free);
168 EXPECT_NOT_NULL(server_conn);
169 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
170 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
171
172 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
173 s2n_connection_ptr_free);
174 EXPECT_NOT_NULL(server_conn);
175 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
176
177 struct s2n_test_io_pair io_pair = { 0 };
178 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
179 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
180
181 /* Force the HRR path */
182 client_conn->security_policy_override = &security_policy_test_tls13_retry;
183
184 /* Force the server to send a cookie */
185 EXPECT_SUCCESS(s2n_dup(&test_cookies[0], &server_conn->cookie));
186
187 /* Begin negotiating handshake.
188 * The first negotiate_until blocks because the client is looking for a SERVER_HELLO,
189 * not the HELLO_RETRY_MESSAGE. This is fine; it's in the right place in the handshake.
190 */
191 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, HELLO_RETRY_MSG),
192 S2N_ERR_IO_BLOCKED);
193 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), HELLO_RETRY_MSG);
194 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_HELLO);
195 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, CLIENT_HELLO));
196 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_HELLO);
197 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_HELLO);
198
199 /* Verify HRR path */
200 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(server_conn));
201 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(client_conn));
202
203 /* At this point, the server has already sent its HRR request with a cookie.
204 * The client has stored the server's cookie, but not responded.
205 * Wipe the cookie on the client, preventing it from sending the response.
206 */
207 EXPECT_NOT_EQUAL(client_conn->cookie.size, 0);
208 EXPECT_SUCCESS(s2n_free(&client_conn->cookie));
209
210 /* Continue negotiating. We should fail because of the "missing" cookie. */
211 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
212 S2N_ERR_MISSING_EXTENSION);
213
214 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
215 };
216
217 /* Self-Talk: Server does NOT use cookies */
218 {
219 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
220 s2n_connection_ptr_free);
221 EXPECT_NOT_NULL(server_conn);
222 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
223
224 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
225 s2n_connection_ptr_free);
226 EXPECT_NOT_NULL(server_conn);
227 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
228
229 struct s2n_test_io_pair io_pair = { 0 };
230 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
231 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
232
233 /* Force the HRR path */
234 client_conn->security_policy_override = &security_policy_test_tls13_retry;
235
236 /* Negotiate handshake */
237 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
238
239 /* Verify HRR path */
240 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(server_conn));
241 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(client_conn));
242
243 /* Verify no cookies */
244 EXPECT_EQUAL(client_conn->cookie.size, 0);
245 EXPECT_EQUAL(server_conn->cookie.size, 0);
246
247 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
248 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
249 };
250
251 /* Self-Talk: Server does use cookies
252 *
253 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.2
254 *= type=test
255 *# When sending a HelloRetryRequest, the server MAY provide a "cookie"
256 *# extension to the client (this is an exception to the usual rule that
257 *# the only extensions that may be sent are those that appear in the
258 *# ClientHello). When sending the new ClientHello, the client MUST copy
259 *# the contents of the extension received in the HelloRetryRequest into
260 *# a "cookie" extension in the new ClientHello.
261 */
262 for (size_t i = 0; i < TEST_COOKIE_COUNT; i++) {
263 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
264 s2n_connection_ptr_free);
265 EXPECT_NOT_NULL(server_conn);
266 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
267
268 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
269 s2n_connection_ptr_free);
270 EXPECT_NOT_NULL(server_conn);
271 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
272
273 struct s2n_test_io_pair io_pair = { 0 };
274 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
275 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
276
277 /* Force the HRR path */
278 client_conn->security_policy_override = &security_policy_test_tls13_retry;
279
280 /* Force the server to send a cookie */
281 EXPECT_SUCCESS(s2n_dup(&test_cookies[i], &server_conn->cookie));
282
283 /* Negotiate handshake */
284 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
285
286 /* Verify HRR path */
287 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(server_conn));
288 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(client_conn));
289
290 /* Verify cookies */
291 S2N_BLOB_EXPECT_EQUAL(test_cookies[i], client_conn->cookie);
292 S2N_BLOB_EXPECT_EQUAL(test_cookies[i], server_conn->cookie);
293
294 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
295 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
296 }
297
298 /* Self-Talk: Full connection lifecycle with cookies
299 * We try the handshake multiple times with different possible call patterns.
300 */
301 {
302 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
303 s2n_connection_ptr_free);
304 EXPECT_NOT_NULL(server_conn);
305 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
306
307 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
308 s2n_connection_ptr_free);
309 EXPECT_NOT_NULL(server_conn);
310 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
311
312 /* We need an arbitrary combination of conditions,
313 * but consistent across test runs.
314 */
315 srand(0);
316
317 for (size_t i = 0; i < 250; i++) {
318 int r = rand();
319 bool hrr = (r % 2) == 0;
320 bool cookie = (r % 3) == 0;
321 size_t cookie_i = i % TEST_COOKIE_COUNT;
322 bool free_handshake = (r % 7) == 0;
323
324 /* Verify calls to s2n_connection_wipe are safe */
325 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
326 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
327
328 /* Create nonblocking pipes */
329 struct s2n_test_io_pair io_pair = { 0 };
330 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
331 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
332
333 /* Force the HRR path */
334 if (hrr) {
335 client_conn->security_policy_override = &security_policy_test_tls13_retry;
336 }
337
338 /* Force the server to send a cookie */
339 if (cookie) {
340 EXPECT_SUCCESS(s2n_dup(&test_cookies[cookie_i], &server_conn->cookie));
341 }
342
343 /* Negotiate handshake */
344 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
345
346 /* Verify HRR path */
347 if (hrr) {
348 EXPECT_TRUE(s2n_is_hello_retry_handshake(server_conn));
349 EXPECT_TRUE(s2n_is_hello_retry_handshake(client_conn));
350 } else {
351 EXPECT_FALSE(s2n_is_hello_retry_handshake(server_conn));
352 EXPECT_FALSE(s2n_is_hello_retry_handshake(client_conn));
353 }
354
355 /* Verify cookie data */
356 if (hrr && cookie) {
357 S2N_BLOB_EXPECT_EQUAL(test_cookies[cookie_i], client_conn->cookie);
358 } else {
359 EXPECT_EQUAL(client_conn->cookie.size, 0);
360 }
361
362 if (free_handshake) {
363 EXPECT_SUCCESS(s2n_connection_free_handshake(client_conn));
364 EXPECT_SUCCESS(s2n_connection_free_handshake(server_conn));
365 }
366
367 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
368 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
369 }
370 };
371
372 for (size_t i = 0; i < TEST_COOKIE_COUNT; i++) {
373 EXPECT_SUCCESS(s2n_free(&test_cookies[i]));
374 }
375 END_TEST();
376 }