xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bss_sock.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bio/bss_sock.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"
630Sstevel@tonic-gate #include <openssl/bio.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #ifdef WATT32
660Sstevel@tonic-gate #define sock_write SockWrite  /* Watt-32 uses same names */
670Sstevel@tonic-gate #define sock_read  SockRead
680Sstevel@tonic-gate #define sock_puts  SockPuts
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static int sock_write(BIO *h, const char *buf, int num);
720Sstevel@tonic-gate static int sock_read(BIO *h, char *buf, int size);
730Sstevel@tonic-gate static int sock_puts(BIO *h, const char *str);
740Sstevel@tonic-gate static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
750Sstevel@tonic-gate static int sock_new(BIO *h);
760Sstevel@tonic-gate static int sock_free(BIO *data);
770Sstevel@tonic-gate int BIO_sock_should_retry(int s);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static BIO_METHOD methods_sockp=
800Sstevel@tonic-gate 	{
810Sstevel@tonic-gate 	BIO_TYPE_SOCKET,
820Sstevel@tonic-gate 	"socket",
830Sstevel@tonic-gate 	sock_write,
840Sstevel@tonic-gate 	sock_read,
850Sstevel@tonic-gate 	sock_puts,
860Sstevel@tonic-gate 	NULL, /* sock_gets, */
870Sstevel@tonic-gate 	sock_ctrl,
880Sstevel@tonic-gate 	sock_new,
890Sstevel@tonic-gate 	sock_free,
900Sstevel@tonic-gate 	NULL,
910Sstevel@tonic-gate 	};
920Sstevel@tonic-gate 
BIO_s_socket(void)930Sstevel@tonic-gate BIO_METHOD *BIO_s_socket(void)
940Sstevel@tonic-gate 	{
950Sstevel@tonic-gate 	return(&methods_sockp);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
BIO_new_socket(int fd,int close_flag)980Sstevel@tonic-gate BIO *BIO_new_socket(int fd, int close_flag)
990Sstevel@tonic-gate 	{
1000Sstevel@tonic-gate 	BIO *ret;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	ret=BIO_new(BIO_s_socket());
1030Sstevel@tonic-gate 	if (ret == NULL) return(NULL);
1040Sstevel@tonic-gate 	BIO_set_fd(ret,fd,close_flag);
1050Sstevel@tonic-gate 	return(ret);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
sock_new(BIO * bi)1080Sstevel@tonic-gate static int sock_new(BIO *bi)
1090Sstevel@tonic-gate 	{
1100Sstevel@tonic-gate 	bi->init=0;
1110Sstevel@tonic-gate 	bi->num=0;
1120Sstevel@tonic-gate 	bi->ptr=NULL;
1130Sstevel@tonic-gate 	bi->flags=0;
1140Sstevel@tonic-gate 	return(1);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
sock_free(BIO * a)1170Sstevel@tonic-gate static int sock_free(BIO *a)
1180Sstevel@tonic-gate 	{
1190Sstevel@tonic-gate 	if (a == NULL) return(0);
1200Sstevel@tonic-gate 	if (a->shutdown)
1210Sstevel@tonic-gate 		{
1220Sstevel@tonic-gate 		if (a->init)
1230Sstevel@tonic-gate 			{
1240Sstevel@tonic-gate 			SHUTDOWN2(a->num);
1250Sstevel@tonic-gate 			}
1260Sstevel@tonic-gate 		a->init=0;
1270Sstevel@tonic-gate 		a->flags=0;
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 	return(1);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
sock_read(BIO * b,char * out,int outl)1320Sstevel@tonic-gate static int sock_read(BIO *b, char *out, int outl)
1330Sstevel@tonic-gate 	{
1340Sstevel@tonic-gate 	int ret=0;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (out != NULL)
1370Sstevel@tonic-gate 		{
1380Sstevel@tonic-gate 		clear_socket_error();
1390Sstevel@tonic-gate 		ret=readsocket(b->num,out,outl);
1400Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
1410Sstevel@tonic-gate 		if (ret <= 0)
1420Sstevel@tonic-gate 			{
1430Sstevel@tonic-gate 			if (BIO_sock_should_retry(ret))
1440Sstevel@tonic-gate 				BIO_set_retry_read(b);
1450Sstevel@tonic-gate 			}
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 	return(ret);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
sock_write(BIO * b,const char * in,int inl)1500Sstevel@tonic-gate static int sock_write(BIO *b, const char *in, int inl)
1510Sstevel@tonic-gate 	{
1520Sstevel@tonic-gate 	int ret;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	clear_socket_error();
1550Sstevel@tonic-gate 	ret=writesocket(b->num,in,inl);
1560Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
1570Sstevel@tonic-gate 	if (ret <= 0)
1580Sstevel@tonic-gate 		{
1590Sstevel@tonic-gate 		if (BIO_sock_should_retry(ret))
1600Sstevel@tonic-gate 			BIO_set_retry_write(b);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	return(ret);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
sock_ctrl(BIO * b,int cmd,long num,void * ptr)1650Sstevel@tonic-gate static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
1660Sstevel@tonic-gate 	{
1670Sstevel@tonic-gate 	long ret=1;
1680Sstevel@tonic-gate 	int *ip;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	switch (cmd)
1710Sstevel@tonic-gate 		{
1720Sstevel@tonic-gate 	case BIO_CTRL_RESET:
1730Sstevel@tonic-gate 		num=0;
1740Sstevel@tonic-gate 	case BIO_C_FILE_SEEK:
1750Sstevel@tonic-gate 		ret=0;
1760Sstevel@tonic-gate 		break;
1770Sstevel@tonic-gate 	case BIO_C_FILE_TELL:
1780Sstevel@tonic-gate 	case BIO_CTRL_INFO:
1790Sstevel@tonic-gate 		ret=0;
1800Sstevel@tonic-gate 		break;
1810Sstevel@tonic-gate 	case BIO_C_SET_FD:
1820Sstevel@tonic-gate 		sock_free(b);
1830Sstevel@tonic-gate 		b->num= *((int *)ptr);
1840Sstevel@tonic-gate 		b->shutdown=(int)num;
1850Sstevel@tonic-gate 		b->init=1;
1860Sstevel@tonic-gate 		break;
1870Sstevel@tonic-gate 	case BIO_C_GET_FD:
1880Sstevel@tonic-gate 		if (b->init)
1890Sstevel@tonic-gate 			{
1900Sstevel@tonic-gate 			ip=(int *)ptr;
1910Sstevel@tonic-gate 			if (ip != NULL) *ip=b->num;
1920Sstevel@tonic-gate 			ret=b->num;
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 		else
1950Sstevel@tonic-gate 			ret= -1;
1960Sstevel@tonic-gate 		break;
1970Sstevel@tonic-gate 	case BIO_CTRL_GET_CLOSE:
1980Sstevel@tonic-gate 		ret=b->shutdown;
1990Sstevel@tonic-gate 		break;
2000Sstevel@tonic-gate 	case BIO_CTRL_SET_CLOSE:
2010Sstevel@tonic-gate 		b->shutdown=(int)num;
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	case BIO_CTRL_PENDING:
2040Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
2050Sstevel@tonic-gate 		ret=0;
2060Sstevel@tonic-gate 		break;
2070Sstevel@tonic-gate 	case BIO_CTRL_DUP:
2080Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
2090Sstevel@tonic-gate 		ret=1;
2100Sstevel@tonic-gate 		break;
2110Sstevel@tonic-gate 	default:
2120Sstevel@tonic-gate 		ret=0;
2130Sstevel@tonic-gate 		break;
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 	return(ret);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
sock_puts(BIO * bp,const char * str)2180Sstevel@tonic-gate static int sock_puts(BIO *bp, const char *str)
2190Sstevel@tonic-gate 	{
2200Sstevel@tonic-gate 	int n,ret;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	n=strlen(str);
2230Sstevel@tonic-gate 	ret=sock_write(bp,str,n);
2240Sstevel@tonic-gate 	return(ret);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
BIO_sock_should_retry(int i)2270Sstevel@tonic-gate int BIO_sock_should_retry(int i)
2280Sstevel@tonic-gate 	{
2290Sstevel@tonic-gate 	int err;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if ((i == 0) || (i == -1))
2320Sstevel@tonic-gate 		{
2330Sstevel@tonic-gate 		err=get_last_socket_error();
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
2360Sstevel@tonic-gate 		if ((i == -1) && (err == 0))
2370Sstevel@tonic-gate 			return(1);
2380Sstevel@tonic-gate #endif
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		return(BIO_sock_non_fatal_error(err));
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 	return(0);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
BIO_sock_non_fatal_error(int err)2450Sstevel@tonic-gate int BIO_sock_non_fatal_error(int err)
2460Sstevel@tonic-gate 	{
2470Sstevel@tonic-gate 	switch (err)
2480Sstevel@tonic-gate 		{
249*2139Sjp161948 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
2500Sstevel@tonic-gate # if defined(WSAEWOULDBLOCK)
2510Sstevel@tonic-gate 	case WSAEWOULDBLOCK:
2520Sstevel@tonic-gate # endif
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate # if 0 /* This appears to always be an error */
2550Sstevel@tonic-gate #  if defined(WSAENOTCONN)
2560Sstevel@tonic-gate 	case WSAENOTCONN:
2570Sstevel@tonic-gate #  endif
2580Sstevel@tonic-gate # endif
2590Sstevel@tonic-gate #endif
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate #ifdef EWOULDBLOCK
2620Sstevel@tonic-gate # ifdef WSAEWOULDBLOCK
2630Sstevel@tonic-gate #  if WSAEWOULDBLOCK != EWOULDBLOCK
2640Sstevel@tonic-gate 	case EWOULDBLOCK:
2650Sstevel@tonic-gate #  endif
2660Sstevel@tonic-gate # else
2670Sstevel@tonic-gate 	case EWOULDBLOCK:
2680Sstevel@tonic-gate # endif
2690Sstevel@tonic-gate #endif
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate #if defined(ENOTCONN)
2720Sstevel@tonic-gate 	case ENOTCONN:
2730Sstevel@tonic-gate #endif
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate #ifdef EINTR
2760Sstevel@tonic-gate 	case EINTR:
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate #ifdef EAGAIN
280*2139Sjp161948 # if EWOULDBLOCK != EAGAIN
2810Sstevel@tonic-gate 	case EAGAIN:
2820Sstevel@tonic-gate # endif
2830Sstevel@tonic-gate #endif
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate #ifdef EPROTO
2860Sstevel@tonic-gate 	case EPROTO:
2870Sstevel@tonic-gate #endif
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate #ifdef EINPROGRESS
2900Sstevel@tonic-gate 	case EINPROGRESS:
2910Sstevel@tonic-gate #endif
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate #ifdef EALREADY
2940Sstevel@tonic-gate 	case EALREADY:
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate 		return(1);
2970Sstevel@tonic-gate 		/* break; */
2980Sstevel@tonic-gate 	default:
2990Sstevel@tonic-gate 		break;
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 	return(0);
3020Sstevel@tonic-gate 	}
303