Add support to test_ssl_new for testing with DTLS over SCTP
[openssl.git] / test / ssltestlib.c
1 /*
2  * Copyright 2016 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 <string.h>
11
12 #include "e_os.h"
13 #include "ssltestlib.h"
14
15 static int tls_dump_new(BIO *bi);
16 static int tls_dump_free(BIO *a);
17 static int tls_dump_read(BIO *b, char *out, int outl);
18 static int tls_dump_write(BIO *b, const char *in, int inl);
19 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
20 static int tls_dump_gets(BIO *bp, char *buf, int size);
21 static int tls_dump_puts(BIO *bp, const char *str);
22
23 /* Choose a sufficiently large type likely to be unused for this custom BIO */
24 # define BIO_TYPE_TLS_DUMP_FILTER  (0x80 | BIO_TYPE_FILTER)
25
26 # define BIO_TYPE_MEMPACKET_TEST      0x81
27
28 static BIO_METHOD *method_tls_dump = NULL;
29 static BIO_METHOD *method_mempacket_test = NULL;
30
31 /* Note: Not thread safe! */
32 const BIO_METHOD *bio_f_tls_dump_filter(void)
33 {
34     if (method_tls_dump == NULL) {
35         method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
36                                         "TLS dump filter");
37         if (   method_tls_dump == NULL
38             || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
39             || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
40             || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
41             || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
42             || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
43             || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
44             || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
45             return NULL;
46     }
47     return method_tls_dump;
48 }
49
50 void bio_f_tls_dump_filter_free(void)
51 {
52     BIO_meth_free(method_tls_dump);
53 }
54
55 static int tls_dump_new(BIO *bio)
56 {
57     BIO_set_init(bio, 1);
58     return 1;
59 }
60
61 static int tls_dump_free(BIO *bio)
62 {
63     BIO_set_init(bio, 0);
64
65     return 1;
66 }
67
68 static void copy_flags(BIO *bio)
69 {
70     int flags;
71     BIO *next = BIO_next(bio);
72
73     flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
74     BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
75     BIO_set_flags(bio, flags);
76 }
77
78 #define RECORD_CONTENT_TYPE     0
79 #define RECORD_VERSION_HI       1
80 #define RECORD_VERSION_LO       2
81 #define RECORD_EPOCH_HI         3
82 #define RECORD_EPOCH_LO         4
83 #define RECORD_SEQUENCE_START   5
84 #define RECORD_SEQUENCE_END     10
85 #define RECORD_LEN_HI           11
86 #define RECORD_LEN_LO           12
87
88 #define MSG_TYPE                0
89 #define MSG_LEN_HI              1
90 #define MSG_LEN_MID             2
91 #define MSG_LEN_LO              3
92 #define MSG_SEQ_HI              4
93 #define MSG_SEQ_LO              5
94 #define MSG_FRAG_OFF_HI         6
95 #define MSG_FRAG_OFF_MID        7
96 #define MSG_FRAG_OFF_LO         8
97 #define MSG_FRAG_LEN_HI         9
98 #define MSG_FRAG_LEN_MID        10
99 #define MSG_FRAG_LEN_LO         11
100
101
102 static void dump_data(const char *data, int len)
103 {
104     int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
105     unsigned char *rec;
106
107     printf("---- START OF PACKET ----\n");
108
109     rem = len;
110     rec = (unsigned char *)data;
111
112     while (rem > 0) {
113         if (rem != len)
114             printf("*\n");
115         printf("*---- START OF RECORD ----\n");
116         if (rem < DTLS1_RT_HEADER_LENGTH) {
117             printf("*---- RECORD TRUNCATED ----\n");
118             break;
119         }
120         content = rec[RECORD_CONTENT_TYPE];
121         printf("** Record Content-type: %d\n", content);
122         printf("** Record Version: %02x%02x\n",
123                rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
124         epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
125         printf("** Record Epoch: %d\n", epoch);
126         printf("** Record Sequence: ");
127         for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
128             printf("%02x", rec[i]);
129         reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
130         printf("\n** Record Length: %d\n", reclen);
131
132         /* Now look at message */
133         rec += DTLS1_RT_HEADER_LENGTH;
134         rem -= DTLS1_RT_HEADER_LENGTH;
135         if (content == SSL3_RT_HANDSHAKE) {
136             printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
137             if (epoch > 0) {
138                 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
139             } else if (rem < DTLS1_HM_HEADER_LENGTH
140                     || reclen < DTLS1_HM_HEADER_LENGTH) {
141                 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
142             } else {
143                 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
144                 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
145                          | rec[MSG_LEN_LO];
146                 printf("*** Message Length: %d\n", msglen);
147                 printf("*** Message sequence: %d\n",
148                        (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
149                 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
150                           | (rec[MSG_FRAG_OFF_MID] << 8)
151                           | rec[MSG_FRAG_OFF_LO];
152                 printf("*** Message Fragment offset: %d\n", fragoff);
153                 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
154                           | (rec[MSG_FRAG_LEN_MID] << 8)
155                           | rec[MSG_FRAG_LEN_LO];
156                 printf("*** Message Fragment len: %d\n", fraglen);
157                 if (fragoff + fraglen > msglen)
158                     printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
159                 else if (reclen < fraglen)
160                     printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
161                 else
162                     printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
163             }
164         }
165         if (rem < reclen) {
166             printf("*---- RECORD TRUNCATED ----\n");
167             rem = 0;
168         } else {
169             rec += reclen;
170             rem -= reclen;
171             printf("*---- END OF RECORD ----\n");
172         }
173     }
174     printf("---- END OF PACKET ----\n\n");
175     fflush(stdout);
176 }
177
178 static int tls_dump_read(BIO *bio, char *out, int outl)
179 {
180     int ret;
181     BIO *next = BIO_next(bio);
182
183     ret = BIO_read(next, out, outl);
184     copy_flags(bio);
185
186     if (ret > 0) {
187         dump_data(out, ret);
188     }
189
190     return ret;
191 }
192
193 static int tls_dump_write(BIO *bio, const char *in, int inl)
194 {
195     int ret;
196     BIO *next = BIO_next(bio);
197
198     ret = BIO_write(next, in, inl);
199     copy_flags(bio);
200
201     return ret;
202 }
203
204 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
205 {
206     long ret;
207     BIO *next = BIO_next(bio);
208
209     if (next == NULL)
210         return 0;
211
212     switch (cmd) {
213     case BIO_CTRL_DUP:
214         ret = 0L;
215         break;
216     default:
217         ret = BIO_ctrl(next, cmd, num, ptr);
218         break;
219     }
220     return ret;
221 }
222
223 static int tls_dump_gets(BIO *bio, char *buf, int size)
224 {
225     /* We don't support this - not needed anyway */
226     return -1;
227 }
228
229 static int tls_dump_puts(BIO *bio, const char *str)
230 {
231     return tls_dump_write(bio, str, strlen(str));
232 }
233
234
235 struct mempacket_st {
236     unsigned char *data;
237     int len;
238     unsigned int num;
239     unsigned int type;
240 };
241
242 static void mempacket_free(MEMPACKET *pkt)
243 {
244     if (pkt->data != NULL)
245         OPENSSL_free(pkt->data);
246     OPENSSL_free(pkt);
247 }
248
249 typedef struct mempacket_test_ctx_st {
250     STACK_OF(MEMPACKET) *pkts;
251     unsigned int epoch;
252     unsigned int currrec;
253     unsigned int currpkt;
254     unsigned int lastpkt;
255     unsigned int noinject;
256 } MEMPACKET_TEST_CTX;
257
258 static int mempacket_test_new(BIO *bi);
259 static int mempacket_test_free(BIO *a);
260 static int mempacket_test_read(BIO *b, char *out, int outl);
261 static int mempacket_test_write(BIO *b, const char *in, int inl);
262 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
263 static int mempacket_test_gets(BIO *bp, char *buf, int size);
264 static int mempacket_test_puts(BIO *bp, const char *str);
265
266 const BIO_METHOD *bio_s_mempacket_test(void)
267 {
268     if (method_mempacket_test == NULL) {
269         method_mempacket_test = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
270                                              "Mem Packet Test");
271         if (   method_mempacket_test == NULL
272             || !BIO_meth_set_write(method_mempacket_test, mempacket_test_write)
273             || !BIO_meth_set_read(method_mempacket_test, mempacket_test_read)
274             || !BIO_meth_set_puts(method_mempacket_test, mempacket_test_puts)
275             || !BIO_meth_set_gets(method_mempacket_test, mempacket_test_gets)
276             || !BIO_meth_set_ctrl(method_mempacket_test, mempacket_test_ctrl)
277             || !BIO_meth_set_create(method_mempacket_test, mempacket_test_new)
278             || !BIO_meth_set_destroy(method_mempacket_test, mempacket_test_free))
279             return NULL;
280     }
281     return method_mempacket_test;
282 }
283
284 void bio_s_mempacket_test_free(void)
285 {
286     BIO_meth_free(method_mempacket_test);
287 }
288
289 static int mempacket_test_new(BIO *bio)
290 {
291     MEMPACKET_TEST_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
292     if (ctx == NULL)
293         return 0;
294     ctx->pkts = sk_MEMPACKET_new_null();
295     if (ctx->pkts == NULL) {
296         OPENSSL_free(ctx);
297         return 0;
298     }
299     BIO_set_init(bio, 1);
300     BIO_set_data(bio, ctx);
301     return 1;
302 }
303
304 static int mempacket_test_free(BIO *bio)
305 {
306     MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
307
308     sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
309     OPENSSL_free(ctx);
310     BIO_set_data(bio, NULL);
311     BIO_set_init(bio, 0);
312
313     return 1;
314 }
315
316 /* Record Header values */
317 #define EPOCH_HI        4
318 #define EPOCH_LO        5
319 #define RECORD_SEQUENCE 10
320 #define RECORD_LEN_HI   11
321 #define RECORD_LEN_LO   12
322
323 #define STANDARD_PACKET                 0
324
325 static int mempacket_test_read(BIO *bio, char *out, int outl)
326 {
327     MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
328     MEMPACKET *thispkt;
329     unsigned char *rec;
330     int rem;
331     unsigned int seq, offset, len, epoch;
332
333     BIO_clear_retry_flags(bio);
334
335     thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
336     if (thispkt == NULL || thispkt->num != ctx->currpkt) {
337         /* Probably run out of data */
338         BIO_set_retry_read(bio);
339         return -1;
340     }
341     (void)sk_MEMPACKET_shift(ctx->pkts);
342     ctx->currpkt++;
343
344     if (outl > thispkt->len)
345         outl = thispkt->len;
346
347     if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ) {
348         /*
349          * Overwrite the record sequence number. We strictly number them in
350          * the order received. Since we are actually a reliable transport
351          * we know that there won't be any re-ordering. We overwrite to deal
352          * with any packets that have been injected
353          */
354         rem = thispkt->len;
355         rec = thispkt->data;
356         while (rem > 0) {
357             if (rem < DTLS1_RT_HEADER_LENGTH) {
358                 return -1;
359             }
360             epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
361             if (epoch != ctx->epoch) {
362                 ctx->epoch = epoch;
363                 ctx->currrec = 0;
364             }
365             seq = ctx->currrec;
366             offset = 0;
367             do {
368                 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
369                 seq >>= 8;
370                 offset++;
371             } while (seq > 0);
372             ctx->currrec++;
373
374             len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
375                   + DTLS1_RT_HEADER_LENGTH;
376
377             rec += len;
378             rem -= len;
379         }
380     }
381
382     memcpy(out, thispkt->data, outl);
383
384     mempacket_free(thispkt);
385
386     return outl;
387 }
388
389 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
390                           int type)
391 {
392     MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
393     MEMPACKET *thispkt, *looppkt, *nextpkt;
394     int i;
395
396     if (ctx == NULL)
397         return -1;
398
399     /* We only allow injection before we've started writing any data */
400     if (pktnum >= 0) {
401         if (ctx->noinject)
402             return -1;
403     } else {
404         ctx->noinject = 1;
405     }
406
407     thispkt = OPENSSL_malloc(sizeof(MEMPACKET));
408     if (thispkt == NULL)
409         return -1;
410
411     thispkt->data = OPENSSL_malloc(inl);
412     if (thispkt->data == NULL) {
413         mempacket_free(thispkt);
414         return -1;
415     }
416
417     memcpy(thispkt->data, in, inl);
418     thispkt->len = inl;
419     thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
420     thispkt->type = type;
421
422     for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
423         /* Check if we found the right place to insert this packet */
424         if (looppkt->num > thispkt->num) {
425             if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
426                 mempacket_free(thispkt);
427                 return -1;
428             }
429             /* If we're doing up front injection then we're done */
430             if (pktnum >= 0)
431                 return inl;
432             /*
433              * We need to do some accounting on lastpkt. We increment it first,
434              * but it might now equal the value of injected packets, so we need
435              * to skip over those
436              */
437             ctx->lastpkt++;
438             do {
439                 i++;
440                 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
441                 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
442                     ctx->lastpkt++;
443                 else
444                     return inl;
445             } while(1);
446         } else if (looppkt->num == thispkt->num) {
447             if (!ctx->noinject) {
448                 /* We injected two packets with the same packet number! */
449                 return -1;
450             }
451             ctx->lastpkt++;
452             thispkt->num++;
453         }
454     }
455     /*
456      * We didn't find any packets with a packet number equal to or greater than
457      * this one, so we just add it onto the end
458      */
459     if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
460         mempacket_free(thispkt);
461         return -1;
462     }
463
464     if (pktnum < 0)
465         ctx->lastpkt++;
466
467     return inl;
468 }
469
470 static int mempacket_test_write(BIO *bio, const char *in, int inl)
471 {
472     return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
473 }
474
475 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
476 {
477     long ret = 1;
478     MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
479     MEMPACKET *thispkt;
480
481     switch (cmd) {
482     case BIO_CTRL_EOF:
483         ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
484         break;
485     case BIO_CTRL_GET_CLOSE:
486         ret = BIO_get_shutdown(bio);
487         break;
488     case BIO_CTRL_SET_CLOSE:
489         BIO_set_shutdown(bio, (int)num);
490         break;
491     case BIO_CTRL_WPENDING:
492         ret = 0L;
493         break;
494     case BIO_CTRL_PENDING:
495         thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
496         if (thispkt == NULL)
497             ret = 0;
498         else
499             ret = thispkt->len;
500         break;
501     case BIO_CTRL_FLUSH:
502         ret = 1;
503         break;
504     case BIO_CTRL_RESET:
505     case BIO_CTRL_DUP:
506     case BIO_CTRL_PUSH:
507     case BIO_CTRL_POP:
508     default:
509         ret = 0;
510         break;
511     }
512     return ret;
513 }
514
515 static int mempacket_test_gets(BIO *bio, char *buf, int size)
516 {
517     /* We don't support this - not needed anyway */
518     return -1;
519 }
520
521 static int mempacket_test_puts(BIO *bio, const char *str)
522 {
523     return mempacket_test_write(bio, str, strlen(str));
524 }
525
526 int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
527                         SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
528                         char *privkeyfile)
529 {
530     SSL_CTX *serverctx = NULL;
531     SSL_CTX *clientctx = NULL;
532
533     serverctx = SSL_CTX_new(sm);
534     clientctx = SSL_CTX_new(cm);
535     if (serverctx == NULL || clientctx == NULL) {
536         printf("Failed to create SSL_CTX\n");
537         goto err;
538     }
539
540     if (SSL_CTX_use_certificate_file(serverctx, certfile,
541                                      SSL_FILETYPE_PEM) <= 0) {
542         printf("Failed to load server certificate\n");
543         goto err;
544     }
545     if (SSL_CTX_use_PrivateKey_file(serverctx, privkeyfile,
546                                     SSL_FILETYPE_PEM) <= 0) {
547         printf("Failed to load server private key\n");
548     }
549     if (SSL_CTX_check_private_key(serverctx) <= 0) {
550         printf("Failed to check private key\n");
551         goto err;
552     }
553
554 #ifndef OPENSSL_NO_DH
555     SSL_CTX_set_dh_auto(serverctx, 1);
556 #endif
557
558     *sctx = serverctx;
559     *cctx = clientctx;
560
561     return 1;
562  err:
563     SSL_CTX_free(serverctx);
564     SSL_CTX_free(clientctx);
565     return 0;
566 }
567
568 #define MAXLOOPS    1000000
569
570 /*
571  * NOTE: Transfers control of the BIOs - this function will free them on error
572  */
573 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
574                           SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
575 {
576     SSL *serverssl, *clientssl;
577     BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
578
579     if (*sssl == NULL)
580         serverssl = SSL_new(serverctx);
581     else
582         serverssl = *sssl;
583     if (*cssl == NULL)
584         clientssl = SSL_new(clientctx);
585     else
586         clientssl = *cssl;
587
588     if (serverssl == NULL || clientssl == NULL) {
589         printf("Failed to create SSL object\n");
590         goto error;
591     }
592
593     if (SSL_is_dtls(clientssl)) {
594         s_to_c_bio = BIO_new(bio_s_mempacket_test());
595         c_to_s_bio = BIO_new(bio_s_mempacket_test());
596     } else {
597         s_to_c_bio = BIO_new(BIO_s_mem());
598         c_to_s_bio = BIO_new(BIO_s_mem());
599     }
600     if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
601         printf("Failed to create mem BIOs\n");
602         goto error;
603     }
604
605     if (s_to_c_fbio != NULL)
606         s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio);
607     if (c_to_s_fbio != NULL)
608         c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio);
609     if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
610         printf("Failed to create chained BIOs\n");
611         goto error;
612     }
613
614     /* Set Non-blocking IO behaviour */
615     BIO_set_mem_eof_return(s_to_c_bio, -1);
616     BIO_set_mem_eof_return(c_to_s_bio, -1);
617
618     /* Up ref these as we are passing them to two SSL objects */
619     BIO_up_ref(s_to_c_bio);
620     BIO_up_ref(c_to_s_bio);
621
622     SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
623     SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
624
625     /* BIOs will now be freed when SSL objects are freed */
626     s_to_c_bio = c_to_s_bio = NULL;
627     s_to_c_fbio = c_to_s_fbio = NULL;
628
629     *sssl = serverssl;
630     *cssl = clientssl;
631
632     return 1;
633
634  error:
635     SSL_free(serverssl);
636     SSL_free(clientssl);
637     BIO_free(s_to_c_bio);
638     BIO_free(c_to_s_bio);
639     BIO_free(s_to_c_fbio);
640     BIO_free(c_to_s_fbio);
641
642     return 0;
643 }
644
645 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
646 {
647     int retc = -1, rets = -1, err, abortctr = 0;
648     int clienterr = 0, servererr = 0;
649     unsigned char buf;
650     size_t readbytes;
651
652     do {
653         err = SSL_ERROR_WANT_WRITE;
654         while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
655             retc = SSL_connect(clientssl);
656             if (retc <= 0)
657                 err = SSL_get_error(clientssl, retc);
658         }
659
660         if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
661             printf("SSL_connect() failed %d, %d\n", retc, err);
662             clienterr = 1;
663         }
664         if (want != SSL_ERROR_NONE && err == want)
665             return 0;
666
667         err = SSL_ERROR_WANT_WRITE;
668         while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
669             rets = SSL_accept(serverssl);
670             if (rets <= 0)
671                 err = SSL_get_error(serverssl, rets);
672         }
673
674         if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {
675             printf("SSL_accept() failed %d, %d\n", rets, err);
676             servererr = 1;
677         }
678         if (want != SSL_ERROR_NONE && err == want)
679             return 0;
680         if (clienterr && servererr)
681             return 0;
682         if (++abortctr == MAXLOOPS) {
683             printf("No progress made\n");
684             return 0;
685         }
686     } while (retc <=0 || rets <= 0);
687
688     /*
689      * We attempt to read some data on the client side which we expect to fail.
690      * This will ensure we have received the NewSessionTicket in TLSv1.3 where
691      * appropriate.
692      */
693     if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
694         if (readbytes != 0) {
695             printf("Unexpected success reading data %"OSSLzu"\n", readbytes);
696             return 0;
697         }
698     } else if (SSL_get_error(clientssl, 0) != SSL_ERROR_WANT_READ) {
699         printf("SSL_read_ex() failed\n");
700         return 0;
701     }
702
703     return 1;
704 }