Debian Codegraph @

crt/s2n/tests/unit/s2n_security_rules_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_security_rules.h"
17
18 #include "s2n_test.h"
19 #include "tls/s2n_cipher_preferences.h"
20
21 S2N_RESULT s2n_security_rule_validate_policy(
22 const struct s2n_security_rule *rule,
23 const struct s2n_security_policy *policy,
24 struct s2n_security_rule_result *result);
25
26 struct s2n_cipher_suite *VALID_CIPHER_SUITE = &s2n_tls13_aes_256_gcm_sha384;
27 struct s2n_cipher_suite *EXAMPLE_INVALID_CIPHER_SUITE = &s2n_tls13_aes_128_gcm_sha256;
28 struct s2n_cipher_suite *EXAMPLE_INVALID_CIPHER_SUITE_2 = &s2n_tls13_chacha20_poly1305_sha256;
29 static S2N_RESULT s2n_test_cipher_suite_rule(const struct s2n_cipher_suite *cipher_suite, bool *valid)
30 {
31 RESULT_ENSURE_REF(valid);
32 if (cipher_suite == VALID_CIPHER_SUITE) {
33 *valid = true;
34 } else {
35 *valid = false;
36 }
37 return S2N_RESULT_OK;
38 }
39
40 const struct s2n_signature_scheme *VALID_SIG_SCHEME = &s2n_ecdsa_sha256;
41 const struct s2n_signature_scheme *EXAMPLE_INVALID_SIG_SCHEME = &s2n_ecdsa_sha384;
42 static S2N_RESULT s2n_test_sig_scheme_rule(const struct s2n_signature_scheme *sig_scheme, bool *valid)
43 {
44 RESULT_ENSURE_REF(valid);
45 if (sig_scheme == VALID_SIG_SCHEME) {
46 *valid = true;
47 } else {
48 *valid = false;
49 }
50 return S2N_RESULT_OK;
51 }
52
53 const struct s2n_ecc_named_curve *VALID_CURVE = &s2n_ecc_curve_secp256r1;
54 const struct s2n_ecc_named_curve *EXAMPLE_INVALID_CURVE = &s2n_ecc_curve_secp384r1;
55 static S2N_RESULT s2n_test_curve_rule(const struct s2n_ecc_named_curve *curve, bool *valid)
56 {
57 RESULT_ENSURE_REF(valid);
58 if (curve == VALID_CURVE) {
59 *valid = true;
60 } else {
61 *valid = false;
62 }
63 return S2N_RESULT_OK;
64 }
65
66 const struct s2n_kem_group *VALID_HYBRID_GROUP = &s2n_secp256r1_mlkem_768;
67 const struct s2n_kem_group *EXAMPLE_INVALID_HYBRID_GROUP = &s2n_pure_mlkem_1024;
68 static S2N_RESULT s2n_test_hybrid_group_rule(const struct s2n_kem_group *hybrid_group, bool *valid)
69 {
70 RESULT_ENSURE_REF(valid);
71 if (hybrid_group == VALID_HYBRID_GROUP) {
72 *valid = true;
73 } else {
74 *valid = false;
75 }
76 return S2N_RESULT_OK;
77 }
78
79 const uint8_t VALID_VERSION = S2N_TLS12;
80 const uint8_t EXAMPLE_INVALID_VERSION = S2N_TLS11;
81 static S2N_RESULT s2n_test_version(uint8_t version, bool *valid)
82 {
83 RESULT_ENSURE_REF(valid);
84 if (version == VALID_VERSION) {
85 *valid = true;
86 } else {
87 *valid = false;
88 }
89 return S2N_RESULT_OK;
90 }
91
92 int main(int argc, char **argv)
93 {
94 BEGIN_TEST();
95
96 const struct s2n_security_rule test_rule = {
97 .name = "Test Rule",
98 .validate_cipher_suite = s2n_test_cipher_suite_rule,
99 .validate_sig_scheme = s2n_test_sig_scheme_rule,
100 .validate_cert_sig_scheme = s2n_test_sig_scheme_rule,
101 .validate_curve = s2n_test_curve_rule,
102 .validate_hybrid_group = s2n_test_hybrid_group_rule,
103 .validate_version = s2n_test_version,
104 };
105
106 const struct s2n_cipher_preferences valid_cipher_prefs = {
107 .suites = &VALID_CIPHER_SUITE,
108 .count = 1,
109 };
110 struct s2n_cipher_suite *invalid_cipher_suites[] = {
111 EXAMPLE_INVALID_CIPHER_SUITE,
112 VALID_CIPHER_SUITE,
113 EXAMPLE_INVALID_CIPHER_SUITE_2,
114 };
115 const struct s2n_cipher_preferences invalid_cipher_prefs = {
116 .suites = invalid_cipher_suites,
117 .count = s2n_array_len(invalid_cipher_suites),
118 };
119
120 const struct s2n_signature_preferences valid_sig_prefs = {
121 .signature_schemes = &VALID_SIG_SCHEME,
122 .count = 1,
123 };
124 const struct s2n_signature_preferences invalid_sig_prefs = {
125 .signature_schemes = &EXAMPLE_INVALID_SIG_SCHEME,
126 .count = 1,
127 };
128
129 const struct s2n_ecc_preferences valid_ecc_prefs = {
130 .ecc_curves = &VALID_CURVE,
131 .count = 1,
132 };
133 const struct s2n_ecc_preferences invalid_ecc_prefs = {
134 .ecc_curves = &EXAMPLE_INVALID_CURVE,
135 .count = 1,
136 };
137
138 const struct s2n_kem_preferences valid_kem_preferences = {
139 .kem_count = 0,
140 .kems = NULL,
141 .tls13_kem_groups = &VALID_HYBRID_GROUP,
142 .tls13_kem_group_count = 1,
143 };
144
145 const struct s2n_kem_preferences invalid_kem_preferences = {
146 .kem_count = 0,
147 .kems = NULL,
148 .tls13_kem_groups = &EXAMPLE_INVALID_HYBRID_GROUP,
149 .tls13_kem_group_count = 1,
150 };
151
152 const struct s2n_security_policy valid_policy = {
153 .cipher_preferences = &valid_cipher_prefs,
154 .signature_preferences = &valid_sig_prefs,
155 .certificate_signature_preferences = &valid_sig_prefs,
156 .ecc_preferences = &valid_ecc_prefs,
157 .kem_preferences = &valid_kem_preferences,
158 .minimum_protocol_version = VALID_VERSION,
159 };
160
161 const struct s2n_security_policy invalid_policy = {
162 .cipher_preferences = &invalid_cipher_prefs,
163 .signature_preferences = &invalid_sig_prefs,
164 .certificate_signature_preferences = &invalid_sig_prefs,
165 .ecc_preferences = &invalid_ecc_prefs,
166 .kem_preferences = &invalid_kem_preferences,
167 .minimum_protocol_version = EXAMPLE_INVALID_VERSION,
168 };
169
170 /* Test s2n_security_rule_validate_policy */
171 {
172 /* Test: Marks valid policy as valid */
173 {
174 struct s2n_security_rule_result result = { 0 };
175 EXPECT_OK(s2n_security_rule_validate_policy(
176 &test_rule, &valid_policy, &result));
177 EXPECT_FALSE(result.found_error);
178 };
179
180 /* Test: Marks invalid policy as invalid */
181 {
182 /* Test: Entire policy invalid */
183 {
184 struct s2n_security_rule_result result = { 0 };
185 EXPECT_OK(s2n_security_rule_validate_policy(
186 &test_rule, &invalid_policy, &result));
187 EXPECT_TRUE(result.found_error);
188 };
189
190 /* Test: only cipher suite invalid */
191 {
192 struct s2n_security_policy test_policy = valid_policy;
193 test_policy.cipher_preferences = &invalid_cipher_prefs;
194
195 struct s2n_security_rule_result result = { 0 };
196 EXPECT_OK(s2n_security_rule_validate_policy(
197 &test_rule, &test_policy, &result));
198 EXPECT_TRUE(result.found_error);
199 };
200
201 /* Test: only sig scheme invalid */
202 {
203 struct s2n_security_policy test_policy = valid_policy;
204 test_policy.signature_preferences = &invalid_sig_prefs;
205
206 struct s2n_security_rule_result result = { 0 };
207 EXPECT_OK(s2n_security_rule_validate_policy(
208 &test_rule, &test_policy, &result));
209 EXPECT_TRUE(result.found_error);
210 };
211
212 /* Test: only cert sig scheme invalid */
213 {
214 struct s2n_security_policy test_policy = valid_policy;
215 test_policy.certificate_signature_preferences = &invalid_sig_prefs;
216
217 struct s2n_security_rule_result result = { 0 };
218 EXPECT_OK(s2n_security_rule_validate_policy(
219 &test_rule, &test_policy, &result));
220 EXPECT_TRUE(result.found_error);
221 };
222
223 /* Test: only curve invalid */
224 {
225 struct s2n_security_policy test_policy = valid_policy;
226 test_policy.ecc_preferences = &invalid_ecc_prefs;
227
228 struct s2n_security_rule_result result = { 0 };
229 EXPECT_OK(s2n_security_rule_validate_policy(
230 &test_rule, &test_policy, &result));
231 EXPECT_TRUE(result.found_error);
232 };
233
234 /* Test: only hybrid group invalid */
235 {
236 struct s2n_security_policy test_policy = valid_policy;
237 test_policy.kem_preferences = &invalid_kem_preferences;
238
239 struct s2n_security_rule_result result = { 0 };
240 EXPECT_OK(s2n_security_rule_validate_policy(
241 &test_rule, &test_policy, &result));
242 EXPECT_TRUE(result.found_error);
243 };
244
245 /* Test: only version invalid */
246 {
247 struct s2n_security_policy test_policy = valid_policy;
248 test_policy.minimum_protocol_version = EXAMPLE_INVALID_VERSION;
249
250 struct s2n_security_rule_result result = { 0 };
251 EXPECT_OK(s2n_security_rule_validate_policy(
252 &test_rule, &test_policy, &result));
253 EXPECT_TRUE(result.found_error);
254 };
255 };
256
257 /* Test: skips certificate signature preferences if not present */
258 {
259 struct s2n_security_policy test_policy = valid_policy;
260 test_policy.certificate_signature_preferences = NULL;
261
262 struct s2n_security_rule_result result = { 0 };
263 EXPECT_OK(s2n_security_rule_validate_policy(
264 &test_rule, &test_policy, &result));
265 EXPECT_FALSE(result.found_error);
266 };
267
268 /* Test: writes output for errors
269 *
270 * These tests are little brittle because they include hard-coded outputs.
271 * I think it's worthwhile to clearly verify the output.
272 */
273 {
274 /* Test: Writes no output if output not enabled */
275 {
276 struct s2n_security_policy test_policy = valid_policy;
277 test_policy.ecc_preferences = &invalid_ecc_prefs;
278
279 DEFER_CLEANUP(struct s2n_security_rule_result result = { 0 },
280 s2n_security_rule_result_free);
281
282 EXPECT_OK(s2n_security_rule_validate_policy(
283 &test_rule, &test_policy, &result));
284 EXPECT_TRUE(result.found_error);
285
286 size_t output_size = s2n_stuffer_data_available(&result.output);
287 EXPECT_EQUAL(output_size, 0);
288 };
289
290 /* Test: Writes single line of output */
291 {
292 struct s2n_security_policy test_policy = valid_policy;
293 test_policy.ecc_preferences = &invalid_ecc_prefs;
294 const char expected_output[] =
295 "Test Rule: policy unnamed: curve: secp384r1 (#1)\n";
296
297 DEFER_CLEANUP(struct s2n_security_rule_result result = { 0 },
298 s2n_security_rule_result_free);
299 EXPECT_OK(s2n_security_rule_result_init_output(&result));
300
301 EXPECT_OK(s2n_security_rule_validate_policy(
302 &test_rule, &test_policy, &result));
303 EXPECT_TRUE(result.found_error);
304
305 size_t output_size = s2n_stuffer_data_available(&result.output);
306 EXPECT_EQUAL(output_size, strlen(expected_output));
307 uint8_t *output_bytes = s2n_stuffer_raw_read(&result.output, output_size);
308 EXPECT_NOT_NULL(output_bytes);
309 EXPECT_BYTEARRAY_EQUAL(expected_output, output_bytes, output_size);
310 };
311
312 /* Test: Writes multiple lines of output for same field */
313 {
314 struct s2n_security_policy test_policy = valid_policy;
315 test_policy.cipher_preferences = &invalid_cipher_prefs;
316 const char expected_output[] =
317 "Test Rule: policy unnamed: cipher suite: TLS_AES_128_GCM_SHA256 (#1)\n"
318 "Test Rule: policy unnamed: cipher suite: TLS_CHACHA20_POLY1305_SHA256 (#3)\n";
319
320 DEFER_CLEANUP(struct s2n_security_rule_result result = { 0 },
321 s2n_security_rule_result_free);
322 EXPECT_OK(s2n_security_rule_result_init_output(&result));
323
324 EXPECT_OK(s2n_security_rule_validate_policy(
325 &test_rule, &test_policy, &result));
326 EXPECT_TRUE(result.found_error);
327
328 size_t output_size = s2n_stuffer_data_available(&result.output);
329 EXPECT_EQUAL(output_size, strlen(expected_output));
330 uint8_t *output_bytes = s2n_stuffer_raw_read(&result.output, output_size);
331 EXPECT_NOT_NULL(output_bytes);
332 EXPECT_BYTEARRAY_EQUAL(expected_output, output_bytes, output_size);
333 };
334
335 /* Test: Writes multiple lines of output for different fields */
336 {
337 struct s2n_security_policy test_policy = valid_policy;
338 test_policy.cipher_preferences = &invalid_cipher_prefs;
339 test_policy.ecc_preferences = &invalid_ecc_prefs;
340 const char expected_output[] =
341 "Test Rule: policy unnamed: cipher suite: TLS_AES_128_GCM_SHA256 (#1)\n"
342 "Test Rule: policy unnamed: cipher suite: TLS_CHACHA20_POLY1305_SHA256 (#3)\n"
343 "Test Rule: policy unnamed: curve: secp384r1 (#1)\n";
344
345 DEFER_CLEANUP(struct s2n_security_rule_result result = { 0 },
346 s2n_security_rule_result_free);
347 EXPECT_OK(s2n_security_rule_result_init_output(&result));
348
349 EXPECT_OK(s2n_security_rule_validate_policy(
350 &test_rule, &test_policy, &result));
351 EXPECT_TRUE(result.found_error);
352
353 size_t output_size = s2n_stuffer_data_available(&result.output);
354 EXPECT_EQUAL(output_size, strlen(expected_output));
355 uint8_t *output_bytes = s2n_stuffer_raw_read(&result.output, output_size);
356 EXPECT_NOT_NULL(output_bytes);
357 EXPECT_BYTEARRAY_EQUAL(expected_output, output_bytes, output_size);
358 };
359
360 /* Writes correct name for versioned policy */
361 {
362 const char expected_prefix[] =
363 "Test Rule: policy test_all: ";
364 const size_t expected_prefix_size = strlen(expected_prefix);
365
366 DEFER_CLEANUP(struct s2n_security_rule_result result = { 0 },
367 s2n_security_rule_result_free);
368 EXPECT_OK(s2n_security_rule_result_init_output(&result));
369
370 EXPECT_OK(s2n_security_rule_validate_policy(
371 &test_rule, &security_policy_test_all, &result));
372 EXPECT_TRUE(result.found_error);
373
374 size_t output_size = s2n_stuffer_data_available(&result.output);
375 EXPECT_TRUE(output_size > expected_prefix_size);
376 uint8_t *output_bytes = s2n_stuffer_raw_read(&result.output, expected_prefix_size);
377 EXPECT_NOT_NULL(output_bytes);
378 EXPECT_BYTEARRAY_EQUAL(output_bytes, expected_prefix, expected_prefix_size);
379 };
380 };
381 };
382
383 /* Test s2n_security_policy_validate_security_rules
384 * (and S2N_PERFECT_FORWARD_SECRECY-- we need to test with a real rule) */
385 {
386 struct s2n_cipher_suite *not_forward_secret_suite = &s2n_rsa_with_aes_128_gcm_sha256;
387 const struct s2n_cipher_preferences not_forward_secret_prefs = {
388 .suites = &not_forward_secret_suite,
389 .count = 1,
390 };
391
392 struct s2n_cipher_suite *forward_secret_suites[] = {
393 &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256,
394 &s2n_tls13_aes_256_gcm_sha384,
395 };
396 const struct s2n_cipher_preferences forward_secret_prefs = {
397 .suites = forward_secret_suites,
398 .count = s2n_array_len(forward_secret_suites),
399 };
400
401 struct s2n_security_policy test_policy = valid_policy;
402
403 /* Test: valid policy passes */
404 {
405 test_policy.rules[S2N_PERFECT_FORWARD_SECRECY] = true;
406 test_policy.cipher_preferences = &forward_secret_prefs;
407
408 struct s2n_security_rule_result result = { 0 };
409 EXPECT_OK(s2n_security_policy_validate_security_rules(&test_policy, &result));
410 EXPECT_FALSE(result.found_error);
411 };
412
413 /* Test: invalid policy fails */
414 {
415 test_policy.rules[S2N_PERFECT_FORWARD_SECRECY] = true;
416 test_policy.cipher_preferences = &not_forward_secret_prefs;
417
418 struct s2n_security_rule_result result = { 0 };
419 EXPECT_OK(s2n_security_policy_validate_security_rules(&test_policy, &result));
420 EXPECT_TRUE(result.found_error);
421 };
422
423 /* Test: valid policy without rule passes */
424 {
425 test_policy.rules[S2N_PERFECT_FORWARD_SECRECY] = false;
426 test_policy.cipher_preferences = &forward_secret_prefs;
427
428 struct s2n_security_rule_result result = { 0 };
429 EXPECT_OK(s2n_security_policy_validate_security_rules(&test_policy, &result));
430 EXPECT_FALSE(result.found_error);
431 };
432
433 /* Test: invalid policy without rule passes */
434 {
435 test_policy.rules[S2N_PERFECT_FORWARD_SECRECY] = false;
436 test_policy.cipher_preferences = &not_forward_secret_prefs;
437
438 struct s2n_security_rule_result result = { 0 };
439 EXPECT_OK(s2n_security_policy_validate_security_rules(&test_policy, &result));
440 EXPECT_FALSE(result.found_error);
441 };
442 };
443
444 END_TEST();
445 }