xref: /openssl/crypto/bio/bf_lbuf.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 #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     bwrite_conv,
34     linebuffer_write,
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 
BIO_f_linebuffer(void)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 
linebuffer_new(BIO * bi)56 static int linebuffer_new(BIO *bi)
57 {
58     BIO_LINEBUFFER_CTX *ctx;
59 
60     if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL)
61         return 0;
62     ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
63     if (ctx->obuf == NULL) {
64         OPENSSL_free(ctx);
65         return 0;
66     }
67     ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
68     ctx->obuf_len = 0;
69 
70     bi->init = 1;
71     bi->ptr = (char *)ctx;
72     bi->flags = 0;
73     return 1;
74 }
75 
linebuffer_free(BIO * a)76 static int linebuffer_free(BIO *a)
77 {
78     BIO_LINEBUFFER_CTX *b;
79 
80     if (a == NULL)
81         return 0;
82     b = (BIO_LINEBUFFER_CTX *)a->ptr;
83     OPENSSL_free(b->obuf);
84     OPENSSL_free(a->ptr);
85     a->ptr = NULL;
86     a->init = 0;
87     a->flags = 0;
88     return 1;
89 }
90 
linebuffer_read(BIO * b,char * out,int outl)91 static int linebuffer_read(BIO *b, char *out, int outl)
92 {
93     int ret = 0;
94 
95     if (out == NULL)
96         return 0;
97     if (b->next_bio == NULL)
98         return 0;
99     ret = BIO_read(b->next_bio, out, outl);
100     BIO_clear_retry_flags(b);
101     BIO_copy_next_retry(b);
102     return ret;
103 }
104 
linebuffer_write(BIO * b,const char * in,int inl)105 static int linebuffer_write(BIO *b, const char *in, int inl)
106 {
107     int i, num = 0, foundnl;
108     BIO_LINEBUFFER_CTX *ctx;
109 
110     if ((in == NULL) || (inl <= 0))
111         return 0;
112     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
113     if ((ctx == NULL) || (b->next_bio == NULL))
114         return 0;
115 
116     BIO_clear_retry_flags(b);
117 
118     do {
119         const char *p;
120         char c;
121 
122         for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) ;
123         if (c == '\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 
linebuffer_ctrl(BIO * b,int cmd,long num,void * ptr)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         if (num > INT_MAX)
231             return 0;
232         obs = (int)num;
233         p = ctx->obuf;
234         if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
235             p = OPENSSL_malloc((size_t)obs);
236             if (p == NULL)
237                 return 0;
238         }
239         if (ctx->obuf != p) {
240             if (ctx->obuf_len > obs) {
241                 ctx->obuf_len = obs;
242             }
243             memcpy(p, ctx->obuf, ctx->obuf_len);
244             OPENSSL_free(ctx->obuf);
245             ctx->obuf = p;
246             ctx->obuf_size = obs;
247         }
248         break;
249     case BIO_C_DO_STATE_MACHINE:
250         if (b->next_bio == NULL)
251             return 0;
252         BIO_clear_retry_flags(b);
253         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
254         BIO_copy_next_retry(b);
255         break;
256 
257     case BIO_CTRL_FLUSH:
258         if (b->next_bio == NULL)
259             return 0;
260         if (ctx->obuf_len <= 0) {
261             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
262             BIO_copy_next_retry(b);
263             break;
264         }
265 
266         for (;;) {
267             BIO_clear_retry_flags(b);
268             if (ctx->obuf_len > 0) {
269                 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
270                 BIO_copy_next_retry(b);
271                 if (r <= 0)
272                     return (long)r;
273                 if (r < ctx->obuf_len)
274                     memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
275                 ctx->obuf_len -= r;
276             } else {
277                 ctx->obuf_len = 0;
278                 break;
279             }
280         }
281         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
282         BIO_copy_next_retry(b);
283         break;
284     case BIO_CTRL_DUP:
285         dbio = (BIO *)ptr;
286         if (BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0)
287             ret = 0;
288         break;
289     default:
290         if (b->next_bio == NULL)
291             return 0;
292         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
293         break;
294     }
295     return ret;
296 }
297 
linebuffer_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)298 static long linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
299 {
300     if (b->next_bio == NULL)
301         return 0;
302     return BIO_callback_ctrl(b->next_bio, cmd, fp);
303 }
304 
linebuffer_gets(BIO * b,char * buf,int size)305 static int linebuffer_gets(BIO *b, char *buf, int size)
306 {
307     if (b->next_bio == NULL)
308         return 0;
309     return BIO_gets(b->next_bio, buf, size);
310 }
311 
linebuffer_puts(BIO * b,const char * str)312 static int linebuffer_puts(BIO *b, const char *str)
313 {
314     return linebuffer_write(b, str, strlen(str));
315 }
316