Debian Codegraph @

crt/s2n/tests/unit/s2n_strongly_preferred_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 "tests/s2n_test.h"
20 #include "tls/s2n_kem.h"
21 #include "tls/s2n_security_policies.h"
22 #include "tls/s2n_supported_group_preferences.h"
23 #include "tls/s2n_tls.h"
24
25 int main(int argc, char **argv)
26 {
27 BEGIN_TEST();
28
29 /* Enforce minimum requirements on all security policies that have strongly preferred groups */
30 for (size_t policy_index = 0; security_policy_selection[policy_index].version != NULL; policy_index++) {
31 const struct s2n_security_policy_selection selection = security_policy_selection[policy_index];
32 const char *policy_name = selection.version;
33 const struct s2n_security_policy *security_policy = selection.security_policy;
34 POSIX_ENSURE_REF(security_policy);
35
36 if (security_policy->strongly_preferred_groups == NULL || security_policy->strongly_preferred_groups->count == 0) {
37 continue;
38 }
39
40 POSIX_ENSURE_REF(security_policy->strongly_preferred_groups);
41
42 uint16_t ordered_supported_groups[S2N_KEM_GROUPS_COUNT + S2N_ECC_EVP_SUPPORTED_CURVES_COUNT] = { 0 };
43 uint16_t ordered_supported_group_count = 0;
44
45 POSIX_ENSURE_REF(security_policy->kem_preferences);
46
47 /* Temporarily require that policies don't allow both PQ and strongly supported groups. */
48 EXPECT_EQUAL(0, security_policy->kem_preferences->tls13_kem_group_count);
49
50 POSIX_ENSURE_REF(security_policy->ecc_preferences);
51 for (size_t i = 0; i < security_policy->ecc_preferences->count; i++) {
52 const struct s2n_ecc_named_curve *ecc_curve = security_policy->ecc_preferences->ecc_curves[i];
53 POSIX_ENSURE_REF(ecc_curve);
54
55 ordered_supported_groups[ordered_supported_group_count++] = ecc_curve->iana_id;
56 }
57
58 /* Ensure ordering of strongly preferred IANA's matches the PQ and ECC preference ordering. */
59 for (size_t i = 0; i < security_policy->strongly_preferred_groups->count; i++) {
60 const uint16_t strongly_preferred_iana = security_policy->strongly_preferred_groups->iana_ids[i];
61 const uint16_t standard_preferred_iana = ordered_supported_groups[i];
62
63 /* Ensure that PQ supported groups aren't skipped as that could cause downgrades to ECC. */
64 if (strongly_preferred_iana != standard_preferred_iana) {
65 fprintf(stderr, "Error with Security Policy: %s\n", policy_name);
66 FAIL_MSG("The strongly preferred groups should be a prefix of the standard SupportedGroup preference list.");
67 }
68
69 EXPECT_EQUAL(strongly_preferred_iana, standard_preferred_iana);
70 }
71 }
72
73 END_TEST();
74 }