1 /* $OpenBSD: bf_buff.c,v 1.28 2023/07/05 21:23:37 beck Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59 #include <errno.h>
60 #include <stdio.h>
61 #include <string.h>
62
63 #include <openssl/bio.h>
64 #include <openssl/err.h>
65
66 #include "bio_local.h"
67
68 static int buffer_write(BIO *h, const char *buf, int num);
69 static int buffer_read(BIO *h, char *buf, int size);
70 static int buffer_puts(BIO *h, const char *str);
71 static int buffer_gets(BIO *h, char *str, int size);
72 static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
73 static int buffer_new(BIO *h);
74 static int buffer_free(BIO *data);
75 static long buffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
76 #define DEFAULT_BUFFER_SIZE 4096
77
78 static const BIO_METHOD methods_buffer = {
79 .type = BIO_TYPE_BUFFER,
80 .name = "buffer",
81 .bwrite = buffer_write,
82 .bread = buffer_read,
83 .bputs = buffer_puts,
84 .bgets = buffer_gets,
85 .ctrl = buffer_ctrl,
86 .create = buffer_new,
87 .destroy = buffer_free,
88 .callback_ctrl = buffer_callback_ctrl
89 };
90
91 const BIO_METHOD *
BIO_f_buffer(void)92 BIO_f_buffer(void)
93 {
94 return (&methods_buffer);
95 }
96 LCRYPTO_ALIAS(BIO_f_buffer);
97
98 static int
buffer_new(BIO * bi)99 buffer_new(BIO *bi)
100 {
101 BIO_F_BUFFER_CTX *ctx;
102
103 ctx = malloc(sizeof(BIO_F_BUFFER_CTX));
104 if (ctx == NULL)
105 return (0);
106 ctx->ibuf = malloc(DEFAULT_BUFFER_SIZE);
107 if (ctx->ibuf == NULL) {
108 free(ctx);
109 return (0);
110 }
111 ctx->obuf = malloc(DEFAULT_BUFFER_SIZE);
112 if (ctx->obuf == NULL) {
113 free(ctx->ibuf);
114 free(ctx);
115 return (0);
116 }
117 ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
118 ctx->obuf_size = DEFAULT_BUFFER_SIZE;
119 ctx->ibuf_len = 0;
120 ctx->ibuf_off = 0;
121 ctx->obuf_len = 0;
122 ctx->obuf_off = 0;
123
124 bi->init = 1;
125 bi->ptr = (char *)ctx;
126 bi->flags = 0;
127 return (1);
128 }
129
130 static int
buffer_free(BIO * a)131 buffer_free(BIO *a)
132 {
133 BIO_F_BUFFER_CTX *b;
134
135 if (a == NULL)
136 return (0);
137 b = (BIO_F_BUFFER_CTX *)a->ptr;
138 free(b->ibuf);
139 free(b->obuf);
140 free(a->ptr);
141 a->ptr = NULL;
142 a->init = 0;
143 a->flags = 0;
144 return (1);
145 }
146
147 static int
buffer_read(BIO * b,char * out,int outl)148 buffer_read(BIO *b, char *out, int outl)
149 {
150 int i, num = 0;
151 BIO_F_BUFFER_CTX *ctx;
152
153 if (out == NULL)
154 return (0);
155 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
156
157 if ((ctx == NULL) || (b->next_bio == NULL))
158 return (0);
159 num = 0;
160 BIO_clear_retry_flags(b);
161
162 start:
163 i = ctx->ibuf_len;
164 /* If there is stuff left over, grab it */
165 if (i != 0) {
166 if (i > outl)
167 i = outl;
168 memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
169 ctx->ibuf_off += i;
170 ctx->ibuf_len -= i;
171 num += i;
172 if (outl == i)
173 return (num);
174 outl -= i;
175 out += i;
176 }
177
178 /* We may have done a partial read. try to do more.
179 * We have nothing in the buffer.
180 * If we get an error and have read some data, just return it
181 * and let them retry to get the error again.
182 * copy direct to parent address space */
183 if (outl > ctx->ibuf_size) {
184 for (;;) {
185 i = BIO_read(b->next_bio, out, outl);
186 if (i <= 0) {
187 BIO_copy_next_retry(b);
188 if (i < 0)
189 return ((num > 0) ? num : i);
190 if (i == 0)
191 return (num);
192 }
193 num += i;
194 if (outl == i)
195 return (num);
196 out += i;
197 outl -= i;
198 }
199 }
200 /* else */
201
202 /* we are going to be doing some buffering */
203 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
204 if (i <= 0) {
205 BIO_copy_next_retry(b);
206 if (i < 0)
207 return ((num > 0) ? num : i);
208 if (i == 0)
209 return (num);
210 }
211 ctx->ibuf_off = 0;
212 ctx->ibuf_len = i;
213
214 /* Lets re-read using ourselves :-) */
215 goto start;
216 }
217
218 static int
buffer_write(BIO * b,const char * in,int inl)219 buffer_write(BIO *b, const char *in, int inl)
220 {
221 int i, num = 0;
222 BIO_F_BUFFER_CTX *ctx;
223
224 if ((in == NULL) || (inl <= 0))
225 return (0);
226 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
227 if ((ctx == NULL) || (b->next_bio == NULL))
228 return (0);
229
230 BIO_clear_retry_flags(b);
231 start:
232 i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
233 /* add to buffer and return */
234 if (i >= inl) {
235 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
236 ctx->obuf_len += inl;
237 return (num + inl);
238 }
239 /* else */
240 /* stuff already in buffer, so add to it first, then flush */
241 if (ctx->obuf_len != 0) {
242 if (i > 0) /* lets fill it up if we can */
243 {
244 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
245 in += i;
246 inl -= i;
247 num += i;
248 ctx->obuf_len += i;
249 }
250 /* we now have a full buffer needing flushing */
251 for (;;) {
252 i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
253 ctx->obuf_len);
254 if (i <= 0) {
255 BIO_copy_next_retry(b);
256
257 if (i < 0)
258 return ((num > 0) ? num : i);
259 if (i == 0)
260 return (num);
261 }
262 ctx->obuf_off += i;
263 ctx->obuf_len -= i;
264 if (ctx->obuf_len == 0)
265 break;
266 }
267 }
268 /* we only get here if the buffer has been flushed and we
269 * still have stuff to write */
270 ctx->obuf_off = 0;
271
272 /* we now have inl bytes to write */
273 while (inl >= ctx->obuf_size) {
274 i = BIO_write(b->next_bio, in, inl);
275 if (i <= 0) {
276 BIO_copy_next_retry(b);
277 if (i < 0)
278 return ((num > 0) ? num : i);
279 if (i == 0)
280 return (num);
281 }
282 num += i;
283 in += i;
284 inl -= i;
285 if (inl == 0)
286 return (num);
287 }
288
289 /* copy the rest into the buffer since we have only a small
290 * amount left */
291 goto start;
292 }
293
294 static long
buffer_ctrl(BIO * b,int cmd,long num,void * ptr)295 buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
296 {
297 BIO *dbio;
298 BIO_F_BUFFER_CTX *ctx;
299 long ret = 1;
300 char *p1, *p2;
301 int r, i, *ip;
302 int ibs, obs;
303
304 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
305
306 switch (cmd) {
307 case BIO_CTRL_RESET:
308 ctx->ibuf_off = 0;
309 ctx->ibuf_len = 0;
310 ctx->obuf_off = 0;
311 ctx->obuf_len = 0;
312 if (b->next_bio == NULL)
313 return (0);
314 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
315 break;
316 case BIO_CTRL_INFO:
317 ret = (long)ctx->obuf_len;
318 break;
319 case BIO_C_GET_BUFF_NUM_LINES:
320 ret = 0;
321 p1 = ctx->ibuf;
322 for (i = 0; i < ctx->ibuf_len; i++) {
323 if (p1[ctx->ibuf_off + i] == '\n')
324 ret++;
325 }
326 break;
327 case BIO_CTRL_WPENDING:
328 ret = (long)ctx->obuf_len;
329 if (ret == 0) {
330 if (b->next_bio == NULL)
331 return (0);
332 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
333 }
334 break;
335 case BIO_CTRL_PENDING:
336 ret = (long)ctx->ibuf_len;
337 if (ret == 0) {
338 if (b->next_bio == NULL)
339 return (0);
340 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
341 }
342 break;
343 case BIO_C_SET_BUFF_READ_DATA:
344 if (num > ctx->ibuf_size) {
345 p1 = malloc(num);
346 if (p1 == NULL)
347 goto malloc_error;
348 free(ctx->ibuf);
349 ctx->ibuf = p1;
350 }
351 ctx->ibuf_off = 0;
352 ctx->ibuf_len = (int)num;
353 memcpy(ctx->ibuf, ptr, num);
354 ret = 1;
355 break;
356 case BIO_C_SET_BUFF_SIZE:
357 if (ptr != NULL) {
358 ip = (int *)ptr;
359 if (*ip == 0) {
360 ibs = (int)num;
361 obs = ctx->obuf_size;
362 }
363 else /* if (*ip == 1) */
364 {
365 ibs = ctx->ibuf_size;
366 obs = (int)num;
367 }
368 } else {
369 ibs = (int)num;
370 obs = (int)num;
371 }
372 p1 = ctx->ibuf;
373 p2 = ctx->obuf;
374 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
375 p1 = malloc(num);
376 if (p1 == NULL)
377 goto malloc_error;
378 }
379 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
380 p2 = malloc(num);
381 if (p2 == NULL) {
382 if (p1 != ctx->ibuf)
383 free(p1);
384 goto malloc_error;
385 }
386 }
387 if (ctx->ibuf != p1) {
388 free(ctx->ibuf);
389 ctx->ibuf = p1;
390 ctx->ibuf_off = 0;
391 ctx->ibuf_len = 0;
392 ctx->ibuf_size = ibs;
393 }
394 if (ctx->obuf != p2) {
395 free(ctx->obuf);
396 ctx->obuf = p2;
397 ctx->obuf_off = 0;
398 ctx->obuf_len = 0;
399 ctx->obuf_size = obs;
400 }
401 break;
402 case BIO_C_DO_STATE_MACHINE:
403 if (b->next_bio == NULL)
404 return (0);
405 BIO_clear_retry_flags(b);
406 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
407 BIO_copy_next_retry(b);
408 break;
409
410 case BIO_CTRL_FLUSH:
411 if (b->next_bio == NULL)
412 return (0);
413 if (ctx->obuf_len <= 0) {
414 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
415 break;
416 }
417
418 for (;;) {
419 BIO_clear_retry_flags(b);
420 if (ctx->obuf_len > 0) {
421 r = BIO_write(b->next_bio,
422 &(ctx->obuf[ctx->obuf_off]),
423 ctx->obuf_len);
424 BIO_copy_next_retry(b);
425 if (r <= 0)
426 return ((long)r);
427 ctx->obuf_off += r;
428 ctx->obuf_len -= r;
429 } else {
430 ctx->obuf_len = 0;
431 ctx->obuf_off = 0;
432 break;
433 }
434 }
435 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
436 break;
437 case BIO_CTRL_DUP:
438 dbio = (BIO *)ptr;
439 if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
440 !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
441 ret = 0;
442 break;
443 default:
444 if (b->next_bio == NULL)
445 return (0);
446 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
447 break;
448 }
449 return (ret);
450 malloc_error:
451 BIOerror(ERR_R_MALLOC_FAILURE);
452 return (0);
453 }
454
455 static long
buffer_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)456 buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
457 {
458 long ret = 1;
459
460 if (b->next_bio == NULL)
461 return (0);
462 switch (cmd) {
463 default:
464 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
465 break;
466 }
467 return (ret);
468 }
469
470 static int
buffer_gets(BIO * b,char * buf,int size)471 buffer_gets(BIO *b, char *buf, int size)
472 {
473 BIO_F_BUFFER_CTX *ctx;
474 int num = 0, i, flag;
475 char *p;
476
477 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
478 size--; /* reserve space for a '\0' */
479 BIO_clear_retry_flags(b);
480
481 for (;;) {
482 if (ctx->ibuf_len > 0) {
483 p = &(ctx->ibuf[ctx->ibuf_off]);
484 flag = 0;
485 for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
486 *(buf++) = p[i];
487 if (p[i] == '\n') {
488 flag = 1;
489 i++;
490 break;
491 }
492 }
493 num += i;
494 size -= i;
495 ctx->ibuf_len -= i;
496 ctx->ibuf_off += i;
497 if (flag || size == 0) {
498 *buf = '\0';
499 return (num);
500 }
501 }
502 else /* read another chunk */
503 {
504 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
505 if (i <= 0) {
506 BIO_copy_next_retry(b);
507 *buf = '\0';
508 if (i < 0)
509 return ((num > 0) ? num : i);
510 if (i == 0)
511 return (num);
512 }
513 ctx->ibuf_len = i;
514 ctx->ibuf_off = 0;
515 }
516 }
517 }
518
519 static int
buffer_puts(BIO * b,const char * str)520 buffer_puts(BIO *b, const char *str)
521 {
522 return (buffer_write(b, str, strlen(str)));
523 }
524