Create BIO_read_ex() which handles size_t arguments
[oweals/openssl.git] / crypto / bio / bf_lbuf.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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_lcl.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/evp.h>
15
16 static int linebuffer_write(BIO *h, const char *buf, int num);
17 static int linebuffer_read(BIO *h, char *buf, int size);
18 static int linebuffer_puts(BIO *h, const char *str);
19 static int linebuffer_gets(BIO *h, char *str, int size);
20 static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
21 static int linebuffer_new(BIO *h);
22 static int linebuffer_free(BIO *data);
23 static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
24
25 /* A 10k maximum should be enough for most purposes */
26 #define DEFAULT_LINEBUFFER_SIZE 1024*10
27
28 /* #define DEBUG */
29
30 static const BIO_METHOD methods_linebuffer = {
31     BIO_TYPE_LINEBUFFER,
32     "linebuffer",
33     linebuffer_write,
34     /* TODO: Convert to new style read function */
35     bread_conv,
36     linebuffer_read,
37     linebuffer_puts,
38     linebuffer_gets,
39     linebuffer_ctrl,
40     linebuffer_new,
41     linebuffer_free,
42     linebuffer_callback_ctrl,
43 };
44
45 const BIO_METHOD *BIO_f_linebuffer(void)
46 {
47     return (&methods_linebuffer);
48 }
49
50 typedef struct bio_linebuffer_ctx_struct {
51     char *obuf;                 /* the output char array */
52     int obuf_size;              /* how big is the output buffer */
53     int obuf_len;               /* how many bytes are in it */
54 } BIO_LINEBUFFER_CTX;
55
56 static int linebuffer_new(BIO *bi)
57 {
58     BIO_LINEBUFFER_CTX *ctx;
59
60     ctx = OPENSSL_malloc(sizeof(*ctx));
61     if (ctx == NULL)
62         return (0);
63     ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
64     if (ctx->obuf == NULL) {
65         OPENSSL_free(ctx);
66         return (0);
67     }
68     ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
69     ctx->obuf_len = 0;
70
71     bi->init = 1;
72     bi->ptr = (char *)ctx;
73     bi->flags = 0;
74     return (1);
75 }
76
77 static int linebuffer_free(BIO *a)
78 {
79     BIO_LINEBUFFER_CTX *b;
80
81     if (a == NULL)
82         return (0);
83     b = (BIO_LINEBUFFER_CTX *)a->ptr;
84     OPENSSL_free(b->obuf);
85     OPENSSL_free(a->ptr);
86     a->ptr = NULL;
87     a->init = 0;
88     a->flags = 0;
89     return (1);
90 }
91
92 static int linebuffer_read(BIO *b, char *out, int outl)
93 {
94     int ret = 0;
95
96     if (out == NULL)
97         return (0);
98     if (b->next_bio == NULL)
99         return (0);
100     ret = BIO_read(b->next_bio, out, outl);
101     BIO_clear_retry_flags(b);
102     BIO_copy_next_retry(b);
103     return (ret);
104 }
105
106 static int linebuffer_write(BIO *b, const char *in, int inl)
107 {
108     int i, num = 0, foundnl;
109     BIO_LINEBUFFER_CTX *ctx;
110
111     if ((in == NULL) || (inl <= 0))
112         return (0);
113     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
114     if ((ctx == NULL) || (b->next_bio == NULL))
115         return (0);
116
117     BIO_clear_retry_flags(b);
118
119     do {
120         const char *p;
121
122         for (p = in; p < in + inl && *p != '\n'; p++) ;
123         if (*p == '\n') {
124             p++;
125             foundnl = 1;
126         } else
127             foundnl = 0;
128
129         /*
130          * If a NL was found and we already have text in the save buffer,
131          * concatenate them and write
132          */
133         while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
134                && ctx->obuf_len > 0) {
135             int orig_olen = ctx->obuf_len;
136
137             i = ctx->obuf_size - ctx->obuf_len;
138             if (p - in > 0) {
139                 if (i >= p - in) {
140                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
141                     ctx->obuf_len += p - in;
142                     inl -= p - in;
143                     num += p - in;
144                     in = p;
145                 } else {
146                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
147                     ctx->obuf_len += i;
148                     inl -= i;
149                     in += i;
150                     num += i;
151                 }
152             }
153             i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
154             if (i <= 0) {
155                 ctx->obuf_len = orig_olen;
156                 BIO_copy_next_retry(b);
157
158                 if (i < 0)
159                     return ((num > 0) ? num : i);
160                 if (i == 0)
161                     return (num);
162             }
163             if (i < ctx->obuf_len)
164                 memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
165             ctx->obuf_len -= i;
166         }
167
168         /*
169          * Now that the save buffer is emptied, let's write the input buffer
170          * if a NL was found and there is anything to write.
171          */
172         if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
173             i = BIO_write(b->next_bio, in, p - in);
174             if (i <= 0) {
175                 BIO_copy_next_retry(b);
176                 if (i < 0)
177                     return ((num > 0) ? num : i);
178                 if (i == 0)
179                     return (num);
180             }
181             num += i;
182             in += i;
183             inl -= i;
184         }
185     }
186     while (foundnl && inl > 0);
187     /*
188      * We've written as much as we can.  The rest of the input buffer, if
189      * any, is text that doesn't and with a NL and therefore needs to be
190      * saved for the next trip.
191      */
192     if (inl > 0) {
193         memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
194         ctx->obuf_len += inl;
195         num += inl;
196     }
197     return num;
198 }
199
200 static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
201 {
202     BIO *dbio;
203     BIO_LINEBUFFER_CTX *ctx;
204     long ret = 1;
205     char *p;
206     int r;
207     int obs;
208
209     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
210
211     switch (cmd) {
212     case BIO_CTRL_RESET:
213         ctx->obuf_len = 0;
214         if (b->next_bio == NULL)
215             return (0);
216         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
217         break;
218     case BIO_CTRL_INFO:
219         ret = (long)ctx->obuf_len;
220         break;
221     case BIO_CTRL_WPENDING:
222         ret = (long)ctx->obuf_len;
223         if (ret == 0) {
224             if (b->next_bio == NULL)
225                 return (0);
226             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
227         }
228         break;
229     case BIO_C_SET_BUFF_SIZE:
230         obs = (int)num;
231         p = ctx->obuf;
232         if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
233             p = OPENSSL_malloc((int)num);
234             if (p == NULL)
235                 goto malloc_error;
236         }
237         if (ctx->obuf != p) {
238             if (ctx->obuf_len > obs) {
239                 ctx->obuf_len = obs;
240             }
241             memcpy(p, ctx->obuf, ctx->obuf_len);
242             OPENSSL_free(ctx->obuf);
243             ctx->obuf = p;
244             ctx->obuf_size = obs;
245         }
246         break;
247     case BIO_C_DO_STATE_MACHINE:
248         if (b->next_bio == NULL)
249             return (0);
250         BIO_clear_retry_flags(b);
251         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
252         BIO_copy_next_retry(b);
253         break;
254
255     case BIO_CTRL_FLUSH:
256         if (b->next_bio == NULL)
257             return (0);
258         if (ctx->obuf_len <= 0) {
259             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
260             break;
261         }
262
263         for (;;) {
264             BIO_clear_retry_flags(b);
265             if (ctx->obuf_len > 0) {
266                 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
267                 BIO_copy_next_retry(b);
268                 if (r <= 0)
269                     return ((long)r);
270                 if (r < ctx->obuf_len)
271                     memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
272                 ctx->obuf_len -= r;
273             } else {
274                 ctx->obuf_len = 0;
275                 break;
276             }
277         }
278         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
279         break;
280     case BIO_CTRL_DUP:
281         dbio = (BIO *)ptr;
282         if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
283             ret = 0;
284         break;
285     default:
286         if (b->next_bio == NULL)
287             return (0);
288         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
289         break;
290     }
291     return (ret);
292  malloc_error:
293     BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
294     return (0);
295 }
296
297 static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
298 {
299     long ret = 1;
300
301     if (b->next_bio == NULL)
302         return (0);
303     switch (cmd) {
304     default:
305         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
306         break;
307     }
308     return (ret);
309 }
310
311 static int linebuffer_gets(BIO *b, char *buf, int size)
312 {
313     if (b->next_bio == NULL)
314         return (0);
315     return (BIO_gets(b->next_bio, buf, size));
316 }
317
318 static int linebuffer_puts(BIO *b, const char *str)
319 {
320     return (linebuffer_write(b, str, strlen(str)));
321 }