xref: /openbsd-src/lib/libcrypto/bio/bio_lib.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /* $OpenBSD: bio_lib.c,v 1.44 2023/03/15 06:14:02 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 <limits.h>
61 #include <stdio.h>
62 
63 #include <openssl/bio.h>
64 #include <openssl/crypto.h>
65 #include <openssl/err.h>
66 #include <openssl/stack.h>
67 
68 #include "bio_local.h"
69 
70 /*
71  * Helper function to work out whether to call the new style callback or the old
72  * one, and translate between the two.
73  *
74  * This has a long return type for consistency with the old callback. Similarly
75  * for the "long" used for "inret"
76  */
77 static long
78 bio_call_callback(BIO *b, int oper, const char *argp, size_t len, int argi,
79     long argl, long inret, size_t *processed)
80 {
81 	long ret;
82 	int bareoper;
83 
84 	if (b->callback_ex != NULL)
85 		return b->callback_ex(b, oper, argp, len, argi, argl, inret,
86 		    processed);
87 
88 	/*
89 	 * We have an old style callback, so we will have to do nasty casts and
90 	 * check for overflows.
91 	 */
92 
93 	bareoper = oper & ~BIO_CB_RETURN;
94 
95 	if (bareoper == BIO_CB_READ || bareoper == BIO_CB_WRITE ||
96 	    bareoper == BIO_CB_GETS) {
97 		/* In this case len is set and should be used instead of argi. */
98 		if (len > INT_MAX)
99 			return -1;
100 		argi = (int)len;
101 	}
102 
103 	if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
104 		if (*processed > INT_MAX)
105 			return -1;
106 		inret = *processed;
107 	}
108 
109 	ret = b->callback(b, oper, argp, argi, argl, inret);
110 
111 	if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
112 		*processed = (size_t)ret;
113 		ret = 1;
114 	}
115 
116 	return ret;
117 }
118 
119 int
120 BIO_get_new_index(void)
121 {
122 	static int bio_type_index = BIO_TYPE_START;
123 	int index;
124 
125 	/* The index will collide with the BIO flag bits if it exceeds 255. */
126 	index = CRYPTO_add(&bio_type_index, 1, CRYPTO_LOCK_BIO);
127 	if (index > 255)
128 		return -1;
129 
130 	return index;
131 }
132 
133 BIO *
134 BIO_new(const BIO_METHOD *method)
135 {
136 	BIO *ret = NULL;
137 
138 	/* XXX calloc */
139 	ret = malloc(sizeof(BIO));
140 	if (ret == NULL) {
141 		BIOerror(ERR_R_MALLOC_FAILURE);
142 		return (NULL);
143 	}
144 	if (!BIO_set(ret, method)) {
145 		free(ret);
146 		ret = NULL;
147 	}
148 	return (ret);
149 }
150 
151 int
152 BIO_set(BIO *bio, const BIO_METHOD *method)
153 {
154 	bio->method = method;
155 	bio->callback = NULL;
156 	bio->callback_ex = NULL;
157 	bio->cb_arg = NULL;
158 	bio->init = 0;
159 	bio->shutdown = 1;
160 	bio->flags = 0;
161 	bio->retry_reason = 0;
162 	bio->num = 0;
163 	bio->ptr = NULL;
164 	bio->prev_bio = NULL;
165 	bio->next_bio = NULL;
166 	bio->references = 1;
167 	bio->num_read = 0L;
168 	bio->num_write = 0L;
169 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
170 	if (method->create != NULL) {
171 		if (!method->create(bio)) {
172 			CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
173 			    &bio->ex_data);
174 			return (0);
175 		}
176 	}
177 	return (1);
178 }
179 
180 int
181 BIO_free(BIO *a)
182 {
183 	int ret;
184 
185 	if (a == NULL)
186 		return (0);
187 
188 	if (CRYPTO_add(&a->references, -1, CRYPTO_LOCK_BIO) > 0)
189 		return (1);
190 
191 	if (a->callback != NULL || a->callback_ex != NULL) {
192 		if ((ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0,
193 		    0L, 1L, NULL)) <= 0)
194 			return (ret);
195 	}
196 
197 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
198 
199 	if (a->method != NULL && a->method->destroy != NULL)
200 		a->method->destroy(a);
201 	free(a);
202 	return (1);
203 }
204 
205 void
206 BIO_vfree(BIO *a)
207 {
208 	BIO_free(a);
209 }
210 
211 int
212 BIO_up_ref(BIO *bio)
213 {
214 	int refs = CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO);
215 	return (refs > 1) ? 1 : 0;
216 }
217 
218 void *
219 BIO_get_data(BIO *a)
220 {
221 	return (a->ptr);
222 }
223 
224 void
225 BIO_set_data(BIO *a, void *ptr)
226 {
227 	a->ptr = ptr;
228 }
229 
230 int
231 BIO_get_init(BIO *a)
232 {
233 	return a->init;
234 }
235 
236 void
237 BIO_set_init(BIO *a, int init)
238 {
239 	a->init = init;
240 }
241 
242 int
243 BIO_get_shutdown(BIO *a)
244 {
245 	return (a->shutdown);
246 }
247 
248 void
249 BIO_set_shutdown(BIO *a, int shut)
250 {
251 	a->shutdown = shut;
252 }
253 
254 void
255 BIO_clear_flags(BIO *b, int flags)
256 {
257 	b->flags &= ~flags;
258 }
259 
260 int
261 BIO_test_flags(const BIO *b, int flags)
262 {
263 	return (b->flags & flags);
264 }
265 
266 void
267 BIO_set_flags(BIO *b, int flags)
268 {
269 	b->flags |= flags;
270 }
271 
272 BIO_callback_fn
273 BIO_get_callback(const BIO *b)
274 {
275 	return b->callback;
276 }
277 
278 void
279 BIO_set_callback(BIO *b, BIO_callback_fn cb)
280 {
281 	b->callback = cb;
282 }
283 
284 BIO_callback_fn_ex
285 BIO_get_callback_ex(const BIO *b)
286 {
287 	return b->callback_ex;
288 }
289 
290 void
291 BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
292 {
293 	b->callback_ex = cb;
294 }
295 
296 void
297 BIO_set_callback_arg(BIO *b, char *arg)
298 {
299 	b->cb_arg = arg;
300 }
301 
302 char *
303 BIO_get_callback_arg(const BIO *b)
304 {
305 	return b->cb_arg;
306 }
307 
308 const char *
309 BIO_method_name(const BIO *b)
310 {
311 	return b->method->name;
312 }
313 
314 int
315 BIO_method_type(const BIO *b)
316 {
317 	return b->method->type;
318 }
319 
320 int
321 BIO_read(BIO *b, void *out, int outl)
322 {
323 	size_t readbytes = 0;
324 	int ret;
325 
326 	if (b == NULL) {
327 		BIOerror(ERR_R_PASSED_NULL_PARAMETER);
328 		return (-1);
329 	}
330 
331 	if (outl <= 0)
332 		return (0);
333 
334 	if (out == NULL) {
335 		BIOerror(ERR_R_PASSED_NULL_PARAMETER);
336 		return (-1);
337 	}
338 
339 	if (b->method == NULL || b->method->bread == NULL) {
340 		BIOerror(BIO_R_UNSUPPORTED_METHOD);
341 		return (-2);
342 	}
343 
344 	if (b->callback != NULL || b->callback_ex != NULL) {
345 		if ((ret = (int)bio_call_callback(b, BIO_CB_READ, out, outl, 0,
346 		    0L, 1L, NULL)) <= 0)
347 			return (ret);
348 	}
349 
350 	if (!b->init) {
351 		BIOerror(BIO_R_UNINITIALIZED);
352 		return (-2);
353 	}
354 
355 	if ((ret = b->method->bread(b, out, outl)) > 0)
356 		readbytes = (size_t)ret;
357 
358 	b->num_read += readbytes;
359 
360 	if (b->callback != NULL || b->callback_ex != NULL) {
361 		ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN,
362 		    out, outl, 0, 0L, (ret > 0) ? 1 : ret, &readbytes);
363 	}
364 
365 	if (ret > 0) {
366 		if (readbytes > INT_MAX) {
367 			BIOerror(BIO_R_LENGTH_TOO_LONG);
368 			ret = -1;
369 		} else {
370 			ret = (int)readbytes;
371 		}
372 	}
373 
374 	return (ret);
375 }
376 
377 int
378 BIO_write(BIO *b, const void *in, int inl)
379 {
380 	size_t writebytes = 0;
381 	int ret;
382 
383 	/* Not an error. Things like SMIME_text() assume that this succeeds. */
384 	if (b == NULL)
385 		return (0);
386 
387 	if (inl <= 0)
388 		return (0);
389 
390 	if (in == NULL) {
391 		BIOerror(ERR_R_PASSED_NULL_PARAMETER);
392 		return (-1);
393 	}
394 
395 	if (b->method == NULL || b->method->bwrite == NULL) {
396 		BIOerror(BIO_R_UNSUPPORTED_METHOD);
397 		return (-2);
398 	}
399 
400 	if (b->callback != NULL || b->callback_ex != NULL) {
401 		if ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, in, inl, 0,
402 		    0L, 1L, NULL)) <= 0)
403 			return (ret);
404 	}
405 
406 	if (!b->init) {
407 		BIOerror(BIO_R_UNINITIALIZED);
408 		return (-2);
409 	}
410 
411 	if ((ret = b->method->bwrite(b, in, inl)) > 0)
412 		writebytes = ret;
413 
414 	b->num_write += writebytes;
415 
416 	if (b->callback != NULL || b->callback_ex != NULL) {
417 		ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN,
418 		    in, inl, 0, 0L, (ret > 0) ? 1 : ret, &writebytes);
419 	}
420 
421 	if (ret > 0) {
422 		if (writebytes > INT_MAX) {
423 			BIOerror(BIO_R_LENGTH_TOO_LONG);
424 			ret = -1;
425 		} else {
426 			ret = (int)writebytes;
427 		}
428 	}
429 
430 	return (ret);
431 }
432 
433 int
434 BIO_puts(BIO *b, const char *in)
435 {
436 	size_t writebytes = 0;
437 	int ret;
438 
439 	if (b == NULL || b->method == NULL || b->method->bputs == NULL) {
440 		BIOerror(BIO_R_UNSUPPORTED_METHOD);
441 		return (-2);
442 	}
443 
444 	if (b->callback != NULL || b->callback_ex != NULL) {
445 		if ((ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L,
446 		    1L, NULL)) <= 0)
447 			return (ret);
448 	}
449 
450 	if (!b->init) {
451 		BIOerror(BIO_R_UNINITIALIZED);
452 		return (-2);
453 	}
454 
455 	if ((ret = b->method->bputs(b, in)) > 0)
456 		writebytes = ret;
457 
458 	b->num_write += writebytes;
459 
460 	if (b->callback != NULL || b->callback_ex != NULL) {
461 		ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN,
462 		    in, 0, 0, 0L, (ret > 0) ? 1 : ret, &writebytes);
463 	}
464 
465 	if (ret > 0) {
466 		if (writebytes > INT_MAX) {
467 			BIOerror(BIO_R_LENGTH_TOO_LONG);
468 			ret = -1;
469 		} else {
470 			ret = (int)writebytes;
471 		}
472 	}
473 
474 	return (ret);
475 }
476 
477 int
478 BIO_gets(BIO *b, char *in, int inl)
479 {
480 	size_t readbytes = 0;
481 	int ret;
482 
483 	if (b == NULL || b->method == NULL || b->method->bgets == NULL) {
484 		BIOerror(BIO_R_UNSUPPORTED_METHOD);
485 		return (-2);
486 	}
487 
488 	if (b->callback != NULL || b->callback_ex != NULL) {
489 		if ((ret = (int)bio_call_callback(b, BIO_CB_GETS, in, inl, 0, 0L,
490 		    1, NULL)) <= 0)
491 			return (ret);
492 	}
493 
494 	if (!b->init) {
495 		BIOerror(BIO_R_UNINITIALIZED);
496 		return (-2);
497 	}
498 
499 	if ((ret = b->method->bgets(b, in, inl)) > 0)
500 		readbytes = ret;
501 
502 	if (b->callback != NULL || b->callback_ex != NULL) {
503 		ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, in,
504 		    inl, 0, 0L, (ret > 0) ? 1 : ret, &readbytes);
505 	}
506 
507 	if (ret > 0) {
508 		if (readbytes > INT_MAX) {
509 			BIOerror(BIO_R_LENGTH_TOO_LONG);
510 			ret = -1;
511 		} else {
512 			ret = (int)readbytes;
513 		}
514 	}
515 
516 	return (ret);
517 }
518 
519 int
520 BIO_indent(BIO *b, int indent, int max)
521 {
522 	if (indent > max)
523 		indent = max;
524 	if (indent < 0)
525 		indent = 0;
526 	while (indent--)
527 		if (BIO_puts(b, " ") != 1)
528 			return 0;
529 	return 1;
530 }
531 
532 long
533 BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
534 {
535 	int i;
536 
537 	i = iarg;
538 	return (BIO_ctrl(b, cmd, larg, (char *)&i));
539 }
540 
541 char *
542 BIO_ptr_ctrl(BIO *b, int cmd, long larg)
543 {
544 	char *p = NULL;
545 
546 	if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
547 		return (NULL);
548 	else
549 		return (p);
550 }
551 
552 long
553 BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
554 {
555 	long ret;
556 
557 	if (b == NULL)
558 		return (0);
559 
560 	if (b->method == NULL || b->method->ctrl == NULL) {
561 		BIOerror(BIO_R_UNSUPPORTED_METHOD);
562 		return (-2);
563 	}
564 
565 	if (b->callback != NULL || b->callback_ex != NULL) {
566 		if ((ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg,
567 		    1L, NULL)) <= 0)
568 			return (ret);
569 	}
570 
571 	ret = b->method->ctrl(b, cmd, larg, parg);
572 
573 	if (b->callback != NULL || b->callback_ex != NULL) {
574 		ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0,
575 		    cmd, larg, ret, NULL);
576 	}
577 
578 	return (ret);
579 }
580 
581 long
582 BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
583 {
584 	long ret;
585 
586 	if (b == NULL)
587 		return (0);
588 
589 	if (b->method == NULL || b->method->callback_ctrl == NULL ||
590 	    cmd != BIO_CTRL_SET_CALLBACK) {
591 		BIOerror(BIO_R_UNSUPPORTED_METHOD);
592 		return (-2);
593 	}
594 
595 	if (b->callback != NULL || b->callback_ex != NULL) {
596 		if ((ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0,
597 		    cmd, 0, 1L, NULL)) <= 0)
598 			return (ret);
599 	}
600 
601 	ret = b->method->callback_ctrl(b, cmd, fp);
602 
603 	if (b->callback != NULL || b->callback_ex != NULL) {
604 		ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN,
605 		    (void *)&fp, 0, cmd, 0, ret, NULL);
606 	}
607 
608 	return (ret);
609 }
610 
611 /* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
612  * do; but those macros have inappropriate return type, and for interfacing
613  * from other programming languages, C macros aren't much of a help anyway. */
614 size_t
615 BIO_ctrl_pending(BIO *bio)
616 {
617 	return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
618 }
619 
620 size_t
621 BIO_ctrl_wpending(BIO *bio)
622 {
623 	return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
624 }
625 
626 
627 /*
628  * Append "bio" to the end of the chain containing "b":
629  * Two chains "b -> lb" and "oldhead -> bio"
630  * become two chains "b -> lb -> bio" and "oldhead".
631  */
632 BIO *
633 BIO_push(BIO *b, BIO *bio)
634 {
635 	BIO *lb;
636 
637 	if (b == NULL)
638 		return (bio);
639 	lb = b;
640 	while (lb->next_bio != NULL)
641 		lb = lb->next_bio;
642 	lb->next_bio = bio;
643 	if (bio != NULL) {
644 		if (bio->prev_bio != NULL)
645 			bio->prev_bio->next_bio = NULL;
646 		bio->prev_bio = lb;
647 	}
648 	/* called to do internal processing */
649 	BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
650 	return (b);
651 }
652 
653 /* Remove the first and return the rest */
654 BIO *
655 BIO_pop(BIO *b)
656 {
657 	BIO *ret;
658 
659 	if (b == NULL)
660 		return (NULL);
661 	ret = b->next_bio;
662 
663 	BIO_ctrl(b, BIO_CTRL_POP, 0, b);
664 
665 	if (b->prev_bio != NULL)
666 		b->prev_bio->next_bio = b->next_bio;
667 	if (b->next_bio != NULL)
668 		b->next_bio->prev_bio = b->prev_bio;
669 
670 	b->next_bio = NULL;
671 	b->prev_bio = NULL;
672 	return (ret);
673 }
674 
675 BIO *
676 BIO_get_retry_BIO(BIO *bio, int *reason)
677 {
678 	BIO *b, *last;
679 
680 	b = last = bio;
681 	for (;;) {
682 		if (!BIO_should_retry(b))
683 			break;
684 		last = b;
685 		b = b->next_bio;
686 		if (b == NULL)
687 			break;
688 	}
689 	if (reason != NULL)
690 		*reason = last->retry_reason;
691 	return (last);
692 }
693 
694 int
695 BIO_get_retry_reason(BIO *bio)
696 {
697 	return (bio->retry_reason);
698 }
699 
700 void
701 BIO_set_retry_reason(BIO *bio, int reason)
702 {
703 	bio->retry_reason = reason;
704 }
705 
706 BIO *
707 BIO_find_type(BIO *bio, int type)
708 {
709 	int mt, mask;
710 
711 	if (!bio)
712 		return NULL;
713 	mask = type & 0xff;
714 	do {
715 		if (bio->method != NULL) {
716 			mt = bio->method->type;
717 			if (!mask) {
718 				if (mt & type)
719 					return (bio);
720 			} else if (mt == type)
721 				return (bio);
722 		}
723 		bio = bio->next_bio;
724 	} while (bio != NULL);
725 	return (NULL);
726 }
727 
728 BIO *
729 BIO_next(BIO *b)
730 {
731 	if (!b)
732 		return NULL;
733 	return b->next_bio;
734 }
735 
736 /*
737  * Two chains "bio -> oldtail" and "oldhead -> next" become
738  * three chains "oldtail", "bio -> next", and "oldhead".
739  */
740 void
741 BIO_set_next(BIO *bio, BIO *next)
742 {
743 	/* Cut off the tail of the chain containing bio after bio. */
744 	if (bio->next_bio != NULL)
745 		bio->next_bio->prev_bio = NULL;
746 
747 	/* Cut off the head of the chain containing next before next. */
748 	if (next != NULL && next->prev_bio != NULL)
749 		next->prev_bio->next_bio = NULL;
750 
751 	/* Append the chain starting at next to the chain ending at bio. */
752 	bio->next_bio = next;
753 	if (next != NULL)
754 		next->prev_bio = bio;
755 }
756 
757 void
758 BIO_free_all(BIO *bio)
759 {
760 	BIO *b;
761 	int ref;
762 
763 	while (bio != NULL) {
764 		b = bio;
765 		ref = b->references;
766 		bio = bio->next_bio;
767 		BIO_free(b);
768 		/* Since ref count > 1, don't free anyone else. */
769 		if (ref > 1)
770 			break;
771 	}
772 }
773 
774 BIO *
775 BIO_dup_chain(BIO *in)
776 {
777 	BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
778 
779 	for (bio = in; bio != NULL; bio = bio->next_bio) {
780 		if ((new_bio = BIO_new(bio->method)) == NULL)
781 			goto err;
782 		new_bio->callback = bio->callback;
783 		new_bio->callback_ex = bio->callback_ex;
784 		new_bio->cb_arg = bio->cb_arg;
785 		new_bio->init = bio->init;
786 		new_bio->shutdown = bio->shutdown;
787 		new_bio->flags = bio->flags;
788 
789 		/* This will let SSL_s_sock() work with stdin/stdout */
790 		new_bio->num = bio->num;
791 
792 		if (!BIO_dup_state(bio, (char *)new_bio)) {
793 			BIO_free(new_bio);
794 			goto err;
795 		}
796 
797 		/* copy app data */
798 		if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO,
799 		    &new_bio->ex_data, &bio->ex_data))
800 			goto err;
801 
802 		if (ret == NULL) {
803 			eoc = new_bio;
804 			ret = eoc;
805 		} else {
806 			BIO_push(eoc, new_bio);
807 			eoc = new_bio;
808 		}
809 	}
810 	return (ret);
811 err:
812 	BIO_free(ret);
813 	return (NULL);
814 
815 }
816 
817 void
818 BIO_copy_next_retry(BIO *b)
819 {
820 	BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
821 	b->retry_reason = b->next_bio->retry_reason;
822 }
823 
824 int
825 BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
826     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
827 {
828 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
829 	    new_func, dup_func, free_func);
830 }
831 
832 int
833 BIO_set_ex_data(BIO *bio, int idx, void *data)
834 {
835 	return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
836 }
837 
838 void *
839 BIO_get_ex_data(BIO *bio, int idx)
840 {
841 	return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
842 }
843 
844 unsigned long
845 BIO_number_read(BIO *bio)
846 {
847 	if (bio)
848 		return bio->num_read;
849 	return 0;
850 }
851 
852 unsigned long
853 BIO_number_written(BIO *bio)
854 {
855 	if (bio)
856 		return bio->num_write;
857 	return 0;
858 }
859