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