Debian Codegraph @

crt/s2n/tests/unit/s2n_client_record_version_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 <errno.h>
17 #include <fcntl.h>
18 #include <stdint.h>
19 #include <unistd.h>
20
21 #include "api/s2n.h"
22 #include "s2n_test.h"
23 #include "testlib/s2n_testlib.h"
24 #include "tls/s2n_connection.h"
25 #include "tls/s2n_handshake.h"
26 #include "tls/s2n_tls.h"
27 #include "tls/s2n_tls_parameters.h"
28
29 #define ZERO_TO_THIRTY_ONE 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \
30 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
31
32 int main(int argc, char **argv)
33 {
34 char *cert_chain = NULL;
35 char *private_key = NULL;
36 BEGIN_TEST();
37 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
38
39 EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
40 EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
41 EXPECT_SUCCESS(setenv("S2N_DONT_MLOCK", "1", 0));
42
43 /* Server negotiates TLS1.2 */
44 {
45 struct s2n_connection *client_conn = NULL;
46 struct s2n_config *client_config = NULL;
47 s2n_blocked_status client_blocked;
48
49 uint8_t server_hello_message[] = {
50 /* Protocol version TLS 1.2 */
51 0x03, 0x03,
52 /* Server random */
53 ZERO_TO_THIRTY_ONE,
54 /* SessionID len - 32 bytes */
55 0x20,
56 /* Session ID */
57 ZERO_TO_THIRTY_ONE,
58 /* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
59 0x00, 0x3C,
60 /* Compression method - none */
61 0x00,
62 /* Extensions len */
63 0x00, 0x00
64 };
65 int body_len = sizeof(server_hello_message);
66 uint8_t message_header[] = {
67 /* Handshake message type SERVER HELLO */
68 0x02,
69 /* Body len */
70 (body_len >> 16) & 0xff,
71 (body_len >> 8) & 0xff,
72 (body_len & 0xff),
73 };
74 int message_len = sizeof(message_header) + body_len;
75 uint8_t record_header[] = {
76 /* Record type HANDSHAKE */
77 0x16,
78 /* Protocol version TLS 1.2 */
79 0x03,
80 0x03,
81 /* Message len */
82 (message_len >> 8) & 0xff,
83 (message_len & 0xff),
84 };
85
86 /* Create nonblocking pipes */
87 struct s2n_test_io_pair io_pair;
88 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
89
90 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
91 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
92
93 EXPECT_NOT_NULL(client_config = s2n_config_new());
94 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
95 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
96
97 /* Send the client hello */
98 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
99 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
100 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
101
102 /* Read ClientHello s2n wrote */
103 uint8_t buf[1024];
104 size_t buf_occupied = 0;
105
106 /* we need only first 10 bytes to get to ClientHello protocol version */
107 while (buf_occupied < 10) {
108 ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied);
109
110 /* We should be able to read 10 bytes without blocking */
111 EXPECT_TRUE(n > 0);
112 buf_occupied += n;
113 }
114 /* Record Type is Handshake */
115 EXPECT_EQUAL(buf[0], 0x16);
116 /* Protocol version is TLS1.0 */
117 EXPECT_EQUAL(buf[1], 0x03);
118 EXPECT_EQUAL(buf[2], 0x01);
119 /* Handshake Type is ClientHello */
120 EXPECT_EQUAL(buf[5], 0x01);
121 /* Handshake Protocol Version is TLS1.2 */
122 EXPECT_EQUAL(buf[9], 0x03);
123 EXPECT_EQUAL(buf[10], 0x03);
124
125 /* Read the rest of the pipe */
126 while (1) {
127 ssize_t n = s2n_test_recv(io_pair.server, buf, sizeof(buf));
128
129 if (n > 0) {
130 continue;
131 }
132
133 EXPECT_EQUAL(n, -1);
134 if (errno == EAGAIN || errno == EWOULDBLOCK) {
135 break;
136 }
137 }
138
139 /* Write the server hello */
140 EXPECT_EQUAL(s2n_test_send(io_pair.server, record_header, sizeof(record_header)), sizeof(record_header));
141 EXPECT_EQUAL(s2n_test_send(io_pair.server, message_header, sizeof(message_header)), sizeof(message_header));
142 EXPECT_EQUAL(s2n_test_send(io_pair.server, server_hello_message, sizeof(server_hello_message)), sizeof(server_hello_message));
143
144 /* Verify that we proceed with handshake */
145 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
146 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
147 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
148
149 /* Verify that protocol versions are TLS1.2 now */
150 EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12);
151 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
152 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_TLS12);
153
154 /* Now lets shutdown the connection and verify that alert is sent in record with protocol version TLS1.2 */
155 EXPECT_SUCCESS(s2n_shutdown(client_conn, &client_blocked));
156
157 /* Receive the next record from client and ensure that record protocol version is TLS1.2 */
158 buf_occupied = 0;
159 /* We need only first 5 bytes to get to record protocol version */
160 while (buf_occupied < 5) {
161 ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied);
162
163 /* We should be able to read 5 bytes without blocking */
164 EXPECT_TRUE(n > 0);
165 buf_occupied += n;
166 }
167 /* Protocol version is TLS1.2 now */
168 EXPECT_EQUAL(buf[1], 0x03);
169 EXPECT_EQUAL(buf[2], 0x03);
170
171 EXPECT_SUCCESS(s2n_connection_free(client_conn));
172 EXPECT_SUCCESS(s2n_config_free(client_config));
173
174 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
175 };
176
177 /* Server negotiates SSLv3 */
178 {
179 struct s2n_connection *client_conn = NULL;
180 struct s2n_config *client_config = NULL;
181 s2n_blocked_status client_blocked;
182
183 uint8_t server_hello_message[] = {
184 /* Protocol version SSLv3 */
185 0x03, 0x00,
186 /* Server random */
187 ZERO_TO_THIRTY_ONE,
188 /* SessionID len - 32 bytes */
189 0x20,
190 /* Session ID */
191 ZERO_TO_THIRTY_ONE,
192 /* Cipher suite - DES-CBC3-SHA */
193 0x00, 0x0A,
194 /* Compression method - none */
195 0x00,
196 /* Extensions len */
197 0x00, 0x00
198 };
199 int body_len = sizeof(server_hello_message);
200 uint8_t message_header[] = {
201 /* Handshake message type SERVER HELLO */
202 0x02,
203 /* Body len */
204 (body_len >> 16) & 0xff,
205 (body_len >> 8) & 0xff,
206 (body_len & 0xff),
207 };
208 int message_len = sizeof(message_header) + body_len;
209 uint8_t record_header[] = {
210 /* Record type HANDSHAKE */
211 0x16,
212 /* Protocol version SSLv3 */
213 0x03,
214 0x00,
215 /* Message len */
216 (message_len >> 8) & 0xff,
217 (message_len & 0xff),
218 };
219
220 /* Create nonblocking pipes */
221 struct s2n_test_io_pair io_pair;
222 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
223
224 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
225 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
226
227 EXPECT_NOT_NULL(client_config = s2n_config_new());
228 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
229 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
230
231 /* Send the client hello */
232 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
233 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
234 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
235
236 /* Read ClientHello s2n wrote */
237 uint8_t buf[1024];
238 size_t buf_occupied = 0;
239
240 /* we need only first 10 bytes to get to ClientHello protocol version */
241 while (buf_occupied < 10) {
242 ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied);
243
244 /* We should be able to read 10 bytes without blocking */
245 EXPECT_TRUE(n > 0);
246 buf_occupied += n;
247 }
248 /* Record Type is Handshake */
249 EXPECT_EQUAL(buf[0], 0x16);
250 /* Protocol version is TLS1.0 */
251 EXPECT_EQUAL(buf[1], 0x03);
252 EXPECT_EQUAL(buf[2], 0x01);
253 /* Handshake Type is ClientHello */
254 EXPECT_EQUAL(buf[5], 0x01);
255 /* Handshake Protocol Version is TLS1.2 */
256 EXPECT_EQUAL(buf[9], 0x03);
257 EXPECT_EQUAL(buf[10], 0x03);
258
259 /* Read the rest of the pipe */
260 while (1) {
261 ssize_t n = s2n_test_recv(io_pair.server, buf, sizeof(buf));
262
263 if (n > 0) {
264 continue;
265 }
266
267 EXPECT_EQUAL(n, -1);
268 if (errno == EAGAIN || errno == EWOULDBLOCK) {
269 break;
270 }
271 }
272
273 /* Write the server hello */
274 EXPECT_EQUAL(s2n_test_send(io_pair.server, record_header, sizeof(record_header)), sizeof(record_header));
275 EXPECT_EQUAL(s2n_test_send(io_pair.server, message_header, sizeof(message_header)), sizeof(message_header));
276 EXPECT_EQUAL(s2n_test_send(io_pair.server, server_hello_message, sizeof(server_hello_message)), sizeof(server_hello_message));
277
278 /* Verify that we proceed with handshake */
279 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
280 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
281 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
282
283 /* Verify that protocol versions are SSLv3 with the exeption of client which supports TLS1.2 */
284 EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12);
285 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_SSLv3);
286 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_SSLv3);
287
288 /* Now lets shutdown the connection and verify that alert is sent in record with protocol version SSLv3 */
289 EXPECT_SUCCESS(s2n_shutdown(client_conn, &client_blocked));
290
291 /* Receive the next record from client and ensure that record protocol version is SSLv3 */
292 buf_occupied = 0;
293 /* We need only first 5 bytes to get to record protocol version */
294 while (buf_occupied < 5) {
295 ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied);
296
297 /* We should be able to read 5 bytes without blocking */
298 EXPECT_TRUE(n > 0);
299 buf_occupied += n;
300 }
301 /* Protocol version is SSLv3 now */
302 EXPECT_EQUAL(buf[1], 0x03);
303 EXPECT_EQUAL(buf[2], 0x00);
304
305 EXPECT_SUCCESS(s2n_connection_free(client_conn));
306 EXPECT_SUCCESS(s2n_config_free(client_config));
307
308 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
309 };
310
311 free(cert_chain);
312 free(private_key);
313 END_TEST();
314 }