diff options
Diffstat (limited to 'crypto/bio')
-rw-r--r-- | crypto/bio/Makefile.ssl | 92 | ||||
-rw-r--r-- | crypto/bio/b_dump.c | 125 | ||||
-rw-r--r-- | crypto/bio/b_print.c | 92 | ||||
-rw-r--r-- | crypto/bio/b_sock.c | 512 | ||||
-rw-r--r-- | crypto/bio/bf_buff.c | 489 | ||||
-rw-r--r-- | crypto/bio/bf_nbio.c | 266 | ||||
-rw-r--r-- | crypto/bio/bf_null.c | 194 | ||||
-rw-r--r-- | crypto/bio/bio.err | 43 | ||||
-rw-r--r-- | crypto/bio/bio.h | 597 | ||||
-rw-r--r-- | crypto/bio/bio_cb.c | 132 | ||||
-rw-r--r-- | crypto/bio/bio_err.c | 121 | ||||
-rw-r--r-- | crypto/bio/bio_lib.c | 447 | ||||
-rw-r--r-- | crypto/bio/bss_acpt.c | 484 | ||||
-rw-r--r-- | crypto/bio/bss_conn.c | 558 | ||||
-rw-r--r-- | crypto/bio/bss_fd.c | 62 | ||||
-rw-r--r-- | crypto/bio/bss_file.c | 334 | ||||
-rw-r--r-- | crypto/bio/bss_mem.c | 296 | ||||
-rw-r--r-- | crypto/bio/bss_null.c | 176 | ||||
-rw-r--r-- | crypto/bio/bss_rtcp.c | 296 | ||||
-rw-r--r-- | crypto/bio/bss_sock.c | 449 |
20 files changed, 5765 insertions, 0 deletions
diff --git a/crypto/bio/Makefile.ssl b/crypto/bio/Makefile.ssl new file mode 100644 index 0000000000..9d21a3594d --- /dev/null +++ b/crypto/bio/Makefile.ssl @@ -0,0 +1,92 @@ +# +# SSLeay/crypto/bio/Makefile +# + +DIR= bio +TOP= ../.. +CC= cc +INCLUDES= -I.. -I../../include +CFLAG=-g +INSTALLTOP=/usr/local/ssl +MAKE= make -f Makefile.ssl +MAKEDEPEND= makedepend -f Makefile.ssl +MAKEFILE= Makefile.ssl +AR= ar r + +CFLAGS= $(INCLUDES) $(CFLAG) + +ERR=bio +ERRC=bio_err +GENERAL=Makefile +TEST= +APPS= + +LIB=$(TOP)/libcrypto.a +LIBSRC= bio_lib.c bio_cb.c $(ERRC).c \ + bss_mem.c bss_null.c bss_fd.c \ + bss_file.c bss_sock.c bss_conn.c \ + bf_null.c bf_buff.c b_print.c b_dump.c \ + b_sock.c bss_acpt.c bf_nbio.c +LIBOBJ= bio_lib.o bio_cb.o $(ERRC).o \ + bss_mem.o bss_null.o bss_fd.o \ + bss_file.o bss_sock.o bss_conn.o \ + bf_null.o bf_buff.o b_print.o b_dump.o \ + b_sock.o bss_acpt.o bf_nbio.o + +SRC= $(LIBSRC) + +EXHEADER= bio.h +HEADER= $(EXHEADER) + +ALL= $(GENERAL) $(SRC) $(HEADER) + +top: + (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) + +all: lib + +lib: $(LIBOBJ) + $(AR) $(LIB) $(LIBOBJ) + sh $(TOP)/util/ranlib.sh $(LIB) + @touch lib + +files: + perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO + +links: + /bin/rm -f Makefile + $(TOP)/util/point.sh Makefile.ssl Makefile; + $(TOP)/util/mklink.sh ../../include $(EXHEADER) + $(TOP)/util/mklink.sh ../../test $(TEST) + $(TOP)/util/mklink.sh ../../apps $(APPS) + +install: + @for i in $(EXHEADER) ; \ + do \ + (cp $$i $(INSTALLTOP)/include/$$i; \ + chmod 644 $(INSTALLTOP)/include/$$i ); \ + done; + +tags: + ctags $(SRC) + +tests: + +lint: + lint -DLINT $(INCLUDES) $(SRC)>fluff + +depend: + $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC) + +dclean: + perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new + mv -f Makefile.new $(MAKEFILE) + +clean: + /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff + +errors: + perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h + perl ../err/err_genc.pl $(ERR).h $(ERRC).c + +# DO NOT DELETE THIS LINE -- make depend depends on it. diff --git a/crypto/bio/b_dump.c b/crypto/bio/b_dump.c new file mode 100644 index 0000000000..287aff2a5e --- /dev/null +++ b/crypto/bio/b_dump.c @@ -0,0 +1,125 @@ +/* crypto/bio/b_dump.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +/* + * Stolen from tjh's ssl/ssl_trc.c stuff. + */ + +#include <stdio.h> +#include "cryptlib.h" +#include "bio.h" + +#define TRUNCATE +#define DUMP_WIDTH 16 + +int BIO_dump(bio,s,len) +BIO *bio; +char *s; +int len; +{ + int ret=0; + char buf[160+1],tmp[20]; + int i,j,rows,trunc; + unsigned char ch; + + trunc=0; + +#ifdef TRUNCATE + for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--) + trunc++; +#endif + + rows=(len/DUMP_WIDTH); + if ((rows*DUMP_WIDTH)<len) + rows++; + for(i=0;i<rows;i++) { + buf[0]='\0'; /* start with empty string */ + sprintf(tmp,"%04x - ",i*DUMP_WIDTH); + strcpy(buf,tmp); + for(j=0;j<DUMP_WIDTH;j++) { + if (((i*DUMP_WIDTH)+j)>=len) { + strcat(buf," "); + } else { + ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff; + sprintf(tmp,"%02x%c",ch,j==7?'-':' '); + strcat(buf,tmp); + } + } + strcat(buf," "); + for(j=0;j<DUMP_WIDTH;j++) { + if (((i*DUMP_WIDTH)+j)>=len) + break; + ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff; + sprintf(tmp,"%c",((ch>=' ')&&(ch<='~'))?ch:'.'); + strcat(buf,tmp); + } + strcat(buf,"\n"); + /* if this is the last call then update the ddt_dump thing so that + * we will move the selection point in the debug window + */ + ret+=BIO_write(bio,(char *)buf,strlen(buf)); + } +#ifdef TRUNCATE + if (trunc > 0) { + sprintf(buf,"%04x - <SPACES/NULS>\n",len+trunc); + ret+=BIO_write(bio,(char *)buf,strlen(buf)); + } +#endif + return(ret); +} + diff --git a/crypto/bio/b_print.c b/crypto/bio/b_print.c new file mode 100644 index 0000000000..0296296a7d --- /dev/null +++ b/crypto/bio/b_print.c @@ -0,0 +1,92 @@ +/* crypto/bio/b_print.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +/* + * Stolen from tjh's ssl/ssl_trc.c stuff. + */ + +#include <stdio.h> +#include "cryptlib.h" +#include "bio.h" + +int BIO_printf ( VAR_PLIST( BIO *, bio ) ) +VAR_ALIST + { + VAR_BDEFN(args, BIO *, bio); + char *format; + int ret; + MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */ + + VAR_INIT(args, BIO *, bio); + VAR_ARG(args, char *, format); + + hugebuf[0]='\0'; + +/* no-one uses _doprnt anymore and it appears to be broken under SunOS 4.1.4 */ +#if 0 && defined(sun) && !defined(VAR_ANSI) /**/ + _doprnt(hugebuf,format,args); +#else /* !sun */ + vsprintf(hugebuf,format,args); +#endif /* sun */ + + ret=BIO_write(bio,hugebuf,strlen(hugebuf)); + + VAR_END( args ); + return(ret); + } + diff --git a/crypto/bio/b_sock.c b/crypto/bio/b_sock.c new file mode 100644 index 0000000000..adab62135e --- /dev/null +++ b/crypto/bio/b_sock.c @@ -0,0 +1,512 @@ +/* crypto/bio/b_sock.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#ifndef NO_SOCK + +#include <stdio.h> +#include <errno.h> +#define USE_SOCKETS +#include "cryptlib.h" +#include "bio.h" + +/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ + +#ifdef WIN16 +#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ +#else +#define SOCKET_PROTOCOL IPPROTO_TCP +#endif + +#ifdef WINDOWS +static int wsa_init_done=0; +#endif + +unsigned long BIO_ghbn_hits=0L; +unsigned long BIO_ghbn_miss=0L; + +#ifndef NOPROTO +static int get_ip(char *str,unsigned char *ip); +#else +static int get_ip(); +#endif + +int BIO_get_host_ip(str,ip) +char *str; +unsigned char *ip; + { + int i; + struct hostent *he; + + i=get_ip(str,ip); + if (i > 0) return(1); + if (i < 0) + { + BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS); + return(0); + } + else + { /* do a gethostbyname */ + if (!BIO_sock_init()) return(0); + + he=BIO_gethostbyname(str); + if (he == NULL) + { + BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP); + return(0); + } + + /* cast to short because of win16 winsock definition */ + if ((short)he->h_addrtype != AF_INET) + { + BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); + return(0); + } + for (i=0; i<4; i++) + ip[i]=he->h_addr_list[0][i]; + } + return(1); + } + +int BIO_get_port(str,port_ptr) +char *str; +short *port_ptr; + { + int i; + struct servent *s; + + if (str == NULL) + { + BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED); + return(0); + } + i=atoi(str); + if (i != 0) + *port_ptr=(unsigned short)i; + else + { + s=getservbyname(str,"tcp"); + if (s == NULL) + { + if (strcmp(str,"http") == 0) + *port_ptr=80; + else if (strcmp(str,"telnet") == 0) + *port_ptr=23; + else if (strcmp(str,"socks") == 0) + *port_ptr=1080; + else if (strcmp(str,"https") == 0) + *port_ptr=443; + else if (strcmp(str,"ssl") == 0) + *port_ptr=443; + else if (strcmp(str,"ftp") == 0) + *port_ptr=21; + else if (strcmp(str,"gopher") == 0) + *port_ptr=70; +#if 0 + else if (strcmp(str,"wais") == 0) + *port_ptr=21; +#endif + else + { + SYSerr(SYS_F_GETSERVBYNAME,errno); + return(0); + } + return(1); + } + *port_ptr=htons((unsigned short)s->s_port); + } + return(1); + } + +int BIO_sock_error(sock) +int sock; + { + int j,i,size; + + size=sizeof(int); + + i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(char *)&j,&size); + if (i < 0) + return(1); + else + return(j); + } + +#define GHBN_NUM 4 +static struct ghbn_cache_st + { + char name[128]; + struct hostent ent; + unsigned long order; + } ghbn_cache[GHBN_NUM]; + +struct hostent *BIO_gethostbyname(name) +char *name; + { + struct hostent *ret; + int i,lowi=0; + unsigned long low= (unsigned long)-1; + + CRYPTO_w_lock(CRYPTO_LOCK_BIO_GETHOSTBYNAME); + if (strlen(name) < 128) + { + for (i=0; i<GHBN_NUM; i++) + { + if (low > ghbn_cache[i].order) + { + low=ghbn_cache[i].order; + lowi=i; + } + if (ghbn_cache[i].order > 0) + { + if (strncmp(name,ghbn_cache[i].name,128) == 0) + break; + } + } + } + else + i=GHBN_NUM; + + if (i == GHBN_NUM) /* no hit*/ + { + BIO_ghbn_miss++; + ret=gethostbyname(name); + if (ret == NULL) return(NULL); + /* else add to cache */ + strncpy(ghbn_cache[lowi].name,name,128); + memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent)); + ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits; + } + else + { + BIO_ghbn_hits++; + ret= &(ghbn_cache[i].ent); + ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits; + } + CRYPTO_w_unlock(CRYPTO_LOCK_BIO_GETHOSTBYNAME); + return(ret); + } + +int BIO_sock_init() + { +#ifdef WINDOWS + static struct WSAData wsa_state; + + if (!wsa_init_done) + { + int err; + +#ifdef SIGINT + signal(SIGINT,(void (*)(int))BIO_sock_cleanup); +#endif + wsa_init_done=1; + memset(&wsa_state,0,sizeof(wsa_state)); + if (WSAStartup(0x0101,&wsa_state)!=0) + { + err=WSAGetLastError(); + SYSerr(SYS_F_WSASTARTUP,err); + BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP); + return(-1); + } + } +#endif /* WINDOWS */ + return(1); + } + +void BIO_sock_cleanup() + { +#ifdef WINDOWS + if (wsa_init_done) + { + wsa_init_done=0; + WSACancelBlockingCall(); + WSACleanup(); + } +#endif + } + +int BIO_socket_ioctl(fd,type,arg) +int fd; +long type; +unsigned long *arg; + { + int i,err; + +#ifdef WINDOWS + i=ioctlsocket(fd,type,arg); +#else + i=ioctl(fd,type,arg); +#endif + if (i < 0) + { +#ifdef WINDOWS + err=WSAGetLastError(); +#else + err=errno; +#endif + SYSerr(SYS_F_IOCTLSOCKET,err); + } + return(i); + } + +/* The reason I have implemented this instead of using sscanf is because + * Visual C 1.52c gives an unresolved external when linking a DLL :-( */ +static int get_ip(str,ip) +char *str; +unsigned char ip[4]; + { + unsigned int tmp[4]; + int num=0,c,ok=0; + + tmp[0]=tmp[1]=tmp[2]=tmp[3]=0; + + for (;;) + { + c= *(str++); + if ((c >= '0') && (c <= '9')) + { + ok=1; + tmp[num]=tmp[num]*10+c-'0'; + if (tmp[num] > 255) return(-1); + } + else if (c == '.') + { + if (!ok) return(-1); + if (num == 3) break; + num++; + ok=0; + } + else if ((num == 3) && ok) + break; + else + return(0); + } + ip[0]=tmp[0]; + ip[1]=tmp[1]; + ip[2]=tmp[2]; + ip[3]=tmp[3]; + return(1); + } + +int BIO_get_accept_socket(host) +char *host; + { + int ret=0; + struct sockaddr_in server; + int s= -1; + unsigned char ip[4]; + short port; + char *str,*h,*p,*e; + unsigned long l; + + if (!BIO_sock_init()) return(INVALID_SOCKET); + + if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET); + + h=p=NULL; + h=str; + for (e=str; *e; e++) + { + if (*e == ':') + { + p= &(e[1]); + *e='\0'; + } + else if (*e == '/') + { + *e='\0'; + break; + } + } + + if (p == NULL) + { + p=h; + h="*"; + } + + if (!BIO_get_port(p,&port)) return(INVALID_SOCKET); + + memset((char *)&server,0,sizeof(server)); + server.sin_family=AF_INET; + server.sin_port=htons((unsigned short)port); + + if (strcmp(h,"*") == 0) + server.sin_addr.s_addr=INADDR_ANY; + else + { + if (!BIO_get_host_ip(h,&(ip[0]))) return(INVALID_SOCKET); + l=(unsigned long) + ((unsigned long)ip[0]<<24L)| + ((unsigned long)ip[0]<<16L)| + ((unsigned long)ip[0]<< 8L)| + ((unsigned long)ip[0]); + server.sin_addr.s_addr=htonl(l); + } + + s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); + if (s == INVALID_SOCKET) + { +#ifdef WINDOWS + errno=WSAGetLastError(); +#endif + SYSerr(SYS_F_SOCKET,errno); + BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET); + goto err; + } + if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) + { +#ifdef WINDOWS + errno=WSAGetLastError(); +#endif + SYSerr(SYS_F_BIND,errno); + BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET); + goto err; + } + if (listen(s,5) == -1) + { +#ifdef WINDOWS + errno=WSAGetLastError(); +#endif + SYSerr(SYS_F_LISTEN,errno); + BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET); + goto err; + } + ret=1; +err: + if (str != NULL) Free(str); + if ((ret == 0) && (s != INVALID_SOCKET)) + { +#ifdef WINDOWS + closesocket(s); +#else + close(s); +#endif + s= INVALID_SOCKET; + } + return(s); + } + +int BIO_accept(sock,addr) +int sock; +char **addr; + { + int ret=INVALID_SOCKET; + static struct sockaddr_in from; + unsigned long l; + short port; + int len; + char *p; + + memset((char *)&from,0,sizeof(from)); + len=sizeof(from); + ret=accept(sock,(struct sockaddr *)&from,&len); + if (ret == INVALID_SOCKET) + { +#ifdef WINDOWS + errno=WSAGetLastError(); +#endif + SYSerr(SYS_F_ACCEPT,errno); + BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR); + goto end; + } + + if (addr == NULL) goto end; + + l=ntohl(from.sin_addr.s_addr); + port=ntohs(from.sin_port); + if (*addr == NULL) + { + if ((p=Malloc(24)) == NULL) + { + BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE); + goto end; + } + *addr=p; + } + sprintf(*addr,"%d.%d.%d.%d:%d", + (unsigned char)(l>>24L)&0xff, + (unsigned char)(l>>16L)&0xff, + (unsigned char)(l>> 8L)&0xff, + (unsigned char)(l )&0xff, + port); +end: + return(ret); + } + +int BIO_set_tcp_ndelay(s,on) +int s; +int on; + { + int ret=0; +#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) + int opt; + +#ifdef SOL_TCP + opt=SOL_TCP; +#else +#ifdef IPPROTO_TCP + opt=IPPROTO_TCP; +#endif +#endif + + ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on)); +#endif + return(ret == 0); + } +#endif + diff --git a/crypto/bio/bf_buff.c b/crypto/bio/bf_buff.c new file mode 100644 index 0000000000..ac3ba14fe0 --- /dev/null +++ b/crypto/bio/bf_buff.c @@ -0,0 +1,489 @@ +/* crypto/bio/bf_buff.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <errno.h> +#include "cryptlib.h" +#include "bio.h" +#include "evp.h" + +#ifndef NOPROTO +static int buffer_write(BIO *h,char *buf,int num); +static int buffer_read(BIO *h,char *buf,int size); +static int buffer_puts(BIO *h,char *str); +static int buffer_gets(BIO *h,char *str,int size); +static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int buffer_new(BIO *h); +static int buffer_free(BIO *data); +#else +static int buffer_write(); +static int buffer_read(); +static int buffer_puts(); +static int buffer_gets(); +static long buffer_ctrl(); +static int buffer_new(); +static int buffer_free(); +#endif + +#define DEFAULT_BUFFER_SIZE 1024 + +static BIO_METHOD methods_buffer= + { + BIO_TYPE_BUFFER,"buffer", + buffer_write, + buffer_read, + buffer_puts, + buffer_gets, + buffer_ctrl, + buffer_new, + buffer_free, + }; + +BIO_METHOD *BIO_f_buffer() + { + return(&methods_buffer); + } + +static int buffer_new(bi) +BIO *bi; + { + BIO_F_BUFFER_CTX *ctx; + + ctx=(BIO_F_BUFFER_CTX *)Malloc(sizeof(BIO_F_BUFFER_CTX)); + if (ctx == NULL) return(0); + ctx->ibuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); + if (ctx->ibuf == NULL) { Free(ctx); return(0); } + ctx->obuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); + if (ctx->obuf == NULL) { Free(ctx->ibuf); Free(ctx); return(0); } + ctx->ibuf_size=DEFAULT_BUFFER_SIZE; + ctx->obuf_size=DEFAULT_BUFFER_SIZE; + ctx->ibuf_len=0; + ctx->ibuf_off=0; + ctx->obuf_len=0; + ctx->obuf_off=0; + + bi->init=1; + bi->ptr=(char *)ctx; + bi->flags=0; + return(1); + } + +static int buffer_free(a) +BIO *a; + { + BIO_F_BUFFER_CTX *b; + + if (a == NULL) return(0); + b=(BIO_F_BUFFER_CTX *)a->ptr; + if (b->ibuf != NULL) Free(b->ibuf); + if (b->obuf != NULL) Free(b->obuf); + Free(a->ptr); + a->ptr=NULL; + a->init=0; + a->flags=0; + return(1); + } + +static int buffer_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + int i,num=0; + BIO_F_BUFFER_CTX *ctx; + + if (out == NULL) return(0); + ctx=(BIO_F_BUFFER_CTX *)b->ptr; + + if ((ctx == NULL) || (b->next_bio == NULL)) return(0); + num=0; + BIO_clear_retry_flags(b); + +start: + i=ctx->ibuf_len; + /* If there is stuff left over, grab it */ + if (i != 0) + { + if (i > outl) i=outl; + memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i); + ctx->ibuf_off+=i; + ctx->ibuf_len-=i; + num+=i; + if (outl == i) return(num); + outl-=i; + out+=i; + } + + /* We may have done a partial read. try to do more. + * We have nothing in the buffer. + * If we get an error and have read some data, just return it + * and let them retry to get the error again. + * copy direct to parent address space */ + if (outl > ctx->ibuf_size) + { + for (;;) + { + i=BIO_read(b->next_bio,out,outl); + if (i <= 0) + { + BIO_copy_next_retry(b); + if (i < 0) return((num > 0)?num:i); + if (i == 0) return(num); + } + num+=i; + if (outl == i) return(num); + out+=i; + outl-=i; + } + } + /* else */ + + /* we are going to be doing some buffering */ + i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size); + if (i <= 0) + { + BIO_copy_next_retry(b); + if (i < 0) return((num > 0)?num:i); + if (i == 0) return(num); + } + ctx->ibuf_off=0; + ctx->ibuf_len=i; + + /* Lets re-read using ourselves :-) */ + goto start; + } + +static int buffer_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + int i,num=0; + BIO_F_BUFFER_CTX *ctx; + + if ((in == NULL) || (inl <= 0)) return(0); + ctx=(BIO_F_BUFFER_CTX *)b->ptr; + if ((ctx == NULL) || (b->next_bio == NULL)) return(0); + + BIO_clear_retry_flags(b); +start: + i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off); + /* add to buffer and return */ + if (i >= inl) + { + memcpy(&(ctx->obuf[ctx->obuf_len]),in,inl); + ctx->obuf_len+=inl; + return(num+inl); + } + /* else */ + /* stuff already in buffer, so add to it first, then flush */ + if (ctx->obuf_len != 0) + { + if (i > 0) /* lets fill it up if we can */ + { + memcpy(&(ctx->obuf[ctx->obuf_len]),in,i); + in+=i; + inl-=i; + num+=i; + ctx->obuf_len+=i; + } + /* we now have a full buffer needing flushing */ + for (;;) + { + i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]), + ctx->obuf_len); + if (i <= 0) + { + BIO_copy_next_retry(b); + + if (i < 0) return((num > 0)?num:i); + if (i == 0) return(num); + } + ctx->obuf_off+=i; + ctx->obuf_len-=i; + if (ctx->obuf_len == 0) break; + } + } + /* we only get here if the buffer has been flushed and we + * still have stuff to write */ + ctx->obuf_off=0; + + /* we now have inl bytes to write */ + while (inl >= ctx->obuf_size) + { + i=BIO_write(b->next_bio,in,inl); + if (i <= 0) + { + BIO_copy_next_retry(b); + if (i < 0) return((num > 0)?num:i); + if (i == 0) return(num); + } + num+=i; + in+=i; + inl-=i; + if (inl == 0) return(num); + } + + /* copy the rest into the buffer since we have only a small + * amount left */ + goto start; + } + +static long buffer_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + BIO *dbio; + BIO_F_BUFFER_CTX *ctx; + long ret=1; + char *p1,*p2; + int r,i,*ip; + int ibs,obs; + + ctx=(BIO_F_BUFFER_CTX *)b->ptr; + + switch (cmd) + { + case BIO_CTRL_RESET: + ctx->ibuf_off=0; + ctx->ibuf_len=0; + ctx->obuf_off=0; + ctx->obuf_len=0; + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + break; + case BIO_CTRL_INFO: + ret=(long)ctx->obuf_len; + break; + case BIO_C_GET_BUFF_NUM_LINES: + ret=0; + p1=ctx->ibuf; + for (i=ctx->ibuf_off; i<ctx->ibuf_len; i++) + { + if (p1[i] == '\n') ret++; + } + break; + case BIO_CTRL_WPENDING: + ret=(long)ctx->obuf_len; + if (ret == 0) + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + break; + case BIO_CTRL_PENDING: + ret=(long)ctx->ibuf_len; + if (ret == 0) + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + break; + case BIO_C_SET_BUFF_SIZE: + if (ptr != NULL) + { + ip=(int *)ptr; + if (*ip == 0) + { + ibs=(int)num; + obs=ctx->obuf_size; + } + else /* if (*ip == 1) */ + { + ibs=ctx->ibuf_size; + obs=(int)num; + } + } + else + { + ibs=(int)num; + obs=(int)num; + } + p1=ctx->ibuf; + p2=ctx->obuf; + if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) + { + p1=(char *)Malloc((int)num); + if (p1 == NULL) { ret=0; break; } + } + if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) + { + p2=(char *)Malloc((int)num); + if (p2 == NULL) + { + ret=0; + if (p1 != ctx->ibuf) Free(p1); + break; + } + } + if (ctx->ibuf != p1) + { + Free(ctx->ibuf); + ctx->ibuf=p1; + ctx->ibuf_off=0; + ctx->ibuf_len=0; + ctx->ibuf_size=ibs; + } + if (ctx->obuf != p2) + { + Free(ctx->obuf); + ctx->obuf=p2; + ctx->obuf_off=0; + ctx->obuf_len=0; + ctx->obuf_size=obs; + } + break; + case BIO_C_DO_STATE_MACHINE: + BIO_clear_retry_flags(b); + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + BIO_copy_next_retry(b); + break; + + case BIO_CTRL_FLUSH: + if (ctx->obuf_len <= 0) + break; + + for (;;) + { + BIO_clear_retry_flags(b); + if (ctx->obuf_len > ctx->obuf_off) + { + r=BIO_write(b->next_bio, + &(ctx->obuf[ctx->obuf_off]), + ctx->obuf_len-ctx->obuf_off); +#if 0 +fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_off,r); +#endif + BIO_copy_next_retry(b); + if (r <= 0) return((long)r); + ctx->obuf_off+=r; + } + else + { + ctx->obuf_len=0; + ctx->obuf_off=0; + ret=1; + break; + } + } + break; + case BIO_CTRL_DUP: + dbio=(BIO *)ptr; + if ( !BIO_set_read_buffer_size(dbio,ctx->ibuf_size) || + !BIO_set_write_buffer_size(dbio,ctx->obuf_size)) + ret=0; + break; + default: + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + break; + } + return(ret); + } + +static int buffer_gets(b,buf,size) +BIO *b; +char *buf; +int size; + { + BIO_F_BUFFER_CTX *ctx; + int num=0,i; + char *p; + + ctx=(BIO_F_BUFFER_CTX *)b->ptr; + size--; + BIO_clear_retry_flags(b); + + for (;;) + { + if (ctx->ibuf_len != 0) + { + p= &(ctx->ibuf[ctx->ibuf_off]); + for (i=0; (i<ctx->ibuf_len) && (i<(size-1)); i++) + { + *(buf++)=p[i]; + if (p[i] == '\n') break; + } + num+=i; + size-=i; + ctx->ibuf_len-=i; + ctx->ibuf_off+=i; + if (p[i] == '\n') + { + buf[i+1]='\0'; + ctx->ibuf_len--; + ctx->ibuf_off++; + return(num); + } + } + else /* read another chunk */ + { + i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size); + if (i <= 0) + { + BIO_copy_next_retry(b); + if (i < 0) return((num > 0)?num:i); + if (i == 0) return(num); + } + ctx->ibuf_len=i; + ctx->ibuf_off=0; + } + } + } + +static int buffer_puts(b,str) +BIO *b; +char *str; + { + return(BIO_write(b,str,strlen(str))); + } + diff --git a/crypto/bio/bf_nbio.c b/crypto/bio/bf_nbio.c new file mode 100644 index 0000000000..b493100ba4 --- /dev/null +++ b/crypto/bio/bf_nbio.c @@ -0,0 +1,266 @@ +/* crypto/bio/bf_nbio.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <errno.h> +#include "cryptlib.h" +#include "rand.h" +#include "bio.h" +#include "evp.h" + +/* BIO_put and BIO_get both add to the digest, + * BIO_gets returns the digest */ + +#ifndef NOPROTO +static int nbiof_write(BIO *h,char *buf,int num); +static int nbiof_read(BIO *h,char *buf,int size); +static int nbiof_puts(BIO *h,char *str); +static int nbiof_gets(BIO *h,char *str,int size); +static long nbiof_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int nbiof_new(BIO *h); +static int nbiof_free(BIO *data); +#else +static int nbiof_write(); +static int nbiof_read(); +static int nbiof_puts(); +static int nbiof_gets(); +static long nbiof_ctrl(); +static int nbiof_new(); +static int nbiof_free(); +#endif + +typedef struct nbio_test_st + { + /* only set if we sent a 'should retry' error */ + int lrn; + int lwn; + } NBIO_TEST; + +static BIO_METHOD methods_nbiof= + { + BIO_TYPE_NBIO_TEST,"non-blocking IO test filter", + nbiof_write, + nbiof_read, + nbiof_puts, + nbiof_gets, + nbiof_ctrl, + nbiof_new, + nbiof_free, + }; + +BIO_METHOD *BIO_f_nbio_test() + { + return(&methods_nbiof); + } + +static int nbiof_new(bi) +BIO *bi; + { + NBIO_TEST *nt; + + nt=(NBIO_TEST *)Malloc(sizeof(NBIO_TEST)); + nt->lrn= -1; + nt->lwn= -1; + bi->ptr=(char *)nt; + bi->init=1; + bi->flags=0; + return(1); + } + +static int nbiof_free(a) +BIO *a; + { + if (a == NULL) return(0); + if (a->ptr != NULL) + Free(a->ptr); + a->ptr=NULL; + a->init=0; + a->flags=0; + return(1); + } + +static int nbiof_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + NBIO_TEST *nt; + int ret=0; +#if 0 + int num; + unsigned char n; +#endif + + if (out == NULL) return(0); + if (b->next_bio == NULL) return(0); + nt=(NBIO_TEST *)b->ptr; + + BIO_clear_retry_flags(b); +#if 0 + RAND_bytes(&n,1); + num=(n&0x07); + + if (outl > num) outl=num; + + if (num == 0) + { + ret= -1; + BIO_set_retry_read(b); + } + else +#endif + { + ret=BIO_read(b->next_bio,out,outl); + if (ret < 0) + BIO_copy_next_retry(b); + } + return(ret); + } + +static int nbiof_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + NBIO_TEST *nt; + int ret=0; + int num; + unsigned char n; + + if ((in == NULL) || (inl <= 0)) return(0); + if (b->next_bio == NULL) return(0); + nt=(NBIO_TEST *)b->ptr; + + BIO_clear_retry_flags(b); + +#if 1 + if (nt->lwn > 0) + { + num=nt->lwn; + nt->lwn=0; + } + else + { + RAND_bytes(&n,1); + num=(n&7); + } + + if (inl > num) inl=num; + + if (num == 0) + { + ret= -1; + BIO_set_retry_write(b); + } + else +#endif + { + ret=BIO_write(b->next_bio,in,inl); + if (ret < 0) + { + BIO_copy_next_retry(b); + nt->lwn=inl; + } + } + return(ret); + } + +static long nbiof_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + long ret; + + if (b->next_bio == NULL) return(0); + switch (cmd) + { + case BIO_C_DO_STATE_MACHINE: + BIO_clear_retry_flags(b); + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + BIO_copy_next_retry(b); + break; + case BIO_CTRL_DUP: + break; + default: + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + break; + } + return(ret); + } + +static int nbiof_gets(bp,buf,size) +BIO *bp; +char *buf; +int size; + { + if (bp->next_bio == NULL) return(0); + return(BIO_gets(bp->next_bio,buf,size)); + } + + +static int nbiof_puts(bp,str) +BIO *bp; +char *str; + { + if (bp->next_bio == NULL) return(0); + return(BIO_puts(bp->next_bio,str)); + } + + diff --git a/crypto/bio/bf_null.c b/crypto/bio/bf_null.c new file mode 100644 index 0000000000..9864f4a03c --- /dev/null +++ b/crypto/bio/bf_null.c @@ -0,0 +1,194 @@ +/* crypto/bio/bf_null.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <errno.h> +#include "cryptlib.h" +#include "bio.h" +#include "evp.h" + +/* BIO_put and BIO_get both add to the digest, + * BIO_gets returns the digest */ + +#ifndef NOPROTO +static int nullf_write(BIO *h,char *buf,int num); +static int nullf_read(BIO *h,char *buf,int size); +static int nullf_puts(BIO *h,char *str); +static int nullf_gets(BIO *h,char *str,int size); +static long nullf_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int nullf_new(BIO *h); +static int nullf_free(BIO *data); +#else +static int nullf_write(); +static int nullf_read(); +static int nullf_puts(); +static int nullf_gets(); +static long nullf_ctrl(); +static int nullf_new(); +static int nullf_free(); +#endif + +static BIO_METHOD methods_nullf= + { + BIO_TYPE_NULL_FILTER,"NULL filter", + nullf_write, + nullf_read, + nullf_puts, + nullf_gets, + nullf_ctrl, + nullf_new, + nullf_free, + }; + +BIO_METHOD *BIO_f_null() + { + return(&methods_nullf); + } + +static int nullf_new(bi) +BIO *bi; + { + bi->init=1; + bi->ptr=NULL; + bi->flags=0; + return(1); + } + +static int nullf_free(a) +BIO *a; + { + if (a == NULL) return(0); +/* a->ptr=NULL; + a->init=0; + a->flags=0;*/ + return(1); + } + +static int nullf_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + int ret=0; + + if (out == NULL) return(0); + if (b->next_bio == NULL) return(0); + ret=BIO_read(b->next_bio,out,outl); + BIO_clear_retry_flags(b); + BIO_copy_next_retry(b); + return(ret); + } + +static int nullf_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + int ret=0; + + if ((in == NULL) || (inl <= 0)) return(0); + if (b->next_bio == NULL) return(0); + ret=BIO_write(b->next_bio,in,inl); + BIO_clear_retry_flags(b); + BIO_copy_next_retry(b); + return(ret); + } + +static long nullf_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + long ret; + + if (b->next_bio == NULL) return(0); + switch(cmd) + { + case BIO_C_DO_STATE_MACHINE: + BIO_clear_retry_flags(b); + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + BIO_copy_next_retry(b); + break; + case BIO_CTRL_DUP: + break; + default: + ret=BIO_ctrl(b->next_bio,cmd,num,ptr); + } + return(ret); + } + +static int nullf_gets(bp,buf,size) +BIO *bp; +char *buf; +int size; + { + if (bp->next_bio == NULL) return(0); + return(BIO_gets(bp->next_bio,buf,size)); + } + + +static int nullf_puts(bp,str) +BIO *bp; +char *str; + { + if (bp->next_bio == NULL) return(0); + return(BIO_puts(bp->next_bio,str)); + } + + diff --git a/crypto/bio/bio.err b/crypto/bio/bio.err new file mode 100644 index 0000000000..3363971b31 --- /dev/null +++ b/crypto/bio/bio.err @@ -0,0 +1,43 @@ +/* Error codes for the BIO functions. */ + +/* Function codes. */ +#define BIO_F_ACPT_STATE 100 +#define BIO_F_BIO_ACCEPT 101 +#define BIO_F_BIO_CTRL 102 +#define BIO_F_BIO_GETS 103 +#define BIO_F_BIO_GET_ACCEPT_SOCKET 104 +#define BIO_F_BIO_GET_HOST_IP 105 +#define BIO_F_BIO_GET_PORT 106 +#define BIO_F_BIO_NEW 107 +#define BIO_F_BIO_NEW_FILE 108 +#define BIO_F_BIO_PUTS 109 +#define BIO_F_BIO_READ 110 +#define BIO_F_BIO_SOCK_INIT 111 +#define BIO_F_BIO_WRITE 112 +#define BIO_F_CONN_STATE 113 +#define BIO_F_FILE_CTRL 114 +#define BIO_F_MEM_WRITE 115 +#define BIO_F_WSASTARTUP 116 + +/* Reason codes. */ +#define BIO_R_ACCEPT_ERROR 100 +#define BIO_R_BAD_FOPEN_MODE 101 +#define BIO_R_BAD_HOSTNAME_LOOKUP 102 +#define BIO_R_CONNECT_ERROR 103 +#define BIO_R_ERROR_SETTING_NBIO 104 +#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105 +#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106 +#define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107 +#define BIO_R_INVALID_IP_ADDRESS 108 +#define BIO_R_NBIO_CONNECT_ERROR 109 +#define BIO_R_NO_ACCEPT_PORT_SPECIFIED 110 +#define BIO_R_NO_HOSTHNAME_SPECIFIED 111 +#define BIO_R_NO_PORT_DEFINED 112 +#define BIO_R_NO_PORT_SPECIFIED 113 +#define BIO_R_NULL_PARAMETER 114 +#define BIO_R_UNABLE_TO_BIND_SOCKET 115 +#define BIO_R_UNABLE_TO_CREATE_SOCKET 116 +#define BIO_R_UNABLE_TO_LISTEN_SOCKET 117 +#define BIO_R_UNINITALISED 118 +#define BIO_R_UNSUPPORTED_METHOD 119 +#define BIO_R_WSASTARTUP 120 diff --git a/crypto/bio/bio.h b/crypto/bio/bio.h new file mode 100644 index 0000000000..0b2c6e77e1 --- /dev/null +++ b/crypto/bio/bio.h @@ -0,0 +1,597 @@ +/* crypto/bio/bio.h */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#ifndef HEADER_BIO_H +#define HEADER_BIO_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* These are the 'types' of BIOs */ +#define BIO_TYPE_NONE 0 +#define BIO_TYPE_MEM (1|0x0400) +#define BIO_TYPE_FILE (2|0x0400) + +#define BIO_TYPE_FD (4|0x0400|0x0100) +#define BIO_TYPE_SOCKET (5|0x0400|0x0100) +#define BIO_TYPE_NULL (6|0x0400) +#define BIO_TYPE_SSL (7|0x0200) +#define BIO_TYPE_MD (8|0x0200) /* pasive filter */ +#define BIO_TYPE_BUFFER (9|0x0200) /* filter */ +#define BIO_TYPE_CIPHER (10|0x0200) /* filter */ +#define BIO_TYPE_BASE64 (11|0x0200) /* filter */ +#define BIO_TYPE_CONNECT (12|0x0400|0x0100) /* socket - connect */ +#define BIO_TYPE_ACCEPT (13|0x0400|0x0100) /* socket for accept */ +#define BIO_TYPE_PROXY_CLIENT (14|0x0400) /* client proxy BIO */ +#define BIO_TYPE_PROXY_SERVER (15|0x0200) /* server proxy BIO */ +#define BIO_TYPE_NBIO_TEST (16|0x0200) /* server proxy BIO */ +#define BIO_TYPE_NULL_FILTER (17|0x0200) + +#define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */ +#define BIO_TYPE_FILTER 0x0200 +#define BIO_TYPE_SOURCE_SINK 0x0400 + +/* BIO_FILENAME_READ|BIO_CLOSE to open or close on free. + * BIO_set_fp(in,stdin,BIO_NOCLOSE); */ +#define BIO_NOCLOSE 0x00 +#define BIO_CLOSE 0x01 + +/* These are used in the following macros and are passed to + * BIO_ctrl() */ +#define BIO_CTRL_RESET 1 /* opt - rewind/zero etc */ +#define BIO_CTRL_EOF 2 /* opt - are we at the eof */ +#define BIO_CTRL_INFO 3 /* opt - extra tit-bits */ +#define BIO_CTRL_SET 4 /* man - set the 'IO' type */ +#define BIO_CTRL_GET 5 /* man - get the 'IO' type */ +#define BIO_CTRL_PUSH 6 /* opt - internal, used to signify change */ +#define BIO_CTRL_POP 7 /* opt - internal, used to signify change */ +#define BIO_CTRL_GET_CLOSE 8 /* man - set the 'close' on free */ +#define BIO_CTRL_SET_CLOSE 9 /* man - set the 'close' on free */ +#define BIO_CTRL_PENDING 10 /* opt - is their more data buffered */ +#define BIO_CTRL_FLUSH 11 /* opt - 'flush' buffered output */ +#define BIO_CTRL_DUP 12 /* man - extra stuff for 'duped' BIO */ +#define BIO_CTRL_WPENDING 13 /* opt - number of bytes still to write */ + +#define BIO_CTRL_SET_FILENAME 30 /* BIO_s_file special */ + +/* modifiers */ +#define BIO_FP_READ 0x02 +#define BIO_FP_WRITE 0x04 +#define BIO_FP_APPEND 0x08 +#define BIO_FP_TEXT 0x10 + +#define BIO_FLAGS_READ 0x01 +#define BIO_FLAGS_WRITE 0x02 +#define BIO_FLAGS_IO_SPECIAL 0x04 +#define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL) +#define BIO_FLAGS_SHOULD_RETRY 0x08 + +/* Mostly used in the SSL BIO */ +#define BIO_FLAGS_PROTOCOL_DELAYED_READ 0x10 +#define BIO_FLAGS_PROTOCOL_DELAYED_WRITE 0x20 +#define BIO_FLAGS_PROTOCOL_STARTUP 0x40 + +#define BIO_set_flags(b,f) ((b)->flags|=(f)) +#define BIO_set_retry_special(b) \ + ((b)->flags|=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY)) +#define BIO_set_retry_read(b) \ + ((b)->flags|=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY)) +#define BIO_set_retry_write(b) \ + ((b)->flags|=(BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY)) + +/* These are normally used internally in BIOs */ +#define BIO_clear_flags(b,f) ((b)->flags&= ~(f)) +#define BIO_clear_retry_flags(b) \ + ((b)->flags&= ~(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) +#define BIO_get_retry_flags(b) \ + ((b)->flags&(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) + +/* These shouldbe used by the application to tell why we should retry */ +#define BIO_should_read(a) ((a)->flags & BIO_FLAGS_READ) +#define BIO_should_write(a) ((a)->flags & BIO_FLAGS_WRITE) +#define BIO_should_io_special(a) ((a)->flags & BIO_FLAGS_IO_SPECIAL) +#define BIO_retry_type(a) ((a)->flags & BIO_FLAGS_RWS) +#define BIO_should_retry(a) ((a)->flags & BIO_FLAGS_SHOULD_RETRY) + +/* The next two are used in conjunction with the + * BIO_should_io_special() condition. After this returns true, + * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO + * stack and return the 'reason' for the special and the offending BIO. + * Given a BIO, BIO_get_retry_reason(bio) will return the code. */ +/* Returned from the SSL bio when the certificate retrieval code had an error */ +#define BIO_RR_SSL_X509_LOOKUP 0x01 +/* Returned from the connect BIO when a connect would have blocked */ +#define BIO_RR_CONNECT 0x02 + +/* These are passed by the BIO callback */ +#define BIO_CB_FREE 0x01 +#define BIO_CB_READ 0x02 +#define BIO_CB_WRITE 0x03 +#define BIO_CB_PUTS 0x04 +#define BIO_CB_GETS 0x05 +#define BIO_CB_CTRL 0x06 + +/* The callback is called before and after the underling operation, + * The BIO_CB_RETURN flag indicates if it is after the call */ +#define BIO_CB_RETURN 0x80 +#define BIO_CB_return(a) ((a)|BIO_CB_RETURN)) +#define BIO_cb_pre(a) (!((a)&BIO_CB_RETURN)) +#define BIO_cb_post(a) ((a)&BIO_CB_RETURN) + +#define BIO_set_callback(b,cb) ((b)->callback=(cb)) +#define BIO_set_callback_arg(b,arg) ((b)->cb_arg=(char *)(arg)) +#define BIO_get_callback_arg(b) ((b)->cb_arg) +#define BIO_get_callback(b) ((b)->callback) +#define BIO_method_name(b) ((b)->method->name) +#define BIO_method_type(b) ((b)->method->type) + +#ifndef WIN16 +typedef struct bio_method_st + { + int type; + char *name; + int (*bwrite)(); + int (*bread)(); + int (*bputs)(); + int (*bgets)(); + long (*ctrl)(); + int (*create)(); + int (*destroy)(); + } BIO_METHOD; +#else +typedef struct bio_method_st + { + int type; + char *name; + int (_far *bwrite)(); + int (_far *bread)(); + int (_far *bputs)(); + int (_far *bgets)(); + long (_far *ctrl)(); + int (_far *create)(); + int (_far *destroy)(); + } BIO_METHOD; +#endif + +typedef struct bio_st + { + BIO_METHOD *method; +#ifndef NOPROTO + /* bio, mode, argp, argi, argl, ret */ + long (*callback)(struct bio_st *,int,char *,int, long,long); +#else + long (*callback)(); +#endif + char *cb_arg; /* first argument for the callback */ + + int init; + int shutdown; + int flags; /* extra storage */ + int retry_reason; + int num; + char *ptr; + struct bio_st *next_bio; /* used by filter BIOs */ + struct bio_st *prev_bio; /* used by filter BIOs */ + int references; + unsigned long num_read; + unsigned long num_write; + + char *app_data; + } BIO; + +typedef struct bio_f_buffer_ctx_struct + { + /* BIO *bio; */ /* this is now in the BIO struct */ + int ibuf_size; /* how big is the input buffer */ + int obuf_size; /* how big is the output buffer */ + + char *ibuf; /* the char array */ + int ibuf_len; /* how many bytes are in it */ + int ibuf_off; /* write/read offset */ + + char *obuf; /* the char array */ + int obuf_len; /* how many bytes are in it */ + int obuf_off; /* write/read offset */ + } BIO_F_BUFFER_CTX; + +#define BIO_number_read(b) ((b)->num_read) +#define BIO_number_written(b) ((b)->num_write) + +#define BIO_C_SET_CONNECT 100 +#define BIO_C_DO_STATE_MACHINE 101 +#define BIO_C_SET_NBIO 102 +#define BIO_C_SET_PROXY_PARAM 103 +#define BIO_C_SET_FD 104 +#define BIO_C_GET_FD 105 +#define BIO_C_SET_FILE_PTR 106 +#define BIO_C_GET_FILE_PTR 107 +#define BIO_C_SET_FILENAME 108 +#define BIO_C_SET_SSL 109 +#define BIO_C_GET_SSL 110 +#define BIO_C_SET_MD 111 +#define BIO_C_GET_MD 112 +#define BIO_C_GET_CIPHER_STATUS 113 +#define BIO_C_SET_BUF_MEM 114 +#define BIO_C_GET_BUF_MEM_PTR 115 +#define BIO_C_GET_BUFF_NUM_LINES 116 +#define BIO_C_SET_BUFF_SIZE 117 +#define BIO_C_SET_ACCEPT 118 +#define BIO_C_SSL_MODE 119 +#define BIO_C_GET_MD_CTX 120 +#define BIO_C_GET_PROXY_PARAM 121 + +#define BIO_set_app_data(s,arg) ((s)->app_data=(char *)arg) +#define BIO_get_app_data(s) ((s)->app_data) + +/* BIO_s_connect_socket() */ +#define BIO_set_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name) +#define BIO_set_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port) +#define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) + +/* BIO_s_accept_socket() */ +#define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name) +/* #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */ +#define BIO_set_nbio_accpet(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?"a":NULL) +#define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio) + +#define BIO_do_connect(b) BIO_do_handshake(b) +#define BIO_do_accept(b) BIO_do_handshake(b) +#define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL) + +/* BIO_s_proxy_client() */ +#define BIO_set_url(b,url) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,0,(char *)(url)) +#define BIO_set_proxies(b,p) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,1,(char *)(p)) +/* BIO_set_nbio(b,n) */ +#define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s)) +/* BIO *BIO_get_filter_bio(BIO *bio); */ +#define BIO_set_proxy_cb(b,cb) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(char *)(cb)) +#define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk) + +#define BIO_get_proxy_header(b,skp) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,0,(char *)skp) +#define BIO_get_proxies(b,pxy_p) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,1,(char *)(pxy_p)) +#define BIO_get_url(b,url) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,2,(char *)(url)) + +#define BIO_set_fd(b,fd,c) BIO_ctrl_int(b,BIO_C_SET_FD,c,fd) +#define BIO_get_fd(b,c) BIO_ctrl(b,BIO_C_GET_FD,0,(char *)c) + +#define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp) +#define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp) + +#define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ + BIO_CLOSE|BIO_FP_READ,name) +#define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ + BIO_CLOSE|BIO_FP_WRITE,name) +#define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ + BIO_CLOSE|BIO_FP_APPEND,name) + +#define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl) +#define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp) +#define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL) + +/* defined in evp.h */ +/* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */ + +#define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm) +#define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp) + +/* For the BIO_f_buffer() type */ +#define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL) +#define BIO_set_buffer_size(b,size) BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL) +#define BIO_set_read_buffer_size(b,size) BIO_ctrl_int(b,BIO_C_SET_BUFF_SIZE,size,0) +#define BIO_set_write_buffer_size(b,size) BIO_ctrl_int(b,BIO_C_SET_BUFF_SIZE,size,1) + +/* Don't use the next one unless you know what you are doing :-) */ +#define BIO_dup_state(b,ret) BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)ret) + +#define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL) +#define BIO_eof(b) (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL) +#define BIO_set_close(b,c) (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL) +#define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL) +#define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL) +#define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL) +#define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL) + +/* For the BIO_f_buffer() type */ +#define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL) + +#ifndef NOPROTO +BIO * BIO_new(BIO_METHOD *type); +int BIO_set(BIO *a,BIO_METHOD *type); +int BIO_free(BIO *a); +int BIO_read(BIO *b, char *data, int len); +int BIO_gets(BIO *bp,char *buf, int size); +int BIO_write(BIO *b, char *data, int len); +int BIO_puts(BIO *bp,char *buf); +long BIO_ctrl(BIO *bp,int cmd,long larg,char *parg); +long BIO_ctrl_int(BIO *bp,int cmd,long larg,int iarg); +BIO * BIO_push(BIO *b,BIO *append); +BIO * BIO_pop(BIO *b); +void BIO_free_all(BIO *a); +BIO * BIO_find_type(BIO *b,int bio_type); +BIO * BIO_get_retry_BIO(BIO *bio, int *reason); +int BIO_get_retry_reason(BIO *bio); +BIO * BIO_dup_chain(BIO *in); + +#ifndef WIN16 +long BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi, + long argl,long ret); +#else +long _far _loadds BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi, + long argl,long ret); +#endif + +#if !defined(WIN16) || defined(APPS_WIN16) +BIO_METHOD *BIO_s_file(void); +#else +BIO_METHOD *BIO_s_file_internal_w16(void); +#endif + +BIO_METHOD *BIO_s_mem(void); +BIO_METHOD *BIO_s_socket(void); +BIO_METHOD *BIO_s_connect(void); +BIO_METHOD *BIO_s_accept(void); +BIO_METHOD *BIO_s_fd(void); +BIO_METHOD *BIO_s_null(void); +BIO_METHOD *BIO_f_null(void); +BIO_METHOD *BIO_f_nbio_test(void); +BIO_METHOD *BIO_f_buffer(void); + +int BIO_sock_should_retry(int i); +int BIO_sock_non_fatal_error(int error); +int BIO_fd_should_retry(int i); +int BIO_fd_non_fatal_error(int error); +int BIO_dump(BIO *b,char *bytes,int len); + +struct hostent *BIO_gethostbyname(char *name); +int BIO_sock_error(int sock); +int BIO_socket_ioctl(int fd, long type, unsigned long *arg); +int BIO_get_port(char *str, short *port_ptr); +int BIO_get_host_ip(char *str, unsigned char *ip); +int BIO_get_accept_socket(char *host_port); +int BIO_accept(int sock,char **ip_port); +int BIO_sock_init(void ); +void BIO_sock_cleanup(void); +int BIO_set_tcp_ndelay(int sock,int turn_on); + +void ERR_load_BIO_strings(void ); + +#if !defined(WIN16) || defined(APPS_WIN16) +BIO *BIO_new_file(char *filename, char *mode); +BIO *BIO_new_fp(FILE *stream, int close_flag); +#endif +BIO *BIO_new_socket(int sock, int close_flag); +BIO *BIO_new_fd(int fd, int close_flag); +BIO *BIO_new_connect(char *host_port); +BIO *BIO_new_accept(char *host_port); + +void BIO_copy_next_retry(BIO *b); + +#else + +BIO * BIO_new(); +int BIO_set(); +int BIO_free(); +int BIO_read(); +int BIO_gets(); +int BIO_write(); +int BIO_puts(); +long BIO_ctrl(); +long BIO_ctrl_int(); +BIO * BIO_push(); +BIO * BIO_pop(); +void BIO_free_all(); +BIO * BIO_find_type(); +BIO * BIO_get_retry_BIO(); +int BIO_get_retry_reason(); +BIO * BIO_dup_chain(); + +#ifndef WIN16 +long BIO_debug_callback(); +#else +long _far _loadds BIO_debug_callback(); +#endif + +#if !defined(WIN16) || defined(APPS_WIN16) +BIO_METHOD *BIO_s_file(); +#else +BIO_METHOD *BIO_s_file_internal_w16(); +#endif + +BIO_METHOD *BIO_s_mem(); +BIO_METHOD *BIO_s_socket(); +BIO_METHOD *BIO_s_connect(); +BIO_METHOD *BIO_s_accept(); +BIO_METHOD *BIO_s_fd(); +BIO_METHOD *BIO_s_null(); +BIO_METHOD *BIO_f_null(); +BIO_METHOD *BIO_f_buffer(); +BIO_METHOD *BIO_f_nbio_test(); + +int BIO_sock_should_retry(); +int BIO_sock_non_fatal_error(); +int BIO_fd_should_retry(); +int BIO_fd_non_fatal_error(); +int BIO_dump(); + +struct hostent *BIO_gethostbyname(); +int BIO_sock_error(); +int BIO_socket_ioctl(); +int BIO_get_port(); +int BIO_get_host_ip(); +int BIO_get_accept_socket(); +int BIO_accept(); +int BIO_sock_init(); +void BIO_sock_cleanup(); +int BIO_set_tcp_ndelay(); + +void ERR_load_BIO_strings(); + +#if !defined(WIN16) || defined(APPS_WIN16) +BIO *BIO_new_file(); +BIO *BIO_new_fp(); +#endif +BIO *BIO_new_socket(); +BIO *BIO_new_fd(); +BIO *BIO_new_connect(); +BIO *BIO_new_accept(); + +void BIO_copy_next_retry(); + +#endif + +/* Tim Hudson's portable varargs stuff */ + +#ifndef NOPROTO +#define VAR_ANSI /* select ANSI version by default */ +#endif + +#ifdef VAR_ANSI +/* ANSI version of a "portable" macro set for variable length args */ +#ifndef __STDARG_H__ /**/ +#include <stdarg.h> +#endif /**/ + +#define VAR_PLIST(arg1type,arg1) arg1type arg1, ... +#define VAR_PLIST2(arg1type,arg1,arg2type,arg2) arg1type arg1,arg2type arg2,... +#define VAR_ALIST +#define VAR_BDEFN(args,arg1type,arg1) va_list args +#define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args +#define VAR_INIT(args,arg1type,arg1) va_start(args,arg1); +#define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args,arg2); +#define VAR_ARG(args,type,arg) arg=va_arg(args,type) +#define VAR_END(args) va_end(args); + +#else + +/* K&R version of a "portable" macro set for variable length args */ +#ifndef __VARARGS_H__ +#include <varargs.h> +#endif + +#define VAR_PLIST(arg1type,arg1) va_alist +#define VAR_PLIST2(arg1type,arg1,arg2type,arg2) va_alist +#define VAR_ALIST va_dcl +#define VAR_BDEFN(args,arg1type,arg1) va_list args; arg1type arg1 +#define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args; \ + arg1type arg1; arg2type arg2 +#define VAR_INIT(args,arg1type,arg1) va_start(args); \ + arg1=va_arg(args,arg1type); +#define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args); \ + arg1=va_arg(args,arg1type); arg2=va_arg(args,arg2type); +#define VAR_ARG(args,type,arg) arg=va_arg(args,type) +#define VAR_END(args) va_end(args); + +#endif + +#ifndef NOPROTO +int BIO_printf( VAR_PLIST( BIO *, bio ) ); +#else +int BIO_printf(); +#endif + +/* BEGIN ERROR CODES */ +/* Error codes for the BIO functions. */ + +/* Function codes. */ +#define BIO_F_ACPT_STATE 100 +#define BIO_F_BIO_ACCEPT 101 +#define BIO_F_BIO_CTRL 102 +#define BIO_F_BIO_GETS 103 +#define BIO_F_BIO_GET_ACCEPT_SOCKET 104 +#define BIO_F_BIO_GET_HOST_IP 105 +#define BIO_F_BIO_GET_PORT 106 +#define BIO_F_BIO_NEW 107 +#define BIO_F_BIO_NEW_FILE 108 +#define BIO_F_BIO_PUTS 109 +#define BIO_F_BIO_READ 110 +#define BIO_F_BIO_SOCK_INIT 111 +#define BIO_F_BIO_WRITE 112 +#define BIO_F_CONN_STATE 113 +#define BIO_F_FILE_CTRL 114 +#define BIO_F_MEM_WRITE 115 +#define BIO_F_WSASTARTUP 116 + +/* Reason codes. */ +#define BIO_R_ACCEPT_ERROR 100 +#define BIO_R_BAD_FOPEN_MODE 101 +#define BIO_R_BAD_HOSTNAME_LOOKUP 102 +#define BIO_R_CONNECT_ERROR 103 +#define BIO_R_ERROR_SETTING_NBIO 104 +#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105 +#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106 +#define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107 +#define BIO_R_INVALID_IP_ADDRESS 108 +#define BIO_R_NBIO_CONNECT_ERROR 109 +#define BIO_R_NO_ACCEPT_PORT_SPECIFIED 110 +#define BIO_R_NO_HOSTHNAME_SPECIFIED 111 +#define BIO_R_NO_PORT_DEFINED 112 +#define BIO_R_NO_PORT_SPECIFIED 113 +#define BIO_R_NULL_PARAMETER 114 +#define BIO_R_UNABLE_TO_BIND_SOCKET 115 +#define BIO_R_UNABLE_TO_CREATE_SOCKET 116 +#define BIO_R_UNABLE_TO_LISTEN_SOCKET 117 +#define BIO_R_UNINITALISED 118 +#define BIO_R_UNSUPPORTED_METHOD 119 +#define BIO_R_WSASTARTUP 120 + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/crypto/bio/bio_cb.c b/crypto/bio/bio_cb.c new file mode 100644 index 0000000000..2f98cd1256 --- /dev/null +++ b/crypto/bio/bio_cb.c @@ -0,0 +1,132 @@ +/* crypto/bio/bio_cb.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "cryptlib.h" +#include "bio.h" +#include "err.h" + +long MS_CALLBACK BIO_debug_callback(bio,cmd,argp,argi,argl,ret) +BIO *bio; +int cmd; +char *argp; +int argi; +long argl; +long ret; + { + BIO *b; + MS_STATIC char buf[256]; + char *p; + long r=1; + + if (BIO_CB_RETURN & cmd) + r=ret; + + sprintf(buf,"BIO[%08lX]:",(unsigned long)bio); + p= &(buf[14]); + switch (cmd) + { + case BIO_CB_FREE: + sprintf(p,"Free - %s\n",bio->method->name); + break; + case BIO_CB_READ: + sprintf(p,"read(%d,%d) - %s\n",bio->num,argi,bio->method->name); + break; + case BIO_CB_WRITE: + sprintf(p,"write(%d,%d) - %s\n",bio->num,argi,bio->method->name); + break; + case BIO_CB_PUTS: + sprintf(p,"puts() - %s\n",bio->method->name); + break; + case BIO_CB_GETS: + sprintf(p,"gets(%d) - %s\n",argi,bio->method->name); + break; + case BIO_CB_CTRL: + sprintf(p,"ctrl(%d) - %s\n",argi,bio->method->name); + break; + case BIO_CB_RETURN|BIO_CB_READ: + sprintf(p,"read return %ld\n",ret); + break; + case BIO_CB_RETURN|BIO_CB_WRITE: + sprintf(p,"write return %ld\n",ret); + break; + case BIO_CB_RETURN|BIO_CB_GETS: + sprintf(p,"gets return %ld\n",ret); + break; + case BIO_CB_RETURN|BIO_CB_PUTS: + sprintf(p,"puts return %ld\n",ret); + break; + case BIO_CB_RETURN|BIO_CB_CTRL: + sprintf(p,"ctrl return %ld\n",ret); + break; + default: + sprintf(p,"bio callback - unknown type (%d)\n",cmd); + break; + } + + b=(BIO *)bio->cb_arg; + if (b != NULL) + BIO_write(b,buf,strlen(buf)); +#ifndef WIN16 + else + fputs(buf,stderr); +#endif + return(r); + } diff --git a/crypto/bio/bio_err.c b/crypto/bio/bio_err.c new file mode 100644 index 0000000000..b78878fcb8 --- /dev/null +++ b/crypto/bio/bio_err.c @@ -0,0 +1,121 @@ +/* lib/bio/bio_err.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ +#include <stdio.h> +#include "err.h" +#include "bio.h" + +/* BEGIN ERROR CODES */ +static ERR_STRING_DATA BIO_str_functs[]= + { +{ERR_PACK(0,BIO_F_ACPT_STATE,0), "ACPT_STATE"}, +{ERR_PACK(0,BIO_F_BIO_ACCEPT,0), "BIO_accept"}, +{ERR_PACK(0,BIO_F_BIO_CTRL,0), "BIO_ctrl"}, +{ERR_PACK(0,BIO_F_BIO_GETS,0), "BIO_gets"}, +{ERR_PACK(0,BIO_F_BIO_GET_ACCEPT_SOCKET,0), "BIO_get_accept_socket"}, +{ERR_PACK(0,BIO_F_BIO_GET_HOST_IP,0), "BIO_get_host_ip"}, +{ERR_PACK(0,BIO_F_BIO_GET_PORT,0), "BIO_get_port"}, +{ERR_PACK(0,BIO_F_BIO_NEW,0), "BIO_new"}, +{ERR_PACK(0,BIO_F_BIO_NEW_FILE,0), "BIO_new_file"}, +{ERR_PACK(0,BIO_F_BIO_PUTS,0), "BIO_puts"}, +{ERR_PACK(0,BIO_F_BIO_READ,0), "BIO_read"}, +{ERR_PACK(0,BIO_F_BIO_SOCK_INIT,0), "BIO_sock_init"}, +{ERR_PACK(0,BIO_F_BIO_WRITE,0), "BIO_write"}, +{ERR_PACK(0,BIO_F_CONN_STATE,0), "CONN_STATE"}, +{ERR_PACK(0,BIO_F_FILE_CTRL,0), "FILE_CTRL"}, +{ERR_PACK(0,BIO_F_MEM_WRITE,0), "MEM_WRITE"}, +{ERR_PACK(0,BIO_F_WSASTARTUP,0), "WSASTARTUP"}, +{0,NULL}, + }; + +static ERR_STRING_DATA BIO_str_reasons[]= + { +{BIO_R_ACCEPT_ERROR ,"accept error"}, +{BIO_R_BAD_FOPEN_MODE ,"bad fopen mode"}, +{BIO_R_BAD_HOSTNAME_LOOKUP ,"bad hostname lookup"}, +{BIO_R_CONNECT_ERROR ,"connect error"}, +{BIO_R_ERROR_SETTING_NBIO ,"error setting nbio"}, +{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET,"error setting nbio on accepted socket"}, +{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET,"error setting nbio on accept socket"}, +{BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET ,"gethostbyname addr is not af inet"}, +{BIO_R_INVALID_IP_ADDRESS ,"invalid ip address"}, +{BIO_R_NBIO_CONNECT_ERROR ,"nbio connect error"}, +{BIO_R_NO_ACCEPT_PORT_SPECIFIED ,"no accept port specified"}, +{BIO_R_NO_HOSTHNAME_SPECIFIED ,"no hosthname specified"}, +{BIO_R_NO_PORT_DEFINED ,"no port defined"}, +{BIO_R_NO_PORT_SPECIFIED ,"no port specified"}, +{BIO_R_NULL_PARAMETER ,"null parameter"}, +{BIO_R_UNABLE_TO_BIND_SOCKET ,"unable to bind socket"}, +{BIO_R_UNABLE_TO_CREATE_SOCKET ,"unable to create socket"}, +{BIO_R_UNABLE_TO_LISTEN_SOCKET ,"unable to listen socket"}, +{BIO_R_UNINITALISED ,"uninitalised"}, +{BIO_R_UNSUPPORTED_METHOD ,"unsupported method"}, +{BIO_R_WSASTARTUP ,"wsastartup"}, +{0,NULL}, + }; + +void ERR_load_BIO_strings() + { + static int init=1; + + if (init) + { + init=0; + ERR_load_strings(ERR_LIB_BIO,BIO_str_functs); + ERR_load_strings(ERR_LIB_BIO,BIO_str_reasons); + } + } diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c new file mode 100644 index 0000000000..78e896f9ae --- /dev/null +++ b/crypto/bio/bio_lib.c @@ -0,0 +1,447 @@ +/* crypto/bio/bio_lib.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <errno.h> +#include "cryptlib.h" +#include "bio.h" + +BIO *BIO_new(method) +BIO_METHOD *method; + { + BIO *ret=NULL; + + ret=(BIO *)Malloc(sizeof(BIO)); + if (ret == NULL) + { + BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE); + return(NULL); + } + if (!BIO_set(ret,method)) + { + Free(ret); + ret=NULL; + } + return(ret); + } + +int BIO_set(bio,method) +BIO *bio; +BIO_METHOD *method; + { + bio->method=method; + bio->callback=NULL; + bio->cb_arg=NULL; + bio->init=0; + bio->shutdown=1; + bio->num=0; + bio->flags=0; + bio->retry_reason=0; + bio->ptr=NULL; + bio->prev_bio=NULL; + bio->next_bio=NULL; + bio->references=1; + bio->num_read=0L; + bio->num_write=0L; + if (method->create != NULL) + if (!method->create(bio)) + return(0); + return(1); + } + +int BIO_free(a) +BIO *a; + { + int ret=0,i; + + if (a == NULL) return(0); + + i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO); + + if (i > 0) return(1); +#ifdef REF_CHECK + if (i < 0) + { + fprintf(stderr,"BIO_free, bad reference count\n"); + abort(); + } +#endif + if ((a->callback != NULL) && + ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0)) + return(i); + + if ((a->method == NULL) || (a->method->destroy == NULL)) return(1); + ret=a->method->destroy(a); + Free(a); + return(1); + } + +int BIO_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + int i; + + if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) + { + BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD); + return(-2); + } + + if ((b->callback != NULL) && + ((i=(int)b->callback(b,BIO_CB_READ,out,outl,0L,1L)) <= 0)) + return(i); + + if (!b->init) + { + BIOerr(BIO_F_BIO_READ,BIO_R_UNINITALISED); + return(-2); + } + + i=b->method->bread(b,out,outl); + if (i > 0) b->num_read+=(unsigned long)i; + + if (b->callback != NULL) + i=(int)b->callback(b,BIO_CB_READ|BIO_CB_RETURN,out,outl, + 0L,(long)i); + return(i); + } + +int BIO_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + int i; + + if ((b == NULL) || (b->method == NULL) || (b->method->bwrite == NULL)) + { + BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD); + return(-2); + } + + if ((b->callback != NULL) && + ((i=(int)b->callback(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0)) + return(i); + + if (!b->init) + { + BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITALISED); + return(-2); + } + + i=b->method->bwrite(b,in,inl); + if (i > 0) b->num_write+=(unsigned long)i; + + if (b->callback != NULL) + i=(int)b->callback(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl, + 0L,(long)i); + return(i); + } + +int BIO_puts(b,in) +BIO *b; +char *in; + { + int i; + + if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) + { + BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD); + return(-2); + } + + if ((b->callback != NULL) && + ((i=(int)b->callback(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0)) + return(i); + + if (!b->init) + { + BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITALISED); + return(-2); + } + + i=b->method->bputs(b,in); + + if (b->callback != NULL) + i=(int)b->callback(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0, + 0L,(long)i); + return(i); + } + +int BIO_gets(b,in,inl) +BIO *b; +char *in; +int inl; + { + int i; + + if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) + { + BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD); + return(-2); + } + + if ((b->callback != NULL) && + ((i=(int)b->callback(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0)) + return(i); + + if (!b->init) + { + BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITALISED); + return(-2); + } + + i=b->method->bgets(b,in,inl); + + if (b->callback != NULL) + i=(int)b->callback(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl, + 0L,(long)i); + return(i); + } + +long BIO_ctrl_int(b,cmd,larg,iarg) +BIO *b; +int cmd; +long larg; +int iarg; + { + int i; + + i=iarg; + return(BIO_ctrl(b,cmd,larg,(char *)&i)); + } + +long BIO_ctrl(b,cmd,larg,parg) +BIO *b; +int cmd; +long larg; +char *parg; + { + long ret; + + if (b == NULL) return(0); + + if ((b->method == NULL) || (b->method->ctrl == NULL)) + { + BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD); + return(-2); + } + + if ((b->callback != NULL) && + ((ret=b->callback(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0)) + return(ret); + + ret=b->method->ctrl(b,cmd,larg,parg); + + if (b->callback != NULL) + ret=b->callback(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd, + larg,ret); + return(ret); + } + +/* put the 'bio' on the end of b's list of operators */ +BIO *BIO_push(b,bio) +BIO *b,*bio; + { + BIO *lb; + + if (b == NULL) return(bio); + lb=b; + while (lb->next_bio != NULL) + lb=lb->next_bio; + lb->next_bio=bio; + if (bio != NULL) + bio->prev_bio=lb; + BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL); + return(b); + } + +/* Remove the first and return the rest */ +BIO *BIO_pop(b) +BIO *b; + { + BIO *ret; + + if (b == NULL) return(NULL); + ret=b->next_bio; + + if (b->prev_bio != NULL) + b->prev_bio->next_bio=b->next_bio; + if (b->next_bio != NULL) + b->next_bio->prev_bio=b->prev_bio; + + b->next_bio=NULL; + b->prev_bio=NULL; + BIO_ctrl(b,BIO_CTRL_POP,0,NULL); + return(ret); + } + +BIO *BIO_get_retry_BIO(bio,reason) +BIO *bio; +int *reason; + { + BIO *b,*last; + + b=last=bio; + for (;;) + { + if (!BIO_should_retry(b)) break; + last=b; + b=b->next_bio; + if (b == NULL) break; + } + if (reason != NULL) *reason=last->retry_reason; + return(last); + } + +int BIO_get_retry_reason(bio) +BIO *bio; + { + return(bio->retry_reason); + } + +BIO *BIO_find_type(bio,type) +BIO *bio; +int type; + { + int mt,mask; + + mask=type&0xff; + do { + if (bio->method != NULL) + { + mt=bio->method->type; + + if (!mask) + { + if (mt & type) return(bio); + } + else if (mt == type) + return(bio); + } + bio=bio->next_bio; + } while (bio != NULL); + return(NULL); + } + +void BIO_free_all(bio) +BIO *bio; + { + BIO *b; + int ref; + + while (bio != NULL) + { + b=bio; + ref=b->references; + bio=bio->next_bio; + BIO_free(b); + /* Since ref count > 1, don't free anyone else. */ + if (ref > 1) break; + } + } + +BIO *BIO_dup_chain(in) +BIO *in; + { + BIO *ret=NULL,*eoc=NULL,*bio,*new; + + for (bio=in; bio != NULL; bio=bio->next_bio) + { + if ((new=BIO_new(bio->method)) == NULL) goto err; + new->callback=bio->callback; + new->cb_arg=bio->cb_arg; + new->init=bio->init; + new->shutdown=bio->shutdown; + new->flags=bio->flags; + + /* This will let SSL_s_sock() work with stdin/stdout */ + new->num=bio->num; + + if (!BIO_dup_state(bio,(char *)new)) + { + BIO_free(new); + goto err; + } + if (ret == NULL) + { + eoc=new; + ret=eoc; + } + else + { + BIO_push(eoc,new); + eoc=new; + } + } + return(ret); +err: + if (ret != NULL) + BIO_free(ret); + return(NULL); + } + +void BIO_copy_next_retry(b) +BIO *b; + { + BIO_set_flags(b,BIO_get_retry_flags(b->next_bio)); + b->retry_reason=b->next_bio->retry_reason; + } + diff --git a/crypto/bio/bss_acpt.c b/crypto/bio/bss_acpt.c new file mode 100644 index 0000000000..251474198b --- /dev/null +++ b/crypto/bio/bss_acpt.c @@ -0,0 +1,484 @@ +/* crypto/bio/bss_acpt.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#ifndef NO_SOCK + +#include <stdio.h> +#include <errno.h> +#define USE_SOCKETS +#include "cryptlib.h" +#include "bio.h" + +/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ + +#ifdef WIN16 +#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ +#else +#define SOCKET_PROTOCOL IPPROTO_TCP +#endif + +typedef struct bio_accept_st + { + int state; + char *param_addr; + + int accept_sock; + int accept_nbio; + + char *addr; + int nbio; + BIO *bio_chain; + } BIO_ACCEPT; + +#ifndef NOPROTO +static int acpt_write(BIO *h,char *buf,int num); +static int acpt_read(BIO *h,char *buf,int size); +static int acpt_puts(BIO *h,char *str); +static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int acpt_new(BIO *h); +static int acpt_free(BIO *data); +#else +static int acpt_write(); +static int acpt_read(); +static int acpt_puts(); +static long acpt_ctrl(); +static int acpt_new(); +static int acpt_free(); +#endif + +#ifndef NOPROTO +static int acpt_state(BIO *b, BIO_ACCEPT *c); +static void acpt_close_socket(BIO *data); +BIO_ACCEPT *BIO_ACCEPT_new(void ); +void BIO_ACCEPT_free(BIO_ACCEPT *a); + +#else + +static int acpt_state(); +static void acpt_close_socket(); +BIO_ACCEPT *BIO_ACCEPT_new(); +void BIO_ACCEPT_free(); +#endif + +#define ACPT_S_BEFORE 1 +#define ACPT_S_GET_ACCEPT_SOCKET 2 +#define ACPT_S_OK 3 + +static BIO_METHOD methods_acceptp= + { + BIO_TYPE_ACCEPT,"socket accept", + acpt_write, + acpt_read, + acpt_puts, + NULL, /* connect_gets, */ + acpt_ctrl, + acpt_new, + acpt_free, + }; + +BIO_METHOD *BIO_s_accept() + { + return(&methods_acceptp); + } + +static int acpt_new(bi) +BIO *bi; + { + BIO_ACCEPT *ba; + + bi->init=0; + bi->num=INVALID_SOCKET; + bi->flags=0; + if ((ba=BIO_ACCEPT_new()) == NULL) + return(0); + bi->ptr=(char *)ba; + ba->state=ACPT_S_BEFORE; + bi->shutdown=1; + return(1); + } + +BIO_ACCEPT *BIO_ACCEPT_new() + { + BIO_ACCEPT *ret; + + if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL) + return(NULL); + + memset(ret,0,sizeof(BIO_ACCEPT)); + ret->accept_sock=INVALID_SOCKET; + return(ret); + } + +void BIO_ACCEPT_free(a) +BIO_ACCEPT *a; + { + if (a->param_addr != NULL) Free(a->param_addr); + if (a->addr != NULL) Free(a->addr); + if (a->bio_chain != NULL) BIO_free(a->bio_chain); + Free(a); + } + +static void acpt_close_socket(bio) +BIO *bio; + { + BIO_ACCEPT *c; + + c=(BIO_ACCEPT *)bio->ptr; + if (c->accept_sock != INVALID_SOCKET) + { + shutdown(c->accept_sock,2); +# ifdef WINDOWS + closesocket(c->accept_sock); +# else + close(c->accept_sock); +# endif + c->accept_sock=INVALID_SOCKET; + bio->num=INVALID_SOCKET; + } + } + +static int acpt_free(a) +BIO *a; + { + BIO_ACCEPT *data; + + if (a == NULL) return(0); + data=(BIO_ACCEPT *)a->ptr; + + if (a->shutdown) + { + acpt_close_socket(a); + BIO_ACCEPT_free(data); + a->ptr=NULL; + a->flags=0; + a->init=0; + } + return(1); + } + +static int acpt_state(b,c) +BIO *b; +BIO_ACCEPT *c; + { + BIO *bio=NULL,*dbio; + unsigned long l=1; + int s= -1; + int i; + +again: + switch (c->state) + { + case ACPT_S_BEFORE: + if (c->param_addr == NULL) + { + BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); + return(-1); + } + s=BIO_get_accept_socket(c->param_addr); + if (s == INVALID_SOCKET) + return(-1); + +#ifdef FIONBIO + if (c->accept_nbio) + { + i=BIO_socket_ioctl(b->num,FIONBIO,&l); + if (i < 0) + { +#ifdef WINDOWS + closesocket(s); +#else + close(s); +# endif + BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); + return(-1); + } + } +#endif + c->accept_sock=s; + b->num=s; + c->state=ACPT_S_GET_ACCEPT_SOCKET; + return(1); + break; + case ACPT_S_GET_ACCEPT_SOCKET: + if (b->next_bio != NULL) + { + c->state=ACPT_S_OK; + goto again; + } + i=BIO_accept(c->accept_sock,&(c->addr)); + if (i < 0) return(i); + bio=BIO_new_socket(i,BIO_CLOSE); + if (bio == NULL) goto err; + + BIO_set_callback(bio,BIO_get_callback(b)); + BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); + +#ifdef FIONBIO + if (c->nbio) + { + i=BIO_socket_ioctl(i,FIONBIO,&l); + if (i < 0) + { + BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); + goto err; + } + } +#endif + + /* If the accept BIO has an bio_chain, we dup it and + * put the new socket at the end. */ + if (c->bio_chain != NULL) + { + if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL) + goto err; + if (!BIO_push(dbio,bio)) goto err; + bio=dbio; + } + if (BIO_push(b,bio) == NULL) goto err; + + c->state=ACPT_S_OK; + return(1); +err: + if (bio != NULL) + BIO_free(bio); + else if (s >= 0) + { +#ifdef WINDOWS + closesocket(s); +#else + close(s); +# endif + } + return(0); + break; + case ACPT_S_OK: + if (b->next_bio == NULL) + { + c->state=ACPT_S_GET_ACCEPT_SOCKET; + goto again; + } + return(1); + break; + default: + return(0); + break; + } + + } + +static int acpt_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + int ret=0; + BIO_ACCEPT *data; + + BIO_clear_retry_flags(b); + data=(BIO_ACCEPT *)b->ptr; + + while (b->next_bio == NULL) + { + ret=acpt_state(b,data); + if (ret <= 0) return(ret); + } + + ret=BIO_read(b->next_bio,out,outl); + BIO_copy_next_retry(b); + return(ret); + } + +static int acpt_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + int ret; + BIO_ACCEPT *data; + + BIO_clear_retry_flags(b); + data=(BIO_ACCEPT *)b->ptr; + + while (b->next_bio == NULL) + { + ret=acpt_state(b,data); + if (ret <= 0) return(ret); + } + + ret=BIO_write(b->next_bio,in,inl); + BIO_copy_next_retry(b); + return(ret); + } + +static long acpt_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + BIO *dbio; + int *ip; + long ret=1; + BIO_ACCEPT *data; + + data=(BIO_ACCEPT *)b->ptr; + + switch (cmd) + { + case BIO_CTRL_RESET: + ret=0; + data->state=ACPT_S_BEFORE; + acpt_close_socket(b); + b->flags=0; + break; + case BIO_C_DO_STATE_MACHINE: + /* use this one to start the connection */ + ret=(long)acpt_state(b,data); + break; + case BIO_C_SET_ACCEPT: + if (ptr != NULL) + { + if (num == 0) + { + b->init=1; + if (data->param_addr != NULL) + Free(data->param_addr); + data->param_addr=BUF_strdup(ptr); + } + else if (num == 1) + { + data->accept_nbio=(ptr != NULL); + } + else if (num == 2) + { + if (data->bio_chain != NULL) + BIO_free(data->bio_chain); + data->bio_chain=(BIO *)ptr; + } + } + break; + case BIO_C_SET_NBIO: + data->nbio=(int)num; + break; + case BIO_C_GET_FD: + if (b->init) + { + ip=(int *)ptr; + if (ip != NULL) + *ip=data->accept_sock; + ret=b->num; + } + else + ret= -1; + break; + case BIO_CTRL_GET_CLOSE: + ret=b->shutdown; + break; + case BIO_CTRL_SET_CLOSE: + b->shutdown=(int)num; + break; + case BIO_CTRL_PENDING: + case BIO_CTRL_WPENDING: + ret=0; + break; + case BIO_CTRL_FLUSH: + break; + case BIO_CTRL_DUP: + dbio=(BIO *)ptr; +/* if (data->param_port) EAY EAY + BIO_set_port(dbio,data->param_port); + if (data->param_hostname) + BIO_set_hostname(dbio,data->param_hostname); + BIO_set_nbio(dbio,data->nbio); */ + break; + + default: + ret=0; + break; + } + return(ret); + } + +static int acpt_puts(bp,str) +BIO *bp; +char *str; + { + int n,ret; + + n=strlen(str); + ret=acpt_write(bp,str,n); + return(ret); + } + +BIO *BIO_new_accept(str) +char *str; + { + BIO *ret; + + ret=BIO_new(BIO_s_accept()); + if (ret == NULL) return(NULL); + if (BIO_set_accept_port(ret,str)) + return(ret); + else + { + BIO_free(ret); + return(NULL); + } + } + +#endif diff --git a/crypto/bio/bss_conn.c b/crypto/bio/bss_conn.c new file mode 100644 index 0000000000..4e31e3c1f8 --- /dev/null +++ b/crypto/bio/bss_conn.c @@ -0,0 +1,558 @@ +/* crypto/bio/bss_conn.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#ifndef NO_SOCK + +#include <stdio.h> +#include <errno.h> +#define USE_SOCKETS +#include "cryptlib.h" +#include "bio.h" + +/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ + +#ifdef WIN16 +#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ +#else +#define SOCKET_PROTOCOL IPPROTO_TCP +#endif + +typedef struct bio_connect_st + { + int state; + + char *param_hostname; + char *param_port; + int nbio; + + unsigned char ip[4]; + short port; + + struct sockaddr_in them; + + /* int socket; this will be kept in bio->num so that it is + * compatable with the bss_sock bio */ + int error; + } BIO_CONNECT; + +#ifndef NOPROTO +static int conn_write(BIO *h,char *buf,int num); +static int conn_read(BIO *h,char *buf,int size); +static int conn_puts(BIO *h,char *str); +static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int conn_new(BIO *h); +static int conn_free(BIO *data); +#else +static int conn_write(); +static int conn_read(); +static int conn_puts(); +static long conn_ctrl(); +static int conn_new(); +static int conn_free(); +#endif + +#ifndef NOPROTO + +static int conn_state(BIO *b, BIO_CONNECT *c); +static void conn_close_socket(BIO *data); +BIO_CONNECT *BIO_CONNECT_new(void ); +void BIO_CONNECT_free(BIO_CONNECT *a); + +#else + +static int conn_state(); +static void conn_close_socket(); +BIO_CONNECT *BIO_CONNECT_new(); +void BIO_CONNECT_free(); + +#endif + +#define CONN_S_BEFORE 1 +#define CONN_S_GET_IP 2 +#define CONN_S_GET_PORT 3 +#define CONN_S_CREATE_SOCKET 4 +#define CONN_S_CONNECT 5 +#define CONN_S_OK 6 +#define CONN_S_BLOCKED_CONNECT 7 +#define CONN_S_NBIO 8 + +static BIO_METHOD methods_connectp= + { + BIO_TYPE_CONNECT,"socket connect", + conn_write, + conn_read, + conn_puts, + NULL, /* connect_gets, */ + conn_ctrl, + conn_new, + conn_free, + }; + +static int conn_state(b,c) +BIO *b; +BIO_CONNECT *c; + { + int ret= -1,i; + unsigned long l; + char *p,*q; + + switch (c->state) + { + case CONN_S_BEFORE: + p=c->param_hostname; + if (p == NULL) + { + BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTHNAME_SPECIFIED); + break; + } + for ( ; *p != '\0'; p++) + { + if ((*p == ':') || (*p == '/')) break; + } + + i= *p; + if ((i == ':') || (i == '/')) + { + + *(p++)='\0'; + if (i == ':') + { + for (q=p; *q; q++) + if (*q == '/') + { + *q='\0'; + break; + } + if (c->param_port != NULL) + Free(c->param_port); + c->param_port=BUF_strdup(p); + } + } + + if (p == NULL) + { + BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED); + break; + } + c->state=CONN_S_GET_IP; + + case CONN_S_GET_IP: + if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0) + break; + c->state=CONN_S_GET_PORT; + + case CONN_S_GET_PORT: + if (BIO_get_port(c->param_port,&c->port) <= 0) + break; + c->state=CONN_S_CREATE_SOCKET; + + case CONN_S_CREATE_SOCKET: + /* now setup address */ + memset((char *)&c->them,0,sizeof(c->them)); + c->them.sin_family=AF_INET; + c->them.sin_port=htons((unsigned short)c->port); + l=(unsigned long) + ((unsigned long)c->ip[0]<<24L)| + ((unsigned long)c->ip[1]<<16L)| + ((unsigned long)c->ip[2]<< 8L)| + ((unsigned long)c->ip[3]); + c->them.sin_addr.s_addr=htonl(l); + c->state=CONN_S_CREATE_SOCKET; + + ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); + if (ret == INVALID_SOCKET) + { + SYSerr(SYS_F_SOCKET,errno); + BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET); + break; + } + b->num=ret; + c->state=CONN_S_NBIO; + + case CONN_S_NBIO: +#ifdef FIONBIO + if (c->nbio) + { + l=1; + ret=BIO_socket_ioctl(b->num,FIONBIO,&l); + if (ret < 0) + { + BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO); + break; + } + } +#endif + c->state=CONN_S_CONNECT; + + case CONN_S_CONNECT: + BIO_clear_retry_flags(b); + ret=connect(b->num, + (struct sockaddr *)&c->them, + sizeof(c->them)); + b->retry_reason=0; + if (ret < 0) + { + if (BIO_sock_should_retry(ret)) + { + BIO_set_retry_special(b); + c->state=CONN_S_BLOCKED_CONNECT; + b->retry_reason=BIO_RR_CONNECT; + } + else + { + SYSerr(SYS_F_CONNECT,errno); + BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR); + } + } + else + { + ret=1; + c->state=CONN_S_OK; + } + break; + + case CONN_S_BLOCKED_CONNECT: + BIO_clear_retry_flags(b); + i=BIO_sock_error(b->num); + if (i) + { + SYSerr(SYS_F_CONNECT,i); + BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR); + } + else + { + c->state=CONN_S_OK; + ret=1; + } + break; + + case CONN_S_OK: + ret=1; + break; + default: + abort(); + } + return(ret); + } + +BIO_CONNECT *BIO_CONNECT_new() + { + BIO_CONNECT *ret; + + if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL) + return(NULL); + ret->state=CONN_S_BEFORE; + ret->param_hostname=NULL; + ret->param_port=NULL; + ret->nbio=0; + ret->ip[0]=0; + ret->ip[1]=0; + ret->ip[2]=0; + ret->ip[3]=0; + ret->port=0; + memset((char *)&ret->them,0,sizeof(ret->them)); + ret->error=0; + return(ret); + } + +void BIO_CONNECT_free(a) +BIO_CONNECT *a; + { + if (a->param_hostname != NULL) + Free(a->param_hostname); + if (a->param_port != NULL) + Free(a->param_port); + Free(a); + } + +BIO_METHOD *BIO_s_connect() + { + return(&methods_connectp); + } + +static int conn_new(bi) +BIO *bi; + { + bi->init=0; + bi->num=INVALID_SOCKET; + bi->flags=0; + if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL) + return(0); + else + return(1); + } + +static void conn_close_socket(bio) +BIO *bio; + { + BIO_CONNECT *c; + + c=(BIO_CONNECT *)bio->ptr; + if (bio->num != INVALID_SOCKET) + { + /* Only do a shutdown if things were established */ + if (c->state == CONN_S_OK) + shutdown(bio->num,2); +# ifdef WINDOWS + closesocket(bio->num); +# else + close(bio->num); +# endif + bio->num=INVALID_SOCKET; + } + } + +static int conn_free(a) +BIO *a; + { + BIO_CONNECT *data; + + if (a == NULL) return(0); + data=(BIO_CONNECT *)a->ptr; + + if (a->shutdown) + { + conn_close_socket(a); + BIO_CONNECT_free(data); + a->ptr=NULL; + a->flags=0; + a->init=0; + } + return(1); + } + +static int conn_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + int ret=0; + BIO_CONNECT *data; + + data=(BIO_CONNECT *)b->ptr; + if (data->state != CONN_S_OK) + { + ret=conn_state(b,data); + if (ret <= 0) return(ret); + } + + if (out != NULL) + { + errno=0; +#if defined(WINDOWS) + ret=recv(b->num,out,outl,0); +#else + ret=read(b->num,out,outl); +#endif + BIO_clear_retry_flags(b); + if (ret <= 0) + { + if (BIO_sock_should_retry(ret)) + BIO_set_retry_read(b); + } + } + return(ret); + } + +static int conn_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + int ret; + BIO_CONNECT *data; + + data=(BIO_CONNECT *)b->ptr; + if (data->state != CONN_S_OK) + { + ret=conn_state(b,data); + if (ret <= 0) return(ret); + } + + errno=0; +#if defined(WINDOWS) + ret=send(b->num,in,inl,0); +#else + ret=write(b->num,in,inl); +#endif + BIO_clear_retry_flags(b); + if (ret <= 0) + { + if (BIO_sock_should_retry(ret)) + BIO_set_retry_write(b); + } + return(ret); + } + +static long conn_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + BIO *dbio; + int *ip; + long ret=1; + BIO_CONNECT *data; + + data=(BIO_CONNECT *)b->ptr; + + switch (cmd) + { + case BIO_CTRL_RESET: + ret=0; + data->state=CONN_S_BEFORE; + conn_close_socket(b); + b->flags=0; + break; + case BIO_C_DO_STATE_MACHINE: + /* use this one to start the connection */ + if (!data->state != CONN_S_OK) + ret=(long)conn_state(b,data); + else + ret=1; + break; + case BIO_C_SET_CONNECT: + if (ptr != NULL) + { + b->init=1; + if (num == 0) + { + if (data->param_hostname != NULL) + Free(data->param_hostname); + data->param_hostname=BUF_strdup(ptr); + } + else if (num == 1) + { + if (data->param_port != NULL) + Free(data->param_port); + data->param_port=BUF_strdup(ptr); + } + } + break; + case BIO_C_SET_NBIO: + data->nbio=(int)num; + break; + case BIO_C_GET_FD: + if (b->init) + { + ip=(int *)ptr; + if (ip != NULL) + *ip=b->num; + ret=b->num; + } + else + ret= -1; + break; + case BIO_CTRL_GET_CLOSE: + ret=b->shutdown; + break; + case BIO_CTRL_SET_CLOSE: + b->shutdown=(int)num; + break; + case BIO_CTRL_PENDING: + case BIO_CTRL_WPENDING: + ret=0; + break; + case BIO_CTRL_FLUSH: + break; + case BIO_CTRL_DUP: + dbio=(BIO *)ptr; + if (data->param_port) + BIO_set_port(dbio,data->param_port); + if (data->param_hostname) + BIO_set_hostname(dbio,data->param_hostname); + BIO_set_nbio(dbio,data->nbio); + break; + + default: + ret=0; + break; + } + return(ret); + } + +static int conn_puts(bp,str) +BIO *bp; +char *str; + { + int n,ret; + + n=strlen(str); + ret=conn_write(bp,str,n); + return(ret); + } + +BIO *BIO_new_connect(str) +char *str; + { + BIO *ret; + + ret=BIO_new(BIO_s_connect()); + if (ret == NULL) return(NULL); + if (BIO_set_hostname(ret,str)) + return(ret); + else + { + BIO_free(ret); + return(NULL); + } + } + +#endif + diff --git a/crypto/bio/bss_fd.c b/crypto/bio/bss_fd.c new file mode 100644 index 0000000000..06ae229ff6 --- /dev/null +++ b/crypto/bio/bss_fd.c @@ -0,0 +1,62 @@ +/* crypto/bio/bss_fd.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#define BIO_FD +#include "bss_sock.c" +#undef BIO_FD + diff --git a/crypto/bio/bss_file.c b/crypto/bio/bss_file.c new file mode 100644 index 0000000000..33c18e744b --- /dev/null +++ b/crypto/bio/bss_file.c @@ -0,0 +1,334 @@ +/* crypto/bio/bss_file.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <errno.h> +#include "cryptlib.h" +#include "bio.h" +#include "err.h" + +#ifndef NOPROTO +static int MS_CALLBACK file_write(BIO *h,char *buf,int num); +static int MS_CALLBACK file_read(BIO *h,char *buf,int size); +static int MS_CALLBACK file_puts(BIO *h,char *str); +static int MS_CALLBACK file_gets(BIO *h,char *str,int size); +static long MS_CALLBACK file_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int MS_CALLBACK file_new(BIO *h); +static int MS_CALLBACK file_free(BIO *data); +#else +static int MS_CALLBACK file_write(); +static int MS_CALLBACK file_read(); +static int MS_CALLBACK file_puts(); +static int MS_CALLBACK file_gets(); +static long MS_CALLBACK file_ctrl(); +static int MS_CALLBACK file_new(); +static int MS_CALLBACK file_free(); +#endif + +static BIO_METHOD methods_filep= + { + BIO_TYPE_FILE,"FILE pointer", + file_write, + file_read, + file_puts, + file_gets, + file_ctrl, + file_new, + file_free, + }; + +#if !defined(WIN16) || defined(APPS_WIN16) +BIO *BIO_new_file(filename,mode) +char *filename; +char *mode; + { + BIO *ret; + FILE *file; + + if ((file=fopen(filename,mode)) == NULL) + { + SYSerr(SYS_F_FOPEN,errno); + BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); + return(NULL); + } + + if ((ret=BIO_new(BIO_s_file())) == NULL) + return(NULL); +#if 0 + if ((ret=BIO_new(BIO_s_file_internal_w16())) == NULL) + return(NULL); +#endif + + BIO_set_fp(ret,file,BIO_CLOSE); + return(ret); + } + +BIO *BIO_new_fp(stream,close_flag) +FILE *stream; +int close_flag; + { + BIO *ret; + + if ((ret=BIO_new(BIO_s_file())) == NULL) + return(NULL); +#if 0 + if ((ret=BIO_new(BIO_s_file_internal_w16())) == NULL) +#endif + + BIO_set_fp(ret,stream,close_flag); + return(ret); + } +#endif /* !APPS_WIN16 */ + +#if !defined(WIN16) || defined(APPS_WIN16) + +BIO_METHOD *BIO_s_file() + { + return(&methods_filep); + } + +#else + +BIO_METHOD *BIO_s_file_internal_w16() + { + return(&methods_filep); + } + +#endif + +static int MS_CALLBACK file_new(bi) +BIO *bi; + { + bi->init=0; + bi->num=0; + bi->ptr=NULL; + return(1); + } + +static int MS_CALLBACK file_free(a) +BIO *a; + { + if (a == NULL) return(0); + if (a->shutdown) + { + if ((a->init) && (a->ptr != NULL)) + { + fclose((FILE *)a->ptr); + a->ptr=NULL; + } + a->init=0; + } + return(1); + } + +static int MS_CALLBACK file_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + int ret=0; + + if (b->init && (out != NULL)) + { + ret=fread(out,1,(int)outl,(FILE *)b->ptr); + } + return(ret); + } + +static int MS_CALLBACK file_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + int ret=0; + + if (b->init && (in != NULL)) + { + if (fwrite(in,(int)inl,1,(FILE *)b->ptr)) + ret=inl; + /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ + /* acording to Tim Hudson <tjh@cryptsoft.com>, the commented + * out version above can cause 'inl' write calls under + * some stupid stdio implementations (VMS) */ + } + return(ret); + } + +static long MS_CALLBACK file_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + long ret=1; + FILE *fp=(FILE *)b->ptr; + FILE **fpp; + char p[4]; + + switch (cmd) + { + case BIO_CTRL_RESET: + ret=(long)fseek(fp,num,0); + break; + case BIO_CTRL_EOF: + ret=(long)feof(fp); + break; + case BIO_CTRL_INFO: + ret=ftell(fp); + break; + case BIO_C_SET_FILE_PTR: + file_free(b); + b->shutdown=(int)num; + b->ptr=(char *)ptr; + b->init=1; + break; + case BIO_C_SET_FILENAME: + file_free(b); + b->shutdown=(int)num&BIO_CLOSE; + if (num & BIO_FP_APPEND) + { + if (num & BIO_FP_READ) + strcpy(p,"a+"); + else strcpy(p,"a"); + } + else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) + strcpy(p,"r+"); + else if (num & BIO_FP_WRITE) + strcpy(p,"w"); + else if (num & BIO_FP_READ) + strcpy(p,"r"); + else + { + BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE); + ret=0; + break; + } +#if defined(MSDOS) || defined(WINDOWS) + if (!(num & BIO_FP_TEXT)) + strcat(p,"b"); + else + strcat(p,"t"); +#endif + fp=fopen(ptr,p); + if (fp == NULL) + { + SYSerr(SYS_F_FOPEN,errno); + BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB); + ret=0; + break; + } + b->ptr=(char *)fp; + b->init=1; + break; + case BIO_C_GET_FILE_PTR: + /* the ptr parameter is actually a FILE ** in this case. */ + if (ptr != NULL) + { + fpp=(FILE **)ptr; + *fpp=(FILE *)b->ptr; + } + break; + case BIO_CTRL_GET_CLOSE: + ret=(long)b->shutdown; + break; + case BIO_CTRL_SET_CLOSE: + b->shutdown=(int)num; + break; + case BIO_CTRL_FLUSH: + fflush((FILE *)b->ptr); + break; + case BIO_CTRL_DUP: + ret=1; + break; + + case BIO_CTRL_WPENDING: + case BIO_CTRL_PENDING: + case BIO_CTRL_PUSH: + case BIO_CTRL_POP: + default: + ret=0; + break; + } + return(ret); + } + +static int MS_CALLBACK file_gets(bp,buf,size) +BIO *bp; +char *buf; +int size; + { + int ret=0; + + buf[0]='\0'; + fgets(buf,size,(FILE *)bp->ptr); + if (buf[0] != '\0') + ret=strlen(buf); + return(ret); + } + +static int MS_CALLBACK file_puts(bp,str) +BIO *bp; +char *str; + { + int n,ret; + + n=strlen(str); + ret=file_write(bp,str,n); + return(ret); + } + diff --git a/crypto/bio/bss_mem.c b/crypto/bio/bss_mem.c new file mode 100644 index 0000000000..9b4cf40acf --- /dev/null +++ b/crypto/bio/bss_mem.c @@ -0,0 +1,296 @@ +/* crypto/bio/bss_mem.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <errno.h> +#include "cryptlib.h" +#include "bio.h" + +#ifndef NOPROTO +static int mem_write(BIO *h,char *buf,int num); +static int mem_read(BIO *h,char *buf,int size); +static int mem_puts(BIO *h,char *str); +static int mem_gets(BIO *h,char *str,int size); +static long mem_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int mem_new(BIO *h); +static int mem_free(BIO *data); +#else +static int mem_write(); +static int mem_read(); +static int mem_puts(); +static int mem_gets(); +static long mem_ctrl(); +static int mem_new(); +static int mem_free(); +#endif + +static BIO_METHOD mem_method= + { + BIO_TYPE_MEM,"memory buffer", + mem_write, + mem_read, + mem_puts, + mem_gets, + mem_ctrl, + mem_new, + mem_free, + }; + +BIO_METHOD *BIO_s_mem() + { + return(&mem_method); + } + +static int mem_new(bi) +BIO *bi; + { + BUF_MEM *b; + + if ((b=BUF_MEM_new()) == NULL) + return(0); + bi->shutdown=1; + bi->init=1; + bi->num=0; + bi->ptr=(char *)b; + return(1); + } + +static int mem_free(a) +BIO *a; + { + if (a == NULL) return(0); + if (a->shutdown) + { + if ((a->init) && (a->ptr != NULL)) + { + BUF_MEM_free((BUF_MEM *)a->ptr); + a->ptr=NULL; + } + } + return(1); + } + +static int mem_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + int ret= -1; + BUF_MEM *bm; + int i; + char *from,*to; + + bm=(BUF_MEM *)b->ptr; + BIO_clear_retry_flags(b); + ret=(outl > bm->length)?bm->length:outl; + if ((out != NULL) && (ret > 0)) + { + memcpy(out,bm->data,ret); + bm->length-=ret; + /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */ + from=(char *)&(bm->data[ret]); + to=(char *)&(bm->data[0]); + for (i=0; i<bm->length; i++) + to[i]=from[i]; + } + else if (bm->length == 0) + { + BIO_set_retry_read(b); + ret= -1; + } + return(ret); + } + +static int mem_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + int ret= -1; + int blen; + BUF_MEM *bm; + + bm=(BUF_MEM *)b->ptr; + if (in == NULL) + { + BIOerr(BIO_F_MEM_WRITE,BIO_R_NULL_PARAMETER); + goto end; + } + + BIO_clear_retry_flags(b); + blen=bm->length; + if (BUF_MEM_grow(bm,blen+inl) != (blen+inl)) + goto end; + memcpy(&(bm->data[blen]),in,inl); + ret=inl; +end: + return(ret); + } + +static long mem_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + long ret=1; + char **pptr; + + BUF_MEM *bm=(BUF_MEM *)b->ptr; + + switch (cmd) + { + case BIO_CTRL_RESET: + if (bm->data != NULL) + memset(bm->data,0,bm->max); + bm->length=0; + break; + case BIO_CTRL_EOF: + ret=(long)(bm->length == 0); + break; + case BIO_CTRL_INFO: + ret=(long)bm->length; + if (ptr != NULL) + { + pptr=(char **)ptr; + *pptr=(char *)&(bm->data[0]); + } + break; + case BIO_C_SET_BUF_MEM: + mem_free(b); + b->shutdown=(int)num; + b->ptr=ptr; + break; + case BIO_C_GET_BUF_MEM_PTR: + if (ptr != NULL) + { + pptr=(char **)ptr; + *pptr=(char *)bm; + } + break; + case BIO_CTRL_GET_CLOSE: + ret=(long)b->shutdown; + break; + case BIO_CTRL_SET_CLOSE: + b->shutdown=(int)num; + break; + + case BIO_CTRL_WPENDING: + ret=0L; + break; + case BIO_CTRL_PENDING: + ret=(long)bm->length; + break; + case BIO_CTRL_DUP: + case BIO_CTRL_FLUSH: + ret=1; + break; + case BIO_CTRL_PUSH: + case BIO_CTRL_POP: + default: + ret=0; + break; + } + return(ret); + } + +static int mem_gets(bp,buf,size) +BIO *bp; +char *buf; +int size; + { + int i,j; + int ret= -1; + char *p; + BUF_MEM *bm=(BUF_MEM *)bp->ptr; + + BIO_clear_retry_flags(bp); + j=bm->length; + if (j <= 0) return(0); + p=bm->data; + for (i=0; i<j; i++) + { + if (p[i] == '\n') break; + } + if (i == j) + { + BIO_set_retry_read(bp); + /* return(-1); change the semantics 0.6.6a */ + } + else + i++; + /* i is the max to copy */ + if ((size-1) < i) i=size-1; + i=mem_read(bp,buf,i); + if (i > 0) buf[i]='\0'; + ret=i; + return(ret); + } + +static int mem_puts(bp,str) +BIO *bp; +char *str; + { + int n,ret; + + n=strlen(str); + ret=mem_write(bp,str,n); + /* memory semantics is that it will always work */ + return(ret); + } + diff --git a/crypto/bio/bss_null.c b/crypto/bio/bss_null.c new file mode 100644 index 0000000000..f93d471121 --- /dev/null +++ b/crypto/bio/bss_null.c @@ -0,0 +1,176 @@ +/* crypto/bio/bss_null.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include <stdio.h> +#include <errno.h> +#include "cryptlib.h" +#include "bio.h" + +#ifndef NOPROTO +static int null_write(BIO *h,char *buf,int num); +static int null_read(BIO *h,char *buf,int size); +static int null_puts(BIO *h,char *str); +static int null_gets(BIO *h,char *str,int size); +static long null_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int null_new(BIO *h); +static int null_free(BIO *data); +#else +static int null_write(); +static int null_read(); +static int null_puts(); +static int null_gets(); +static long null_ctrl(); +static int null_new(); +static int null_free(); +#endif + +static BIO_METHOD null_method= + { + BIO_TYPE_NULL,"NULL", + null_write, + null_read, + null_puts, + null_gets, + null_ctrl, + null_new, + null_free, + }; + +BIO_METHOD *BIO_s_null() + { + return(&null_method); + } + +static int null_new(bi) +BIO *bi; + { + bi->init=1; + bi->num=0; + bi->ptr=(NULL); + return(1); + } + +static int null_free(a) +BIO *a; + { + if (a == NULL) return(0); + return(1); + } + +static int null_read(b,out,outl) +BIO *b; +char *out; +int outl; + { + return(0); + } + +static int null_write(b,in,inl) +BIO *b; +char *in; +int inl; + { + return(inl); + } + +static long null_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + long ret=1; + + switch (cmd) + { + case BIO_CTRL_RESET: + case BIO_CTRL_EOF: + case BIO_CTRL_SET: + case BIO_CTRL_SET_CLOSE: + case BIO_CTRL_FLUSH: + case BIO_CTRL_DUP: + ret=1; + break; + case BIO_CTRL_GET_CLOSE: + case BIO_CTRL_INFO: + case BIO_CTRL_GET: + case BIO_CTRL_PENDING: + case BIO_CTRL_WPENDING: + default: + ret=0; + break; + } + return(ret); + } + +static int null_gets(bp,buf,size) +BIO *bp; +char *buf; +int size; + { + return(0); + } + +static int null_puts(bp,str) +BIO *bp; +char *str; + { + if (str == NULL) return(0); + return(strlen(str)); + } + diff --git a/crypto/bio/bss_rtcp.c b/crypto/bio/bss_rtcp.c new file mode 100644 index 0000000000..cd48f7ce95 --- /dev/null +++ b/crypto/bio/bss_rtcp.c @@ -0,0 +1,296 @@ +/* crypto/bio/bss_rtcp.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +/* Written by David L. Jones <jonesd@kcgl1.eng.ohio-state.edu> + * Date: 22-JUL-1996 + */ +/* VMS */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include "cryptlib.h" +#include "bio.h" + +#include <iodef.h> /* VMS IO$_ definitions */ +extern int SYS$QIOW(); +typedef unsigned short io_channel; +/*************************************************************************/ +struct io_status { short status, count; long flags; }; + +struct rpc_msg { /* Should have member alignment inhibited */ + char channel; /* 'A'-app data. 'R'-remote client 'G'-global */ + char function; /* 'G'-get, 'P'-put, 'C'-confirm, 'X'-close */ + unsigned short int length; /* Amount of data returned or max to return */ + char data[4092]; /* variable data */ +}; +#define RPC_HDR_SIZE (sizeof(struct rpc_msg) - 4092) + +struct rpc_ctx { + int filled, pos; + struct rpc_msg msg; +}; + +static int rtcp_write(BIO *h,char *buf,int num); +static int rtcp_read(BIO *h,char *buf,int size); +static int rtcp_puts(BIO *h,char *str); +static int rtcp_gets(BIO *h,char *str,int size); +static long rtcp_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int rtcp_new(BIO *h); +static int rtcp_free(BIO *data); + +static BIO_METHOD rtcp_method= + { + BIO_TYPE_FD,"RTCP", + rtcp_write, + rtcp_read, + rtcp_puts, + rtcp_gets, + rtcp_ctrl, + rtcp_new, + rtcp_free, + }; + +BIO_METHOD *BIO_s_rtcp() + { + return(&rtcp_method); + } +/*****************************************************************************/ +/* Decnet I/O routines. + */ +static int get ( io_channel chan, char *buffer, int maxlen, int *length ) +{ + int status; + struct io_status iosb; + status = SYS$QIOW ( 0, chan, IO$_READVBLK, &iosb, 0, 0, + buffer, maxlen, 0, 0, 0, 0 ); + if ( (status&1) == 1 ) status = iosb.status; + if ( (status&1) == 1 ) *length = iosb.count; + return status; +} + +static int put ( io_channel chan, char *buffer, int length ) +{ + int status; + struct io_status iosb; + status = SYS$QIOW ( 0, chan, IO$_WRITEVBLK, &iosb, 0, 0, + buffer, length, 0, 0, 0, 0 ); + if ( (status&1) == 1 ) status = iosb.status; + return status; +} +/***************************************************************************/ + +static int rtcp_new(bi) +BIO *bi; +{ + struct rpc_ctx *ctx; + bi->init=1; + bi->num=0; + bi->flags = 0; + bi->ptr=Malloc(sizeof(struct rpc_ctx)); + ctx = (struct rpc_ctx *) bi->ptr; + ctx->filled = 0; + ctx->pos = 0; + return(1); +} + +static int rtcp_free(a) +BIO *a; +{ + if (a == NULL) return(0); + if ( a->ptr ) Free ( a->ptr ); + a->ptr = NULL; + return(1); +} + +static int rtcp_read(b,out,outl) +BIO *b; +char *out; +int outl; +{ + int status, length; + struct rpc_ctx *ctx; + /* + * read data, return existing. + */ + ctx = (struct rpc_ctx *) b->ptr; + if ( ctx->pos < ctx->filled ) { + length = ctx->filled - ctx->pos; + if ( length > outl ) length = outl; + memmove ( out, &ctx->msg.data[ctx->pos], length ); + ctx->pos += length; + return length; + } + /* + * Requst more data from R channel. + */ + ctx->msg.channel = 'R'; + ctx->msg.function = 'G'; + ctx->msg.length = sizeof(ctx->msg.data); + status = put ( b->num, (char *) &ctx->msg, RPC_HDR_SIZE ); + if ( (status&1) == 0 ) { + return -1; + } + /* + * Read. + */ + ctx->pos = ctx->filled = 0; + status = get ( b->num, (char *) &ctx->msg, sizeof(ctx->msg), &length ); + if ( (status&1) == 0 ) length = -1; + if ( ctx->msg.channel != 'R' || ctx->msg.function != 'C' ) { + length = -1; + } + ctx->filled = length - RPC_HDR_SIZE; + + if ( ctx->pos < ctx->filled ) { + length = ctx->filled - ctx->pos; + if ( length > outl ) length = outl; + memmove ( out, ctx->msg.data, length ); + ctx->pos += length; + return length; + } + + return length; +} + +static int rtcp_write(b,in,inl) +BIO *b; +char *in; +int inl; +{ + int status, i, segment, length; + struct rpc_ctx *ctx; + /* + * Output data, send in chunks no larger that sizeof(ctx->msg.data). + */ + ctx = (struct rpc_ctx *) b->ptr; + for ( i = 0; i < inl; i += segment ) { + segment = inl - i; + if ( segment > sizeof(ctx->msg.data) ) segment = sizeof(ctx->msg.data); + ctx->msg.channel = 'R'; + ctx->msg.function = 'P'; + ctx->msg.length = segment; + memmove ( ctx->msg.data, &in[i], segment ); + status = put ( b->num, (char *) &ctx->msg, segment + RPC_HDR_SIZE ); + if ((status&1) == 0 ) { i = -1; break; } + + status = get ( b->num, (char *) &ctx->msg, sizeof(ctx->msg), &length ); + if ( ((status&1) == 0) || (length < RPC_HDR_SIZE) ) { i = -1; break; } + if ( (ctx->msg.channel != 'R') || (ctx->msg.function != 'C') ) { + printf("unexpected response when confirming put %c %c\n", + ctx->msg.channel, ctx->msg.function ); + + } + } + return(i); +} + +static long rtcp_ctrl(b,cmd,num,ptr) +BIO *b; +int cmd; +long num; +char *ptr; + { + long ret=1; + + switch (cmd) + { + case BIO_CTRL_RESET: + case BIO_CTRL_EOF: + ret = 1; + break; + case BIO_CTRL_SET: + b->num = num; + ret = 1; + break; + case BIO_CTRL_SET_CLOSE: + case BIO_CTRL_FLUSH: + case BIO_CTRL_DUP: + ret=1; + break; + case BIO_CTRL_GET_CLOSE: + case BIO_CTRL_INFO: + case BIO_CTRL_GET: + case BIO_CTRL_PENDING: + case BIO_CTRL_WPENDING: + default: + ret=0; + break; + } + return(ret); + } + +static int rtcp_gets(bp,buf,size) +BIO *bp; +char *buf; +int size; + { + return(0); + } + +static int rtcp_puts(bp,str) +BIO *bp; +char *str; +{ + int length; + if (str == NULL) return(0); + length = strlen ( str ); + if ( length == 0 ) return (0); + return rtcp_write ( bp,str, length ); +} + diff --git a/crypto/bio/bss_sock.c b/crypto/bio/bss_sock.c new file mode 100644 index 0000000000..ac96027051 --- /dev/null +++ b/crypto/bio/bss_sock.c @@ -0,0 +1,449 @@ +/* crypto/bio/bss_sock.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#if !defined(NO_SOCK) || defined(BIO_FD) + +#include <stdio.h> +#include <errno.h> +#define USE_SOCKETS +#include "cryptlib.h" +#include "bio.h" + +#ifndef BIO_FD +#ifndef NOPROTO +static int sock_write(BIO *h,char *buf,int num); +static int sock_read(BIO *h,char *buf,int size); +static int sock_puts(BIO *h,char *str); +static long sock_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int sock_new(BIO *h); +static int sock_free(BIO *data); +int BIO_sock_should_retry(int s); +#else +static int sock_write(); +static int sock_read(); +static int sock_puts(); +static long sock_ctrl(); +static int sock_new(); +static int sock_free(); +int BIO_sock_should_retry(); +#endif + +#else + +#ifndef NOPROTO +static int fd_write(BIO *h,char *buf,int num); +static int fd_read(BIO *h,char *buf,int size); +static int fd_puts(BIO *h,char *str); +static long fd_ctrl(BIO *h,int cmd,long arg1,char *arg2); +static int fd_new(BIO *h); +static int fd_free(BIO *data); +int BIO_fd_should_retry(int s); +#else +static int fd_write(); +static int fd_read(); +static int fd_puts(); +static long fd_ctrl(); +static int fd_new(); +static int fd_free(); +int BIO_fd_should_retry(); +#endif +#endif + +#ifndef BIO_FD +static BIO_METHOD methods_sockp= + { + BIO_TYPE_SOCKET,"socket", + sock_write, + sock_read, + sock_puts, + NULL, /* sock_gets, */ + sock_ctrl, + sock_new, + sock_free, + }; + +BIO_METHOD *BIO_s_socket() + { + return(&methods_sockp); + } +#else +static BIO_METHOD methods_fdp= + { + BIO_TYPE_FD,"file descriptor", + fd_write, + fd_read, + fd_puts, + NULL, /* fd_gets, */ + fd_ctrl, + fd_new, + fd_free, + }; + +BIO_METHOD *BIO_s_fd() + { + return(&methods_fdp); + } +#endif + +#ifndef BIO_FD +BIO *BIO_new_socket(fd,close_flag) +#else +BIO *BIO_new_fd(fd,close_flag) +#endif +int fd; +int close_flag; + { + BIO *ret; + +#ifndef BIO_FD + ret=BIO_new(BIO_s_socket()); +#else + ret=BIO_new(BIO_s_fd()); +#endif + if (ret == NULL) return(NULL); + BIO_set_fd(ret,fd,close_flag); + return(ret); + } + +#ifndef BIO_FD +static int sock_new(bi) +#else +static int fd_new(bi) +#endif +BIO *bi; + { + bi->init=0; + bi->num=0; + bi->ptr=NULL; + bi->flags=0; + return(1); + } + +#ifndef BIO_FD +static int sock_free(a) +#else +static int fd_free(a) +#endif +BIO *a; + { + if (a == NULL) return(0); + if (a->shutdown) + { + if (a->init) + { +#ifndef BIO_FD + shutdown(a->num,2); +# ifdef WINDOWS + closesocket(a->num); +# else + close(a->num); +# endif +#else /* BIO_FD */ + close(a->num); +#endif + + } + a->init=0; + a->flags=0; + } + return(1); + } + +#ifndef BIO_FD +static int sock_read(b,out,outl) +#else +static int fd_read(b,out,outl) +#endif +BIO *b; +char *out; +int outl; + { + int ret=0; + + if (out != NULL) + { + errno=0; +#if defined(WINDOWS) && !defined(BIO_FD) + ret=recv(b->num,out,outl,0); +#else + ret=read(b->num,out,outl); +#endif + BIO_clear_retry_flags(b); + if (ret <= 0) + { +#ifndef BIO_FD + if (BIO_sock_should_retry(ret)) +#else + if (BIO_fd_should_retry(ret)) +#endif + BIO_set_retry_read(b); + } + } + return(ret); + } + +#ifndef BIO_FD +static int sock_write(b,in,inl) +#else +static int fd_write(b,in,inl) +#endif +BIO *b; +char *in; +int inl; + { + int ret; + + errno=0; +#if defined(WINDOWS) && !defined(BIO_FD) + ret=send(b->num,in,inl,0); +#else + ret=write(b->num,in,inl); +#endif + BIO_clear_retry_flags(b); + if (ret <= 0) + { +#ifndef BIO_FD + if (BIO_sock_should_retry(ret)) +#else + if (BIO_fd_should_retry(ret)) +#endif + BIO_set_retry_write(b); + } + return(ret); + } + +#ifndef BIO_FD +static long sock_ctrl(b,cmd,num,ptr) +#else +static long fd_ctrl(b,cmd,num,ptr) +#endif +BIO *b; +int cmd; +long num; +char *ptr; + { + long ret=1; + int *ip; + + switch (cmd) + { + case BIO_CTRL_RESET: +#ifdef BIO_FD + ret=(long)lseek(b->num,0,0); +#else + ret=0; +#endif + break; + case BIO_CTRL_INFO: + ret=0; + break; + case BIO_C_SET_FD: +#ifndef BIO_FD + sock_free(b); +#else + fd_free(b); +#endif + b->num= *((int *)ptr); + b->shutdown=(int)num; + b->init=1; + break; + case BIO_C_GET_FD: + if (b->init) + { + ip=(int *)ptr; + if (ip != NULL) *ip=b->num; + ret=b->num; + } + else + ret= -1; + break; + case BIO_CTRL_GET_CLOSE: + ret=b->shutdown; + break; + case BIO_CTRL_SET_CLOSE: + b->shutdown=(int)num; + break; + case BIO_CTRL_PENDING: + case BIO_CTRL_WPENDING: + ret=0; + break; + case BIO_CTRL_DUP: + case BIO_CTRL_FLUSH: + ret=1; + break; + break; + default: + ret=0; + break; + } + return(ret); + } + +#ifdef undef +static int sock_gets(bp,buf,size) +BIO *bp; +char *buf; +int size; + { + return(-1); + } +#endif + +#ifndef BIO_FD +static int sock_puts(bp,str) +#else +static int fd_puts(bp,str) +#endif +BIO *bp; +char *str; + { + int n,ret; + + n=strlen(str); +#ifndef BIO_FD + ret=sock_write(bp,str,n); +#else + ret=fd_write(bp,str,n); +#endif + return(ret); + } + +#ifndef BIO_FD +int BIO_sock_should_retry(i) +#else +int BIO_fd_should_retry(i) +#endif +int i; + { + if ((i == 0) || (i == -1)) + { +#if !defined(BIO_FD) && defined(WINDOWS) + errno=WSAGetLastError(); +#endif + +#if defined(WINDOWS) /* more microsoft stupidity */ + if ((i == -1) && (errno == 0)) + return(1); +#endif +#ifndef BIO_FD + return(BIO_sock_non_fatal_error(errno)); +#else + return(BIO_fd_non_fatal_error(errno)); +#endif + } + return(0); + } + +#ifndef BIO_FD +int BIO_sock_non_fatal_error(err) +#else +int BIO_fd_non_fatal_error(err) +#endif +int err; + { + switch (err) + { +#if !defined(BIO_FD) && defined(WINDOWS) +# if defined(WSAEWOULDBLOCK) + case WSAEWOULDBLOCK: +# endif + +# if defined(WSAENOTCONN) + case WSAENOTCONN: +# endif +#endif + +#ifdef EWOULDBLOCK +# ifdef WSAEWOULDBLOCK +# if WSAEWOULDBLOCK != EWOULDBLOCK + case EWOULDBLOCK: +# endif +# else + case EWOULDBLOCK: +# endif +#endif + +#ifdef EINTR + case EINTR: +#endif + +#ifdef EAGAIN +#if EWOULDBLOCK != EAGAIN + case EAGAIN: +# endif +#endif + +#ifdef EPROTO + case EPROTO: +#endif + +#ifdef EINPROGRESS + case EINPROGRESS: +#endif + +#ifdef EALREADY + case EALREADY: +#endif + return(1); + break; + default: + break; + } + return(0); + } +#endif |