Debian Codegraph @

crt/s2n/tests/unit/s2n_fips_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 "crypto/s2n_fips.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19
20 S2N_RESULT s2n_fips_validate_hash_algorithm(s2n_hash_algorithm hash_alg, bool *valid);
21
22 int main(int argc, char **argv)
23 {
24 BEGIN_TEST();
25
26 /* Test s2n_fips_validate_cipher_suite */
27 {
28 /* Safety */
29 {
30 bool is_valid = false;
31 EXPECT_ERROR_WITH_ERRNO(
32 s2n_fips_validate_cipher_suite(NULL, &is_valid),
33 S2N_ERR_NULL);
34 EXPECT_ERROR_WITH_ERRNO(
35 s2n_fips_validate_cipher_suite(&s2n_null_cipher_suite, NULL),
36 S2N_ERR_NULL);
37 }
38
39 /* Test: Valid */
40 const struct s2n_cipher_suite *valid[] = {
41 &s2n_tls13_aes_256_gcm_sha384,
42 &s2n_ecdhe_rsa_with_aes_128_gcm_sha256,
43 &s2n_ecdhe_rsa_with_aes_256_cbc_sha384,
44 };
45 for (size_t i = 0; i < s2n_array_len(valid); i++) {
46 bool is_valid = false;
47 EXPECT_OK(s2n_fips_validate_cipher_suite(valid[i], &is_valid));
48 EXPECT_TRUE(is_valid);
49 }
50
51 /* Test: Invalid */
52 const struct s2n_cipher_suite *invalid[] = {
53 &s2n_null_cipher_suite,
54 &s2n_rsa_with_rc4_128_md5,
55 &s2n_rsa_with_aes_128_gcm_sha256,
56 };
57 for (size_t i = 0; i < s2n_array_len(invalid); i++) {
58 bool is_valid = true;
59 EXPECT_OK(s2n_fips_validate_cipher_suite(invalid[i], &is_valid));
60 EXPECT_FALSE(is_valid);
61 }
62
63 /* Test: check all */
64 for (size_t i = 0; i < cipher_preferences_test_all.count; i++) {
65 const struct s2n_cipher_suite *cipher_suite = cipher_preferences_test_all.suites[i];
66
67 bool is_valid = false;
68 EXPECT_OK(s2n_fips_validate_cipher_suite(cipher_suite, &is_valid));
69 if (!is_valid) {
70 continue;
71 }
72
73 /* Must be in the "test_all_fips" security policy */
74 const struct s2n_cipher_preferences *test_all_fips_prefs =
75 security_policy_test_all_fips.cipher_preferences;
76 bool is_in_test_all_fips_prefs = false;
77 for (size_t j = 0; j < test_all_fips_prefs->count; j++) {
78 if (cipher_suite == test_all_fips_prefs->suites[j]) {
79 is_in_test_all_fips_prefs = true;
80 }
81 }
82 EXPECT_TRUE(is_in_test_all_fips_prefs);
83
84 /* We copy our lists of allowed cipher suites directly from the standards,
85 * but we should double check any invariants we can just in case.
86 */
87
88 /* RSA key exchange is disallowed after 2023
89 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf */
90 EXPECT_NOT_EQUAL(cipher_suite->key_exchange_alg, &s2n_rsa);
91
92 /* AES is required.
93 * Multiple s2n_ciphers represent AES, so just check the name.
94 */
95 EXPECT_NOT_NULL(strstr(cipher_suite->name, "AES"));
96
97 /* Must use valid prf hash algorithm */
98 bool hash_is_valid = false;
99 s2n_hash_algorithm hash_alg = 0;
100 EXPECT_SUCCESS(s2n_hmac_hash_alg(cipher_suite->prf_alg, &hash_alg));
101 EXPECT_OK(s2n_fips_validate_hash_algorithm(hash_alg, &hash_is_valid));
102 EXPECT_TRUE(hash_is_valid);
103 }
104 };
105
106 /* Test s2n_fips_validate_signature_scheme */
107 {
108 /* Safety */
109 {
110 bool is_valid = false;
111 EXPECT_ERROR_WITH_ERRNO(
112 s2n_fips_validate_signature_scheme(NULL, &is_valid),
113 S2N_ERR_NULL);
114 EXPECT_ERROR_WITH_ERRNO(
115 s2n_fips_validate_signature_scheme(&s2n_null_sig_scheme, NULL),
116 S2N_ERR_NULL);
117 }
118
119 /* Test: Valid */
120 const struct s2n_signature_scheme *valid[] = {
121 &s2n_ecdsa_sha256,
122 &s2n_rsa_pkcs1_sha384,
123 &s2n_rsa_pss_pss_sha256,
124 };
125 for (size_t i = 0; i < s2n_array_len(valid); i++) {
126 bool is_valid = false;
127 EXPECT_OK(s2n_fips_validate_signature_scheme(valid[i], &is_valid));
128 EXPECT_TRUE(is_valid);
129 }
130
131 /* Test: Invalid */
132 const struct s2n_signature_scheme *invalid[] = {
133 &s2n_rsa_pkcs1_md5_sha1,
134 &s2n_rsa_pkcs1_sha1,
135 &s2n_ecdsa_sha1,
136 &s2n_null_sig_scheme,
137 };
138 for (size_t i = 0; i < s2n_array_len(invalid); i++) {
139 bool is_valid = true;
140 EXPECT_OK(s2n_fips_validate_signature_scheme(invalid[i], &is_valid));
141 EXPECT_FALSE(is_valid);
142 }
143
144 /* Test: check all */
145 const struct s2n_signature_preferences *all_sig_schemes =
146 security_policy_test_all.signature_preferences;
147 for (size_t i = 0; i < all_sig_schemes->count; i++) {
148 const struct s2n_signature_scheme *sig_scheme = all_sig_schemes->signature_schemes[i];
149
150 bool is_valid = false;
151 EXPECT_OK(s2n_fips_validate_signature_scheme(sig_scheme, &is_valid));
152 if (!is_valid) {
153 continue;
154 }
155
156 /* Must be in the "test_all_fips" security policy */
157 const struct s2n_signature_preferences *test_all_fips_prefs =
158 security_policy_test_all_fips.signature_preferences;
159 bool is_in_test_all_fips_prefs = false;
160 for (size_t j = 0; j < test_all_fips_prefs->count; j++) {
161 if (sig_scheme == test_all_fips_prefs->signature_schemes[j]) {
162 is_in_test_all_fips_prefs = true;
163 }
164 }
165 EXPECT_TRUE(is_in_test_all_fips_prefs);
166 }
167 };
168
169 /* Test s2n_fips_validate_curve */
170 {
171 /* Safety */
172 {
173 bool is_valid = false;
174 EXPECT_ERROR_WITH_ERRNO(
175 s2n_fips_validate_curve(NULL, &is_valid),
176 S2N_ERR_NULL);
177 EXPECT_ERROR_WITH_ERRNO(
178 s2n_fips_validate_curve(&s2n_ecc_curve_secp256r1, NULL),
179 S2N_ERR_NULL);
180 }
181
182 /* Test: Valid */
183 const struct s2n_ecc_named_curve *valid[] = { &s2n_ecc_curve_secp256r1 };
184 for (size_t i = 0; i < s2n_array_len(valid); i++) {
185 bool is_valid = false;
186 EXPECT_OK(s2n_fips_validate_curve(valid[i], &is_valid));
187 EXPECT_TRUE(is_valid);
188 }
189
190 /* Test: Invalid */
191 const struct s2n_ecc_named_curve *invalid[] = { &s2n_ecc_curve_x25519 };
192 for (size_t i = 0; i < s2n_array_len(invalid); i++) {
193 bool is_valid = true;
194 EXPECT_OK(s2n_fips_validate_curve(invalid[i], &is_valid));
195 EXPECT_FALSE(is_valid);
196 }
197
198 /* Test: check all */
199 for (size_t i = 0; i < s2n_ecc_preferences_test_all.count; i++) {
200 const struct s2n_ecc_named_curve *curve = s2n_ecc_preferences_test_all.ecc_curves[i];
201
202 bool is_valid = false;
203 EXPECT_OK(s2n_fips_validate_curve(curve, &is_valid));
204 if (!is_valid) {
205 continue;
206 }
207
208 /* Must be in the "test_all_fips" security policy */
209 const struct s2n_ecc_preferences *test_all_fips_prefs =
210 security_policy_test_all_fips.ecc_preferences;
211 bool is_in_test_all_fips_prefs = false;
212 for (size_t j = 0; j < test_all_fips_prefs->count; j++) {
213 if (curve == test_all_fips_prefs->ecc_curves[j]) {
214 is_in_test_all_fips_prefs = true;
215 }
216 }
217 EXPECT_TRUE(is_in_test_all_fips_prefs);
218 }
219 };
220
221 /* Test s2n_fips_validate_version */
222 {
223 /* Safety */
224 EXPECT_ERROR_WITH_ERRNO(s2n_fips_validate_version(0, NULL), S2N_ERR_NULL);
225
226 /* Test: Valid */
227 uint8_t valid[] = { S2N_TLS12, S2N_TLS13 };
228 for (size_t i = 0; i < s2n_array_len(valid); i++) {
229 bool is_valid = false;
230 EXPECT_OK(s2n_fips_validate_version(valid[i], &is_valid));
231 EXPECT_TRUE(is_valid);
232 }
233
234 /* Test: Invalid */
235 uint8_t invalid[] = { 0, 1, S2N_SSLv2, S2N_SSLv3, S2N_TLS11 };
236 for (size_t i = 0; i < s2n_array_len(invalid); i++) {
237 bool is_valid = true;
238 EXPECT_OK(s2n_fips_validate_version(invalid[i], &is_valid));
239 EXPECT_FALSE(is_valid);
240 }
241
242 /* Test: check all */
243 for (size_t version = 0; version < UINT8_MAX; version++) {
244 bool is_valid = false;
245 EXPECT_OK(s2n_fips_validate_version(version, &is_valid));
246 }
247 };
248
249 END_TEST();
250 }