Debian Codegraph @

crt/s2n/tests/unit/s2n_connection_protocol_versions_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 "api/s2n.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/s2n_tls.h"
20
21 static S2N_RESULT s2n_write_test_supported_versions_extension(struct s2n_blob *supported_versions_blob, uint8_t version,
22 uint8_t extension_length)
23 {
24 RESULT_ENSURE_REF(supported_versions_blob);
25
26 struct s2n_stuffer supported_versions_stuffer = { 0 };
27 RESULT_GUARD_POSIX(s2n_stuffer_init(&supported_versions_stuffer, supported_versions_blob));
28
29 /* Write the length byte. */
30 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&supported_versions_stuffer, extension_length));
31 /* Write the supported version. */
32 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&supported_versions_stuffer, version / 10));
33 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&supported_versions_stuffer, version % 10));
34
35 return S2N_RESULT_OK;
36 }
37
38 struct s2n_overwrite_client_hello_ctx {
39 uint8_t client_hello_version;
40 uint8_t client_supported_version;
41 uint8_t extension_length;
42
43 uint8_t supported_versions_data[3];
44 int invoked_count;
45 };
46
47 static int s2n_overwrite_client_hello_cb(struct s2n_connection *conn, void *ctx)
48 {
49 EXPECT_NOT_NULL(conn);
50 EXPECT_NOT_NULL(ctx);
51
52 struct s2n_overwrite_client_hello_ctx *context = (struct s2n_overwrite_client_hello_ctx *) ctx;
53 context->invoked_count += 1;
54
55 if (context->extension_length) {
56 struct s2n_blob supported_versions_blob = { 0 };
57 EXPECT_SUCCESS(s2n_blob_init(&supported_versions_blob, context->supported_versions_data,
58 sizeof(context->supported_versions_data)));
59
60 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(conn);
61 EXPECT_NOT_NULL(client_hello);
62
63 s2n_extension_type_id supported_versions_id = 0;
64 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(S2N_EXTENSION_SUPPORTED_VERSIONS, &supported_versions_id));
65 s2n_parsed_extension *extension = &client_hello->extensions.parsed_extensions[supported_versions_id];
66
67 EXPECT_OK(s2n_write_test_supported_versions_extension(&supported_versions_blob,
68 context->client_supported_version, context->extension_length));
69
70 extension->extension_type = S2N_EXTENSION_SUPPORTED_VERSIONS;
71 extension->extension = supported_versions_blob;
72 }
73
74 /* The client version fields are set when parsing the client hello before the client hello
75 * callback is invoked. The version fields are overridden to emulate receiving a client hello
76 * with a different version.
77 */
78 if (context->client_hello_version) {
79 conn->client_hello.legacy_version = context->client_hello_version;
80 conn->client_protocol_version = context->client_hello_version;
81 }
82
83 return S2N_SUCCESS;
84 }
85
86 int main(int argc, char **argv)
87 {
88 BEGIN_TEST();
89
90 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
91 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
92 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
93
94 /* Safety */
95 {
96 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_client_protocol_version(NULL), S2N_ERR_NULL);
97 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_client_hello_version(NULL), S2N_ERR_NULL);
98 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_server_protocol_version(NULL), S2N_ERR_NULL);
99 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_actual_protocol_version(NULL), S2N_ERR_NULL);
100 }
101
102 /* Test protocol version getters on the server when a supported versions extension is received */
103 for (uint8_t server_version = S2N_TLS12; server_version <= S2N_TLS13; server_version++) {
104 for (uint8_t client_hello_version = S2N_SSLv3; client_hello_version <= S2N_TLS12; client_hello_version++) {
105 for (uint8_t client_supported_version = S2N_SSLv3; client_supported_version <= S2N_TLS13; client_supported_version++) {
106 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
107 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
108
109 if (server_version == S2N_TLS12) {
110 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all_tls12"));
111 } else if (s2n_is_tls13_fully_supported()) {
112 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
113 } else {
114 continue;
115 }
116
117 struct s2n_overwrite_client_hello_ctx context = {
118 .client_hello_version = client_hello_version,
119 .client_supported_version = client_supported_version,
120 .extension_length = 2,
121 };
122 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, s2n_overwrite_client_hello_cb, &context));
123
124 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
125 s2n_connection_ptr_free);
126 EXPECT_NOT_NULL(client);
127 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
128
129 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
130 s2n_connection_ptr_free);
131 EXPECT_NOT_NULL(server);
132 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
133
134 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
135 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
136 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
137
138 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_HELLO));
139 EXPECT_EQUAL(context.invoked_count, 1);
140
141 /* Ensure that a supported versions extension was received. */
142 bool supported_versions_received = false;
143 EXPECT_SUCCESS(s2n_client_hello_has_extension(&server->client_hello, S2N_EXTENSION_SUPPORTED_VERSIONS,
144 &supported_versions_received));
145 EXPECT_TRUE(supported_versions_received);
146
147 EXPECT_EQUAL(s2n_connection_get_server_protocol_version(server), server_version);
148 EXPECT_EQUAL(s2n_connection_get_client_hello_version(server), client_hello_version);
149
150 /* The reported client protocol version should always match the version specified
151 * in the supported versions extension, even for TLS 1.2 servers which don't
152 * process the extension for version selection.
153 */
154 EXPECT_EQUAL(s2n_connection_get_client_protocol_version(server), client_supported_version);
155
156 uint8_t actual_protocol_version = s2n_connection_get_actual_protocol_version(server);
157 if (server_version == S2N_TLS12) {
158 /* For backwards compatibility, TLS 1.2 servers always use the client hello
159 * version to determine the client's maximum version, even if a supported
160 * versions extension was received.
161 */
162 EXPECT_EQUAL(actual_protocol_version, S2N_MIN(server_version, client_hello_version));
163 } else {
164 /* TLS 1.3 servers always use the version in the supported versions extension,
165 * regardless of the client hello version.
166 */
167 EXPECT_EQUAL(actual_protocol_version, S2N_MIN(server_version, client_supported_version));
168 }
169 }
170 }
171 }
172
173 /* Test protocol version getters on the server when a supported versions extension isn't received */
174 for (uint8_t server_version = S2N_TLS12; server_version <= S2N_TLS13; server_version++) {
175 for (uint8_t client_hello_version = S2N_SSLv3; client_hello_version <= S2N_TLS12; client_hello_version++) {
176 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
177
178 /* A TLS 1.2 security policy is set to prevent the client from sending a supported
179 * versions extension.
180 */
181 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all_tls12"));
182
183 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
184 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
185
186 if (server_version == S2N_TLS12) {
187 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "test_all_tls12"));
188 } else if (s2n_is_tls13_fully_supported()) {
189 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "test_all"));
190 } else {
191 continue;
192 }
193
194 struct s2n_overwrite_client_hello_ctx context = {
195 .client_hello_version = client_hello_version,
196 };
197 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(server_config, s2n_overwrite_client_hello_cb, &context));
198
199 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
200 s2n_connection_ptr_free);
201 EXPECT_NOT_NULL(client);
202 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
203
204 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
205 s2n_connection_ptr_free);
206 EXPECT_NOT_NULL(server);
207 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
208
209 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
210 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
211 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
212
213 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_HELLO));
214 EXPECT_EQUAL(context.invoked_count, 1);
215
216 /* Ensure that a supported versions extension wasn't received. */
217 bool supported_versions_received = false;
218 EXPECT_SUCCESS(s2n_client_hello_has_extension(&server->client_hello, S2N_EXTENSION_SUPPORTED_VERSIONS,
219 &supported_versions_received));
220 EXPECT_FALSE(supported_versions_received);
221
222 EXPECT_EQUAL(s2n_connection_get_server_protocol_version(server), server_version);
223 EXPECT_EQUAL(s2n_connection_get_client_protocol_version(server), client_hello_version);
224 EXPECT_EQUAL(s2n_connection_get_client_hello_version(server), client_hello_version);
225 EXPECT_EQUAL(s2n_connection_get_actual_protocol_version(server), client_hello_version);
226 }
227 }
228
229 /* Test protocol version getters on the client */
230 for (uint8_t server_version = S2N_SSLv3; server_version <= S2N_TLS13; server_version++) {
231 if (server_version == S2N_TLS13 && !s2n_is_tls13_fully_supported()) {
232 continue;
233 }
234
235 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
236 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
237 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
238
239 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
240 EXPECT_NOT_NULL(client);
241 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
242
243 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
244 EXPECT_NOT_NULL(server);
245 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
246 server->server_protocol_version = server_version;
247
248 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
249 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
250 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
251
252 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_CERT));
253
254 EXPECT_EQUAL(s2n_connection_get_server_protocol_version(client), server_version);
255 EXPECT_EQUAL(s2n_connection_get_client_protocol_version(client), s2n_get_highest_fully_supported_tls_version());
256 EXPECT_EQUAL(s2n_connection_get_client_hello_version(client), S2N_TLS12);
257 EXPECT_EQUAL(s2n_connection_get_actual_protocol_version(client), server_version);
258 }
259
260 /* Ensure that TLS 1.2 servers report the client hello version as the client protocol version
261 * if a malformed supported versions extension was received
262 */
263 for (uint8_t client_hello_version = S2N_SSLv3; client_hello_version <= S2N_TLS12; client_hello_version++) {
264 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
265 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
266 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all_tls12"));
267
268 struct s2n_overwrite_client_hello_ctx context = {
269 .client_hello_version = client_hello_version,
270 .client_supported_version = S2N_TLS13,
271 /* Write an invalid length */
272 .extension_length = 11,
273 };
274 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, s2n_overwrite_client_hello_cb, &context));
275
276 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
277 s2n_connection_ptr_free);
278 EXPECT_NOT_NULL(client);
279 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
280
281 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
282 s2n_connection_ptr_free);
283 EXPECT_NOT_NULL(server);
284 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
285
286 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
287 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
288 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
289
290 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_HELLO));
291 EXPECT_EQUAL(context.invoked_count, 1);
292
293 /* Ensure that a supported versions extension was received. */
294 bool supported_versions_received = false;
295 EXPECT_SUCCESS(s2n_client_hello_has_extension(&server->client_hello, S2N_EXTENSION_SUPPORTED_VERSIONS,
296 &supported_versions_received));
297 EXPECT_TRUE(supported_versions_received);
298
299 EXPECT_EQUAL(s2n_connection_get_client_protocol_version(server), client_hello_version);
300 }
301
302 /* Ensure that TLS 1.2 servers report the client hello version as the client protocol version
303 * if an invalid supported version is received
304 */
305 for (uint8_t client_hello_version = S2N_SSLv3; client_hello_version <= S2N_TLS12; client_hello_version++) {
306 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
307 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
308 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all_tls12"));
309
310 struct s2n_overwrite_client_hello_ctx context = {
311 .client_hello_version = client_hello_version,
312 /* Write an invalid version */
313 .client_supported_version = S2N_TLS13 + 10,
314 .extension_length = 2,
315 };
316 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, s2n_overwrite_client_hello_cb, &context));
317
318 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
319 s2n_connection_ptr_free);
320 EXPECT_NOT_NULL(client);
321 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
322
323 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
324 s2n_connection_ptr_free);
325 EXPECT_NOT_NULL(server);
326 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
327
328 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
329 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
330 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
331
332 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_HELLO));
333 EXPECT_EQUAL(context.invoked_count, 1);
334
335 /* Ensure that a supported versions extension was received. */
336 bool supported_versions_received = false;
337 EXPECT_SUCCESS(s2n_client_hello_has_extension(&server->client_hello, S2N_EXTENSION_SUPPORTED_VERSIONS,
338 &supported_versions_received));
339 EXPECT_TRUE(supported_versions_received);
340
341 EXPECT_EQUAL(s2n_connection_get_client_protocol_version(server), client_hello_version);
342 }
343
344 /* Ensure that TLS 1.3 servers report an unknown protocol version if a supported versions
345 * extension can't be processed
346 */
347 if (s2n_is_tls13_fully_supported()) {
348 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
349 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
350 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
351
352 struct s2n_overwrite_client_hello_ctx context = {
353 .client_supported_version = S2N_TLS13,
354 /* Write an invalid length */
355 .extension_length = 11,
356 };
357 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, s2n_overwrite_client_hello_cb, &context));
358
359 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
360 s2n_connection_ptr_free);
361 EXPECT_NOT_NULL(client);
362 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
363
364 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
365 s2n_connection_ptr_free);
366 EXPECT_NOT_NULL(server);
367 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
368 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
369
370 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
371 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
372 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
373
374 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_HELLO),
375 S2N_ERR_BAD_MESSAGE);
376 EXPECT_EQUAL(context.invoked_count, 1);
377
378 /* Ensure that a supported versions extension was received. */
379 bool supported_versions_received = false;
380 EXPECT_SUCCESS(s2n_client_hello_has_extension(&server->client_hello, S2N_EXTENSION_SUPPORTED_VERSIONS,
381 &supported_versions_received));
382 EXPECT_TRUE(supported_versions_received);
383
384 EXPECT_EQUAL(s2n_connection_get_server_protocol_version(server), S2N_TLS13);
385 EXPECT_EQUAL(s2n_connection_get_client_protocol_version(server), s2n_unknown_protocol_version);
386 EXPECT_EQUAL(s2n_connection_get_actual_protocol_version(server), s2n_unknown_protocol_version);
387 }
388
389 END_TEST();
390 }