2 * Copyright 2017-2018 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 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;
24 static unsigned char chsessid[SSL_MAX_SSL_SESSION_ID_LENGTH];
25 static size_t chsessidlen = 0;
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);
35 /* Choose a sufficiently large type likely to be unused for this custom BIO */
36 # define BIO_TYPE_WATCHCCS_FILTER (0x80 | BIO_TYPE_FILTER)
38 static BIO_METHOD *method_watchccs = NULL;
40 static const BIO_METHOD *bio_f_watchccs_filter()
42 if (method_watchccs == NULL) {
43 method_watchccs = BIO_meth_new(BIO_TYPE_WATCHCCS_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))
55 return method_watchccs;
58 static int watchccs_new(BIO *bio)
64 static int watchccs_free(BIO *bio)
70 static int watchccs_read(BIO *bio, char *out, int outl)
73 BIO *next = BIO_next(bio);
80 BIO_clear_retry_flags(bio);
82 ret = BIO_read(next, out, outl);
83 if (ret <= 0 && BIO_should_read(next))
84 BIO_set_retry_read(bio);
89 static int watchccs_write(BIO *bio, const char *in, int inl)
92 BIO *next = BIO_next(bio);
93 PACKET pkt, msg, msgbody, sessionid;
94 unsigned int rectype, recvers, msgtype, expectedrecvers;
101 BIO_clear_retry_flags(bio);
103 if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
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))
113 expectedrecvers = TLS1_2_VERSION;
115 if (rectype == SSL3_RT_HANDSHAKE) {
116 if (!PACKET_get_1(&msg, &msgtype)
117 || !PACKET_get_length_prefixed_3(&msg, &msgbody))
119 if (msgtype == SSL3_MT_CLIENT_HELLO) {
123 * Skip legacy_version (2 bytes) and Random (32 bytes) to read
126 if (!PACKET_forward(&msgbody, 34)
127 || !PACKET_get_length_prefixed_1(&msgbody, &sessionid))
131 expectedrecvers = TLS1_VERSION;
133 /* Save the session id for later */
134 chsessidlen = PACKET_remaining(&sessionid);
135 if (!PACKET_copy_bytes(&sessionid, chsessid, chsessidlen))
139 * Check the session id for the second ClientHello is the
140 * same as the first one.
142 if (PACKET_remaining(&sessionid) != chsessidlen
144 && memcmp(chsessid, PACKET_data(&sessionid),
148 } else if (msgtype == SSL3_MT_SERVER_HELLO) {
151 * Skip legacy_version (2 bytes) and Random (32 bytes) to read
154 if (!PACKET_forward(&msgbody, 34)
155 || !PACKET_get_length_prefixed_1(&msgbody, &sessionid))
159 * Check the session id is the same as the one in the
162 if (PACKET_remaining(&sessionid) != chsessidlen
164 && memcmp(chsessid, PACKET_data(&sessionid),
168 } else if (rectype == SSL3_RT_CHANGE_CIPHER_SPEC) {
169 if (bio == s_to_c_fbio) {
171 * Server writing. We shouldn't have written any app data
172 * yet, and we should have seen both the ClientHello and the
182 } else if (!cappdataseen) {
184 * Client writing. We shouldn't have written any app data
185 * yet, and we should have seen the ClientHello
187 if (shseen == 1 && !ccsaftersh)
189 else if (shseen == 0 && !ccsbeforesh)
196 } else if(rectype == SSL3_RT_APPLICATION_DATA) {
197 if (bio == s_to_c_fbio)
202 if (recvers != expectedrecvers)
206 ret = BIO_write(next, in, inl);
207 if (ret <= 0 && BIO_should_write(next))
208 BIO_set_retry_write(bio);
213 static long watchccs_ctrl(BIO *bio, int cmd, long num, void *ptr)
216 BIO *next = BIO_next(bio);
226 ret = BIO_ctrl(next, cmd, num, ptr);
232 static int watchccs_gets(BIO *bio, char *buf, int size)
234 /* We don't support this - not needed anyway */
238 static int watchccs_puts(BIO *bio, const char *str)
240 return watchccs_write(bio, str, strlen(str));
243 static int test_tls13ccs(int tst)
245 SSL_CTX *sctx = NULL, *cctx = NULL;
246 SSL *sssl = NULL, *cssl = NULL;
248 const char msg[] = "Dummy data";
250 size_t written, readbytes;
251 SSL_SESSION *sess = NULL;
253 chseen = shseen = sccsseen = ccsaftersh = ccsbeforesh = 0;
254 sappdataseen = cappdataseen = badccs = badvers = badsessid = 0;
257 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
258 TLS1_VERSION, TLS_MAX_VERSION,
259 &sctx, &cctx, cert, privkey))
260 || !TEST_true(SSL_CTX_set_max_early_data(sctx,
261 SSL3_RT_MAX_PLAIN_LENGTH))
262 || !TEST_true(SSL_CTX_set_max_early_data(cctx,
263 SSL3_RT_MAX_PLAIN_LENGTH)))
267 * Test 0: Simple Handshake
268 * Test 1: Simple Handshake, client middlebox compat mode disabled
269 * Test 2: Simple Handshake, server middlebox compat mode disabled
270 * Test 3: HRR Handshake
271 * Test 4: HRR Handshake, client middlebox compat mode disabled
272 * Test 5: HRR Handshake, server middlebox compat mode disabled
273 * Test 6: Early data handshake
274 * Test 7: Early data handshake, client middlebox compat mode disabled
275 * Test 8: Early data handshake, server middlebox compat mode disabled
276 * Test 9: Early data then HRR
277 * Test 10: Early data then HRR, client middlebox compat mode disabled
278 * Test 11: Early data then HRR, server middlebox compat mode disabled
290 SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
296 SSL_CTX_clear_options(sctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
299 TEST_error("Invalid test value");
304 /* Get a session suitable for early_data */
305 if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL))
306 || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
308 sess = SSL_get1_session(cssl);
318 if ((tst >= 3 && tst <= 5) || tst >= 9) {
320 if (!TEST_true(SSL_CTX_set1_groups_list(sctx, "P-256")))
324 s_to_c_fbio = BIO_new(bio_f_watchccs_filter());
325 c_to_s_fbio = BIO_new(bio_f_watchccs_filter());
326 if (!TEST_ptr(s_to_c_fbio)
327 || !TEST_ptr(c_to_s_fbio)) {
328 BIO_free(s_to_c_fbio);
329 BIO_free(c_to_s_fbio);
333 /* BIOs get freed on error */
334 if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, s_to_c_fbio,
340 if (!TEST_true(SSL_set_session(cssl, sess))
341 || !TEST_true(SSL_write_early_data(cssl, msg, strlen(msg),
344 && !TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf),
346 SSL_READ_EARLY_DATA_SUCCESS)))
349 if (!TEST_int_gt(SSL_connect(cssl), 0))
352 if (!TEST_int_le(SSL_connect(cssl), 0))
355 if (!TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf),
357 SSL_READ_EARLY_DATA_FINISH))
361 /* Perform handshake (or complete it if doing early data ) */
362 if (!TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
366 * Check there were no unexpected CCS messages, all record versions
367 * were as expected, and that the session ids were reflected by the server
370 if (!TEST_false(badccs) || !TEST_false(badvers) || !TEST_false(badsessid))
375 if (!TEST_true(sccsseen)
376 || !TEST_true(ccsaftersh)
377 || !TEST_false(ccsbeforesh)
378 || !TEST_size_t_gt(chsessidlen, 0))
383 if (!TEST_true(sccsseen)
384 || !TEST_false(ccsaftersh)
385 || !TEST_false(ccsbeforesh)
386 || !TEST_size_t_eq(chsessidlen, 0))
391 if (!TEST_false(sccsseen)
392 || !TEST_true(ccsaftersh)
393 || !TEST_false(ccsbeforesh)
394 || !TEST_size_t_gt(chsessidlen, 0))
399 if (!TEST_true(sccsseen)
400 || !TEST_true(ccsaftersh)
401 || !TEST_false(ccsbeforesh)
402 || !TEST_size_t_gt(chsessidlen, 0))
407 if (!TEST_true(sccsseen)
408 || !TEST_false(ccsaftersh)
409 || !TEST_false(ccsbeforesh)
410 || !TEST_size_t_eq(chsessidlen, 0))
415 if (!TEST_false(sccsseen)
416 || !TEST_true(ccsaftersh)
417 || !TEST_false(ccsbeforesh)
418 || !TEST_size_t_gt(chsessidlen, 0))
423 if (!TEST_true(sccsseen)
424 || !TEST_false(ccsaftersh)
425 || !TEST_true(ccsbeforesh)
426 || !TEST_size_t_gt(chsessidlen, 0))
431 if (!TEST_true(sccsseen)
432 || !TEST_false(ccsaftersh)
433 || !TEST_false(ccsbeforesh)
434 || !TEST_size_t_eq(chsessidlen, 0))
439 if (!TEST_false(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_true(ccsbeforesh)
450 || !TEST_size_t_gt(chsessidlen, 0))
455 if (!TEST_true(sccsseen)
456 || !TEST_false(ccsaftersh)
457 || !TEST_false(ccsbeforesh)
458 || !TEST_size_t_eq(chsessidlen, 0))
463 if (!TEST_false(sccsseen)
464 || !TEST_false(ccsaftersh)
465 || !TEST_true(ccsbeforesh)
466 || !TEST_size_t_gt(chsessidlen, 0))
471 TEST_error("Invalid test value");
477 SSL_SESSION_free(sess);
486 int setup_tests(void)
488 if (!TEST_ptr(cert = test_get_argument(0))
489 || !TEST_ptr(privkey = test_get_argument(1)))
492 ADD_ALL_TESTS(test_tls13ccs, 12);
497 void cleanup_tests(void)
499 BIO_meth_free(method_watchccs);