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 #include "test_main_custom.h"
22 /* Should we fragment records or not? 0 = no, !0 = yes*/
23 static int fragment = 0;
25 static char *cert = NULL;
26 static char *privkey = NULL;
28 static int async_new(BIO *bi);
29 static int async_free(BIO *a);
30 static int async_read(BIO *b, char *out, int outl);
31 static int async_write(BIO *b, const char *in, int inl);
32 static long async_ctrl(BIO *b, int cmd, long num, void *ptr);
33 static int async_gets(BIO *bp, char *buf, int size);
34 static int async_puts(BIO *bp, const char *str);
36 /* Choose a sufficiently large type likely to be unused for this custom BIO */
37 # define BIO_TYPE_ASYNC_FILTER (0x80 | BIO_TYPE_FILTER)
39 static BIO_METHOD *methods_async = NULL;
46 static const BIO_METHOD *bio_f_async_filter()
48 if (methods_async == NULL) {
49 methods_async = BIO_meth_new(BIO_TYPE_ASYNC_FILTER, "Async filter");
50 if ( methods_async == NULL
51 || !BIO_meth_set_write(methods_async, async_write)
52 || !BIO_meth_set_read(methods_async, async_read)
53 || !BIO_meth_set_puts(methods_async, async_puts)
54 || !BIO_meth_set_gets(methods_async, async_gets)
55 || !BIO_meth_set_ctrl(methods_async, async_ctrl)
56 || !BIO_meth_set_create(methods_async, async_new)
57 || !BIO_meth_set_destroy(methods_async, async_free))
63 static int async_new(BIO *bio)
65 struct async_ctrs *ctrs;
67 ctrs = OPENSSL_zalloc(sizeof(struct async_ctrs));
71 BIO_set_data(bio, ctrs);
76 static int async_free(BIO *bio)
78 struct async_ctrs *ctrs;
82 ctrs = BIO_get_data(bio);
84 BIO_set_data(bio, NULL);
90 static int async_read(BIO *bio, char *out, int outl)
92 struct async_ctrs *ctrs;
94 BIO *next = BIO_next(bio);
101 ctrs = BIO_get_data(bio);
103 BIO_clear_retry_flags(bio);
105 if (ctrs->rctr > 0) {
106 ret = BIO_read(next, out, 1);
107 if (ret <= 0 && BIO_should_read(next))
108 BIO_set_retry_read(bio);
112 BIO_set_retry_read(bio);
118 #define MIN_RECORD_LEN 6
120 #define CONTENTTYPEPOS 0
121 #define VERSIONHIPOS 1
122 #define VERSIONLOPOS 2
125 static int async_write(BIO *bio, const char *in, int inl)
127 struct async_ctrs *ctrs;
130 BIO *next = BIO_next(bio);
137 ctrs = BIO_get_data(bio);
139 BIO_clear_retry_flags(bio);
141 if (ctrs->wctr > 0) {
146 if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
149 while (PACKET_remaining(&pkt) > 0) {
150 PACKET payload, wholebody;
151 unsigned int contenttype, versionhi, versionlo, data;
152 unsigned int msgtype = 0, negversion = 0;
154 if (!PACKET_get_1(&pkt, &contenttype)
155 || !PACKET_get_1(&pkt, &versionhi)
156 || !PACKET_get_1(&pkt, &versionlo)
157 || !PACKET_get_length_prefixed_2(&pkt, &payload))
160 /* Pretend we wrote out the record header */
161 written += SSL3_RT_HEADER_LENGTH;
164 if (contenttype == SSL3_RT_HANDSHAKE
165 && !PACKET_get_1(&wholebody, &msgtype))
168 if (msgtype == SSL3_MT_SERVER_HELLO
169 && (!PACKET_forward(&wholebody,
170 SSL3_HM_HEADER_LENGTH - 1)
171 || !PACKET_get_net_2(&wholebody, &negversion)))
174 while (PACKET_get_1(&payload, &data)) {
175 /* Create a new one byte long record for each byte in the
176 * record in the input buffer
178 char smallrec[MIN_RECORD_LEN] = {
179 0, /* Content type */
187 smallrec[CONTENTTYPEPOS] = contenttype;
188 smallrec[VERSIONHIPOS] = versionhi;
189 smallrec[VERSIONLOPOS] = versionlo;
190 smallrec[DATAPOS] = data;
191 ret = BIO_write(next, smallrec, MIN_RECORD_LEN);
197 * We can't fragment anything after the ServerHello (or CCS <=
198 * TLS1.2), otherwise we get a bad record MAC
199 * TODO(TLS1.3): Change TLS1_3_VERSION_DRAFT to TLS1_3_VERSION
202 if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC
203 || (negversion == TLS1_3_VERSION_DRAFT
204 && msgtype == SSL3_MT_SERVER_HELLO)) {
210 /* Write any data we have left after fragmenting */
212 if ((int)written < inl) {
213 ret = BIO_write(next, in + written, inl - written);
216 if (ret <= 0 && BIO_should_write(next))
217 BIO_set_retry_write(bio);
222 BIO_set_retry_write(bio);
228 static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
231 BIO *next = BIO_next(bio);
241 ret = BIO_ctrl(next, cmd, num, ptr);
247 static int async_gets(BIO *bio, char *buf, int size)
249 /* We don't support this - not needed anyway */
253 static int async_puts(BIO *bio, const char *str)
255 return async_write(bio, str, strlen(str));
258 #define MAX_ATTEMPTS 100
260 static int test_asyncio(int test)
262 SSL_CTX *serverctx = NULL, *clientctx = NULL;
263 SSL *serverssl = NULL, *clientssl = NULL;
264 BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
265 int testresult = 0, ret;
267 const char testdata[] = "Test data";
268 char buf[sizeof(testdata)];
270 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
271 &serverctx, &clientctx, cert, privkey)))
275 * We do 2 test runs. The first time around we just do a normal handshake
276 * with lots of async io going on. The second time around we also break up
277 * all records so that the content is only one byte length (up until the
284 s_to_c_fbio = BIO_new(bio_f_async_filter());
285 c_to_s_fbio = BIO_new(bio_f_async_filter());
286 if (!TEST_ptr(s_to_c_fbio)
287 || !TEST_ptr(c_to_s_fbio)) {
288 BIO_free(s_to_c_fbio);
289 BIO_free(c_to_s_fbio);
293 /* BIOs get freed on error */
294 if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl,
295 &clientssl, s_to_c_fbio, c_to_s_fbio))
296 || !TEST_true(create_ssl_connection(serverssl, clientssl,
301 * Send and receive some test data. Do the whole thing twice to ensure
302 * we hit at least one async event in both reading and writing
304 for (j = 0; j < 2; j++) {
308 * Write some test data. It should never take more than 2 attempts
309 * (the first one might be a retryable fail).
311 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
313 ret = SSL_write(clientssl, testdata + len,
314 sizeof(testdata) - len);
318 int ssl_error = SSL_get_error(clientssl, ret);
320 if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
321 ssl_error == SSL_ERROR_SSL))
325 if (!TEST_size_t_eq(len, sizeof(testdata)))
329 * Now read the test data. It may take more attemps here because
330 * it could fail once for each byte read, including all overhead
331 * bytes from the record header/padding etc.
333 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
334 i < MAX_ATTEMPTS; i++) {
335 ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
339 int ssl_error = SSL_get_error(serverssl, ret);
341 if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
342 ssl_error == SSL_ERROR_SSL))
346 if (!TEST_mem_eq(testdata, sizeof(testdata), buf, len))
350 /* Also frees the BIOs */
353 clientssl = serverssl = NULL;
360 SSL_CTX_free(clientctx);
361 SSL_CTX_free(serverctx);
366 int test_main(int argc, char *argv[])
370 if (!TEST_int_eq(argc, 3))
376 ADD_ALL_TESTS(test_asyncio, 2);
378 testresult = run_tests(argv[0]);
381 BIO_meth_free(methods_async);