Debian Codegraph @

crt/s2n/tests/unit/s2n_policy_defaults_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/policy/s2n_policy_defaults.h"
17
18 #include "s2n_test.h"
19 #include "testlib/s2n_testlib.h"
20
21 S2N_RESULT s2n_test_strict_compatibility(uint64_t start, uint64_t end,
22 struct s2n_test_cert_chain_list *cert_chains)
23 {
24 for (size_t client_i = start; client_i <= end; client_i++) {
25 for (size_t server_i = start; server_i <= end; server_i++) {
26 for (size_t cert_i = 0; cert_i < cert_chains->count; cert_i++) {
27 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
28 EXPECT_NOT_NULL(config);
29 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
30 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config,
31 cert_chains->chains[cert_i].chain));
32 EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED));
33
34 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
35 s2n_connection_ptr_free);
36 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
37 EXPECT_SUCCESS(s2n_connection_set_security_policy(client,
38 s2n_security_policy_get(S2N_POLICY_STRICT, client_i)));
39
40 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
41 s2n_connection_ptr_free);
42 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
43 EXPECT_SUCCESS(s2n_connection_set_security_policy(server,
44 s2n_security_policy_get(S2N_POLICY_STRICT, server_i)));
45
46 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair, s2n_io_stuffer_pair_free);
47 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
48 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
49 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
50
51 /* For now, consider a HRR a potential breaking change.
52 * We can revisit later if necessary.
53 */
54 EXPECT_FALSE(IS_HELLO_RETRY_HANDSHAKE(server));
55 }
56 }
57 }
58 return S2N_RESULT_OK;
59 }
60
61 int main(int argc, char **argv)
62 {
63 BEGIN_TEST();
64
65 /* Test: s2n_security_policy_get */
66 {
67 /* Policies exist only for expected policy + version combinations */
68 for (size_t policy_i = 0; policy_i < UINT8_MAX; policy_i++) {
69 bool first_null_found = false;
70 uint64_t versions_found = 0;
71
72 for (size_t version_i = 0; version_i < UINT8_MAX; version_i++) {
73 const struct s2n_security_policy *policy = s2n_security_policy_get(policy_i, version_i);
74
75 /* Invalid policy or version values should be NULL */
76 if (version_i == 0 || policy_i == 0) {
77 /* Versioning starts at 1 instead of 0.
78 * We may want to later assign 0 a special meaning, like "none".
79 */
80 EXPECT_NULL_WITH_ERRNO(policy, S2N_ERR_INVALID_SECURITY_POLICY);
81 continue;
82 } else if (policy_i >= S2N_MAX_DEFAULT_POLICIES) {
83 EXPECT_NULL_WITH_ERRNO(policy, S2N_ERR_INVALID_SECURITY_POLICY);
84 continue;
85 } else if (version_i >= S2N_MAX_POLICY_VERSIONS) {
86 EXPECT_NULL_WITH_ERRNO(policy, S2N_ERR_INVALID_SECURITY_POLICY);
87 continue;
88 }
89
90 if (policy) {
91 /* The policy exists because the version is valid.
92 * Versions should be contiguous. No previous gaps.
93 */
94 EXPECT_FALSE(first_null_found);
95 versions_found++;
96 } else if (first_null_found) {
97 /* If we've already found the first invalid version, all later
98 * versions should be invalid too.
99 */
100 EXPECT_NULL_WITH_ERRNO(policy, S2N_ERR_INVALID_SECURITY_POLICY);
101 } else {
102 /* We have found the first invalid version */
103 first_null_found = true;
104 EXPECT_NULL_WITH_ERRNO(policy, S2N_ERR_INVALID_SECURITY_POLICY);
105 }
106 }
107
108 if (policy_i > 0 && policy_i < S2N_MAX_DEFAULT_POLICIES) {
109 /* Each valid policy should have at least one valid version */
110 EXPECT_TRUE(versions_found > 0);
111 /* If we did't find the last valid version, we're not testing enough */
112 EXPECT_TRUE(first_null_found);
113 }
114 };
115
116 /* Check known good values.
117 * Note: don't add EVERY new version to this test. We should only test
118 * an interesting selection of inputs.
119 */
120 {
121 EXPECT_NOT_NULL(s2n_security_policy_get(S2N_POLICY_STRICT, S2N_STRICT_2025_08_20));
122 EXPECT_NOT_NULL(s2n_security_policy_get(S2N_POLICY_COMPATIBLE, S2N_COMPAT_2025_08_20));
123 }
124 };
125
126 /* Test: S2N_POLICY_STRICT backwards compatibility promises enforced */
127 if (s2n_is_tls13_fully_supported()) {
128 DEFER_CLEANUP(struct s2n_test_cert_chain_list cert_chains = { 0 }, s2n_test_cert_chains_free);
129 EXPECT_OK(s2n_test_cert_chains_init(&cert_chains));
130
131 EXPECT_OK(s2n_test_strict_compatibility(1, S2N_STRICT_LATEST_1, &cert_chains));
132 };
133
134 END_TEST();
135 }