xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bf_nbio.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bio/bf_nbio.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 "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/rand.h>
630Sstevel@tonic-gate #include <openssl/bio.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /* BIO_put and BIO_get both add to the digest,
660Sstevel@tonic-gate  * BIO_gets returns the digest */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int nbiof_write(BIO *h,const char *buf,int num);
690Sstevel@tonic-gate static int nbiof_read(BIO *h,char *buf,int size);
700Sstevel@tonic-gate static int nbiof_puts(BIO *h,const char *str);
710Sstevel@tonic-gate static int nbiof_gets(BIO *h,char *str,int size);
720Sstevel@tonic-gate static long nbiof_ctrl(BIO *h,int cmd,long arg1,void *arg2);
730Sstevel@tonic-gate static int nbiof_new(BIO *h);
740Sstevel@tonic-gate static int nbiof_free(BIO *data);
750Sstevel@tonic-gate static long nbiof_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
760Sstevel@tonic-gate typedef struct nbio_test_st
770Sstevel@tonic-gate 	{
780Sstevel@tonic-gate 	/* only set if we sent a 'should retry' error */
790Sstevel@tonic-gate 	int lrn;
800Sstevel@tonic-gate 	int lwn;
810Sstevel@tonic-gate 	} NBIO_TEST;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static BIO_METHOD methods_nbiof=
840Sstevel@tonic-gate 	{
850Sstevel@tonic-gate 	BIO_TYPE_NBIO_TEST,
860Sstevel@tonic-gate 	"non-blocking IO test filter",
870Sstevel@tonic-gate 	nbiof_write,
880Sstevel@tonic-gate 	nbiof_read,
890Sstevel@tonic-gate 	nbiof_puts,
900Sstevel@tonic-gate 	nbiof_gets,
910Sstevel@tonic-gate 	nbiof_ctrl,
920Sstevel@tonic-gate 	nbiof_new,
930Sstevel@tonic-gate 	nbiof_free,
940Sstevel@tonic-gate 	nbiof_callback_ctrl,
950Sstevel@tonic-gate 	};
960Sstevel@tonic-gate 
BIO_f_nbio_test(void)970Sstevel@tonic-gate BIO_METHOD *BIO_f_nbio_test(void)
980Sstevel@tonic-gate 	{
990Sstevel@tonic-gate 	return(&methods_nbiof);
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 
nbiof_new(BIO * bi)1020Sstevel@tonic-gate static int nbiof_new(BIO *bi)
1030Sstevel@tonic-gate 	{
1040Sstevel@tonic-gate 	NBIO_TEST *nt;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (!(nt=(NBIO_TEST *)OPENSSL_malloc(sizeof(NBIO_TEST)))) return(0);
1070Sstevel@tonic-gate 	nt->lrn= -1;
1080Sstevel@tonic-gate 	nt->lwn= -1;
1090Sstevel@tonic-gate 	bi->ptr=(char *)nt;
1100Sstevel@tonic-gate 	bi->init=1;
1110Sstevel@tonic-gate 	bi->flags=0;
1120Sstevel@tonic-gate 	return(1);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
nbiof_free(BIO * a)1150Sstevel@tonic-gate static int nbiof_free(BIO *a)
1160Sstevel@tonic-gate 	{
1170Sstevel@tonic-gate 	if (a == NULL) return(0);
1180Sstevel@tonic-gate 	if (a->ptr != NULL)
1190Sstevel@tonic-gate 		OPENSSL_free(a->ptr);
1200Sstevel@tonic-gate 	a->ptr=NULL;
1210Sstevel@tonic-gate 	a->init=0;
1220Sstevel@tonic-gate 	a->flags=0;
1230Sstevel@tonic-gate 	return(1);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
nbiof_read(BIO * b,char * out,int outl)1260Sstevel@tonic-gate static int nbiof_read(BIO *b, char *out, int outl)
1270Sstevel@tonic-gate 	{
1280Sstevel@tonic-gate 	NBIO_TEST *nt;
1290Sstevel@tonic-gate 	int ret=0;
130*2139Sjp161948 #if 1
1310Sstevel@tonic-gate 	int num;
1320Sstevel@tonic-gate 	unsigned char n;
1330Sstevel@tonic-gate #endif
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (out == NULL) return(0);
1360Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
1370Sstevel@tonic-gate 	nt=(NBIO_TEST *)b->ptr;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
140*2139Sjp161948 #if 1
1410Sstevel@tonic-gate 	RAND_pseudo_bytes(&n,1);
1420Sstevel@tonic-gate 	num=(n&0x07);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (outl > num) outl=num;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if (num == 0)
1470Sstevel@tonic-gate 		{
1480Sstevel@tonic-gate 		ret= -1;
1490Sstevel@tonic-gate 		BIO_set_retry_read(b);
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	else
1520Sstevel@tonic-gate #endif
1530Sstevel@tonic-gate 		{
1540Sstevel@tonic-gate 		ret=BIO_read(b->next_bio,out,outl);
1550Sstevel@tonic-gate 		if (ret < 0)
1560Sstevel@tonic-gate 			BIO_copy_next_retry(b);
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 	return(ret);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
nbiof_write(BIO * b,const char * in,int inl)1610Sstevel@tonic-gate static int nbiof_write(BIO *b, const char *in, int inl)
1620Sstevel@tonic-gate 	{
1630Sstevel@tonic-gate 	NBIO_TEST *nt;
1640Sstevel@tonic-gate 	int ret=0;
1650Sstevel@tonic-gate 	int num;
1660Sstevel@tonic-gate 	unsigned char n;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if ((in == NULL) || (inl <= 0)) return(0);
1690Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
1700Sstevel@tonic-gate 	nt=(NBIO_TEST *)b->ptr;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate #if 1
1750Sstevel@tonic-gate 	if (nt->lwn > 0)
1760Sstevel@tonic-gate 		{
1770Sstevel@tonic-gate 		num=nt->lwn;
1780Sstevel@tonic-gate 		nt->lwn=0;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	else
1810Sstevel@tonic-gate 		{
1820Sstevel@tonic-gate 		RAND_pseudo_bytes(&n,1);
1830Sstevel@tonic-gate 		num=(n&7);
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (inl > num) inl=num;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (num == 0)
1890Sstevel@tonic-gate 		{
1900Sstevel@tonic-gate 		ret= -1;
1910Sstevel@tonic-gate 		BIO_set_retry_write(b);
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 	else
1940Sstevel@tonic-gate #endif
1950Sstevel@tonic-gate 		{
1960Sstevel@tonic-gate 		ret=BIO_write(b->next_bio,in,inl);
1970Sstevel@tonic-gate 		if (ret < 0)
1980Sstevel@tonic-gate 			{
1990Sstevel@tonic-gate 			BIO_copy_next_retry(b);
2000Sstevel@tonic-gate 			nt->lwn=inl;
2010Sstevel@tonic-gate 			}
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	return(ret);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
nbiof_ctrl(BIO * b,int cmd,long num,void * ptr)2060Sstevel@tonic-gate static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
2070Sstevel@tonic-gate 	{
2080Sstevel@tonic-gate 	long ret;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
2110Sstevel@tonic-gate 	switch (cmd)
2120Sstevel@tonic-gate 		{
2130Sstevel@tonic-gate         case BIO_C_DO_STATE_MACHINE:
2140Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
2150Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
2160Sstevel@tonic-gate 		BIO_copy_next_retry(b);
2170Sstevel@tonic-gate 		break;
2180Sstevel@tonic-gate 	case BIO_CTRL_DUP:
2190Sstevel@tonic-gate 		ret=0L;
2200Sstevel@tonic-gate 		break;
2210Sstevel@tonic-gate 	default:
2220Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
2230Sstevel@tonic-gate 		break;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	return(ret);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
nbiof_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)2280Sstevel@tonic-gate static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
2290Sstevel@tonic-gate 	{
2300Sstevel@tonic-gate 	long ret=1;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
2330Sstevel@tonic-gate 	switch (cmd)
2340Sstevel@tonic-gate 		{
2350Sstevel@tonic-gate 	default:
2360Sstevel@tonic-gate 		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
2370Sstevel@tonic-gate 		break;
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 	return(ret);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
nbiof_gets(BIO * bp,char * buf,int size)2420Sstevel@tonic-gate static int nbiof_gets(BIO *bp, char *buf, int size)
2430Sstevel@tonic-gate 	{
2440Sstevel@tonic-gate 	if (bp->next_bio == NULL) return(0);
2450Sstevel@tonic-gate 	return(BIO_gets(bp->next_bio,buf,size));
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 
nbiof_puts(BIO * bp,const char * str)2490Sstevel@tonic-gate static int nbiof_puts(BIO *bp, const char *str)
2500Sstevel@tonic-gate 	{
2510Sstevel@tonic-gate 	if (bp->next_bio == NULL) return(0);
2520Sstevel@tonic-gate 	return(BIO_puts(bp->next_bio,str));
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 
256