Debian Codegraph @

crt/s2n/tests/unit/s2n_choose_supported_group_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 #include "s2n_test.h"
16 #include "testlib/s2n_testlib.h"
17 #include "tls/extensions/s2n_client_supported_groups.h"
18 #include "tls/s2n_kem_preferences.h"
19 #include "tls/s2n_security_policies.h"
20 #include "tls/s2n_tls.h"
21 #include "tls/s2n_tls13.h"
22
23 /* Include the C file directly to allow testing of static functions */
24 #include "tls/extensions/s2n_client_supported_groups.c"
25
26 /* This test checks the logic in the function s2n_choose_supported_group, which should select the highest
27 * supported group or, if none are available, select NULL. */
28 int main()
29 {
30 BEGIN_TEST();
31 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
32
33 /* Tests with 20240501 KEM preferences (kem_preferences_null) */
34 {
35 /* If the lists of mutually supported groups are empty, chosen group should be set to null */
36 {
37 struct s2n_connection *server_conn = NULL;
38 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
39 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "20240501"));
40
41 const struct s2n_ecc_preferences *ecc_pref = NULL;
42 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(server_conn, &ecc_pref));
43 EXPECT_NOT_NULL(ecc_pref);
44
45 const struct s2n_kem_preferences *kem_pref = NULL;
46 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(server_conn, &kem_pref));
47 EXPECT_NOT_NULL(kem_pref);
48 EXPECT_EQUAL(kem_pref, &kem_preferences_null);
49
50 for (size_t i = 0; i < ecc_pref->count; i++) {
51 EXPECT_NULL(server_conn->kex_params.mutually_supported_curves[i]);
52 }
53
54 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
55 EXPECT_NULL(server_conn->kex_params.mutually_supported_kem_groups[i]);
56 }
57
58 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
59 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
60 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
61 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
62
63 EXPECT_SUCCESS(s2n_choose_supported_group(server_conn));
64
65 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
66 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
67 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
68 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
69 EXPECT_SUCCESS(s2n_connection_free(server_conn));
70 };
71
72 /* If the lists of mutually supported groups have one ECC match,
73 * the chosen group should be set to the ECC match. */
74 {
75 struct s2n_connection *server_conn = NULL;
76 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
77 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "20240501"));
78
79 const struct s2n_ecc_preferences *ecc_pref = NULL;
80 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(server_conn, &ecc_pref));
81 EXPECT_NOT_NULL(ecc_pref);
82
83 const struct s2n_kem_preferences *kem_pref = NULL;
84 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(server_conn, &kem_pref));
85 EXPECT_NOT_NULL(kem_pref);
86 EXPECT_EQUAL(kem_pref, &kem_preferences_null);
87
88 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
89 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
90 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
91 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
92
93 for (size_t i = 0; i < ecc_pref->count; i++) {
94 EXPECT_NULL(server_conn->kex_params.mutually_supported_curves[i]);
95 }
96
97 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
98 EXPECT_NULL(server_conn->kex_params.mutually_supported_kem_groups[i]);
99 }
100
101 server_conn->kex_params.mutually_supported_curves[1] = ecc_pref->ecc_curves[1];
102 EXPECT_SUCCESS(s2n_choose_supported_group(server_conn));
103
104 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
105 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
106 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
107 EXPECT_EQUAL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve, ecc_pref->ecc_curves[1]);
108 EXPECT_SUCCESS(s2n_connection_free(server_conn));
109 };
110
111 /* If the lists of mutually supported groups have several matches, the chosen group should be set to
112 * the highest supported ECC. */
113 {
114 struct s2n_connection *server_conn = NULL;
115 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
116 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "20240501"));
117
118 const struct s2n_ecc_preferences *ecc_pref = NULL;
119 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(server_conn, &ecc_pref));
120 EXPECT_NOT_NULL(ecc_pref);
121
122 const struct s2n_kem_preferences *kem_pref = NULL;
123 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(server_conn, &kem_pref));
124 EXPECT_NOT_NULL(kem_pref);
125 EXPECT_EQUAL(kem_pref, &kem_preferences_null);
126
127 for (size_t i = 0; i < ecc_pref->count; i++) {
128 server_conn->kex_params.mutually_supported_curves[i] = ecc_pref->ecc_curves[i];
129 }
130
131 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
132 EXPECT_NULL(server_conn->kex_params.mutually_supported_kem_groups[i]);
133 }
134
135 EXPECT_SUCCESS(s2n_choose_supported_group(server_conn));
136
137 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
138 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
139 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
140 EXPECT_EQUAL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve, ecc_pref->ecc_curves[0]);
141 EXPECT_SUCCESS(s2n_connection_free(server_conn));
142 };
143 };
144
145 /* Test for PQ */
146 {
147 const struct s2n_kem_preferences test_kem_prefs = {
148 .kem_count = 0,
149 .kems = NULL,
150 .tls13_kem_group_count = kem_preferences_all.tls13_kem_group_count,
151 .tls13_kem_groups = kem_preferences_all.tls13_kem_groups,
152 };
153
154 const struct s2n_security_policy test_pq_security_policy = {
155 .minimum_protocol_version = S2N_SSLv3,
156 .cipher_preferences = &cipher_preferences_test_all_tls13,
157 .kem_preferences = &test_kem_prefs,
158 .signature_preferences = &s2n_signature_preferences_20200207,
159 .ecc_preferences = &s2n_ecc_preferences_20200310,
160 };
161
162 /* If the server supports PQ, but the client didn't send any PQ IDs, mutually_supported_kem_groups will
163 * not be populated, and the highest preference ECC should be chosen. */
164 {
165 struct s2n_connection *server_conn = NULL;
166 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
167 server_conn->security_policy_override = &test_pq_security_policy;
168
169 const struct s2n_ecc_preferences *ecc_pref = NULL;
170 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(server_conn, &ecc_pref));
171 EXPECT_NOT_NULL(ecc_pref);
172
173 const struct s2n_kem_preferences *kem_pref = NULL;
174 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(server_conn, &kem_pref));
175 EXPECT_NOT_NULL(kem_pref);
176 EXPECT_EQUAL(kem_pref, &test_kem_prefs);
177
178 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
179 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
180 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
181 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
182
183 for (size_t i = 0; i < ecc_pref->count; i++) {
184 server_conn->kex_params.mutually_supported_curves[i] = ecc_pref->ecc_curves[i];
185 }
186
187 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
188 EXPECT_NULL(server_conn->kex_params.mutually_supported_kem_groups[i]);
189 }
190
191 EXPECT_SUCCESS(s2n_choose_supported_group(server_conn));
192
193 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
194 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
195 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
196 EXPECT_EQUAL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve, ecc_pref->ecc_curves[0]);
197 EXPECT_SUCCESS(s2n_connection_free(server_conn));
198 };
199
200 /* If server has multiple mutually supported KEM groups and ECC curves, the highest preferred KEM group
201 * should be chosen. */
202 {
203 struct s2n_connection *server_conn = NULL;
204 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
205 server_conn->security_policy_override = &test_pq_security_policy;
206
207 const struct s2n_ecc_preferences *ecc_pref = NULL;
208 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(server_conn, &ecc_pref));
209 EXPECT_NOT_NULL(ecc_pref);
210
211 const struct s2n_kem_preferences *kem_pref = NULL;
212 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(server_conn, &kem_pref));
213 EXPECT_NOT_NULL(kem_pref);
214 EXPECT_EQUAL(kem_pref, &test_kem_prefs);
215
216 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
217 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
218 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
219 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
220
221 for (size_t i = 0; i < ecc_pref->count; i++) {
222 server_conn->kex_params.mutually_supported_curves[i] = ecc_pref->ecc_curves[i];
223 }
224
225 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
226 server_conn->kex_params.mutually_supported_kem_groups[i] = kem_pref->tls13_kem_groups[i];
227 }
228
229 EXPECT_SUCCESS(s2n_choose_supported_group(server_conn));
230
231 const struct s2n_kem_group *kem_group = s2n_kem_preferences_get_highest_priority_group(kem_pref);
232 if (s2n_pq_is_enabled()) {
233 EXPECT_NOT_NULL(kem_group);
234 EXPECT_EQUAL(server_conn->kex_params.server_kem_group_params.kem_group, kem_group);
235 EXPECT_EQUAL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve, kem_group->curve);
236 EXPECT_EQUAL(server_conn->kex_params.server_kem_group_params.kem_params.kem, kem_group->kem);
237 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
238 } else {
239 EXPECT_NULL(kem_group);
240 }
241 EXPECT_SUCCESS(s2n_connection_free(server_conn));
242 };
243 /* If server has one mutually supported KEM group and multiple mutually supported ECC, the KEM
244 * group should be chosen.
245 * Need at least one available KEM to test fallback. */
246 uint32_t available_groups = 0;
247 EXPECT_OK(s2n_kem_preferences_groups_available(&kem_preferences_all, &available_groups));
248 if (available_groups >= 1) {
249 struct s2n_connection *server_conn = NULL;
250 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
251 server_conn->security_policy_override = &test_pq_security_policy;
252
253 const struct s2n_ecc_preferences *ecc_pref = NULL;
254 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(server_conn, &ecc_pref));
255 EXPECT_NOT_NULL(ecc_pref);
256
257 const struct s2n_kem_preferences *kem_pref = NULL;
258 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(server_conn, &kem_pref));
259 EXPECT_NOT_NULL(kem_pref);
260 EXPECT_EQUAL(kem_pref, &test_kem_prefs);
261
262 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
263 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
264 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
265 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
266
267 for (size_t i = 0; i < ecc_pref->count; i++) {
268 server_conn->kex_params.mutually_supported_curves[i] = ecc_pref->ecc_curves[i];
269 }
270
271 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
272 EXPECT_NULL(server_conn->kex_params.mutually_supported_kem_groups[i]);
273 }
274
275 const struct s2n_kem_group *chosen_group = s2n_kem_preferences_get_highest_priority_group(kem_pref);
276 EXPECT_NOT_NULL(chosen_group);
277 server_conn->kex_params.mutually_supported_kem_groups[0] = chosen_group;
278 EXPECT_SUCCESS(s2n_choose_supported_group(server_conn));
279
280 EXPECT_EQUAL(server_conn->kex_params.server_kem_group_params.kem_group, chosen_group);
281 EXPECT_EQUAL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve, chosen_group->curve);
282 EXPECT_EQUAL(server_conn->kex_params.server_kem_group_params.kem_params.kem, chosen_group->kem);
283 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
284 EXPECT_SUCCESS(s2n_connection_free(server_conn));
285 }
286 /* If there are no mutually supported KEM groups or ECC curves, chosen group should be set to null */
287 {
288 struct s2n_connection *server_conn = NULL;
289 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
290 server_conn->security_policy_override = &test_pq_security_policy;
291
292 const struct s2n_ecc_preferences *ecc_pref = NULL;
293 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(server_conn, &ecc_pref));
294 EXPECT_NOT_NULL(ecc_pref);
295
296 const struct s2n_kem_preferences *kem_pref = NULL;
297 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(server_conn, &kem_pref));
298 EXPECT_NOT_NULL(kem_pref);
299 EXPECT_EQUAL(kem_pref, &test_kem_prefs);
300
301 for (size_t i = 0; i < ecc_pref->count; i++) {
302 EXPECT_NULL(server_conn->kex_params.mutually_supported_curves[i]);
303 }
304
305 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
306 EXPECT_NULL(server_conn->kex_params.mutually_supported_kem_groups[i]);
307 }
308
309 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
310 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
311 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
312 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
313
314 EXPECT_SUCCESS(s2n_choose_supported_group(server_conn));
315
316 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_group);
317 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve);
318 EXPECT_NULL(server_conn->kex_params.server_kem_group_params.kem_params.kem);
319 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
320 EXPECT_SUCCESS(s2n_connection_free(server_conn));
321 };
322 };
323
324 END_TEST();
325 }