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) {
146 unsigned int contenttype, versionhi, versionlo, data;
148 if ( !PACKET_get_1(&pkt, &contenttype)
149 || !PACKET_get_1(&pkt, &versionhi)
150 || !PACKET_get_1(&pkt, &versionlo)
151 || !PACKET_get_length_prefixed_2(&pkt, &payload))
154 /* Pretend we wrote out the record header */
155 written += SSL3_RT_HEADER_LENGTH;
157 while (PACKET_get_1(&payload, &data)) {
158 /* Create a new one byte long record for each byte in the
159 * record in the input buffer
161 char smallrec[MIN_RECORD_LEN] = {
162 0, /* Content type */
170 smallrec[CONTENTTYPEPOS] = contenttype;
171 smallrec[VERSIONHIPOS] = versionhi;
172 smallrec[VERSIONLOPOS] = versionlo;
173 smallrec[DATAPOS] = data;
174 ret = BIO_write(next, smallrec, MIN_RECORD_LEN);
180 * We can't fragment anything after the CCS, otherwise we
181 * get a bad record MAC
183 if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC) {
189 /* Write any data we have left after fragmenting */
191 if ((int)written < inl) {
192 ret = BIO_write(next, in + written , inl - written);
195 if (ret <= 0 && BIO_should_write(next))
196 BIO_set_retry_write(bio);
201 BIO_set_retry_write(bio);
207 static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
210 BIO *next = BIO_next(bio);
220 ret = BIO_ctrl(next, cmd, num, ptr);
226 static int async_gets(BIO *bio, char *buf, int size)
228 /* We don't support this - not needed anyway */
232 static int async_puts(BIO *bio, const char *str)
234 return async_write(bio, str, strlen(str));
237 #define MAX_ATTEMPTS 100
239 int main(int argc, char *argv[])
241 SSL_CTX *serverctx = NULL, *clientctx = NULL;
242 SSL *serverssl = NULL, *clientssl = NULL;
243 BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
244 int test, err = 1, ret;
246 const char testdata[] = "Test data";
247 char buf[sizeof(testdata)];
249 CRYPTO_set_mem_debug(1);
250 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
253 printf("Invalid argument count\n");
257 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
258 &serverctx, &clientctx, argv[1], argv[2])) {
259 printf("Failed to create SSL_CTX pair\n");
264 * We do 2 test runs. The first time around we just do a normal handshake
265 * with lots of async io going on. The second time around we also break up
266 * all records so that the content is only one byte length (up until the
269 for (test = 1; test < 3; test++) {
274 s_to_c_fbio = BIO_new(bio_f_async_filter());
275 c_to_s_fbio = BIO_new(bio_f_async_filter());
276 if (s_to_c_fbio == NULL || c_to_s_fbio == NULL) {
277 printf("Failed to create filter BIOs\n");
278 BIO_free(s_to_c_fbio);
279 BIO_free(c_to_s_fbio);
283 /* BIOs get freed on error */
284 if (!create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl,
285 s_to_c_fbio, c_to_s_fbio)) {
286 printf("Test %d failed: Create SSL objects failed\n", test);
290 if (!create_ssl_connection(serverssl, clientssl)) {
291 printf("Test %d failed: Create SSL connection failed\n", test);
296 * Send and receive some test data. Do the whole thing twice to ensure
297 * we hit at least one async event in both reading and writing
299 for (j = 0; j < 2; j++) {
301 * Write some test data. It should never take more than 2 attempts
302 * (the first one might be a retryable fail). A zero return from
303 * SSL_write() is a non-retryable failure, so fail immediately if
306 for (ret = -1, i = 0; ret < 0 && i < 2 * sizeof(testdata); i++)
307 ret = SSL_write(clientssl, testdata, sizeof(testdata));
309 printf("Test %d failed: Failed to write app data\n", test);
313 * Now read the test data. It may take more attemps here because
314 * it could fail once for each byte read, including all overhead
315 * bytes from the record header/padding etc. Fail immediately if we
316 * get a zero return from SSL_read().
318 for (ret = -1, i = 0; ret < 0 && i < MAX_ATTEMPTS; i++)
319 ret = SSL_read(serverssl, buf, sizeof(buf));
321 printf("Test %d failed: Failed to read app data\n", test);
324 if (ret != sizeof(testdata)
325 || memcmp(buf, testdata, sizeof(testdata)) != 0) {
326 printf("Test %d failed: Unexpected app data received\n", test);
331 /* Also frees the BIOs */
334 clientssl = serverssl = NULL;
337 printf("Test success\n");
342 ERR_print_errors_fp(stderr);
346 SSL_CTX_free(clientctx);
347 SSL_CTX_free(serverctx);
349 # ifndef OPENSSL_NO_CRYPTO_MDEBUG
350 CRYPTO_mem_leaks_fp(stderr);