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