Debian Codegraph @

crt/s2n/tests/unit/s2n_client_hello_get_supported_groups_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 "crypto/s2n_pq.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/extensions/s2n_client_supported_groups.h"
20 #include "tls/s2n_client_hello.h"
21 #include "tls/s2n_tls.h"
22 #include "utils/s2n_random.h"
23
24 #define S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT 16
25
26 /* Each supported group is 2 bytes. */
27 #define S2N_TEST_SUPPORTED_GROUPS_LIST_SIZE (S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT * S2N_SUPPORTED_GROUP_SIZE)
28
29 /* 2 length bytes + space for the list of supported groups. */
30 #define S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE (2 + S2N_TEST_SUPPORTED_GROUPS_LIST_SIZE)
31
32 struct s2n_client_hello_context {
33 struct s2n_stuffer *sent_supported_groups_extension;
34 int invoked_count;
35 };
36
37 int s2n_check_received_supported_groups_cb(struct s2n_connection *conn, void *ctx)
38 {
39 EXPECT_NOT_NULL(ctx);
40
41 struct s2n_client_hello_context *context = (struct s2n_client_hello_context *) ctx;
42 EXPECT_NOT_NULL(context->sent_supported_groups_extension);
43 context->invoked_count += 1;
44
45 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(conn);
46 EXPECT_NOT_NULL(client_hello);
47
48 uint16_t received_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
49 uint16_t received_groups_count = 0;
50 EXPECT_SUCCESS(s2n_client_hello_get_supported_groups(client_hello, received_groups,
51 s2n_array_len(received_groups), &received_groups_count));
52
53 uint16_t sent_groups_count = 0;
54 EXPECT_OK(s2n_supported_groups_parse_count(context->sent_supported_groups_extension, &sent_groups_count));
55 EXPECT_EQUAL(received_groups_count, sent_groups_count);
56
57 for (size_t i = 0; i < received_groups_count; i++) {
58 uint16_t received_group = received_groups[i];
59
60 /* s2n_stuffer_read_uint16 is used to read each of the sent supported groups in
61 * network-order endianness, and compare them to the received supported groups which have
62 * already been converted to the machine's endianness.
63 */
64 uint16_t sent_group = 0;
65 EXPECT_SUCCESS(s2n_stuffer_read_uint16(context->sent_supported_groups_extension, &sent_group));
66
67 EXPECT_EQUAL(received_group, sent_group);
68 }
69
70 return S2N_SUCCESS;
71 }
72
73 int main(int argc, char **argv)
74 {
75 BEGIN_TEST();
76
77 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
78 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
79 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
80
81 s2n_extension_type_id supported_groups_id = 0;
82 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(S2N_EXTENSION_SUPPORTED_GROUPS, &supported_groups_id));
83
84 /* Safety */
85 {
86 struct s2n_client_hello client_hello = { 0 };
87 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
88 uint16_t supported_groups_count = 0;
89
90 int ret = s2n_client_hello_get_supported_groups(NULL, supported_groups, s2n_array_len(supported_groups),
91 &supported_groups_count);
92 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_NULL);
93 EXPECT_EQUAL(supported_groups_count, 0);
94
95 ret = s2n_client_hello_get_supported_groups(&client_hello, NULL, s2n_array_len(supported_groups),
96 &supported_groups_count);
97 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_NULL);
98 EXPECT_EQUAL(supported_groups_count, 0);
99
100 ret = s2n_client_hello_get_supported_groups(&client_hello, supported_groups, s2n_array_len(supported_groups), NULL);
101 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_NULL);
102 EXPECT_EQUAL(supported_groups_count, 0);
103 }
104
105 /* Ensure that the maximum size of the provided supported groups list is respected. */
106 {
107 struct s2n_client_hello client_hello = { 0 };
108
109 uint8_t extension_data[S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 };
110 struct s2n_blob extension_blob = { 0 };
111 EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data)));
112
113 s2n_parsed_extension *supported_groups_extension = &client_hello.extensions.parsed_extensions[supported_groups_id];
114 supported_groups_extension->extension_type = S2N_EXTENSION_SUPPORTED_GROUPS;
115 supported_groups_extension->extension = extension_blob;
116
117 struct s2n_stuffer extension_stuffer = { 0 };
118 EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob));
119 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&extension_stuffer, S2N_TEST_SUPPORTED_GROUPS_LIST_SIZE));
120
121 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
122 uint16_t supported_groups_count = 0;
123
124 /* Fail if the provided buffer is too small. */
125 int ret = s2n_client_hello_get_supported_groups(&client_hello, supported_groups,
126 S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT - 1, &supported_groups_count);
127 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_INSUFFICIENT_MEM_SIZE);
128 EXPECT_EQUAL(supported_groups_count, 0);
129
130 EXPECT_SUCCESS(s2n_stuffer_reread(&extension_stuffer));
131
132 /* Succeed with a correctly sized buffer. */
133 EXPECT_SUCCESS(s2n_client_hello_get_supported_groups(&client_hello, supported_groups,
134 S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT, &supported_groups_count));
135 EXPECT_EQUAL(supported_groups_count, S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT);
136 }
137
138 /* Error if the client hello isn't parsed yet. */
139 {
140 struct s2n_client_hello client_hello = { 0 };
141
142 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
143 uint16_t supported_groups_count = 0;
144 int ret = s2n_client_hello_get_supported_groups(&client_hello, supported_groups,
145 s2n_array_len(supported_groups), &supported_groups_count);
146 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_EXTENSION_NOT_RECEIVED);
147 }
148
149 /* Error if a supported groups extension wasn't received. */
150 for (int disable_ecc = 0; disable_ecc <= 1; disable_ecc++) {
151 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
152 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
153 if (disable_ecc) {
154 /* The 20150202 security policy doesn't contain any ECDHE cipher suites, so the
155 * supported groups extension won't be sent.
156 */
157 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20150202"));
158 } else {
159 /* The 20170210 security policy contains ECDHE cipher suites, so the supported groups
160 * extension will be sent.
161 */
162 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20170210"));
163 }
164
165 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
166 s2n_connection_ptr_free);
167 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
168
169 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
170 s2n_connection_ptr_free);
171 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
172
173 EXPECT_SUCCESS(s2n_client_hello_send(client));
174 EXPECT_SUCCESS(s2n_stuffer_copy(&client->handshake.io, &server->handshake.io,
175 s2n_stuffer_data_available(&client->handshake.io)));
176 EXPECT_SUCCESS(s2n_client_hello_recv(server));
177
178 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server);
179 EXPECT_NOT_NULL(client_hello);
180
181 bool supported_groups_extension_exists = false;
182 EXPECT_SUCCESS(s2n_client_hello_has_extension(client_hello, S2N_EXTENSION_SUPPORTED_GROUPS,
183 &supported_groups_extension_exists));
184 EXPECT_EQUAL(supported_groups_extension_exists, !disable_ecc);
185
186 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
187 uint16_t supported_groups_count = 0;
188 int ret = s2n_client_hello_get_supported_groups(client_hello, supported_groups, s2n_array_len(supported_groups),
189 &supported_groups_count);
190
191 if (disable_ecc) {
192 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_EXTENSION_NOT_RECEIVED);
193 EXPECT_EQUAL(supported_groups_count, 0);
194 } else {
195 EXPECT_SUCCESS(ret);
196
197 /* The 20170210 security policy contains 2 ECC curves. */
198 EXPECT_EQUAL(supported_groups_count, 2);
199 }
200 }
201
202 /* Test parsing a supported groups extension with a malformed groups list length. */
203 {
204 struct s2n_client_hello client_hello = { 0 };
205
206 s2n_parsed_extension *supported_groups_extension = &client_hello.extensions.parsed_extensions[supported_groups_id];
207 supported_groups_extension->extension_type = S2N_EXTENSION_SUPPORTED_GROUPS;
208
209 /* Test parsing a correct groups list length */
210 {
211 uint8_t extension_data[S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 };
212 struct s2n_blob extension_blob = { 0 };
213 EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data)));
214 supported_groups_extension->extension = extension_blob;
215
216 struct s2n_stuffer extension_stuffer = { 0 };
217 EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob));
218
219 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&extension_stuffer, S2N_TEST_SUPPORTED_GROUPS_LIST_SIZE));
220
221 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
222 uint16_t supported_groups_count = 0;
223 EXPECT_SUCCESS(s2n_client_hello_get_supported_groups(&client_hello, supported_groups,
224 s2n_array_len(supported_groups), &supported_groups_count));
225
226 EXPECT_EQUAL(supported_groups_count, S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT);
227 }
228
229 /* Test parsing a groups list length that is larger than the extension length */
230 {
231 uint8_t extension_data[S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 };
232 struct s2n_blob extension_blob = { 0 };
233 uint32_t extension_too_small_size = S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE - 2;
234 EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, extension_too_small_size));
235 supported_groups_extension->extension = extension_blob;
236
237 struct s2n_stuffer extension_stuffer = { 0 };
238 EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob));
239 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&extension_stuffer, S2N_TEST_SUPPORTED_GROUPS_LIST_SIZE));
240
241 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
242 uint16_t supported_groups_count = 0;
243 int ret = s2n_client_hello_get_supported_groups(&client_hello, supported_groups,
244 s2n_array_len(supported_groups), &supported_groups_count);
245 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_INVALID_PARSED_EXTENSIONS);
246
247 EXPECT_EQUAL(supported_groups_count, 0);
248 }
249
250 /* Test parsing a groups list that contains a malformed supported group */
251 {
252 uint8_t extension_data[S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 };
253 struct s2n_blob extension_blob = { 0 };
254 EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data)));
255 supported_groups_extension->extension = extension_blob;
256
257 struct s2n_stuffer extension_stuffer = { 0 };
258 EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob));
259
260 uint16_t one_and_a_half_groups_size = 3;
261 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&extension_stuffer, one_and_a_half_groups_size));
262
263 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
264 uint16_t supported_groups_count = 0;
265 int ret = s2n_client_hello_get_supported_groups(&client_hello, supported_groups,
266 s2n_array_len(supported_groups), &supported_groups_count);
267 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_INVALID_PARSED_EXTENSIONS);
268
269 EXPECT_EQUAL(supported_groups_count, 0);
270 }
271 }
272
273 /* Ensure that the supported groups in the client hello are written to the output array. */
274 {
275 struct s2n_client_hello client_hello = { 0 };
276
277 s2n_parsed_extension *supported_groups_extension = &client_hello.extensions.parsed_extensions[supported_groups_id];
278 supported_groups_extension->extension_type = S2N_EXTENSION_SUPPORTED_GROUPS;
279
280 for (uint16_t test_groups_count = 0; test_groups_count < S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT; test_groups_count++) {
281 uint16_t test_groups_list_size = test_groups_count * 2;
282
283 uint8_t test_groups_list_data[S2N_TEST_SUPPORTED_GROUPS_LIST_SIZE] = { 0 };
284 struct s2n_blob test_groups_list_blob = { 0 };
285 EXPECT_SUCCESS(s2n_blob_init(&test_groups_list_blob, test_groups_list_data, test_groups_list_size));
286 EXPECT_OK(s2n_get_public_random_data(&test_groups_list_blob));
287
288 uint8_t extension_data[S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 };
289 struct s2n_blob extension_blob = { 0 };
290 EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data)));
291 supported_groups_extension->extension = extension_blob;
292
293 struct s2n_stuffer extension_stuffer = { 0 };
294 EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob));
295 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&extension_stuffer, test_groups_list_size));
296 EXPECT_SUCCESS(s2n_stuffer_write(&extension_stuffer, &test_groups_list_blob));
297
298 uint16_t supported_groups[S2N_TEST_SUPPORTED_GROUPS_LIST_COUNT] = { 0 };
299 uint16_t supported_groups_count = 0;
300 EXPECT_SUCCESS(s2n_client_hello_get_supported_groups(&client_hello, supported_groups,
301 s2n_array_len(supported_groups), &supported_groups_count));
302 EXPECT_EQUAL(supported_groups_count, test_groups_count);
303
304 struct s2n_stuffer test_groups_list_stuffer = { 0 };
305 EXPECT_SUCCESS(s2n_stuffer_init_written(&test_groups_list_stuffer, &test_groups_list_blob));
306
307 for (size_t i = 0; i < supported_groups_count; i++) {
308 uint16_t test_group = 0;
309 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&test_groups_list_stuffer, &test_group));
310 uint16_t written_group = supported_groups[i];
311
312 EXPECT_EQUAL(test_group, written_group);
313 }
314 }
315 }
316
317 /* Self-talk: Ensure that the retrieved supported groups match what was sent by the client.
318 *
319 * This test also ensures that s2n_client_hello_get_supported_groups is usable from within the
320 * client hello callback.
321 */
322 {
323 /* Test security policies with a range of different ECC curves and KEM groups. */
324 const char *policies[] = {
325 "20170210",
326 "20190801",
327 "AWS-CRT-SDK-TLSv1.2-2023",
328 "20230317",
329 "20210816",
330 "PQ-TLS-1-2-2024-10-07",
331 "test_all"
332 };
333
334 for (int version_index = 0; version_index < s2n_array_len(policies); version_index++) {
335 const char *policy = policies[version_index];
336
337 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
338 EXPECT_NOT_NULL(client_config);
339 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
340 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, policy));
341
342 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
343 EXPECT_NOT_NULL(server_config);
344 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(server_config));
345 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, policy));
346 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
347
348 struct s2n_client_hello_context context = {
349 .invoked_count = 0,
350 };
351 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(server_config, s2n_check_received_supported_groups_cb,
352 &context));
353
354 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
355 EXPECT_NOT_NULL(client_conn);
356 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
357
358 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
359 EXPECT_NOT_NULL(server_conn);
360 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
361
362 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
363 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
364 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
365 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
366
367 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
368 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
369
370 uint8_t sent_supported_groups_data[S2N_TEST_SUPPORTED_GROUPS_EXTENSION_SIZE];
371 struct s2n_blob sent_supported_groups_blob = { 0 };
372 EXPECT_SUCCESS(s2n_blob_init(&sent_supported_groups_blob, sent_supported_groups_data,
373 s2n_array_len(sent_supported_groups_data)));
374
375 struct s2n_stuffer sent_supported_groups_stuffer = { 0 };
376 EXPECT_SUCCESS(s2n_stuffer_init(&sent_supported_groups_stuffer, &sent_supported_groups_blob));
377 EXPECT_SUCCESS(s2n_client_supported_groups_extension.send(client_conn, &sent_supported_groups_stuffer));
378 context.sent_supported_groups_extension = &sent_supported_groups_stuffer;
379
380 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
381
382 EXPECT_EQUAL(context.invoked_count, 1);
383 }
384 }
385
386 END_TEST();
387 }