xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bss_fd.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bio/bss_fd.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 #define USE_SOCKETS
620Sstevel@tonic-gate #include "cryptlib.h"
63*2139Sjp161948 /*
64*2139Sjp161948  * As for unconditional usage of "UPLINK" interface in this module.
65*2139Sjp161948  * Trouble is that unlike Unix file descriptors [which are indexes
66*2139Sjp161948  * in kernel-side per-process table], corresponding descriptors on
67*2139Sjp161948  * platforms which require "UPLINK" interface seem to be indexes
68*2139Sjp161948  * in a user-land, non-global table. Well, in fact they are indexes
69*2139Sjp161948  * in stdio _iob[], and recall that _iob[] was the very reason why
70*2139Sjp161948  * "UPLINK" interface was introduced in first place. But one way on
71*2139Sjp161948  * another. Neither libcrypto or libssl use this BIO meaning that
72*2139Sjp161948  * file descriptors can only be provided by application. Therefore
73*2139Sjp161948  * "UPLINK" calls are due...
74*2139Sjp161948  */
75*2139Sjp161948 #include "bio_lcl.h"
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int fd_write(BIO *h, const char *buf, int num);
780Sstevel@tonic-gate static int fd_read(BIO *h, char *buf, int size);
790Sstevel@tonic-gate static int fd_puts(BIO *h, const char *str);
800Sstevel@tonic-gate static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
810Sstevel@tonic-gate static int fd_new(BIO *h);
820Sstevel@tonic-gate static int fd_free(BIO *data);
830Sstevel@tonic-gate int BIO_fd_should_retry(int s);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static BIO_METHOD methods_fdp=
860Sstevel@tonic-gate 	{
870Sstevel@tonic-gate 	BIO_TYPE_FD,"file descriptor",
880Sstevel@tonic-gate 	fd_write,
890Sstevel@tonic-gate 	fd_read,
900Sstevel@tonic-gate 	fd_puts,
910Sstevel@tonic-gate 	NULL, /* fd_gets, */
920Sstevel@tonic-gate 	fd_ctrl,
930Sstevel@tonic-gate 	fd_new,
940Sstevel@tonic-gate 	fd_free,
950Sstevel@tonic-gate 	NULL,
960Sstevel@tonic-gate 	};
970Sstevel@tonic-gate 
BIO_s_fd(void)980Sstevel@tonic-gate BIO_METHOD *BIO_s_fd(void)
990Sstevel@tonic-gate 	{
1000Sstevel@tonic-gate 	return(&methods_fdp);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
BIO_new_fd(int fd,int close_flag)1030Sstevel@tonic-gate BIO *BIO_new_fd(int fd,int close_flag)
1040Sstevel@tonic-gate 	{
1050Sstevel@tonic-gate 	BIO *ret;
1060Sstevel@tonic-gate 	ret=BIO_new(BIO_s_fd());
1070Sstevel@tonic-gate 	if (ret == NULL) return(NULL);
1080Sstevel@tonic-gate 	BIO_set_fd(ret,fd,close_flag);
1090Sstevel@tonic-gate 	return(ret);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 
fd_new(BIO * bi)1120Sstevel@tonic-gate static int fd_new(BIO *bi)
1130Sstevel@tonic-gate 	{
1140Sstevel@tonic-gate 	bi->init=0;
115*2139Sjp161948 	bi->num=-1;
1160Sstevel@tonic-gate 	bi->ptr=NULL;
117*2139Sjp161948 	bi->flags=BIO_FLAGS_UPLINK; /* essentially redundant */
1180Sstevel@tonic-gate 	return(1);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
fd_free(BIO * a)1210Sstevel@tonic-gate static int fd_free(BIO *a)
1220Sstevel@tonic-gate 	{
1230Sstevel@tonic-gate 	if (a == NULL) return(0);
1240Sstevel@tonic-gate 	if (a->shutdown)
1250Sstevel@tonic-gate 		{
1260Sstevel@tonic-gate 		if (a->init)
1270Sstevel@tonic-gate 			{
128*2139Sjp161948 			UP_close(a->num);
1290Sstevel@tonic-gate 			}
1300Sstevel@tonic-gate 		a->init=0;
131*2139Sjp161948 		a->flags=BIO_FLAGS_UPLINK;
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 	return(1);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
fd_read(BIO * b,char * out,int outl)1360Sstevel@tonic-gate static int fd_read(BIO *b, char *out,int outl)
1370Sstevel@tonic-gate 	{
1380Sstevel@tonic-gate 	int ret=0;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (out != NULL)
1410Sstevel@tonic-gate 		{
1420Sstevel@tonic-gate 		clear_sys_error();
143*2139Sjp161948 		ret=UP_read(b->num,out,outl);
1440Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
1450Sstevel@tonic-gate 		if (ret <= 0)
1460Sstevel@tonic-gate 			{
1470Sstevel@tonic-gate 			if (BIO_fd_should_retry(ret))
1480Sstevel@tonic-gate 				BIO_set_retry_read(b);
1490Sstevel@tonic-gate 			}
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	return(ret);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
fd_write(BIO * b,const char * in,int inl)1540Sstevel@tonic-gate static int fd_write(BIO *b, const char *in, int inl)
1550Sstevel@tonic-gate 	{
1560Sstevel@tonic-gate 	int ret;
1570Sstevel@tonic-gate 	clear_sys_error();
158*2139Sjp161948 	ret=UP_write(b->num,in,inl);
1590Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
1600Sstevel@tonic-gate 	if (ret <= 0)
1610Sstevel@tonic-gate 		{
1620Sstevel@tonic-gate 		if (BIO_fd_should_retry(ret))
1630Sstevel@tonic-gate 			BIO_set_retry_write(b);
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 	return(ret);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
fd_ctrl(BIO * b,int cmd,long num,void * ptr)1680Sstevel@tonic-gate static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
1690Sstevel@tonic-gate 	{
1700Sstevel@tonic-gate 	long ret=1;
1710Sstevel@tonic-gate 	int *ip;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	switch (cmd)
1740Sstevel@tonic-gate 		{
1750Sstevel@tonic-gate 	case BIO_CTRL_RESET:
1760Sstevel@tonic-gate 		num=0;
1770Sstevel@tonic-gate 	case BIO_C_FILE_SEEK:
178*2139Sjp161948 		ret=(long)UP_lseek(b->num,num,0);
1790Sstevel@tonic-gate 		break;
1800Sstevel@tonic-gate 	case BIO_C_FILE_TELL:
1810Sstevel@tonic-gate 	case BIO_CTRL_INFO:
182*2139Sjp161948 		ret=(long)UP_lseek(b->num,0,1);
1830Sstevel@tonic-gate 		break;
1840Sstevel@tonic-gate 	case BIO_C_SET_FD:
1850Sstevel@tonic-gate 		fd_free(b);
1860Sstevel@tonic-gate 		b->num= *((int *)ptr);
1870Sstevel@tonic-gate 		b->shutdown=(int)num;
1880Sstevel@tonic-gate 		b->init=1;
1890Sstevel@tonic-gate 		break;
1900Sstevel@tonic-gate 	case BIO_C_GET_FD:
1910Sstevel@tonic-gate 		if (b->init)
1920Sstevel@tonic-gate 			{
1930Sstevel@tonic-gate 			ip=(int *)ptr;
1940Sstevel@tonic-gate 			if (ip != NULL) *ip=b->num;
1950Sstevel@tonic-gate 			ret=b->num;
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 		else
1980Sstevel@tonic-gate 			ret= -1;
1990Sstevel@tonic-gate 		break;
2000Sstevel@tonic-gate 	case BIO_CTRL_GET_CLOSE:
2010Sstevel@tonic-gate 		ret=b->shutdown;
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	case BIO_CTRL_SET_CLOSE:
2040Sstevel@tonic-gate 		b->shutdown=(int)num;
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 	case BIO_CTRL_PENDING:
2070Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
2080Sstevel@tonic-gate 		ret=0;
2090Sstevel@tonic-gate 		break;
2100Sstevel@tonic-gate 	case BIO_CTRL_DUP:
2110Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
2120Sstevel@tonic-gate 		ret=1;
2130Sstevel@tonic-gate 		break;
2140Sstevel@tonic-gate 	default:
2150Sstevel@tonic-gate 		ret=0;
2160Sstevel@tonic-gate 		break;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	return(ret);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
fd_puts(BIO * bp,const char * str)2210Sstevel@tonic-gate static int fd_puts(BIO *bp, const char *str)
2220Sstevel@tonic-gate 	{
2230Sstevel@tonic-gate 	int n,ret;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	n=strlen(str);
2260Sstevel@tonic-gate 	ret=fd_write(bp,str,n);
2270Sstevel@tonic-gate 	return(ret);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
BIO_fd_should_retry(int i)2300Sstevel@tonic-gate int BIO_fd_should_retry(int i)
2310Sstevel@tonic-gate 	{
2320Sstevel@tonic-gate 	int err;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if ((i == 0) || (i == -1))
2350Sstevel@tonic-gate 		{
2360Sstevel@tonic-gate 		err=get_last_sys_error();
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
2390Sstevel@tonic-gate 		if ((i == -1) && (err == 0))
2400Sstevel@tonic-gate 			return(1);
2410Sstevel@tonic-gate #endif
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		return(BIO_fd_non_fatal_error(err));
2440Sstevel@tonic-gate 		}
2450Sstevel@tonic-gate 	return(0);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
BIO_fd_non_fatal_error(int err)2480Sstevel@tonic-gate int BIO_fd_non_fatal_error(int err)
2490Sstevel@tonic-gate 	{
2500Sstevel@tonic-gate 	switch (err)
2510Sstevel@tonic-gate 		{
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate #ifdef EWOULDBLOCK
2540Sstevel@tonic-gate # ifdef WSAEWOULDBLOCK
2550Sstevel@tonic-gate #  if WSAEWOULDBLOCK != EWOULDBLOCK
2560Sstevel@tonic-gate 	case EWOULDBLOCK:
2570Sstevel@tonic-gate #  endif
2580Sstevel@tonic-gate # else
2590Sstevel@tonic-gate 	case EWOULDBLOCK:
2600Sstevel@tonic-gate # endif
2610Sstevel@tonic-gate #endif
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate #if defined(ENOTCONN)
2640Sstevel@tonic-gate 	case ENOTCONN:
2650Sstevel@tonic-gate #endif
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate #ifdef EINTR
2680Sstevel@tonic-gate 	case EINTR:
2690Sstevel@tonic-gate #endif
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate #ifdef EAGAIN
2720Sstevel@tonic-gate #if EWOULDBLOCK != EAGAIN
2730Sstevel@tonic-gate 	case EAGAIN:
2740Sstevel@tonic-gate # endif
2750Sstevel@tonic-gate #endif
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate #ifdef EPROTO
2780Sstevel@tonic-gate 	case EPROTO:
2790Sstevel@tonic-gate #endif
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate #ifdef EINPROGRESS
2820Sstevel@tonic-gate 	case EINPROGRESS:
2830Sstevel@tonic-gate #endif
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate #ifdef EALREADY
2860Sstevel@tonic-gate 	case EALREADY:
2870Sstevel@tonic-gate #endif
2880Sstevel@tonic-gate 		return(1);
2890Sstevel@tonic-gate 		/* break; */
2900Sstevel@tonic-gate 	default:
2910Sstevel@tonic-gate 		break;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 	return(0);
2940Sstevel@tonic-gate 	}
295