xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bio_lib.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bio/bio_lib.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <errno.h>
610Sstevel@tonic-gate #include <openssl/crypto.h>
620Sstevel@tonic-gate #include "cryptlib.h"
630Sstevel@tonic-gate #include <openssl/bio.h>
640Sstevel@tonic-gate #include <openssl/stack.h>
650Sstevel@tonic-gate 
BIO_new(BIO_METHOD * method)660Sstevel@tonic-gate BIO *BIO_new(BIO_METHOD *method)
670Sstevel@tonic-gate 	{
680Sstevel@tonic-gate 	BIO *ret=NULL;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	ret=(BIO *)OPENSSL_malloc(sizeof(BIO));
710Sstevel@tonic-gate 	if (ret == NULL)
720Sstevel@tonic-gate 		{
730Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);
740Sstevel@tonic-gate 		return(NULL);
750Sstevel@tonic-gate 		}
760Sstevel@tonic-gate 	if (!BIO_set(ret,method))
770Sstevel@tonic-gate 		{
780Sstevel@tonic-gate 		OPENSSL_free(ret);
790Sstevel@tonic-gate 		ret=NULL;
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 	return(ret);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
BIO_set(BIO * bio,BIO_METHOD * method)840Sstevel@tonic-gate int BIO_set(BIO *bio, BIO_METHOD *method)
850Sstevel@tonic-gate 	{
860Sstevel@tonic-gate 	bio->method=method;
870Sstevel@tonic-gate 	bio->callback=NULL;
880Sstevel@tonic-gate 	bio->cb_arg=NULL;
890Sstevel@tonic-gate 	bio->init=0;
900Sstevel@tonic-gate 	bio->shutdown=1;
910Sstevel@tonic-gate 	bio->flags=0;
920Sstevel@tonic-gate 	bio->retry_reason=0;
930Sstevel@tonic-gate 	bio->num=0;
940Sstevel@tonic-gate 	bio->ptr=NULL;
950Sstevel@tonic-gate 	bio->prev_bio=NULL;
960Sstevel@tonic-gate 	bio->next_bio=NULL;
970Sstevel@tonic-gate 	bio->references=1;
980Sstevel@tonic-gate 	bio->num_read=0L;
990Sstevel@tonic-gate 	bio->num_write=0L;
1000Sstevel@tonic-gate 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
1010Sstevel@tonic-gate 	if (method->create != NULL)
1020Sstevel@tonic-gate 		if (!method->create(bio))
1030Sstevel@tonic-gate 			{
1040Sstevel@tonic-gate 			CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
1050Sstevel@tonic-gate 					&bio->ex_data);
1060Sstevel@tonic-gate 			return(0);
1070Sstevel@tonic-gate 			}
1080Sstevel@tonic-gate 	return(1);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
BIO_free(BIO * a)1110Sstevel@tonic-gate int BIO_free(BIO *a)
1120Sstevel@tonic-gate 	{
1130Sstevel@tonic-gate 	int ret=0,i;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (a == NULL) return(0);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO);
1180Sstevel@tonic-gate #ifdef REF_PRINT
1190Sstevel@tonic-gate 	REF_PRINT("BIO",a);
1200Sstevel@tonic-gate #endif
1210Sstevel@tonic-gate 	if (i > 0) return(1);
1220Sstevel@tonic-gate #ifdef REF_CHECK
1230Sstevel@tonic-gate 	if (i < 0)
1240Sstevel@tonic-gate 		{
1250Sstevel@tonic-gate 		fprintf(stderr,"BIO_free, bad reference count\n");
1260Sstevel@tonic-gate 		abort();
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate #endif
1290Sstevel@tonic-gate 	if ((a->callback != NULL) &&
1300Sstevel@tonic-gate 		((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0))
1310Sstevel@tonic-gate 			return(i);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if ((a->method == NULL) || (a->method->destroy == NULL)) return(1);
1360Sstevel@tonic-gate 	ret=a->method->destroy(a);
1370Sstevel@tonic-gate 	OPENSSL_free(a);
1380Sstevel@tonic-gate 	return(1);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
BIO_vfree(BIO * a)1410Sstevel@tonic-gate void BIO_vfree(BIO *a)
1420Sstevel@tonic-gate     { BIO_free(a); }
1430Sstevel@tonic-gate 
BIO_read(BIO * b,void * out,int outl)1440Sstevel@tonic-gate int BIO_read(BIO *b, void *out, int outl)
1450Sstevel@tonic-gate 	{
1460Sstevel@tonic-gate 	int i;
147*2139Sjp161948 	long (*cb)(BIO *,int,const char *,int,long,long);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL))
1500Sstevel@tonic-gate 		{
1510Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD);
1520Sstevel@tonic-gate 		return(-2);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	cb=b->callback;
1560Sstevel@tonic-gate 	if ((cb != NULL) &&
1570Sstevel@tonic-gate 		((i=(int)cb(b,BIO_CB_READ,out,outl,0L,1L)) <= 0))
1580Sstevel@tonic-gate 			return(i);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (!b->init)
1610Sstevel@tonic-gate 		{
1620Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_READ,BIO_R_UNINITIALIZED);
1630Sstevel@tonic-gate 		return(-2);
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	i=b->method->bread(b,out,outl);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (i > 0) b->num_read+=(unsigned long)i;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (cb != NULL)
1710Sstevel@tonic-gate 		i=(int)cb(b,BIO_CB_READ|BIO_CB_RETURN,out,outl,
1720Sstevel@tonic-gate 			0L,(long)i);
1730Sstevel@tonic-gate 	return(i);
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
BIO_write(BIO * b,const void * in,int inl)1760Sstevel@tonic-gate int BIO_write(BIO *b, const void *in, int inl)
1770Sstevel@tonic-gate 	{
1780Sstevel@tonic-gate 	int i;
179*2139Sjp161948 	long (*cb)(BIO *,int,const char *,int,long,long);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (b == NULL)
1820Sstevel@tonic-gate 		return(0);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	cb=b->callback;
1850Sstevel@tonic-gate 	if ((b->method == NULL) || (b->method->bwrite == NULL))
1860Sstevel@tonic-gate 		{
1870Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD);
1880Sstevel@tonic-gate 		return(-2);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if ((cb != NULL) &&
1920Sstevel@tonic-gate 		((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0))
1930Sstevel@tonic-gate 			return(i);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (!b->init)
1960Sstevel@tonic-gate 		{
1970Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED);
1980Sstevel@tonic-gate 		return(-2);
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	i=b->method->bwrite(b,in,inl);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (i > 0) b->num_write+=(unsigned long)i;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (cb != NULL)
2060Sstevel@tonic-gate 		i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl,
2070Sstevel@tonic-gate 			0L,(long)i);
2080Sstevel@tonic-gate 	return(i);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
BIO_puts(BIO * b,const char * in)2110Sstevel@tonic-gate int BIO_puts(BIO *b, const char *in)
2120Sstevel@tonic-gate 	{
2130Sstevel@tonic-gate 	int i;
214*2139Sjp161948 	long (*cb)(BIO *,int,const char *,int,long,long);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL))
2170Sstevel@tonic-gate 		{
2180Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD);
2190Sstevel@tonic-gate 		return(-2);
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	cb=b->callback;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if ((cb != NULL) &&
2250Sstevel@tonic-gate 		((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0))
2260Sstevel@tonic-gate 			return(i);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (!b->init)
2290Sstevel@tonic-gate 		{
2300Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED);
2310Sstevel@tonic-gate 		return(-2);
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	i=b->method->bputs(b,in);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (i > 0) b->num_write+=(unsigned long)i;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (cb != NULL)
2390Sstevel@tonic-gate 		i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,
2400Sstevel@tonic-gate 			0L,(long)i);
2410Sstevel@tonic-gate 	return(i);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
BIO_gets(BIO * b,char * in,int inl)2440Sstevel@tonic-gate int BIO_gets(BIO *b, char *in, int inl)
2450Sstevel@tonic-gate 	{
2460Sstevel@tonic-gate 	int i;
247*2139Sjp161948 	long (*cb)(BIO *,int,const char *,int,long,long);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL))
2500Sstevel@tonic-gate 		{
2510Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD);
2520Sstevel@tonic-gate 		return(-2);
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	cb=b->callback;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if ((cb != NULL) &&
2580Sstevel@tonic-gate 		((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0))
2590Sstevel@tonic-gate 			return(i);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (!b->init)
2620Sstevel@tonic-gate 		{
2630Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED);
2640Sstevel@tonic-gate 		return(-2);
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	i=b->method->bgets(b,in,inl);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (cb != NULL)
2700Sstevel@tonic-gate 		i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl,
2710Sstevel@tonic-gate 			0L,(long)i);
2720Sstevel@tonic-gate 	return(i);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
BIO_indent(BIO * b,int indent,int max)2750Sstevel@tonic-gate int BIO_indent(BIO *b,int indent,int max)
2760Sstevel@tonic-gate 	{
2770Sstevel@tonic-gate 	if(indent < 0)
2780Sstevel@tonic-gate 		indent=0;
2790Sstevel@tonic-gate 	if(indent > max)
2800Sstevel@tonic-gate 		indent=max;
2810Sstevel@tonic-gate 	while(indent--)
2820Sstevel@tonic-gate 		if(BIO_puts(b," ") != 1)
2830Sstevel@tonic-gate 			return 0;
2840Sstevel@tonic-gate 	return 1;
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
BIO_int_ctrl(BIO * b,int cmd,long larg,int iarg)2870Sstevel@tonic-gate long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
2880Sstevel@tonic-gate 	{
2890Sstevel@tonic-gate 	int i;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	i=iarg;
2920Sstevel@tonic-gate 	return(BIO_ctrl(b,cmd,larg,(char *)&i));
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
BIO_ptr_ctrl(BIO * b,int cmd,long larg)2950Sstevel@tonic-gate char *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
2960Sstevel@tonic-gate 	{
2970Sstevel@tonic-gate 	char *p=NULL;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (BIO_ctrl(b,cmd,larg,(char *)&p) <= 0)
3000Sstevel@tonic-gate 		return(NULL);
3010Sstevel@tonic-gate 	else
3020Sstevel@tonic-gate 		return(p);
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
BIO_ctrl(BIO * b,int cmd,long larg,void * parg)3050Sstevel@tonic-gate long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
3060Sstevel@tonic-gate 	{
3070Sstevel@tonic-gate 	long ret;
308*2139Sjp161948 	long (*cb)(BIO *,int,const char *,int,long,long);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (b == NULL) return(0);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if ((b->method == NULL) || (b->method->ctrl == NULL))
3130Sstevel@tonic-gate 		{
3140Sstevel@tonic-gate 		BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);
3150Sstevel@tonic-gate 		return(-2);
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	cb=b->callback;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if ((cb != NULL) &&
3210Sstevel@tonic-gate 		((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0))
3220Sstevel@tonic-gate 		return(ret);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	ret=b->method->ctrl(b,cmd,larg,parg);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (cb != NULL)
3270Sstevel@tonic-gate 		ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd,
3280Sstevel@tonic-gate 			larg,ret);
3290Sstevel@tonic-gate 	return(ret);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
BIO_callback_ctrl(BIO * b,int cmd,void (* fp)(struct bio_st *,int,const char *,int,long,long))3320Sstevel@tonic-gate long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long))
3330Sstevel@tonic-gate 	{
3340Sstevel@tonic-gate 	long ret;
335*2139Sjp161948 	long (*cb)(BIO *,int,const char *,int,long,long);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (b == NULL) return(0);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if ((b->method == NULL) || (b->method->callback_ctrl == NULL))
3400Sstevel@tonic-gate 		{
341*2139Sjp161948 		BIOerr(BIO_F_BIO_CALLBACK_CTRL,BIO_R_UNSUPPORTED_METHOD);
3420Sstevel@tonic-gate 		return(-2);
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	cb=b->callback;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if ((cb != NULL) &&
3480Sstevel@tonic-gate 		((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0))
3490Sstevel@tonic-gate 		return(ret);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	ret=b->method->callback_ctrl(b,cmd,fp);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if (cb != NULL)
3540Sstevel@tonic-gate 		ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd,
3550Sstevel@tonic-gate 			0,ret);
3560Sstevel@tonic-gate 	return(ret);
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
3600Sstevel@tonic-gate  * do; but those macros have inappropriate return type, and for interfacing
3610Sstevel@tonic-gate  * from other programming languages, C macros aren't much of a help anyway. */
BIO_ctrl_pending(BIO * bio)3620Sstevel@tonic-gate size_t BIO_ctrl_pending(BIO *bio)
3630Sstevel@tonic-gate 	{
3640Sstevel@tonic-gate 	return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
BIO_ctrl_wpending(BIO * bio)3670Sstevel@tonic-gate size_t BIO_ctrl_wpending(BIO *bio)
3680Sstevel@tonic-gate 	{
3690Sstevel@tonic-gate 	return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /* put the 'bio' on the end of b's list of operators */
BIO_push(BIO * b,BIO * bio)3740Sstevel@tonic-gate BIO *BIO_push(BIO *b, BIO *bio)
3750Sstevel@tonic-gate 	{
3760Sstevel@tonic-gate 	BIO *lb;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (b == NULL) return(bio);
3790Sstevel@tonic-gate 	lb=b;
3800Sstevel@tonic-gate 	while (lb->next_bio != NULL)
3810Sstevel@tonic-gate 		lb=lb->next_bio;
3820Sstevel@tonic-gate 	lb->next_bio=bio;
3830Sstevel@tonic-gate 	if (bio != NULL)
3840Sstevel@tonic-gate 		bio->prev_bio=lb;
3850Sstevel@tonic-gate 	/* called to do internal processing */
3860Sstevel@tonic-gate 	BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL);
3870Sstevel@tonic-gate 	return(b);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /* Remove the first and return the rest */
BIO_pop(BIO * b)3910Sstevel@tonic-gate BIO *BIO_pop(BIO *b)
3920Sstevel@tonic-gate 	{
3930Sstevel@tonic-gate 	BIO *ret;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if (b == NULL) return(NULL);
3960Sstevel@tonic-gate 	ret=b->next_bio;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	BIO_ctrl(b,BIO_CTRL_POP,0,NULL);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (b->prev_bio != NULL)
4010Sstevel@tonic-gate 		b->prev_bio->next_bio=b->next_bio;
4020Sstevel@tonic-gate 	if (b->next_bio != NULL)
4030Sstevel@tonic-gate 		b->next_bio->prev_bio=b->prev_bio;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	b->next_bio=NULL;
4060Sstevel@tonic-gate 	b->prev_bio=NULL;
4070Sstevel@tonic-gate 	return(ret);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
BIO_get_retry_BIO(BIO * bio,int * reason)4100Sstevel@tonic-gate BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
4110Sstevel@tonic-gate 	{
4120Sstevel@tonic-gate 	BIO *b,*last;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	b=last=bio;
4150Sstevel@tonic-gate 	for (;;)
4160Sstevel@tonic-gate 		{
4170Sstevel@tonic-gate 		if (!BIO_should_retry(b)) break;
4180Sstevel@tonic-gate 		last=b;
4190Sstevel@tonic-gate 		b=b->next_bio;
4200Sstevel@tonic-gate 		if (b == NULL) break;
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 	if (reason != NULL) *reason=last->retry_reason;
4230Sstevel@tonic-gate 	return(last);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
BIO_get_retry_reason(BIO * bio)4260Sstevel@tonic-gate int BIO_get_retry_reason(BIO *bio)
4270Sstevel@tonic-gate 	{
4280Sstevel@tonic-gate 	return(bio->retry_reason);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
BIO_find_type(BIO * bio,int type)4310Sstevel@tonic-gate BIO *BIO_find_type(BIO *bio, int type)
4320Sstevel@tonic-gate 	{
4330Sstevel@tonic-gate 	int mt,mask;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if(!bio) return NULL;
4360Sstevel@tonic-gate 	mask=type&0xff;
4370Sstevel@tonic-gate 	do	{
4380Sstevel@tonic-gate 		if (bio->method != NULL)
4390Sstevel@tonic-gate 			{
4400Sstevel@tonic-gate 			mt=bio->method->type;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 			if (!mask)
4430Sstevel@tonic-gate 				{
4440Sstevel@tonic-gate 				if (mt & type) return(bio);
4450Sstevel@tonic-gate 				}
4460Sstevel@tonic-gate 			else if (mt == type)
4470Sstevel@tonic-gate 				return(bio);
4480Sstevel@tonic-gate 			}
4490Sstevel@tonic-gate 		bio=bio->next_bio;
4500Sstevel@tonic-gate 		} while (bio != NULL);
4510Sstevel@tonic-gate 	return(NULL);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
BIO_next(BIO * b)4540Sstevel@tonic-gate BIO *BIO_next(BIO *b)
4550Sstevel@tonic-gate 	{
4560Sstevel@tonic-gate 	if(!b) return NULL;
4570Sstevel@tonic-gate 	return b->next_bio;
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
BIO_free_all(BIO * bio)4600Sstevel@tonic-gate void BIO_free_all(BIO *bio)
4610Sstevel@tonic-gate 	{
4620Sstevel@tonic-gate 	BIO *b;
4630Sstevel@tonic-gate 	int ref;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	while (bio != NULL)
4660Sstevel@tonic-gate 		{
4670Sstevel@tonic-gate 		b=bio;
4680Sstevel@tonic-gate 		ref=b->references;
4690Sstevel@tonic-gate 		bio=bio->next_bio;
4700Sstevel@tonic-gate 		BIO_free(b);
4710Sstevel@tonic-gate 		/* Since ref count > 1, don't free anyone else. */
4720Sstevel@tonic-gate 		if (ref > 1) break;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
BIO_dup_chain(BIO * in)4760Sstevel@tonic-gate BIO *BIO_dup_chain(BIO *in)
4770Sstevel@tonic-gate 	{
4780Sstevel@tonic-gate 	BIO *ret=NULL,*eoc=NULL,*bio,*new;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	for (bio=in; bio != NULL; bio=bio->next_bio)
4810Sstevel@tonic-gate 		{
4820Sstevel@tonic-gate 		if ((new=BIO_new(bio->method)) == NULL) goto err;
4830Sstevel@tonic-gate 		new->callback=bio->callback;
4840Sstevel@tonic-gate 		new->cb_arg=bio->cb_arg;
4850Sstevel@tonic-gate 		new->init=bio->init;
4860Sstevel@tonic-gate 		new->shutdown=bio->shutdown;
4870Sstevel@tonic-gate 		new->flags=bio->flags;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		/* This will let SSL_s_sock() work with stdin/stdout */
4900Sstevel@tonic-gate 		new->num=bio->num;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		if (!BIO_dup_state(bio,(char *)new))
4930Sstevel@tonic-gate 			{
4940Sstevel@tonic-gate 			BIO_free(new);
4950Sstevel@tonic-gate 			goto err;
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		/* copy app data */
4990Sstevel@tonic-gate 		if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new->ex_data,
5000Sstevel@tonic-gate 					&bio->ex_data))
5010Sstevel@tonic-gate 			goto err;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		if (ret == NULL)
5040Sstevel@tonic-gate 			{
5050Sstevel@tonic-gate 			eoc=new;
5060Sstevel@tonic-gate 			ret=eoc;
5070Sstevel@tonic-gate 			}
5080Sstevel@tonic-gate 		else
5090Sstevel@tonic-gate 			{
5100Sstevel@tonic-gate 			BIO_push(eoc,new);
5110Sstevel@tonic-gate 			eoc=new;
5120Sstevel@tonic-gate 			}
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 	return(ret);
5150Sstevel@tonic-gate err:
5160Sstevel@tonic-gate 	if (ret != NULL)
5170Sstevel@tonic-gate 		BIO_free(ret);
5180Sstevel@tonic-gate 	return(NULL);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
BIO_copy_next_retry(BIO * b)5210Sstevel@tonic-gate void BIO_copy_next_retry(BIO *b)
5220Sstevel@tonic-gate 	{
5230Sstevel@tonic-gate 	BIO_set_flags(b,BIO_get_retry_flags(b->next_bio));
5240Sstevel@tonic-gate 	b->retry_reason=b->next_bio->retry_reason;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
BIO_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)5270Sstevel@tonic-gate int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
5280Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
5290Sstevel@tonic-gate 	{
5300Sstevel@tonic-gate 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
5310Sstevel@tonic-gate 				new_func, dup_func, free_func);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
BIO_set_ex_data(BIO * bio,int idx,void * data)5340Sstevel@tonic-gate int BIO_set_ex_data(BIO *bio, int idx, void *data)
5350Sstevel@tonic-gate 	{
5360Sstevel@tonic-gate 	return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
BIO_get_ex_data(BIO * bio,int idx)5390Sstevel@tonic-gate void *BIO_get_ex_data(BIO *bio, int idx)
5400Sstevel@tonic-gate 	{
5410Sstevel@tonic-gate 	return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
BIO_number_read(BIO * bio)5440Sstevel@tonic-gate unsigned long BIO_number_read(BIO *bio)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	if(bio) return bio->num_read;
5470Sstevel@tonic-gate 	return 0;
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
BIO_number_written(BIO * bio)5500Sstevel@tonic-gate unsigned long BIO_number_written(BIO *bio)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	if(bio) return bio->num_write;
5530Sstevel@tonic-gate 	return 0;
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate IMPLEMENT_STACK_OF(BIO)
557