Debian Codegraph @

crt/s2n/tests/unit/s2n_fragmentation_coalescing_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 <stdlib.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 #include "api/s2n.h"
22 #include "s2n_test.h"
23 #include "tls/s2n_connection.h"
24 #include "tls/s2n_handshake.h"
25
26 /*
27 * The TLS protocol allows messages to be fragmented, interleaved and coalesced into 'records'. These
28 * tests check that fragmented messages are recombined, that several messages in the same record work
29 * and that messages interleaved with alerts (including a fragmented alert message) all work.
30 *
31 * To do this we fork() subprocesses that write records to a pipe, s2n is configured to read fragments
32 * from the pipe.
33 */
34
35 #define TLS_ALERT 21
36 #define TLS_HANDSHAKE 22
37 #define TLS_HEARTBEAT 24
38
39 #define ZERO_TO_THIRTY_ONE 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \
40 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
41
42 uint8_t zero_to_thirty_one[] = { ZERO_TO_THIRTY_ONE };
43
44 uint8_t server_hello_message[] = { /* SERVER HELLO */
45 0x02,
46
47 /* Length */
48 0x00, 0x00, 0x46,
49
50 /* Protocol version */
51 0x03, 0x03,
52
53 /* Server random */
54 ZERO_TO_THIRTY_ONE,
55
56 /* SessionID len - 32 bytes */
57 0x20,
58
59 /* Session ID */
60 ZERO_TO_THIRTY_ONE,
61
62 /* Cipher suite - TLS_RSA_WITH_AES_256_CBC_SHA256 */
63 0x00, 0x3D,
64
65 /* Compression method: none */
66 0x00
67 };
68
69 uint8_t server_cert[] = { /* SERVER CERT */
70 0x0B,
71
72 /* Length of the handshake message */
73 0x00, 0x03, 0x38,
74
75 /* Length of all certificates */
76 0x00, 0x03, 0x35,
77
78 /* Length of the first cert */
79 0x00, 0x03, 0x32,
80
81 /* Certificate data - via openssl x509 -in cert.pem -outform DER | xxd -i */
82 0x30, 0x82, 0x03, 0x2e, 0x30, 0x82, 0x02, 0x16, 0x02, 0x09, 0x00, 0xcb,
83 0xd6, 0x5a, 0xfa, 0x37, 0xcf, 0xe0, 0xbf, 0x30, 0x0d, 0x06, 0x09, 0x2a,
84 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x59,
85 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
86 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a,
87 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
88 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x18, 0x49, 0x6e, 0x74,
89 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
90 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x31, 0x12, 0x30,
91 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x09, 0x6c, 0x6f, 0x63, 0x61,
92 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x34, 0x30,
93 0x35, 0x31, 0x30, 0x31, 0x37, 0x30, 0x38, 0x32, 0x33, 0x5a, 0x17, 0x0d,
94 0x32, 0x34, 0x30, 0x35, 0x30, 0x37, 0x31, 0x37, 0x30, 0x38, 0x32, 0x33,
95 0x5a, 0x30, 0x59, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
96 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
97 0x08, 0x13, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74,
98 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x18,
99 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64,
100 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64,
101 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x09, 0x6c,
102 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x82, 0x01, 0x22,
103 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
104 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a,
105 0x02, 0x82, 0x01, 0x01, 0x00, 0xc8, 0x96, 0xd6, 0x94, 0x98, 0x78, 0x3e,
106 0x1b, 0xb2, 0x1d, 0x6b, 0x65, 0xc2, 0xb4, 0x44, 0x31, 0xd5, 0x87, 0x96,
107 0x0d, 0x7e, 0x35, 0x53, 0x6c, 0xc1, 0x29, 0xb6, 0x34, 0x95, 0x3f, 0x9a,
108 0xb9, 0x77, 0xfc, 0xd5, 0xc4, 0xf7, 0x75, 0x84, 0xdd, 0x7c, 0x96, 0xe5,
109 0x6f, 0xeb, 0xf8, 0x09, 0x35, 0x9a, 0x18, 0xda, 0x1f, 0xa2, 0x07, 0x33,
110 0x79, 0xb9, 0xbc, 0x07, 0x6f, 0xce, 0x17, 0xd1, 0x7e, 0x59, 0x69, 0x0a,
111 0x00, 0x98, 0x4a, 0xb1, 0x33, 0xc0, 0x13, 0xbf, 0xd2, 0x34, 0x07, 0x62,
112 0xe0, 0x4a, 0xaf, 0xe0, 0x57, 0xcd, 0x6d, 0x62, 0xa4, 0x19, 0xbe, 0x31,
113 0x69, 0xcc, 0x71, 0x6f, 0x83, 0xc7, 0xd9, 0x73, 0xfd, 0x57, 0x70, 0xa1,
114 0x27, 0xa9, 0x4c, 0x48, 0x8d, 0xd5, 0xeb, 0xc1, 0x66, 0x11, 0xfe, 0x24,
115 0x70, 0x43, 0x75, 0xe1, 0x5f, 0x2f, 0xb9, 0xf2, 0x02, 0xe4, 0x71, 0x3f,
116 0x2d, 0x3e, 0x20, 0x08, 0xf0, 0xc9, 0xe1, 0x47, 0xd4, 0x51, 0xb0, 0x20,
117 0x12, 0x14, 0x9e, 0x6d, 0x3e, 0xab, 0xfc, 0xa1, 0x58, 0x07, 0x94, 0xf7,
118 0x01, 0xe0, 0xdc, 0xd5, 0x57, 0x67, 0x69, 0xa4, 0x5b, 0x96, 0xb3, 0xfa,
119 0x2b, 0x03, 0x38, 0xe6, 0xf4, 0xec, 0xd0, 0x88, 0xb4, 0xf7, 0xf6, 0x2b,
120 0x97, 0x30, 0x71, 0x69, 0x33, 0xcc, 0x8c, 0xb1, 0x82, 0x29, 0xaf, 0x09,
121 0x32, 0xff, 0x0f, 0x5b, 0x64, 0x74, 0x53, 0xd5, 0x82, 0xa8, 0x79, 0xb3,
122 0x04, 0x7f, 0x96, 0xdd, 0x0f, 0x71, 0x3e, 0xb7, 0xe1, 0x08, 0x89, 0xe6,
123 0xe0, 0x95, 0xa8, 0x6f, 0xc5, 0xa0, 0x33, 0x53, 0x6e, 0x89, 0x8b, 0xb3,
124 0x14, 0x1d, 0x02, 0x35, 0xa4, 0x1c, 0x74, 0xc4, 0xbb, 0x87, 0x46, 0x99,
125 0x10, 0x05, 0x67, 0x6b, 0x28, 0x50, 0xf7, 0xaf, 0xcf, 0x69, 0xda, 0x63,
126 0x28, 0xd1, 0x34, 0x2e, 0xea, 0xfd, 0x9d, 0x4c, 0x5b, 0x02, 0x03, 0x01,
127 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
128 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x64, 0x55,
129 0xef, 0x5b, 0x91, 0xb7, 0xfd, 0x5d, 0x00, 0x3b, 0x0c, 0x0f, 0xd7, 0xe0,
130 0x26, 0xfc, 0xd6, 0xf3, 0xd8, 0xc5, 0x00, 0xdf, 0x3b, 0x85, 0x70, 0x91,
131 0x85, 0x35, 0xb1, 0x7d, 0x78, 0x58, 0x33, 0x39, 0x27, 0xc4, 0x9e, 0x56,
132 0x31, 0xbd, 0x89, 0x02, 0x56, 0x8c, 0x73, 0xf8, 0x13, 0xa6, 0x20, 0xe2,
133 0x40, 0x19, 0x1b, 0xbc, 0x1f, 0xa2, 0x25, 0xee, 0x40, 0x7a, 0x98, 0x10,
134 0x59, 0xbc, 0xb1, 0x3c, 0x93, 0x6d, 0x4a, 0x50, 0x3d, 0x19, 0xf2, 0x81,
135 0xcf, 0x52, 0x0d, 0x47, 0x97, 0x05, 0xb0, 0xe2, 0xf6, 0xed, 0x5a, 0xc1,
136 0xa0, 0xc6, 0x07, 0x31, 0xaa, 0x25, 0xbd, 0xe7, 0xac, 0x95, 0xcd, 0x40,
137 0x5b, 0x61, 0xdf, 0x06, 0xd5, 0xd6, 0x5d, 0xe5, 0x92, 0x10, 0x5e, 0xc5,
138 0x40, 0xd8, 0x32, 0x7b, 0xc6, 0x43, 0x3c, 0xdc, 0xde, 0x49, 0x64, 0x88,
139 0xd1, 0x5c, 0x8a, 0xdb, 0xbe, 0xb6, 0xc2, 0xc4, 0xe0, 0x4e, 0xe5, 0x21,
140 0x1c, 0x06, 0x89, 0xe3, 0x9e, 0xba, 0xd1, 0xe5, 0xf9, 0xef, 0xe7, 0xbc,
141 0x22, 0xf6, 0x8c, 0xef, 0x13, 0x84, 0x7c, 0x13, 0xc3, 0x29, 0x8b, 0x54,
142 0xd1, 0xad, 0xbc, 0x66, 0xe8, 0x6f, 0x4a, 0xbd, 0x9a, 0x90, 0x9b, 0x46,
143 0x0b, 0x07, 0x2c, 0xd8, 0x9e, 0xab, 0xb3, 0xa2, 0x3e, 0xad, 0x5f, 0x38,
144 0x52, 0x4b, 0x43, 0xc4, 0x50, 0xbd, 0x2d, 0x47, 0xb3, 0x06, 0x8f, 0x03,
145 0xf4, 0x59, 0x0c, 0x3c, 0xba, 0x0f, 0x28, 0xa3, 0x47, 0xd5, 0xd5, 0xd1,
146 0xe8, 0xb3, 0xbc, 0x18, 0xe9, 0x2a, 0x59, 0x4a, 0xe1, 0x3c, 0x81, 0x26,
147 0x7f, 0x2f, 0x4a, 0x61, 0xeb, 0x37, 0xab, 0x66, 0x57, 0xea, 0xcb, 0xe4,
148 0xe2, 0xbc, 0x01, 0xb6, 0x89, 0xa6, 0x1d, 0x1b, 0xf7, 0xd2, 0x43, 0xf1,
149 0x9e, 0x75, 0x35, 0x61, 0x7b, 0x79, 0xd9, 0x18, 0xbe, 0x5d, 0xcc, 0xce,
150 0xc0, 0x4b
151 };
152
153 uint8_t heartbeat_message[] = {
154 0x01, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
155 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
156 };
157
158 uint8_t warning_alert[] = { /* warning: user cancelled */
159 0x01, 0x5a
160 };
161
162 uint8_t fatal_alert[] = { /* Fatal: unexpected message */
163 0x02, 0x0a
164 };
165
166 void fragmented_message(int write_fd)
167 {
168 int written = 0;
169 /* Split the hello message into 5 fragments and write it */
170 for (int i = 0; i < 5; i++) {
171 int length = sizeof(server_hello_message) / 5;
172
173 if (i == 0) {
174 length += sizeof(server_hello_message) % 5;
175 }
176
177 uint8_t record_header[5] = { TLS_HANDSHAKE, 0x03, 0x03, (length >> 8), length & 0xff };
178
179 if (write(write_fd, record_header, 5) != 5) {
180 _exit(100);
181 }
182
183 if (write(write_fd, server_hello_message + written, length) != length) {
184 _exit(100);
185 }
186
187 written += length;
188 }
189
190 /* Close the pipe and exit */
191 close(write_fd);
192 }
193
194 void coalesced_message(int write_fd)
195 {
196 int length = sizeof(server_hello_message) + sizeof(server_cert);
197
198 uint8_t record_header[5] = { TLS_HANDSHAKE, 0x03, 0x03, (length >> 8), length & 0xff };
199
200 if (write(write_fd, record_header, 5) != 5) {
201 _exit(100);
202 }
203
204 if (write(write_fd, server_hello_message, sizeof(server_hello_message)) != sizeof(server_hello_message)) {
205 _exit(100);
206 }
207
208 if (write(write_fd, server_cert, sizeof(server_cert)) != sizeof(server_cert)) {
209 _exit(100);
210 }
211
212 close(write_fd);
213 }
214
215 void interleaved_message(int write_fd)
216 {
217 int length = sizeof(server_hello_message) / 2;
218 uint8_t record_header[5] = { TLS_HANDSHAKE, 0x03, 0x03, (length >> 8), length & 0xff };
219 int written = 0;
220
221 /* Write half of the message */
222 if (write(write_fd, record_header, 5) != 5) {
223 _exit(100);
224 }
225 if (write(write_fd, server_hello_message, length) != length) {
226 _exit(100);
227 }
228 written += length;
229
230 /* Write the heartbeat record */
231 record_header[0] = TLS_HEARTBEAT;
232 record_header[3] = sizeof(heartbeat_message) >> 8;
233 record_header[4] = sizeof(heartbeat_message) & 0xff;
234
235 if (write(write_fd, record_header, 5) != 5) {
236 _exit(100);
237 }
238 if (write(write_fd, heartbeat_message, sizeof(heartbeat_message)) != sizeof(heartbeat_message)) {
239 _exit(100);
240 }
241
242 /* Write the rest of the hello message */
243 length = sizeof(server_hello_message) - written;
244 record_header[0] = TLS_HANDSHAKE;
245 record_header[3] = length >> 8;
246 record_header[4] = length & 0xff;
247 if (write(write_fd, record_header, 5) != 5) {
248 _exit(100);
249 }
250 if (write(write_fd, server_hello_message + written, length) != length) {
251 _exit(100);
252 }
253
254 /* Close the pipe and exit */
255 close(write_fd);
256 }
257
258 void interleaved_fragmented_fatal_alert(int write_fd)
259 {
260 int length = sizeof(server_hello_message) / 2;
261 uint8_t record_header[5] = { TLS_HANDSHAKE, 0x03, 0x03, (length >> 8), length & 0xff };
262 int written = 0;
263
264 /* Write half of the message */
265 if (write(write_fd, record_header, 5) != 5) {
266 _exit(100);
267 }
268 if (write(write_fd, server_hello_message, length) != length) {
269 _exit(100);
270 }
271 written += length;
272
273 /* Write half of the alert message */
274 record_header[0] = TLS_ALERT;
275 record_header[3] = 0;
276 record_header[4] = 1;
277
278 if (write(write_fd, record_header, 5) != 5) {
279 _exit(100);
280 }
281 if (write(write_fd, fatal_alert, 1) != 1) {
282 _exit(100);
283 }
284
285 /* Write another quarter of the of the hello message */
286 length = sizeof(server_hello_message) / 4;
287 record_header[0] = TLS_HANDSHAKE;
288 record_header[3] = length >> 8;
289 record_header[4] = length & 0xff;
290 if (write(write_fd, record_header, 5) != 5) {
291 _exit(100);
292 }
293 if (write(write_fd, server_hello_message + written, length) != length) {
294 _exit(100);
295 }
296 written += length;
297
298 /* Write second half of the alert message */
299 record_header[0] = TLS_ALERT;
300 record_header[3] = 0;
301 record_header[4] = 1;
302
303 if (write(write_fd, record_header, 5) != 5) {
304 _exit(100);
305 }
306 if (write(write_fd, fatal_alert + 1, 1) != 1) {
307 _exit(100);
308 }
309
310 /* Write the rest of the hello message */
311 length = sizeof(server_hello_message) - written;
312 record_header[0] = TLS_HANDSHAKE;
313 record_header[3] = length >> 8;
314 record_header[4] = length & 0xff;
315 if (write(write_fd, record_header, 5) != 5) {
316 _exit(100);
317 }
318 if (write(write_fd, server_hello_message + written, length) != length) {
319 _exit(100);
320 }
321
322 /* Close the pipe and exit */
323 close(write_fd);
324 }
325
326 void interleaved_fragmented_warning_alert(int write_fd)
327 {
328 int length = sizeof(server_hello_message) / 2;
329 uint8_t record_header[5] = { TLS_HANDSHAKE, 0x03, 0x03, (length >> 8), length & 0xff };
330 int written = 0;
331
332 /* Write half of the message */
333 if (write(write_fd, record_header, 5) != 5) {
334 _exit(100);
335 }
336 if (write(write_fd, server_hello_message, length) != length) {
337 _exit(100);
338 }
339 written += length;
340
341 /* Write half of the alert message */
342 record_header[0] = TLS_ALERT;
343 record_header[3] = 0;
344 record_header[4] = 1;
345 if (write(write_fd, warning_alert, 1) != 1) {
346 _exit(100);
347 }
348
349 /* Write another quarter of the of the hello message */
350 length = sizeof(server_hello_message) / 4;
351 record_header[0] = TLS_HANDSHAKE;
352 record_header[3] = length >> 8;
353 record_header[4] = length & 0xff;
354 if (write(write_fd, record_header, 5) != 5) {
355 _exit(100);
356 }
357 if (write(write_fd, server_hello_message + written, length) != length) {
358 _exit(100);
359 }
360 written += length;
361
362 /* Write second half of the alert message */
363 record_header[0] = TLS_ALERT;
364 record_header[3] = 0;
365 record_header[4] = 1;
366
367 if (write(write_fd, record_header, 5) != 5) {
368 _exit(100);
369 }
370 if (write(write_fd, warning_alert + 1, 1) != 1) {
371 _exit(100);
372 }
373
374 /* Write the rest of the hello message */
375 length = sizeof(server_hello_message) - written;
376 record_header[0] = TLS_HANDSHAKE;
377 record_header[3] = length >> 8;
378 record_header[4] = length & 0xff;
379 if (write(write_fd, record_header, 5) != 5) {
380 _exit(100);
381 }
382 if (write(write_fd, server_hello_message + written, length) != length) {
383 _exit(100);
384 }
385
386 /* Close the pipe and exit */
387 close(write_fd);
388 }
389
390 int main(int argc, char **argv)
391 {
392 struct s2n_connection *conn = NULL;
393 struct s2n_config *config = NULL;
394
395 s2n_blocked_status blocked;
396 int status = 0;
397 pid_t pid = 0;
398 int p[2];
399
400 BEGIN_TEST();
401 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
402
403 EXPECT_NOT_NULL(config = s2n_config_new());
404 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
405 EXPECT_SUCCESS(s2n_config_set_check_stapled_ocsp_response(config, 0));
406 /* The server hello has TLS_RSA_WITH_AES_256_CBC_SHA256 hardcoded,
407 so we need to set a cipher preference that will accept that value */
408 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20170328"));
409 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
410 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
411
412 conn->server_protocol_version = S2N_TLS12;
413 conn->client_protocol_version = S2N_TLS12;
414 conn->actual_protocol_version = S2N_TLS12;
415
416 /* Create a pipe */
417 EXPECT_SUCCESS(pipe(p));
418
419 /* Set up the connection to read from the fd */
420 EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, p[0]));
421
422 /* Pretend the client hello has already been set */
423 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
424 conn->handshake.message_number = SERVER_HELLO;
425
426 /* Create a child process */
427 pid = fork();
428 if (pid == 0) {
429 /* This is the child process, close the read end of the pipe */
430 EXPECT_SUCCESS(close(p[0]));
431
432 /* Write the fragmented hello message */
433 fragmented_message(p[1]);
434 EXPECT_SUCCESS(s2n_config_free(config));
435 EXPECT_SUCCESS(s2n_connection_free(conn));
436 exit(0);
437 }
438
439 /* This is the parent process, close the write end of the pipe */
440 EXPECT_SUCCESS(close(p[1]));
441
442 /* Negotiate the handshake. This will fail due to EOF, but that's ok. */
443 EXPECT_FAILURE(s2n_negotiate(conn, &blocked));
444
445 /* Verify that the data is as we expect it */
446 EXPECT_EQUAL(memcmp(conn->handshake_params.server_random, zero_to_thirty_one, 32), 0);
447
448 /* Check that the server hello message was processed */
449 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), SERVER_CERT);
450
451 /* Clean up */
452 EXPECT_EQUAL(waitpid(pid, &status, 0), pid);
453 EXPECT_EQUAL(status, 0);
454 EXPECT_SUCCESS(close(p[0]));
455
456 /* Create a pipe */
457 EXPECT_SUCCESS(pipe(p));
458
459 /* Wipe the connection */
460 EXPECT_SUCCESS(s2n_connection_wipe(conn));
461 conn->server_protocol_version = S2N_TLS12;
462 conn->client_protocol_version = S2N_TLS12;
463 conn->actual_protocol_version = S2N_TLS12;
464
465 /* Set up the connection to read from the fd */
466 EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, p[0]));
467
468 /* Pretend the client hello has already been set */
469 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
470 conn->handshake.message_number = SERVER_HELLO;
471
472 /* Create a child process */
473 pid = fork();
474 if (pid == 0) {
475 /* This is the child process, close the read end of the pipe */
476 EXPECT_SUCCESS(close(p[0]));
477
478 /* Write the fragmented hello message */
479 coalesced_message(p[1]);
480 EXPECT_SUCCESS(s2n_connection_free(conn));
481 EXPECT_SUCCESS(s2n_config_free(config));
482 exit(0);
483 }
484
485 /* This is the parent process, close the write end of the pipe */
486 EXPECT_SUCCESS(close(p[1]));
487
488 /* Negotiate the handshake. This will fail due to EOF, but that's ok. */
489 EXPECT_FAILURE(s2n_negotiate(conn, &blocked));
490
491 /* Verify that the data is as we expect it */
492 EXPECT_EQUAL(memcmp(conn->handshake_params.server_random, zero_to_thirty_one, 32), 0);
493
494 /* Check that the server done message was processed */
495 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), SERVER_HELLO_DONE);
496
497 /* Clean up */
498 EXPECT_EQUAL(waitpid(pid, &status, 0), pid);
499 EXPECT_EQUAL(status, 0);
500 EXPECT_SUCCESS(close(p[0]));
501
502 /* Create a pipe */
503 EXPECT_SUCCESS(pipe(p));
504
505 /* Wipe the connection */
506 EXPECT_SUCCESS(s2n_connection_wipe(conn));
507 conn->server_protocol_version = S2N_TLS12;
508 conn->client_protocol_version = S2N_TLS12;
509 conn->actual_protocol_version = S2N_TLS12;
510
511 /* Set up the connection to read from the fd */
512 EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, p[0]));
513
514 /* Pretend the client hello has already been set */
515 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
516 conn->handshake.message_number = SERVER_HELLO;
517
518 /* Create a child process */
519 pid = fork();
520 if (pid == 0) {
521 /* This is the child process, close the read end of the pipe */
522 EXPECT_SUCCESS(close(p[0]));
523
524 /* Write the fragmented hello message */
525 interleaved_message(p[1]);
526 EXPECT_SUCCESS(s2n_connection_free(conn));
527 EXPECT_SUCCESS(s2n_config_free(config));
528 exit(0);
529 }
530
531 /* This is the parent process, close the write end of the pipe */
532 EXPECT_SUCCESS(close(p[1]));
533
534 /* Negotiate the handshake. This will fail due to EOF, but that's ok. */
535 EXPECT_FAILURE(s2n_negotiate(conn, &blocked));
536
537 /* Verify that the data is as we expect it */
538 EXPECT_EQUAL(memcmp(conn->handshake_params.server_random, zero_to_thirty_one, 32), 0);
539
540 /* Check that the server hello message was processed */
541 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), SERVER_CERT);
542
543 /* Clean up */
544 EXPECT_EQUAL(waitpid(pid, &status, 0), pid);
545 EXPECT_EQUAL(status, 0);
546 EXPECT_SUCCESS(close(p[0]));
547
548 /* Create a pipe */
549 EXPECT_SUCCESS(pipe(p));
550
551 /* Wipe the connection */
552 EXPECT_SUCCESS(s2n_connection_wipe(conn));
553 conn->server_protocol_version = S2N_TLS12;
554 conn->client_protocol_version = S2N_TLS12;
555 conn->actual_protocol_version = S2N_TLS12;
556
557 /* Set up the connection to read from the fd */
558 EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, p[0]));
559
560 /* Pretend the client hello has already been set */
561 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
562 conn->handshake.message_number = SERVER_HELLO;
563
564 /* Create a child process */
565 pid = fork();
566 if (pid == 0) {
567 /* This is the child process, close the read end of the pipe */
568 EXPECT_SUCCESS(close(p[0]));
569
570 /* Write the fragmented hello message */
571 interleaved_fragmented_warning_alert(p[1]);
572 EXPECT_SUCCESS(s2n_connection_free(conn));
573 EXPECT_SUCCESS(s2n_config_free(config));
574 exit(0);
575 }
576
577 /* This is the parent process, close the write end of the pipe */
578 EXPECT_SUCCESS(close(p[1]));
579
580 /* Negotiate the handshake. This will fail due to EOF, but that's ok. */
581 EXPECT_FAILURE(s2n_negotiate(conn, &blocked));
582
583 /* Verify that the data is as we expect it */
584 EXPECT_NOT_EQUAL(memcmp(conn->handshake_params.server_random, zero_to_thirty_one, 32), 0);
585
586 /* Check that the server hello message was not processed */
587 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), SERVER_HELLO);
588
589 /* Clean up */
590 EXPECT_EQUAL(waitpid(pid, &status, 0), pid);
591 EXPECT_EQUAL(status, 0);
592 EXPECT_SUCCESS(close(p[0]));
593
594 /* Create a pipe */
595 EXPECT_SUCCESS(pipe(p));
596
597 /* Wipe the connection */
598 EXPECT_SUCCESS(s2n_connection_wipe(conn));
599 conn->server_protocol_version = S2N_TLS12;
600 conn->client_protocol_version = S2N_TLS12;
601 conn->actual_protocol_version = S2N_TLS12;
602
603 /* Set up the connection to read from the fd */
604 EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, p[0]));
605
606 /* Pretend the client hello has already been set */
607 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
608 conn->handshake.message_number = SERVER_HELLO;
609
610 /* Create a child process */
611 pid = fork();
612 if (pid == 0) {
613 /* This is the child process, close the read end of the pipe */
614 EXPECT_SUCCESS(close(p[0]));
615
616 /* Write the fragmented hello message */
617 interleaved_fragmented_fatal_alert(p[1]);
618 EXPECT_SUCCESS(s2n_connection_free(conn));
619 EXPECT_SUCCESS(s2n_config_free(config));
620 exit(0);
621 }
622
623 /* This is the parent process, close the write end of the pipe */
624 EXPECT_SUCCESS(close(p[1]));
625
626 /* Negotiate the handshake. This will fail due to EOF, but that's ok. */
627 EXPECT_FAILURE(s2n_negotiate(conn, &blocked));
628
629 /* Verify that the data failed */
630 EXPECT_NOT_EQUAL(memcmp(conn->handshake_params.server_random, zero_to_thirty_one, 32), 0);
631
632 /* Check that the server hello message was not processed */
633 EXPECT_NOT_EQUAL(s2n_conn_get_current_message_type(conn), SERVER_CERT);
634
635 /* Clean up */
636 EXPECT_EQUAL(waitpid(pid, &status, 0), pid);
637 EXPECT_EQUAL(status, 0);
638 EXPECT_SUCCESS(close(p[0]));
639
640 EXPECT_SUCCESS(s2n_connection_free(conn));
641 EXPECT_SUCCESS(s2n_config_free(config));
642
643 END_TEST();
644 }