xref: /dragonfly/crypto/libressl/crypto/bio/bf_buff.c (revision 6f5ec8b5)
1 /* $OpenBSD: bf_buff.c,v 1.27 2022/01/14 08:40:57 tb 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 *
92 BIO_f_buffer(void)
93 {
94 	return (&methods_buffer);
95 }
96 
97 static int
98 buffer_new(BIO *bi)
99 {
100 	BIO_F_BUFFER_CTX *ctx;
101 
102 	ctx = malloc(sizeof(BIO_F_BUFFER_CTX));
103 	if (ctx == NULL)
104 		return (0);
105 	ctx->ibuf = malloc(DEFAULT_BUFFER_SIZE);
106 	if (ctx->ibuf == NULL) {
107 		free(ctx);
108 		return (0);
109 	}
110 	ctx->obuf = malloc(DEFAULT_BUFFER_SIZE);
111 	if (ctx->obuf == NULL) {
112 		free(ctx->ibuf);
113 		free(ctx);
114 		return (0);
115 	}
116 	ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
117 	ctx->obuf_size = DEFAULT_BUFFER_SIZE;
118 	ctx->ibuf_len = 0;
119 	ctx->ibuf_off = 0;
120 	ctx->obuf_len = 0;
121 	ctx->obuf_off = 0;
122 
123 	bi->init = 1;
124 	bi->ptr = (char *)ctx;
125 	bi->flags = 0;
126 	return (1);
127 }
128 
129 static int
130 buffer_free(BIO *a)
131 {
132 	BIO_F_BUFFER_CTX *b;
133 
134 	if (a == NULL)
135 		return (0);
136 	b = (BIO_F_BUFFER_CTX *)a->ptr;
137 	free(b->ibuf);
138 	free(b->obuf);
139 	free(a->ptr);
140 	a->ptr = NULL;
141 	a->init = 0;
142 	a->flags = 0;
143 	return (1);
144 }
145 
146 static int
147 buffer_read(BIO *b, char *out, int outl)
148 {
149 	int i, num = 0;
150 	BIO_F_BUFFER_CTX *ctx;
151 
152 	if (out == NULL)
153 		return (0);
154 	ctx = (BIO_F_BUFFER_CTX *)b->ptr;
155 
156 	if ((ctx == NULL) || (b->next_bio == NULL))
157 		return (0);
158 	num = 0;
159 	BIO_clear_retry_flags(b);
160 
161 start:
162 	i = ctx->ibuf_len;
163 	/* If there is stuff left over, grab it */
164 	if (i != 0) {
165 		if (i > outl)
166 			i = outl;
167 		memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
168 		ctx->ibuf_off += i;
169 		ctx->ibuf_len -= i;
170 		num += i;
171 		if (outl == i)
172 			return (num);
173 		outl -= i;
174 		out += i;
175 	}
176 
177 	/* We may have done a partial read. try to do more.
178 	 * We have nothing in the buffer.
179 	 * If we get an error and have read some data, just return it
180 	 * and let them retry to get the error again.
181 	 * copy direct to parent address space */
182 	if (outl > ctx->ibuf_size) {
183 		for (;;) {
184 			i = BIO_read(b->next_bio, out, outl);
185 			if (i <= 0) {
186 				BIO_copy_next_retry(b);
187 				if (i < 0)
188 					return ((num > 0) ? num : i);
189 				if (i == 0)
190 					return (num);
191 			}
192 			num += i;
193 			if (outl == i)
194 				return (num);
195 			out += i;
196 			outl -= i;
197 		}
198 	}
199 	/* else */
200 
201 	/* we are going to be doing some buffering */
202 	i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
203 	if (i <= 0) {
204 		BIO_copy_next_retry(b);
205 		if (i < 0)
206 			return ((num > 0) ? num : i);
207 		if (i == 0)
208 			return (num);
209 	}
210 	ctx->ibuf_off = 0;
211 	ctx->ibuf_len = i;
212 
213 	/* Lets re-read using ourselves :-) */
214 	goto start;
215 }
216 
217 static int
218 buffer_write(BIO *b, const char *in, int inl)
219 {
220 	int i, num = 0;
221 	BIO_F_BUFFER_CTX *ctx;
222 
223 	if ((in == NULL) || (inl <= 0))
224 		return (0);
225 	ctx = (BIO_F_BUFFER_CTX *)b->ptr;
226 	if ((ctx == NULL) || (b->next_bio == NULL))
227 		return (0);
228 
229 	BIO_clear_retry_flags(b);
230 start:
231 	i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
232 	/* add to buffer and return */
233 	if (i >= inl) {
234 		memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
235 		ctx->obuf_len += inl;
236 		return (num + inl);
237 	}
238 	/* else */
239 	/* stuff already in buffer, so add to it first, then flush */
240 	if (ctx->obuf_len != 0) {
241 		if (i > 0) /* lets fill it up if we can */
242 		{
243 			memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
244 			in += i;
245 			inl -= i;
246 			num += i;
247 			ctx->obuf_len += i;
248 		}
249 		/* we now have a full buffer needing flushing */
250 		for (;;) {
251 			i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
252 			    ctx->obuf_len);
253 			if (i <= 0) {
254 				BIO_copy_next_retry(b);
255 
256 				if (i < 0)
257 					return ((num > 0) ? num : i);
258 				if (i == 0)
259 					return (num);
260 			}
261 			ctx->obuf_off += i;
262 			ctx->obuf_len -= i;
263 			if (ctx->obuf_len == 0)
264 				break;
265 		}
266 	}
267 	/* we only get here if the buffer has been flushed and we
268 	 * still have stuff to write */
269 	ctx->obuf_off = 0;
270 
271 	/* we now have inl bytes to write */
272 	while (inl >= ctx->obuf_size) {
273 		i = BIO_write(b->next_bio, in, inl);
274 		if (i <= 0) {
275 			BIO_copy_next_retry(b);
276 			if (i < 0)
277 				return ((num > 0) ? num : i);
278 			if (i == 0)
279 				return (num);
280 		}
281 		num += i;
282 		in += i;
283 		inl -= i;
284 		if (inl == 0)
285 			return (num);
286 	}
287 
288 	/* copy the rest into the buffer since we have only a small
289 	 * amount left */
290 	goto start;
291 }
292 
293 static long
294 buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
295 {
296 	BIO *dbio;
297 	BIO_F_BUFFER_CTX *ctx;
298 	long ret = 1;
299 	char *p1, *p2;
300 	int r, i, *ip;
301 	int ibs, obs;
302 
303 	ctx = (BIO_F_BUFFER_CTX *)b->ptr;
304 
305 	switch (cmd) {
306 	case BIO_CTRL_RESET:
307 		ctx->ibuf_off = 0;
308 		ctx->ibuf_len = 0;
309 		ctx->obuf_off = 0;
310 		ctx->obuf_len = 0;
311 		if (b->next_bio == NULL)
312 			return (0);
313 		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
314 		break;
315 	case BIO_CTRL_INFO:
316 		ret = (long)ctx->obuf_len;
317 		break;
318 	case BIO_C_GET_BUFF_NUM_LINES:
319 		ret = 0;
320 		p1 = ctx->ibuf;
321 		for (i = 0; i < ctx->ibuf_len; i++) {
322 			if (p1[ctx->ibuf_off + i] == '\n')
323 				ret++;
324 		}
325 		break;
326 	case BIO_CTRL_WPENDING:
327 		ret = (long)ctx->obuf_len;
328 		if (ret == 0) {
329 			if (b->next_bio == NULL)
330 				return (0);
331 			ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
332 		}
333 		break;
334 	case BIO_CTRL_PENDING:
335 		ret = (long)ctx->ibuf_len;
336 		if (ret == 0) {
337 			if (b->next_bio == NULL)
338 				return (0);
339 			ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
340 		}
341 		break;
342 	case BIO_C_SET_BUFF_READ_DATA:
343 		if (num > ctx->ibuf_size) {
344 			p1 = malloc(num);
345 			if (p1 == NULL)
346 				goto malloc_error;
347 			free(ctx->ibuf);
348 			ctx->ibuf = p1;
349 		}
350 		ctx->ibuf_off = 0;
351 		ctx->ibuf_len = (int)num;
352 		memcpy(ctx->ibuf, ptr, num);
353 		ret = 1;
354 		break;
355 	case BIO_C_SET_BUFF_SIZE:
356 		if (ptr != NULL) {
357 			ip = (int *)ptr;
358 			if (*ip == 0) {
359 				ibs = (int)num;
360 				obs = ctx->obuf_size;
361 			}
362 			else /* if (*ip == 1) */
363 			{
364 				ibs = ctx->ibuf_size;
365 				obs = (int)num;
366 			}
367 		} else {
368 			ibs = (int)num;
369 			obs = (int)num;
370 		}
371 		p1 = ctx->ibuf;
372 		p2 = ctx->obuf;
373 		if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
374 			p1 = malloc(num);
375 			if (p1 == NULL)
376 				goto malloc_error;
377 		}
378 		if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
379 			p2 = malloc(num);
380 			if (p2 == NULL) {
381 				if (p1 != ctx->ibuf)
382 					free(p1);
383 				goto malloc_error;
384 			}
385 		}
386 		if (ctx->ibuf != p1) {
387 			free(ctx->ibuf);
388 			ctx->ibuf = p1;
389 			ctx->ibuf_off = 0;
390 			ctx->ibuf_len = 0;
391 			ctx->ibuf_size = ibs;
392 		}
393 		if (ctx->obuf != p2) {
394 			free(ctx->obuf);
395 			ctx->obuf = p2;
396 			ctx->obuf_off = 0;
397 			ctx->obuf_len = 0;
398 			ctx->obuf_size = obs;
399 		}
400 		break;
401 	case BIO_C_DO_STATE_MACHINE:
402 		if (b->next_bio == NULL)
403 			return (0);
404 		BIO_clear_retry_flags(b);
405 		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
406 		BIO_copy_next_retry(b);
407 		break;
408 
409 	case BIO_CTRL_FLUSH:
410 		if (b->next_bio == NULL)
411 			return (0);
412 		if (ctx->obuf_len <= 0) {
413 			ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
414 			break;
415 		}
416 
417 		for (;;) {
418 			BIO_clear_retry_flags(b);
419 			if (ctx->obuf_len > 0) {
420 				r = BIO_write(b->next_bio,
421 				    &(ctx->obuf[ctx->obuf_off]),
422 				    ctx->obuf_len);
423 				BIO_copy_next_retry(b);
424 				if (r <= 0)
425 					return ((long)r);
426 				ctx->obuf_off += r;
427 				ctx->obuf_len -= r;
428 			} else {
429 				ctx->obuf_len = 0;
430 				ctx->obuf_off = 0;
431 				break;
432 			}
433 		}
434 		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
435 		break;
436 	case BIO_CTRL_DUP:
437 		dbio = (BIO *)ptr;
438 		if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
439 		    !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
440 			ret = 0;
441 		break;
442 	default:
443 		if (b->next_bio == NULL)
444 			return (0);
445 		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
446 		break;
447 	}
448 	return (ret);
449 malloc_error:
450 	BIOerror(ERR_R_MALLOC_FAILURE);
451 	return (0);
452 }
453 
454 static long
455 buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
456 {
457 	long ret = 1;
458 
459 	if (b->next_bio == NULL)
460 		return (0);
461 	switch (cmd) {
462 	default:
463 		ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
464 		break;
465 	}
466 	return (ret);
467 }
468 
469 static int
470 buffer_gets(BIO *b, char *buf, int size)
471 {
472 	BIO_F_BUFFER_CTX *ctx;
473 	int num = 0, i, flag;
474 	char *p;
475 
476 	ctx = (BIO_F_BUFFER_CTX *)b->ptr;
477 	size--; /* reserve space for a '\0' */
478 	BIO_clear_retry_flags(b);
479 
480 	for (;;) {
481 		if (ctx->ibuf_len > 0) {
482 			p = &(ctx->ibuf[ctx->ibuf_off]);
483 			flag = 0;
484 			for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
485 				*(buf++) = p[i];
486 				if (p[i] == '\n') {
487 					flag = 1;
488 					i++;
489 					break;
490 				}
491 			}
492 			num += i;
493 			size -= i;
494 			ctx->ibuf_len -= i;
495 			ctx->ibuf_off += i;
496 			if (flag || size == 0) {
497 				*buf = '\0';
498 				return (num);
499 			}
500 		}
501 		else	/* read another chunk */
502 		{
503 			i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
504 			if (i <= 0) {
505 				BIO_copy_next_retry(b);
506 				*buf = '\0';
507 				if (i < 0)
508 					return ((num > 0) ? num : i);
509 				if (i == 0)
510 					return (num);
511 			}
512 			ctx->ibuf_len = i;
513 			ctx->ibuf_off = 0;
514 		}
515 	}
516 }
517 
518 static int
519 buffer_puts(BIO *b, const char *str)
520 {
521 	return (buffer_write(b, str, strlen(str)));
522 }
523