2 * Copyright 2016-2018 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"
21 /* Should we fragment records or not? 0 = no, !0 = yes*/
22 static int fragment = 0;
24 static char *cert = NULL;
25 static char *privkey = NULL;
27 static int async_new(BIO *bi);
28 static int async_free(BIO *a);
29 static int async_read(BIO *b, char *out, int outl);
30 static int async_write(BIO *b, const char *in, int inl);
31 static long async_ctrl(BIO *b, int cmd, long num, void *ptr);
32 static int async_gets(BIO *bp, char *buf, int size);
33 static int async_puts(BIO *bp, const char *str);
35 /* Choose a sufficiently large type likely to be unused for this custom BIO */
36 # define BIO_TYPE_ASYNC_FILTER (0x80 | BIO_TYPE_FILTER)
38 static BIO_METHOD *methods_async = NULL;
45 static const BIO_METHOD *bio_f_async_filter(void)
47 if (methods_async == NULL) {
48 methods_async = BIO_meth_new(BIO_TYPE_ASYNC_FILTER, "Async filter");
49 if ( methods_async == NULL
50 || !BIO_meth_set_write(methods_async, async_write)
51 || !BIO_meth_set_read(methods_async, async_read)
52 || !BIO_meth_set_puts(methods_async, async_puts)
53 || !BIO_meth_set_gets(methods_async, async_gets)
54 || !BIO_meth_set_ctrl(methods_async, async_ctrl)
55 || !BIO_meth_set_create(methods_async, async_new)
56 || !BIO_meth_set_destroy(methods_async, async_free))
62 static int async_new(BIO *bio)
64 struct async_ctrs *ctrs;
66 ctrs = OPENSSL_zalloc(sizeof(struct async_ctrs));
70 BIO_set_data(bio, ctrs);
75 static int async_free(BIO *bio)
77 struct async_ctrs *ctrs;
81 ctrs = BIO_get_data(bio);
83 BIO_set_data(bio, NULL);
89 static int async_read(BIO *bio, char *out, int outl)
91 struct async_ctrs *ctrs;
93 BIO *next = BIO_next(bio);
100 ctrs = BIO_get_data(bio);
102 BIO_clear_retry_flags(bio);
104 if (ctrs->rctr > 0) {
105 ret = BIO_read(next, out, 1);
106 if (ret <= 0 && BIO_should_read(next))
107 BIO_set_retry_read(bio);
111 BIO_set_retry_read(bio);
117 #define MIN_RECORD_LEN 6
119 #define CONTENTTYPEPOS 0
120 #define VERSIONHIPOS 1
121 #define VERSIONLOPOS 2
124 static int async_write(BIO *bio, const char *in, int inl)
126 struct async_ctrs *ctrs;
129 BIO *next = BIO_next(bio);
136 ctrs = BIO_get_data(bio);
138 BIO_clear_retry_flags(bio);
140 if (ctrs->wctr > 0) {
145 if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
148 while (PACKET_remaining(&pkt) > 0) {
149 PACKET payload, wholebody, sessionid, extensions;
150 unsigned int contenttype, versionhi, versionlo, data;
151 unsigned int msgtype = 0, negversion = 0;
153 if (!PACKET_get_1(&pkt, &contenttype)
154 || !PACKET_get_1(&pkt, &versionhi)
155 || !PACKET_get_1(&pkt, &versionlo)
156 || !PACKET_get_length_prefixed_2(&pkt, &payload))
159 /* Pretend we wrote out the record header */
160 written += SSL3_RT_HEADER_LENGTH;
163 if (contenttype == SSL3_RT_HANDSHAKE
164 && !PACKET_get_1(&wholebody, &msgtype))
167 if (msgtype == SSL3_MT_SERVER_HELLO) {
168 if (!PACKET_forward(&wholebody,
169 SSL3_HM_HEADER_LENGTH - 1)
170 || !PACKET_get_net_2(&wholebody, &negversion)
171 /* Skip random (32 bytes) */
172 || !PACKET_forward(&wholebody, 32)
173 /* Skip session id */
174 || !PACKET_get_length_prefixed_1(&wholebody,
177 * Skip ciphersuite (2 bytes) and compression
180 || !PACKET_forward(&wholebody, 2 + 1)
181 || !PACKET_get_length_prefixed_2(&wholebody,
186 * Find the negotiated version in supported_versions
187 * extension, if present.
189 while (PACKET_remaining(&extensions)) {
193 if (!PACKET_get_net_2(&extensions, &type)
194 || !PACKET_get_length_prefixed_2(&extensions,
198 if (type == TLSEXT_TYPE_supported_versions
199 && (!PACKET_get_net_2(&extbody, &negversion)
200 || PACKET_remaining(&extbody) != 0))
205 while (PACKET_get_1(&payload, &data)) {
206 /* Create a new one byte long record for each byte in the
207 * record in the input buffer
209 char smallrec[MIN_RECORD_LEN] = {
210 0, /* Content type */
218 smallrec[CONTENTTYPEPOS] = contenttype;
219 smallrec[VERSIONHIPOS] = versionhi;
220 smallrec[VERSIONLOPOS] = versionlo;
221 smallrec[DATAPOS] = data;
222 ret = BIO_write(next, smallrec, MIN_RECORD_LEN);
228 * We can't fragment anything after the ServerHello (or CCS <=
229 * TLS1.2), otherwise we get a bad record MAC
230 * TODO(TLS1.3): Change TLS1_3_VERSION_DRAFT to TLS1_3_VERSION
233 if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC
234 || (negversion == TLS1_3_VERSION_DRAFT
235 && msgtype == SSL3_MT_SERVER_HELLO)) {
241 /* Write any data we have left after fragmenting */
243 if ((int)written < inl) {
244 ret = BIO_write(next, in + written, inl - written);
247 if (ret <= 0 && BIO_should_write(next))
248 BIO_set_retry_write(bio);
253 BIO_set_retry_write(bio);
259 static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
262 BIO *next = BIO_next(bio);
272 ret = BIO_ctrl(next, cmd, num, ptr);
278 static int async_gets(BIO *bio, char *buf, int size)
280 /* We don't support this - not needed anyway */
284 static int async_puts(BIO *bio, const char *str)
286 return async_write(bio, str, strlen(str));
289 #define MAX_ATTEMPTS 100
291 static int test_asyncio(int test)
293 SSL_CTX *serverctx = NULL, *clientctx = NULL;
294 SSL *serverssl = NULL, *clientssl = NULL;
295 BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
296 int testresult = 0, ret;
298 const char testdata[] = "Test data";
299 char buf[sizeof(testdata)];
301 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
302 TLS1_VERSION, TLS_MAX_VERSION,
303 &serverctx, &clientctx, cert, privkey)))
307 * We do 2 test runs. The first time around we just do a normal handshake
308 * with lots of async io going on. The second time around we also break up
309 * all records so that the content is only one byte length (up until the
316 s_to_c_fbio = BIO_new(bio_f_async_filter());
317 c_to_s_fbio = BIO_new(bio_f_async_filter());
318 if (!TEST_ptr(s_to_c_fbio)
319 || !TEST_ptr(c_to_s_fbio)) {
320 BIO_free(s_to_c_fbio);
321 BIO_free(c_to_s_fbio);
325 /* BIOs get freed on error */
326 if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl,
327 &clientssl, s_to_c_fbio, c_to_s_fbio))
328 || !TEST_true(create_ssl_connection(serverssl, clientssl,
333 * Send and receive some test data. Do the whole thing twice to ensure
334 * we hit at least one async event in both reading and writing
336 for (j = 0; j < 2; j++) {
340 * Write some test data. It should never take more than 2 attempts
341 * (the first one might be a retryable fail).
343 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
345 ret = SSL_write(clientssl, testdata + len,
346 sizeof(testdata) - len);
350 int ssl_error = SSL_get_error(clientssl, ret);
352 if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
353 ssl_error == SSL_ERROR_SSL))
357 if (!TEST_size_t_eq(len, sizeof(testdata)))
361 * Now read the test data. It may take more attempts here because
362 * it could fail once for each byte read, including all overhead
363 * bytes from the record header/padding etc.
365 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
366 i < MAX_ATTEMPTS; i++) {
367 ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
371 int ssl_error = SSL_get_error(serverssl, ret);
373 if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
374 ssl_error == SSL_ERROR_SSL))
378 if (!TEST_mem_eq(testdata, sizeof(testdata), buf, len))
382 /* Also frees the BIOs */
385 clientssl = serverssl = NULL;
392 SSL_CTX_free(clientctx);
393 SSL_CTX_free(serverctx);
398 int setup_tests(void)
400 if (!TEST_ptr(cert = test_get_argument(0))
401 || !TEST_ptr(privkey = test_get_argument(1)))
404 ADD_ALL_TESTS(test_asyncio, 2);
408 void cleanup_tests(void)
410 BIO_meth_free(methods_async);