diff options
| author | ryker <> | 1998-10-05 20:13:14 +0000 |
|---|---|---|
| committer | ryker <> | 1998-10-05 20:13:14 +0000 |
| commit | fe5d0717e2760d02faf23bf5a714f17b33ae4abb (patch) | |
| tree | 8d4ad346f10a36bdd90b503d222bda6b4ecd0037 /src/lib/libcrypto/bio | |
| parent | 75bf5ead4149b2b67781def7ace1ec720ae1753e (diff) | |
| parent | aeeae06a79815dc190061534d47236cec09f9e32 (diff) | |
| download | openbsd-fe5d0717e2760d02faf23bf5a714f17b33ae4abb.tar.gz openbsd-fe5d0717e2760d02faf23bf5a714f17b33ae4abb.tar.bz2 openbsd-fe5d0717e2760d02faf23bf5a714f17b33ae4abb.zip | |
This commit was generated by cvs2git to track changes on a CVS vendor
branch.
Diffstat (limited to 'src/lib/libcrypto/bio')
| -rw-r--r-- | src/lib/libcrypto/bio/b_dump.c | 125 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/b_print.c | 92 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/b_sock.c | 628 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bf_buff.c | 512 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bf_nbio.c | 268 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bf_null.c | 196 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bio.h | 688 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bio_cb.c | 138 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bio_err.c | 130 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bio_lib.c | 519 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_acpt.c | 500 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_conn.c | 648 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_fd.c | 62 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_file.c | 339 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_mem.c | 297 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_null.c | 177 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_sock.c | 461 |
17 files changed, 5780 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bio/b_dump.c b/src/lib/libcrypto/bio/b_dump.c new file mode 100644 index 0000000000..db84ad3d47 --- /dev/null +++ b/src/lib/libcrypto/bio/b_dump.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | /* crypto/bio/b_dump.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | * Stolen from tjh's ssl/ssl_trc.c stuff. | ||
| 61 | */ | ||
| 62 | |||
| 63 | #include <stdio.h> | ||
| 64 | #include "cryptlib.h" | ||
| 65 | #include "bio.h" | ||
| 66 | |||
| 67 | #define TRUNCATE | ||
| 68 | #define DUMP_WIDTH 16 | ||
| 69 | |||
| 70 | int BIO_dump(bio,s,len) | ||
| 71 | BIO *bio; | ||
| 72 | char *s; | ||
| 73 | int len; | ||
| 74 | { | ||
| 75 | int ret=0; | ||
| 76 | char buf[160+1],tmp[20]; | ||
| 77 | int i,j,rows,trunc; | ||
| 78 | unsigned char ch; | ||
| 79 | |||
| 80 | trunc=0; | ||
| 81 | |||
| 82 | #ifdef TRUNCATE | ||
| 83 | for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--) | ||
| 84 | trunc++; | ||
| 85 | #endif | ||
| 86 | |||
| 87 | rows=(len/DUMP_WIDTH); | ||
| 88 | if ((rows*DUMP_WIDTH)<len) | ||
| 89 | rows++; | ||
| 90 | for(i=0;i<rows;i++) { | ||
| 91 | buf[0]='\0'; /* start with empty string */ | ||
| 92 | sprintf(tmp,"%04x - ",i*DUMP_WIDTH); | ||
| 93 | strcpy(buf,tmp); | ||
| 94 | for(j=0;j<DUMP_WIDTH;j++) { | ||
| 95 | if (((i*DUMP_WIDTH)+j)>=len) { | ||
| 96 | strcat(buf," "); | ||
| 97 | } else { | ||
| 98 | ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff; | ||
| 99 | sprintf(tmp,"%02x%c",ch,j==7?'-':' '); | ||
| 100 | strcat(buf,tmp); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | strcat(buf," "); | ||
| 104 | for(j=0;j<DUMP_WIDTH;j++) { | ||
| 105 | if (((i*DUMP_WIDTH)+j)>=len) | ||
| 106 | break; | ||
| 107 | ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff; | ||
| 108 | sprintf(tmp,"%c",((ch>=' ')&&(ch<='~'))?ch:'.'); | ||
| 109 | strcat(buf,tmp); | ||
| 110 | } | ||
| 111 | strcat(buf,"\n"); | ||
| 112 | /* if this is the last call then update the ddt_dump thing so that | ||
| 113 | * we will move the selection point in the debug window | ||
| 114 | */ | ||
| 115 | ret+=BIO_write(bio,(char *)buf,strlen(buf)); | ||
| 116 | } | ||
| 117 | #ifdef TRUNCATE | ||
| 118 | if (trunc > 0) { | ||
| 119 | sprintf(buf,"%04x - <SPACES/NULS>\n",len+trunc); | ||
| 120 | ret+=BIO_write(bio,(char *)buf,strlen(buf)); | ||
| 121 | } | ||
| 122 | #endif | ||
| 123 | return(ret); | ||
| 124 | } | ||
| 125 | |||
diff --git a/src/lib/libcrypto/bio/b_print.c b/src/lib/libcrypto/bio/b_print.c new file mode 100644 index 0000000000..cdadeb839a --- /dev/null +++ b/src/lib/libcrypto/bio/b_print.c | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | /* crypto/bio/b_print.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | * Stolen from tjh's ssl/ssl_trc.c stuff. | ||
| 61 | */ | ||
| 62 | |||
| 63 | #include <stdio.h> | ||
| 64 | #include "cryptlib.h" | ||
| 65 | #include "bio.h" | ||
| 66 | |||
| 67 | int BIO_printf ( VAR_PLIST( BIO *, bio ) ) | ||
| 68 | VAR_ALIST | ||
| 69 | { | ||
| 70 | VAR_BDEFN(args, BIO *, bio); | ||
| 71 | char *format; | ||
| 72 | int ret; | ||
| 73 | MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */ | ||
| 74 | |||
| 75 | VAR_INIT(args, BIO *, bio); | ||
| 76 | VAR_ARG(args, char *, format); | ||
| 77 | |||
| 78 | hugebuf[0]='\0'; | ||
| 79 | |||
| 80 | /* no-one uses _doprnt anymore and it appears to be broken under SunOS 4.1.4 */ | ||
| 81 | #if 0 && defined(sun) && !defined(VAR_ANSI) /**/ | ||
| 82 | _doprnt(hugebuf,format,args); | ||
| 83 | #else /* !sun */ | ||
| 84 | vsprintf(hugebuf,format,args); | ||
| 85 | #endif /* sun */ | ||
| 86 | |||
| 87 | ret=BIO_write(bio,hugebuf,strlen(hugebuf)); | ||
| 88 | |||
| 89 | VAR_END( args ); | ||
| 90 | return(ret); | ||
| 91 | } | ||
| 92 | |||
diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c new file mode 100644 index 0000000000..a45909527c --- /dev/null +++ b/src/lib/libcrypto/bio/b_sock.c | |||
| @@ -0,0 +1,628 @@ | |||
| 1 | /* crypto/bio/b_sock.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #ifndef NO_SOCK | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <stdlib.h> | ||
| 63 | #include <errno.h> | ||
| 64 | #define USE_SOCKETS | ||
| 65 | #include "cryptlib.h" | ||
| 66 | #include "bio.h" | ||
| 67 | |||
| 68 | /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ | ||
| 69 | |||
| 70 | #ifdef WIN16 | ||
| 71 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | ||
| 72 | #else | ||
| 73 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
| 74 | #endif | ||
| 75 | |||
| 76 | #ifdef SO_MAXCONN | ||
| 77 | #define MAX_LISTEN SOMAXCONN | ||
| 78 | #elif defined(SO_MAXCONN) | ||
| 79 | #define MAX_LISTEN SO_MAXCONN | ||
| 80 | #else | ||
| 81 | #define MAX_LISTEN 32 | ||
| 82 | #endif | ||
| 83 | |||
| 84 | #ifdef WINDOWS | ||
| 85 | static int wsa_init_done=0; | ||
| 86 | #endif | ||
| 87 | |||
| 88 | static unsigned long BIO_ghbn_hits=0L; | ||
| 89 | static unsigned long BIO_ghbn_miss=0L; | ||
| 90 | |||
| 91 | #define GHBN_NUM 4 | ||
| 92 | static struct ghbn_cache_st | ||
| 93 | { | ||
| 94 | char name[129]; | ||
| 95 | struct hostent *ent; | ||
| 96 | unsigned long order; | ||
| 97 | } ghbn_cache[GHBN_NUM]; | ||
| 98 | |||
| 99 | #ifndef NOPROTO | ||
| 100 | static int get_ip(char *str,unsigned char *ip); | ||
| 101 | static void ghbn_free(struct hostent *a); | ||
| 102 | static struct hostent *ghbn_dup(struct hostent *a); | ||
| 103 | #else | ||
| 104 | static int get_ip(); | ||
| 105 | static void ghbn_free(); | ||
| 106 | static struct hostent *ghbn_dup(); | ||
| 107 | #endif | ||
| 108 | |||
| 109 | int BIO_get_host_ip(str,ip) | ||
| 110 | char *str; | ||
| 111 | unsigned char *ip; | ||
| 112 | { | ||
| 113 | int i; | ||
| 114 | struct hostent *he; | ||
| 115 | |||
| 116 | i=get_ip(str,ip); | ||
| 117 | if (i > 0) return(1); | ||
| 118 | if (i < 0) | ||
| 119 | { | ||
| 120 | BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS); | ||
| 121 | ERR_add_error_data(2,"host=",str); | ||
| 122 | return(0); | ||
| 123 | } | ||
| 124 | else | ||
| 125 | { /* do a gethostbyname */ | ||
| 126 | if (!BIO_sock_init()) return(0); | ||
| 127 | |||
| 128 | he=BIO_gethostbyname(str); | ||
| 129 | if (he == NULL) | ||
| 130 | { | ||
| 131 | BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP); | ||
| 132 | ERR_add_error_data(2,"host=",str); | ||
| 133 | return(0); | ||
| 134 | } | ||
| 135 | |||
| 136 | /* cast to short because of win16 winsock definition */ | ||
| 137 | if ((short)he->h_addrtype != AF_INET) | ||
| 138 | { | ||
| 139 | BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); | ||
| 140 | ERR_add_error_data(2,"host=",str); | ||
| 141 | return(0); | ||
| 142 | } | ||
| 143 | for (i=0; i<4; i++) | ||
| 144 | ip[i]=he->h_addr_list[0][i]; | ||
| 145 | } | ||
| 146 | return(1); | ||
| 147 | } | ||
| 148 | |||
| 149 | int BIO_get_port(str,port_ptr) | ||
| 150 | char *str; | ||
| 151 | short *port_ptr; | ||
| 152 | { | ||
| 153 | int i; | ||
| 154 | struct servent *s; | ||
| 155 | |||
| 156 | if (str == NULL) | ||
| 157 | { | ||
| 158 | BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED); | ||
| 159 | return(0); | ||
| 160 | } | ||
| 161 | i=atoi(str); | ||
| 162 | if (i != 0) | ||
| 163 | *port_ptr=(unsigned short)i; | ||
| 164 | else | ||
| 165 | { | ||
| 166 | s=getservbyname(str,"tcp"); | ||
| 167 | if (s == NULL) | ||
| 168 | { | ||
| 169 | if (strcmp(str,"http") == 0) | ||
| 170 | *port_ptr=80; | ||
| 171 | else if (strcmp(str,"telnet") == 0) | ||
| 172 | *port_ptr=23; | ||
| 173 | else if (strcmp(str,"socks") == 0) | ||
| 174 | *port_ptr=1080; | ||
| 175 | else if (strcmp(str,"https") == 0) | ||
| 176 | *port_ptr=443; | ||
| 177 | else if (strcmp(str,"ssl") == 0) | ||
| 178 | *port_ptr=443; | ||
| 179 | else if (strcmp(str,"ftp") == 0) | ||
| 180 | *port_ptr=21; | ||
| 181 | else if (strcmp(str,"gopher") == 0) | ||
| 182 | *port_ptr=70; | ||
| 183 | #if 0 | ||
| 184 | else if (strcmp(str,"wais") == 0) | ||
| 185 | *port_ptr=21; | ||
| 186 | #endif | ||
| 187 | else | ||
| 188 | { | ||
| 189 | SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error()); | ||
| 190 | ERR_add_error_data(3,"service='",str,"'"); | ||
| 191 | return(0); | ||
| 192 | } | ||
| 193 | return(1); | ||
| 194 | } | ||
| 195 | *port_ptr=htons((unsigned short)s->s_port); | ||
| 196 | } | ||
| 197 | return(1); | ||
| 198 | } | ||
| 199 | |||
| 200 | int BIO_sock_error(sock) | ||
| 201 | int sock; | ||
| 202 | { | ||
| 203 | int j,i,size; | ||
| 204 | |||
| 205 | size=sizeof(int); | ||
| 206 | |||
| 207 | i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(char *)&j,&size); | ||
| 208 | if (i < 0) | ||
| 209 | return(1); | ||
| 210 | else | ||
| 211 | return(j); | ||
| 212 | } | ||
| 213 | |||
| 214 | long BIO_ghbn_ctrl(cmd,iarg,parg) | ||
| 215 | int cmd; | ||
| 216 | int iarg; | ||
| 217 | char *parg; | ||
| 218 | { | ||
| 219 | int i; | ||
| 220 | char **p; | ||
| 221 | |||
| 222 | switch (cmd) | ||
| 223 | { | ||
| 224 | case BIO_GHBN_CTRL_HITS: | ||
| 225 | return(BIO_ghbn_hits); | ||
| 226 | break; | ||
| 227 | case BIO_GHBN_CTRL_MISSES: | ||
| 228 | return(BIO_ghbn_miss); | ||
| 229 | break; | ||
| 230 | case BIO_GHBN_CTRL_CACHE_SIZE: | ||
| 231 | return(GHBN_NUM); | ||
| 232 | break; | ||
| 233 | case BIO_GHBN_CTRL_GET_ENTRY: | ||
| 234 | if ((iarg >= 0) && (iarg <GHBN_NUM) && | ||
| 235 | (ghbn_cache[iarg].order > 0)) | ||
| 236 | { | ||
| 237 | p=(char **)parg; | ||
| 238 | if (p == NULL) return(0); | ||
| 239 | *p=ghbn_cache[iarg].name; | ||
| 240 | ghbn_cache[iarg].name[128]='\0'; | ||
| 241 | return(1); | ||
| 242 | } | ||
| 243 | return(0); | ||
| 244 | break; | ||
| 245 | case BIO_GHBN_CTRL_FLUSH: | ||
| 246 | for (i=0; i<GHBN_NUM; i++) | ||
| 247 | ghbn_cache[i].order=0; | ||
| 248 | break; | ||
| 249 | default: | ||
| 250 | return(0); | ||
| 251 | } | ||
| 252 | return(1); | ||
| 253 | } | ||
| 254 | |||
| 255 | static struct hostent *ghbn_dup(a) | ||
| 256 | struct hostent *a; | ||
| 257 | { | ||
| 258 | struct hostent *ret; | ||
| 259 | int i,j; | ||
| 260 | |||
| 261 | ret=(struct hostent *)malloc(sizeof(struct hostent)); | ||
| 262 | if (ret == NULL) return(NULL); | ||
| 263 | memset(ret,0,sizeof(struct hostent)); | ||
| 264 | |||
| 265 | for (i=0; a->h_aliases[i] != NULL; i++) | ||
| 266 | ; | ||
| 267 | i++; | ||
| 268 | ret->h_aliases=(char **)malloc(sizeof(char *)*i); | ||
| 269 | memset(ret->h_aliases,0,sizeof(char *)*i); | ||
| 270 | if (ret == NULL) goto err; | ||
| 271 | |||
| 272 | for (i=0; a->h_addr_list[i] != NULL; i++) | ||
| 273 | ; | ||
| 274 | i++; | ||
| 275 | ret->h_addr_list=(char **)malloc(sizeof(char *)*i); | ||
| 276 | memset(ret->h_addr_list,0,sizeof(char *)*i); | ||
| 277 | if (ret->h_addr_list == NULL) goto err; | ||
| 278 | |||
| 279 | j=strlen(a->h_name)+1; | ||
| 280 | if ((ret->h_name=malloc(j)) == NULL) goto err; | ||
| 281 | memcpy((char *)ret->h_name,a->h_name,j); | ||
| 282 | for (i=0; a->h_aliases[i] != NULL; i++) | ||
| 283 | { | ||
| 284 | j=strlen(a->h_aliases[i])+1; | ||
| 285 | if ((ret->h_aliases[i]=malloc(j)) == NULL) goto err; | ||
| 286 | memcpy(ret->h_aliases[i],a->h_aliases[i],j); | ||
| 287 | } | ||
| 288 | ret->h_length=a->h_length; | ||
| 289 | ret->h_addrtype=a->h_addrtype; | ||
| 290 | for (i=0; a->h_addr_list[i] != NULL; i++) | ||
| 291 | { | ||
| 292 | if ((ret->h_addr_list[i]=malloc(a->h_length)) == NULL) | ||
| 293 | goto err; | ||
| 294 | memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length); | ||
| 295 | } | ||
| 296 | return(ret); | ||
| 297 | err: | ||
| 298 | if (ret != NULL) | ||
| 299 | ghbn_free(ret); | ||
| 300 | return(NULL); | ||
| 301 | } | ||
| 302 | |||
| 303 | static void ghbn_free(a) | ||
| 304 | struct hostent *a; | ||
| 305 | { | ||
| 306 | int i; | ||
| 307 | |||
| 308 | if (a->h_aliases != NULL) | ||
| 309 | { | ||
| 310 | for (i=0; a->h_aliases[i] != NULL; i++) | ||
| 311 | free(a->h_aliases[i]); | ||
| 312 | free(a->h_aliases); | ||
| 313 | } | ||
| 314 | if (a->h_addr_list != NULL) | ||
| 315 | { | ||
| 316 | for (i=0; a->h_addr_list[i] != NULL; i++) | ||
| 317 | free(a->h_addr_list[i]); | ||
| 318 | free(a->h_addr_list); | ||
| 319 | } | ||
| 320 | if (a->h_name != NULL) free((char *)a->h_name); | ||
| 321 | free(a); | ||
| 322 | } | ||
| 323 | |||
| 324 | struct hostent *BIO_gethostbyname(name) | ||
| 325 | char *name; | ||
| 326 | { | ||
| 327 | struct hostent *ret; | ||
| 328 | int i,lowi=0,j; | ||
| 329 | unsigned long low= (unsigned long)-1; | ||
| 330 | |||
| 331 | /* return(gethostbyname(name)); */ | ||
| 332 | |||
| 333 | CRYPTO_w_lock(CRYPTO_LOCK_BIO_GETHOSTBYNAME); | ||
| 334 | j=strlen(name); | ||
| 335 | if (j < 128) | ||
| 336 | { | ||
| 337 | for (i=0; i<GHBN_NUM; i++) | ||
| 338 | { | ||
| 339 | if (low > ghbn_cache[i].order) | ||
| 340 | { | ||
| 341 | low=ghbn_cache[i].order; | ||
| 342 | lowi=i; | ||
| 343 | } | ||
| 344 | if (ghbn_cache[i].order > 0) | ||
| 345 | { | ||
| 346 | if (strncmp(name,ghbn_cache[i].name,128) == 0) | ||
| 347 | break; | ||
| 348 | } | ||
| 349 | } | ||
| 350 | } | ||
| 351 | else | ||
| 352 | i=GHBN_NUM; | ||
| 353 | |||
| 354 | if (i == GHBN_NUM) /* no hit*/ | ||
| 355 | { | ||
| 356 | BIO_ghbn_miss++; | ||
| 357 | ret=gethostbyname(name); | ||
| 358 | |||
| 359 | if (ret == NULL) return(NULL); | ||
| 360 | if (j > 128) return(ret); /* too big to cache */ | ||
| 361 | |||
| 362 | /* else add to cache */ | ||
| 363 | if (ghbn_cache[lowi].ent != NULL) | ||
| 364 | ghbn_free(ghbn_cache[lowi].ent); | ||
| 365 | |||
| 366 | strncpy(ghbn_cache[lowi].name,name,128); | ||
| 367 | ghbn_cache[lowi].ent=ghbn_dup(ret); | ||
| 368 | ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits; | ||
| 369 | } | ||
| 370 | else | ||
| 371 | { | ||
| 372 | BIO_ghbn_hits++; | ||
| 373 | ret= ghbn_cache[i].ent; | ||
| 374 | ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits; | ||
| 375 | } | ||
| 376 | CRYPTO_w_unlock(CRYPTO_LOCK_BIO_GETHOSTBYNAME); | ||
| 377 | return(ret); | ||
| 378 | } | ||
| 379 | |||
| 380 | int BIO_sock_init() | ||
| 381 | { | ||
| 382 | #ifdef WINDOWS | ||
| 383 | static struct WSAData wsa_state; | ||
| 384 | |||
| 385 | if (!wsa_init_done) | ||
| 386 | { | ||
| 387 | int err; | ||
| 388 | |||
| 389 | #ifdef SIGINT | ||
| 390 | signal(SIGINT,(void (*)(int))BIO_sock_cleanup); | ||
| 391 | #endif | ||
| 392 | wsa_init_done=1; | ||
| 393 | memset(&wsa_state,0,sizeof(wsa_state)); | ||
| 394 | if (WSAStartup(0x0101,&wsa_state)!=0) | ||
| 395 | { | ||
| 396 | err=WSAGetLastError(); | ||
| 397 | SYSerr(SYS_F_WSASTARTUP,err); | ||
| 398 | BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP); | ||
| 399 | return(-1); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | #endif /* WINDOWS */ | ||
| 403 | return(1); | ||
| 404 | } | ||
| 405 | |||
| 406 | void BIO_sock_cleanup() | ||
| 407 | { | ||
| 408 | #ifdef WINDOWS | ||
| 409 | if (wsa_init_done) | ||
| 410 | { | ||
| 411 | wsa_init_done=0; | ||
| 412 | WSACancelBlockingCall(); | ||
| 413 | WSACleanup(); | ||
| 414 | } | ||
| 415 | #endif | ||
| 416 | } | ||
| 417 | |||
| 418 | int BIO_socket_ioctl(fd,type,arg) | ||
| 419 | int fd; | ||
| 420 | long type; | ||
| 421 | unsigned long *arg; | ||
| 422 | { | ||
| 423 | int i; | ||
| 424 | |||
| 425 | i=ioctlsocket(fd,type,arg); | ||
| 426 | if (i < 0) | ||
| 427 | SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error()); | ||
| 428 | return(i); | ||
| 429 | } | ||
| 430 | |||
| 431 | /* The reason I have implemented this instead of using sscanf is because | ||
| 432 | * Visual C 1.52c gives an unresolved external when linking a DLL :-( */ | ||
| 433 | static int get_ip(str,ip) | ||
| 434 | char *str; | ||
| 435 | unsigned char ip[4]; | ||
| 436 | { | ||
| 437 | unsigned int tmp[4]; | ||
| 438 | int num=0,c,ok=0; | ||
| 439 | |||
| 440 | tmp[0]=tmp[1]=tmp[2]=tmp[3]=0; | ||
| 441 | |||
| 442 | for (;;) | ||
| 443 | { | ||
| 444 | c= *(str++); | ||
| 445 | if ((c >= '0') && (c <= '9')) | ||
| 446 | { | ||
| 447 | ok=1; | ||
| 448 | tmp[num]=tmp[num]*10+c-'0'; | ||
| 449 | if (tmp[num] > 255) return(-1); | ||
| 450 | } | ||
| 451 | else if (c == '.') | ||
| 452 | { | ||
| 453 | if (!ok) return(-1); | ||
| 454 | if (num == 3) break; | ||
| 455 | num++; | ||
| 456 | ok=0; | ||
| 457 | } | ||
| 458 | else if ((num == 3) && ok) | ||
| 459 | break; | ||
| 460 | else | ||
| 461 | return(0); | ||
| 462 | } | ||
| 463 | ip[0]=tmp[0]; | ||
| 464 | ip[1]=tmp[1]; | ||
| 465 | ip[2]=tmp[2]; | ||
| 466 | ip[3]=tmp[3]; | ||
| 467 | return(1); | ||
| 468 | } | ||
| 469 | |||
| 470 | int BIO_get_accept_socket(host) | ||
| 471 | char *host; | ||
| 472 | { | ||
| 473 | int ret=0; | ||
| 474 | struct sockaddr_in server; | ||
| 475 | int s= -1; | ||
| 476 | unsigned char ip[4]; | ||
| 477 | short port; | ||
| 478 | char *str,*h,*p,*e; | ||
| 479 | unsigned long l; | ||
| 480 | |||
| 481 | if (!BIO_sock_init()) return(INVALID_SOCKET); | ||
| 482 | |||
| 483 | if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET); | ||
| 484 | |||
| 485 | h=p=NULL; | ||
| 486 | h=str; | ||
| 487 | for (e=str; *e; e++) | ||
| 488 | { | ||
| 489 | if (*e == ':') | ||
| 490 | { | ||
| 491 | p= &(e[1]); | ||
| 492 | *e='\0'; | ||
| 493 | } | ||
| 494 | else if (*e == '/') | ||
| 495 | { | ||
| 496 | *e='\0'; | ||
| 497 | break; | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | if (p == NULL) | ||
| 502 | { | ||
| 503 | p=h; | ||
| 504 | h="*"; | ||
| 505 | } | ||
| 506 | |||
| 507 | if (!BIO_get_port(p,&port)) return(INVALID_SOCKET); | ||
| 508 | |||
| 509 | memset((char *)&server,0,sizeof(server)); | ||
| 510 | server.sin_family=AF_INET; | ||
| 511 | server.sin_port=htons((unsigned short)port); | ||
| 512 | |||
| 513 | if (strcmp(h,"*") == 0) | ||
| 514 | server.sin_addr.s_addr=INADDR_ANY; | ||
| 515 | else | ||
| 516 | { | ||
| 517 | if (!BIO_get_host_ip(h,&(ip[0]))) return(INVALID_SOCKET); | ||
| 518 | l=(unsigned long) | ||
| 519 | ((unsigned long)ip[0]<<24L)| | ||
| 520 | ((unsigned long)ip[0]<<16L)| | ||
| 521 | ((unsigned long)ip[0]<< 8L)| | ||
| 522 | ((unsigned long)ip[0]); | ||
| 523 | server.sin_addr.s_addr=htonl(l); | ||
| 524 | } | ||
| 525 | |||
| 526 | s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); | ||
| 527 | if (s == INVALID_SOCKET) | ||
| 528 | { | ||
| 529 | SYSerr(SYS_F_SOCKET,get_last_socket_error()); | ||
| 530 | ERR_add_error_data(3,"port='",host,"'"); | ||
| 531 | BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET); | ||
| 532 | goto err; | ||
| 533 | } | ||
| 534 | if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) | ||
| 535 | { | ||
| 536 | SYSerr(SYS_F_BIND,get_last_socket_error()); | ||
| 537 | ERR_add_error_data(3,"port='",host,"'"); | ||
| 538 | BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET); | ||
| 539 | goto err; | ||
| 540 | } | ||
| 541 | if (listen(s,MAX_LISTEN) == -1) | ||
| 542 | { | ||
| 543 | SYSerr(SYS_F_BIND,get_last_socket_error()); | ||
| 544 | ERR_add_error_data(3,"port='",host,"'"); | ||
| 545 | BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET); | ||
| 546 | goto err; | ||
| 547 | } | ||
| 548 | ret=1; | ||
| 549 | err: | ||
| 550 | if (str != NULL) Free(str); | ||
| 551 | if ((ret == 0) && (s != INVALID_SOCKET)) | ||
| 552 | { | ||
| 553 | #ifdef WINDOWS | ||
| 554 | closesocket(s); | ||
| 555 | #else | ||
| 556 | close(s); | ||
| 557 | #endif | ||
| 558 | s= INVALID_SOCKET; | ||
| 559 | } | ||
| 560 | return(s); | ||
| 561 | } | ||
| 562 | |||
| 563 | int BIO_accept(sock,addr) | ||
| 564 | int sock; | ||
| 565 | char **addr; | ||
| 566 | { | ||
| 567 | int ret=INVALID_SOCKET; | ||
| 568 | static struct sockaddr_in from; | ||
| 569 | unsigned long l; | ||
| 570 | short port; | ||
| 571 | int len; | ||
| 572 | char *p; | ||
| 573 | |||
| 574 | memset((char *)&from,0,sizeof(from)); | ||
| 575 | len=sizeof(from); | ||
| 576 | ret=accept(sock,(struct sockaddr *)&from,&len); | ||
| 577 | if (ret == INVALID_SOCKET) | ||
| 578 | { | ||
| 579 | SYSerr(SYS_F_ACCEPT,get_last_socket_error()); | ||
| 580 | BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR); | ||
| 581 | goto end; | ||
| 582 | } | ||
| 583 | |||
| 584 | if (addr == NULL) goto end; | ||
| 585 | |||
| 586 | l=ntohl(from.sin_addr.s_addr); | ||
| 587 | port=ntohs(from.sin_port); | ||
| 588 | if (*addr == NULL) | ||
| 589 | { | ||
| 590 | if ((p=Malloc(24)) == NULL) | ||
| 591 | { | ||
| 592 | BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE); | ||
| 593 | goto end; | ||
| 594 | } | ||
| 595 | *addr=p; | ||
| 596 | } | ||
| 597 | sprintf(*addr,"%d.%d.%d.%d:%d", | ||
| 598 | (unsigned char)(l>>24L)&0xff, | ||
| 599 | (unsigned char)(l>>16L)&0xff, | ||
| 600 | (unsigned char)(l>> 8L)&0xff, | ||
| 601 | (unsigned char)(l )&0xff, | ||
| 602 | port); | ||
| 603 | end: | ||
| 604 | return(ret); | ||
| 605 | } | ||
| 606 | |||
| 607 | int BIO_set_tcp_ndelay(s,on) | ||
| 608 | int s; | ||
| 609 | int on; | ||
| 610 | { | ||
| 611 | int ret=0; | ||
| 612 | #if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) | ||
| 613 | int opt; | ||
| 614 | |||
| 615 | #ifdef SOL_TCP | ||
| 616 | opt=SOL_TCP; | ||
| 617 | #else | ||
| 618 | #ifdef IPPROTO_TCP | ||
| 619 | opt=IPPROTO_TCP; | ||
| 620 | #endif | ||
| 621 | #endif | ||
| 622 | |||
| 623 | ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on)); | ||
| 624 | #endif | ||
| 625 | return(ret == 0); | ||
| 626 | } | ||
| 627 | #endif | ||
| 628 | |||
diff --git a/src/lib/libcrypto/bio/bf_buff.c b/src/lib/libcrypto/bio/bf_buff.c new file mode 100644 index 0000000000..7912b88473 --- /dev/null +++ b/src/lib/libcrypto/bio/bf_buff.c | |||
| @@ -0,0 +1,512 @@ | |||
| 1 | /* crypto/bio/bf_buff.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include "bio.h" | ||
| 63 | #include "evp.h" | ||
| 64 | |||
| 65 | #ifndef NOPROTO | ||
| 66 | static int buffer_write(BIO *h,char *buf,int num); | ||
| 67 | static int buffer_read(BIO *h,char *buf,int size); | ||
| 68 | static int buffer_puts(BIO *h,char *str); | ||
| 69 | static int buffer_gets(BIO *h,char *str,int size); | ||
| 70 | static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 71 | static int buffer_new(BIO *h); | ||
| 72 | static int buffer_free(BIO *data); | ||
| 73 | #else | ||
| 74 | static int buffer_write(); | ||
| 75 | static int buffer_read(); | ||
| 76 | static int buffer_puts(); | ||
| 77 | static int buffer_gets(); | ||
| 78 | static long buffer_ctrl(); | ||
| 79 | static int buffer_new(); | ||
| 80 | static int buffer_free(); | ||
| 81 | #endif | ||
| 82 | |||
| 83 | #define DEFAULT_BUFFER_SIZE 1024 | ||
| 84 | |||
| 85 | static BIO_METHOD methods_buffer= | ||
| 86 | { | ||
| 87 | BIO_TYPE_BUFFER, | ||
| 88 | "buffer", | ||
| 89 | buffer_write, | ||
| 90 | buffer_read, | ||
| 91 | buffer_puts, | ||
| 92 | buffer_gets, | ||
| 93 | buffer_ctrl, | ||
| 94 | buffer_new, | ||
| 95 | buffer_free, | ||
| 96 | }; | ||
| 97 | |||
| 98 | BIO_METHOD *BIO_f_buffer() | ||
| 99 | { | ||
| 100 | return(&methods_buffer); | ||
| 101 | } | ||
| 102 | |||
| 103 | static int buffer_new(bi) | ||
| 104 | BIO *bi; | ||
| 105 | { | ||
| 106 | BIO_F_BUFFER_CTX *ctx; | ||
| 107 | |||
| 108 | ctx=(BIO_F_BUFFER_CTX *)Malloc(sizeof(BIO_F_BUFFER_CTX)); | ||
| 109 | if (ctx == NULL) return(0); | ||
| 110 | ctx->ibuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); | ||
| 111 | if (ctx->ibuf == NULL) { Free(ctx); return(0); } | ||
| 112 | ctx->obuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); | ||
| 113 | if (ctx->obuf == NULL) { Free(ctx->ibuf); Free(ctx); return(0); } | ||
| 114 | ctx->ibuf_size=DEFAULT_BUFFER_SIZE; | ||
| 115 | ctx->obuf_size=DEFAULT_BUFFER_SIZE; | ||
| 116 | ctx->ibuf_len=0; | ||
| 117 | ctx->ibuf_off=0; | ||
| 118 | ctx->obuf_len=0; | ||
| 119 | ctx->obuf_off=0; | ||
| 120 | |||
| 121 | bi->init=1; | ||
| 122 | bi->ptr=(char *)ctx; | ||
| 123 | bi->flags=0; | ||
| 124 | return(1); | ||
| 125 | } | ||
| 126 | |||
| 127 | static int buffer_free(a) | ||
| 128 | BIO *a; | ||
| 129 | { | ||
| 130 | BIO_F_BUFFER_CTX *b; | ||
| 131 | |||
| 132 | if (a == NULL) return(0); | ||
| 133 | b=(BIO_F_BUFFER_CTX *)a->ptr; | ||
| 134 | if (b->ibuf != NULL) Free(b->ibuf); | ||
| 135 | if (b->obuf != NULL) Free(b->obuf); | ||
| 136 | Free(a->ptr); | ||
| 137 | a->ptr=NULL; | ||
| 138 | a->init=0; | ||
| 139 | a->flags=0; | ||
| 140 | return(1); | ||
| 141 | } | ||
| 142 | |||
| 143 | static int buffer_read(b,out,outl) | ||
| 144 | BIO *b; | ||
| 145 | char *out; | ||
| 146 | int outl; | ||
| 147 | { | ||
| 148 | int i,num=0; | ||
| 149 | BIO_F_BUFFER_CTX *ctx; | ||
| 150 | |||
| 151 | if (out == NULL) return(0); | ||
| 152 | ctx=(BIO_F_BUFFER_CTX *)b->ptr; | ||
| 153 | |||
| 154 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
| 155 | num=0; | ||
| 156 | BIO_clear_retry_flags(b); | ||
| 157 | |||
| 158 | start: | ||
| 159 | i=ctx->ibuf_len; | ||
| 160 | /* If there is stuff left over, grab it */ | ||
| 161 | if (i != 0) | ||
| 162 | { | ||
| 163 | if (i > outl) i=outl; | ||
| 164 | memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i); | ||
| 165 | ctx->ibuf_off+=i; | ||
| 166 | ctx->ibuf_len-=i; | ||
| 167 | num+=i; | ||
| 168 | if (outl == i) return(num); | ||
| 169 | outl-=i; | ||
| 170 | out+=i; | ||
| 171 | } | ||
| 172 | |||
| 173 | /* We may have done a partial read. try to do more. | ||
| 174 | * We have nothing in the buffer. | ||
| 175 | * If we get an error and have read some data, just return it | ||
| 176 | * and let them retry to get the error again. | ||
| 177 | * copy direct to parent address space */ | ||
| 178 | if (outl > ctx->ibuf_size) | ||
| 179 | { | ||
| 180 | for (;;) | ||
| 181 | { | ||
| 182 | i=BIO_read(b->next_bio,out,outl); | ||
| 183 | if (i <= 0) | ||
| 184 | { | ||
| 185 | BIO_copy_next_retry(b); | ||
| 186 | if (i < 0) return((num > 0)?num:i); | ||
| 187 | if (i == 0) return(num); | ||
| 188 | } | ||
| 189 | num+=i; | ||
| 190 | if (outl == i) return(num); | ||
| 191 | out+=i; | ||
| 192 | outl-=i; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | /* else */ | ||
| 196 | |||
| 197 | /* we are going to be doing some buffering */ | ||
| 198 | i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size); | ||
| 199 | if (i <= 0) | ||
| 200 | { | ||
| 201 | BIO_copy_next_retry(b); | ||
| 202 | if (i < 0) return((num > 0)?num:i); | ||
| 203 | if (i == 0) return(num); | ||
| 204 | } | ||
| 205 | ctx->ibuf_off=0; | ||
| 206 | ctx->ibuf_len=i; | ||
| 207 | |||
| 208 | /* Lets re-read using ourselves :-) */ | ||
| 209 | goto start; | ||
| 210 | } | ||
| 211 | |||
| 212 | static int buffer_write(b,in,inl) | ||
| 213 | BIO *b; | ||
| 214 | char *in; | ||
| 215 | int inl; | ||
| 216 | { | ||
| 217 | int i,num=0; | ||
| 218 | BIO_F_BUFFER_CTX *ctx; | ||
| 219 | |||
| 220 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 221 | ctx=(BIO_F_BUFFER_CTX *)b->ptr; | ||
| 222 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
| 223 | |||
| 224 | BIO_clear_retry_flags(b); | ||
| 225 | start: | ||
| 226 | i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off); | ||
| 227 | /* add to buffer and return */ | ||
| 228 | if (i >= inl) | ||
| 229 | { | ||
| 230 | memcpy(&(ctx->obuf[ctx->obuf_len]),in,inl); | ||
| 231 | ctx->obuf_len+=inl; | ||
| 232 | return(num+inl); | ||
| 233 | } | ||
| 234 | /* else */ | ||
| 235 | /* stuff already in buffer, so add to it first, then flush */ | ||
| 236 | if (ctx->obuf_len != 0) | ||
| 237 | { | ||
| 238 | if (i > 0) /* lets fill it up if we can */ | ||
| 239 | { | ||
| 240 | memcpy(&(ctx->obuf[ctx->obuf_len]),in,i); | ||
| 241 | in+=i; | ||
| 242 | inl-=i; | ||
| 243 | num+=i; | ||
| 244 | ctx->obuf_len+=i; | ||
| 245 | } | ||
| 246 | /* we now have a full buffer needing flushing */ | ||
| 247 | for (;;) | ||
| 248 | { | ||
| 249 | i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]), | ||
| 250 | ctx->obuf_len); | ||
| 251 | if (i <= 0) | ||
| 252 | { | ||
| 253 | BIO_copy_next_retry(b); | ||
| 254 | |||
| 255 | if (i < 0) return((num > 0)?num:i); | ||
| 256 | if (i == 0) return(num); | ||
| 257 | } | ||
| 258 | ctx->obuf_off+=i; | ||
| 259 | ctx->obuf_len-=i; | ||
| 260 | if (ctx->obuf_len == 0) break; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | /* we only get here if the buffer has been flushed and we | ||
| 264 | * still have stuff to write */ | ||
| 265 | ctx->obuf_off=0; | ||
| 266 | |||
| 267 | /* we now have inl bytes to write */ | ||
| 268 | while (inl >= ctx->obuf_size) | ||
| 269 | { | ||
| 270 | i=BIO_write(b->next_bio,in,inl); | ||
| 271 | if (i <= 0) | ||
| 272 | { | ||
| 273 | BIO_copy_next_retry(b); | ||
| 274 | if (i < 0) return((num > 0)?num:i); | ||
| 275 | if (i == 0) return(num); | ||
| 276 | } | ||
| 277 | num+=i; | ||
| 278 | in+=i; | ||
| 279 | inl-=i; | ||
| 280 | if (inl == 0) return(num); | ||
| 281 | } | ||
| 282 | |||
| 283 | /* copy the rest into the buffer since we have only a small | ||
| 284 | * amount left */ | ||
| 285 | goto start; | ||
| 286 | } | ||
| 287 | |||
| 288 | static long buffer_ctrl(b,cmd,num,ptr) | ||
| 289 | BIO *b; | ||
| 290 | int cmd; | ||
| 291 | long num; | ||
| 292 | char *ptr; | ||
| 293 | { | ||
| 294 | BIO *dbio; | ||
| 295 | BIO_F_BUFFER_CTX *ctx; | ||
| 296 | long ret=1; | ||
| 297 | char *p1,*p2; | ||
| 298 | int r,i,*ip; | ||
| 299 | int ibs,obs; | ||
| 300 | |||
| 301 | ctx=(BIO_F_BUFFER_CTX *)b->ptr; | ||
| 302 | |||
| 303 | switch (cmd) | ||
| 304 | { | ||
| 305 | case BIO_CTRL_RESET: | ||
| 306 | ctx->ibuf_off=0; | ||
| 307 | ctx->ibuf_len=0; | ||
| 308 | ctx->obuf_off=0; | ||
| 309 | ctx->obuf_len=0; | ||
| 310 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 311 | break; | ||
| 312 | case BIO_CTRL_INFO: | ||
| 313 | ret=(long)ctx->obuf_len; | ||
| 314 | break; | ||
| 315 | case BIO_C_GET_BUFF_NUM_LINES: | ||
| 316 | ret=0; | ||
| 317 | p1=ctx->ibuf; | ||
| 318 | for (i=ctx->ibuf_off; i<ctx->ibuf_len; i++) | ||
| 319 | { | ||
| 320 | if (p1[i] == '\n') ret++; | ||
| 321 | } | ||
| 322 | break; | ||
| 323 | case BIO_CTRL_WPENDING: | ||
| 324 | ret=(long)ctx->obuf_len; | ||
| 325 | if (ret == 0) | ||
| 326 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 327 | break; | ||
| 328 | case BIO_CTRL_PENDING: | ||
| 329 | ret=(long)ctx->ibuf_len; | ||
| 330 | if (ret == 0) | ||
| 331 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 332 | break; | ||
| 333 | case BIO_C_SET_BUFF_READ_DATA: | ||
| 334 | if (num > ctx->ibuf_size) | ||
| 335 | { | ||
| 336 | p1=Malloc((int)num); | ||
| 337 | if (p1 == NULL) goto malloc_error; | ||
| 338 | if (ctx->ibuf != NULL) Free(ctx->ibuf); | ||
| 339 | ctx->ibuf=p1; | ||
| 340 | } | ||
| 341 | ctx->ibuf_off=0; | ||
| 342 | ctx->ibuf_len=(int)num; | ||
| 343 | memcpy(ctx->ibuf,ptr,(int)num); | ||
| 344 | ret=1; | ||
| 345 | break; | ||
| 346 | case BIO_C_SET_BUFF_SIZE: | ||
| 347 | if (ptr != NULL) | ||
| 348 | { | ||
| 349 | ip=(int *)ptr; | ||
| 350 | if (*ip == 0) | ||
| 351 | { | ||
| 352 | ibs=(int)num; | ||
| 353 | obs=ctx->obuf_size; | ||
| 354 | } | ||
| 355 | else /* if (*ip == 1) */ | ||
| 356 | { | ||
| 357 | ibs=ctx->ibuf_size; | ||
| 358 | obs=(int)num; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | else | ||
| 362 | { | ||
| 363 | ibs=(int)num; | ||
| 364 | obs=(int)num; | ||
| 365 | } | ||
| 366 | p1=ctx->ibuf; | ||
| 367 | p2=ctx->obuf; | ||
| 368 | if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) | ||
| 369 | { | ||
| 370 | p1=(char *)Malloc((int)num); | ||
| 371 | if (p1 == NULL) goto malloc_error; | ||
| 372 | } | ||
| 373 | if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) | ||
| 374 | { | ||
| 375 | p2=(char *)Malloc((int)num); | ||
| 376 | if (p2 == NULL) | ||
| 377 | { | ||
| 378 | if (p1 != ctx->ibuf) Free(p1); | ||
| 379 | goto malloc_error; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | if (ctx->ibuf != p1) | ||
| 383 | { | ||
| 384 | Free(ctx->ibuf); | ||
| 385 | ctx->ibuf=p1; | ||
| 386 | ctx->ibuf_off=0; | ||
| 387 | ctx->ibuf_len=0; | ||
| 388 | ctx->ibuf_size=ibs; | ||
| 389 | } | ||
| 390 | if (ctx->obuf != p2) | ||
| 391 | { | ||
| 392 | Free(ctx->obuf); | ||
| 393 | ctx->obuf=p2; | ||
| 394 | ctx->obuf_off=0; | ||
| 395 | ctx->obuf_len=0; | ||
| 396 | ctx->obuf_size=obs; | ||
| 397 | } | ||
| 398 | break; | ||
| 399 | case BIO_C_DO_STATE_MACHINE: | ||
| 400 | BIO_clear_retry_flags(b); | ||
| 401 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 402 | BIO_copy_next_retry(b); | ||
| 403 | break; | ||
| 404 | |||
| 405 | case BIO_CTRL_FLUSH: | ||
| 406 | if (ctx->obuf_len <= 0) | ||
| 407 | { | ||
| 408 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 409 | break; | ||
| 410 | } | ||
| 411 | |||
| 412 | for (;;) | ||
| 413 | { | ||
| 414 | BIO_clear_retry_flags(b); | ||
| 415 | if (ctx->obuf_len > ctx->obuf_off) | ||
| 416 | { | ||
| 417 | r=BIO_write(b->next_bio, | ||
| 418 | &(ctx->obuf[ctx->obuf_off]), | ||
| 419 | ctx->obuf_len-ctx->obuf_off); | ||
| 420 | #if 0 | ||
| 421 | fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_off,r); | ||
| 422 | #endif | ||
| 423 | BIO_copy_next_retry(b); | ||
| 424 | if (r <= 0) return((long)r); | ||
| 425 | ctx->obuf_off+=r; | ||
| 426 | } | ||
| 427 | else | ||
| 428 | { | ||
| 429 | ctx->obuf_len=0; | ||
| 430 | ctx->obuf_off=0; | ||
| 431 | ret=1; | ||
| 432 | break; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | break; | ||
| 436 | case BIO_CTRL_DUP: | ||
| 437 | dbio=(BIO *)ptr; | ||
| 438 | if ( !BIO_set_read_buffer_size(dbio,ctx->ibuf_size) || | ||
| 439 | !BIO_set_write_buffer_size(dbio,ctx->obuf_size)) | ||
| 440 | ret=0; | ||
| 441 | break; | ||
| 442 | default: | ||
| 443 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 444 | break; | ||
| 445 | } | ||
| 446 | return(ret); | ||
| 447 | malloc_error: | ||
| 448 | BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE); | ||
| 449 | return(0); | ||
| 450 | } | ||
| 451 | |||
| 452 | static int buffer_gets(b,buf,size) | ||
| 453 | BIO *b; | ||
| 454 | char *buf; | ||
| 455 | int size; | ||
| 456 | { | ||
| 457 | BIO_F_BUFFER_CTX *ctx; | ||
| 458 | int num=0,i,flag; | ||
| 459 | char *p; | ||
| 460 | |||
| 461 | ctx=(BIO_F_BUFFER_CTX *)b->ptr; | ||
| 462 | size--; /* reserve space for a '\0' */ | ||
| 463 | BIO_clear_retry_flags(b); | ||
| 464 | |||
| 465 | for (;;) | ||
| 466 | { | ||
| 467 | if (ctx->ibuf_len > 0) | ||
| 468 | { | ||
| 469 | p= &(ctx->ibuf[ctx->ibuf_off]); | ||
| 470 | flag=0; | ||
| 471 | for (i=0; (i<ctx->ibuf_len) && (i<size); i++) | ||
| 472 | { | ||
| 473 | *(buf++)=p[i]; | ||
| 474 | if (p[i] == '\n') | ||
| 475 | { | ||
| 476 | flag=1; | ||
| 477 | i++; | ||
| 478 | break; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | num+=i; | ||
| 482 | size-=i; | ||
| 483 | ctx->ibuf_len-=i; | ||
| 484 | ctx->ibuf_off+=i; | ||
| 485 | if ((flag) || (i == size)) | ||
| 486 | { | ||
| 487 | *buf='\0'; | ||
| 488 | return(num); | ||
| 489 | } | ||
| 490 | } | ||
| 491 | else /* read another chunk */ | ||
| 492 | { | ||
| 493 | i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size); | ||
| 494 | if (i <= 0) | ||
| 495 | { | ||
| 496 | BIO_copy_next_retry(b); | ||
| 497 | if (i < 0) return((num > 0)?num:i); | ||
| 498 | if (i == 0) return(num); | ||
| 499 | } | ||
| 500 | ctx->ibuf_len=i; | ||
| 501 | ctx->ibuf_off=0; | ||
| 502 | } | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | static int buffer_puts(b,str) | ||
| 507 | BIO *b; | ||
| 508 | char *str; | ||
| 509 | { | ||
| 510 | return(BIO_write(b,str,strlen(str))); | ||
| 511 | } | ||
| 512 | |||
diff --git a/src/lib/libcrypto/bio/bf_nbio.c b/src/lib/libcrypto/bio/bf_nbio.c new file mode 100644 index 0000000000..034b3024df --- /dev/null +++ b/src/lib/libcrypto/bio/bf_nbio.c | |||
| @@ -0,0 +1,268 @@ | |||
| 1 | /* crypto/bio/bf_nbio.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include "rand.h" | ||
| 63 | #include "bio.h" | ||
| 64 | #include "evp.h" | ||
| 65 | |||
| 66 | /* BIO_put and BIO_get both add to the digest, | ||
| 67 | * BIO_gets returns the digest */ | ||
| 68 | |||
| 69 | #ifndef NOPROTO | ||
| 70 | static int nbiof_write(BIO *h,char *buf,int num); | ||
| 71 | static int nbiof_read(BIO *h,char *buf,int size); | ||
| 72 | static int nbiof_puts(BIO *h,char *str); | ||
| 73 | static int nbiof_gets(BIO *h,char *str,int size); | ||
| 74 | static long nbiof_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 75 | static int nbiof_new(BIO *h); | ||
| 76 | static int nbiof_free(BIO *data); | ||
| 77 | #else | ||
| 78 | static int nbiof_write(); | ||
| 79 | static int nbiof_read(); | ||
| 80 | static int nbiof_puts(); | ||
| 81 | static int nbiof_gets(); | ||
| 82 | static long nbiof_ctrl(); | ||
| 83 | static int nbiof_new(); | ||
| 84 | static int nbiof_free(); | ||
| 85 | #endif | ||
| 86 | |||
| 87 | typedef struct nbio_test_st | ||
| 88 | { | ||
| 89 | /* only set if we sent a 'should retry' error */ | ||
| 90 | int lrn; | ||
| 91 | int lwn; | ||
| 92 | } NBIO_TEST; | ||
| 93 | |||
| 94 | static BIO_METHOD methods_nbiof= | ||
| 95 | { | ||
| 96 | BIO_TYPE_NBIO_TEST, | ||
| 97 | "non-blocking IO test filter", | ||
| 98 | nbiof_write, | ||
| 99 | nbiof_read, | ||
| 100 | nbiof_puts, | ||
| 101 | nbiof_gets, | ||
| 102 | nbiof_ctrl, | ||
| 103 | nbiof_new, | ||
| 104 | nbiof_free, | ||
| 105 | }; | ||
| 106 | |||
| 107 | BIO_METHOD *BIO_f_nbio_test() | ||
| 108 | { | ||
| 109 | return(&methods_nbiof); | ||
| 110 | } | ||
| 111 | |||
| 112 | static int nbiof_new(bi) | ||
| 113 | BIO *bi; | ||
| 114 | { | ||
| 115 | NBIO_TEST *nt; | ||
| 116 | |||
| 117 | nt=(NBIO_TEST *)Malloc(sizeof(NBIO_TEST)); | ||
| 118 | nt->lrn= -1; | ||
| 119 | nt->lwn= -1; | ||
| 120 | bi->ptr=(char *)nt; | ||
| 121 | bi->init=1; | ||
| 122 | bi->flags=0; | ||
| 123 | return(1); | ||
| 124 | } | ||
| 125 | |||
| 126 | static int nbiof_free(a) | ||
| 127 | BIO *a; | ||
| 128 | { | ||
| 129 | if (a == NULL) return(0); | ||
| 130 | if (a->ptr != NULL) | ||
| 131 | Free(a->ptr); | ||
| 132 | a->ptr=NULL; | ||
| 133 | a->init=0; | ||
| 134 | a->flags=0; | ||
| 135 | return(1); | ||
| 136 | } | ||
| 137 | |||
| 138 | static int nbiof_read(b,out,outl) | ||
| 139 | BIO *b; | ||
| 140 | char *out; | ||
| 141 | int outl; | ||
| 142 | { | ||
| 143 | NBIO_TEST *nt; | ||
| 144 | int ret=0; | ||
| 145 | #if 0 | ||
| 146 | int num; | ||
| 147 | unsigned char n; | ||
| 148 | #endif | ||
| 149 | |||
| 150 | if (out == NULL) return(0); | ||
| 151 | if (b->next_bio == NULL) return(0); | ||
| 152 | nt=(NBIO_TEST *)b->ptr; | ||
| 153 | |||
| 154 | BIO_clear_retry_flags(b); | ||
| 155 | #if 0 | ||
| 156 | RAND_bytes(&n,1); | ||
| 157 | num=(n&0x07); | ||
| 158 | |||
| 159 | if (outl > num) outl=num; | ||
| 160 | |||
| 161 | if (num == 0) | ||
| 162 | { | ||
| 163 | ret= -1; | ||
| 164 | BIO_set_retry_read(b); | ||
| 165 | } | ||
| 166 | else | ||
| 167 | #endif | ||
| 168 | { | ||
| 169 | ret=BIO_read(b->next_bio,out,outl); | ||
| 170 | if (ret < 0) | ||
| 171 | BIO_copy_next_retry(b); | ||
| 172 | } | ||
| 173 | return(ret); | ||
| 174 | } | ||
| 175 | |||
| 176 | static int nbiof_write(b,in,inl) | ||
| 177 | BIO *b; | ||
| 178 | char *in; | ||
| 179 | int inl; | ||
| 180 | { | ||
| 181 | NBIO_TEST *nt; | ||
| 182 | int ret=0; | ||
| 183 | int num; | ||
| 184 | unsigned char n; | ||
| 185 | |||
| 186 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 187 | if (b->next_bio == NULL) return(0); | ||
| 188 | nt=(NBIO_TEST *)b->ptr; | ||
| 189 | |||
| 190 | BIO_clear_retry_flags(b); | ||
| 191 | |||
| 192 | #if 1 | ||
| 193 | if (nt->lwn > 0) | ||
| 194 | { | ||
| 195 | num=nt->lwn; | ||
| 196 | nt->lwn=0; | ||
| 197 | } | ||
| 198 | else | ||
| 199 | { | ||
| 200 | RAND_bytes(&n,1); | ||
| 201 | num=(n&7); | ||
| 202 | } | ||
| 203 | |||
| 204 | if (inl > num) inl=num; | ||
| 205 | |||
| 206 | if (num == 0) | ||
| 207 | { | ||
| 208 | ret= -1; | ||
| 209 | BIO_set_retry_write(b); | ||
| 210 | } | ||
| 211 | else | ||
| 212 | #endif | ||
| 213 | { | ||
| 214 | ret=BIO_write(b->next_bio,in,inl); | ||
| 215 | if (ret < 0) | ||
| 216 | { | ||
| 217 | BIO_copy_next_retry(b); | ||
| 218 | nt->lwn=inl; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | return(ret); | ||
| 222 | } | ||
| 223 | |||
| 224 | static long nbiof_ctrl(b,cmd,num,ptr) | ||
| 225 | BIO *b; | ||
| 226 | int cmd; | ||
| 227 | long num; | ||
| 228 | char *ptr; | ||
| 229 | { | ||
| 230 | long ret; | ||
| 231 | |||
| 232 | if (b->next_bio == NULL) return(0); | ||
| 233 | switch (cmd) | ||
| 234 | { | ||
| 235 | case BIO_C_DO_STATE_MACHINE: | ||
| 236 | BIO_clear_retry_flags(b); | ||
| 237 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 238 | BIO_copy_next_retry(b); | ||
| 239 | break; | ||
| 240 | case BIO_CTRL_DUP: | ||
| 241 | ret=0L; | ||
| 242 | break; | ||
| 243 | default: | ||
| 244 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 245 | break; | ||
| 246 | } | ||
| 247 | return(ret); | ||
| 248 | } | ||
| 249 | |||
| 250 | static int nbiof_gets(bp,buf,size) | ||
| 251 | BIO *bp; | ||
| 252 | char *buf; | ||
| 253 | int size; | ||
| 254 | { | ||
| 255 | if (bp->next_bio == NULL) return(0); | ||
| 256 | return(BIO_gets(bp->next_bio,buf,size)); | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | static int nbiof_puts(bp,str) | ||
| 261 | BIO *bp; | ||
| 262 | char *str; | ||
| 263 | { | ||
| 264 | if (bp->next_bio == NULL) return(0); | ||
| 265 | return(BIO_puts(bp->next_bio,str)); | ||
| 266 | } | ||
| 267 | |||
| 268 | |||
diff --git a/src/lib/libcrypto/bio/bf_null.c b/src/lib/libcrypto/bio/bf_null.c new file mode 100644 index 0000000000..a47a65741a --- /dev/null +++ b/src/lib/libcrypto/bio/bf_null.c | |||
| @@ -0,0 +1,196 @@ | |||
| 1 | /* crypto/bio/bf_null.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include "bio.h" | ||
| 63 | #include "evp.h" | ||
| 64 | |||
| 65 | /* BIO_put and BIO_get both add to the digest, | ||
| 66 | * BIO_gets returns the digest */ | ||
| 67 | |||
| 68 | #ifndef NOPROTO | ||
| 69 | static int nullf_write(BIO *h,char *buf,int num); | ||
| 70 | static int nullf_read(BIO *h,char *buf,int size); | ||
| 71 | static int nullf_puts(BIO *h,char *str); | ||
| 72 | static int nullf_gets(BIO *h,char *str,int size); | ||
| 73 | static long nullf_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 74 | static int nullf_new(BIO *h); | ||
| 75 | static int nullf_free(BIO *data); | ||
| 76 | #else | ||
| 77 | static int nullf_write(); | ||
| 78 | static int nullf_read(); | ||
| 79 | static int nullf_puts(); | ||
| 80 | static int nullf_gets(); | ||
| 81 | static long nullf_ctrl(); | ||
| 82 | static int nullf_new(); | ||
| 83 | static int nullf_free(); | ||
| 84 | #endif | ||
| 85 | |||
| 86 | static BIO_METHOD methods_nullf= | ||
| 87 | { | ||
| 88 | BIO_TYPE_NULL_FILTER, | ||
| 89 | "NULL filter", | ||
| 90 | nullf_write, | ||
| 91 | nullf_read, | ||
| 92 | nullf_puts, | ||
| 93 | nullf_gets, | ||
| 94 | nullf_ctrl, | ||
| 95 | nullf_new, | ||
| 96 | nullf_free, | ||
| 97 | }; | ||
| 98 | |||
| 99 | BIO_METHOD *BIO_f_null() | ||
| 100 | { | ||
| 101 | return(&methods_nullf); | ||
| 102 | } | ||
| 103 | |||
| 104 | static int nullf_new(bi) | ||
| 105 | BIO *bi; | ||
| 106 | { | ||
| 107 | bi->init=1; | ||
| 108 | bi->ptr=NULL; | ||
| 109 | bi->flags=0; | ||
| 110 | return(1); | ||
| 111 | } | ||
| 112 | |||
| 113 | static int nullf_free(a) | ||
| 114 | BIO *a; | ||
| 115 | { | ||
| 116 | if (a == NULL) return(0); | ||
| 117 | /* a->ptr=NULL; | ||
| 118 | a->init=0; | ||
| 119 | a->flags=0;*/ | ||
| 120 | return(1); | ||
| 121 | } | ||
| 122 | |||
| 123 | static int nullf_read(b,out,outl) | ||
| 124 | BIO *b; | ||
| 125 | char *out; | ||
| 126 | int outl; | ||
| 127 | { | ||
| 128 | int ret=0; | ||
| 129 | |||
| 130 | if (out == NULL) return(0); | ||
| 131 | if (b->next_bio == NULL) return(0); | ||
| 132 | ret=BIO_read(b->next_bio,out,outl); | ||
| 133 | BIO_clear_retry_flags(b); | ||
| 134 | BIO_copy_next_retry(b); | ||
| 135 | return(ret); | ||
| 136 | } | ||
| 137 | |||
| 138 | static int nullf_write(b,in,inl) | ||
| 139 | BIO *b; | ||
| 140 | char *in; | ||
| 141 | int inl; | ||
| 142 | { | ||
| 143 | int ret=0; | ||
| 144 | |||
| 145 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 146 | if (b->next_bio == NULL) return(0); | ||
| 147 | ret=BIO_write(b->next_bio,in,inl); | ||
| 148 | BIO_clear_retry_flags(b); | ||
| 149 | BIO_copy_next_retry(b); | ||
| 150 | return(ret); | ||
| 151 | } | ||
| 152 | |||
| 153 | static long nullf_ctrl(b,cmd,num,ptr) | ||
| 154 | BIO *b; | ||
| 155 | int cmd; | ||
| 156 | long num; | ||
| 157 | char *ptr; | ||
| 158 | { | ||
| 159 | long ret; | ||
| 160 | |||
| 161 | if (b->next_bio == NULL) return(0); | ||
| 162 | switch(cmd) | ||
| 163 | { | ||
| 164 | case BIO_C_DO_STATE_MACHINE: | ||
| 165 | BIO_clear_retry_flags(b); | ||
| 166 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 167 | BIO_copy_next_retry(b); | ||
| 168 | break; | ||
| 169 | case BIO_CTRL_DUP: | ||
| 170 | ret=0L; | ||
| 171 | break; | ||
| 172 | default: | ||
| 173 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 174 | } | ||
| 175 | return(ret); | ||
| 176 | } | ||
| 177 | |||
| 178 | static int nullf_gets(bp,buf,size) | ||
| 179 | BIO *bp; | ||
| 180 | char *buf; | ||
| 181 | int size; | ||
| 182 | { | ||
| 183 | if (bp->next_bio == NULL) return(0); | ||
| 184 | return(BIO_gets(bp->next_bio,buf,size)); | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | static int nullf_puts(bp,str) | ||
| 189 | BIO *bp; | ||
| 190 | char *str; | ||
| 191 | { | ||
| 192 | if (bp->next_bio == NULL) return(0); | ||
| 193 | return(BIO_puts(bp->next_bio,str)); | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
diff --git a/src/lib/libcrypto/bio/bio.h b/src/lib/libcrypto/bio/bio.h new file mode 100644 index 0000000000..300b330e00 --- /dev/null +++ b/src/lib/libcrypto/bio/bio.h | |||
| @@ -0,0 +1,688 @@ | |||
| 1 | /* crypto/bio/bio.h */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #ifndef HEADER_BIO_H | ||
| 60 | #define HEADER_BIO_H | ||
| 61 | |||
| 62 | #ifdef __cplusplus | ||
| 63 | extern "C" { | ||
| 64 | #endif | ||
| 65 | |||
| 66 | #include "crypto.h" | ||
| 67 | |||
| 68 | /* These are the 'types' of BIOs */ | ||
| 69 | #define BIO_TYPE_NONE 0 | ||
| 70 | #define BIO_TYPE_MEM (1|0x0400) | ||
| 71 | #define BIO_TYPE_FILE (2|0x0400) | ||
| 72 | |||
| 73 | #define BIO_TYPE_FD (4|0x0400|0x0100) | ||
| 74 | #define BIO_TYPE_SOCKET (5|0x0400|0x0100) | ||
| 75 | #define BIO_TYPE_NULL (6|0x0400) | ||
| 76 | #define BIO_TYPE_SSL (7|0x0200) | ||
| 77 | #define BIO_TYPE_MD (8|0x0200) /* pasive filter */ | ||
| 78 | #define BIO_TYPE_BUFFER (9|0x0200) /* filter */ | ||
| 79 | #define BIO_TYPE_CIPHER (10|0x0200) /* filter */ | ||
| 80 | #define BIO_TYPE_BASE64 (11|0x0200) /* filter */ | ||
| 81 | #define BIO_TYPE_CONNECT (12|0x0400|0x0100) /* socket - connect */ | ||
| 82 | #define BIO_TYPE_ACCEPT (13|0x0400|0x0100) /* socket for accept */ | ||
| 83 | #define BIO_TYPE_PROXY_CLIENT (14|0x0200) /* client proxy BIO */ | ||
| 84 | #define BIO_TYPE_PROXY_SERVER (15|0x0200) /* server proxy BIO */ | ||
| 85 | #define BIO_TYPE_NBIO_TEST (16|0x0200) /* server proxy BIO */ | ||
| 86 | #define BIO_TYPE_NULL_FILTER (17|0x0200) | ||
| 87 | |||
| 88 | #define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */ | ||
| 89 | #define BIO_TYPE_FILTER 0x0200 | ||
| 90 | #define BIO_TYPE_SOURCE_SINK 0x0400 | ||
| 91 | |||
| 92 | /* BIO_FILENAME_READ|BIO_CLOSE to open or close on free. | ||
| 93 | * BIO_set_fp(in,stdin,BIO_NOCLOSE); */ | ||
| 94 | #define BIO_NOCLOSE 0x00 | ||
| 95 | #define BIO_CLOSE 0x01 | ||
| 96 | |||
| 97 | /* These are used in the following macros and are passed to | ||
| 98 | * BIO_ctrl() */ | ||
| 99 | #define BIO_CTRL_RESET 1 /* opt - rewind/zero etc */ | ||
| 100 | #define BIO_CTRL_EOF 2 /* opt - are we at the eof */ | ||
| 101 | #define BIO_CTRL_INFO 3 /* opt - extra tit-bits */ | ||
| 102 | #define BIO_CTRL_SET 4 /* man - set the 'IO' type */ | ||
| 103 | #define BIO_CTRL_GET 5 /* man - get the 'IO' type */ | ||
| 104 | #define BIO_CTRL_PUSH 6 /* opt - internal, used to signify change */ | ||
| 105 | #define BIO_CTRL_POP 7 /* opt - internal, used to signify change */ | ||
| 106 | #define BIO_CTRL_GET_CLOSE 8 /* man - set the 'close' on free */ | ||
| 107 | #define BIO_CTRL_SET_CLOSE 9 /* man - set the 'close' on free */ | ||
| 108 | #define BIO_CTRL_PENDING 10 /* opt - is their more data buffered */ | ||
| 109 | #define BIO_CTRL_FLUSH 11 /* opt - 'flush' buffered output */ | ||
| 110 | #define BIO_CTRL_DUP 12 /* man - extra stuff for 'duped' BIO */ | ||
| 111 | #define BIO_CTRL_WPENDING 13 /* opt - number of bytes still to write */ | ||
| 112 | /* callback is int cb(BIO *bio,state,ret); */ | ||
| 113 | #define BIO_CTRL_SET_CALLBACK 14 /* opt - set callback function */ | ||
| 114 | #define BIO_CTRL_GET_CALLBACK 15 /* opt - set callback function */ | ||
| 115 | |||
| 116 | #define BIO_CTRL_SET_FILENAME 30 /* BIO_s_file special */ | ||
| 117 | |||
| 118 | /* modifiers */ | ||
| 119 | #define BIO_FP_READ 0x02 | ||
| 120 | #define BIO_FP_WRITE 0x04 | ||
| 121 | #define BIO_FP_APPEND 0x08 | ||
| 122 | #define BIO_FP_TEXT 0x10 | ||
| 123 | |||
| 124 | #define BIO_FLAGS_READ 0x01 | ||
| 125 | #define BIO_FLAGS_WRITE 0x02 | ||
| 126 | #define BIO_FLAGS_IO_SPECIAL 0x04 | ||
| 127 | #define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL) | ||
| 128 | #define BIO_FLAGS_SHOULD_RETRY 0x08 | ||
| 129 | |||
| 130 | /* Used in BIO_gethostbyname() */ | ||
| 131 | #define BIO_GHBN_CTRL_HITS 1 | ||
| 132 | #define BIO_GHBN_CTRL_MISSES 2 | ||
| 133 | #define BIO_GHBN_CTRL_CACHE_SIZE 3 | ||
| 134 | #define BIO_GHBN_CTRL_GET_ENTRY 4 | ||
| 135 | #define BIO_GHBN_CTRL_FLUSH 5 | ||
| 136 | |||
| 137 | /* Mostly used in the SSL BIO */ | ||
| 138 | /* Not used anymore | ||
| 139 | * #define BIO_FLAGS_PROTOCOL_DELAYED_READ 0x10 | ||
| 140 | * #define BIO_FLAGS_PROTOCOL_DELAYED_WRITE 0x20 | ||
| 141 | * #define BIO_FLAGS_PROTOCOL_STARTUP 0x40 | ||
| 142 | */ | ||
| 143 | |||
| 144 | #define BIO_FLAGS_BASE64_NO_NL 0x100 | ||
| 145 | |||
| 146 | #define BIO_set_flags(b,f) ((b)->flags|=(f)) | ||
| 147 | #define BIO_get_flags(b) ((b)->flags) | ||
| 148 | #define BIO_set_retry_special(b) \ | ||
| 149 | ((b)->flags|=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY)) | ||
| 150 | #define BIO_set_retry_read(b) \ | ||
| 151 | ((b)->flags|=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY)) | ||
| 152 | #define BIO_set_retry_write(b) \ | ||
| 153 | ((b)->flags|=(BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY)) | ||
| 154 | |||
| 155 | /* These are normally used internally in BIOs */ | ||
| 156 | #define BIO_clear_flags(b,f) ((b)->flags&= ~(f)) | ||
| 157 | #define BIO_clear_retry_flags(b) \ | ||
| 158 | ((b)->flags&= ~(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) | ||
| 159 | #define BIO_get_retry_flags(b) \ | ||
| 160 | ((b)->flags&(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) | ||
| 161 | |||
| 162 | /* These shouldbe used by the application to tell why we should retry */ | ||
| 163 | #define BIO_should_read(a) ((a)->flags & BIO_FLAGS_READ) | ||
| 164 | #define BIO_should_write(a) ((a)->flags & BIO_FLAGS_WRITE) | ||
| 165 | #define BIO_should_io_special(a) ((a)->flags & BIO_FLAGS_IO_SPECIAL) | ||
| 166 | #define BIO_retry_type(a) ((a)->flags & BIO_FLAGS_RWS) | ||
| 167 | #define BIO_should_retry(a) ((a)->flags & BIO_FLAGS_SHOULD_RETRY) | ||
| 168 | |||
| 169 | /* The next two are used in conjunction with the | ||
| 170 | * BIO_should_io_special() condition. After this returns true, | ||
| 171 | * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO | ||
| 172 | * stack and return the 'reason' for the special and the offending BIO. | ||
| 173 | * Given a BIO, BIO_get_retry_reason(bio) will return the code. */ | ||
| 174 | /* Returned from the SSL bio when the certificate retrieval code had an error */ | ||
| 175 | #define BIO_RR_SSL_X509_LOOKUP 0x01 | ||
| 176 | /* Returned from the connect BIO when a connect would have blocked */ | ||
| 177 | #define BIO_RR_CONNECT 0x02 | ||
| 178 | |||
| 179 | /* These are passed by the BIO callback */ | ||
| 180 | #define BIO_CB_FREE 0x01 | ||
| 181 | #define BIO_CB_READ 0x02 | ||
| 182 | #define BIO_CB_WRITE 0x03 | ||
| 183 | #define BIO_CB_PUTS 0x04 | ||
| 184 | #define BIO_CB_GETS 0x05 | ||
| 185 | #define BIO_CB_CTRL 0x06 | ||
| 186 | |||
| 187 | /* The callback is called before and after the underling operation, | ||
| 188 | * The BIO_CB_RETURN flag indicates if it is after the call */ | ||
| 189 | #define BIO_CB_RETURN 0x80 | ||
| 190 | #define BIO_CB_return(a) ((a)|BIO_CB_RETURN)) | ||
| 191 | #define BIO_cb_pre(a) (!((a)&BIO_CB_RETURN)) | ||
| 192 | #define BIO_cb_post(a) ((a)&BIO_CB_RETURN) | ||
| 193 | |||
| 194 | #define BIO_set_callback(b,cb) ((b)->callback=(cb)) | ||
| 195 | #define BIO_set_callback_arg(b,arg) ((b)->cb_arg=(char *)(arg)) | ||
| 196 | #define BIO_get_callback_arg(b) ((b)->cb_arg) | ||
| 197 | #define BIO_get_callback(b) ((b)->callback) | ||
| 198 | #define BIO_method_name(b) ((b)->method->name) | ||
| 199 | #define BIO_method_type(b) ((b)->method->type) | ||
| 200 | |||
| 201 | #ifndef WIN16 | ||
| 202 | typedef struct bio_method_st | ||
| 203 | { | ||
| 204 | int type; | ||
| 205 | char *name; | ||
| 206 | int (*bwrite)(); | ||
| 207 | int (*bread)(); | ||
| 208 | int (*bputs)(); | ||
| 209 | int (*bgets)(); | ||
| 210 | long (*ctrl)(); | ||
| 211 | int (*create)(); | ||
| 212 | int (*destroy)(); | ||
| 213 | } BIO_METHOD; | ||
| 214 | #else | ||
| 215 | typedef struct bio_method_st | ||
| 216 | { | ||
| 217 | int type; | ||
| 218 | char *name; | ||
| 219 | int (_far *bwrite)(); | ||
| 220 | int (_far *bread)(); | ||
| 221 | int (_far *bputs)(); | ||
| 222 | int (_far *bgets)(); | ||
| 223 | long (_far *ctrl)(); | ||
| 224 | int (_far *create)(); | ||
| 225 | int (_far *destroy)(); | ||
| 226 | } BIO_METHOD; | ||
| 227 | #endif | ||
| 228 | |||
| 229 | typedef struct bio_st | ||
| 230 | { | ||
| 231 | BIO_METHOD *method; | ||
| 232 | #ifndef NOPROTO | ||
| 233 | /* bio, mode, argp, argi, argl, ret */ | ||
| 234 | long (*callback)(struct bio_st *,int,char *,int, long,long); | ||
| 235 | #else | ||
| 236 | long (*callback)(); | ||
| 237 | #endif | ||
| 238 | char *cb_arg; /* first argument for the callback */ | ||
| 239 | |||
| 240 | int init; | ||
| 241 | int shutdown; | ||
| 242 | int flags; /* extra storage */ | ||
| 243 | int retry_reason; | ||
| 244 | int num; | ||
| 245 | char *ptr; | ||
| 246 | struct bio_st *next_bio; /* used by filter BIOs */ | ||
| 247 | struct bio_st *prev_bio; /* used by filter BIOs */ | ||
| 248 | int references; | ||
| 249 | unsigned long num_read; | ||
| 250 | unsigned long num_write; | ||
| 251 | |||
| 252 | CRYPTO_EX_DATA ex_data; | ||
| 253 | } BIO; | ||
| 254 | |||
| 255 | typedef struct bio_f_buffer_ctx_struct | ||
| 256 | { | ||
| 257 | /* BIO *bio; */ /* this is now in the BIO struct */ | ||
| 258 | int ibuf_size; /* how big is the input buffer */ | ||
| 259 | int obuf_size; /* how big is the output buffer */ | ||
| 260 | |||
| 261 | char *ibuf; /* the char array */ | ||
| 262 | int ibuf_len; /* how many bytes are in it */ | ||
| 263 | int ibuf_off; /* write/read offset */ | ||
| 264 | |||
| 265 | char *obuf; /* the char array */ | ||
| 266 | int obuf_len; /* how many bytes are in it */ | ||
| 267 | int obuf_off; /* write/read offset */ | ||
| 268 | } BIO_F_BUFFER_CTX; | ||
| 269 | |||
| 270 | /* connect BIO stuff */ | ||
| 271 | #define BIO_CONN_S_BEFORE 1 | ||
| 272 | #define BIO_CONN_S_GET_IP 2 | ||
| 273 | #define BIO_CONN_S_GET_PORT 3 | ||
| 274 | #define BIO_CONN_S_CREATE_SOCKET 4 | ||
| 275 | #define BIO_CONN_S_CONNECT 5 | ||
| 276 | #define BIO_CONN_S_OK 6 | ||
| 277 | #define BIO_CONN_S_BLOCKED_CONNECT 7 | ||
| 278 | #define BIO_CONN_S_NBIO 8 | ||
| 279 | #define BIO_CONN_get_param_hostname BIO_ctrl | ||
| 280 | |||
| 281 | #define BIO_number_read(b) ((b)->num_read) | ||
| 282 | #define BIO_number_written(b) ((b)->num_write) | ||
| 283 | |||
| 284 | #define BIO_C_SET_CONNECT 100 | ||
| 285 | #define BIO_C_DO_STATE_MACHINE 101 | ||
| 286 | #define BIO_C_SET_NBIO 102 | ||
| 287 | #define BIO_C_SET_PROXY_PARAM 103 | ||
| 288 | #define BIO_C_SET_FD 104 | ||
| 289 | #define BIO_C_GET_FD 105 | ||
| 290 | #define BIO_C_SET_FILE_PTR 106 | ||
| 291 | #define BIO_C_GET_FILE_PTR 107 | ||
| 292 | #define BIO_C_SET_FILENAME 108 | ||
| 293 | #define BIO_C_SET_SSL 109 | ||
| 294 | #define BIO_C_GET_SSL 110 | ||
| 295 | #define BIO_C_SET_MD 111 | ||
| 296 | #define BIO_C_GET_MD 112 | ||
| 297 | #define BIO_C_GET_CIPHER_STATUS 113 | ||
| 298 | #define BIO_C_SET_BUF_MEM 114 | ||
| 299 | #define BIO_C_GET_BUF_MEM_PTR 115 | ||
| 300 | #define BIO_C_GET_BUFF_NUM_LINES 116 | ||
| 301 | #define BIO_C_SET_BUFF_SIZE 117 | ||
| 302 | #define BIO_C_SET_ACCEPT 118 | ||
| 303 | #define BIO_C_SSL_MODE 119 | ||
| 304 | #define BIO_C_GET_MD_CTX 120 | ||
| 305 | #define BIO_C_GET_PROXY_PARAM 121 | ||
| 306 | #define BIO_C_SET_BUFF_READ_DATA 122 /* data to read first */ | ||
| 307 | #define BIO_C_GET_CONNECT 123 | ||
| 308 | #define BIO_C_GET_ACCEPT 124 | ||
| 309 | #define BIO_C_SET_SSL_RENEGOTIATE_BYTES 125 | ||
| 310 | #define BIO_C_GET_SSL_NUM_RENEGOTIATES 126 | ||
| 311 | #define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT 127 | ||
| 312 | |||
| 313 | #define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,(char *)arg) | ||
| 314 | #define BIO_get_app_data(s) BIO_get_ex_data(s,0) | ||
| 315 | |||
| 316 | int BIO_get_ex_num(BIO *bio); | ||
| 317 | int BIO_set_ex_data(BIO *bio,int idx,char *data); | ||
| 318 | char *BIO_get_ex_data(BIO *bio,int idx); | ||
| 319 | void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)()); | ||
| 320 | int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(), | ||
| 321 | int (*dup_func)(), void (*free_func)()); | ||
| 322 | |||
| 323 | /* BIO_s_connect_socket() */ | ||
| 324 | #define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name) | ||
| 325 | #define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port) | ||
| 326 | #define BIO_set_conn_ip(b,ip) BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip) | ||
| 327 | #define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port) | ||
| 328 | #define BIO_get_conn_hostname(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0) | ||
| 329 | #define BIO_get_conn_port(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1) | ||
| 330 | #define BIO_get_conn_ip(b,ip) BIO_ptr_ctrl(b,BIO_C_SET_CONNECT,2) | ||
| 331 | #define BIO_get_conn_int port(b,port) BIO_int_ctrl(b,BIO_C_SET_CONNECT,3,port) | ||
| 332 | |||
| 333 | #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) | ||
| 334 | |||
| 335 | /* BIO_s_accept_socket() */ | ||
| 336 | #define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name) | ||
| 337 | #define BIO_get_accept_port(b) BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0) | ||
| 338 | /* #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */ | ||
| 339 | #define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?"a":NULL) | ||
| 340 | #define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio) | ||
| 341 | |||
| 342 | #define BIO_do_connect(b) BIO_do_handshake(b) | ||
| 343 | #define BIO_do_accept(b) BIO_do_handshake(b) | ||
| 344 | #define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL) | ||
| 345 | |||
| 346 | /* BIO_s_proxy_client() */ | ||
| 347 | #define BIO_set_url(b,url) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,0,(char *)(url)) | ||
| 348 | #define BIO_set_proxies(b,p) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,1,(char *)(p)) | ||
| 349 | /* BIO_set_nbio(b,n) */ | ||
| 350 | #define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s)) | ||
| 351 | /* BIO *BIO_get_filter_bio(BIO *bio); */ | ||
| 352 | #define BIO_set_proxy_cb(b,cb) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(char *)(cb)) | ||
| 353 | #define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk) | ||
| 354 | #define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool) | ||
| 355 | |||
| 356 | #define BIO_get_proxy_header(b,skp) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,0,(char *)skp) | ||
| 357 | #define BIO_get_proxies(b,pxy_p) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,1,(char *)(pxy_p)) | ||
| 358 | #define BIO_get_url(b,url) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,2,(char *)(url)) | ||
| 359 | #define BIO_get_no_connect_return(b) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,5,NULL) | ||
| 360 | |||
| 361 | #define BIO_set_fd(b,fd,c) BIO_int_ctrl(b,BIO_C_SET_FD,c,fd) | ||
| 362 | #define BIO_get_fd(b,c) BIO_ctrl(b,BIO_C_GET_FD,0,(char *)c) | ||
| 363 | |||
| 364 | #define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp) | ||
| 365 | #define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp) | ||
| 366 | |||
| 367 | #define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ | ||
| 368 | BIO_CLOSE|BIO_FP_READ,name) | ||
| 369 | #define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ | ||
| 370 | BIO_CLOSE|BIO_FP_WRITE,name) | ||
| 371 | #define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ | ||
| 372 | BIO_CLOSE|BIO_FP_APPEND,name) | ||
| 373 | |||
| 374 | /* WARNING WARNING, this ups the reference count on the read bio of the | ||
| 375 | * SSL structure. This is because the ssl read BIO is now pointed to by | ||
| 376 | * the next_bio field in the bio. So when you free the BIO, make sure | ||
| 377 | * you are doing a BIO_free_all() to catch the underlying BIO. */ | ||
| 378 | #define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl) | ||
| 379 | #define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp) | ||
| 380 | #define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL) | ||
| 381 | #define BIO_set_ssl_renegotiate_bytes(b,num) \ | ||
| 382 | BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL); | ||
| 383 | #define BIO_get_num_renegotiates(b) \ | ||
| 384 | BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL); | ||
| 385 | #define BIO_set_ssl_renegotiate_timeout(b,seconds) \ | ||
| 386 | BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL); | ||
| 387 | |||
| 388 | /* defined in evp.h */ | ||
| 389 | /* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */ | ||
| 390 | |||
| 391 | #define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm) | ||
| 392 | #define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp) | ||
| 393 | |||
| 394 | /* For the BIO_f_buffer() type */ | ||
| 395 | #define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL) | ||
| 396 | #define BIO_set_buffer_size(b,size) BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL) | ||
| 397 | #define BIO_set_read_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,0) | ||
| 398 | #define BIO_set_write_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,1) | ||
| 399 | #define BIO_set_buffer_read_data(b,buf,num) BIO_ctrl(b,BIO_C_SET_BUFF_READ_DATA,num,buf) | ||
| 400 | |||
| 401 | /* Don't use the next one unless you know what you are doing :-) */ | ||
| 402 | #define BIO_dup_state(b,ret) BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)(ret)) | ||
| 403 | |||
| 404 | #define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL) | ||
| 405 | #define BIO_eof(b) (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL) | ||
| 406 | #define BIO_set_close(b,c) (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL) | ||
| 407 | #define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL) | ||
| 408 | #define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL) | ||
| 409 | #define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL) | ||
| 410 | #define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL) | ||
| 411 | #define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0,(char *)cbp) | ||
| 412 | #define BIO_set_info_callback(b,cb) (int)BIO_ctrl(b,BIO_CTRL_SET_CALLBACK,0,(char *)cb) | ||
| 413 | |||
| 414 | /* For the BIO_f_buffer() type */ | ||
| 415 | #define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL) | ||
| 416 | |||
| 417 | #ifdef NO_STDIO | ||
| 418 | #define NO_FP_API | ||
| 419 | #endif | ||
| 420 | |||
| 421 | #ifndef NOPROTO | ||
| 422 | # if defined(WIN16) && defined(_WINDLL) | ||
| 423 | BIO_METHOD *BIO_s_file_internal(void); | ||
| 424 | BIO *BIO_new_file_internal(char *filename, char *mode); | ||
| 425 | BIO *BIO_new_fp_internal(FILE *stream, int close_flag); | ||
| 426 | # define BIO_s_file BIO_s_file_internal | ||
| 427 | # define BIO_new_file BIO_new_file_internal | ||
| 428 | # define BIO_new_fp BIO_new_fp_internal | ||
| 429 | # else /* FP_API */ | ||
| 430 | BIO_METHOD *BIO_s_file(void ); | ||
| 431 | BIO *BIO_new_file(char *filename, char *mode); | ||
| 432 | BIO *BIO_new_fp(FILE *stream, int close_flag); | ||
| 433 | # define BIO_s_file_internal BIO_s_file | ||
| 434 | # define BIO_new_file_internal BIO_new_file | ||
| 435 | # define BIO_new_fp_internal BIO_s_file | ||
| 436 | # endif /* FP_API */ | ||
| 437 | #else | ||
| 438 | # if defined(WIN16) && defined(_WINDLL) | ||
| 439 | BIO_METHOD *BIO_s_file_internal(); | ||
| 440 | BIO *BIO_new_file_internal(); | ||
| 441 | BIO *BIO_new_fp_internal(); | ||
| 442 | # define BIO_s_file BIO_s_file_internal | ||
| 443 | # define BIO_new_file BIO_new_file_internal | ||
| 444 | # define BIO_new_fp BIO_new_fp_internal | ||
| 445 | # else /* FP_API */ | ||
| 446 | BIO_METHOD *BIO_s_file(); | ||
| 447 | BIO *BIO_new_file(); | ||
| 448 | BIO *BIO_new_fp(); | ||
| 449 | # define BIO_s_file_internal BIO_s_file | ||
| 450 | # define BIO_new_file_internal BIO_new_file | ||
| 451 | # define BIO_new_fp_internal BIO_s_file | ||
| 452 | # endif /* FP_API */ | ||
| 453 | #endif | ||
| 454 | |||
| 455 | #ifndef NOPROTO | ||
| 456 | BIO * BIO_new(BIO_METHOD *type); | ||
| 457 | int BIO_set(BIO *a,BIO_METHOD *type); | ||
| 458 | int BIO_free(BIO *a); | ||
| 459 | int BIO_read(BIO *b, char *data, int len); | ||
| 460 | int BIO_gets(BIO *bp,char *buf, int size); | ||
| 461 | int BIO_write(BIO *b, char *data, int len); | ||
| 462 | int BIO_puts(BIO *bp,char *buf); | ||
| 463 | long BIO_ctrl(BIO *bp,int cmd,long larg,char *parg); | ||
| 464 | char * BIO_ptr_ctrl(BIO *bp,int cmd,long larg); | ||
| 465 | long BIO_int_ctrl(BIO *bp,int cmd,long larg,int iarg); | ||
| 466 | BIO * BIO_push(BIO *b,BIO *append); | ||
| 467 | BIO * BIO_pop(BIO *b); | ||
| 468 | void BIO_free_all(BIO *a); | ||
| 469 | BIO * BIO_find_type(BIO *b,int bio_type); | ||
| 470 | BIO * BIO_get_retry_BIO(BIO *bio, int *reason); | ||
| 471 | int BIO_get_retry_reason(BIO *bio); | ||
| 472 | BIO * BIO_dup_chain(BIO *in); | ||
| 473 | |||
| 474 | #ifndef WIN16 | ||
| 475 | long BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi, | ||
| 476 | long argl,long ret); | ||
| 477 | #else | ||
| 478 | long _far _loadds BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi, | ||
| 479 | long argl,long ret); | ||
| 480 | #endif | ||
| 481 | |||
| 482 | BIO_METHOD *BIO_s_mem(void); | ||
| 483 | BIO_METHOD *BIO_s_socket(void); | ||
| 484 | BIO_METHOD *BIO_s_connect(void); | ||
| 485 | BIO_METHOD *BIO_s_accept(void); | ||
| 486 | BIO_METHOD *BIO_s_fd(void); | ||
| 487 | BIO_METHOD *BIO_s_null(void); | ||
| 488 | BIO_METHOD *BIO_f_null(void); | ||
| 489 | BIO_METHOD *BIO_f_nbio_test(void); | ||
| 490 | BIO_METHOD *BIO_f_buffer(void); | ||
| 491 | |||
| 492 | int BIO_sock_should_retry(int i); | ||
| 493 | int BIO_sock_non_fatal_error(int error); | ||
| 494 | int BIO_fd_should_retry(int i); | ||
| 495 | int BIO_fd_non_fatal_error(int error); | ||
| 496 | int BIO_dump(BIO *b,char *bytes,int len); | ||
| 497 | |||
| 498 | struct hostent *BIO_gethostbyname(char *name); | ||
| 499 | int BIO_sock_error(int sock); | ||
| 500 | int BIO_socket_ioctl(int fd, long type, unsigned long *arg); | ||
| 501 | int BIO_get_port(char *str, short *port_ptr); | ||
| 502 | int BIO_get_host_ip(char *str, unsigned char *ip); | ||
| 503 | int BIO_get_accept_socket(char *host_port); | ||
| 504 | int BIO_accept(int sock,char **ip_port); | ||
| 505 | int BIO_sock_init(void ); | ||
| 506 | void BIO_sock_cleanup(void); | ||
| 507 | int BIO_set_tcp_ndelay(int sock,int turn_on); | ||
| 508 | |||
| 509 | void ERR_load_BIO_strings(void ); | ||
| 510 | |||
| 511 | BIO *BIO_new_socket(int sock, int close_flag); | ||
| 512 | BIO *BIO_new_fd(int fd, int close_flag); | ||
| 513 | BIO *BIO_new_connect(char *host_port); | ||
| 514 | BIO *BIO_new_accept(char *host_port); | ||
| 515 | |||
| 516 | void BIO_copy_next_retry(BIO *b); | ||
| 517 | |||
| 518 | long BIO_ghbn_ctrl(int cmd,int iarg,char *parg); | ||
| 519 | |||
| 520 | #else | ||
| 521 | |||
| 522 | BIO * BIO_new(); | ||
| 523 | int BIO_set(); | ||
| 524 | int BIO_free(); | ||
| 525 | int BIO_read(); | ||
| 526 | int BIO_gets(); | ||
| 527 | int BIO_write(); | ||
| 528 | int BIO_puts(); | ||
| 529 | char * BIO_ptr_ctrl(); | ||
| 530 | long BIO_ctrl(); | ||
| 531 | long BIO_int_ctrl(); | ||
| 532 | BIO * BIO_push(); | ||
| 533 | BIO * BIO_pop(); | ||
| 534 | void BIO_free_all(); | ||
| 535 | BIO * BIO_find_type(); | ||
| 536 | BIO * BIO_get_retry_BIO(); | ||
| 537 | int BIO_get_retry_reason(); | ||
| 538 | BIO * BIO_dup_chain(); | ||
| 539 | |||
| 540 | #ifndef WIN16 | ||
| 541 | long BIO_debug_callback(); | ||
| 542 | #else | ||
| 543 | long _far _loadds BIO_debug_callback(); | ||
| 544 | #endif | ||
| 545 | |||
| 546 | BIO_METHOD *BIO_s_mem(); | ||
| 547 | BIO_METHOD *BIO_s_socket(); | ||
| 548 | BIO_METHOD *BIO_s_connect(); | ||
| 549 | BIO_METHOD *BIO_s_accept(); | ||
| 550 | BIO_METHOD *BIO_s_fd(); | ||
| 551 | BIO_METHOD *BIO_s_null(); | ||
| 552 | BIO_METHOD *BIO_f_null(); | ||
| 553 | BIO_METHOD *BIO_f_buffer(); | ||
| 554 | BIO_METHOD *BIO_f_nbio_test(); | ||
| 555 | |||
| 556 | int BIO_sock_should_retry(); | ||
| 557 | int BIO_sock_non_fatal_error(); | ||
| 558 | int BIO_fd_should_retry(); | ||
| 559 | int BIO_fd_non_fatal_error(); | ||
| 560 | int BIO_dump(); | ||
| 561 | |||
| 562 | struct hostent *BIO_gethostbyname(); | ||
| 563 | int BIO_sock_error(); | ||
| 564 | int BIO_socket_ioctl(); | ||
| 565 | int BIO_get_port(); | ||
| 566 | int BIO_get_host_ip(); | ||
| 567 | int BIO_get_accept_socket(); | ||
| 568 | int BIO_accept(); | ||
| 569 | int BIO_sock_init(); | ||
| 570 | void BIO_sock_cleanup(); | ||
| 571 | int BIO_set_tcp_ndelay(); | ||
| 572 | |||
| 573 | void ERR_load_BIO_strings(); | ||
| 574 | |||
| 575 | BIO *BIO_new_socket(); | ||
| 576 | BIO *BIO_new_fd(); | ||
| 577 | BIO *BIO_new_connect(); | ||
| 578 | BIO *BIO_new_accept(); | ||
| 579 | |||
| 580 | void BIO_copy_next_retry(); | ||
| 581 | |||
| 582 | int BIO_ghbn_ctrl(); | ||
| 583 | |||
| 584 | #endif | ||
| 585 | |||
| 586 | /* Tim Hudson's portable varargs stuff */ | ||
| 587 | |||
| 588 | #ifndef NOPROTO | ||
| 589 | #define VAR_ANSI /* select ANSI version by default */ | ||
| 590 | #endif | ||
| 591 | |||
| 592 | #ifdef VAR_ANSI | ||
| 593 | /* ANSI version of a "portable" macro set for variable length args */ | ||
| 594 | #ifndef __STDARG_H__ /**/ | ||
| 595 | #include <stdarg.h> | ||
| 596 | #endif /**/ | ||
| 597 | |||
| 598 | #define VAR_PLIST(arg1type,arg1) arg1type arg1, ... | ||
| 599 | #define VAR_PLIST2(arg1type,arg1,arg2type,arg2) arg1type arg1,arg2type arg2,... | ||
| 600 | #define VAR_ALIST | ||
| 601 | #define VAR_BDEFN(args,arg1type,arg1) va_list args | ||
| 602 | #define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args | ||
| 603 | #define VAR_INIT(args,arg1type,arg1) va_start(args,arg1); | ||
| 604 | #define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args,arg2); | ||
| 605 | #define VAR_ARG(args,type,arg) arg=va_arg(args,type) | ||
| 606 | #define VAR_END(args) va_end(args); | ||
| 607 | |||
| 608 | #else | ||
| 609 | |||
| 610 | /* K&R version of a "portable" macro set for variable length args */ | ||
| 611 | #ifndef __VARARGS_H__ | ||
| 612 | #include <varargs.h> | ||
| 613 | #endif | ||
| 614 | |||
| 615 | #define VAR_PLIST(arg1type,arg1) va_alist | ||
| 616 | #define VAR_PLIST2(arg1type,arg1,arg2type,arg2) va_alist | ||
| 617 | #define VAR_ALIST va_dcl | ||
| 618 | #define VAR_BDEFN(args,arg1type,arg1) va_list args; arg1type arg1 | ||
| 619 | #define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args; \ | ||
| 620 | arg1type arg1; arg2type arg2 | ||
| 621 | #define VAR_INIT(args,arg1type,arg1) va_start(args); \ | ||
| 622 | arg1=va_arg(args,arg1type); | ||
| 623 | #define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args); \ | ||
| 624 | arg1=va_arg(args,arg1type); arg2=va_arg(args,arg2type); | ||
| 625 | #define VAR_ARG(args,type,arg) arg=va_arg(args,type) | ||
| 626 | #define VAR_END(args) va_end(args); | ||
| 627 | |||
| 628 | #endif | ||
| 629 | |||
| 630 | #ifndef NOPROTO | ||
| 631 | int BIO_printf( VAR_PLIST( BIO *, bio ) ); | ||
| 632 | #else | ||
| 633 | int BIO_printf(); | ||
| 634 | #endif | ||
| 635 | |||
| 636 | /* BEGIN ERROR CODES */ | ||
| 637 | /* Error codes for the BIO functions. */ | ||
| 638 | |||
| 639 | /* Function codes. */ | ||
| 640 | #define BIO_F_ACPT_STATE 100 | ||
| 641 | #define BIO_F_BIO_ACCEPT 101 | ||
| 642 | #define BIO_F_BIO_CTRL 102 | ||
| 643 | #define BIO_F_BIO_GETS 103 | ||
| 644 | #define BIO_F_BIO_GET_ACCEPT_SOCKET 104 | ||
| 645 | #define BIO_F_BIO_GET_HOST_IP 105 | ||
| 646 | #define BIO_F_BIO_GET_PORT 106 | ||
| 647 | #define BIO_F_BIO_NEW 107 | ||
| 648 | #define BIO_F_BIO_NEW_FILE 108 | ||
| 649 | #define BIO_F_BIO_PUTS 109 | ||
| 650 | #define BIO_F_BIO_READ 110 | ||
| 651 | #define BIO_F_BIO_SOCK_INIT 111 | ||
| 652 | #define BIO_F_BIO_WRITE 112 | ||
| 653 | #define BIO_F_BUFFER_CTRL 113 | ||
| 654 | #define BIO_F_CONN_STATE 114 | ||
| 655 | #define BIO_F_FILE_CTRL 115 | ||
| 656 | #define BIO_F_MEM_WRITE 116 | ||
| 657 | #define BIO_F_SSL_NEW 117 | ||
| 658 | #define BIO_F_WSASTARTUP 118 | ||
| 659 | |||
| 660 | /* Reason codes. */ | ||
| 661 | #define BIO_R_ACCEPT_ERROR 100 | ||
| 662 | #define BIO_R_BAD_FOPEN_MODE 101 | ||
| 663 | #define BIO_R_BAD_HOSTNAME_LOOKUP 102 | ||
| 664 | #define BIO_R_CONNECT_ERROR 103 | ||
| 665 | #define BIO_R_ERROR_SETTING_NBIO 104 | ||
| 666 | #define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105 | ||
| 667 | #define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106 | ||
| 668 | #define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107 | ||
| 669 | #define BIO_R_INVALID_IP_ADDRESS 108 | ||
| 670 | #define BIO_R_KEEPALIVE 109 | ||
| 671 | #define BIO_R_NBIO_CONNECT_ERROR 110 | ||
| 672 | #define BIO_R_NO_ACCEPT_PORT_SPECIFIED 111 | ||
| 673 | #define BIO_R_NO_HOSTHNAME_SPECIFIED 112 | ||
| 674 | #define BIO_R_NO_PORT_DEFINED 113 | ||
| 675 | #define BIO_R_NO_PORT_SPECIFIED 114 | ||
| 676 | #define BIO_R_NULL_PARAMETER 115 | ||
| 677 | #define BIO_R_UNABLE_TO_BIND_SOCKET 116 | ||
| 678 | #define BIO_R_UNABLE_TO_CREATE_SOCKET 117 | ||
| 679 | #define BIO_R_UNABLE_TO_LISTEN_SOCKET 118 | ||
| 680 | #define BIO_R_UNINITALISED 119 | ||
| 681 | #define BIO_R_UNSUPPORTED_METHOD 120 | ||
| 682 | #define BIO_R_WSASTARTUP 121 | ||
| 683 | |||
| 684 | #ifdef __cplusplus | ||
| 685 | } | ||
| 686 | #endif | ||
| 687 | #endif | ||
| 688 | |||
diff --git a/src/lib/libcrypto/bio/bio_cb.c b/src/lib/libcrypto/bio/bio_cb.c new file mode 100644 index 0000000000..bc6ed9eda1 --- /dev/null +++ b/src/lib/libcrypto/bio/bio_cb.c | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | /* crypto/bio/bio_cb.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <string.h> | ||
| 61 | #include <stdlib.h> | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include "bio.h" | ||
| 64 | #include "err.h" | ||
| 65 | |||
| 66 | long MS_CALLBACK BIO_debug_callback(bio,cmd,argp,argi,argl,ret) | ||
| 67 | BIO *bio; | ||
| 68 | int cmd; | ||
| 69 | char *argp; | ||
| 70 | int argi; | ||
| 71 | long argl; | ||
| 72 | long ret; | ||
| 73 | { | ||
| 74 | BIO *b; | ||
| 75 | MS_STATIC char buf[256]; | ||
| 76 | char *p; | ||
| 77 | long r=1; | ||
| 78 | |||
| 79 | if (BIO_CB_RETURN & cmd) | ||
| 80 | r=ret; | ||
| 81 | |||
| 82 | sprintf(buf,"BIO[%08lX]:",(unsigned long)bio); | ||
| 83 | p= &(buf[14]); | ||
| 84 | switch (cmd) | ||
| 85 | { | ||
| 86 | case BIO_CB_FREE: | ||
| 87 | sprintf(p,"Free - %s\n",bio->method->name); | ||
| 88 | break; | ||
| 89 | case BIO_CB_READ: | ||
| 90 | if (bio->method->type & BIO_TYPE_DESCRIPTOR) | ||
| 91 | sprintf(p,"read(%d,%d) - %s fd=%d\n",bio->num,argi,bio->method->name,bio->num); | ||
| 92 | else | ||
| 93 | sprintf(p,"read(%d,%d) - %s\n",bio->num,argi,bio->method->name); | ||
| 94 | break; | ||
| 95 | case BIO_CB_WRITE: | ||
| 96 | if (bio->method->type & BIO_TYPE_DESCRIPTOR) | ||
| 97 | sprintf(p,"write(%d,%d) - %s fd=%d\n",bio->num,argi,bio->method->name,bio->num); | ||
| 98 | else | ||
| 99 | sprintf(p,"write(%d,%d) - %s\n",bio->num,argi,bio->method->name); | ||
| 100 | break; | ||
| 101 | case BIO_CB_PUTS: | ||
| 102 | sprintf(p,"puts() - %s\n",bio->method->name); | ||
| 103 | break; | ||
| 104 | case BIO_CB_GETS: | ||
| 105 | sprintf(p,"gets(%d) - %s\n",argi,bio->method->name); | ||
| 106 | break; | ||
| 107 | case BIO_CB_CTRL: | ||
| 108 | sprintf(p,"ctrl(%d) - %s\n",argi,bio->method->name); | ||
| 109 | break; | ||
| 110 | case BIO_CB_RETURN|BIO_CB_READ: | ||
| 111 | sprintf(p,"read return %ld\n",ret); | ||
| 112 | break; | ||
| 113 | case BIO_CB_RETURN|BIO_CB_WRITE: | ||
| 114 | sprintf(p,"write return %ld\n",ret); | ||
| 115 | break; | ||
| 116 | case BIO_CB_RETURN|BIO_CB_GETS: | ||
| 117 | sprintf(p,"gets return %ld\n",ret); | ||
| 118 | break; | ||
| 119 | case BIO_CB_RETURN|BIO_CB_PUTS: | ||
| 120 | sprintf(p,"puts return %ld\n",ret); | ||
| 121 | break; | ||
| 122 | case BIO_CB_RETURN|BIO_CB_CTRL: | ||
| 123 | sprintf(p,"ctrl return %ld\n",ret); | ||
| 124 | break; | ||
| 125 | default: | ||
| 126 | sprintf(p,"bio callback - unknown type (%d)\n",cmd); | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | |||
| 130 | b=(BIO *)bio->cb_arg; | ||
| 131 | if (b != NULL) | ||
| 132 | BIO_write(b,buf,strlen(buf)); | ||
| 133 | #if !defined(NO_STDIO) && !defined(WIN16) | ||
| 134 | else | ||
| 135 | fputs(buf,stderr); | ||
| 136 | #endif | ||
| 137 | return(r); | ||
| 138 | } | ||
diff --git a/src/lib/libcrypto/bio/bio_err.c b/src/lib/libcrypto/bio/bio_err.c new file mode 100644 index 0000000000..37e14ca107 --- /dev/null +++ b/src/lib/libcrypto/bio/bio_err.c | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | /* lib/bio/bio_err.c */ | ||
| 2 | /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | #include <stdio.h> | ||
| 59 | #include "err.h" | ||
| 60 | #include "bio.h" | ||
| 61 | |||
| 62 | /* BEGIN ERROR CODES */ | ||
| 63 | #ifndef NO_ERR | ||
| 64 | static ERR_STRING_DATA BIO_str_functs[]= | ||
| 65 | { | ||
| 66 | {ERR_PACK(0,BIO_F_ACPT_STATE,0), "ACPT_STATE"}, | ||
| 67 | {ERR_PACK(0,BIO_F_BIO_ACCEPT,0), "BIO_accept"}, | ||
| 68 | {ERR_PACK(0,BIO_F_BIO_CTRL,0), "BIO_ctrl"}, | ||
| 69 | {ERR_PACK(0,BIO_F_BIO_GETS,0), "BIO_gets"}, | ||
| 70 | {ERR_PACK(0,BIO_F_BIO_GET_ACCEPT_SOCKET,0), "BIO_get_accept_socket"}, | ||
| 71 | {ERR_PACK(0,BIO_F_BIO_GET_HOST_IP,0), "BIO_get_host_ip"}, | ||
| 72 | {ERR_PACK(0,BIO_F_BIO_GET_PORT,0), "BIO_get_port"}, | ||
| 73 | {ERR_PACK(0,BIO_F_BIO_NEW,0), "BIO_new"}, | ||
| 74 | {ERR_PACK(0,BIO_F_BIO_NEW_FILE,0), "BIO_new_file"}, | ||
| 75 | {ERR_PACK(0,BIO_F_BIO_PUTS,0), "BIO_puts"}, | ||
| 76 | {ERR_PACK(0,BIO_F_BIO_READ,0), "BIO_read"}, | ||
| 77 | {ERR_PACK(0,BIO_F_BIO_SOCK_INIT,0), "BIO_sock_init"}, | ||
| 78 | {ERR_PACK(0,BIO_F_BIO_WRITE,0), "BIO_write"}, | ||
| 79 | {ERR_PACK(0,BIO_F_BUFFER_CTRL,0), "BUFFER_CTRL"}, | ||
| 80 | {ERR_PACK(0,BIO_F_CONN_STATE,0), "CONN_STATE"}, | ||
| 81 | {ERR_PACK(0,BIO_F_FILE_CTRL,0), "FILE_CTRL"}, | ||
| 82 | {ERR_PACK(0,BIO_F_MEM_WRITE,0), "MEM_WRITE"}, | ||
| 83 | {ERR_PACK(0,BIO_F_SSL_NEW,0), "SSL_NEW"}, | ||
| 84 | {ERR_PACK(0,BIO_F_WSASTARTUP,0), "WSASTARTUP"}, | ||
| 85 | {0,NULL}, | ||
| 86 | }; | ||
| 87 | |||
| 88 | static ERR_STRING_DATA BIO_str_reasons[]= | ||
| 89 | { | ||
| 90 | {BIO_R_ACCEPT_ERROR ,"accept error"}, | ||
| 91 | {BIO_R_BAD_FOPEN_MODE ,"bad fopen mode"}, | ||
| 92 | {BIO_R_BAD_HOSTNAME_LOOKUP ,"bad hostname lookup"}, | ||
| 93 | {BIO_R_CONNECT_ERROR ,"connect error"}, | ||
| 94 | {BIO_R_ERROR_SETTING_NBIO ,"error setting nbio"}, | ||
| 95 | {BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET,"error setting nbio on accepted socket"}, | ||
| 96 | {BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET,"error setting nbio on accept socket"}, | ||
| 97 | {BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET ,"gethostbyname addr is not af inet"}, | ||
| 98 | {BIO_R_INVALID_IP_ADDRESS ,"invalid ip address"}, | ||
| 99 | {BIO_R_KEEPALIVE ,"keepalive"}, | ||
| 100 | {BIO_R_NBIO_CONNECT_ERROR ,"nbio connect error"}, | ||
| 101 | {BIO_R_NO_ACCEPT_PORT_SPECIFIED ,"no accept port specified"}, | ||
| 102 | {BIO_R_NO_HOSTHNAME_SPECIFIED ,"no hosthname specified"}, | ||
| 103 | {BIO_R_NO_PORT_DEFINED ,"no port defined"}, | ||
| 104 | {BIO_R_NO_PORT_SPECIFIED ,"no port specified"}, | ||
| 105 | {BIO_R_NULL_PARAMETER ,"null parameter"}, | ||
| 106 | {BIO_R_UNABLE_TO_BIND_SOCKET ,"unable to bind socket"}, | ||
| 107 | {BIO_R_UNABLE_TO_CREATE_SOCKET ,"unable to create socket"}, | ||
| 108 | {BIO_R_UNABLE_TO_LISTEN_SOCKET ,"unable to listen socket"}, | ||
| 109 | {BIO_R_UNINITALISED ,"uninitalised"}, | ||
| 110 | {BIO_R_UNSUPPORTED_METHOD ,"unsupported method"}, | ||
| 111 | {BIO_R_WSASTARTUP ,"wsastartup"}, | ||
| 112 | {0,NULL}, | ||
| 113 | }; | ||
| 114 | |||
| 115 | #endif | ||
| 116 | |||
| 117 | void ERR_load_BIO_strings() | ||
| 118 | { | ||
| 119 | static int init=1; | ||
| 120 | |||
| 121 | if (init); | ||
| 122 | {; | ||
| 123 | init=0; | ||
| 124 | #ifndef NO_ERR | ||
| 125 | ERR_load_strings(ERR_LIB_BIO,BIO_str_functs); | ||
| 126 | ERR_load_strings(ERR_LIB_BIO,BIO_str_reasons); | ||
| 127 | #endif | ||
| 128 | |||
| 129 | } | ||
| 130 | } | ||
diff --git a/src/lib/libcrypto/bio/bio_lib.c b/src/lib/libcrypto/bio/bio_lib.c new file mode 100644 index 0000000000..7a66b0892e --- /dev/null +++ b/src/lib/libcrypto/bio/bio_lib.c | |||
| @@ -0,0 +1,519 @@ | |||
| 1 | /* crypto/bio/bio_lib.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #include "crypto.h" | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include "bio.h" | ||
| 64 | #include "stack.h" | ||
| 65 | |||
| 66 | static STACK *bio_meth=NULL; | ||
| 67 | static int bio_meth_num=0; | ||
| 68 | |||
| 69 | BIO *BIO_new(method) | ||
| 70 | BIO_METHOD *method; | ||
| 71 | { | ||
| 72 | BIO *ret=NULL; | ||
| 73 | |||
| 74 | ret=(BIO *)Malloc(sizeof(BIO)); | ||
| 75 | if (ret == NULL) | ||
| 76 | { | ||
| 77 | BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE); | ||
| 78 | return(NULL); | ||
| 79 | } | ||
| 80 | if (!BIO_set(ret,method)) | ||
| 81 | { | ||
| 82 | Free(ret); | ||
| 83 | ret=NULL; | ||
| 84 | } | ||
| 85 | return(ret); | ||
| 86 | } | ||
| 87 | |||
| 88 | int BIO_set(bio,method) | ||
| 89 | BIO *bio; | ||
| 90 | BIO_METHOD *method; | ||
| 91 | { | ||
| 92 | bio->method=method; | ||
| 93 | bio->callback=NULL; | ||
| 94 | bio->cb_arg=NULL; | ||
| 95 | bio->init=0; | ||
| 96 | bio->shutdown=1; | ||
| 97 | bio->flags=0; | ||
| 98 | bio->retry_reason=0; | ||
| 99 | bio->num=0; | ||
| 100 | bio->ptr=NULL; | ||
| 101 | bio->prev_bio=NULL; | ||
| 102 | bio->next_bio=NULL; | ||
| 103 | bio->references=1; | ||
| 104 | bio->num_read=0L; | ||
| 105 | bio->num_write=0L; | ||
| 106 | CRYPTO_new_ex_data(bio_meth,(char *)bio,&bio->ex_data); | ||
| 107 | if (method->create != NULL) | ||
| 108 | if (!method->create(bio)) | ||
| 109 | return(0); | ||
| 110 | return(1); | ||
| 111 | } | ||
| 112 | |||
| 113 | int BIO_free(a) | ||
| 114 | BIO *a; | ||
| 115 | { | ||
| 116 | int ret=0,i; | ||
| 117 | |||
| 118 | if (a == NULL) return(0); | ||
| 119 | |||
| 120 | i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO); | ||
| 121 | #ifdef REF_PRINT | ||
| 122 | REF_PRINT("BIO",a); | ||
| 123 | #endif | ||
| 124 | if (i > 0) return(1); | ||
| 125 | #ifdef REF_CHECK | ||
| 126 | if (i < 0) | ||
| 127 | { | ||
| 128 | fprintf(stderr,"BIO_free, bad reference count\n"); | ||
| 129 | abort(); | ||
| 130 | } | ||
| 131 | #endif | ||
| 132 | if ((a->callback != NULL) && | ||
| 133 | ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0)) | ||
| 134 | return(i); | ||
| 135 | |||
| 136 | CRYPTO_free_ex_data(bio_meth,(char *)a,&a->ex_data); | ||
| 137 | |||
| 138 | if ((a->method == NULL) || (a->method->destroy == NULL)) return(1); | ||
| 139 | ret=a->method->destroy(a); | ||
| 140 | Free(a); | ||
| 141 | return(1); | ||
| 142 | } | ||
| 143 | |||
| 144 | int BIO_read(b,out,outl) | ||
| 145 | BIO *b; | ||
| 146 | char *out; | ||
| 147 | int outl; | ||
| 148 | { | ||
| 149 | int i; | ||
| 150 | long (*cb)(); | ||
| 151 | |||
| 152 | if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) | ||
| 153 | { | ||
| 154 | BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD); | ||
| 155 | return(-2); | ||
| 156 | } | ||
| 157 | |||
| 158 | cb=b->callback; | ||
| 159 | if ((cb != NULL) && | ||
| 160 | ((i=(int)cb(b,BIO_CB_READ,out,outl,0L,1L)) <= 0)) | ||
| 161 | return(i); | ||
| 162 | |||
| 163 | if (!b->init) | ||
| 164 | { | ||
| 165 | BIOerr(BIO_F_BIO_READ,BIO_R_UNINITALISED); | ||
| 166 | return(-2); | ||
| 167 | } | ||
| 168 | |||
| 169 | i=b->method->bread(b,out,outl); | ||
| 170 | if (i > 0) b->num_read+=(unsigned long)i; | ||
| 171 | |||
| 172 | if (cb != NULL) | ||
| 173 | i=(int)cb(b,BIO_CB_READ|BIO_CB_RETURN,out,outl, | ||
| 174 | 0L,(long)i); | ||
| 175 | return(i); | ||
| 176 | } | ||
| 177 | |||
| 178 | int BIO_write(b,in,inl) | ||
| 179 | BIO *b; | ||
| 180 | char *in; | ||
| 181 | int inl; | ||
| 182 | { | ||
| 183 | int i; | ||
| 184 | long (*cb)(); | ||
| 185 | |||
| 186 | if (b == NULL) | ||
| 187 | return(0); | ||
| 188 | |||
| 189 | cb=b->callback; | ||
| 190 | if ((b->method == NULL) || (b->method->bwrite == NULL)) | ||
| 191 | { | ||
| 192 | BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD); | ||
| 193 | return(-2); | ||
| 194 | } | ||
| 195 | |||
| 196 | if ((cb != NULL) && | ||
| 197 | ((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0)) | ||
| 198 | return(i); | ||
| 199 | |||
| 200 | if (!b->init) | ||
| 201 | { | ||
| 202 | BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITALISED); | ||
| 203 | return(-2); | ||
| 204 | } | ||
| 205 | |||
| 206 | i=b->method->bwrite(b,in,inl); | ||
| 207 | if (i > 0) b->num_write+=(unsigned long)i; | ||
| 208 | |||
| 209 | if (cb != NULL) | ||
| 210 | i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl, | ||
| 211 | 0L,(long)i); | ||
| 212 | return(i); | ||
| 213 | } | ||
| 214 | |||
| 215 | int BIO_puts(b,in) | ||
| 216 | BIO *b; | ||
| 217 | char *in; | ||
| 218 | { | ||
| 219 | int i; | ||
| 220 | long (*cb)(); | ||
| 221 | |||
| 222 | if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) | ||
| 223 | { | ||
| 224 | BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD); | ||
| 225 | return(-2); | ||
| 226 | } | ||
| 227 | |||
| 228 | cb=b->callback; | ||
| 229 | |||
| 230 | if ((cb != NULL) && | ||
| 231 | ((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0)) | ||
| 232 | return(i); | ||
| 233 | |||
| 234 | if (!b->init) | ||
| 235 | { | ||
| 236 | BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITALISED); | ||
| 237 | return(-2); | ||
| 238 | } | ||
| 239 | |||
| 240 | i=b->method->bputs(b,in); | ||
| 241 | |||
| 242 | if (cb != NULL) | ||
| 243 | i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0, | ||
| 244 | 0L,(long)i); | ||
| 245 | return(i); | ||
| 246 | } | ||
| 247 | |||
| 248 | int BIO_gets(b,in,inl) | ||
| 249 | BIO *b; | ||
| 250 | char *in; | ||
| 251 | int inl; | ||
| 252 | { | ||
| 253 | int i; | ||
| 254 | long (*cb)(); | ||
| 255 | |||
| 256 | if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) | ||
| 257 | { | ||
| 258 | BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD); | ||
| 259 | return(-2); | ||
| 260 | } | ||
| 261 | |||
| 262 | cb=b->callback; | ||
| 263 | |||
| 264 | if ((cb != NULL) && | ||
| 265 | ((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0)) | ||
| 266 | return(i); | ||
| 267 | |||
| 268 | if (!b->init) | ||
| 269 | { | ||
| 270 | BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITALISED); | ||
| 271 | return(-2); | ||
| 272 | } | ||
| 273 | |||
| 274 | i=b->method->bgets(b,in,inl); | ||
| 275 | |||
| 276 | if (cb != NULL) | ||
| 277 | i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl, | ||
| 278 | 0L,(long)i); | ||
| 279 | return(i); | ||
| 280 | } | ||
| 281 | |||
| 282 | long BIO_int_ctrl(b,cmd,larg,iarg) | ||
| 283 | BIO *b; | ||
| 284 | int cmd; | ||
| 285 | long larg; | ||
| 286 | int iarg; | ||
| 287 | { | ||
| 288 | int i; | ||
| 289 | |||
| 290 | i=iarg; | ||
| 291 | return(BIO_ctrl(b,cmd,larg,(char *)&i)); | ||
| 292 | } | ||
| 293 | |||
| 294 | char *BIO_ptr_ctrl(b,cmd,larg) | ||
| 295 | BIO *b; | ||
| 296 | int cmd; | ||
| 297 | long larg; | ||
| 298 | { | ||
| 299 | char *p=NULL; | ||
| 300 | |||
| 301 | if (BIO_ctrl(b,cmd,larg,(char *)&p) <= 0) | ||
| 302 | return(NULL); | ||
| 303 | else | ||
| 304 | return(p); | ||
| 305 | } | ||
| 306 | |||
| 307 | long BIO_ctrl(b,cmd,larg,parg) | ||
| 308 | BIO *b; | ||
| 309 | int cmd; | ||
| 310 | long larg; | ||
| 311 | char *parg; | ||
| 312 | { | ||
| 313 | long ret; | ||
| 314 | long (*cb)(); | ||
| 315 | |||
| 316 | if (b == NULL) return(0); | ||
| 317 | |||
| 318 | if ((b->method == NULL) || (b->method->ctrl == NULL)) | ||
| 319 | { | ||
| 320 | BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD); | ||
| 321 | return(-2); | ||
| 322 | } | ||
| 323 | |||
| 324 | cb=b->callback; | ||
| 325 | |||
| 326 | if ((cb != NULL) && | ||
| 327 | ((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0)) | ||
| 328 | return(ret); | ||
| 329 | |||
| 330 | ret=b->method->ctrl(b,cmd,larg,parg); | ||
| 331 | |||
| 332 | if (cb != NULL) | ||
| 333 | ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd, | ||
| 334 | larg,ret); | ||
| 335 | return(ret); | ||
| 336 | } | ||
| 337 | |||
| 338 | /* put the 'bio' on the end of b's list of operators */ | ||
| 339 | BIO *BIO_push(b,bio) | ||
| 340 | BIO *b,*bio; | ||
| 341 | { | ||
| 342 | BIO *lb; | ||
| 343 | |||
| 344 | if (b == NULL) return(bio); | ||
| 345 | lb=b; | ||
| 346 | while (lb->next_bio != NULL) | ||
| 347 | lb=lb->next_bio; | ||
| 348 | lb->next_bio=bio; | ||
| 349 | if (bio != NULL) | ||
| 350 | bio->prev_bio=lb; | ||
| 351 | /* called to do internal processing */ | ||
| 352 | BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL); | ||
| 353 | return(b); | ||
| 354 | } | ||
| 355 | |||
| 356 | /* Remove the first and return the rest */ | ||
| 357 | BIO *BIO_pop(b) | ||
| 358 | BIO *b; | ||
| 359 | { | ||
| 360 | BIO *ret; | ||
| 361 | |||
| 362 | if (b == NULL) return(NULL); | ||
| 363 | ret=b->next_bio; | ||
| 364 | |||
| 365 | if (b->prev_bio != NULL) | ||
| 366 | b->prev_bio->next_bio=b->next_bio; | ||
| 367 | if (b->next_bio != NULL) | ||
| 368 | b->next_bio->prev_bio=b->prev_bio; | ||
| 369 | |||
| 370 | b->next_bio=NULL; | ||
| 371 | b->prev_bio=NULL; | ||
| 372 | BIO_ctrl(b,BIO_CTRL_POP,0,NULL); | ||
| 373 | return(ret); | ||
| 374 | } | ||
| 375 | |||
| 376 | BIO *BIO_get_retry_BIO(bio,reason) | ||
| 377 | BIO *bio; | ||
| 378 | int *reason; | ||
| 379 | { | ||
| 380 | BIO *b,*last; | ||
| 381 | |||
| 382 | b=last=bio; | ||
| 383 | for (;;) | ||
| 384 | { | ||
| 385 | if (!BIO_should_retry(b)) break; | ||
| 386 | last=b; | ||
| 387 | b=b->next_bio; | ||
| 388 | if (b == NULL) break; | ||
| 389 | } | ||
| 390 | if (reason != NULL) *reason=last->retry_reason; | ||
| 391 | return(last); | ||
| 392 | } | ||
| 393 | |||
| 394 | int BIO_get_retry_reason(bio) | ||
| 395 | BIO *bio; | ||
| 396 | { | ||
| 397 | return(bio->retry_reason); | ||
| 398 | } | ||
| 399 | |||
| 400 | BIO *BIO_find_type(bio,type) | ||
| 401 | BIO *bio; | ||
| 402 | int type; | ||
| 403 | { | ||
| 404 | int mt,mask; | ||
| 405 | |||
| 406 | mask=type&0xff; | ||
| 407 | do { | ||
| 408 | if (bio->method != NULL) | ||
| 409 | { | ||
| 410 | mt=bio->method->type; | ||
| 411 | |||
| 412 | if (!mask) | ||
| 413 | { | ||
| 414 | if (mt & type) return(bio); | ||
| 415 | } | ||
| 416 | else if (mt == type) | ||
| 417 | return(bio); | ||
| 418 | } | ||
| 419 | bio=bio->next_bio; | ||
| 420 | } while (bio != NULL); | ||
| 421 | return(NULL); | ||
| 422 | } | ||
| 423 | |||
| 424 | void BIO_free_all(bio) | ||
| 425 | BIO *bio; | ||
| 426 | { | ||
| 427 | BIO *b; | ||
| 428 | int ref; | ||
| 429 | |||
| 430 | while (bio != NULL) | ||
| 431 | { | ||
| 432 | b=bio; | ||
| 433 | ref=b->references; | ||
| 434 | bio=bio->next_bio; | ||
| 435 | BIO_free(b); | ||
| 436 | /* Since ref count > 1, don't free anyone else. */ | ||
| 437 | if (ref > 1) break; | ||
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | BIO *BIO_dup_chain(in) | ||
| 442 | BIO *in; | ||
| 443 | { | ||
| 444 | BIO *ret=NULL,*eoc=NULL,*bio,*new; | ||
| 445 | |||
| 446 | for (bio=in; bio != NULL; bio=bio->next_bio) | ||
| 447 | { | ||
| 448 | if ((new=BIO_new(bio->method)) == NULL) goto err; | ||
| 449 | new->callback=bio->callback; | ||
| 450 | new->cb_arg=bio->cb_arg; | ||
| 451 | new->init=bio->init; | ||
| 452 | new->shutdown=bio->shutdown; | ||
| 453 | new->flags=bio->flags; | ||
| 454 | |||
| 455 | /* This will let SSL_s_sock() work with stdin/stdout */ | ||
| 456 | new->num=bio->num; | ||
| 457 | |||
| 458 | if (!BIO_dup_state(bio,(char *)new)) | ||
| 459 | { | ||
| 460 | BIO_free(new); | ||
| 461 | goto err; | ||
| 462 | } | ||
| 463 | |||
| 464 | /* copy app data */ | ||
| 465 | if (!CRYPTO_dup_ex_data(bio_meth,&new->ex_data,&bio->ex_data)) | ||
| 466 | goto err; | ||
| 467 | |||
| 468 | if (ret == NULL) | ||
| 469 | { | ||
| 470 | eoc=new; | ||
| 471 | ret=eoc; | ||
| 472 | } | ||
| 473 | else | ||
| 474 | { | ||
| 475 | BIO_push(eoc,new); | ||
| 476 | eoc=new; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | return(ret); | ||
| 480 | err: | ||
| 481 | if (ret != NULL) | ||
| 482 | BIO_free(ret); | ||
| 483 | return(NULL); | ||
| 484 | } | ||
| 485 | |||
| 486 | void BIO_copy_next_retry(b) | ||
| 487 | BIO *b; | ||
| 488 | { | ||
| 489 | BIO_set_flags(b,BIO_get_retry_flags(b->next_bio)); | ||
| 490 | b->retry_reason=b->next_bio->retry_reason; | ||
| 491 | } | ||
| 492 | |||
| 493 | int BIO_get_ex_new_index(argl,argp,new_func,dup_func,free_func) | ||
| 494 | long argl; | ||
| 495 | char *argp; | ||
| 496 | int (*new_func)(); | ||
| 497 | int (*dup_func)(); | ||
| 498 | void (*free_func)(); | ||
| 499 | { | ||
| 500 | bio_meth_num++; | ||
| 501 | return(CRYPTO_get_ex_new_index(bio_meth_num-1,&bio_meth, | ||
| 502 | argl,argp,new_func,dup_func,free_func)); | ||
| 503 | } | ||
| 504 | |||
| 505 | int BIO_set_ex_data(bio,idx,data) | ||
| 506 | BIO *bio; | ||
| 507 | int idx; | ||
| 508 | char *data; | ||
| 509 | { | ||
| 510 | return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data)); | ||
| 511 | } | ||
| 512 | |||
| 513 | char *BIO_get_ex_data(bio,idx) | ||
| 514 | BIO *bio; | ||
| 515 | int idx; | ||
| 516 | { | ||
| 517 | return(CRYPTO_get_ex_data(&(bio->ex_data),idx)); | ||
| 518 | } | ||
| 519 | |||
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c new file mode 100644 index 0000000000..e49902fa9f --- /dev/null +++ b/src/lib/libcrypto/bio/bss_acpt.c | |||
| @@ -0,0 +1,500 @@ | |||
| 1 | /* crypto/bio/bss_acpt.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #ifndef NO_SOCK | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <errno.h> | ||
| 63 | #define USE_SOCKETS | ||
| 64 | #include "cryptlib.h" | ||
| 65 | #include "bio.h" | ||
| 66 | |||
| 67 | /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ | ||
| 68 | |||
| 69 | #ifdef WIN16 | ||
| 70 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | ||
| 71 | #else | ||
| 72 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
| 73 | #endif | ||
| 74 | |||
| 75 | typedef struct bio_accept_st | ||
| 76 | { | ||
| 77 | int state; | ||
| 78 | char *param_addr; | ||
| 79 | |||
| 80 | int accept_sock; | ||
| 81 | int accept_nbio; | ||
| 82 | |||
| 83 | char *addr; | ||
| 84 | int nbio; | ||
| 85 | BIO *bio_chain; | ||
| 86 | } BIO_ACCEPT; | ||
| 87 | |||
| 88 | #ifndef NOPROTO | ||
| 89 | static int acpt_write(BIO *h,char *buf,int num); | ||
| 90 | static int acpt_read(BIO *h,char *buf,int size); | ||
| 91 | static int acpt_puts(BIO *h,char *str); | ||
| 92 | static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 93 | static int acpt_new(BIO *h); | ||
| 94 | static int acpt_free(BIO *data); | ||
| 95 | #else | ||
| 96 | static int acpt_write(); | ||
| 97 | static int acpt_read(); | ||
| 98 | static int acpt_puts(); | ||
| 99 | static long acpt_ctrl(); | ||
| 100 | static int acpt_new(); | ||
| 101 | static int acpt_free(); | ||
| 102 | #endif | ||
| 103 | |||
| 104 | #ifndef NOPROTO | ||
| 105 | static int acpt_state(BIO *b, BIO_ACCEPT *c); | ||
| 106 | static void acpt_close_socket(BIO *data); | ||
| 107 | BIO_ACCEPT *BIO_ACCEPT_new(void ); | ||
| 108 | void BIO_ACCEPT_free(BIO_ACCEPT *a); | ||
| 109 | |||
| 110 | #else | ||
| 111 | |||
| 112 | static int acpt_state(); | ||
| 113 | static void acpt_close_socket(); | ||
| 114 | BIO_ACCEPT *BIO_ACCEPT_new(); | ||
| 115 | void BIO_ACCEPT_free(); | ||
| 116 | #endif | ||
| 117 | |||
| 118 | #define ACPT_S_BEFORE 1 | ||
| 119 | #define ACPT_S_GET_ACCEPT_SOCKET 2 | ||
| 120 | #define ACPT_S_OK 3 | ||
| 121 | |||
| 122 | static BIO_METHOD methods_acceptp= | ||
| 123 | { | ||
| 124 | BIO_TYPE_ACCEPT, | ||
| 125 | "socket accept", | ||
| 126 | acpt_write, | ||
| 127 | acpt_read, | ||
| 128 | acpt_puts, | ||
| 129 | NULL, /* connect_gets, */ | ||
| 130 | acpt_ctrl, | ||
| 131 | acpt_new, | ||
| 132 | acpt_free, | ||
| 133 | }; | ||
| 134 | |||
| 135 | BIO_METHOD *BIO_s_accept() | ||
| 136 | { | ||
| 137 | return(&methods_acceptp); | ||
| 138 | } | ||
| 139 | |||
| 140 | static int acpt_new(bi) | ||
| 141 | BIO *bi; | ||
| 142 | { | ||
| 143 | BIO_ACCEPT *ba; | ||
| 144 | |||
| 145 | bi->init=0; | ||
| 146 | bi->num=INVALID_SOCKET; | ||
| 147 | bi->flags=0; | ||
| 148 | if ((ba=BIO_ACCEPT_new()) == NULL) | ||
| 149 | return(0); | ||
| 150 | bi->ptr=(char *)ba; | ||
| 151 | ba->state=ACPT_S_BEFORE; | ||
| 152 | bi->shutdown=1; | ||
| 153 | return(1); | ||
| 154 | } | ||
| 155 | |||
| 156 | BIO_ACCEPT *BIO_ACCEPT_new() | ||
| 157 | { | ||
| 158 | BIO_ACCEPT *ret; | ||
| 159 | |||
| 160 | if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL) | ||
| 161 | return(NULL); | ||
| 162 | |||
| 163 | memset(ret,0,sizeof(BIO_ACCEPT)); | ||
| 164 | ret->accept_sock=INVALID_SOCKET; | ||
| 165 | return(ret); | ||
| 166 | } | ||
| 167 | |||
| 168 | void BIO_ACCEPT_free(a) | ||
| 169 | BIO_ACCEPT *a; | ||
| 170 | { | ||
| 171 | if (a->param_addr != NULL) Free(a->param_addr); | ||
| 172 | if (a->addr != NULL) Free(a->addr); | ||
| 173 | if (a->bio_chain != NULL) BIO_free(a->bio_chain); | ||
| 174 | Free(a); | ||
| 175 | } | ||
| 176 | |||
| 177 | static void acpt_close_socket(bio) | ||
| 178 | BIO *bio; | ||
| 179 | { | ||
| 180 | BIO_ACCEPT *c; | ||
| 181 | |||
| 182 | c=(BIO_ACCEPT *)bio->ptr; | ||
| 183 | if (c->accept_sock != INVALID_SOCKET) | ||
| 184 | { | ||
| 185 | shutdown(c->accept_sock,2); | ||
| 186 | # ifdef WINDOWS | ||
| 187 | closesocket(c->accept_sock); | ||
| 188 | # else | ||
| 189 | close(c->accept_sock); | ||
| 190 | # endif | ||
| 191 | c->accept_sock=INVALID_SOCKET; | ||
| 192 | bio->num=INVALID_SOCKET; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | static int acpt_free(a) | ||
| 197 | BIO *a; | ||
| 198 | { | ||
| 199 | BIO_ACCEPT *data; | ||
| 200 | |||
| 201 | if (a == NULL) return(0); | ||
| 202 | data=(BIO_ACCEPT *)a->ptr; | ||
| 203 | |||
| 204 | if (a->shutdown) | ||
| 205 | { | ||
| 206 | acpt_close_socket(a); | ||
| 207 | BIO_ACCEPT_free(data); | ||
| 208 | a->ptr=NULL; | ||
| 209 | a->flags=0; | ||
| 210 | a->init=0; | ||
| 211 | } | ||
| 212 | return(1); | ||
| 213 | } | ||
| 214 | |||
| 215 | static int acpt_state(b,c) | ||
| 216 | BIO *b; | ||
| 217 | BIO_ACCEPT *c; | ||
| 218 | { | ||
| 219 | BIO *bio=NULL,*dbio; | ||
| 220 | unsigned long l=1; | ||
| 221 | int s= -1; | ||
| 222 | int i; | ||
| 223 | |||
| 224 | again: | ||
| 225 | switch (c->state) | ||
| 226 | { | ||
| 227 | case ACPT_S_BEFORE: | ||
| 228 | if (c->param_addr == NULL) | ||
| 229 | { | ||
| 230 | BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); | ||
| 231 | return(-1); | ||
| 232 | } | ||
| 233 | s=BIO_get_accept_socket(c->param_addr); | ||
| 234 | if (s == INVALID_SOCKET) | ||
| 235 | return(-1); | ||
| 236 | |||
| 237 | #ifdef FIONBIO | ||
| 238 | if (c->accept_nbio) | ||
| 239 | { | ||
| 240 | i=BIO_socket_ioctl(b->num,FIONBIO,&l); | ||
| 241 | if (i < 0) | ||
| 242 | { | ||
| 243 | #ifdef WINDOWS | ||
| 244 | closesocket(s); | ||
| 245 | #else | ||
| 246 | close(s); | ||
| 247 | # endif | ||
| 248 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); | ||
| 249 | return(-1); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | #endif | ||
| 253 | c->accept_sock=s; | ||
| 254 | b->num=s; | ||
| 255 | c->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
| 256 | return(1); | ||
| 257 | break; | ||
| 258 | case ACPT_S_GET_ACCEPT_SOCKET: | ||
| 259 | if (b->next_bio != NULL) | ||
| 260 | { | ||
| 261 | c->state=ACPT_S_OK; | ||
| 262 | goto again; | ||
| 263 | } | ||
| 264 | i=BIO_accept(c->accept_sock,&(c->addr)); | ||
| 265 | if (i < 0) return(i); | ||
| 266 | bio=BIO_new_socket(i,BIO_CLOSE); | ||
| 267 | if (bio == NULL) goto err; | ||
| 268 | |||
| 269 | BIO_set_callback(bio,BIO_get_callback(b)); | ||
| 270 | BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); | ||
| 271 | |||
| 272 | #ifdef FIONBIO | ||
| 273 | if (c->nbio) | ||
| 274 | { | ||
| 275 | i=BIO_socket_ioctl(i,FIONBIO,&l); | ||
| 276 | if (i < 0) | ||
| 277 | { | ||
| 278 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); | ||
| 279 | goto err; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | #endif | ||
| 283 | |||
| 284 | /* If the accept BIO has an bio_chain, we dup it and | ||
| 285 | * put the new socket at the end. */ | ||
| 286 | if (c->bio_chain != NULL) | ||
| 287 | { | ||
| 288 | if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL) | ||
| 289 | goto err; | ||
| 290 | if (!BIO_push(dbio,bio)) goto err; | ||
| 291 | bio=dbio; | ||
| 292 | } | ||
| 293 | if (BIO_push(b,bio) == NULL) goto err; | ||
| 294 | |||
| 295 | c->state=ACPT_S_OK; | ||
| 296 | return(1); | ||
| 297 | err: | ||
| 298 | if (bio != NULL) | ||
| 299 | BIO_free(bio); | ||
| 300 | else if (s >= 0) | ||
| 301 | { | ||
| 302 | #ifdef WINDOWS | ||
| 303 | closesocket(s); | ||
| 304 | #else | ||
| 305 | close(s); | ||
| 306 | # endif | ||
| 307 | } | ||
| 308 | return(0); | ||
| 309 | break; | ||
| 310 | case ACPT_S_OK: | ||
| 311 | if (b->next_bio == NULL) | ||
| 312 | { | ||
| 313 | c->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
| 314 | goto again; | ||
| 315 | } | ||
| 316 | return(1); | ||
| 317 | break; | ||
| 318 | default: | ||
| 319 | return(0); | ||
| 320 | break; | ||
| 321 | } | ||
| 322 | |||
| 323 | } | ||
| 324 | |||
| 325 | static int acpt_read(b,out,outl) | ||
| 326 | BIO *b; | ||
| 327 | char *out; | ||
| 328 | int outl; | ||
| 329 | { | ||
| 330 | int ret=0; | ||
| 331 | BIO_ACCEPT *data; | ||
| 332 | |||
| 333 | BIO_clear_retry_flags(b); | ||
| 334 | data=(BIO_ACCEPT *)b->ptr; | ||
| 335 | |||
| 336 | while (b->next_bio == NULL) | ||
| 337 | { | ||
| 338 | ret=acpt_state(b,data); | ||
| 339 | if (ret <= 0) return(ret); | ||
| 340 | } | ||
| 341 | |||
| 342 | ret=BIO_read(b->next_bio,out,outl); | ||
| 343 | BIO_copy_next_retry(b); | ||
| 344 | return(ret); | ||
| 345 | } | ||
| 346 | |||
| 347 | static int acpt_write(b,in,inl) | ||
| 348 | BIO *b; | ||
| 349 | char *in; | ||
| 350 | int inl; | ||
| 351 | { | ||
| 352 | int ret; | ||
| 353 | BIO_ACCEPT *data; | ||
| 354 | |||
| 355 | BIO_clear_retry_flags(b); | ||
| 356 | data=(BIO_ACCEPT *)b->ptr; | ||
| 357 | |||
| 358 | while (b->next_bio == NULL) | ||
| 359 | { | ||
| 360 | ret=acpt_state(b,data); | ||
| 361 | if (ret <= 0) return(ret); | ||
| 362 | } | ||
| 363 | |||
| 364 | ret=BIO_write(b->next_bio,in,inl); | ||
| 365 | BIO_copy_next_retry(b); | ||
| 366 | return(ret); | ||
| 367 | } | ||
| 368 | |||
| 369 | static long acpt_ctrl(b,cmd,num,ptr) | ||
| 370 | BIO *b; | ||
| 371 | int cmd; | ||
| 372 | long num; | ||
| 373 | char *ptr; | ||
| 374 | { | ||
| 375 | BIO *dbio; | ||
| 376 | int *ip; | ||
| 377 | long ret=1; | ||
| 378 | BIO_ACCEPT *data; | ||
| 379 | char **pp; | ||
| 380 | |||
| 381 | data=(BIO_ACCEPT *)b->ptr; | ||
| 382 | |||
| 383 | switch (cmd) | ||
| 384 | { | ||
| 385 | case BIO_CTRL_RESET: | ||
| 386 | ret=0; | ||
| 387 | data->state=ACPT_S_BEFORE; | ||
| 388 | acpt_close_socket(b); | ||
| 389 | b->flags=0; | ||
| 390 | break; | ||
| 391 | case BIO_C_DO_STATE_MACHINE: | ||
| 392 | /* use this one to start the connection */ | ||
| 393 | ret=(long)acpt_state(b,data); | ||
| 394 | break; | ||
| 395 | case BIO_C_SET_ACCEPT: | ||
| 396 | if (ptr != NULL) | ||
| 397 | { | ||
| 398 | if (num == 0) | ||
| 399 | { | ||
| 400 | b->init=1; | ||
| 401 | if (data->param_addr != NULL) | ||
| 402 | Free(data->param_addr); | ||
| 403 | data->param_addr=BUF_strdup(ptr); | ||
| 404 | } | ||
| 405 | else if (num == 1) | ||
| 406 | { | ||
| 407 | data->accept_nbio=(ptr != NULL); | ||
| 408 | } | ||
| 409 | else if (num == 2) | ||
| 410 | { | ||
| 411 | if (data->bio_chain != NULL) | ||
| 412 | BIO_free(data->bio_chain); | ||
| 413 | data->bio_chain=(BIO *)ptr; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | break; | ||
| 417 | case BIO_C_SET_NBIO: | ||
| 418 | data->nbio=(int)num; | ||
| 419 | break; | ||
| 420 | case BIO_C_GET_FD: | ||
| 421 | if (b->init) | ||
| 422 | { | ||
| 423 | ip=(int *)ptr; | ||
| 424 | if (ip != NULL) | ||
| 425 | *ip=data->accept_sock; | ||
| 426 | ret=b->num; | ||
| 427 | } | ||
| 428 | else | ||
| 429 | ret= -1; | ||
| 430 | break; | ||
| 431 | case BIO_C_GET_ACCEPT: | ||
| 432 | if (b->init) | ||
| 433 | { | ||
| 434 | if (ptr != NULL) | ||
| 435 | { | ||
| 436 | pp=(char **)ptr; | ||
| 437 | *pp=data->param_addr; | ||
| 438 | } | ||
| 439 | else | ||
| 440 | ret= -1; | ||
| 441 | } | ||
| 442 | else | ||
| 443 | ret= -1; | ||
| 444 | break; | ||
| 445 | case BIO_CTRL_GET_CLOSE: | ||
| 446 | ret=b->shutdown; | ||
| 447 | break; | ||
| 448 | case BIO_CTRL_SET_CLOSE: | ||
| 449 | b->shutdown=(int)num; | ||
| 450 | break; | ||
| 451 | case BIO_CTRL_PENDING: | ||
| 452 | case BIO_CTRL_WPENDING: | ||
| 453 | ret=0; | ||
| 454 | break; | ||
| 455 | case BIO_CTRL_FLUSH: | ||
| 456 | break; | ||
| 457 | case BIO_CTRL_DUP: | ||
| 458 | dbio=(BIO *)ptr; | ||
| 459 | /* if (data->param_port) EAY EAY | ||
| 460 | BIO_set_port(dbio,data->param_port); | ||
| 461 | if (data->param_hostname) | ||
| 462 | BIO_set_hostname(dbio,data->param_hostname); | ||
| 463 | BIO_set_nbio(dbio,data->nbio); */ | ||
| 464 | break; | ||
| 465 | |||
| 466 | default: | ||
| 467 | ret=0; | ||
| 468 | break; | ||
| 469 | } | ||
| 470 | return(ret); | ||
| 471 | } | ||
| 472 | |||
| 473 | static int acpt_puts(bp,str) | ||
| 474 | BIO *bp; | ||
| 475 | char *str; | ||
| 476 | { | ||
| 477 | int n,ret; | ||
| 478 | |||
| 479 | n=strlen(str); | ||
| 480 | ret=acpt_write(bp,str,n); | ||
| 481 | return(ret); | ||
| 482 | } | ||
| 483 | |||
| 484 | BIO *BIO_new_accept(str) | ||
| 485 | char *str; | ||
| 486 | { | ||
| 487 | BIO *ret; | ||
| 488 | |||
| 489 | ret=BIO_new(BIO_s_accept()); | ||
| 490 | if (ret == NULL) return(NULL); | ||
| 491 | if (BIO_set_accept_port(ret,str)) | ||
| 492 | return(ret); | ||
| 493 | else | ||
| 494 | { | ||
| 495 | BIO_free(ret); | ||
| 496 | return(NULL); | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | #endif | ||
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c new file mode 100644 index 0000000000..6e547bf866 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_conn.c | |||
| @@ -0,0 +1,648 @@ | |||
| 1 | /* crypto/bio/bss_conn.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #ifndef NO_SOCK | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <errno.h> | ||
| 63 | #define USE_SOCKETS | ||
| 64 | #include "cryptlib.h" | ||
| 65 | #include "bio.h" | ||
| 66 | |||
| 67 | /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ | ||
| 68 | |||
| 69 | #ifdef WIN16 | ||
| 70 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | ||
| 71 | #else | ||
| 72 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
| 73 | #endif | ||
| 74 | |||
| 75 | typedef struct bio_connect_st | ||
| 76 | { | ||
| 77 | int state; | ||
| 78 | |||
| 79 | char *param_hostname; | ||
| 80 | char *param_port; | ||
| 81 | int nbio; | ||
| 82 | |||
| 83 | unsigned char ip[4]; | ||
| 84 | short port; | ||
| 85 | |||
| 86 | struct sockaddr_in them; | ||
| 87 | |||
| 88 | /* int socket; this will be kept in bio->num so that it is | ||
| 89 | * compatable with the bss_sock bio */ | ||
| 90 | int error; | ||
| 91 | |||
| 92 | /* called when the connection is initially made | ||
| 93 | * callback(BIO,state,ret); The callback should return | ||
| 94 | * 'ret'. state is for compatablity with the ssl info_callback */ | ||
| 95 | int (*info_callback)(); | ||
| 96 | } BIO_CONNECT; | ||
| 97 | |||
| 98 | #ifndef NOPROTO | ||
| 99 | static int conn_write(BIO *h,char *buf,int num); | ||
| 100 | static int conn_read(BIO *h,char *buf,int size); | ||
| 101 | static int conn_puts(BIO *h,char *str); | ||
| 102 | static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 103 | static int conn_new(BIO *h); | ||
| 104 | static int conn_free(BIO *data); | ||
| 105 | #else | ||
| 106 | static int conn_write(); | ||
| 107 | static int conn_read(); | ||
| 108 | static int conn_puts(); | ||
| 109 | static long conn_ctrl(); | ||
| 110 | static int conn_new(); | ||
| 111 | static int conn_free(); | ||
| 112 | #endif | ||
| 113 | |||
| 114 | #ifndef NOPROTO | ||
| 115 | |||
| 116 | static int conn_state(BIO *b, BIO_CONNECT *c); | ||
| 117 | static void conn_close_socket(BIO *data); | ||
| 118 | BIO_CONNECT *BIO_CONNECT_new(void ); | ||
| 119 | void BIO_CONNECT_free(BIO_CONNECT *a); | ||
| 120 | |||
| 121 | #else | ||
| 122 | |||
| 123 | static int conn_state(); | ||
| 124 | static void conn_close_socket(); | ||
| 125 | BIO_CONNECT *BIO_CONNECT_new(); | ||
| 126 | void BIO_CONNECT_free(); | ||
| 127 | |||
| 128 | #endif | ||
| 129 | |||
| 130 | static BIO_METHOD methods_connectp= | ||
| 131 | { | ||
| 132 | BIO_TYPE_CONNECT, | ||
| 133 | "socket connect", | ||
| 134 | conn_write, | ||
| 135 | conn_read, | ||
| 136 | conn_puts, | ||
| 137 | NULL, /* connect_gets, */ | ||
| 138 | conn_ctrl, | ||
| 139 | conn_new, | ||
| 140 | conn_free, | ||
| 141 | }; | ||
| 142 | |||
| 143 | static int conn_state(b,c) | ||
| 144 | BIO *b; | ||
| 145 | BIO_CONNECT *c; | ||
| 146 | { | ||
| 147 | int ret= -1,i; | ||
| 148 | unsigned long l; | ||
| 149 | char *p,*q; | ||
| 150 | int (*cb)()=NULL; | ||
| 151 | |||
| 152 | if (c->info_callback != NULL) | ||
| 153 | cb=c->info_callback; | ||
| 154 | |||
| 155 | for (;;) | ||
| 156 | { | ||
| 157 | switch (c->state) | ||
| 158 | { | ||
| 159 | case BIO_CONN_S_BEFORE: | ||
| 160 | p=c->param_hostname; | ||
| 161 | if (p == NULL) | ||
| 162 | { | ||
| 163 | BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTHNAME_SPECIFIED); | ||
| 164 | goto exit_loop; | ||
| 165 | } | ||
| 166 | for ( ; *p != '\0'; p++) | ||
| 167 | { | ||
| 168 | if ((*p == ':') || (*p == '/')) break; | ||
| 169 | } | ||
| 170 | |||
| 171 | i= *p; | ||
| 172 | if ((i == ':') || (i == '/')) | ||
| 173 | { | ||
| 174 | |||
| 175 | *(p++)='\0'; | ||
| 176 | if (i == ':') | ||
| 177 | { | ||
| 178 | for (q=p; *q; q++) | ||
| 179 | if (*q == '/') | ||
| 180 | { | ||
| 181 | *q='\0'; | ||
| 182 | break; | ||
| 183 | } | ||
| 184 | if (c->param_port != NULL) | ||
| 185 | Free(c->param_port); | ||
| 186 | c->param_port=BUF_strdup(p); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | if (p == NULL) | ||
| 191 | { | ||
| 192 | BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED); | ||
| 193 | ERR_add_error_data(2,"host=",c->param_hostname); | ||
| 194 | goto exit_loop; | ||
| 195 | } | ||
| 196 | c->state=BIO_CONN_S_GET_IP; | ||
| 197 | break; | ||
| 198 | |||
| 199 | case BIO_CONN_S_GET_IP: | ||
| 200 | if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0) | ||
| 201 | goto exit_loop; | ||
| 202 | c->state=BIO_CONN_S_GET_PORT; | ||
| 203 | break; | ||
| 204 | |||
| 205 | case BIO_CONN_S_GET_PORT: | ||
| 206 | if (BIO_get_port(c->param_port,&c->port) <= 0) | ||
| 207 | goto exit_loop; | ||
| 208 | c->state=BIO_CONN_S_CREATE_SOCKET; | ||
| 209 | break; | ||
| 210 | |||
| 211 | case BIO_CONN_S_CREATE_SOCKET: | ||
| 212 | /* now setup address */ | ||
| 213 | memset((char *)&c->them,0,sizeof(c->them)); | ||
| 214 | c->them.sin_family=AF_INET; | ||
| 215 | c->them.sin_port=htons((unsigned short)c->port); | ||
| 216 | l=(unsigned long) | ||
| 217 | ((unsigned long)c->ip[0]<<24L)| | ||
| 218 | ((unsigned long)c->ip[1]<<16L)| | ||
| 219 | ((unsigned long)c->ip[2]<< 8L)| | ||
| 220 | ((unsigned long)c->ip[3]); | ||
| 221 | c->them.sin_addr.s_addr=htonl(l); | ||
| 222 | c->state=BIO_CONN_S_CREATE_SOCKET; | ||
| 223 | |||
| 224 | ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); | ||
| 225 | if (ret == INVALID_SOCKET) | ||
| 226 | { | ||
| 227 | SYSerr(SYS_F_SOCKET,get_last_socket_error()); | ||
| 228 | ERR_add_error_data(4,"host=",c->param_hostname, | ||
| 229 | ":",c->param_port); | ||
| 230 | BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET); | ||
| 231 | goto exit_loop; | ||
| 232 | } | ||
| 233 | b->num=ret; | ||
| 234 | c->state=BIO_CONN_S_NBIO; | ||
| 235 | break; | ||
| 236 | |||
| 237 | case BIO_CONN_S_NBIO: | ||
| 238 | #ifdef FIONBIO | ||
| 239 | if (c->nbio) | ||
| 240 | { | ||
| 241 | l=1; | ||
| 242 | ret=BIO_socket_ioctl(b->num,FIONBIO,&l); | ||
| 243 | if (ret < 0) | ||
| 244 | { | ||
| 245 | BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO); | ||
| 246 | ERR_add_error_data(4,"host=", | ||
| 247 | c->param_hostname, | ||
| 248 | ":",c->param_port); | ||
| 249 | goto exit_loop; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | #endif | ||
| 253 | c->state=BIO_CONN_S_CONNECT; | ||
| 254 | |||
| 255 | #ifdef SO_KEEPALIVE | ||
| 256 | i=1; | ||
| 257 | i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); | ||
| 258 | if (i < 0) | ||
| 259 | { | ||
| 260 | SYSerr(SYS_F_SOCKET,get_last_socket_error()); | ||
| 261 | ERR_add_error_data(4,"host=",c->param_hostname, | ||
| 262 | ":",c->param_port); | ||
| 263 | BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE); | ||
| 264 | goto exit_loop; | ||
| 265 | } | ||
| 266 | #endif | ||
| 267 | break; | ||
| 268 | |||
| 269 | case BIO_CONN_S_CONNECT: | ||
| 270 | BIO_clear_retry_flags(b); | ||
| 271 | ret=connect(b->num, | ||
| 272 | (struct sockaddr *)&c->them, | ||
| 273 | sizeof(c->them)); | ||
| 274 | b->retry_reason=0; | ||
| 275 | if (ret < 0) | ||
| 276 | { | ||
| 277 | if (BIO_sock_should_retry(ret)) | ||
| 278 | { | ||
| 279 | BIO_set_retry_special(b); | ||
| 280 | c->state=BIO_CONN_S_BLOCKED_CONNECT; | ||
| 281 | b->retry_reason=BIO_RR_CONNECT; | ||
| 282 | } | ||
| 283 | else | ||
| 284 | { | ||
| 285 | SYSerr(SYS_F_CONNECT,get_last_socket_error()); | ||
| 286 | ERR_add_error_data(4,"host=", | ||
| 287 | c->param_hostname, | ||
| 288 | ":",c->param_port); | ||
| 289 | BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR); | ||
| 290 | } | ||
| 291 | goto exit_loop; | ||
| 292 | } | ||
| 293 | else | ||
| 294 | c->state=BIO_CONN_S_OK; | ||
| 295 | break; | ||
| 296 | |||
| 297 | case BIO_CONN_S_BLOCKED_CONNECT: | ||
| 298 | i=BIO_sock_error(b->num); | ||
| 299 | if (i) | ||
| 300 | { | ||
| 301 | BIO_clear_retry_flags(b); | ||
| 302 | SYSerr(SYS_F_CONNECT,i); | ||
| 303 | ERR_add_error_data(4,"host=", | ||
| 304 | c->param_hostname, | ||
| 305 | ":",c->param_port); | ||
| 306 | BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR); | ||
| 307 | ret=0; | ||
| 308 | goto exit_loop; | ||
| 309 | } | ||
| 310 | else | ||
| 311 | c->state=BIO_CONN_S_OK; | ||
| 312 | break; | ||
| 313 | |||
| 314 | case BIO_CONN_S_OK: | ||
| 315 | ret=1; | ||
| 316 | goto exit_loop; | ||
| 317 | default: | ||
| 318 | abort(); | ||
| 319 | goto exit_loop; | ||
| 320 | } | ||
| 321 | |||
| 322 | if (cb != NULL) | ||
| 323 | { | ||
| 324 | if (!(ret=cb((BIO *)b,c->state,ret))) | ||
| 325 | goto end; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | if (1) | ||
| 330 | { | ||
| 331 | exit_loop: | ||
| 332 | if (cb != NULL) | ||
| 333 | ret=cb((BIO *)b,c->state,ret); | ||
| 334 | } | ||
| 335 | end: | ||
| 336 | return(ret); | ||
| 337 | } | ||
| 338 | |||
| 339 | BIO_CONNECT *BIO_CONNECT_new() | ||
| 340 | { | ||
| 341 | BIO_CONNECT *ret; | ||
| 342 | |||
| 343 | if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL) | ||
| 344 | return(NULL); | ||
| 345 | ret->state=BIO_CONN_S_BEFORE; | ||
| 346 | ret->param_hostname=NULL; | ||
| 347 | ret->param_port=NULL; | ||
| 348 | ret->info_callback=NULL; | ||
| 349 | ret->nbio=0; | ||
| 350 | ret->ip[0]=0; | ||
| 351 | ret->ip[1]=0; | ||
| 352 | ret->ip[2]=0; | ||
| 353 | ret->ip[3]=0; | ||
| 354 | ret->port=0; | ||
| 355 | memset((char *)&ret->them,0,sizeof(ret->them)); | ||
| 356 | ret->error=0; | ||
| 357 | return(ret); | ||
| 358 | } | ||
| 359 | |||
| 360 | void BIO_CONNECT_free(a) | ||
| 361 | BIO_CONNECT *a; | ||
| 362 | { | ||
| 363 | if (a->param_hostname != NULL) | ||
| 364 | Free(a->param_hostname); | ||
| 365 | if (a->param_port != NULL) | ||
| 366 | Free(a->param_port); | ||
| 367 | Free(a); | ||
| 368 | } | ||
| 369 | |||
| 370 | BIO_METHOD *BIO_s_connect() | ||
| 371 | { | ||
| 372 | return(&methods_connectp); | ||
| 373 | } | ||
| 374 | |||
| 375 | static int conn_new(bi) | ||
| 376 | BIO *bi; | ||
| 377 | { | ||
| 378 | bi->init=0; | ||
| 379 | bi->num=INVALID_SOCKET; | ||
| 380 | bi->flags=0; | ||
| 381 | if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL) | ||
| 382 | return(0); | ||
| 383 | else | ||
| 384 | return(1); | ||
| 385 | } | ||
| 386 | |||
| 387 | static void conn_close_socket(bio) | ||
| 388 | BIO *bio; | ||
| 389 | { | ||
| 390 | BIO_CONNECT *c; | ||
| 391 | |||
| 392 | c=(BIO_CONNECT *)bio->ptr; | ||
| 393 | if (bio->num != INVALID_SOCKET) | ||
| 394 | { | ||
| 395 | /* Only do a shutdown if things were established */ | ||
| 396 | if (c->state == BIO_CONN_S_OK) | ||
| 397 | shutdown(bio->num,2); | ||
| 398 | # ifdef WINDOWS | ||
| 399 | closesocket(bio->num); | ||
| 400 | # else | ||
| 401 | close(bio->num); | ||
| 402 | # endif | ||
| 403 | bio->num=INVALID_SOCKET; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | static int conn_free(a) | ||
| 408 | BIO *a; | ||
| 409 | { | ||
| 410 | BIO_CONNECT *data; | ||
| 411 | |||
| 412 | if (a == NULL) return(0); | ||
| 413 | data=(BIO_CONNECT *)a->ptr; | ||
| 414 | |||
| 415 | if (a->shutdown) | ||
| 416 | { | ||
| 417 | conn_close_socket(a); | ||
| 418 | BIO_CONNECT_free(data); | ||
| 419 | a->ptr=NULL; | ||
| 420 | a->flags=0; | ||
| 421 | a->init=0; | ||
| 422 | } | ||
| 423 | return(1); | ||
| 424 | } | ||
| 425 | |||
| 426 | static int conn_read(b,out,outl) | ||
| 427 | BIO *b; | ||
| 428 | char *out; | ||
| 429 | int outl; | ||
| 430 | { | ||
| 431 | int ret=0; | ||
| 432 | BIO_CONNECT *data; | ||
| 433 | |||
| 434 | data=(BIO_CONNECT *)b->ptr; | ||
| 435 | if (data->state != BIO_CONN_S_OK) | ||
| 436 | { | ||
| 437 | ret=conn_state(b,data); | ||
| 438 | if (ret <= 0) | ||
| 439 | return(ret); | ||
| 440 | } | ||
| 441 | |||
| 442 | if (out != NULL) | ||
| 443 | { | ||
| 444 | clear_socket_error(); | ||
| 445 | #if defined(WINDOWS) | ||
| 446 | ret=recv(b->num,out,outl,0); | ||
| 447 | #else | ||
| 448 | ret=read(b->num,out,outl); | ||
| 449 | #endif | ||
| 450 | BIO_clear_retry_flags(b); | ||
| 451 | if (ret <= 0) | ||
| 452 | { | ||
| 453 | if (BIO_sock_should_retry(ret)) | ||
| 454 | BIO_set_retry_read(b); | ||
| 455 | } | ||
| 456 | } | ||
| 457 | return(ret); | ||
| 458 | } | ||
| 459 | |||
| 460 | static int conn_write(b,in,inl) | ||
| 461 | BIO *b; | ||
| 462 | char *in; | ||
| 463 | int inl; | ||
| 464 | { | ||
| 465 | int ret; | ||
| 466 | BIO_CONNECT *data; | ||
| 467 | |||
| 468 | data=(BIO_CONNECT *)b->ptr; | ||
| 469 | if (data->state != BIO_CONN_S_OK) | ||
| 470 | { | ||
| 471 | ret=conn_state(b,data); | ||
| 472 | if (ret <= 0) return(ret); | ||
| 473 | } | ||
| 474 | |||
| 475 | clear_socket_error(); | ||
| 476 | #if defined(WINDOWS) | ||
| 477 | ret=send(b->num,in,inl,0); | ||
| 478 | #else | ||
| 479 | ret=write(b->num,in,inl); | ||
| 480 | #endif | ||
| 481 | BIO_clear_retry_flags(b); | ||
| 482 | if (ret <= 0) | ||
| 483 | { | ||
| 484 | if (BIO_sock_should_retry(ret)) | ||
| 485 | BIO_set_retry_write(b); | ||
| 486 | } | ||
| 487 | return(ret); | ||
| 488 | } | ||
| 489 | |||
| 490 | static long conn_ctrl(b,cmd,num,ptr) | ||
| 491 | BIO *b; | ||
| 492 | int cmd; | ||
| 493 | long num; | ||
| 494 | char *ptr; | ||
| 495 | { | ||
| 496 | BIO *dbio; | ||
| 497 | int *ip; | ||
| 498 | char **pptr; | ||
| 499 | long ret=1; | ||
| 500 | BIO_CONNECT *data; | ||
| 501 | |||
| 502 | data=(BIO_CONNECT *)b->ptr; | ||
| 503 | |||
| 504 | switch (cmd) | ||
| 505 | { | ||
| 506 | case BIO_CTRL_RESET: | ||
| 507 | ret=0; | ||
| 508 | data->state=BIO_CONN_S_BEFORE; | ||
| 509 | conn_close_socket(b); | ||
| 510 | b->flags=0; | ||
| 511 | break; | ||
| 512 | case BIO_C_DO_STATE_MACHINE: | ||
| 513 | /* use this one to start the connection */ | ||
| 514 | if (!data->state != BIO_CONN_S_OK) | ||
| 515 | ret=(long)conn_state(b,data); | ||
| 516 | else | ||
| 517 | ret=1; | ||
| 518 | break; | ||
| 519 | case BIO_C_GET_CONNECT: | ||
| 520 | if (ptr != NULL) | ||
| 521 | { | ||
| 522 | pptr=(char **)ptr; | ||
| 523 | if (num == 0) | ||
| 524 | { | ||
| 525 | *pptr=data->param_hostname; | ||
| 526 | |||
| 527 | } | ||
| 528 | else if (num == 1) | ||
| 529 | { | ||
| 530 | *pptr=data->param_port; | ||
| 531 | } | ||
| 532 | else if (num == 2) | ||
| 533 | { | ||
| 534 | *pptr= (char *)&(data->ip[0]); | ||
| 535 | } | ||
| 536 | else if (num == 3) | ||
| 537 | { | ||
| 538 | *((int *)ptr)=data->port; | ||
| 539 | } | ||
| 540 | if ((!b->init) || (ptr == NULL)) | ||
| 541 | *pptr="not initalised"; | ||
| 542 | ret=1; | ||
| 543 | } | ||
| 544 | break; | ||
| 545 | case BIO_C_SET_CONNECT: | ||
| 546 | if (ptr != NULL) | ||
| 547 | { | ||
| 548 | b->init=1; | ||
| 549 | if (num == 0) | ||
| 550 | { | ||
| 551 | if (data->param_hostname != NULL) | ||
| 552 | Free(data->param_hostname); | ||
| 553 | data->param_hostname=BUF_strdup(ptr); | ||
| 554 | } | ||
| 555 | else if (num == 1) | ||
| 556 | { | ||
| 557 | if (data->param_port != NULL) | ||
| 558 | Free(data->param_port); | ||
| 559 | data->param_port=BUF_strdup(ptr); | ||
| 560 | } | ||
| 561 | else if (num == 2) | ||
| 562 | memcpy(data->ip,ptr,4); | ||
| 563 | else if (num == 3) | ||
| 564 | data->port= *(int *)ptr; | ||
| 565 | } | ||
| 566 | break; | ||
| 567 | case BIO_C_SET_NBIO: | ||
| 568 | data->nbio=(int)num; | ||
| 569 | break; | ||
| 570 | case BIO_C_GET_FD: | ||
| 571 | if (b->init) | ||
| 572 | { | ||
| 573 | ip=(int *)ptr; | ||
| 574 | if (ip != NULL) | ||
| 575 | *ip=b->num; | ||
| 576 | ret=b->num; | ||
| 577 | } | ||
| 578 | else | ||
| 579 | ret= -1; | ||
| 580 | break; | ||
| 581 | case BIO_CTRL_GET_CLOSE: | ||
| 582 | ret=b->shutdown; | ||
| 583 | break; | ||
| 584 | case BIO_CTRL_SET_CLOSE: | ||
| 585 | b->shutdown=(int)num; | ||
| 586 | break; | ||
| 587 | case BIO_CTRL_PENDING: | ||
| 588 | case BIO_CTRL_WPENDING: | ||
| 589 | ret=0; | ||
| 590 | break; | ||
| 591 | case BIO_CTRL_FLUSH: | ||
| 592 | break; | ||
| 593 | case BIO_CTRL_DUP: | ||
| 594 | dbio=(BIO *)ptr; | ||
| 595 | if (data->param_port) | ||
| 596 | BIO_set_conn_port(dbio,data->param_port); | ||
| 597 | if (data->param_hostname) | ||
| 598 | BIO_set_conn_hostname(dbio,data->param_hostname); | ||
| 599 | BIO_set_nbio(dbio,data->nbio); | ||
| 600 | BIO_set_info_callback(dbio,data->info_callback); | ||
| 601 | break; | ||
| 602 | case BIO_CTRL_SET_CALLBACK: | ||
| 603 | data->info_callback=(int (*)())ptr; | ||
| 604 | break; | ||
| 605 | case BIO_CTRL_GET_CALLBACK: | ||
| 606 | { | ||
| 607 | int (**fptr)(); | ||
| 608 | |||
| 609 | fptr=(int (**)())ptr; | ||
| 610 | *fptr=data->info_callback; | ||
| 611 | } | ||
| 612 | break; | ||
| 613 | default: | ||
| 614 | ret=0; | ||
| 615 | break; | ||
| 616 | } | ||
| 617 | return(ret); | ||
| 618 | } | ||
| 619 | |||
| 620 | static int conn_puts(bp,str) | ||
| 621 | BIO *bp; | ||
| 622 | char *str; | ||
| 623 | { | ||
| 624 | int n,ret; | ||
| 625 | |||
| 626 | n=strlen(str); | ||
| 627 | ret=conn_write(bp,str,n); | ||
| 628 | return(ret); | ||
| 629 | } | ||
| 630 | |||
| 631 | BIO *BIO_new_connect(str) | ||
| 632 | char *str; | ||
| 633 | { | ||
| 634 | BIO *ret; | ||
| 635 | |||
| 636 | ret=BIO_new(BIO_s_connect()); | ||
| 637 | if (ret == NULL) return(NULL); | ||
| 638 | if (BIO_set_conn_hostname(ret,str)) | ||
| 639 | return(ret); | ||
| 640 | else | ||
| 641 | { | ||
| 642 | BIO_free(ret); | ||
| 643 | return(NULL); | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | #endif | ||
| 648 | |||
diff --git a/src/lib/libcrypto/bio/bss_fd.c b/src/lib/libcrypto/bio/bss_fd.c new file mode 100644 index 0000000000..686c4909a2 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_fd.c | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | /* crypto/bio/bss_fd.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #define BIO_FD | ||
| 60 | #include "bss_sock.c" | ||
| 61 | #undef BIO_FD | ||
| 62 | |||
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c new file mode 100644 index 0000000000..1484cf849e --- /dev/null +++ b/src/lib/libcrypto/bio/bss_file.c | |||
| @@ -0,0 +1,339 @@ | |||
| 1 | /* crypto/bio/bss_file.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout | ||
| 61 | * with binary data (e.g. asn1parse -inform DER < xxx) under | ||
| 62 | * Windows | ||
| 63 | */ | ||
| 64 | |||
| 65 | #ifndef HEADER_BSS_FILE_C | ||
| 66 | #define HEADER_BSS_FILE_C | ||
| 67 | |||
| 68 | #include <stdio.h> | ||
| 69 | #include <errno.h> | ||
| 70 | #include "cryptlib.h" | ||
| 71 | #include "bio.h" | ||
| 72 | #include "err.h" | ||
| 73 | |||
| 74 | #if !defined(NO_STDIO) | ||
| 75 | |||
| 76 | #ifndef NOPROTO | ||
| 77 | static int MS_CALLBACK file_write(BIO *h,char *buf,int num); | ||
| 78 | static int MS_CALLBACK file_read(BIO *h,char *buf,int size); | ||
| 79 | static int MS_CALLBACK file_puts(BIO *h,char *str); | ||
| 80 | static int MS_CALLBACK file_gets(BIO *h,char *str,int size); | ||
| 81 | static long MS_CALLBACK file_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 82 | static int MS_CALLBACK file_new(BIO *h); | ||
| 83 | static int MS_CALLBACK file_free(BIO *data); | ||
| 84 | #else | ||
| 85 | static int MS_CALLBACK file_write(); | ||
| 86 | static int MS_CALLBACK file_read(); | ||
| 87 | static int MS_CALLBACK file_puts(); | ||
| 88 | static int MS_CALLBACK file_gets(); | ||
| 89 | static long MS_CALLBACK file_ctrl(); | ||
| 90 | static int MS_CALLBACK file_new(); | ||
| 91 | static int MS_CALLBACK file_free(); | ||
| 92 | #endif | ||
| 93 | |||
| 94 | static BIO_METHOD methods_filep= | ||
| 95 | { | ||
| 96 | BIO_TYPE_FILE, | ||
| 97 | "FILE pointer", | ||
| 98 | file_write, | ||
| 99 | file_read, | ||
| 100 | file_puts, | ||
| 101 | file_gets, | ||
| 102 | file_ctrl, | ||
| 103 | file_new, | ||
| 104 | file_free, | ||
| 105 | }; | ||
| 106 | |||
| 107 | BIO *BIO_new_file(filename,mode) | ||
| 108 | char *filename; | ||
| 109 | char *mode; | ||
| 110 | { | ||
| 111 | BIO *ret; | ||
| 112 | FILE *file; | ||
| 113 | |||
| 114 | if ((file=fopen(filename,mode)) == NULL) | ||
| 115 | { | ||
| 116 | SYSerr(SYS_F_FOPEN,get_last_sys_error()); | ||
| 117 | ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); | ||
| 118 | BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); | ||
| 119 | return(NULL); | ||
| 120 | } | ||
| 121 | if ((ret=BIO_new(BIO_s_file_internal())) == NULL) | ||
| 122 | return(NULL); | ||
| 123 | |||
| 124 | BIO_set_fp(ret,file,BIO_CLOSE); | ||
| 125 | return(ret); | ||
| 126 | } | ||
| 127 | |||
| 128 | BIO *BIO_new_fp(stream,close_flag) | ||
| 129 | FILE *stream; | ||
| 130 | int close_flag; | ||
| 131 | { | ||
| 132 | BIO *ret; | ||
| 133 | |||
| 134 | if ((ret=BIO_new(BIO_s_file())) == NULL) | ||
| 135 | return(NULL); | ||
| 136 | |||
| 137 | BIO_set_fp(ret,stream,close_flag); | ||
| 138 | return(ret); | ||
| 139 | } | ||
| 140 | |||
| 141 | BIO_METHOD *BIO_s_file() | ||
| 142 | { | ||
| 143 | return(&methods_filep); | ||
| 144 | } | ||
| 145 | |||
| 146 | static int MS_CALLBACK file_new(bi) | ||
| 147 | BIO *bi; | ||
| 148 | { | ||
| 149 | bi->init=0; | ||
| 150 | bi->num=0; | ||
| 151 | bi->ptr=NULL; | ||
| 152 | return(1); | ||
| 153 | } | ||
| 154 | |||
| 155 | static int MS_CALLBACK file_free(a) | ||
| 156 | BIO *a; | ||
| 157 | { | ||
| 158 | if (a == NULL) return(0); | ||
| 159 | if (a->shutdown) | ||
| 160 | { | ||
| 161 | if ((a->init) && (a->ptr != NULL)) | ||
| 162 | { | ||
| 163 | fclose((FILE *)a->ptr); | ||
| 164 | a->ptr=NULL; | ||
| 165 | } | ||
| 166 | a->init=0; | ||
| 167 | } | ||
| 168 | return(1); | ||
| 169 | } | ||
| 170 | |||
| 171 | static int MS_CALLBACK file_read(b,out,outl) | ||
| 172 | BIO *b; | ||
| 173 | char *out; | ||
| 174 | int outl; | ||
| 175 | { | ||
| 176 | int ret=0; | ||
| 177 | |||
| 178 | if (b->init && (out != NULL)) | ||
| 179 | { | ||
| 180 | ret=fread(out,1,(int)outl,(FILE *)b->ptr); | ||
| 181 | } | ||
| 182 | return(ret); | ||
| 183 | } | ||
| 184 | |||
| 185 | static int MS_CALLBACK file_write(b,in,inl) | ||
| 186 | BIO *b; | ||
| 187 | char *in; | ||
| 188 | int inl; | ||
| 189 | { | ||
| 190 | int ret=0; | ||
| 191 | |||
| 192 | if (b->init && (in != NULL)) | ||
| 193 | { | ||
| 194 | if (fwrite(in,(int)inl,1,(FILE *)b->ptr)) | ||
| 195 | ret=inl; | ||
| 196 | /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ | ||
| 197 | /* acording to Tim Hudson <tjh@cryptsoft.com>, the commented | ||
| 198 | * out version above can cause 'inl' write calls under | ||
| 199 | * some stupid stdio implementations (VMS) */ | ||
| 200 | } | ||
| 201 | return(ret); | ||
| 202 | } | ||
| 203 | |||
| 204 | static long MS_CALLBACK file_ctrl(b,cmd,num,ptr) | ||
| 205 | BIO *b; | ||
| 206 | int cmd; | ||
| 207 | long num; | ||
| 208 | char *ptr; | ||
| 209 | { | ||
| 210 | long ret=1; | ||
| 211 | FILE *fp=(FILE *)b->ptr; | ||
| 212 | FILE **fpp; | ||
| 213 | char p[4]; | ||
| 214 | |||
| 215 | switch (cmd) | ||
| 216 | { | ||
| 217 | case BIO_CTRL_RESET: | ||
| 218 | ret=(long)fseek(fp,num,0); | ||
| 219 | break; | ||
| 220 | case BIO_CTRL_EOF: | ||
| 221 | ret=(long)feof(fp); | ||
| 222 | break; | ||
| 223 | case BIO_CTRL_INFO: | ||
| 224 | ret=ftell(fp); | ||
| 225 | break; | ||
| 226 | case BIO_C_SET_FILE_PTR: | ||
| 227 | file_free(b); | ||
| 228 | b->shutdown=(int)num; | ||
| 229 | b->ptr=(char *)ptr; | ||
| 230 | b->init=1; | ||
| 231 | #if defined(MSDOS) || defined(WINDOWS) | ||
| 232 | /* Set correct text/binary mode */ | ||
| 233 | if (num & BIO_FP_TEXT) | ||
| 234 | _setmode(fileno((FILE *)ptr),_O_TEXT); | ||
| 235 | else | ||
| 236 | _setmode(fileno((FILE *)ptr),_O_BINARY); | ||
| 237 | #endif | ||
| 238 | break; | ||
| 239 | case BIO_C_SET_FILENAME: | ||
| 240 | file_free(b); | ||
| 241 | b->shutdown=(int)num&BIO_CLOSE; | ||
| 242 | if (num & BIO_FP_APPEND) | ||
| 243 | { | ||
| 244 | if (num & BIO_FP_READ) | ||
| 245 | strcpy(p,"a+"); | ||
| 246 | else strcpy(p,"a"); | ||
| 247 | } | ||
| 248 | else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) | ||
| 249 | strcpy(p,"r+"); | ||
| 250 | else if (num & BIO_FP_WRITE) | ||
| 251 | strcpy(p,"w"); | ||
| 252 | else if (num & BIO_FP_READ) | ||
| 253 | strcpy(p,"r"); | ||
| 254 | else | ||
| 255 | { | ||
| 256 | BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE); | ||
| 257 | ret=0; | ||
| 258 | break; | ||
| 259 | } | ||
| 260 | #if defined(MSDOS) || defined(WINDOWS) | ||
| 261 | if (!(num & BIO_FP_TEXT)) | ||
| 262 | strcat(p,"b"); | ||
| 263 | else | ||
| 264 | strcat(p,"t"); | ||
| 265 | #endif | ||
| 266 | fp=fopen(ptr,p); | ||
| 267 | if (fp == NULL) | ||
| 268 | { | ||
| 269 | SYSerr(SYS_F_FOPEN,get_last_sys_error()); | ||
| 270 | ERR_add_error_data(5,"fopen('",ptr,"','",p,"')"); | ||
| 271 | BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB); | ||
| 272 | ret=0; | ||
| 273 | break; | ||
| 274 | } | ||
| 275 | b->ptr=(char *)fp; | ||
| 276 | b->init=1; | ||
| 277 | break; | ||
| 278 | case BIO_C_GET_FILE_PTR: | ||
| 279 | /* the ptr parameter is actually a FILE ** in this case. */ | ||
| 280 | if (ptr != NULL) | ||
| 281 | { | ||
| 282 | fpp=(FILE **)ptr; | ||
| 283 | *fpp=(FILE *)b->ptr; | ||
| 284 | } | ||
| 285 | break; | ||
| 286 | case BIO_CTRL_GET_CLOSE: | ||
| 287 | ret=(long)b->shutdown; | ||
| 288 | break; | ||
| 289 | case BIO_CTRL_SET_CLOSE: | ||
| 290 | b->shutdown=(int)num; | ||
| 291 | break; | ||
| 292 | case BIO_CTRL_FLUSH: | ||
| 293 | fflush((FILE *)b->ptr); | ||
| 294 | break; | ||
| 295 | case BIO_CTRL_DUP: | ||
| 296 | ret=1; | ||
| 297 | break; | ||
| 298 | |||
| 299 | case BIO_CTRL_WPENDING: | ||
| 300 | case BIO_CTRL_PENDING: | ||
| 301 | case BIO_CTRL_PUSH: | ||
| 302 | case BIO_CTRL_POP: | ||
| 303 | default: | ||
| 304 | ret=0; | ||
| 305 | break; | ||
| 306 | } | ||
| 307 | return(ret); | ||
| 308 | } | ||
| 309 | |||
| 310 | static int MS_CALLBACK file_gets(bp,buf,size) | ||
| 311 | BIO *bp; | ||
| 312 | char *buf; | ||
| 313 | int size; | ||
| 314 | { | ||
| 315 | int ret=0; | ||
| 316 | |||
| 317 | buf[0]='\0'; | ||
| 318 | fgets(buf,size,(FILE *)bp->ptr); | ||
| 319 | if (buf[0] != '\0') | ||
| 320 | ret=strlen(buf); | ||
| 321 | return(ret); | ||
| 322 | } | ||
| 323 | |||
| 324 | static int MS_CALLBACK file_puts(bp,str) | ||
| 325 | BIO *bp; | ||
| 326 | char *str; | ||
| 327 | { | ||
| 328 | int n,ret; | ||
| 329 | |||
| 330 | n=strlen(str); | ||
| 331 | ret=file_write(bp,str,n); | ||
| 332 | return(ret); | ||
| 333 | } | ||
| 334 | |||
| 335 | #endif /* NO_STDIO */ | ||
| 336 | |||
| 337 | #endif /* HEADER_BSS_FILE_C */ | ||
| 338 | |||
| 339 | |||
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c new file mode 100644 index 0000000000..40c4e39f02 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_mem.c | |||
| @@ -0,0 +1,297 @@ | |||
| 1 | /* crypto/bio/bss_mem.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include "bio.h" | ||
| 63 | |||
| 64 | #ifndef NOPROTO | ||
| 65 | static int mem_write(BIO *h,char *buf,int num); | ||
| 66 | static int mem_read(BIO *h,char *buf,int size); | ||
| 67 | static int mem_puts(BIO *h,char *str); | ||
| 68 | static int mem_gets(BIO *h,char *str,int size); | ||
| 69 | static long mem_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 70 | static int mem_new(BIO *h); | ||
| 71 | static int mem_free(BIO *data); | ||
| 72 | #else | ||
| 73 | static int mem_write(); | ||
| 74 | static int mem_read(); | ||
| 75 | static int mem_puts(); | ||
| 76 | static int mem_gets(); | ||
| 77 | static long mem_ctrl(); | ||
| 78 | static int mem_new(); | ||
| 79 | static int mem_free(); | ||
| 80 | #endif | ||
| 81 | |||
| 82 | static BIO_METHOD mem_method= | ||
| 83 | { | ||
| 84 | BIO_TYPE_MEM, | ||
| 85 | "memory buffer", | ||
| 86 | mem_write, | ||
| 87 | mem_read, | ||
| 88 | mem_puts, | ||
| 89 | mem_gets, | ||
| 90 | mem_ctrl, | ||
| 91 | mem_new, | ||
| 92 | mem_free, | ||
| 93 | }; | ||
| 94 | |||
| 95 | BIO_METHOD *BIO_s_mem() | ||
| 96 | { | ||
| 97 | return(&mem_method); | ||
| 98 | } | ||
| 99 | |||
| 100 | static int mem_new(bi) | ||
| 101 | BIO *bi; | ||
| 102 | { | ||
| 103 | BUF_MEM *b; | ||
| 104 | |||
| 105 | if ((b=BUF_MEM_new()) == NULL) | ||
| 106 | return(0); | ||
| 107 | bi->shutdown=1; | ||
| 108 | bi->init=1; | ||
| 109 | bi->num=0; | ||
| 110 | bi->ptr=(char *)b; | ||
| 111 | return(1); | ||
| 112 | } | ||
| 113 | |||
| 114 | static int mem_free(a) | ||
| 115 | BIO *a; | ||
| 116 | { | ||
| 117 | if (a == NULL) return(0); | ||
| 118 | if (a->shutdown) | ||
| 119 | { | ||
| 120 | if ((a->init) && (a->ptr != NULL)) | ||
| 121 | { | ||
| 122 | BUF_MEM_free((BUF_MEM *)a->ptr); | ||
| 123 | a->ptr=NULL; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | return(1); | ||
| 127 | } | ||
| 128 | |||
| 129 | static int mem_read(b,out,outl) | ||
| 130 | BIO *b; | ||
| 131 | char *out; | ||
| 132 | int outl; | ||
| 133 | { | ||
| 134 | int ret= -1; | ||
| 135 | BUF_MEM *bm; | ||
| 136 | int i; | ||
| 137 | char *from,*to; | ||
| 138 | |||
| 139 | bm=(BUF_MEM *)b->ptr; | ||
| 140 | BIO_clear_retry_flags(b); | ||
| 141 | ret=(outl > bm->length)?bm->length:outl; | ||
| 142 | if ((out != NULL) && (ret > 0)) | ||
| 143 | { | ||
| 144 | memcpy(out,bm->data,ret); | ||
| 145 | bm->length-=ret; | ||
| 146 | /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */ | ||
| 147 | from=(char *)&(bm->data[ret]); | ||
| 148 | to=(char *)&(bm->data[0]); | ||
| 149 | for (i=0; i<bm->length; i++) | ||
| 150 | to[i]=from[i]; | ||
| 151 | } | ||
| 152 | else if (bm->length == 0) | ||
| 153 | { | ||
| 154 | BIO_set_retry_read(b); | ||
| 155 | ret= -1; | ||
| 156 | } | ||
| 157 | return(ret); | ||
| 158 | } | ||
| 159 | |||
| 160 | static int mem_write(b,in,inl) | ||
| 161 | BIO *b; | ||
| 162 | char *in; | ||
| 163 | int inl; | ||
| 164 | { | ||
| 165 | int ret= -1; | ||
| 166 | int blen; | ||
| 167 | BUF_MEM *bm; | ||
| 168 | |||
| 169 | bm=(BUF_MEM *)b->ptr; | ||
| 170 | if (in == NULL) | ||
| 171 | { | ||
| 172 | BIOerr(BIO_F_MEM_WRITE,BIO_R_NULL_PARAMETER); | ||
| 173 | goto end; | ||
| 174 | } | ||
| 175 | |||
| 176 | BIO_clear_retry_flags(b); | ||
| 177 | blen=bm->length; | ||
| 178 | if (BUF_MEM_grow(bm,blen+inl) != (blen+inl)) | ||
| 179 | goto end; | ||
| 180 | memcpy(&(bm->data[blen]),in,inl); | ||
| 181 | ret=inl; | ||
| 182 | end: | ||
| 183 | return(ret); | ||
| 184 | } | ||
| 185 | |||
| 186 | static long mem_ctrl(b,cmd,num,ptr) | ||
| 187 | BIO *b; | ||
| 188 | int cmd; | ||
| 189 | long num; | ||
| 190 | char *ptr; | ||
| 191 | { | ||
| 192 | long ret=1; | ||
| 193 | char **pptr; | ||
| 194 | |||
| 195 | BUF_MEM *bm=(BUF_MEM *)b->ptr; | ||
| 196 | |||
| 197 | switch (cmd) | ||
| 198 | { | ||
| 199 | case BIO_CTRL_RESET: | ||
| 200 | if (bm->data != NULL) | ||
| 201 | memset(bm->data,0,bm->max); | ||
| 202 | bm->length=0; | ||
| 203 | break; | ||
| 204 | case BIO_CTRL_EOF: | ||
| 205 | ret=(long)(bm->length == 0); | ||
| 206 | break; | ||
| 207 | case BIO_CTRL_INFO: | ||
| 208 | ret=(long)bm->length; | ||
| 209 | if (ptr != NULL) | ||
| 210 | { | ||
| 211 | pptr=(char **)ptr; | ||
| 212 | *pptr=(char *)&(bm->data[0]); | ||
| 213 | } | ||
| 214 | break; | ||
| 215 | case BIO_C_SET_BUF_MEM: | ||
| 216 | mem_free(b); | ||
| 217 | b->shutdown=(int)num; | ||
| 218 | b->ptr=ptr; | ||
| 219 | break; | ||
| 220 | case BIO_C_GET_BUF_MEM_PTR: | ||
| 221 | if (ptr != NULL) | ||
| 222 | { | ||
| 223 | pptr=(char **)ptr; | ||
| 224 | *pptr=(char *)bm; | ||
| 225 | } | ||
| 226 | break; | ||
| 227 | case BIO_CTRL_GET_CLOSE: | ||
| 228 | ret=(long)b->shutdown; | ||
| 229 | break; | ||
| 230 | case BIO_CTRL_SET_CLOSE: | ||
| 231 | b->shutdown=(int)num; | ||
| 232 | break; | ||
| 233 | |||
| 234 | case BIO_CTRL_WPENDING: | ||
| 235 | ret=0L; | ||
| 236 | break; | ||
| 237 | case BIO_CTRL_PENDING: | ||
| 238 | ret=(long)bm->length; | ||
| 239 | break; | ||
| 240 | case BIO_CTRL_DUP: | ||
| 241 | case BIO_CTRL_FLUSH: | ||
| 242 | ret=1; | ||
| 243 | break; | ||
| 244 | case BIO_CTRL_PUSH: | ||
| 245 | case BIO_CTRL_POP: | ||
| 246 | default: | ||
| 247 | ret=0; | ||
| 248 | break; | ||
| 249 | } | ||
| 250 | return(ret); | ||
| 251 | } | ||
| 252 | |||
| 253 | static int mem_gets(bp,buf,size) | ||
| 254 | BIO *bp; | ||
| 255 | char *buf; | ||
| 256 | int size; | ||
| 257 | { | ||
| 258 | int i,j; | ||
| 259 | int ret= -1; | ||
| 260 | char *p; | ||
| 261 | BUF_MEM *bm=(BUF_MEM *)bp->ptr; | ||
| 262 | |||
| 263 | BIO_clear_retry_flags(bp); | ||
| 264 | j=bm->length; | ||
| 265 | if (j <= 0) return(0); | ||
| 266 | p=bm->data; | ||
| 267 | for (i=0; i<j; i++) | ||
| 268 | { | ||
| 269 | if (p[i] == '\n') break; | ||
| 270 | } | ||
| 271 | if (i == j) | ||
| 272 | { | ||
| 273 | BIO_set_retry_read(bp); | ||
| 274 | /* return(-1); change the semantics 0.6.6a */ | ||
| 275 | } | ||
| 276 | else | ||
| 277 | i++; | ||
| 278 | /* i is the max to copy */ | ||
| 279 | if ((size-1) < i) i=size-1; | ||
| 280 | i=mem_read(bp,buf,i); | ||
| 281 | if (i > 0) buf[i]='\0'; | ||
| 282 | ret=i; | ||
| 283 | return(ret); | ||
| 284 | } | ||
| 285 | |||
| 286 | static int mem_puts(bp,str) | ||
| 287 | BIO *bp; | ||
| 288 | char *str; | ||
| 289 | { | ||
| 290 | int n,ret; | ||
| 291 | |||
| 292 | n=strlen(str); | ||
| 293 | ret=mem_write(bp,str,n); | ||
| 294 | /* memory semantics is that it will always work */ | ||
| 295 | return(ret); | ||
| 296 | } | ||
| 297 | |||
diff --git a/src/lib/libcrypto/bio/bss_null.c b/src/lib/libcrypto/bio/bss_null.c new file mode 100644 index 0000000000..0791a2471a --- /dev/null +++ b/src/lib/libcrypto/bio/bss_null.c | |||
| @@ -0,0 +1,177 @@ | |||
| 1 | /* crypto/bio/bss_null.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include "bio.h" | ||
| 63 | |||
| 64 | #ifndef NOPROTO | ||
| 65 | static int null_write(BIO *h,char *buf,int num); | ||
| 66 | static int null_read(BIO *h,char *buf,int size); | ||
| 67 | static int null_puts(BIO *h,char *str); | ||
| 68 | static int null_gets(BIO *h,char *str,int size); | ||
| 69 | static long null_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 70 | static int null_new(BIO *h); | ||
| 71 | static int null_free(BIO *data); | ||
| 72 | #else | ||
| 73 | static int null_write(); | ||
| 74 | static int null_read(); | ||
| 75 | static int null_puts(); | ||
| 76 | static int null_gets(); | ||
| 77 | static long null_ctrl(); | ||
| 78 | static int null_new(); | ||
| 79 | static int null_free(); | ||
| 80 | #endif | ||
| 81 | |||
| 82 | static BIO_METHOD null_method= | ||
| 83 | { | ||
| 84 | BIO_TYPE_NULL, | ||
| 85 | "NULL", | ||
| 86 | null_write, | ||
| 87 | null_read, | ||
| 88 | null_puts, | ||
| 89 | null_gets, | ||
| 90 | null_ctrl, | ||
| 91 | null_new, | ||
| 92 | null_free, | ||
| 93 | }; | ||
| 94 | |||
| 95 | BIO_METHOD *BIO_s_null() | ||
| 96 | { | ||
| 97 | return(&null_method); | ||
| 98 | } | ||
| 99 | |||
| 100 | static int null_new(bi) | ||
| 101 | BIO *bi; | ||
| 102 | { | ||
| 103 | bi->init=1; | ||
| 104 | bi->num=0; | ||
| 105 | bi->ptr=(NULL); | ||
| 106 | return(1); | ||
| 107 | } | ||
| 108 | |||
| 109 | static int null_free(a) | ||
| 110 | BIO *a; | ||
| 111 | { | ||
| 112 | if (a == NULL) return(0); | ||
| 113 | return(1); | ||
| 114 | } | ||
| 115 | |||
| 116 | static int null_read(b,out,outl) | ||
| 117 | BIO *b; | ||
| 118 | char *out; | ||
| 119 | int outl; | ||
| 120 | { | ||
| 121 | return(0); | ||
| 122 | } | ||
| 123 | |||
| 124 | static int null_write(b,in,inl) | ||
| 125 | BIO *b; | ||
| 126 | char *in; | ||
| 127 | int inl; | ||
| 128 | { | ||
| 129 | return(inl); | ||
| 130 | } | ||
| 131 | |||
| 132 | static long null_ctrl(b,cmd,num,ptr) | ||
| 133 | BIO *b; | ||
| 134 | int cmd; | ||
| 135 | long num; | ||
| 136 | char *ptr; | ||
| 137 | { | ||
| 138 | long ret=1; | ||
| 139 | |||
| 140 | switch (cmd) | ||
| 141 | { | ||
| 142 | case BIO_CTRL_RESET: | ||
| 143 | case BIO_CTRL_EOF: | ||
| 144 | case BIO_CTRL_SET: | ||
| 145 | case BIO_CTRL_SET_CLOSE: | ||
| 146 | case BIO_CTRL_FLUSH: | ||
| 147 | case BIO_CTRL_DUP: | ||
| 148 | ret=1; | ||
| 149 | break; | ||
| 150 | case BIO_CTRL_GET_CLOSE: | ||
| 151 | case BIO_CTRL_INFO: | ||
| 152 | case BIO_CTRL_GET: | ||
| 153 | case BIO_CTRL_PENDING: | ||
| 154 | case BIO_CTRL_WPENDING: | ||
| 155 | default: | ||
| 156 | ret=0; | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | return(ret); | ||
| 160 | } | ||
| 161 | |||
| 162 | static int null_gets(bp,buf,size) | ||
| 163 | BIO *bp; | ||
| 164 | char *buf; | ||
| 165 | int size; | ||
| 166 | { | ||
| 167 | return(0); | ||
| 168 | } | ||
| 169 | |||
| 170 | static int null_puts(bp,str) | ||
| 171 | BIO *bp; | ||
| 172 | char *str; | ||
| 173 | { | ||
| 174 | if (str == NULL) return(0); | ||
| 175 | return(strlen(str)); | ||
| 176 | } | ||
| 177 | |||
diff --git a/src/lib/libcrypto/bio/bss_sock.c b/src/lib/libcrypto/bio/bss_sock.c new file mode 100644 index 0000000000..d907a2867b --- /dev/null +++ b/src/lib/libcrypto/bio/bss_sock.c | |||
| @@ -0,0 +1,461 @@ | |||
| 1 | /* crypto/bio/bss_sock.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #if !defined(NO_SOCK) || defined(BIO_FD) | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <errno.h> | ||
| 63 | #define USE_SOCKETS | ||
| 64 | #include "cryptlib.h" | ||
| 65 | #include "bio.h" | ||
| 66 | |||
| 67 | #ifndef BIO_FD | ||
| 68 | #ifndef NOPROTO | ||
| 69 | static int sock_write(BIO *h,char *buf,int num); | ||
| 70 | static int sock_read(BIO *h,char *buf,int size); | ||
| 71 | static int sock_puts(BIO *h,char *str); | ||
| 72 | static long sock_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 73 | static int sock_new(BIO *h); | ||
| 74 | static int sock_free(BIO *data); | ||
| 75 | int BIO_sock_should_retry(int s); | ||
| 76 | #else | ||
| 77 | static int sock_write(); | ||
| 78 | static int sock_read(); | ||
| 79 | static int sock_puts(); | ||
| 80 | static long sock_ctrl(); | ||
| 81 | static int sock_new(); | ||
| 82 | static int sock_free(); | ||
| 83 | int BIO_sock_should_retry(); | ||
| 84 | #endif | ||
| 85 | |||
| 86 | #else | ||
| 87 | |||
| 88 | #ifndef NOPROTO | ||
| 89 | static int fd_write(BIO *h,char *buf,int num); | ||
| 90 | static int fd_read(BIO *h,char *buf,int size); | ||
| 91 | static int fd_puts(BIO *h,char *str); | ||
| 92 | static long fd_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 93 | static int fd_new(BIO *h); | ||
| 94 | static int fd_free(BIO *data); | ||
| 95 | int BIO_fd_should_retry(int s); | ||
| 96 | #else | ||
| 97 | static int fd_write(); | ||
| 98 | static int fd_read(); | ||
| 99 | static int fd_puts(); | ||
| 100 | static long fd_ctrl(); | ||
| 101 | static int fd_new(); | ||
| 102 | static int fd_free(); | ||
| 103 | int BIO_fd_should_retry(); | ||
| 104 | #endif | ||
| 105 | #endif | ||
| 106 | |||
| 107 | #ifndef BIO_FD | ||
| 108 | static BIO_METHOD methods_sockp= | ||
| 109 | { | ||
| 110 | BIO_TYPE_SOCKET, | ||
| 111 | "socket", | ||
| 112 | sock_write, | ||
| 113 | sock_read, | ||
| 114 | sock_puts, | ||
| 115 | NULL, /* sock_gets, */ | ||
| 116 | sock_ctrl, | ||
| 117 | sock_new, | ||
| 118 | sock_free, | ||
| 119 | }; | ||
| 120 | |||
| 121 | BIO_METHOD *BIO_s_socket() | ||
| 122 | { | ||
| 123 | return(&methods_sockp); | ||
| 124 | } | ||
| 125 | #else | ||
| 126 | static BIO_METHOD methods_fdp= | ||
| 127 | { | ||
| 128 | BIO_TYPE_FD,"file descriptor", | ||
| 129 | fd_write, | ||
| 130 | fd_read, | ||
| 131 | fd_puts, | ||
| 132 | NULL, /* fd_gets, */ | ||
| 133 | fd_ctrl, | ||
| 134 | fd_new, | ||
| 135 | fd_free, | ||
| 136 | }; | ||
| 137 | |||
| 138 | BIO_METHOD *BIO_s_fd() | ||
| 139 | { | ||
| 140 | return(&methods_fdp); | ||
| 141 | } | ||
| 142 | #endif | ||
| 143 | |||
| 144 | #ifndef BIO_FD | ||
| 145 | BIO *BIO_new_socket(fd,close_flag) | ||
| 146 | #else | ||
| 147 | BIO *BIO_new_fd(fd,close_flag) | ||
| 148 | #endif | ||
| 149 | int fd; | ||
| 150 | int close_flag; | ||
| 151 | { | ||
| 152 | BIO *ret; | ||
| 153 | |||
| 154 | #ifndef BIO_FD | ||
| 155 | ret=BIO_new(BIO_s_socket()); | ||
| 156 | #else | ||
| 157 | ret=BIO_new(BIO_s_fd()); | ||
| 158 | #endif | ||
| 159 | if (ret == NULL) return(NULL); | ||
| 160 | BIO_set_fd(ret,fd,close_flag); | ||
| 161 | return(ret); | ||
| 162 | } | ||
| 163 | |||
| 164 | #ifndef BIO_FD | ||
| 165 | static int sock_new(bi) | ||
| 166 | #else | ||
| 167 | static int fd_new(bi) | ||
| 168 | #endif | ||
| 169 | BIO *bi; | ||
| 170 | { | ||
| 171 | bi->init=0; | ||
| 172 | bi->num=0; | ||
| 173 | bi->ptr=NULL; | ||
| 174 | bi->flags=0; | ||
| 175 | return(1); | ||
| 176 | } | ||
| 177 | |||
| 178 | #ifndef BIO_FD | ||
| 179 | static int sock_free(a) | ||
| 180 | #else | ||
| 181 | static int fd_free(a) | ||
| 182 | #endif | ||
| 183 | BIO *a; | ||
| 184 | { | ||
| 185 | if (a == NULL) return(0); | ||
| 186 | if (a->shutdown) | ||
| 187 | { | ||
| 188 | if (a->init) | ||
| 189 | { | ||
| 190 | #ifndef BIO_FD | ||
| 191 | shutdown(a->num,2); | ||
| 192 | # ifdef WINDOWS | ||
| 193 | closesocket(a->num); | ||
| 194 | # else | ||
| 195 | close(a->num); | ||
| 196 | # endif | ||
| 197 | #else /* BIO_FD */ | ||
| 198 | close(a->num); | ||
| 199 | #endif | ||
| 200 | |||
| 201 | } | ||
| 202 | a->init=0; | ||
| 203 | a->flags=0; | ||
| 204 | } | ||
| 205 | return(1); | ||
| 206 | } | ||
| 207 | |||
| 208 | #ifndef BIO_FD | ||
| 209 | static int sock_read(b,out,outl) | ||
| 210 | #else | ||
| 211 | static int fd_read(b,out,outl) | ||
| 212 | #endif | ||
| 213 | BIO *b; | ||
| 214 | char *out; | ||
| 215 | int outl; | ||
| 216 | { | ||
| 217 | int ret=0; | ||
| 218 | |||
| 219 | if (out != NULL) | ||
| 220 | { | ||
| 221 | #if defined(WINDOWS) && !defined(BIO_FD) | ||
| 222 | clear_socket_error(); | ||
| 223 | ret=recv(b->num,out,outl,0); | ||
| 224 | #else | ||
| 225 | clear_sys_error(); | ||
| 226 | ret=read(b->num,out,outl); | ||
| 227 | #endif | ||
| 228 | BIO_clear_retry_flags(b); | ||
| 229 | if (ret <= 0) | ||
| 230 | { | ||
| 231 | #ifndef BIO_FD | ||
| 232 | if (BIO_sock_should_retry(ret)) | ||
| 233 | #else | ||
| 234 | if (BIO_fd_should_retry(ret)) | ||
| 235 | #endif | ||
| 236 | BIO_set_retry_read(b); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | return(ret); | ||
| 240 | } | ||
| 241 | |||
| 242 | #ifndef BIO_FD | ||
| 243 | static int sock_write(b,in,inl) | ||
| 244 | #else | ||
| 245 | static int fd_write(b,in,inl) | ||
| 246 | #endif | ||
| 247 | BIO *b; | ||
| 248 | char *in; | ||
| 249 | int inl; | ||
| 250 | { | ||
| 251 | int ret; | ||
| 252 | |||
| 253 | #if defined(WINDOWS) && !defined(BIO_FD) | ||
| 254 | clear_socket_error(); | ||
| 255 | ret=send(b->num,in,inl,0); | ||
| 256 | #else | ||
| 257 | clear_sys_error(); | ||
| 258 | ret=write(b->num,in,inl); | ||
| 259 | #endif | ||
| 260 | BIO_clear_retry_flags(b); | ||
| 261 | if (ret <= 0) | ||
| 262 | { | ||
| 263 | #ifndef BIO_FD | ||
| 264 | if (BIO_sock_should_retry(ret)) | ||
| 265 | #else | ||
| 266 | if (BIO_fd_should_retry(ret)) | ||
| 267 | #endif | ||
| 268 | BIO_set_retry_write(b); | ||
| 269 | } | ||
| 270 | return(ret); | ||
| 271 | } | ||
| 272 | |||
| 273 | #ifndef BIO_FD | ||
| 274 | static long sock_ctrl(b,cmd,num,ptr) | ||
| 275 | #else | ||
| 276 | static long fd_ctrl(b,cmd,num,ptr) | ||
| 277 | #endif | ||
| 278 | BIO *b; | ||
| 279 | int cmd; | ||
| 280 | long num; | ||
| 281 | char *ptr; | ||
| 282 | { | ||
| 283 | long ret=1; | ||
| 284 | int *ip; | ||
| 285 | |||
| 286 | switch (cmd) | ||
| 287 | { | ||
| 288 | case BIO_CTRL_RESET: | ||
| 289 | #ifdef BIO_FD | ||
| 290 | ret=(long)lseek(b->num,0,0); | ||
| 291 | #else | ||
| 292 | ret=0; | ||
| 293 | #endif | ||
| 294 | break; | ||
| 295 | case BIO_CTRL_INFO: | ||
| 296 | ret=0; | ||
| 297 | break; | ||
| 298 | case BIO_C_SET_FD: | ||
| 299 | #ifndef BIO_FD | ||
| 300 | sock_free(b); | ||
| 301 | #else | ||
| 302 | fd_free(b); | ||
| 303 | #endif | ||
| 304 | b->num= *((int *)ptr); | ||
| 305 | b->shutdown=(int)num; | ||
| 306 | b->init=1; | ||
| 307 | break; | ||
| 308 | case BIO_C_GET_FD: | ||
| 309 | if (b->init) | ||
| 310 | { | ||
| 311 | ip=(int *)ptr; | ||
| 312 | if (ip != NULL) *ip=b->num; | ||
| 313 | ret=b->num; | ||
| 314 | } | ||
| 315 | else | ||
| 316 | ret= -1; | ||
| 317 | break; | ||
| 318 | case BIO_CTRL_GET_CLOSE: | ||
| 319 | ret=b->shutdown; | ||
| 320 | break; | ||
| 321 | case BIO_CTRL_SET_CLOSE: | ||
| 322 | b->shutdown=(int)num; | ||
| 323 | break; | ||
| 324 | case BIO_CTRL_PENDING: | ||
| 325 | case BIO_CTRL_WPENDING: | ||
| 326 | ret=0; | ||
| 327 | break; | ||
| 328 | case BIO_CTRL_DUP: | ||
| 329 | case BIO_CTRL_FLUSH: | ||
| 330 | ret=1; | ||
| 331 | break; | ||
| 332 | break; | ||
| 333 | default: | ||
| 334 | ret=0; | ||
| 335 | break; | ||
| 336 | } | ||
| 337 | return(ret); | ||
| 338 | } | ||
| 339 | |||
| 340 | #ifdef undef | ||
| 341 | static int sock_gets(bp,buf,size) | ||
| 342 | BIO *bp; | ||
| 343 | char *buf; | ||
| 344 | int size; | ||
| 345 | { | ||
| 346 | return(-1); | ||
| 347 | } | ||
| 348 | #endif | ||
| 349 | |||
| 350 | #ifndef BIO_FD | ||
| 351 | static int sock_puts(bp,str) | ||
| 352 | #else | ||
| 353 | static int fd_puts(bp,str) | ||
| 354 | #endif | ||
| 355 | BIO *bp; | ||
| 356 | char *str; | ||
| 357 | { | ||
| 358 | int n,ret; | ||
| 359 | |||
| 360 | n=strlen(str); | ||
| 361 | #ifndef BIO_FD | ||
| 362 | ret=sock_write(bp,str,n); | ||
| 363 | #else | ||
| 364 | ret=fd_write(bp,str,n); | ||
| 365 | #endif | ||
| 366 | return(ret); | ||
| 367 | } | ||
| 368 | |||
| 369 | #ifndef BIO_FD | ||
| 370 | int BIO_sock_should_retry(i) | ||
| 371 | #else | ||
| 372 | int BIO_fd_should_retry(i) | ||
| 373 | #endif | ||
| 374 | int i; | ||
| 375 | { | ||
| 376 | int err; | ||
| 377 | |||
| 378 | if ((i == 0) || (i == -1)) | ||
| 379 | { | ||
| 380 | #if !defined(BIO_FD) && defined(WINDOWS) | ||
| 381 | err=get_last_socket_error(); | ||
| 382 | #else | ||
| 383 | err=get_last_sys_error(); | ||
| 384 | #endif | ||
| 385 | |||
| 386 | #if defined(WINDOWS) /* more microsoft stupidity */ | ||
| 387 | if ((i == -1) && (err == 0)) | ||
| 388 | return(1); | ||
| 389 | #endif | ||
| 390 | |||
| 391 | #ifndef BIO_FD | ||
| 392 | return(BIO_sock_non_fatal_error(err)); | ||
| 393 | #else | ||
| 394 | return(BIO_fd_non_fatal_error(err)); | ||
| 395 | #endif | ||
| 396 | } | ||
| 397 | return(0); | ||
| 398 | } | ||
| 399 | |||
| 400 | #ifndef BIO_FD | ||
| 401 | int BIO_sock_non_fatal_error(err) | ||
| 402 | #else | ||
| 403 | int BIO_fd_non_fatal_error(err) | ||
| 404 | #endif | ||
| 405 | int err; | ||
| 406 | { | ||
| 407 | switch (err) | ||
| 408 | { | ||
| 409 | #if !defined(BIO_FD) && defined(WINDOWS) | ||
| 410 | # if defined(WSAEWOULDBLOCK) | ||
| 411 | case WSAEWOULDBLOCK: | ||
| 412 | # endif | ||
| 413 | |||
| 414 | # if defined(WSAENOTCONN) | ||
| 415 | case WSAENOTCONN: | ||
| 416 | # endif | ||
| 417 | #endif | ||
| 418 | |||
| 419 | #ifdef EWOULDBLOCK | ||
| 420 | # ifdef WSAEWOULDBLOCK | ||
| 421 | # if WSAEWOULDBLOCK != EWOULDBLOCK | ||
| 422 | case EWOULDBLOCK: | ||
| 423 | # endif | ||
| 424 | # else | ||
| 425 | case EWOULDBLOCK: | ||
| 426 | # endif | ||
| 427 | #endif | ||
| 428 | |||
| 429 | #if defined(ENOTCONN) | ||
| 430 | case ENOTCONN: | ||
| 431 | #endif | ||
| 432 | |||
| 433 | #ifdef EINTR | ||
| 434 | case EINTR: | ||
| 435 | #endif | ||
| 436 | |||
| 437 | #ifdef EAGAIN | ||
| 438 | #if EWOULDBLOCK != EAGAIN | ||
| 439 | case EAGAIN: | ||
| 440 | # endif | ||
| 441 | #endif | ||
| 442 | |||
| 443 | #ifdef EPROTO | ||
| 444 | case EPROTO: | ||
| 445 | #endif | ||
| 446 | |||
| 447 | #ifdef EINPROGRESS | ||
| 448 | case EINPROGRESS: | ||
| 449 | #endif | ||
| 450 | |||
| 451 | #ifdef EALREADY | ||
| 452 | case EALREADY: | ||
| 453 | #endif | ||
| 454 | return(1); | ||
| 455 | break; | ||
| 456 | default: | ||
| 457 | break; | ||
| 458 | } | ||
| 459 | return(0); | ||
| 460 | } | ||
| 461 | #endif | ||
