Debian Codegraph @

crt/s2n/tests/unit/s2n_npn_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 #include <string.h>
18
19 #include "s2n_test.h"
20 #include "stuffer/s2n_stuffer.h"
21 #include "testlib/s2n_testlib.h"
22 #include "tests/s2n_test.h"
23 #include "tls/extensions/s2n_client_alpn.h"
24 #include "tls/extensions/s2n_npn.h"
25 #include "tls/s2n_tls.h"
26 #include "utils/s2n_safety.h"
27
28 #define HTTP11 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31
29 #define SPDY1 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31
30 #define SPDY2 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32
31 #define SPDY3 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33
32
33 S2N_RESULT s2n_calculate_padding(uint8_t protocol_len, uint8_t *padding_len);
34
35 int main(int argc, char **argv)
36 {
37 BEGIN_TEST();
38
39 const char *protocols[] = { "http/1.1", "spdy/1", "spdy/2" };
40 const uint8_t protocols_count = s2n_array_len(protocols);
41
42 /* Should-send tests on the client side */
43 {
44 /* No connection */
45 EXPECT_FALSE(s2n_client_npn_extension.should_send(NULL));
46
47 /* No config */
48 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
49 EXPECT_NOT_NULL(client_conn);
50 EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));
51
52 /* No application protocols set */
53 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
54 EXPECT_NOT_NULL(config);
55 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
56 EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));
57
58 /* Application protocols set but NPN not supported. In this case the ALPN extension will be sent. */
59 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
60 EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));
61 EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn));
62
63 /* Both ALPN and NPN extensions will be sent */
64 client_conn->config->npn_supported = true;
65 EXPECT_TRUE(s2n_client_npn_extension.should_send(client_conn));
66 EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn));
67
68 /*
69 *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-3
70 *= type=test
71 *# For the same reasons, after a handshake has been performed for a
72 *# given connection, renegotiations on the same connection MUST NOT
73 *# include the "next_protocol_negotiation" extension.
74 */
75 client_conn->handshake.renegotiation = true;
76 EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));
77 EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn));
78 };
79
80 /* s2n_client_npn_recv */
81 {
82 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
83 EXPECT_NOT_NULL(server_conn);
84 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
85 EXPECT_NOT_NULL(config);
86 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
87 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
88
89 DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
90 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
91
92 /* NPN not supported */
93 EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension));
94 EXPECT_FALSE(server_conn->npn_negotiated);
95
96 /* NPN supported */
97 server_conn->config->npn_supported = true;
98 EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension));
99 EXPECT_TRUE(server_conn->npn_negotiated);
100
101 /* Server has already negotiated a protocol with the ALPN extension */
102 uint8_t first_protocol_len = strlen(protocols[0]);
103 EXPECT_MEMCPY_SUCCESS(server_conn->application_protocol, protocols[0], first_protocol_len + 1);
104 server_conn->npn_negotiated = false;
105 EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension));
106 EXPECT_FALSE(server_conn->npn_negotiated);
107 };
108
109 /* Should-send tests on the server side */
110 {
111 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
112 EXPECT_NOT_NULL(server_conn);
113 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
114 EXPECT_NOT_NULL(config);
115 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
116 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
117
118 /* NPN not negotiated */
119 EXPECT_FALSE(s2n_server_npn_extension.should_send(server_conn));
120
121 /* NPN negotiated */
122 server_conn->npn_negotiated = true;
123 EXPECT_TRUE(s2n_server_npn_extension.should_send(server_conn));
124 };
125
126 /* s2n_server_npn_send */
127 {
128 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
129 EXPECT_NOT_NULL(server_conn);
130 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
131 EXPECT_NOT_NULL(config);
132 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
133 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
134 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
135 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
136
137 EXPECT_SUCCESS(s2n_server_npn_extension.send(server_conn, &out));
138
139 uint8_t protocol_len = 0;
140 uint8_t protocol[UINT8_MAX] = { 0 };
141 for (size_t i = 0; i < protocols_count; i++) {
142 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &protocol_len));
143 EXPECT_EQUAL(protocol_len, strlen(protocols[i]));
144
145 EXPECT_SUCCESS(s2n_stuffer_read_bytes(&out, protocol, protocol_len));
146 EXPECT_BYTEARRAY_EQUAL(protocol, protocols[i], protocol_len);
147 }
148
149 EXPECT_EQUAL(s2n_stuffer_data_available(&out), 0);
150 };
151
152 /* s2n_server_npn_recv */
153 {
154 /* Client has no application protocols configured. Not sure how this
155 * could happen, but added to be thorough. */
156 {
157 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
158 EXPECT_NOT_NULL(client_conn);
159 DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
160 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
161
162 EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
163 EXPECT_NULL(s2n_get_application_protocol(client_conn));
164 };
165
166 /* NPN recv extension can read NPN send extension */
167 {
168 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
169 EXPECT_NOT_NULL(client_conn);
170 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
171 EXPECT_NOT_NULL(config);
172 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
173 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
174
175 DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
176 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
177
178 EXPECT_SUCCESS(s2n_server_npn_extension.send(client_conn, &extension));
179 EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
180
181 /* Server sent the same list that the client configured so the first protocol in the list is chosen */
182 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
183 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
184 };
185
186 /* No match exists */
187 {
188 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
189 EXPECT_NOT_NULL(client_conn);
190 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
191 EXPECT_NOT_NULL(config);
192 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
193 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
194
195 DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
196 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
197
198 uint8_t protocol[] = { SPDY3 };
199 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension, sizeof(protocol)));
200 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, protocol, sizeof(protocol)));
201
202 /*
203 *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-4
204 *= type=test
205 *# In the event that the client doesn't support any of server's protocols, or
206 *# the server doesn't advertise any, it SHOULD select the first protocol
207 *# that it supports.
208 */
209 EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
210 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
211 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
212 };
213
214 /* Server sends empty list */
215 {
216 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
217 EXPECT_NOT_NULL(client_conn);
218 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
219 EXPECT_NOT_NULL(config);
220 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
221 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
222
223 DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
224 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
225
226 /*
227 *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-4
228 *= type=test
229 *# In the event that the client doesn't support any of server's protocols, or
230 *# the server doesn't advertise any, it SHOULD select the first protocol
231 *# that it supports.
232 */
233 EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
234 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
235 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
236 };
237
238 /* Multiple matches exist and server's preferred choice is selected */
239 {
240 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
241 EXPECT_NOT_NULL(client_conn);
242 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
243 EXPECT_NOT_NULL(config);
244 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
245 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
246
247 DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
248 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
249
250 uint8_t wire_bytes[] = {
251 /* Size and bytes of first protocol */
252 0x06,
253 SPDY1,
254 /* Size and bytes of second protocol */
255 0x08,
256 HTTP11,
257 /* Size and bytes of second protocol */
258 0x06,
259 SPDY2,
260 };
261
262 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, wire_bytes, sizeof(wire_bytes)));
263 EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
264
265 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
266
267 /* Client's second protocol is selected because the server prefers it over client's first protocol */
268 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[1], strlen(protocols[1]));
269 };
270 };
271
272 /* Check application protocol array can hold the largest uint8_t value.
273 *
274 * We frequently copy a uint8_t's worth of data into this array. Adding
275 * checks to ensure that the array will be large enough causes compilers
276 * to give warnings that the check will always be true.
277 * This test will fail if we ever make that array smaller, so we remember
278 * to go back and add those checks.
279 */
280 {
281 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
282 EXPECT_NOT_NULL(server_conn);
283 /* Not <= because the application protocol is a string, which needs to
284 * be terminated by a null character */
285 EXPECT_TRUE(UINT8_MAX < sizeof(server_conn->application_protocol));
286 };
287
288 END_TEST();
289 }