2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/ssl.h>
12 #include "ssltestlib.h"
14 #include "../ssl/packet_locl.h"
16 static char *cert = NULL;
17 static char *privkey = NULL;
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;
23 unsigned char chsessid[SSL_MAX_SSL_SESSION_ID_LENGTH];
24 size_t chsessidlen = 0;
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);
34 /* Choose a sufficiently large type likely to be unused for this custom BIO */
35 # define BIO_TYPE_WATCHCCS_FILTER (0x80 | BIO_TYPE_FILTER)
37 static BIO_METHOD *method_watchccs = NULL;
39 static const BIO_METHOD *bio_f_watchccs_filter()
41 if (method_watchccs == NULL) {
42 method_watchccs = BIO_meth_new(BIO_TYPE_WATCHCCS_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))
54 return method_watchccs;
57 static int watchccs_new(BIO *bio)
63 static int watchccs_free(BIO *bio)
69 static int watchccs_read(BIO *bio, char *out, int outl)
72 BIO *next = BIO_next(bio);
79 BIO_clear_retry_flags(bio);
81 ret = BIO_read(next, out, outl);
82 if (ret <= 0 && BIO_should_read(next))
83 BIO_set_retry_read(bio);
88 static int watchccs_write(BIO *bio, const char *in, int inl)
91 BIO *next = BIO_next(bio);
92 PACKET pkt, msg, msgbody, sessionid;
93 unsigned int rectype, recvers, msgtype, expectedrecvers;
100 BIO_clear_retry_flags(bio);
102 if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
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))
112 expectedrecvers = TLS1_2_VERSION;
114 if (rectype == SSL3_RT_HANDSHAKE) {
115 if (!PACKET_get_1(&msg, &msgtype)
116 || !PACKET_get_length_prefixed_3(&msg, &msgbody))
118 if (msgtype == SSL3_MT_CLIENT_HELLO) {
120 expectedrecvers = TLS1_VERSION;
122 * Skip legacy_version (2 bytes) and Random (32 bytes) to read
125 if (!PACKET_forward(&msgbody, 34)
126 || !PACKET_get_length_prefixed_1(&msgbody, &sessionid))
130 /* Save the session id for later */
131 chsessidlen = PACKET_remaining(&sessionid);
132 if (!PACKET_copy_bytes(&sessionid, chsessid, chsessidlen))
136 * Check the session id for the second ClientHello is the
137 * same as the first one.
139 if (PACKET_remaining(&sessionid) != chsessidlen
141 && memcmp(chsessid, PACKET_data(&sessionid),
145 } else if (msgtype == SSL3_MT_SERVER_HELLO) {
148 * Skip legacy_version (2 bytes) and Random (32 bytes) to read
151 if (!PACKET_forward(&msgbody, 34)
152 || !PACKET_get_length_prefixed_1(&msgbody, &sessionid))
156 * Check the session id is the same as the one in the
159 if (PACKET_remaining(&sessionid) != chsessidlen
161 && memcmp(chsessid, PACKET_data(&sessionid),
165 } else if (rectype == SSL3_RT_CHANGE_CIPHER_SPEC) {
166 if (bio == s_to_c_fbio) {
168 * Server writing. We shouldn't have written any app data
169 * yet, and we should have seen both the ClientHello and the
179 } else if (!cappdataseen) {
181 * Client writing. We shouldn't have written any app data
182 * yet, and we should have seen the ClientHello
184 if (shseen == 1 && !ccsaftersh)
186 else if (shseen == 0 && !ccsbeforesh)
193 } else if(rectype == SSL3_RT_APPLICATION_DATA) {
194 if (bio == s_to_c_fbio)
199 if (recvers != expectedrecvers)
203 ret = BIO_write(next, in, inl);
204 if (ret <= 0 && BIO_should_write(next))
205 BIO_set_retry_write(bio);
210 static long watchccs_ctrl(BIO *bio, int cmd, long num, void *ptr)
213 BIO *next = BIO_next(bio);
223 ret = BIO_ctrl(next, cmd, num, ptr);
229 static int watchccs_gets(BIO *bio, char *buf, int size)
231 /* We don't support this - not needed anyway */
235 static int watchccs_puts(BIO *bio, const char *str)
237 return watchccs_write(bio, str, strlen(str));
240 static int test_tls13ccs(int tst)
242 SSL_CTX *sctx = NULL, *cctx = NULL;
243 SSL *sssl = NULL, *cssl = NULL;
245 const char msg[] = "Dummy data";
247 size_t written, readbytes;
248 SSL_SESSION *sess = NULL;
250 chseen = shseen = sccsseen = ccsaftersh = ccsbeforesh = 0;
251 sappdataseen = cappdataseen = badccs = badvers = badsessid = 0;
254 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
255 &sctx, &cctx, cert, privkey)))
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
282 SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
288 SSL_CTX_clear_options(sctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
291 TEST_error("Invalid test value");
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)))
300 sess = SSL_get1_session(cssl);
310 if ((tst >= 3 && tst <= 5) || tst >= 9) {
312 if (!TEST_true(SSL_CTX_set1_groups_list(sctx, "P-256")))
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);
325 /* BIOs get freed on error */
326 if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, s_to_c_fbio,
332 if (!TEST_true(SSL_set_session(cssl, sess))
333 || !TEST_true(SSL_write_early_data(cssl, msg, strlen(msg),
336 && !TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf),
338 SSL_READ_EARLY_DATA_SUCCESS)))
341 if (!TEST_int_gt(SSL_connect(cssl), 0))
344 if (!TEST_int_le(SSL_connect(cssl), 0))
347 if (!TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf),
349 SSL_READ_EARLY_DATA_FINISH))
353 /* Perform handshake (or complete it if doing early data ) */
354 if (!TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
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
362 if (!TEST_false(badccs) || !TEST_false(badvers) || !TEST_false(badsessid))
367 if (!TEST_true(sccsseen)
368 || !TEST_true(ccsaftersh)
369 || !TEST_false(ccsbeforesh)
370 || !TEST_size_t_gt(chsessidlen, 0))
375 if (!TEST_true(sccsseen)
376 || !TEST_false(ccsaftersh)
377 || !TEST_false(ccsbeforesh)
378 || !TEST_size_t_eq(chsessidlen, 0))
383 if (!TEST_false(sccsseen)
384 || !TEST_true(ccsaftersh)
385 || !TEST_false(ccsbeforesh)
386 || !TEST_size_t_gt(chsessidlen, 0))
391 if (!TEST_true(sccsseen)
392 || !TEST_true(ccsaftersh)
393 || !TEST_false(ccsbeforesh)
394 || !TEST_size_t_gt(chsessidlen, 0))
399 if (!TEST_true(sccsseen)
400 || !TEST_false(ccsaftersh)
401 || !TEST_false(ccsbeforesh)
402 || !TEST_size_t_eq(chsessidlen, 0))
407 if (!TEST_false(sccsseen)
408 || !TEST_true(ccsaftersh)
409 || !TEST_false(ccsbeforesh)
410 || !TEST_size_t_gt(chsessidlen, 0))
415 if (!TEST_true(sccsseen)
416 || !TEST_false(ccsaftersh)
417 || !TEST_true(ccsbeforesh)
418 || !TEST_size_t_gt(chsessidlen, 0))
423 if (!TEST_true(sccsseen)
424 || !TEST_false(ccsaftersh)
425 || !TEST_false(ccsbeforesh)
426 || !TEST_size_t_eq(chsessidlen, 0))
431 if (!TEST_false(sccsseen)
432 || !TEST_false(ccsaftersh)
433 || !TEST_true(ccsbeforesh)
434 || !TEST_size_t_gt(chsessidlen, 0))
439 if (!TEST_true(sccsseen)
440 || !TEST_false(ccsaftersh)
441 || !TEST_true(ccsbeforesh)
442 || !TEST_size_t_gt(chsessidlen, 0))
447 if (!TEST_true(sccsseen)
448 || !TEST_false(ccsaftersh)
449 || !TEST_false(ccsbeforesh)
450 || !TEST_size_t_eq(chsessidlen, 0))
455 if (!TEST_false(sccsseen)
456 || !TEST_false(ccsaftersh)
457 || !TEST_true(ccsbeforesh)
458 || !TEST_size_t_gt(chsessidlen, 0))
463 TEST_error("Invalid test value");
469 SSL_SESSION_free(sess);
478 int setup_tests(void)
480 if (!TEST_ptr(cert = test_get_argument(0))
481 || !TEST_ptr(privkey = test_get_argument(1)))
484 ADD_ALL_TESTS(test_tls13ccs, 12);
489 void cleanup_tests(void)
491 BIO_meth_free(method_watchccs);