Debian Codegraph @

crt/s2n/tests/unit/s2n_next_protocol_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 "s2n_test.h"
17 #include "stuffer/s2n_stuffer.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/s2n_tls.h"
20 #include "utils/s2n_safety.h"
21
22 S2N_RESULT s2n_calculate_padding(uint8_t protocol_len, uint8_t *padding_len);
23 S2N_RESULT s2n_write_npn_protocol(struct s2n_connection *conn, struct s2n_stuffer *out);
24 S2N_RESULT s2n_read_npn_protocol(struct s2n_connection *conn, struct s2n_stuffer *in);
25
26 int main(int argc, char **argv)
27 {
28 BEGIN_TEST();
29
30 const char *protocols[] = { "http/1.1", "spdy/1", "spdy/2" };
31
32 /* Test s2n_next_protocol_send */
33 {
34 /* Safety checks */
35 EXPECT_FAILURE(s2n_next_protocol_send(NULL));
36
37 /* Should fail for >= TLS1.3 */
38 {
39 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
40 EXPECT_NOT_NULL(client_conn);
41
42 /* Fails for TLS1.3 */
43 client_conn->actual_protocol_version = S2N_TLS13;
44 EXPECT_FAILURE_WITH_ERRNO(s2n_next_protocol_send(client_conn), S2N_ERR_BAD_MESSAGE);
45
46 /* Succeeds for TLS1.2 */
47 client_conn->actual_protocol_version = S2N_TLS12;
48 EXPECT_SUCCESS(s2n_next_protocol_send(client_conn));
49 };
50 };
51
52 /* Test s2n_next_protocol_recv */
53 {
54 /* Safety checks */
55 EXPECT_FAILURE(s2n_next_protocol_recv(NULL));
56
57 /* Should parse s2n_next_protocol_send */
58 {
59 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
60 EXPECT_NOT_NULL(server_conn);
61 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
62 EXPECT_NOT_NULL(client_conn);
63
64 client_conn->actual_protocol_version = S2N_TLS12;
65
66 char protocol[] = { "http/1.1" };
67 uint8_t protocol_len = strlen(protocol);
68 EXPECT_MEMCPY_SUCCESS(client_conn->application_protocol, protocol, protocol_len + 1);
69
70 struct s2n_stuffer *stuffer = &client_conn->handshake.io;
71
72 EXPECT_SUCCESS(s2n_next_protocol_send(client_conn));
73 EXPECT_SUCCESS(s2n_stuffer_copy(stuffer, &server_conn->handshake.io, s2n_stuffer_data_available(stuffer)));
74
75 /* Fails for TLS1.3 */
76 server_conn->actual_protocol_version = S2N_TLS13;
77 EXPECT_FAILURE_WITH_ERRNO(s2n_next_protocol_recv(server_conn), S2N_ERR_BAD_MESSAGE);
78
79 /* Succeeds for TLS1.2 */
80 server_conn->actual_protocol_version = S2N_TLS12;
81 EXPECT_SUCCESS(s2n_next_protocol_recv(server_conn));
82 EXPECT_EQUAL(s2n_stuffer_data_available(&server_conn->handshake.io), 0);
83
84 EXPECT_BYTEARRAY_EQUAL(client_conn->application_protocol, server_conn->application_protocol, protocol_len);
85 };
86 };
87
88 /* s2n_write_npn_protocol */
89 {
90 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
91 EXPECT_NOT_NULL(client_conn);
92
93 uint8_t first_protocol_len = strlen(protocols[0]);
94 EXPECT_MEMCPY_SUCCESS(client_conn->application_protocol, protocols[0], first_protocol_len + 1);
95
96 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
97 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
98 EXPECT_OK(s2n_write_npn_protocol(client_conn, &out));
99
100 uint8_t protocol_len = 0;
101 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &protocol_len));
102
103 uint8_t protocol[UINT8_MAX] = { 0 };
104 EXPECT_SUCCESS(s2n_stuffer_read_bytes(&out, protocol, protocol_len));
105 EXPECT_BYTEARRAY_EQUAL(protocol, protocols[0], protocol_len);
106
107 uint8_t padding_len = 0;
108 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &padding_len));
109 EXPECT_TRUE(padding_len > 0);
110
111 for (size_t i = 0; i < padding_len; i++) {
112 uint8_t byte = UINT8_MAX;
113 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &byte));
114 EXPECT_EQUAL(byte, 0);
115 }
116 EXPECT_EQUAL(s2n_stuffer_data_available(&out), 0);
117 };
118
119 /* s2n_read_npn_protocol can parse s2n_write_npn_protocol */
120 {
121 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
122 EXPECT_NOT_NULL(client_conn);
123
124 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
125 EXPECT_NOT_NULL(server_conn);
126
127 uint8_t first_protocol_len = strlen(protocols[0]);
128 EXPECT_MEMCPY_SUCCESS(client_conn->application_protocol, protocols[0], first_protocol_len + 1);
129
130 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
131 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
132 EXPECT_OK(s2n_write_npn_protocol(client_conn, &out));
133 EXPECT_OK(s2n_read_npn_protocol(server_conn, &out));
134
135 EXPECT_NOT_NULL(s2n_get_application_protocol(server_conn));
136 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(server_conn), protocols[0], strlen(protocols[0]));
137 };
138
139 /* s2n_read_npn_protocol can read empty message */
140 {
141 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
142 EXPECT_NOT_NULL(server_conn);
143
144 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
145 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
146
147 /* Zero-length protocol */
148 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&out, 0));
149 uint8_t padding_len = 0;
150 EXPECT_OK(s2n_calculate_padding(0, &padding_len));
151 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&out, padding_len));
152 for (size_t i = 0; i < padding_len; i++) {
153 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&out, 0));
154 }
155
156 EXPECT_OK(s2n_read_npn_protocol(server_conn, &out));
157 EXPECT_NULL(s2n_get_application_protocol(server_conn));
158 };
159
160 /* s2n_read_npn_protocol errors on malformed message */
161 {
162 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
163 EXPECT_NOT_NULL(server_conn);
164
165 uint8_t wire_bytes[] = {
166 /* Incorrect length of protocol */
167 0x10,
168 };
169
170 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
171 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
172 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&out, wire_bytes, sizeof(wire_bytes)));
173 EXPECT_ERROR_WITH_ERRNO(s2n_read_npn_protocol(server_conn, &out), S2N_ERR_NULL);
174 };
175
176 /* s2n_read_npn_protocol errors on malformed padding */
177 {
178 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
179 EXPECT_NOT_NULL(server_conn);
180
181 uint8_t wire_bytes[] = {
182 /* Zero-length protocol */
183 0x00,
184 0x00,
185 /* Padding character is not zero */
186 0x01,
187 0xFF,
188 };
189
190 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
191 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
192 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&out, wire_bytes, sizeof(wire_bytes)));
193 EXPECT_ERROR_WITH_ERRNO(s2n_read_npn_protocol(server_conn, &out), S2N_ERR_SAFETY);
194 };
195
196 /*
197 *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-3
198 *= type=test
199 *# The length of "padding" SHOULD be 32 - ((len(selected_protocol) + 2) % 32).
200 */
201 {
202 struct {
203 uint8_t protocol_len;
204 uint8_t expected_padding;
205 } test_cases[] = {
206 { .protocol_len = 0, .expected_padding = 30 },
207 { .protocol_len = 32, .expected_padding = 30 },
208 { .protocol_len = 17, .expected_padding = 13 },
209 { .protocol_len = 2, .expected_padding = 28 },
210 { .protocol_len = UINT8_MAX, .expected_padding = 31 },
211 { .protocol_len = 30, .expected_padding = 32 },
212 };
213
214 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
215 uint8_t output = 0;
216 EXPECT_OK(s2n_calculate_padding(test_cases[i].protocol_len, &output));
217 EXPECT_EQUAL(output, test_cases[i].expected_padding);
218 }
219 };
220 END_TEST();
221 }