Debian Codegraph @

crt/s2n/tests/unit/s2n_connection_preferences_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 <stdlib.h>
17
18 #include "api/s2n.h"
19 #include "crypto/s2n_fips.h"
20 #include "s2n_test.h"
21 #include "tls/s2n_config.h"
22 #include "tls/s2n_connection.h"
23 #include "tls/s2n_security_policies.h"
24 #include "tls/s2n_tls13.h"
25
26 int main(int argc, char **argv)
27 {
28 BEGIN_TEST();
29 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
30
31 const struct s2n_security_policy *default_security_policy = NULL, *tls13_security_policy = NULL, *fips_security_policy = NULL;
32 EXPECT_SUCCESS(s2n_find_security_policy_from_version("default_tls13", &tls13_security_policy));
33 EXPECT_SUCCESS(s2n_find_security_policy_from_version("default_fips", &fips_security_policy));
34 EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &default_security_policy));
35
36 /* Test default TLS1.2 */
37 if (!s2n_is_in_fips_mode()) {
38 struct s2n_connection *conn = NULL;
39 const struct s2n_cipher_preferences *cipher_preferences = NULL;
40 const struct s2n_security_policy *security_policy = NULL;
41 const struct s2n_kem_preferences *kem_preferences = NULL;
42 const struct s2n_signature_preferences *signature_preferences = NULL;
43 const struct s2n_ecc_preferences *ecc_preferences = NULL;
44
45 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
46 EXPECT_NULL(conn->security_policy_override);
47
48 EXPECT_SUCCESS(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
49 EXPECT_EQUAL(cipher_preferences, default_security_policy->cipher_preferences);
50
51 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
52 EXPECT_EQUAL(security_policy, default_security_policy);
53
54 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(conn, &kem_preferences));
55 EXPECT_EQUAL(kem_preferences, default_security_policy->kem_preferences);
56
57 EXPECT_SUCCESS(s2n_connection_get_signature_preferences(conn, &signature_preferences));
58 EXPECT_EQUAL(signature_preferences, default_security_policy->signature_preferences);
59
60 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
61 EXPECT_EQUAL(ecc_preferences, default_security_policy->ecc_preferences);
62
63 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "20170328"));
64 EXPECT_NOT_NULL(conn->security_policy_override);
65
66 cipher_preferences = NULL;
67 EXPECT_SUCCESS(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
68 EXPECT_EQUAL(cipher_preferences, security_policy_20170328.cipher_preferences);
69
70 security_policy = NULL;
71 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
72 EXPECT_EQUAL(security_policy, &security_policy_20170328);
73
74 kem_preferences = NULL;
75 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(conn, &kem_preferences));
76 EXPECT_EQUAL(kem_preferences, security_policy_20170328.kem_preferences);
77
78 signature_preferences = NULL;
79 EXPECT_SUCCESS(s2n_connection_get_signature_preferences(conn, &signature_preferences));
80 EXPECT_EQUAL(signature_preferences, security_policy_20170328.signature_preferences);
81
82 ecc_preferences = NULL;
83 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
84 EXPECT_EQUAL(ecc_preferences, security_policy_20170328.ecc_preferences);
85
86 EXPECT_SUCCESS(s2n_connection_free(conn));
87 }
88
89 /* Test TLS1.3 */
90 {
91 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
92 struct s2n_connection *conn = NULL;
93 const struct s2n_cipher_preferences *cipher_preferences = NULL;
94 const struct s2n_security_policy *security_policy = NULL;
95 const struct s2n_kem_preferences *kem_preferences = NULL;
96 const struct s2n_signature_preferences *signature_preferences = NULL;
97 const struct s2n_ecc_preferences *ecc_preferences = NULL;
98
99 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
100 EXPECT_NULL(conn->security_policy_override);
101
102 EXPECT_SUCCESS(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
103 EXPECT_EQUAL(cipher_preferences, tls13_security_policy->cipher_preferences);
104
105 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
106 EXPECT_EQUAL(security_policy, tls13_security_policy);
107
108 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(conn, &kem_preferences));
109 EXPECT_EQUAL(kem_preferences, tls13_security_policy->kem_preferences);
110
111 EXPECT_SUCCESS(s2n_connection_get_signature_preferences(conn, &signature_preferences));
112 EXPECT_EQUAL(signature_preferences, tls13_security_policy->signature_preferences);
113
114 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
115 EXPECT_EQUAL(ecc_preferences, tls13_security_policy->ecc_preferences);
116
117 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all_tls13"));
118 EXPECT_NOT_NULL(conn->security_policy_override);
119
120 cipher_preferences = NULL;
121 EXPECT_SUCCESS(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
122 EXPECT_EQUAL(cipher_preferences, security_policy_test_all_tls13.cipher_preferences);
123
124 security_policy = NULL;
125 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
126 EXPECT_EQUAL(security_policy, &security_policy_test_all_tls13);
127
128 kem_preferences = NULL;
129 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(conn, &kem_preferences));
130 EXPECT_EQUAL(kem_preferences, security_policy_test_all_tls13.kem_preferences);
131
132 signature_preferences = NULL;
133 EXPECT_SUCCESS(s2n_connection_get_signature_preferences(conn, &signature_preferences));
134 EXPECT_EQUAL(signature_preferences, security_policy_test_all_tls13.signature_preferences);
135
136 ecc_preferences = NULL;
137 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
138 EXPECT_EQUAL(ecc_preferences, security_policy_test_all_tls13.ecc_preferences);
139
140 EXPECT_SUCCESS(s2n_connection_free(conn));
141 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
142 };
143
144 /* Test default fips */
145
146 if (s2n_is_in_fips_mode()) {
147 struct s2n_connection *conn = NULL;
148 const struct s2n_cipher_preferences *cipher_preferences = NULL;
149 const struct s2n_security_policy *security_policy = NULL;
150 const struct s2n_kem_preferences *kem_preferences = NULL;
151 const struct s2n_signature_preferences *signature_preferences = NULL;
152 const struct s2n_ecc_preferences *ecc_preferences = NULL;
153
154 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
155 EXPECT_NULL(conn->security_policy_override);
156
157 EXPECT_SUCCESS(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
158 EXPECT_EQUAL(cipher_preferences, fips_security_policy->cipher_preferences);
159
160 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
161 EXPECT_EQUAL(security_policy, fips_security_policy);
162
163 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(conn, &kem_preferences));
164 EXPECT_EQUAL(kem_preferences, fips_security_policy->kem_preferences);
165
166 EXPECT_SUCCESS(s2n_connection_get_signature_preferences(conn, &signature_preferences));
167 EXPECT_EQUAL(signature_preferences, fips_security_policy->signature_preferences);
168
169 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
170 EXPECT_EQUAL(ecc_preferences, fips_security_policy->ecc_preferences);
171
172 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "test_all_fips"));
173 EXPECT_NOT_NULL(conn->security_policy_override);
174
175 cipher_preferences = NULL;
176 EXPECT_SUCCESS(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
177 EXPECT_EQUAL(cipher_preferences, security_policy_test_all_fips.cipher_preferences);
178
179 security_policy = NULL;
180 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
181 EXPECT_EQUAL(security_policy, &security_policy_test_all_fips);
182
183 kem_preferences = NULL;
184 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(conn, &kem_preferences));
185 EXPECT_EQUAL(kem_preferences, security_policy_test_all_fips.kem_preferences);
186
187 signature_preferences = NULL;
188 EXPECT_SUCCESS(s2n_connection_get_signature_preferences(conn, &signature_preferences));
189 EXPECT_EQUAL(signature_preferences, security_policy_test_all_fips.signature_preferences);
190
191 ecc_preferences = NULL;
192 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
193 EXPECT_EQUAL(ecc_preferences, security_policy_test_all_fips.ecc_preferences);
194
195 EXPECT_SUCCESS(s2n_connection_free(conn));
196 }
197
198 /* Test for NULL */
199 {
200 struct s2n_connection *conn = NULL;
201 const struct s2n_cipher_preferences *cipher_preferences = NULL;
202 const struct s2n_security_policy *security_policy = NULL;
203 const struct s2n_kem_preferences *kem_preferences = NULL;
204 const struct s2n_signature_preferences *signature_preferences = NULL;
205 const struct s2n_ecc_preferences *ecc_preferences = NULL;
206
207 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
208 EXPECT_NULL(conn->security_policy_override);
209 EXPECT_FAILURE(s2n_connection_set_cipher_preferences(conn, NULL));
210 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "null"));
211 EXPECT_NOT_NULL(conn->security_policy_override);
212
213 EXPECT_SUCCESS(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
214 EXPECT_EQUAL(cipher_preferences, &cipher_preferences_null);
215
216 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
217 EXPECT_EQUAL(security_policy, &security_policy_null);
218
219 EXPECT_SUCCESS(s2n_connection_get_kem_preferences(conn, &kem_preferences));
220 EXPECT_EQUAL(kem_preferences, &kem_preferences_null);
221
222 EXPECT_SUCCESS(s2n_connection_get_signature_preferences(conn, &signature_preferences));
223 EXPECT_EQUAL(signature_preferences, &s2n_signature_preferences_null);
224
225 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
226 EXPECT_EQUAL(ecc_preferences, &s2n_ecc_preferences_null);
227
228 EXPECT_SUCCESS(s2n_connection_free(conn));
229 };
230
231 /* Test Error Case */
232 {
233 struct s2n_connection *conn = NULL;
234 const struct s2n_cipher_preferences *cipher_preferences = NULL;
235 const struct s2n_security_policy *security_policy = NULL;
236 const struct s2n_kem_preferences *kem_preferences = NULL;
237 const struct s2n_signature_preferences *signature_preferences = NULL;
238 const struct s2n_ecc_preferences *ecc_preferences = NULL;
239
240 EXPECT_FAILURE(s2n_connection_get_cipher_preferences(conn, &cipher_preferences));
241 EXPECT_FAILURE(s2n_connection_get_security_policy(conn, &security_policy));
242 EXPECT_FAILURE(s2n_connection_get_kem_preferences(conn, &kem_preferences));
243 EXPECT_FAILURE(s2n_connection_get_signature_preferences(conn, &signature_preferences));
244 EXPECT_FAILURE(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
245
246 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
247 s2n_config_ptr_free);
248 EXPECT_NOT_NULL(config);
249
250 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
251 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
252 EXPECT_NOT_NULL(conn->config->security_policy);
253 EXPECT_NULL(conn->security_policy_override);
254
255 /* Invalidate the security policy and expect failure. */
256 config->security_policy = NULL;
257
258 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_cipher_preferences(conn, &cipher_preferences), S2N_ERR_INVALID_CIPHER_PREFERENCES);
259 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_security_policy(conn, &security_policy), S2N_ERR_INVALID_SECURITY_POLICY);
260 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_kem_preferences(conn, &kem_preferences), S2N_ERR_INVALID_KEM_PREFERENCES);
261 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_signature_preferences(conn, &signature_preferences), S2N_ERR_INVALID_SIGNATURE_ALGORITHMS_PREFERENCES);
262 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_ecc_preferences(conn, &ecc_preferences), S2N_ERR_INVALID_ECC_PREFERENCES);
263
264 EXPECT_SUCCESS(s2n_connection_free(conn));
265 };
266
267 /* s2n_connection_get_curve */
268 {
269 struct s2n_connection *conn = NULL;
270 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
271 const char *curve_name = NULL;
272 char no_curve[] = { "NONE" };
273
274 /* No curve negotiated yet */
275 EXPECT_NOT_NULL(curve_name = s2n_connection_get_curve(conn));
276 EXPECT_BYTEARRAY_EQUAL(curve_name, no_curve, strlen(no_curve));
277 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_key_exchange_group(conn, &curve_name), S2N_ERR_INVALID_STATE);
278
279 /* TLS1.3 always returns a curve */
280 conn->actual_protocol_version = S2N_TLS13;
281 conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
282 EXPECT_NOT_NULL(curve_name = s2n_connection_get_curve(conn));
283 EXPECT_BYTEARRAY_EQUAL(curve_name, s2n_ecc_curve_secp256r1.name, strlen(s2n_ecc_curve_secp256r1.name));
284 curve_name = NULL;
285 s2n_connection_get_key_exchange_group(conn, &curve_name);
286 EXPECT_NOT_NULL(curve_name);
287 EXPECT_BYTEARRAY_EQUAL(curve_name, s2n_ecc_curve_secp256r1.name, strlen(s2n_ecc_curve_secp256r1.name));
288
289 /* TLS1.2 returns a curve if ECDHE cipher negotiated */
290 conn->actual_protocol_version = S2N_TLS12;
291 conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_128_cbc_sha256;
292 conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
293 EXPECT_OK(s2n_handshake_type_set_flag(conn, FULL_HANDSHAKE));
294 EXPECT_NOT_NULL(curve_name = s2n_connection_get_curve(conn));
295 EXPECT_BYTEARRAY_EQUAL(curve_name, s2n_ecc_curve_secp256r1.name, strlen(s2n_ecc_curve_secp256r1.name));
296 curve_name = NULL;
297 s2n_connection_get_key_exchange_group(conn, &curve_name);
298 EXPECT_NOT_NULL(curve_name);
299 EXPECT_BYTEARRAY_EQUAL(curve_name, s2n_ecc_curve_secp256r1.name, strlen(s2n_ecc_curve_secp256r1.name));
300
301 /* TLS1.2 does not return a curve if ECDHE cipher was not negotiated */
302 conn->actual_protocol_version = S2N_TLS12;
303 conn->secure->cipher_suite = &s2n_rsa_with_aes_256_gcm_sha384;
304 conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
305 EXPECT_NOT_NULL(curve_name = s2n_connection_get_curve(conn));
306 EXPECT_BYTEARRAY_EQUAL(curve_name, no_curve, strlen(no_curve));
307 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_key_exchange_group(conn, &curve_name), S2N_ERR_INVALID_STATE);
308
309 EXPECT_SUCCESS(s2n_connection_free(conn));
310 };
311
312 END_TEST();
313 }