Debian Codegraph @

crt/s2n/tests/unit/s2n_server_supported_versions_extension_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 <stdint.h>
17
18 #include "s2n_test.h"
19 #include "stuffer/s2n_stuffer.h"
20 #include "tls/extensions/s2n_server_supported_versions.h"
21 #include "tls/s2n_config.h"
22 #include "tls/s2n_connection.h"
23 #include "tls/s2n_tls.h"
24 #include "tls/s2n_tls13.h"
25 #include "utils/s2n_safety.h"
26
27 int write_test_supported_version(struct s2n_stuffer *list, uint8_t supported_version)
28 {
29 POSIX_GUARD(s2n_stuffer_write_uint8(list, S2N_TLS_PROTOCOL_VERSION_LEN));
30
31 POSIX_GUARD(s2n_stuffer_write_uint8(list, supported_version / 10));
32 POSIX_GUARD(s2n_stuffer_write_uint8(list, supported_version % 10));
33
34 return 0;
35 }
36
37 int main(int argc, char **argv)
38 {
39 BEGIN_TEST();
40
41 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
42 uint8_t latest_version = S2N_TLS13;
43
44 struct s2n_config *config = NULL;
45 EXPECT_NOT_NULL(config = s2n_config_new());
46
47 /* Server sends a supported_version the client can parse */
48 {
49 struct s2n_connection *server_conn = NULL;
50 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
51 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
52
53 uint16_t expected_length = 6;
54
55 struct s2n_stuffer extension = { 0 };
56 EXPECT_SUCCESS(s2n_stuffer_alloc(&extension, expected_length));
57
58 EXPECT_SUCCESS(s2n_server_supported_versions_extension.send(server_conn, &extension));
59
60 /* Check that size is correct */
61 uint16_t extension_length = s2n_extensions_server_supported_versions_size(server_conn)
62 - S2N_EXTENSION_TYPE_FIELD_LENGTH - S2N_EXTENSION_LENGTH_FIELD_LENGTH;
63 EXPECT_EQUAL(extension_length, s2n_stuffer_data_available(&extension));
64
65 /* Check that the client can process the version */
66 struct s2n_connection *client_conn = NULL;
67 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
68 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
69
70 EXPECT_SUCCESS(s2n_server_supported_versions_extension.recv(client_conn, &extension));
71 EXPECT_EQUAL(client_conn->client_protocol_version, latest_version);
72 EXPECT_EQUAL(client_conn->server_protocol_version, latest_version);
73 EXPECT_EQUAL(client_conn->actual_protocol_version, latest_version);
74
75 /* Clean up */
76 EXPECT_SUCCESS(s2n_connection_free(client_conn));
77 EXPECT_SUCCESS(s2n_connection_free(server_conn));
78 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
79 };
80
81 /* Client alerts if supported_version less than min supported by client */
82 {
83 struct s2n_connection *client_conn = NULL;
84 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
85 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
86
87 uint8_t unsupported_version_unknown = S2N_UNKNOWN_PROTOCOL_VERSION;
88
89 uint16_t supported_version_length = 6;
90
91 struct s2n_stuffer extension = { 0 };
92 EXPECT_SUCCESS(s2n_stuffer_alloc(&extension, supported_version_length));
93
94 EXPECT_SUCCESS(write_test_supported_version(&extension, unsupported_version_unknown));
95 EXPECT_FAILURE_WITH_ERRNO(s2n_server_supported_versions_extension.recv(client_conn, &extension), S2N_ERR_BAD_MESSAGE);
96
97 EXPECT_SUCCESS(s2n_connection_free(client_conn));
98 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
99 };
100
101 /* Client alerts if supported_version greater than max supported by client */
102 {
103 struct s2n_connection *client_conn = NULL;
104 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
105 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
106
107 uint8_t unsupported_version_gt_tls13 = 255;
108
109 uint16_t supported_version_length = 6;
110
111 struct s2n_stuffer extension = { 0 };
112 EXPECT_SUCCESS(s2n_stuffer_alloc(&extension, supported_version_length));
113
114 EXPECT_SUCCESS(write_test_supported_version(&extension, unsupported_version_gt_tls13));
115 EXPECT_FAILURE_WITH_ERRNO(s2n_server_supported_versions_extension.recv(client_conn, &extension), S2N_ERR_BAD_MESSAGE);
116
117 EXPECT_SUCCESS(s2n_connection_free(client_conn));
118 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
119 };
120
121 /* Client alerts if supported_version is empty */
122 {
123 struct s2n_connection *client_conn = NULL;
124 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
125 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
126
127 struct s2n_stuffer extension = { 0 };
128 EXPECT_SUCCESS(s2n_stuffer_alloc(&extension, 1));
129 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension, 0));
130
131 EXPECT_FAILURE_WITH_ERRNO(s2n_server_supported_versions_extension.recv(client_conn, &extension), S2N_ERR_BAD_MESSAGE);
132
133 EXPECT_SUCCESS(s2n_connection_free(client_conn));
134 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
135 };
136
137 /* Client alerts if supported_version is malformed */
138 {
139 struct s2n_connection *client_conn = NULL;
140 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
141 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
142
143 struct s2n_stuffer extension = { 0 };
144 EXPECT_SUCCESS(s2n_stuffer_alloc(&extension, 1));
145
146 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension, 13));
147
148 EXPECT_FAILURE_WITH_ERRNO(s2n_server_supported_versions_extension.recv(client_conn, &extension), S2N_ERR_BAD_MESSAGE);
149
150 EXPECT_SUCCESS(s2n_connection_free(client_conn));
151 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
152 };
153
154 EXPECT_SUCCESS(s2n_config_free(config));
155
156 END_TEST();
157 }