xref: /openssl/crypto/bio/bf_buff.c (revision da1c088f)
1 /*
2  * Copyright 1995-2023 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 
BIO_f_buffer(void)40 const BIO_METHOD *BIO_f_buffer(void)
41 {
42     return &methods_buffer;
43 }
44 
buffer_new(BIO * bi)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 
buffer_free(BIO * a)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 
buffer_read(BIO * b,char * out,int outl)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 
buffer_write(BIO * b,const char * in,int inl)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 
buffer_ctrl(BIO * b,int cmd,long num,void * ptr)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                 return 0;
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                 return 0;
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                 return 0;
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             BIO_copy_next_retry(b);
364             break;
365         }
366 
367         for (;;) {
368             BIO_clear_retry_flags(b);
369             if (ctx->obuf_len > 0) {
370                 r = BIO_write(b->next_bio,
371                               &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
372                 BIO_copy_next_retry(b);
373                 if (r <= 0)
374                     return (long)r;
375                 ctx->obuf_off += r;
376                 ctx->obuf_len -= r;
377             } else {
378                 ctx->obuf_len = 0;
379                 ctx->obuf_off = 0;
380                 break;
381             }
382         }
383         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
384         BIO_copy_next_retry(b);
385         break;
386     case BIO_CTRL_DUP:
387         dbio = (BIO *)ptr;
388         if (BIO_set_read_buffer_size(dbio, ctx->ibuf_size) <= 0 ||
389             BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0)
390             ret = 0;
391         break;
392     case BIO_CTRL_PEEK:
393         /* Ensure there's stuff in the input buffer */
394         {
395             char fake_buf[1];
396             (void)buffer_read(b, fake_buf, 0);
397         }
398         if (num > ctx->ibuf_len)
399             num = ctx->ibuf_len;
400         memcpy(ptr, &(ctx->ibuf[ctx->ibuf_off]), num);
401         ret = num;
402         break;
403     default:
404         if (b->next_bio == NULL)
405             return 0;
406         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
407         break;
408     }
409     return ret;
410 }
411 
buffer_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)412 static long buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
413 {
414     if (b->next_bio == NULL)
415         return 0;
416     return BIO_callback_ctrl(b->next_bio, cmd, fp);
417 }
418 
buffer_gets(BIO * b,char * buf,int size)419 static int buffer_gets(BIO *b, char *buf, int size)
420 {
421     BIO_F_BUFFER_CTX *ctx;
422     int num = 0, i, flag;
423     char *p;
424 
425     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
426     size--;                     /* reserve space for a '\0' */
427     BIO_clear_retry_flags(b);
428 
429     for (;;) {
430         if (ctx->ibuf_len > 0) {
431             p = &(ctx->ibuf[ctx->ibuf_off]);
432             flag = 0;
433             for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
434                 *(buf++) = p[i];
435                 if (p[i] == '\n') {
436                     flag = 1;
437                     i++;
438                     break;
439                 }
440             }
441             num += i;
442             size -= i;
443             ctx->ibuf_len -= i;
444             ctx->ibuf_off += i;
445             if (flag || size == 0) {
446                 *buf = '\0';
447                 return num;
448             }
449         } else {                /* read another chunk */
450 
451             i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
452             if (i <= 0) {
453                 BIO_copy_next_retry(b);
454                 *buf = '\0';
455                 if (i < 0)
456                     return ((num > 0) ? num : i);
457                 if (i == 0)
458                     return num;
459             }
460             ctx->ibuf_len = i;
461             ctx->ibuf_off = 0;
462         }
463     }
464 }
465 
buffer_puts(BIO * b,const char * str)466 static int buffer_puts(BIO *b, const char *str)
467 {
468     return buffer_write(b, str, strlen(str));
469 }
470