Debian Codegraph @

crt/s2n/tests/unit/s2n_client_secure_renegotiation_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 /* Success: server sends an empty initial renegotiation_info */
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_extensions[] = {
50 /* Extension type TLS_EXTENSION_RENEGOTIATION_INFO */
51 0xff,
52 0x01,
53 /* Extension size */
54 0x00,
55 0x01,
56 /* renegotiated_connection len */
57 0x00,
58 };
59 int server_extensions_len = sizeof(server_extensions);
60 uint8_t server_hello_message[] = {
61 /* Protocol version TLS 1.2 */
62 0x03,
63 0x03,
64 /* Server random */
65 ZERO_TO_THIRTY_ONE,
66 /* SessionID len - 32 bytes */
67 0x20,
68 /* Session ID */
69 ZERO_TO_THIRTY_ONE,
70 /* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
71 0x00,
72 0x3C,
73 /* Compression method - none */
74 0x00,
75 /* Extensions len */
76 (server_extensions_len >> 8) & 0xff,
77 (server_extensions_len & 0xff),
78 };
79 int body_len = sizeof(server_hello_message) + server_extensions_len;
80 uint8_t message_header[] = {
81 /* Handshake message type SERVER HELLO */
82 0x02,
83 /* Body len */
84 (body_len >> 16) & 0xff,
85 (body_len >> 8) & 0xff,
86 (body_len & 0xff),
87 };
88 int message_len = sizeof(message_header) + body_len;
89 uint8_t record_header[] = {
90 /* Record type HANDSHAKE */
91 0x16,
92 /* Protocol version TLS 1.2 */
93 0x03,
94 0x03,
95 /* Message len */
96 (message_len >> 8) & 0xff,
97 (message_len & 0xff),
98 };
99
100 /* Create nonblocking pipes */
101 struct s2n_test_io_pair io_pair;
102 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
103
104 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
105 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
106
107 EXPECT_NOT_NULL(client_config = s2n_config_new());
108 /* Security policy must allow cipher suite hard coded into server hello */
109 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
110 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
111
112 /* Send the client hello */
113 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
114 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
115 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
116
117 /* Write the server hello */
118 EXPECT_EQUAL(s2n_test_send(io_pair.server, record_header, sizeof(record_header)), sizeof(record_header));
119 EXPECT_EQUAL(s2n_test_send(io_pair.server, message_header, sizeof(message_header)), sizeof(message_header));
120 EXPECT_EQUAL(s2n_test_send(io_pair.server, server_hello_message, sizeof(server_hello_message)), sizeof(server_hello_message));
121 EXPECT_EQUAL(s2n_test_send(io_pair.server, server_extensions, sizeof(server_extensions)), sizeof(server_extensions));
122
123 /* Verify that we proceed with handshake */
124 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
125 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
126 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
127
128 /* Secure renegotiation is set */
129 EXPECT_EQUAL(client_conn->secure_renegotiation, 1);
130
131 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
132 EXPECT_SUCCESS(s2n_connection_free(client_conn));
133 EXPECT_SUCCESS(s2n_config_free(client_config));
134 };
135
136 /* Success: server doesn't send an renegotiation_info extension */
137 {
138 struct s2n_connection *client_conn = NULL;
139 struct s2n_config *client_config = NULL;
140 s2n_blocked_status client_blocked;
141
142 uint8_t server_hello_message[] = {
143 /* Protocol version TLS 1.2 */
144 0x03, 0x03,
145 /* Server random */
146 ZERO_TO_THIRTY_ONE,
147 /* SessionID len - 32 bytes */
148 0x20,
149 /* Session ID */
150 ZERO_TO_THIRTY_ONE,
151 /* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
152 0x00, 0x3C,
153 /* Compression method - none */
154 0x00,
155 /* Extensions len */
156 0x00, 0x00
157 };
158 int body_len = sizeof(server_hello_message);
159 uint8_t message_header[] = {
160 /* Handshake message type SERVER HELLO */
161 0x02,
162 /* Body len */
163 (body_len >> 16) & 0xff,
164 (body_len >> 8) & 0xff,
165 (body_len & 0xff),
166 };
167 int message_len = sizeof(message_header) + body_len;
168 uint8_t record_header[] = {
169 /* Record type HANDSHAKE */
170 0x16,
171 /* Protocol version TLS 1.2 */
172 0x03,
173 0x03,
174 /* Message len */
175 (message_len >> 8) & 0xff,
176 (message_len & 0xff),
177 };
178
179 /* Create nonblocking pipes */
180 struct s2n_test_io_pair io_pair;
181 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
182
183 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
184 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
185
186 EXPECT_NOT_NULL(client_config = s2n_config_new());
187 /* Security policy must allow cipher suite hard coded into server hello */
188 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
189 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
190
191 /* Send the client hello */
192 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
193 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
194 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
195
196 /* Write the server hello */
197 EXPECT_EQUAL(s2n_test_send(io_pair.server, record_header, sizeof(record_header)), sizeof(record_header));
198 EXPECT_EQUAL(s2n_test_send(io_pair.server, message_header, sizeof(message_header)), sizeof(message_header));
199 EXPECT_EQUAL(s2n_test_send(io_pair.server, server_hello_message, sizeof(server_hello_message)), sizeof(server_hello_message));
200
201 /* Verify that we proceed with handshake */
202 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
203 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
204 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
205
206 /* Secure renegotiation is not set, as server doesn't support it */
207 EXPECT_EQUAL(client_conn->secure_renegotiation, 0);
208
209 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
210 EXPECT_SUCCESS(s2n_connection_free(client_conn));
211 EXPECT_SUCCESS(s2n_config_free(client_config));
212 };
213
214 /* Failure: server sends a non-empty initial renegotiation_info */
215 {
216 struct s2n_connection *client_conn = NULL;
217 struct s2n_config *client_config = NULL;
218 s2n_blocked_status client_blocked;
219
220 uint8_t server_extensions[] = {
221 /* Extension type TLS_EXTENSION_RENEGOTIATION_INFO */
222 0xff,
223 0x01,
224 /* Extension size */
225 0x00,
226 0x21,
227 /* renegotiated_connection len */
228 0x20,
229 /* fake renegotiated_connection */
230 ZERO_TO_THIRTY_ONE,
231 };
232 int server_extensions_len = sizeof(server_extensions);
233 uint8_t server_hello_message[] = {
234 /* Protocol version TLS 1.2 */
235 0x03,
236 0x03,
237 /* Server random */
238 ZERO_TO_THIRTY_ONE,
239 /* SessionID len - 32 bytes */
240 0x20,
241 /* Session ID */
242 ZERO_TO_THIRTY_ONE,
243 /* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
244 0x00,
245 0x3C,
246 /* Compression method - none */
247 0x00,
248 /* Extensions len */
249 (server_extensions_len >> 8) & 0xff,
250 (server_extensions_len & 0xff),
251 };
252 int body_len = sizeof(server_hello_message) + server_extensions_len;
253 uint8_t message_header[] = {
254 /* Handshake message type SERVER HELLO */
255 0x02,
256 /* Body len */
257 (body_len >> 16) & 0xff,
258 (body_len >> 8) & 0xff,
259 (body_len & 0xff),
260 };
261 int message_len = sizeof(message_header) + body_len;
262 uint8_t record_header[] = {
263 /* Record type HANDSHAKE */
264 0x16,
265 /* Protocol version TLS 1.2 */
266 0x03,
267 0x03,
268 /* Message len */
269 (message_len >> 8) & 0xff,
270 (message_len & 0xff),
271 };
272
273 /* Create nonblocking pipes */
274 struct s2n_test_io_pair io_pair;
275 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
276
277 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
278 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
279
280 EXPECT_NOT_NULL(client_config = s2n_config_new());
281 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
282 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
283
284 /* Send the client hello */
285 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
286 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
287 EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ);
288
289 /* Write the server hello */
290 EXPECT_EQUAL(s2n_test_send(io_pair.server, record_header, sizeof(record_header)), sizeof(record_header));
291 EXPECT_EQUAL(s2n_test_send(io_pair.server, message_header, sizeof(message_header)), sizeof(message_header));
292 EXPECT_EQUAL(s2n_test_send(io_pair.server, server_hello_message, sizeof(server_hello_message)), sizeof(server_hello_message));
293 EXPECT_EQUAL(s2n_test_send(io_pair.server, server_extensions, sizeof(server_extensions)), sizeof(server_extensions));
294
295 /* Verify that we fail for non-empty renegotiated_connection */
296 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
297 EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1);
298 EXPECT_EQUAL(s2n_errno, S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO);
299
300 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
301 EXPECT_SUCCESS(s2n_connection_free(client_conn));
302 EXPECT_SUCCESS(s2n_config_free(client_config));
303 };
304
305 free(cert_chain);
306 free(private_key);
307 END_TEST();
308 }