xref: /freebsd/crypto/openssl/test/asynciotest.c (revision e0c4386e)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License");
5*e0c4386eSCy Schubert  * you may not use this file except in compliance with the License.
6*e0c4386eSCy Schubert  * You may obtain a copy of the License at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  * or in the file LICENSE in the source distribution.
9*e0c4386eSCy Schubert  */
10*e0c4386eSCy Schubert 
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include <openssl/ssl.h>
13*e0c4386eSCy Schubert #include <openssl/bio.h>
14*e0c4386eSCy Schubert #include <openssl/err.h>
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert #include "internal/packet.h"
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert #include "helpers/ssltestlib.h"
19*e0c4386eSCy Schubert #include "testutil.h"
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert /* Should we fragment records or not? 0 = no, !0 = yes*/
22*e0c4386eSCy Schubert static int fragment = 0;
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert static char *cert = NULL;
25*e0c4386eSCy Schubert static char *privkey = NULL;
26*e0c4386eSCy Schubert 
27*e0c4386eSCy Schubert static int async_new(BIO *bi);
28*e0c4386eSCy Schubert static int async_free(BIO *a);
29*e0c4386eSCy Schubert static int async_read(BIO *b, char *out, int outl);
30*e0c4386eSCy Schubert static int async_write(BIO *b, const char *in, int inl);
31*e0c4386eSCy Schubert static long async_ctrl(BIO *b, int cmd, long num, void *ptr);
32*e0c4386eSCy Schubert static int async_gets(BIO *bp, char *buf, int size);
33*e0c4386eSCy Schubert static int async_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_ASYNC_FILTER  (0x80 | BIO_TYPE_FILTER)
37*e0c4386eSCy Schubert 
38*e0c4386eSCy Schubert static BIO_METHOD *methods_async = NULL;
39*e0c4386eSCy Schubert 
40*e0c4386eSCy Schubert struct async_ctrs {
41*e0c4386eSCy Schubert     unsigned int rctr;
42*e0c4386eSCy Schubert     unsigned int wctr;
43*e0c4386eSCy Schubert };
44*e0c4386eSCy Schubert 
bio_f_async_filter(void)45*e0c4386eSCy Schubert static const BIO_METHOD *bio_f_async_filter(void)
46*e0c4386eSCy Schubert {
47*e0c4386eSCy Schubert     if (methods_async == NULL) {
48*e0c4386eSCy Schubert         methods_async = BIO_meth_new(BIO_TYPE_ASYNC_FILTER, "Async filter");
49*e0c4386eSCy Schubert         if (   methods_async == NULL
50*e0c4386eSCy Schubert             || !BIO_meth_set_write(methods_async, async_write)
51*e0c4386eSCy Schubert             || !BIO_meth_set_read(methods_async, async_read)
52*e0c4386eSCy Schubert             || !BIO_meth_set_puts(methods_async, async_puts)
53*e0c4386eSCy Schubert             || !BIO_meth_set_gets(methods_async, async_gets)
54*e0c4386eSCy Schubert             || !BIO_meth_set_ctrl(methods_async, async_ctrl)
55*e0c4386eSCy Schubert             || !BIO_meth_set_create(methods_async, async_new)
56*e0c4386eSCy Schubert             || !BIO_meth_set_destroy(methods_async, async_free))
57*e0c4386eSCy Schubert             return NULL;
58*e0c4386eSCy Schubert     }
59*e0c4386eSCy Schubert     return methods_async;
60*e0c4386eSCy Schubert }
61*e0c4386eSCy Schubert 
async_new(BIO * bio)62*e0c4386eSCy Schubert static int async_new(BIO *bio)
63*e0c4386eSCy Schubert {
64*e0c4386eSCy Schubert     struct async_ctrs *ctrs;
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert     ctrs = OPENSSL_zalloc(sizeof(struct async_ctrs));
67*e0c4386eSCy Schubert     if (ctrs == NULL)
68*e0c4386eSCy Schubert         return 0;
69*e0c4386eSCy Schubert 
70*e0c4386eSCy Schubert     BIO_set_data(bio, ctrs);
71*e0c4386eSCy Schubert     BIO_set_init(bio, 1);
72*e0c4386eSCy Schubert     return 1;
73*e0c4386eSCy Schubert }
74*e0c4386eSCy Schubert 
async_free(BIO * bio)75*e0c4386eSCy Schubert static int async_free(BIO *bio)
76*e0c4386eSCy Schubert {
77*e0c4386eSCy Schubert     struct async_ctrs *ctrs;
78*e0c4386eSCy Schubert 
79*e0c4386eSCy Schubert     if (bio == NULL)
80*e0c4386eSCy Schubert         return 0;
81*e0c4386eSCy Schubert     ctrs = BIO_get_data(bio);
82*e0c4386eSCy Schubert     OPENSSL_free(ctrs);
83*e0c4386eSCy Schubert     BIO_set_data(bio, NULL);
84*e0c4386eSCy Schubert     BIO_set_init(bio, 0);
85*e0c4386eSCy Schubert 
86*e0c4386eSCy Schubert     return 1;
87*e0c4386eSCy Schubert }
88*e0c4386eSCy Schubert 
async_read(BIO * bio,char * out,int outl)89*e0c4386eSCy Schubert static int async_read(BIO *bio, char *out, int outl)
90*e0c4386eSCy Schubert {
91*e0c4386eSCy Schubert     struct async_ctrs *ctrs;
92*e0c4386eSCy Schubert     int ret = 0;
93*e0c4386eSCy Schubert     BIO *next = BIO_next(bio);
94*e0c4386eSCy Schubert 
95*e0c4386eSCy Schubert     if (outl <= 0)
96*e0c4386eSCy Schubert         return 0;
97*e0c4386eSCy Schubert     if (next == NULL)
98*e0c4386eSCy Schubert         return 0;
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert     ctrs = BIO_get_data(bio);
101*e0c4386eSCy Schubert 
102*e0c4386eSCy Schubert     BIO_clear_retry_flags(bio);
103*e0c4386eSCy Schubert 
104*e0c4386eSCy Schubert     if (ctrs->rctr > 0) {
105*e0c4386eSCy Schubert         ret = BIO_read(next, out, 1);
106*e0c4386eSCy Schubert         if (ret <= 0 && BIO_should_read(next))
107*e0c4386eSCy Schubert             BIO_set_retry_read(bio);
108*e0c4386eSCy Schubert         ctrs->rctr = 0;
109*e0c4386eSCy Schubert     } else {
110*e0c4386eSCy Schubert         ctrs->rctr++;
111*e0c4386eSCy Schubert         BIO_set_retry_read(bio);
112*e0c4386eSCy Schubert     }
113*e0c4386eSCy Schubert 
114*e0c4386eSCy Schubert     return ret;
115*e0c4386eSCy Schubert }
116*e0c4386eSCy Schubert 
117*e0c4386eSCy Schubert #define MIN_RECORD_LEN  6
118*e0c4386eSCy Schubert 
119*e0c4386eSCy Schubert #define CONTENTTYPEPOS  0
120*e0c4386eSCy Schubert #define VERSIONHIPOS    1
121*e0c4386eSCy Schubert #define VERSIONLOPOS    2
122*e0c4386eSCy Schubert #define DATAPOS         5
123*e0c4386eSCy Schubert 
async_write(BIO * bio,const char * in,int inl)124*e0c4386eSCy Schubert static int async_write(BIO *bio, const char *in, int inl)
125*e0c4386eSCy Schubert {
126*e0c4386eSCy Schubert     struct async_ctrs *ctrs;
127*e0c4386eSCy Schubert     int ret = 0;
128*e0c4386eSCy Schubert     size_t written = 0;
129*e0c4386eSCy Schubert     BIO *next = BIO_next(bio);
130*e0c4386eSCy Schubert 
131*e0c4386eSCy Schubert     if (inl <= 0)
132*e0c4386eSCy Schubert         return 0;
133*e0c4386eSCy Schubert     if (next == NULL)
134*e0c4386eSCy Schubert         return 0;
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert     ctrs = BIO_get_data(bio);
137*e0c4386eSCy Schubert 
138*e0c4386eSCy Schubert     BIO_clear_retry_flags(bio);
139*e0c4386eSCy Schubert 
140*e0c4386eSCy Schubert     if (ctrs->wctr > 0) {
141*e0c4386eSCy Schubert         ctrs->wctr = 0;
142*e0c4386eSCy Schubert         if (fragment) {
143*e0c4386eSCy Schubert             PACKET pkt;
144*e0c4386eSCy Schubert 
145*e0c4386eSCy Schubert             if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
146*e0c4386eSCy Schubert                 return -1;
147*e0c4386eSCy Schubert 
148*e0c4386eSCy Schubert             while (PACKET_remaining(&pkt) > 0) {
149*e0c4386eSCy Schubert                 PACKET payload, wholebody, sessionid, extensions;
150*e0c4386eSCy Schubert                 unsigned int contenttype, versionhi, versionlo, data;
151*e0c4386eSCy Schubert                 unsigned int msgtype = 0, negversion = 0;
152*e0c4386eSCy Schubert 
153*e0c4386eSCy Schubert                 if (!PACKET_get_1(&pkt, &contenttype)
154*e0c4386eSCy Schubert                         || !PACKET_get_1(&pkt, &versionhi)
155*e0c4386eSCy Schubert                         || !PACKET_get_1(&pkt, &versionlo)
156*e0c4386eSCy Schubert                         || !PACKET_get_length_prefixed_2(&pkt, &payload))
157*e0c4386eSCy Schubert                     return -1;
158*e0c4386eSCy Schubert 
159*e0c4386eSCy Schubert                 /* Pretend we wrote out the record header */
160*e0c4386eSCy Schubert                 written += SSL3_RT_HEADER_LENGTH;
161*e0c4386eSCy Schubert 
162*e0c4386eSCy Schubert                 wholebody = payload;
163*e0c4386eSCy Schubert                 if (contenttype == SSL3_RT_HANDSHAKE
164*e0c4386eSCy Schubert                         && !PACKET_get_1(&wholebody, &msgtype))
165*e0c4386eSCy Schubert                     return -1;
166*e0c4386eSCy Schubert 
167*e0c4386eSCy Schubert                 if (msgtype == SSL3_MT_SERVER_HELLO) {
168*e0c4386eSCy Schubert                     if (!PACKET_forward(&wholebody,
169*e0c4386eSCy Schubert                                             SSL3_HM_HEADER_LENGTH - 1)
170*e0c4386eSCy Schubert                             || !PACKET_get_net_2(&wholebody, &negversion)
171*e0c4386eSCy Schubert                                /* Skip random (32 bytes) */
172*e0c4386eSCy Schubert                             || !PACKET_forward(&wholebody, 32)
173*e0c4386eSCy Schubert                                /* Skip session id */
174*e0c4386eSCy Schubert                             || !PACKET_get_length_prefixed_1(&wholebody,
175*e0c4386eSCy Schubert                                                              &sessionid)
176*e0c4386eSCy Schubert                                /*
177*e0c4386eSCy Schubert                                 * Skip ciphersuite (2 bytes) and compression
178*e0c4386eSCy Schubert                                 * method (1 byte)
179*e0c4386eSCy Schubert                                 */
180*e0c4386eSCy Schubert                             || !PACKET_forward(&wholebody, 2 + 1)
181*e0c4386eSCy Schubert                             || !PACKET_get_length_prefixed_2(&wholebody,
182*e0c4386eSCy Schubert                                                              &extensions))
183*e0c4386eSCy Schubert                         return -1;
184*e0c4386eSCy Schubert 
185*e0c4386eSCy Schubert                     /*
186*e0c4386eSCy Schubert                      * Find the negotiated version in supported_versions
187*e0c4386eSCy Schubert                      * extension, if present.
188*e0c4386eSCy Schubert                      */
189*e0c4386eSCy Schubert                     while (PACKET_remaining(&extensions)) {
190*e0c4386eSCy Schubert                         unsigned int type;
191*e0c4386eSCy Schubert                         PACKET extbody;
192*e0c4386eSCy Schubert 
193*e0c4386eSCy Schubert                         if (!PACKET_get_net_2(&extensions, &type)
194*e0c4386eSCy Schubert                                 || !PACKET_get_length_prefixed_2(&extensions,
195*e0c4386eSCy Schubert                                 &extbody))
196*e0c4386eSCy Schubert                             return -1;
197*e0c4386eSCy Schubert 
198*e0c4386eSCy Schubert                         if (type == TLSEXT_TYPE_supported_versions
199*e0c4386eSCy Schubert                                 && (!PACKET_get_net_2(&extbody, &negversion)
200*e0c4386eSCy Schubert                                     || PACKET_remaining(&extbody) != 0))
201*e0c4386eSCy Schubert                             return -1;
202*e0c4386eSCy Schubert                     }
203*e0c4386eSCy Schubert                 }
204*e0c4386eSCy Schubert 
205*e0c4386eSCy Schubert                 while (PACKET_get_1(&payload, &data)) {
206*e0c4386eSCy Schubert                     /* Create a new one byte long record for each byte in the
207*e0c4386eSCy Schubert                      * record in the input buffer
208*e0c4386eSCy Schubert                      */
209*e0c4386eSCy Schubert                     char smallrec[MIN_RECORD_LEN] = {
210*e0c4386eSCy Schubert                         0, /* Content type */
211*e0c4386eSCy Schubert                         0, /* Version hi */
212*e0c4386eSCy Schubert                         0, /* Version lo */
213*e0c4386eSCy Schubert                         0, /* Length hi */
214*e0c4386eSCy Schubert                         1, /* Length lo */
215*e0c4386eSCy Schubert                         0  /* Data */
216*e0c4386eSCy Schubert                     };
217*e0c4386eSCy Schubert 
218*e0c4386eSCy Schubert                     smallrec[CONTENTTYPEPOS] = contenttype;
219*e0c4386eSCy Schubert                     smallrec[VERSIONHIPOS] = versionhi;
220*e0c4386eSCy Schubert                     smallrec[VERSIONLOPOS] = versionlo;
221*e0c4386eSCy Schubert                     smallrec[DATAPOS] = data;
222*e0c4386eSCy Schubert                     ret = BIO_write(next, smallrec, MIN_RECORD_LEN);
223*e0c4386eSCy Schubert                     if (ret <= 0)
224*e0c4386eSCy Schubert                         return -1;
225*e0c4386eSCy Schubert                     written++;
226*e0c4386eSCy Schubert                 }
227*e0c4386eSCy Schubert                 /*
228*e0c4386eSCy Schubert                  * We can't fragment anything after the ServerHello (or CCS <=
229*e0c4386eSCy Schubert                  * TLS1.2), otherwise we get a bad record MAC
230*e0c4386eSCy Schubert                  */
231*e0c4386eSCy Schubert                 if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC
232*e0c4386eSCy Schubert                         || (negversion == TLS1_3_VERSION
233*e0c4386eSCy Schubert                             && msgtype == SSL3_MT_SERVER_HELLO)) {
234*e0c4386eSCy Schubert                     fragment = 0;
235*e0c4386eSCy Schubert                     break;
236*e0c4386eSCy Schubert                 }
237*e0c4386eSCy Schubert             }
238*e0c4386eSCy Schubert         }
239*e0c4386eSCy Schubert         /* Write any data we have left after fragmenting */
240*e0c4386eSCy Schubert         ret = 0;
241*e0c4386eSCy Schubert         if ((int)written < inl) {
242*e0c4386eSCy Schubert             ret = BIO_write(next, in + written, inl - written);
243*e0c4386eSCy Schubert         }
244*e0c4386eSCy Schubert 
245*e0c4386eSCy Schubert         if (ret <= 0 && BIO_should_write(next))
246*e0c4386eSCy Schubert             BIO_set_retry_write(bio);
247*e0c4386eSCy Schubert         else
248*e0c4386eSCy Schubert             ret += written;
249*e0c4386eSCy Schubert     } else {
250*e0c4386eSCy Schubert         ctrs->wctr++;
251*e0c4386eSCy Schubert         BIO_set_retry_write(bio);
252*e0c4386eSCy Schubert     }
253*e0c4386eSCy Schubert 
254*e0c4386eSCy Schubert     return ret;
255*e0c4386eSCy Schubert }
256*e0c4386eSCy Schubert 
async_ctrl(BIO * bio,int cmd,long num,void * ptr)257*e0c4386eSCy Schubert static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
258*e0c4386eSCy Schubert {
259*e0c4386eSCy Schubert     long ret;
260*e0c4386eSCy Schubert     BIO *next = BIO_next(bio);
261*e0c4386eSCy Schubert 
262*e0c4386eSCy Schubert     if (next == NULL)
263*e0c4386eSCy Schubert         return 0;
264*e0c4386eSCy Schubert 
265*e0c4386eSCy Schubert     switch (cmd) {
266*e0c4386eSCy Schubert     case BIO_CTRL_DUP:
267*e0c4386eSCy Schubert         ret = 0L;
268*e0c4386eSCy Schubert         break;
269*e0c4386eSCy Schubert     default:
270*e0c4386eSCy Schubert         ret = BIO_ctrl(next, cmd, num, ptr);
271*e0c4386eSCy Schubert         break;
272*e0c4386eSCy Schubert     }
273*e0c4386eSCy Schubert     return ret;
274*e0c4386eSCy Schubert }
275*e0c4386eSCy Schubert 
async_gets(BIO * bio,char * buf,int size)276*e0c4386eSCy Schubert static int async_gets(BIO *bio, char *buf, int size)
277*e0c4386eSCy Schubert {
278*e0c4386eSCy Schubert     /* We don't support this - not needed anyway */
279*e0c4386eSCy Schubert     return -1;
280*e0c4386eSCy Schubert }
281*e0c4386eSCy Schubert 
async_puts(BIO * bio,const char * str)282*e0c4386eSCy Schubert static int async_puts(BIO *bio, const char *str)
283*e0c4386eSCy Schubert {
284*e0c4386eSCy Schubert     return async_write(bio, str, strlen(str));
285*e0c4386eSCy Schubert }
286*e0c4386eSCy Schubert 
287*e0c4386eSCy Schubert #define MAX_ATTEMPTS    100
288*e0c4386eSCy Schubert 
test_asyncio(int test)289*e0c4386eSCy Schubert static int test_asyncio(int test)
290*e0c4386eSCy Schubert {
291*e0c4386eSCy Schubert     SSL_CTX *serverctx = NULL, *clientctx = NULL;
292*e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
293*e0c4386eSCy Schubert     BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
294*e0c4386eSCy Schubert     int testresult = 0, ret;
295*e0c4386eSCy Schubert     size_t i, j;
296*e0c4386eSCy Schubert     const char testdata[] = "Test data";
297*e0c4386eSCy Schubert     char buf[sizeof(testdata)];
298*e0c4386eSCy Schubert 
299*e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(),
300*e0c4386eSCy Schubert                                        TLS_client_method(),
301*e0c4386eSCy Schubert                                        TLS1_VERSION, 0,
302*e0c4386eSCy Schubert                                        &serverctx, &clientctx, cert, privkey)))
303*e0c4386eSCy Schubert         goto end;
304*e0c4386eSCy Schubert 
305*e0c4386eSCy Schubert     /*
306*e0c4386eSCy Schubert      * We do 2 test runs. The first time around we just do a normal handshake
307*e0c4386eSCy Schubert      * with lots of async io going on. The second time around we also break up
308*e0c4386eSCy Schubert      * all records so that the content is only one byte length (up until the
309*e0c4386eSCy Schubert      * CCS)
310*e0c4386eSCy Schubert      */
311*e0c4386eSCy Schubert     if (test == 1)
312*e0c4386eSCy Schubert         fragment = 1;
313*e0c4386eSCy Schubert 
314*e0c4386eSCy Schubert 
315*e0c4386eSCy Schubert     s_to_c_fbio = BIO_new(bio_f_async_filter());
316*e0c4386eSCy Schubert     c_to_s_fbio = BIO_new(bio_f_async_filter());
317*e0c4386eSCy Schubert     if (!TEST_ptr(s_to_c_fbio)
318*e0c4386eSCy Schubert             || !TEST_ptr(c_to_s_fbio)) {
319*e0c4386eSCy Schubert         BIO_free(s_to_c_fbio);
320*e0c4386eSCy Schubert         BIO_free(c_to_s_fbio);
321*e0c4386eSCy Schubert         goto end;
322*e0c4386eSCy Schubert     }
323*e0c4386eSCy Schubert 
324*e0c4386eSCy Schubert     /* BIOs get freed on error */
325*e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl,
326*e0c4386eSCy Schubert                                       &clientssl, s_to_c_fbio, c_to_s_fbio))
327*e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
328*e0c4386eSCy Schubert                           SSL_ERROR_NONE)))
329*e0c4386eSCy Schubert         goto end;
330*e0c4386eSCy Schubert 
331*e0c4386eSCy Schubert     /*
332*e0c4386eSCy Schubert      * Send and receive some test data. Do the whole thing twice to ensure
333*e0c4386eSCy Schubert      * we hit at least one async event in both reading and writing
334*e0c4386eSCy Schubert      */
335*e0c4386eSCy Schubert     for (j = 0; j < 2; j++) {
336*e0c4386eSCy Schubert         int len;
337*e0c4386eSCy Schubert 
338*e0c4386eSCy Schubert         /*
339*e0c4386eSCy Schubert          * Write some test data. It should never take more than 2 attempts
340*e0c4386eSCy Schubert          * (the first one might be a retryable fail).
341*e0c4386eSCy Schubert          */
342*e0c4386eSCy Schubert         for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
343*e0c4386eSCy Schubert             i++) {
344*e0c4386eSCy Schubert             ret = SSL_write(clientssl, testdata + len,
345*e0c4386eSCy Schubert                 sizeof(testdata) - len);
346*e0c4386eSCy Schubert             if (ret > 0) {
347*e0c4386eSCy Schubert                 len += ret;
348*e0c4386eSCy Schubert             } else {
349*e0c4386eSCy Schubert                 int ssl_error = SSL_get_error(clientssl, ret);
350*e0c4386eSCy Schubert 
351*e0c4386eSCy Schubert                 if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
352*e0c4386eSCy Schubert                                 ssl_error == SSL_ERROR_SSL))
353*e0c4386eSCy Schubert                     goto end;
354*e0c4386eSCy Schubert             }
355*e0c4386eSCy Schubert         }
356*e0c4386eSCy Schubert         if (!TEST_size_t_eq(len, sizeof(testdata)))
357*e0c4386eSCy Schubert             goto end;
358*e0c4386eSCy Schubert 
359*e0c4386eSCy Schubert         /*
360*e0c4386eSCy Schubert          * Now read the test data. It may take more attempts here because
361*e0c4386eSCy Schubert          * it could fail once for each byte read, including all overhead
362*e0c4386eSCy Schubert          * bytes from the record header/padding etc.
363*e0c4386eSCy Schubert          */
364*e0c4386eSCy Schubert         for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
365*e0c4386eSCy Schubert                 i < MAX_ATTEMPTS; i++) {
366*e0c4386eSCy Schubert             ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
367*e0c4386eSCy Schubert             if (ret > 0) {
368*e0c4386eSCy Schubert                 len += ret;
369*e0c4386eSCy Schubert             } else {
370*e0c4386eSCy Schubert                 int ssl_error = SSL_get_error(serverssl, ret);
371*e0c4386eSCy Schubert 
372*e0c4386eSCy Schubert                 if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
373*e0c4386eSCy Schubert                                 ssl_error == SSL_ERROR_SSL))
374*e0c4386eSCy Schubert                     goto end;
375*e0c4386eSCy Schubert             }
376*e0c4386eSCy Schubert         }
377*e0c4386eSCy Schubert         if (!TEST_mem_eq(testdata, sizeof(testdata), buf, len))
378*e0c4386eSCy Schubert             goto end;
379*e0c4386eSCy Schubert     }
380*e0c4386eSCy Schubert 
381*e0c4386eSCy Schubert     /* Also frees the BIOs */
382*e0c4386eSCy Schubert     SSL_free(clientssl);
383*e0c4386eSCy Schubert     SSL_free(serverssl);
384*e0c4386eSCy Schubert     clientssl = serverssl = NULL;
385*e0c4386eSCy Schubert 
386*e0c4386eSCy Schubert     testresult = 1;
387*e0c4386eSCy Schubert 
388*e0c4386eSCy Schubert  end:
389*e0c4386eSCy Schubert     SSL_free(clientssl);
390*e0c4386eSCy Schubert     SSL_free(serverssl);
391*e0c4386eSCy Schubert     SSL_CTX_free(clientctx);
392*e0c4386eSCy Schubert     SSL_CTX_free(serverctx);
393*e0c4386eSCy Schubert 
394*e0c4386eSCy Schubert     return testresult;
395*e0c4386eSCy Schubert }
396*e0c4386eSCy Schubert 
397*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("certname privkey\n")
398*e0c4386eSCy Schubert 
setup_tests(void)399*e0c4386eSCy Schubert int setup_tests(void)
400*e0c4386eSCy Schubert {
401*e0c4386eSCy Schubert     if (!test_skip_common_options()) {
402*e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
403*e0c4386eSCy Schubert         return 0;
404*e0c4386eSCy Schubert     }
405*e0c4386eSCy Schubert 
406*e0c4386eSCy Schubert     if (!TEST_ptr(cert = test_get_argument(0))
407*e0c4386eSCy Schubert             || !TEST_ptr(privkey = test_get_argument(1)))
408*e0c4386eSCy Schubert         return 0;
409*e0c4386eSCy Schubert 
410*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_asyncio, 2);
411*e0c4386eSCy Schubert     return 1;
412*e0c4386eSCy Schubert }
413*e0c4386eSCy Schubert 
cleanup_tests(void)414*e0c4386eSCy Schubert void cleanup_tests(void)
415*e0c4386eSCy Schubert {
416*e0c4386eSCy Schubert     BIO_meth_free(methods_async);
417*e0c4386eSCy Schubert }
418