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 #include "ssltestlib.h"
20 /* Should we fragment records or not? 0 = no, !0 = yes*/
21 static int fragment
= 0;
23 static int async_new(BIO
*bi
);
24 static int async_free(BIO
*a
);
25 static int async_read(BIO
*b
, char *out
, int outl
);
26 static int async_write(BIO
*b
, const char *in
, int inl
);
27 static long async_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
28 static int async_gets(BIO
*bp
, char *buf
, int size
);
29 static int async_puts(BIO
*bp
, const char *str
);
31 /* Choose a sufficiently large type likely to be unused for this custom BIO */
32 # define BIO_TYPE_ASYNC_FILTER (0x80 | BIO_TYPE_FILTER)
34 static BIO_METHOD
*methods_async
= NULL
;
41 static const BIO_METHOD
*bio_f_async_filter()
43 if (methods_async
== NULL
) {
44 methods_async
= BIO_meth_new(BIO_TYPE_ASYNC_FILTER
, "Async filter");
45 if ( methods_async
== NULL
46 || !BIO_meth_set_write(methods_async
, async_write
)
47 || !BIO_meth_set_read(methods_async
, async_read
)
48 || !BIO_meth_set_puts(methods_async
, async_puts
)
49 || !BIO_meth_set_gets(methods_async
, async_gets
)
50 || !BIO_meth_set_ctrl(methods_async
, async_ctrl
)
51 || !BIO_meth_set_create(methods_async
, async_new
)
52 || !BIO_meth_set_destroy(methods_async
, async_free
))
58 static int async_new(BIO
*bio
)
60 struct async_ctrs
*ctrs
;
62 ctrs
= OPENSSL_zalloc(sizeof(struct async_ctrs
));
66 BIO_set_data(bio
, ctrs
);
71 static int async_free(BIO
*bio
)
73 struct async_ctrs
*ctrs
;
77 ctrs
= BIO_get_data(bio
);
79 BIO_set_data(bio
, NULL
);
85 static int async_read(BIO
*bio
, char *out
, int outl
)
87 struct async_ctrs
*ctrs
;
89 BIO
*next
= BIO_next(bio
);
96 ctrs
= BIO_get_data(bio
);
98 BIO_clear_retry_flags(bio
);
100 if (ctrs
->rctr
> 0) {
101 ret
= BIO_read(next
, out
, 1);
102 if (ret
<= 0 && BIO_should_read(next
))
103 BIO_set_retry_read(bio
);
107 BIO_set_retry_read(bio
);
113 #define MIN_RECORD_LEN 6
115 #define CONTENTTYPEPOS 0
116 #define VERSIONHIPOS 1
117 #define VERSIONLOPOS 2
120 static int async_write(BIO
*bio
, const char *in
, int inl
)
122 struct async_ctrs
*ctrs
;
125 BIO
*next
= BIO_next(bio
);
132 ctrs
= BIO_get_data(bio
);
134 BIO_clear_retry_flags(bio
);
136 if (ctrs
->wctr
> 0) {
141 if (!PACKET_buf_init(&pkt
, (const unsigned char *)in
, inl
))
144 while (PACKET_remaining(&pkt
) > 0) {
145 PACKET payload
, wholebody
;
146 unsigned int contenttype
, versionhi
, versionlo
, data
;
147 unsigned int msgtype
= 0, negversion
= 0;
149 if ( !PACKET_get_1(&pkt
, &contenttype
)
150 || !PACKET_get_1(&pkt
, &versionhi
)
151 || !PACKET_get_1(&pkt
, &versionlo
)
152 || !PACKET_get_length_prefixed_2(&pkt
, &payload
))
155 /* Pretend we wrote out the record header */
156 written
+= SSL3_RT_HEADER_LENGTH
;
159 if (contenttype
== SSL3_RT_HANDSHAKE
160 && !PACKET_get_1(&wholebody
, &msgtype
))
163 if (msgtype
== SSL3_MT_SERVER_HELLO
164 && (!PACKET_forward(&wholebody
,
165 SSL3_HM_HEADER_LENGTH
- 1)
166 || !PACKET_get_net_2(&wholebody
, &negversion
)))
169 while (PACKET_get_1(&payload
, &data
)) {
170 /* Create a new one byte long record for each byte in the
171 * record in the input buffer
173 char smallrec
[MIN_RECORD_LEN
] = {
174 0, /* Content type */
182 smallrec
[CONTENTTYPEPOS
] = contenttype
;
183 smallrec
[VERSIONHIPOS
] = versionhi
;
184 smallrec
[VERSIONLOPOS
] = versionlo
;
185 smallrec
[DATAPOS
] = data
;
186 ret
= BIO_write(next
, smallrec
, MIN_RECORD_LEN
);
192 * We can't fragment anything after the ServerHello (or CCS <=
193 * TLS1.2), otherwise we get a bad record MAC
194 * TODO(TLS1.3): Change TLS1_3_VERSION_DRAFT to TLS1_3_VERSION
197 if (contenttype
== SSL3_RT_CHANGE_CIPHER_SPEC
198 || (negversion
== TLS1_3_VERSION_DRAFT
199 && msgtype
== SSL3_MT_SERVER_HELLO
)) {
205 /* Write any data we have left after fragmenting */
207 if ((int)written
< inl
) {
208 ret
= BIO_write(next
, in
+ written
, inl
- written
);
211 if (ret
<= 0 && BIO_should_write(next
))
212 BIO_set_retry_write(bio
);
217 BIO_set_retry_write(bio
);
223 static long async_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
226 BIO
*next
= BIO_next(bio
);
236 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
242 static int async_gets(BIO
*bio
, char *buf
, int size
)
244 /* We don't support this - not needed anyway */
248 static int async_puts(BIO
*bio
, const char *str
)
250 return async_write(bio
, str
, strlen(str
));
253 #define MAX_ATTEMPTS 100
255 int main(int argc
, char *argv
[])
257 SSL_CTX
*serverctx
= NULL
, *clientctx
= NULL
;
258 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
259 BIO
*s_to_c_fbio
= NULL
, *c_to_s_fbio
= NULL
;
260 int test
, err
= 1, ret
;
262 const char testdata
[] = "Test data";
263 char buf
[sizeof(testdata
)];
265 CRYPTO_set_mem_debug(1);
266 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
269 printf("Invalid argument count\n");
273 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
274 &serverctx
, &clientctx
, argv
[1], argv
[2])) {
275 printf("Failed to create SSL_CTX pair\n");
280 * We do 2 test runs. The first time around we just do a normal handshake
281 * with lots of async io going on. The second time around we also break up
282 * all records so that the content is only one byte length (up until the
285 for (test
= 1; test
< 3; test
++) {
290 s_to_c_fbio
= BIO_new(bio_f_async_filter());
291 c_to_s_fbio
= BIO_new(bio_f_async_filter());
292 if (s_to_c_fbio
== NULL
|| c_to_s_fbio
== NULL
) {
293 printf("Failed to create filter BIOs\n");
294 BIO_free(s_to_c_fbio
);
295 BIO_free(c_to_s_fbio
);
299 /* BIOs get freed on error */
300 if (!create_ssl_objects(serverctx
, clientctx
, &serverssl
, &clientssl
,
301 s_to_c_fbio
, c_to_s_fbio
)) {
302 printf("Test %d failed: Create SSL objects failed\n", test
);
306 if (!create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
307 printf("Test %d failed: Create SSL connection failed\n", test
);
312 * Send and receive some test data. Do the whole thing twice to ensure
313 * we hit at least one async event in both reading and writing
315 for (j
= 0; j
< 2; j
++) {
319 * Write some test data. It should never take more than 2 attempts
320 * (the first one might be a retryable fail).
322 for (ret
= -1, i
= 0, len
= 0; len
!= sizeof(testdata
) && i
< 2;
324 ret
= SSL_write(clientssl
, testdata
+ len
,
325 sizeof(testdata
) - len
);
329 int ssl_error
= SSL_get_error(clientssl
, ret
);
331 if (ssl_error
== SSL_ERROR_SYSCALL
||
332 ssl_error
== SSL_ERROR_SSL
) {
333 printf("Test %d failed: Failed to write app data\n", test
);
339 if (len
!= sizeof(testdata
)) {
341 printf("Test %d failed: Failed to write all app data\n", test
);
345 * Now read the test data. It may take more attemps here because
346 * it could fail once for each byte read, including all overhead
347 * bytes from the record header/padding etc.
349 for (ret
= -1, i
= 0, len
= 0; len
!= sizeof(testdata
) &&
350 i
< MAX_ATTEMPTS
; i
++)
352 ret
= SSL_read(serverssl
, buf
+ len
, sizeof(buf
) - len
);
356 int ssl_error
= SSL_get_error(serverssl
, ret
);
358 if (ssl_error
== SSL_ERROR_SYSCALL
||
359 ssl_error
== SSL_ERROR_SSL
) {
360 printf("Test %d failed: Failed to read app data\n", test
);
366 if (len
!= sizeof(testdata
)
367 || memcmp(buf
, testdata
, sizeof(testdata
)) != 0) {
369 printf("Test %d failed: Unexpected app data received\n", test
);
374 /* Also frees the BIOs */
377 clientssl
= serverssl
= NULL
;
380 printf("Test success\n");
385 ERR_print_errors_fp(stderr
);
389 SSL_CTX_free(clientctx
);
390 SSL_CTX_free(serverctx
);
392 # ifndef OPENSSL_NO_CRYPTO_MDEBUG
393 CRYPTO_mem_leaks_fp(stderr
);