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