2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
12 #include <openssl/ssl.h>
13 #include <openssl/bio.h>
14 #include <openssl/err.h>
16 #include "../ssl/packet_locl.h"
18 /* Should we fragment records or not? 0 = no, !0 = yes*/
19 static int fragment
= 0;
21 static int async_new(BIO
*bi
);
22 static int async_free(BIO
*a
);
23 static int async_read(BIO
*b
, char *out
, int outl
);
24 static int async_write(BIO
*b
, const char *in
, int inl
);
25 static long async_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
26 static int async_gets(BIO
*bp
, char *buf
, int size
);
27 static int async_puts(BIO
*bp
, const char *str
);
29 /* Choose a sufficiently large type likely to be unused for this custom BIO */
30 # define BIO_TYPE_ASYNC_FILTER (0x80 | BIO_TYPE_FILTER)
32 static BIO_METHOD
*methods_async
= NULL
;
39 static const BIO_METHOD
*bio_f_async_filter()
41 if (methods_async
== NULL
) {
42 methods_async
= BIO_meth_new(BIO_TYPE_ASYNC_FILTER
, "Async filter");
43 if ( methods_async
== NULL
44 || !BIO_meth_set_write(methods_async
, async_write
)
45 || !BIO_meth_set_read(methods_async
, async_read
)
46 || !BIO_meth_set_puts(methods_async
, async_puts
)
47 || !BIO_meth_set_gets(methods_async
, async_gets
)
48 || !BIO_meth_set_ctrl(methods_async
, async_ctrl
)
49 || !BIO_meth_set_create(methods_async
, async_new
)
50 || !BIO_meth_set_destroy(methods_async
, async_free
))
56 static int async_new(BIO
*bio
)
58 struct async_ctrs
*ctrs
;
60 ctrs
= OPENSSL_zalloc(sizeof(struct async_ctrs
));
64 BIO_set_data(bio
, ctrs
);
69 static int async_free(BIO
*bio
)
71 struct async_ctrs
*ctrs
;
75 ctrs
= BIO_get_data(bio
);
77 BIO_set_data(bio
, NULL
);
83 static int async_read(BIO
*bio
, char *out
, int outl
)
85 struct async_ctrs
*ctrs
;
87 BIO
*next
= BIO_next(bio
);
94 ctrs
= BIO_get_data(bio
);
96 BIO_clear_retry_flags(bio
);
99 ret
= BIO_read(next
, out
, 1);
100 if (ret
<= 0 && BIO_should_read(next
))
101 BIO_set_retry_read(bio
);
105 BIO_set_retry_read(bio
);
111 #define MIN_RECORD_LEN 6
113 #define CONTENTTYPEPOS 0
114 #define VERSIONHIPOS 1
115 #define VERSIONLOPOS 2
118 static int async_write(BIO
*bio
, const char *in
, int inl
)
120 struct async_ctrs
*ctrs
;
123 BIO
*next
= BIO_next(bio
);
130 ctrs
= BIO_get_data(bio
);
132 BIO_clear_retry_flags(bio
);
134 if (ctrs
->wctr
> 0) {
139 if (!PACKET_buf_init(&pkt
, (const unsigned char *)in
, inl
))
142 while (PACKET_remaining(&pkt
) > 0) {
144 unsigned int contenttype
, versionhi
, versionlo
, data
;
146 if ( !PACKET_get_1(&pkt
, &contenttype
)
147 || !PACKET_get_1(&pkt
, &versionhi
)
148 || !PACKET_get_1(&pkt
, &versionlo
)
149 || !PACKET_get_length_prefixed_2(&pkt
, &payload
))
152 /* Pretend we wrote out the record header */
153 written
+= SSL3_RT_HEADER_LENGTH
;
155 while (PACKET_get_1(&payload
, &data
)) {
156 /* Create a new one byte long record for each byte in the
157 * record in the input buffer
159 char smallrec
[MIN_RECORD_LEN
] = {
160 0, /* Content type */
168 smallrec
[CONTENTTYPEPOS
] = contenttype
;
169 smallrec
[VERSIONHIPOS
] = versionhi
;
170 smallrec
[VERSIONLOPOS
] = versionlo
;
171 smallrec
[DATAPOS
] = data
;
172 ret
= BIO_write(next
, smallrec
, MIN_RECORD_LEN
);
178 * We can't fragment anything after the CCS, otherwise we
179 * get a bad record MAC
181 if (contenttype
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
187 /* Write any data we have left after fragmenting */
189 if ((int)written
< inl
) {
190 ret
= BIO_write(next
, in
+ written
, inl
- written
);
193 if (ret
<= 0 && BIO_should_write(next
))
194 BIO_set_retry_write(bio
);
199 BIO_set_retry_write(bio
);
205 static long async_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
208 BIO
*next
= BIO_next(bio
);
218 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
224 static int async_gets(BIO
*bio
, char *buf
, int size
)
226 /* We don't support this - not needed anyway */
230 static int async_puts(BIO
*bio
, const char *str
)
232 return async_write(bio
, str
, strlen(str
));
235 #define MAXLOOPS 100000
237 int main(int argc
, char *argv
[])
239 SSL_CTX
*serverctx
= NULL
, *clientctx
= NULL
;
240 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
241 BIO
*s_to_c_bio
= NULL
, *c_to_s_bio
= NULL
;
242 BIO
*s_to_c_fbio
= NULL
, *c_to_s_fbio
= NULL
;
243 int retc
= -1, rets
= -1, err
, abortctr
;
246 CRYPTO_set_mem_debug(1);
247 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
250 printf("Invalid argument count\n");
254 serverctx
= SSL_CTX_new(TLS_server_method());
255 clientctx
= SSL_CTX_new(TLS_client_method());
256 if (serverctx
== NULL
|| clientctx
== NULL
) {
257 printf("Failed to create SSL_CTX\n");
261 if (SSL_CTX_use_certificate_file(serverctx
, argv
[1],
262 SSL_FILETYPE_PEM
) <= 0) {
263 printf("Failed to load server certificate\n");
266 if (SSL_CTX_use_PrivateKey_file(serverctx
, argv
[2],
267 SSL_FILETYPE_PEM
) <= 0) {
268 printf("Failed to load server private key\n");
270 if (SSL_CTX_check_private_key(serverctx
) <= 0) {
271 printf("Failed to check private key\n");
276 * We do 2 test runs. The first time around we just do a normal handshake
277 * with lots of async io going on. The second time around we also break up
278 * all records so that the content is only one byte length (up until the
281 for (test
= 1; test
< 3; test
++) {
287 serverssl
= SSL_new(serverctx
);
288 clientssl
= SSL_new(clientctx
);
289 if (serverssl
== NULL
|| clientssl
== NULL
) {
290 printf("Failed to create SSL object\n");
294 s_to_c_bio
= BIO_new(BIO_s_mem());
295 c_to_s_bio
= BIO_new(BIO_s_mem());
296 if (s_to_c_bio
== NULL
|| c_to_s_bio
== NULL
) {
297 printf("Failed to create mem BIOs\n");
301 s_to_c_fbio
= BIO_new(bio_f_async_filter());
302 c_to_s_fbio
= BIO_new(bio_f_async_filter());
303 if (s_to_c_fbio
== NULL
|| c_to_s_fbio
== NULL
) {
304 printf("Failed to create filter BIOs\n");
308 s_to_c_bio
= BIO_push(s_to_c_fbio
, s_to_c_bio
);
309 c_to_s_bio
= BIO_push(c_to_s_fbio
, c_to_s_bio
);
310 if (s_to_c_bio
== NULL
|| c_to_s_bio
== NULL
) {
311 printf("Failed to create chained BIOs\n");
315 /* Set Non-blocking IO behaviour */
316 BIO_set_mem_eof_return(s_to_c_bio
, -1);
317 BIO_set_mem_eof_return(c_to_s_bio
, -1);
319 /* Up ref these as we are passing them to two SSL objects */
320 BIO_up_ref(s_to_c_bio
);
321 BIO_up_ref(c_to_s_bio
);
323 SSL_set_bio(serverssl
, c_to_s_bio
, s_to_c_bio
);
324 SSL_set_bio(clientssl
, s_to_c_bio
, c_to_s_bio
);
327 err
= SSL_ERROR_WANT_WRITE
;
328 while (retc
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
329 retc
= SSL_connect(clientssl
);
331 err
= SSL_get_error(clientssl
, retc
);
334 if (retc
<= 0 && err
!= SSL_ERROR_WANT_READ
) {
335 printf("Test %d failed: SSL_connect() failed %d, %d\n",
340 err
= SSL_ERROR_WANT_WRITE
;
341 while (rets
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
342 rets
= SSL_accept(serverssl
);
344 err
= SSL_get_error(serverssl
, rets
);
347 if (rets
<= 0 && err
!= SSL_ERROR_WANT_READ
) {
348 printf("Test %d failed: SSL_accept() failed %d, %d\n",
352 if (++abortctr
== MAXLOOPS
) {
353 printf("Test %d failed: No progress made\n", test
);
356 } while (retc
<=0 || rets
<= 0);
358 /* Also frees the BIOs */
361 clientssl
= serverssl
= NULL
;
364 printf("Test success\n");
367 if (retc
<= 0 || rets
<= 0)
368 ERR_print_errors_fp(stderr
);
372 SSL_CTX_free(clientctx
);
373 SSL_CTX_free(serverctx
);
375 # ifndef OPENSSL_NO_CRYPTO_MDEBUG
376 CRYPTO_mem_leaks_fp(stderr
);
379 return (retc
> 0 && rets
> 0) ? 0 : 1;