Add an SSL_SESSION accessor for obtaining the protocol version number, with
[openssl.git] / test / asynciotest.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include <string.h>
12 #include <openssl/ssl.h>
13 #include <openssl/bio.h>
14 #include <openssl/err.h>
15
16 #include "../ssl/packet_locl.h"
17
18 /* Should we fragment records or not? 0 = no, !0 = yes*/
19 static int fragment = 0;
20
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);
28
29 /* Choose a sufficiently large type likely to be unused for this custom BIO */
30 # define BIO_TYPE_ASYNC_FILTER  (0x80 | BIO_TYPE_FILTER)
31
32 static BIO_METHOD *methods_async = NULL;
33
34 struct async_ctrs {
35     unsigned int rctr;
36     unsigned int wctr;
37 };
38
39 static const BIO_METHOD *bio_f_async_filter()
40 {
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))
51             return NULL;
52     }
53     return methods_async;
54 }
55
56 static int async_new(BIO *bio)
57 {
58     struct async_ctrs *ctrs;
59
60     ctrs = OPENSSL_zalloc(sizeof(struct async_ctrs));
61     if (ctrs == NULL)
62         return 0;
63
64     BIO_set_data(bio, ctrs);
65     BIO_set_init(bio, 1);
66     return 1;
67 }
68
69 static int async_free(BIO *bio)
70 {
71     struct async_ctrs *ctrs;
72
73     if (bio == NULL)
74         return 0;
75     ctrs = BIO_get_data(bio);
76     OPENSSL_free(ctrs);
77     BIO_set_data(bio, NULL);
78     BIO_set_init(bio, 0);
79
80     return 1;
81 }
82
83 static int async_read(BIO *bio, char *out, int outl)
84 {
85     struct async_ctrs *ctrs;
86     int ret = 0;
87     BIO *next = BIO_next(bio);
88
89     if (outl <= 0)
90         return 0;
91     if (next == NULL)
92         return 0;
93
94     ctrs = BIO_get_data(bio);
95
96     BIO_clear_retry_flags(bio);
97
98     if (ctrs->rctr > 0) {
99         ret = BIO_read(next, out, 1);
100         if (ret <= 0 && BIO_should_read(next))
101             BIO_set_retry_read(bio);
102         ctrs->rctr = 0;
103     } else {
104         ctrs->rctr++;
105         BIO_set_retry_read(bio);
106     }
107
108     return ret;
109 }
110
111 #define MIN_RECORD_LEN  6
112
113 #define CONTENTTYPEPOS  0
114 #define VERSIONHIPOS    1
115 #define VERSIONLOPOS    2
116 #define DATAPOS         5
117
118 static int async_write(BIO *bio, const char *in, int inl)
119 {
120     struct async_ctrs *ctrs;
121     int ret = 0;
122     size_t written = 0;
123     BIO *next = BIO_next(bio);
124
125     if (inl <= 0)
126         return 0;
127     if (next == NULL)
128         return 0;
129
130     ctrs = BIO_get_data(bio);
131
132     BIO_clear_retry_flags(bio);
133
134     if (ctrs->wctr > 0) {
135         ctrs->wctr = 0;
136         if (fragment) {
137             PACKET pkt;
138
139             if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
140                 abort();
141
142             while (PACKET_remaining(&pkt) > 0) {
143                 PACKET payload;
144                 unsigned int contenttype, versionhi, versionlo, data;
145
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))
150                     abort();
151
152                 /* Pretend we wrote out the record header */
153                 written += SSL3_RT_HEADER_LENGTH;
154
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
158                      */
159                     char smallrec[MIN_RECORD_LEN] = {
160                         0, /* Content type */
161                         0, /* Version hi */
162                         0, /* Version lo */
163                         0, /* Length hi */
164                         1, /* Length lo */
165                         0  /* Data */
166                     };
167
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);
173                     if (ret <= 0)
174                         abort();
175                     written++;
176                 }
177                 /*
178                  * We can't fragment anything after the CCS, otherwise we
179                  * get a bad record MAC
180                  */
181                 if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC) {
182                     fragment = 0;
183                     break;
184                 }
185             }
186         }
187         /* Write any data we have left after fragmenting */
188         ret = 0;
189         if ((int)written < inl) {
190             ret = BIO_write(next, in + written , inl - written);
191         }
192
193         if (ret <= 0 && BIO_should_write(next))
194             BIO_set_retry_write(bio);
195         else
196             ret += written;
197     } else {
198         ctrs->wctr++;
199         BIO_set_retry_write(bio);
200     }
201
202     return ret;
203 }
204
205 static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
206 {
207     long ret;
208     BIO *next = BIO_next(bio);
209
210     if (next == NULL)
211         return 0;
212
213     switch (cmd) {
214     case BIO_CTRL_DUP:
215         ret = 0L;
216         break;
217     default:
218         ret = BIO_ctrl(next, cmd, num, ptr);
219         break;
220     }
221     return ret;
222 }
223
224 static int async_gets(BIO *bio, char *buf, int size)
225 {
226     /* We don't support this - not needed anyway */
227     return -1;
228 }
229
230 static int async_puts(BIO *bio, const char *str)
231 {
232     return async_write(bio, str, strlen(str));
233 }
234
235 #define MAXLOOPS    100000
236
237 int main(int argc, char *argv[])
238 {
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;
244     int test;
245
246     CRYPTO_set_mem_debug(1);
247     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
248
249     if (argc != 3) {
250         printf("Invalid argument count\n");
251         goto end;
252     }
253
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");
258         goto end;
259     }
260
261     if (SSL_CTX_use_certificate_file(serverctx, argv[1],
262                                      SSL_FILETYPE_PEM) <= 0) {
263         printf("Failed to load server certificate\n");
264         goto end;
265     }
266     if (SSL_CTX_use_PrivateKey_file(serverctx, argv[2],
267                                     SSL_FILETYPE_PEM) <= 0) {
268         printf("Failed to load server private key\n");
269     }
270     if (SSL_CTX_check_private_key(serverctx) <= 0) {
271         printf("Failed to check private key\n");
272         goto end;
273     }
274
275     /*
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
279      * CCS)
280      */
281     for (test = 1; test < 3; test++) {
282         abortctr = 0;
283         retc = rets = -1;
284         if (test == 2)
285             fragment = 1;
286
287         serverssl = SSL_new(serverctx);
288         clientssl = SSL_new(clientctx);
289         if (serverssl == NULL || clientssl == NULL) {
290             printf("Failed to create SSL object\n");
291             goto end;
292         }
293
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");
298             goto end;
299         }
300
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");
305             goto end;
306         }
307
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");
312             goto end;
313         }
314
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);
318
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);
322
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);
325
326         do {
327             err = SSL_ERROR_WANT_WRITE;
328             while (retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
329                 retc = SSL_connect(clientssl);
330                 if (retc <= 0)
331                     err = SSL_get_error(clientssl, retc);
332             }
333
334             if (retc <= 0 && err != SSL_ERROR_WANT_READ) {
335                 printf("Test %d failed: SSL_connect() failed %d, %d\n",
336                        test, retc, err);
337                 goto end;
338             }
339
340             err = SSL_ERROR_WANT_WRITE;
341             while (rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
342                 rets = SSL_accept(serverssl);
343                 if (rets <= 0)
344                     err = SSL_get_error(serverssl, rets);
345             }
346
347             if (rets <= 0 && err != SSL_ERROR_WANT_READ) {
348                 printf("Test %d failed: SSL_accept() failed %d, %d\n",
349                        test, retc, err);
350                 goto end;
351             }
352             if (++abortctr == MAXLOOPS) {
353                 printf("Test %d failed: No progress made\n", test);
354                 goto end;
355             }
356         } while (retc <=0 || rets <= 0);
357
358         /* Also frees the BIOs */
359         SSL_free(clientssl);
360         SSL_free(serverssl);
361         clientssl = serverssl = NULL;
362     }
363
364     printf("Test success\n");
365
366  end:
367     if (retc <= 0 || rets <= 0)
368         ERR_print_errors_fp(stderr);
369
370     SSL_free(clientssl);
371     SSL_free(serverssl);
372     SSL_CTX_free(clientctx);
373     SSL_CTX_free(serverctx);
374
375 # ifndef OPENSSL_NO_CRYPTO_MDEBUG
376     CRYPTO_mem_leaks_fp(stderr);
377 # endif
378
379     return (retc > 0 && rets > 0) ? 0 : 1;
380 }