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