]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/tls13ccstest.c
The record version for ClientHello2 should be TLS1.2
[thirdparty/openssl.git] / test / tls13ccstest.c
1 /*
2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/ssl.h>
11 #include <string.h>
12 #include "ssltestlib.h"
13 #include "testutil.h"
14 #include "../ssl/packet_locl.h"
15
16 static char *cert = NULL;
17 static char *privkey = NULL;
18
19 static BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
20 static int chseen = 0, shseen = 0, sccsseen = 0, ccsaftersh = 0;
21 static int ccsbeforesh = 0, sappdataseen = 0, cappdataseen = 0, badccs = 0;
22 static int badvers = 0, badsessid = 0;
23
24 static unsigned char chsessid[SSL_MAX_SSL_SESSION_ID_LENGTH];
25 static size_t chsessidlen = 0;
26
27 static int watchccs_new(BIO *bi);
28 static int watchccs_free(BIO *a);
29 static int watchccs_read(BIO *b, char *out, int outl);
30 static int watchccs_write(BIO *b, const char *in, int inl);
31 static long watchccs_ctrl(BIO *b, int cmd, long num, void *ptr);
32 static int watchccs_gets(BIO *bp, char *buf, int size);
33 static int watchccs_puts(BIO *bp, const char *str);
34
35 /* Choose a sufficiently large type likely to be unused for this custom BIO */
36 # define BIO_TYPE_WATCHCCS_FILTER (0x80 | BIO_TYPE_FILTER)
37
38 static BIO_METHOD *method_watchccs = NULL;
39
40 static const BIO_METHOD *bio_f_watchccs_filter()
41 {
42 if (method_watchccs == NULL) {
43 method_watchccs = BIO_meth_new(BIO_TYPE_WATCHCCS_FILTER,
44 "Watch CCS filter");
45 if ( method_watchccs == NULL
46 || !BIO_meth_set_write(method_watchccs, watchccs_write)
47 || !BIO_meth_set_read(method_watchccs, watchccs_read)
48 || !BIO_meth_set_puts(method_watchccs, watchccs_puts)
49 || !BIO_meth_set_gets(method_watchccs, watchccs_gets)
50 || !BIO_meth_set_ctrl(method_watchccs, watchccs_ctrl)
51 || !BIO_meth_set_create(method_watchccs, watchccs_new)
52 || !BIO_meth_set_destroy(method_watchccs, watchccs_free))
53 return NULL;
54 }
55 return method_watchccs;
56 }
57
58 static int watchccs_new(BIO *bio)
59 {
60 BIO_set_init(bio, 1);
61 return 1;
62 }
63
64 static int watchccs_free(BIO *bio)
65 {
66 BIO_set_init(bio, 0);
67 return 1;
68 }
69
70 static int watchccs_read(BIO *bio, char *out, int outl)
71 {
72 int ret = 0;
73 BIO *next = BIO_next(bio);
74
75 if (outl <= 0)
76 return 0;
77 if (next == NULL)
78 return 0;
79
80 BIO_clear_retry_flags(bio);
81
82 ret = BIO_read(next, out, outl);
83 if (ret <= 0 && BIO_should_read(next))
84 BIO_set_retry_read(bio);
85
86 return ret;
87 }
88
89 static int watchccs_write(BIO *bio, const char *in, int inl)
90 {
91 int ret = 0;
92 BIO *next = BIO_next(bio);
93 PACKET pkt, msg, msgbody, sessionid;
94 unsigned int rectype, recvers, msgtype, expectedrecvers;
95
96 if (inl <= 0)
97 return 0;
98 if (next == NULL)
99 return 0;
100
101 BIO_clear_retry_flags(bio);
102
103 if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
104 return 0;
105
106 /* We assume that we always write complete records each time */
107 while (PACKET_remaining(&pkt)) {
108 if (!PACKET_get_1(&pkt, &rectype)
109 || !PACKET_get_net_2(&pkt, &recvers)
110 || !PACKET_get_length_prefixed_2(&pkt, &msg))
111 return 0;
112
113 expectedrecvers = TLS1_2_VERSION;
114
115 if (rectype == SSL3_RT_HANDSHAKE) {
116 if (!PACKET_get_1(&msg, &msgtype)
117 || !PACKET_get_length_prefixed_3(&msg, &msgbody))
118 return 0;
119 if (msgtype == SSL3_MT_CLIENT_HELLO) {
120 chseen++;
121
122 /*
123 * Skip legacy_version (2 bytes) and Random (32 bytes) to read
124 * session_id.
125 */
126 if (!PACKET_forward(&msgbody, 34)
127 || !PACKET_get_length_prefixed_1(&msgbody, &sessionid))
128 return 0;
129
130 if (chseen == 1) {
131 expectedrecvers = TLS1_VERSION;
132
133 /* Save the session id for later */
134 chsessidlen = PACKET_remaining(&sessionid);
135 if (!PACKET_copy_bytes(&sessionid, chsessid, chsessidlen))
136 return 0;
137 } else {
138 /*
139 * Check the session id for the second ClientHello is the
140 * same as the first one.
141 */
142 if (PACKET_remaining(&sessionid) != chsessidlen
143 || (chsessidlen > 0
144 && memcmp(chsessid, PACKET_data(&sessionid),
145 chsessidlen) != 0))
146 badsessid = 1;
147 }
148 } else if (msgtype == SSL3_MT_SERVER_HELLO) {
149 shseen++;
150 /*
151 * Skip legacy_version (2 bytes) and Random (32 bytes) to read
152 * session_id.
153 */
154 if (!PACKET_forward(&msgbody, 34)
155 || !PACKET_get_length_prefixed_1(&msgbody, &sessionid))
156 return 0;
157
158 /*
159 * Check the session id is the same as the one in the
160 * ClientHello
161 */
162 if (PACKET_remaining(&sessionid) != chsessidlen
163 || (chsessidlen > 0
164 && memcmp(chsessid, PACKET_data(&sessionid),
165 chsessidlen) != 0))
166 badsessid = 1;
167 }
168 } else if (rectype == SSL3_RT_CHANGE_CIPHER_SPEC) {
169 if (bio == s_to_c_fbio) {
170 /*
171 * Server writing. We shouldn't have written any app data
172 * yet, and we should have seen both the ClientHello and the
173 * ServerHello
174 */
175 if (!sappdataseen
176 && chseen == 1
177 && shseen == 1
178 && !sccsseen)
179 sccsseen = 1;
180 else
181 badccs = 1;
182 } else if (!cappdataseen) {
183 /*
184 * Client writing. We shouldn't have written any app data
185 * yet, and we should have seen the ClientHello
186 */
187 if (shseen == 1 && !ccsaftersh)
188 ccsaftersh = 1;
189 else if (shseen == 0 && !ccsbeforesh)
190 ccsbeforesh = 1;
191 else
192 badccs = 1;
193 } else {
194 badccs = 1;
195 }
196 } else if(rectype == SSL3_RT_APPLICATION_DATA) {
197 if (bio == s_to_c_fbio)
198 sappdataseen = 1;
199 else
200 cappdataseen = 1;
201 }
202 if (recvers != expectedrecvers)
203 badvers = 1;
204 }
205
206 ret = BIO_write(next, in, inl);
207 if (ret <= 0 && BIO_should_write(next))
208 BIO_set_retry_write(bio);
209
210 return ret;
211 }
212
213 static long watchccs_ctrl(BIO *bio, int cmd, long num, void *ptr)
214 {
215 long ret;
216 BIO *next = BIO_next(bio);
217
218 if (next == NULL)
219 return 0;
220
221 switch (cmd) {
222 case BIO_CTRL_DUP:
223 ret = 0;
224 break;
225 default:
226 ret = BIO_ctrl(next, cmd, num, ptr);
227 break;
228 }
229 return ret;
230 }
231
232 static int watchccs_gets(BIO *bio, char *buf, int size)
233 {
234 /* We don't support this - not needed anyway */
235 return -1;
236 }
237
238 static int watchccs_puts(BIO *bio, const char *str)
239 {
240 return watchccs_write(bio, str, strlen(str));
241 }
242
243 static int test_tls13ccs(int tst)
244 {
245 SSL_CTX *sctx = NULL, *cctx = NULL;
246 SSL *sssl = NULL, *cssl = NULL;
247 int ret = 0;
248 const char msg[] = "Dummy data";
249 char buf[80];
250 size_t written, readbytes;
251 SSL_SESSION *sess = NULL;
252
253 chseen = shseen = sccsseen = ccsaftersh = ccsbeforesh = 0;
254 sappdataseen = cappdataseen = badccs = badvers = badsessid = 0;
255 chsessidlen = 0;
256
257 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
258 &sctx, &cctx, cert, privkey)))
259 goto err;
260
261 /*
262 * Test 0: Simple Handshake
263 * Test 1: Simple Handshake, client middlebox compat mode disabled
264 * Test 2: Simple Handshake, server middlebox compat mode disabled
265 * Test 3: HRR Handshake
266 * Test 4: HRR Handshake, client middlebox compat mode disabled
267 * Test 5: HRR Handshake, server middlebox compat mode disabled
268 * Test 6: Early data handshake
269 * Test 7: Early data handshake, client middlebox compat mode disabled
270 * Test 8: Early data handshake, server middlebox compat mode disabled
271 * Test 9: Early data then HRR
272 * Test 10: Early data then HRR, client middlebox compat mode disabled
273 * Test 11: Early data then HRR, server middlebox compat mode disabled
274 */
275 switch (tst) {
276 case 0:
277 case 3:
278 case 6:
279 case 9:
280 break;
281 case 1:
282 case 4:
283 case 7:
284 case 10:
285 SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
286 break;
287 case 2:
288 case 5:
289 case 8:
290 case 11:
291 SSL_CTX_clear_options(sctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
292 break;
293 default:
294 TEST_error("Invalid test value");
295 goto err;
296 }
297
298 if (tst >= 6) {
299 /* Get a session suitable for early_data */
300 if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL))
301 || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
302 goto err;
303 sess = SSL_get1_session(cssl);
304 if (!TEST_ptr(sess))
305 goto err;
306 SSL_shutdown(cssl);
307 SSL_shutdown(sssl);
308 SSL_free(sssl);
309 SSL_free(cssl);
310 sssl = cssl = NULL;
311 }
312
313 if ((tst >= 3 && tst <= 5) || tst >= 9) {
314 /* HRR handshake */
315 if (!TEST_true(SSL_CTX_set1_groups_list(sctx, "P-256")))
316 goto err;
317 }
318
319 s_to_c_fbio = BIO_new(bio_f_watchccs_filter());
320 c_to_s_fbio = BIO_new(bio_f_watchccs_filter());
321 if (!TEST_ptr(s_to_c_fbio)
322 || !TEST_ptr(c_to_s_fbio)) {
323 BIO_free(s_to_c_fbio);
324 BIO_free(c_to_s_fbio);
325 goto err;
326 }
327
328 /* BIOs get freed on error */
329 if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, s_to_c_fbio,
330 c_to_s_fbio)))
331 goto err;
332
333 if (tst >= 6) {
334 /* Early data */
335 if (!TEST_true(SSL_set_session(cssl, sess))
336 || !TEST_true(SSL_write_early_data(cssl, msg, strlen(msg),
337 &written))
338 || (tst <= 8
339 && !TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf),
340 &readbytes),
341 SSL_READ_EARLY_DATA_SUCCESS)))
342 goto err;
343 if (tst <= 8) {
344 if (!TEST_int_gt(SSL_connect(cssl), 0))
345 goto err;
346 } else {
347 if (!TEST_int_le(SSL_connect(cssl), 0))
348 goto err;
349 }
350 if (!TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf),
351 &readbytes),
352 SSL_READ_EARLY_DATA_FINISH))
353 goto err;
354 }
355
356 /* Perform handshake (or complete it if doing early data ) */
357 if (!TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
358 goto err;
359
360 /*
361 * Check there were no unexpected CCS messages, all record versions
362 * were as expected, and that the session ids were reflected by the server
363 * correctly.
364 */
365 if (!TEST_false(badccs) || !TEST_false(badvers) || !TEST_false(badsessid))
366 goto err;
367
368 switch (tst) {
369 case 0:
370 if (!TEST_true(sccsseen)
371 || !TEST_true(ccsaftersh)
372 || !TEST_false(ccsbeforesh)
373 || !TEST_size_t_gt(chsessidlen, 0))
374 goto err;
375 break;
376
377 case 1:
378 if (!TEST_true(sccsseen)
379 || !TEST_false(ccsaftersh)
380 || !TEST_false(ccsbeforesh)
381 || !TEST_size_t_eq(chsessidlen, 0))
382 goto err;
383 break;
384
385 case 2:
386 if (!TEST_false(sccsseen)
387 || !TEST_true(ccsaftersh)
388 || !TEST_false(ccsbeforesh)
389 || !TEST_size_t_gt(chsessidlen, 0))
390 goto err;
391 break;
392
393 case 3:
394 if (!TEST_true(sccsseen)
395 || !TEST_true(ccsaftersh)
396 || !TEST_false(ccsbeforesh)
397 || !TEST_size_t_gt(chsessidlen, 0))
398 goto err;
399 break;
400
401 case 4:
402 if (!TEST_true(sccsseen)
403 || !TEST_false(ccsaftersh)
404 || !TEST_false(ccsbeforesh)
405 || !TEST_size_t_eq(chsessidlen, 0))
406 goto err;
407 break;
408
409 case 5:
410 if (!TEST_false(sccsseen)
411 || !TEST_true(ccsaftersh)
412 || !TEST_false(ccsbeforesh)
413 || !TEST_size_t_gt(chsessidlen, 0))
414 goto err;
415 break;
416
417 case 6:
418 if (!TEST_true(sccsseen)
419 || !TEST_false(ccsaftersh)
420 || !TEST_true(ccsbeforesh)
421 || !TEST_size_t_gt(chsessidlen, 0))
422 goto err;
423 break;
424
425 case 7:
426 if (!TEST_true(sccsseen)
427 || !TEST_false(ccsaftersh)
428 || !TEST_false(ccsbeforesh)
429 || !TEST_size_t_eq(chsessidlen, 0))
430 goto err;
431 break;
432
433 case 8:
434 if (!TEST_false(sccsseen)
435 || !TEST_false(ccsaftersh)
436 || !TEST_true(ccsbeforesh)
437 || !TEST_size_t_gt(chsessidlen, 0))
438 goto err;
439 break;
440
441 case 9:
442 if (!TEST_true(sccsseen)
443 || !TEST_false(ccsaftersh)
444 || !TEST_true(ccsbeforesh)
445 || !TEST_size_t_gt(chsessidlen, 0))
446 goto err;
447 break;
448
449 case 10:
450 if (!TEST_true(sccsseen)
451 || !TEST_false(ccsaftersh)
452 || !TEST_false(ccsbeforesh)
453 || !TEST_size_t_eq(chsessidlen, 0))
454 goto err;
455 break;
456
457 case 11:
458 if (!TEST_false(sccsseen)
459 || !TEST_false(ccsaftersh)
460 || !TEST_true(ccsbeforesh)
461 || !TEST_size_t_gt(chsessidlen, 0))
462 goto err;
463 break;
464
465 default:
466 TEST_error("Invalid test value");
467 goto err;
468 }
469
470 ret = 1;
471 err:
472 SSL_SESSION_free(sess);
473 SSL_free(sssl);
474 SSL_free(cssl);
475 SSL_CTX_free(sctx);
476 SSL_CTX_free(cctx);
477
478 return ret;
479 }
480
481 int setup_tests(void)
482 {
483 if (!TEST_ptr(cert = test_get_argument(0))
484 || !TEST_ptr(privkey = test_get_argument(1)))
485 return 0;
486
487 ADD_ALL_TESTS(test_tls13ccs, 12);
488
489 return 1;
490 }
491
492 void cleanup_tests(void)
493 {
494 BIO_meth_free(method_watchccs);
495 }