Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / crypto / bio / bf_buff.c
1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "bio_local.h"
13 #include "internal/cryptlib.h"
14
15 static int buffer_write(BIO *h, const char *buf, int num);
16 static int buffer_read(BIO *h, char *buf, int size);
17 static int buffer_puts(BIO *h, const char *str);
18 static int buffer_gets(BIO *h, char *str, int size);
19 static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20 static int buffer_new(BIO *h);
21 static int buffer_free(BIO *data);
22 static long buffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
23 #define DEFAULT_BUFFER_SIZE     4096
24
25 static const BIO_METHOD methods_buffer = {
26     BIO_TYPE_BUFFER,
27     "buffer",
28     bwrite_conv,
29     buffer_write,
30     bread_conv,
31     buffer_read,
32     buffer_puts,
33     buffer_gets,
34     buffer_ctrl,
35     buffer_new,
36     buffer_free,
37     buffer_callback_ctrl,
38 };
39
40 const BIO_METHOD *BIO_f_buffer(void)
41 {
42     return &methods_buffer;
43 }
44
45 static int buffer_new(BIO *bi)
46 {
47     BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
48
49     if (ctx == NULL)
50         return 0;
51     ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
52     ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
53     if (ctx->ibuf == NULL) {
54         OPENSSL_free(ctx);
55         return 0;
56     }
57     ctx->obuf_size = DEFAULT_BUFFER_SIZE;
58     ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
59     if (ctx->obuf == NULL) {
60         OPENSSL_free(ctx->ibuf);
61         OPENSSL_free(ctx);
62         return 0;
63     }
64
65     bi->init = 1;
66     bi->ptr = (char *)ctx;
67     bi->flags = 0;
68     return 1;
69 }
70
71 static int buffer_free(BIO *a)
72 {
73     BIO_F_BUFFER_CTX *b;
74
75     if (a == NULL)
76         return 0;
77     b = (BIO_F_BUFFER_CTX *)a->ptr;
78     OPENSSL_free(b->ibuf);
79     OPENSSL_free(b->obuf);
80     OPENSSL_free(a->ptr);
81     a->ptr = NULL;
82     a->init = 0;
83     a->flags = 0;
84     return 1;
85 }
86
87 static int buffer_read(BIO *b, char *out, int outl)
88 {
89     int i, num = 0;
90     BIO_F_BUFFER_CTX *ctx;
91
92     if (out == NULL)
93         return 0;
94     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
95
96     if ((ctx == NULL) || (b->next_bio == NULL))
97         return 0;
98     num = 0;
99     BIO_clear_retry_flags(b);
100
101  start:
102     i = ctx->ibuf_len;
103     /* If there is stuff left over, grab it */
104     if (i != 0) {
105         if (i > outl)
106             i = outl;
107         memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
108         ctx->ibuf_off += i;
109         ctx->ibuf_len -= i;
110         num += i;
111         if (outl == i)
112             return num;
113         outl -= i;
114         out += i;
115     }
116
117     /*
118      * We may have done a partial read. try to do more. We have nothing in
119      * the buffer. If we get an error and have read some data, just return it
120      * and let them retry to get the error again. copy direct to parent
121      * address space
122      */
123     if (outl > ctx->ibuf_size) {
124         for (;;) {
125             i = BIO_read(b->next_bio, out, outl);
126             if (i <= 0) {
127                 BIO_copy_next_retry(b);
128                 if (i < 0)
129                     return ((num > 0) ? num : i);
130                 if (i == 0)
131                     return num;
132             }
133             num += i;
134             if (outl == i)
135                 return num;
136             out += i;
137             outl -= i;
138         }
139     }
140     /* else */
141
142     /* we are going to be doing some buffering */
143     i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
144     if (i <= 0) {
145         BIO_copy_next_retry(b);
146         if (i < 0)
147             return ((num > 0) ? num : i);
148         if (i == 0)
149             return num;
150     }
151     ctx->ibuf_off = 0;
152     ctx->ibuf_len = i;
153
154     /* Lets re-read using ourselves :-) */
155     goto start;
156 }
157
158 static int buffer_write(BIO *b, const char *in, int inl)
159 {
160     int i, num = 0;
161     BIO_F_BUFFER_CTX *ctx;
162
163     if ((in == NULL) || (inl <= 0))
164         return 0;
165     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
166     if ((ctx == NULL) || (b->next_bio == NULL))
167         return 0;
168
169     BIO_clear_retry_flags(b);
170  start:
171     i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
172     /* add to buffer and return */
173     if (i >= inl) {
174         memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
175         ctx->obuf_len += inl;
176         return (num + inl);
177     }
178     /* else */
179     /* stuff already in buffer, so add to it first, then flush */
180     if (ctx->obuf_len != 0) {
181         if (i > 0) {            /* lets fill it up if we can */
182             memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
183             in += i;
184             inl -= i;
185             num += i;
186             ctx->obuf_len += i;
187         }
188         /* we now have a full buffer needing flushing */
189         for (;;) {
190             i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
191                           ctx->obuf_len);
192             if (i <= 0) {
193                 BIO_copy_next_retry(b);
194
195                 if (i < 0)
196                     return ((num > 0) ? num : i);
197                 if (i == 0)
198                     return num;
199             }
200             ctx->obuf_off += i;
201             ctx->obuf_len -= i;
202             if (ctx->obuf_len == 0)
203                 break;
204         }
205     }
206     /*
207      * we only get here if the buffer has been flushed and we still have
208      * stuff to write
209      */
210     ctx->obuf_off = 0;
211
212     /* we now have inl bytes to write */
213     while (inl >= ctx->obuf_size) {
214         i = BIO_write(b->next_bio, in, inl);
215         if (i <= 0) {
216             BIO_copy_next_retry(b);
217             if (i < 0)
218                 return ((num > 0) ? num : i);
219             if (i == 0)
220                 return num;
221         }
222         num += i;
223         in += i;
224         inl -= i;
225         if (inl == 0)
226             return num;
227     }
228
229     /*
230      * copy the rest into the buffer since we have only a small amount left
231      */
232     goto start;
233 }
234
235 static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
236 {
237     BIO *dbio;
238     BIO_F_BUFFER_CTX *ctx;
239     long ret = 1;
240     char *p1, *p2;
241     int r, i, *ip;
242     int ibs, obs;
243
244     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
245
246     switch (cmd) {
247     case BIO_CTRL_RESET:
248         ctx->ibuf_off = 0;
249         ctx->ibuf_len = 0;
250         ctx->obuf_off = 0;
251         ctx->obuf_len = 0;
252         if (b->next_bio == NULL)
253             return 0;
254         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
255         break;
256     case BIO_CTRL_EOF:
257         if (ctx->ibuf_len > 0)
258             return 0;
259         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
260         break;
261     case BIO_CTRL_INFO:
262         ret = (long)ctx->obuf_len;
263         break;
264     case BIO_C_GET_BUFF_NUM_LINES:
265         ret = 0;
266         p1 = ctx->ibuf;
267         for (i = 0; i < ctx->ibuf_len; i++) {
268             if (p1[ctx->ibuf_off + i] == '\n')
269                 ret++;
270         }
271         break;
272     case BIO_CTRL_WPENDING:
273         ret = (long)ctx->obuf_len;
274         if (ret == 0) {
275             if (b->next_bio == NULL)
276                 return 0;
277             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
278         }
279         break;
280     case BIO_CTRL_PENDING:
281         ret = (long)ctx->ibuf_len;
282         if (ret == 0) {
283             if (b->next_bio == NULL)
284                 return 0;
285             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
286         }
287         break;
288     case BIO_C_SET_BUFF_READ_DATA:
289         if (num > ctx->ibuf_size) {
290             if (num <= 0)
291                 return 0;
292             p1 = OPENSSL_malloc((size_t)num);
293             if (p1 == NULL)
294                 goto malloc_error;
295             OPENSSL_free(ctx->ibuf);
296             ctx->ibuf = p1;
297         }
298         ctx->ibuf_off = 0;
299         ctx->ibuf_len = (int)num;
300         memcpy(ctx->ibuf, ptr, (int)num);
301         ret = 1;
302         break;
303     case BIO_C_SET_BUFF_SIZE:
304         if (ptr != NULL) {
305             ip = (int *)ptr;
306             if (*ip == 0) {
307                 ibs = (int)num;
308                 obs = ctx->obuf_size;
309             } else {            /* if (*ip == 1) */
310
311                 ibs = ctx->ibuf_size;
312                 obs = (int)num;
313             }
314         } else {
315             ibs = (int)num;
316             obs = (int)num;
317         }
318         p1 = ctx->ibuf;
319         p2 = ctx->obuf;
320         if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
321             if (num <= 0)
322                 return 0;
323             p1 = OPENSSL_malloc((size_t)num);
324             if (p1 == NULL)
325                 goto malloc_error;
326         }
327         if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
328             p2 = OPENSSL_malloc((size_t)num);
329             if (p2 == NULL) {
330                 if (p1 != ctx->ibuf)
331                     OPENSSL_free(p1);
332                 goto malloc_error;
333             }
334         }
335         if (ctx->ibuf != p1) {
336             OPENSSL_free(ctx->ibuf);
337             ctx->ibuf = p1;
338             ctx->ibuf_off = 0;
339             ctx->ibuf_len = 0;
340             ctx->ibuf_size = ibs;
341         }
342         if (ctx->obuf != p2) {
343             OPENSSL_free(ctx->obuf);
344             ctx->obuf = p2;
345             ctx->obuf_off = 0;
346             ctx->obuf_len = 0;
347             ctx->obuf_size = obs;
348         }
349         break;
350     case BIO_C_DO_STATE_MACHINE:
351         if (b->next_bio == NULL)
352             return 0;
353         BIO_clear_retry_flags(b);
354         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
355         BIO_copy_next_retry(b);
356         break;
357
358     case BIO_CTRL_FLUSH:
359         if (b->next_bio == NULL)
360             return 0;
361         if (ctx->obuf_len <= 0) {
362             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
363             break;
364         }
365
366         for (;;) {
367             BIO_clear_retry_flags(b);
368             if (ctx->obuf_len > 0) {
369                 r = BIO_write(b->next_bio,
370                               &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
371                 BIO_copy_next_retry(b);
372                 if (r <= 0)
373                     return (long)r;
374                 ctx->obuf_off += r;
375                 ctx->obuf_len -= r;
376             } else {
377                 ctx->obuf_len = 0;
378                 ctx->obuf_off = 0;
379                 break;
380             }
381         }
382         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
383         break;
384     case BIO_CTRL_DUP:
385         dbio = (BIO *)ptr;
386         if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
387             !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
388             ret = 0;
389         break;
390     case BIO_CTRL_PEEK:
391         /* Ensure there's stuff in the input buffer */
392         {
393             char fake_buf[1];
394             (void)buffer_read(b, fake_buf, 0);
395         }
396         if (num > ctx->ibuf_len)
397             num = ctx->ibuf_len;
398         memcpy(ptr, &(ctx->ibuf[ctx->ibuf_off]), num);
399         ret = num;
400         break;
401     default:
402         if (b->next_bio == NULL)
403             return 0;
404         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
405         break;
406     }
407     return ret;
408  malloc_error:
409     ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
410     return 0;
411 }
412
413 static long buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
414 {
415     if (b->next_bio == NULL)
416         return 0;
417     return BIO_callback_ctrl(b->next_bio, cmd, fp);
418 }
419
420 static int buffer_gets(BIO *b, char *buf, int size)
421 {
422     BIO_F_BUFFER_CTX *ctx;
423     int num = 0, i, flag;
424     char *p;
425
426     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
427     size--;                     /* reserve space for a '\0' */
428     BIO_clear_retry_flags(b);
429
430     for (;;) {
431         if (ctx->ibuf_len > 0) {
432             p = &(ctx->ibuf[ctx->ibuf_off]);
433             flag = 0;
434             for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
435                 *(buf++) = p[i];
436                 if (p[i] == '\n') {
437                     flag = 1;
438                     i++;
439                     break;
440                 }
441             }
442             num += i;
443             size -= i;
444             ctx->ibuf_len -= i;
445             ctx->ibuf_off += i;
446             if (flag || size == 0) {
447                 *buf = '\0';
448                 return num;
449             }
450         } else {                /* read another chunk */
451
452             i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
453             if (i <= 0) {
454                 BIO_copy_next_retry(b);
455                 *buf = '\0';
456                 if (i < 0)
457                     return ((num > 0) ? num : i);
458                 if (i == 0)
459                     return num;
460             }
461             ctx->ibuf_len = i;
462             ctx->ibuf_off = 0;
463         }
464     }
465 }
466
467 static int buffer_puts(BIO *b, const char *str)
468 {
469     return buffer_write(b, str, strlen(str));
470 }