Debian Codegraph @

crt/s2n/tests/unit/s2n_protocol_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 "tls/s2n_protocol_preferences.h"
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "tls/extensions/s2n_client_alpn.h"
21 #include "tls/s2n_connection.h"
22
23 #define LEN_PREFIX 1
24
25 int main(int argc, char **argv)
26 {
27 BEGIN_TEST();
28
29 /* input values */
30 const uint8_t protocol1[] = "protocol1";
31 const size_t protocol1_len = strlen((const char *) protocol1);
32 const uint8_t protocol2[] = "protocol abc 2";
33 const size_t protocol2_len = strlen((const char *) protocol2);
34
35 const uint8_t large_value[255] = { 0 };
36
37 /* Test config append */
38 {
39 struct s2n_config *config = NULL;
40 EXPECT_NOT_NULL(config = s2n_config_new());
41 EXPECT_EQUAL(config->application_protocols.size, 0);
42 size_t prev_size = 0;
43
44 /* should grow the blob with the provided value */
45 EXPECT_SUCCESS(s2n_config_append_protocol_preference(config, protocol1, sizeof(protocol1)));
46 EXPECT_EQUAL(config->application_protocols.size, LEN_PREFIX + sizeof(protocol1));
47 prev_size = config->application_protocols.size;
48
49 /* should grow the blob even more */
50 EXPECT_SUCCESS(s2n_config_append_protocol_preference(config, protocol2, sizeof(protocol2)));
51 EXPECT_EQUAL(config->application_protocols.size, prev_size + LEN_PREFIX + sizeof(protocol2));
52 prev_size = config->application_protocols.size;
53
54 /* should reallocate the blob with large values */
55 EXPECT_SUCCESS(s2n_config_append_protocol_preference(config, large_value, sizeof(large_value)));
56 EXPECT_EQUAL(config->application_protocols.size, prev_size + LEN_PREFIX + sizeof(large_value));
57 prev_size = config->application_protocols.size;
58
59 /* should not allow empty protocol values */
60 EXPECT_FAILURE(s2n_config_append_protocol_preference(config, large_value, 0));
61 EXPECT_EQUAL(config->application_protocols.size, prev_size);
62
63 EXPECT_SUCCESS(s2n_config_free(config));
64 };
65
66 /* Test connection append */
67 {
68 struct s2n_connection *conn = NULL;
69 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
70 EXPECT_EQUAL(conn->application_protocols_overridden.size, 0);
71 size_t prev_size = 0;
72
73 /* should grow the blob with the provided value */
74 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol1, sizeof(protocol1)));
75 EXPECT_EQUAL(conn->application_protocols_overridden.size, LEN_PREFIX + sizeof(protocol1));
76 prev_size = conn->application_protocols_overridden.size;
77
78 /* should grow the blob even more */
79 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol2, sizeof(protocol2)));
80 EXPECT_EQUAL(conn->application_protocols_overridden.size, prev_size + LEN_PREFIX + sizeof(protocol2));
81 prev_size = conn->application_protocols_overridden.size;
82
83 /* should reallocate the blob with large values */
84 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, large_value, sizeof(large_value)));
85 EXPECT_EQUAL(conn->application_protocols_overridden.size, prev_size + LEN_PREFIX + sizeof(large_value));
86 prev_size = conn->application_protocols_overridden.size;
87
88 /* should not allow empty protocol values */
89 EXPECT_FAILURE(s2n_connection_append_protocol_preference(conn, large_value, 0));
90 EXPECT_EQUAL(conn->application_protocols_overridden.size, prev_size);
91
92 EXPECT_SUCCESS(s2n_connection_free(conn));
93 };
94
95 const char *protocols[] = { (const char *) protocol1, (const char *) protocol2 };
96 const uint8_t protocols_count = s2n_array_len(protocols);
97
98 char oversized_value[257] = { 0 };
99 memset(&oversized_value, 1, 256);
100 EXPECT_EQUAL(strlen(oversized_value), 256);
101 const char *oversized[] = { oversized_value };
102 const uint8_t oversized_count = s2n_array_len(oversized);
103
104 /* Test config set */
105 {
106 struct s2n_config *config = NULL;
107 EXPECT_NOT_NULL(config = s2n_config_new());
108 EXPECT_EQUAL(config->application_protocols.size, 0);
109
110 /* should copy the preference list */
111 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
112 EXPECT_EQUAL(config->application_protocols.size, LEN_PREFIX + protocol1_len + LEN_PREFIX + protocol2_len);
113
114 /* should correctly free the old list list */
115 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, 1));
116 EXPECT_EQUAL(config->application_protocols.size, LEN_PREFIX + protocol1_len);
117
118 /* should clear the preference list */
119 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, NULL, protocols_count));
120 EXPECT_EQUAL(config->application_protocols.size, 0);
121
122 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
123 EXPECT_EQUAL(config->application_protocols.size, LEN_PREFIX + protocol1_len + LEN_PREFIX + protocol2_len);
124 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, 0));
125 EXPECT_EQUAL(config->application_protocols.size, 0);
126
127 /* should limit the length of the protocol value */
128 EXPECT_FAILURE(s2n_config_set_protocol_preferences(config, oversized, oversized_count));
129 EXPECT_EQUAL(config->application_protocols.size, 0);
130
131 EXPECT_SUCCESS(s2n_config_free(config));
132 };
133
134 /* Test connection set */
135 {
136 struct s2n_connection *conn = NULL;
137 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
138 EXPECT_EQUAL(conn->application_protocols_overridden.size, 0);
139
140 /* should copy the preference list */
141 EXPECT_SUCCESS(s2n_connection_set_protocol_preferences(conn, protocols, protocols_count));
142 EXPECT_EQUAL(conn->application_protocols_overridden.size, LEN_PREFIX + protocol1_len + LEN_PREFIX + protocol2_len);
143
144 /* should correctly free the old list */
145 EXPECT_SUCCESS(s2n_connection_set_protocol_preferences(conn, protocols, 1));
146 EXPECT_EQUAL(conn->application_protocols_overridden.size, LEN_PREFIX + protocol1_len);
147
148 /* should clear the preference list */
149 EXPECT_SUCCESS(s2n_connection_set_protocol_preferences(conn, NULL, protocols_count));
150 EXPECT_EQUAL(conn->application_protocols_overridden.size, 0);
151
152 EXPECT_SUCCESS(s2n_connection_set_protocol_preferences(conn, protocols, protocols_count));
153 EXPECT_EQUAL(conn->application_protocols_overridden.size, LEN_PREFIX + protocol1_len + LEN_PREFIX + protocol2_len);
154 EXPECT_SUCCESS(s2n_connection_set_protocol_preferences(conn, protocols, 0));
155 EXPECT_EQUAL(conn->application_protocols_overridden.size, 0);
156
157 /* should limit the length of the protocol value */
158
159 EXPECT_FAILURE(s2n_connection_set_protocol_preferences(conn, oversized, oversized_count));
160 EXPECT_EQUAL(conn->application_protocols_overridden.size, 0);
161
162 EXPECT_SUCCESS(s2n_connection_free(conn));
163 };
164
165 /* Test s2n_protocol_preferences_read */
166 {
167 /* Safety checks */
168 {
169 struct s2n_stuffer stuffer = { 0 };
170 struct s2n_blob blob = { 0 };
171 EXPECT_ERROR_WITH_ERRNO(s2n_protocol_preferences_read(NULL, &blob), S2N_ERR_NULL);
172 EXPECT_ERROR_WITH_ERRNO(s2n_protocol_preferences_read(&stuffer, NULL), S2N_ERR_NULL);
173 };
174
175 /* Fail to read zero-length protocol */
176 {
177 struct s2n_stuffer input = { 0 };
178 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
179 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, 0));
180
181 struct s2n_blob result = { 0 };
182 EXPECT_ERROR_WITH_ERRNO(s2n_protocol_preferences_read(&input, &result), S2N_ERR_SAFETY);
183 EXPECT_EQUAL(result.size, 0);
184
185 EXPECT_SUCCESS(s2n_stuffer_free(&input));
186 };
187
188 /* Read valid value */
189 {
190 struct s2n_stuffer input = { 0 };
191 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
192 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, sizeof(protocol1)));
193 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, protocol1, sizeof(protocol1)));
194
195 struct s2n_blob result = { 0 };
196 EXPECT_OK(s2n_protocol_preferences_read(&input, &result));
197 EXPECT_EQUAL(result.size, sizeof(protocol1));
198 EXPECT_BYTEARRAY_EQUAL(result.data, protocol1, sizeof(protocol1));
199
200 EXPECT_SUCCESS(s2n_stuffer_free(&input));
201 };
202
203 /* Read what we write */
204 {
205 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
206 EXPECT_NOT_NULL(conn);
207
208 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol1, sizeof(protocol1)));
209 EXPECT_SUCCESS(s2n_client_alpn_extension.send(conn, &conn->handshake.io));
210
211 /* Skip list size */
212 EXPECT_SUCCESS(s2n_stuffer_skip_read(&conn->handshake.io, sizeof(uint16_t)));
213
214 struct s2n_blob result = { 0 };
215 EXPECT_OK(s2n_protocol_preferences_read(&conn->handshake.io, &result));
216 EXPECT_EQUAL(result.size, sizeof(protocol1));
217 EXPECT_BYTEARRAY_EQUAL(result.data, protocol1, sizeof(protocol1));
218
219 EXPECT_SUCCESS(s2n_connection_free(conn));
220 };
221 };
222
223 /* s2n_protocol_preferences_contain */
224 {
225 uint8_t protocol3[] = "protocol3";
226 struct s2n_blob protocol3_blob = { 0 };
227 EXPECT_SUCCESS(s2n_blob_init(&protocol3_blob, protocol3, sizeof(protocol3)));
228
229 /* Safety checks */
230 {
231 struct s2n_blob blob = { 0 };
232 bool match = false;
233 EXPECT_ERROR_WITH_ERRNO(s2n_protocol_preferences_contain(NULL, &blob, &match), S2N_ERR_NULL);
234 EXPECT_ERROR_WITH_ERRNO(s2n_protocol_preferences_contain(&blob, NULL, &match), S2N_ERR_NULL);
235 EXPECT_ERROR_WITH_ERRNO(s2n_protocol_preferences_contain(&blob, &blob, NULL), S2N_ERR_NULL);
236 };
237
238 /* No supported protocols */
239 {
240 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
241 EXPECT_NOT_NULL(conn);
242
243 bool result = true;
244 EXPECT_OK(s2n_protocol_preferences_contain(&conn->application_protocols_overridden, &protocol3_blob, &result));
245 EXPECT_FALSE(result);
246
247 EXPECT_SUCCESS(s2n_connection_free(conn));
248 };
249
250 /* No match */
251 {
252 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
253 EXPECT_NOT_NULL(conn);
254
255 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol1, sizeof(protocol1)));
256 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol2, sizeof(protocol2)));
257
258 bool result = true;
259 EXPECT_OK(s2n_protocol_preferences_contain(&conn->application_protocols_overridden, &protocol3_blob, &result));
260 EXPECT_FALSE(result);
261
262 EXPECT_SUCCESS(s2n_connection_free(conn));
263 };
264
265 /* Match */
266 {
267 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
268 EXPECT_NOT_NULL(conn);
269
270 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol1, sizeof(protocol1)));
271 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol2, sizeof(protocol2)));
272 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, protocol3, sizeof(protocol3)));
273
274 bool result = false;
275 EXPECT_OK(s2n_protocol_preferences_contain(&conn->application_protocols_overridden, &protocol3_blob, &result));
276 EXPECT_TRUE(result);
277
278 EXPECT_SUCCESS(s2n_connection_free(conn));
279 };
280 };
281
282 END_TEST();
283 }