diff options
Diffstat (limited to 'src/lib/libcrypto/bio')
23 files changed, 9848 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bio/Makefile b/src/lib/libcrypto/bio/Makefile new file mode 100644 index 0000000000..1ef6c2fb9f --- /dev/null +++ b/src/lib/libcrypto/bio/Makefile | |||
| @@ -0,0 +1,221 @@ | |||
| 1 | # | ||
| 2 | # OpenSSL/crypto/bio/Makefile | ||
| 3 | # | ||
| 4 | |||
| 5 | DIR= bio | ||
| 6 | TOP= ../.. | ||
| 7 | CC= cc | ||
| 8 | INCLUDES= -I.. -I$(TOP) -I../../include | ||
| 9 | CFLAG=-g | ||
| 10 | MAKEFILE= Makefile | ||
| 11 | AR= ar r | ||
| 12 | |||
| 13 | CFLAGS= $(INCLUDES) $(CFLAG) | ||
| 14 | |||
| 15 | GENERAL=Makefile | ||
| 16 | TEST= | ||
| 17 | APPS= | ||
| 18 | |||
| 19 | LIB=$(TOP)/libcrypto.a | ||
| 20 | LIBSRC= bio_lib.c bio_cb.c bio_err.c \ | ||
| 21 | bss_mem.c bss_null.c bss_fd.c \ | ||
| 22 | bss_file.c bss_sock.c bss_conn.c \ | ||
| 23 | bf_null.c bf_buff.c b_print.c b_dump.c \ | ||
| 24 | b_sock.c bss_acpt.c bf_nbio.c bss_log.c bss_bio.c \ | ||
| 25 | bss_dgram.c | ||
| 26 | # bf_lbuf.c | ||
| 27 | LIBOBJ= bio_lib.o bio_cb.o bio_err.o \ | ||
| 28 | bss_mem.o bss_null.o bss_fd.o \ | ||
| 29 | bss_file.o bss_sock.o bss_conn.o \ | ||
| 30 | bf_null.o bf_buff.o b_print.o b_dump.o \ | ||
| 31 | b_sock.o bss_acpt.o bf_nbio.o bss_log.o bss_bio.o \ | ||
| 32 | bss_dgram.o | ||
| 33 | # bf_lbuf.o | ||
| 34 | |||
| 35 | SRC= $(LIBSRC) | ||
| 36 | |||
| 37 | EXHEADER= bio.h | ||
| 38 | HEADER= bio_lcl.h $(EXHEADER) | ||
| 39 | |||
| 40 | ALL= $(GENERAL) $(SRC) $(HEADER) | ||
| 41 | |||
| 42 | top: | ||
| 43 | (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) | ||
| 44 | |||
| 45 | all: lib | ||
| 46 | |||
| 47 | lib: $(LIBOBJ) | ||
| 48 | $(AR) $(LIB) $(LIBOBJ) | ||
| 49 | $(RANLIB) $(LIB) || echo Never mind. | ||
| 50 | @touch lib | ||
| 51 | |||
| 52 | files: | ||
| 53 | $(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO | ||
| 54 | |||
| 55 | links: | ||
| 56 | @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER) | ||
| 57 | @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST) | ||
| 58 | @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS) | ||
| 59 | |||
| 60 | install: | ||
| 61 | @[ -n "$(INSTALLTOP)" ] # should be set by top Makefile... | ||
| 62 | @headerlist="$(EXHEADER)"; for i in $$headerlist; \ | ||
| 63 | do \ | ||
| 64 | (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \ | ||
| 65 | chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \ | ||
| 66 | done; | ||
| 67 | |||
| 68 | tags: | ||
| 69 | ctags $(SRC) | ||
| 70 | |||
| 71 | tests: | ||
| 72 | |||
| 73 | lint: | ||
| 74 | lint -DLINT $(INCLUDES) $(SRC)>fluff | ||
| 75 | |||
| 76 | depend: | ||
| 77 | @[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile... | ||
| 78 | $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC) | ||
| 79 | |||
| 80 | dclean: | ||
| 81 | $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new | ||
| 82 | mv -f Makefile.new $(MAKEFILE) | ||
| 83 | |||
| 84 | clean: | ||
| 85 | rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff | ||
| 86 | |||
| 87 | # DO NOT DELETE THIS LINE -- make depend depends on it. | ||
| 88 | |||
| 89 | b_dump.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 90 | b_dump.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 91 | b_dump.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 92 | b_dump.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 93 | b_dump.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 94 | b_dump.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 95 | b_dump.o: ../../include/openssl/symhacks.h ../cryptlib.h b_dump.c bio_lcl.h | ||
| 96 | b_print.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 97 | b_print.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 98 | b_print.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 99 | b_print.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 100 | b_print.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 101 | b_print.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 102 | b_print.o: ../../include/openssl/symhacks.h ../cryptlib.h b_print.c | ||
| 103 | b_sock.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 104 | b_sock.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 105 | b_sock.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 106 | b_sock.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 107 | b_sock.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 108 | b_sock.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 109 | b_sock.o: ../../include/openssl/symhacks.h ../cryptlib.h b_sock.c | ||
| 110 | bf_buff.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 111 | bf_buff.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 112 | bf_buff.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 113 | bf_buff.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 114 | bf_buff.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 115 | bf_buff.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 116 | bf_buff.o: ../../include/openssl/symhacks.h ../cryptlib.h bf_buff.c | ||
| 117 | bf_nbio.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 118 | bf_nbio.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 119 | bf_nbio.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 120 | bf_nbio.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 121 | bf_nbio.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 122 | bf_nbio.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h | ||
| 123 | bf_nbio.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 124 | bf_nbio.o: ../cryptlib.h bf_nbio.c | ||
| 125 | bf_null.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 126 | bf_null.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 127 | bf_null.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 128 | bf_null.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 129 | bf_null.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 130 | bf_null.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 131 | bf_null.o: ../../include/openssl/symhacks.h ../cryptlib.h bf_null.c | ||
| 132 | bio_cb.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 133 | bio_cb.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 134 | bio_cb.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 135 | bio_cb.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 136 | bio_cb.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 137 | bio_cb.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 138 | bio_cb.o: ../../include/openssl/symhacks.h ../cryptlib.h bio_cb.c | ||
| 139 | bio_err.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h | ||
| 140 | bio_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 141 | bio_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 142 | bio_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 143 | bio_err.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 144 | bio_err.o: ../../include/openssl/symhacks.h bio_err.c | ||
| 145 | bio_lib.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 146 | bio_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 147 | bio_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 148 | bio_lib.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 149 | bio_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 150 | bio_lib.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 151 | bio_lib.o: ../../include/openssl/symhacks.h ../cryptlib.h bio_lib.c | ||
| 152 | bss_acpt.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 153 | bss_acpt.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 154 | bss_acpt.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 155 | bss_acpt.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 156 | bss_acpt.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 157 | bss_acpt.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 158 | bss_acpt.o: ../../include/openssl/symhacks.h ../cryptlib.h bss_acpt.c | ||
| 159 | bss_bio.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 160 | bss_bio.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
| 161 | bss_bio.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
| 162 | bss_bio.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
| 163 | bss_bio.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h | ||
| 164 | bss_bio.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 165 | bss_bio.o: bss_bio.c | ||
| 166 | bss_conn.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 167 | bss_conn.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 168 | bss_conn.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 169 | bss_conn.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 170 | bss_conn.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 171 | bss_conn.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 172 | bss_conn.o: ../../include/openssl/symhacks.h ../cryptlib.h bss_conn.c | ||
| 173 | bss_dgram.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 174 | bss_dgram.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 175 | bss_dgram.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 176 | bss_dgram.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 177 | bss_dgram.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 178 | bss_dgram.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 179 | bss_dgram.o: ../../include/openssl/symhacks.h ../cryptlib.h bss_dgram.c | ||
| 180 | bss_fd.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 181 | bss_fd.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 182 | bss_fd.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 183 | bss_fd.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 184 | bss_fd.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 185 | bss_fd.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 186 | bss_fd.o: ../../include/openssl/symhacks.h ../cryptlib.h bio_lcl.h bss_fd.c | ||
| 187 | bss_file.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 188 | bss_file.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 189 | bss_file.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 190 | bss_file.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 191 | bss_file.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 192 | bss_file.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 193 | bss_file.o: ../../include/openssl/symhacks.h ../cryptlib.h bio_lcl.h bss_file.c | ||
| 194 | bss_log.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 195 | bss_log.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 196 | bss_log.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 197 | bss_log.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 198 | bss_log.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 199 | bss_log.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 200 | bss_log.o: ../../include/openssl/symhacks.h ../cryptlib.h bss_log.c | ||
| 201 | bss_mem.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 202 | bss_mem.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 203 | bss_mem.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 204 | bss_mem.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 205 | bss_mem.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 206 | bss_mem.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 207 | bss_mem.o: ../../include/openssl/symhacks.h ../cryptlib.h bss_mem.c | ||
| 208 | bss_null.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 209 | bss_null.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 210 | bss_null.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 211 | bss_null.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 212 | bss_null.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 213 | bss_null.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 214 | bss_null.o: ../../include/openssl/symhacks.h ../cryptlib.h bss_null.c | ||
| 215 | bss_sock.o: ../../e_os.h ../../include/openssl/bio.h | ||
| 216 | bss_sock.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 217 | bss_sock.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 218 | bss_sock.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 219 | bss_sock.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 220 | bss_sock.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 221 | bss_sock.o: ../../include/openssl/symhacks.h ../cryptlib.h bss_sock.c | ||
diff --git a/src/lib/libcrypto/bio/b_dump.c b/src/lib/libcrypto/bio/b_dump.c new file mode 100644 index 0000000000..c80ecc4295 --- /dev/null +++ b/src/lib/libcrypto/bio/b_dump.c | |||
| @@ -0,0 +1,187 @@ | |||
| 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_lcl.h" | ||
| 66 | |||
| 67 | #define TRUNCATE | ||
| 68 | #define DUMP_WIDTH 16 | ||
| 69 | #define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4)) | ||
| 70 | |||
| 71 | int BIO_dump_cb(int (*cb)(const void *data, size_t len, void *u), | ||
| 72 | void *u, const char *s, int len) | ||
| 73 | { | ||
| 74 | return BIO_dump_indent_cb(cb, u, s, len, 0); | ||
| 75 | } | ||
| 76 | |||
| 77 | int BIO_dump_indent_cb(int (*cb)(const void *data, size_t len, void *u), | ||
| 78 | void *u, const char *s, int len, int indent) | ||
| 79 | { | ||
| 80 | int ret=0; | ||
| 81 | char buf[288+1],tmp[20],str[128+1]; | ||
| 82 | int i,j,rows,trc; | ||
| 83 | unsigned char ch; | ||
| 84 | int dump_width; | ||
| 85 | |||
| 86 | trc=0; | ||
| 87 | |||
| 88 | #ifdef TRUNCATE | ||
| 89 | for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--) | ||
| 90 | trc++; | ||
| 91 | #endif | ||
| 92 | |||
| 93 | if (indent < 0) | ||
| 94 | indent = 0; | ||
| 95 | if (indent) | ||
| 96 | { | ||
| 97 | if (indent > 128) indent=128; | ||
| 98 | memset(str,' ',indent); | ||
| 99 | } | ||
| 100 | str[indent]='\0'; | ||
| 101 | |||
| 102 | dump_width=DUMP_WIDTH_LESS_INDENT(indent); | ||
| 103 | rows=(len/dump_width); | ||
| 104 | if ((rows*dump_width)<len) | ||
| 105 | rows++; | ||
| 106 | for(i=0;i<rows;i++) | ||
| 107 | { | ||
| 108 | buf[0]='\0'; /* start with empty string */ | ||
| 109 | BUF_strlcpy(buf,str,sizeof buf); | ||
| 110 | BIO_snprintf(tmp,sizeof tmp,"%04x - ",i*dump_width); | ||
| 111 | BUF_strlcat(buf,tmp,sizeof buf); | ||
| 112 | for(j=0;j<dump_width;j++) | ||
| 113 | { | ||
| 114 | if (((i*dump_width)+j)>=len) | ||
| 115 | { | ||
| 116 | BUF_strlcat(buf," ",sizeof buf); | ||
| 117 | } | ||
| 118 | else | ||
| 119 | { | ||
| 120 | ch=((unsigned char)*(s+i*dump_width+j)) & 0xff; | ||
| 121 | BIO_snprintf(tmp,sizeof tmp,"%02x%c",ch, | ||
| 122 | j==7?'-':' '); | ||
| 123 | BUF_strlcat(buf,tmp,sizeof buf); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | BUF_strlcat(buf," ",sizeof buf); | ||
| 127 | for(j=0;j<dump_width;j++) | ||
| 128 | { | ||
| 129 | if (((i*dump_width)+j)>=len) | ||
| 130 | break; | ||
| 131 | ch=((unsigned char)*(s+i*dump_width+j)) & 0xff; | ||
| 132 | #ifndef CHARSET_EBCDIC | ||
| 133 | BIO_snprintf(tmp,sizeof tmp,"%c", | ||
| 134 | ((ch>=' ')&&(ch<='~'))?ch:'.'); | ||
| 135 | #else | ||
| 136 | BIO_snprintf(tmp,sizeof tmp,"%c", | ||
| 137 | ((ch>=os_toascii[' '])&&(ch<=os_toascii['~'])) | ||
| 138 | ? os_toebcdic[ch] | ||
| 139 | : '.'); | ||
| 140 | #endif | ||
| 141 | BUF_strlcat(buf,tmp,sizeof buf); | ||
| 142 | } | ||
| 143 | BUF_strlcat(buf,"\n",sizeof buf); | ||
| 144 | /* if this is the last call then update the ddt_dump thing so | ||
| 145 | * that we will move the selection point in the debug window | ||
| 146 | */ | ||
| 147 | ret+=cb((void *)buf,strlen(buf),u); | ||
| 148 | } | ||
| 149 | #ifdef TRUNCATE | ||
| 150 | if (trc > 0) | ||
| 151 | { | ||
| 152 | BIO_snprintf(buf,sizeof buf,"%s%04x - <SPACES/NULS>\n",str, | ||
| 153 | len+trc); | ||
| 154 | ret+=cb((void *)buf,strlen(buf),u); | ||
| 155 | } | ||
| 156 | #endif | ||
| 157 | return(ret); | ||
| 158 | } | ||
| 159 | |||
| 160 | #ifndef OPENSSL_NO_FP_API | ||
| 161 | static int write_fp(const void *data, size_t len, void *fp) | ||
| 162 | { | ||
| 163 | return UP_fwrite(data, len, 1, fp); | ||
| 164 | } | ||
| 165 | int BIO_dump_fp(FILE *fp, const char *s, int len) | ||
| 166 | { | ||
| 167 | return BIO_dump_cb(write_fp, fp, s, len); | ||
| 168 | } | ||
| 169 | int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent) | ||
| 170 | { | ||
| 171 | return BIO_dump_indent_cb(write_fp, fp, s, len, indent); | ||
| 172 | } | ||
| 173 | #endif | ||
| 174 | |||
| 175 | static int write_bio(const void *data, size_t len, void *bp) | ||
| 176 | { | ||
| 177 | return BIO_write((BIO *)bp, (const char *)data, len); | ||
| 178 | } | ||
| 179 | int BIO_dump(BIO *bp, const char *s, int len) | ||
| 180 | { | ||
| 181 | return BIO_dump_cb(write_bio, bp, s, len); | ||
| 182 | } | ||
| 183 | int BIO_dump_indent(BIO *bp, const char *s, int len, int indent) | ||
| 184 | { | ||
| 185 | return BIO_dump_indent_cb(write_bio, bp, s, len, indent); | ||
| 186 | } | ||
| 187 | |||
diff --git a/src/lib/libcrypto/bio/b_print.c b/src/lib/libcrypto/bio/b_print.c new file mode 100644 index 0000000000..2fffcfc025 --- /dev/null +++ b/src/lib/libcrypto/bio/b_print.c | |||
| @@ -0,0 +1,843 @@ | |||
| 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 | /* disable assert() unless BIO_DEBUG has been defined */ | ||
| 60 | #ifndef BIO_DEBUG | ||
| 61 | # ifndef NDEBUG | ||
| 62 | # define NDEBUG | ||
| 63 | # endif | ||
| 64 | #endif | ||
| 65 | |||
| 66 | /* | ||
| 67 | * Stolen from tjh's ssl/ssl_trc.c stuff. | ||
| 68 | */ | ||
| 69 | |||
| 70 | #include <stdio.h> | ||
| 71 | #include <string.h> | ||
| 72 | #include <ctype.h> | ||
| 73 | #include <assert.h> | ||
| 74 | #include <limits.h> | ||
| 75 | #include "cryptlib.h" | ||
| 76 | #ifndef NO_SYS_TYPES_H | ||
| 77 | #include <sys/types.h> | ||
| 78 | #endif | ||
| 79 | #include <openssl/bn.h> /* To get BN_LLONG properly defined */ | ||
| 80 | #include <openssl/bio.h> | ||
| 81 | |||
| 82 | #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT) | ||
| 83 | # ifndef HAVE_LONG_LONG | ||
| 84 | # define HAVE_LONG_LONG 1 | ||
| 85 | # endif | ||
| 86 | #endif | ||
| 87 | |||
| 88 | /***************************************************************************/ | ||
| 89 | |||
| 90 | /* | ||
| 91 | * Copyright Patrick Powell 1995 | ||
| 92 | * This code is based on code written by Patrick Powell <papowell@astart.com> | ||
| 93 | * It may be used for any purpose as long as this notice remains intact | ||
| 94 | * on all source code distributions. | ||
| 95 | */ | ||
| 96 | |||
| 97 | /* | ||
| 98 | * This code contains numerious changes and enhancements which were | ||
| 99 | * made by lots of contributors over the last years to Patrick Powell's | ||
| 100 | * original code: | ||
| 101 | * | ||
| 102 | * o Patrick Powell <papowell@astart.com> (1995) | ||
| 103 | * o Brandon Long <blong@fiction.net> (1996, for Mutt) | ||
| 104 | * o Thomas Roessler <roessler@guug.de> (1998, for Mutt) | ||
| 105 | * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt) | ||
| 106 | * o Andrew Tridgell <tridge@samba.org> (1998, for Samba) | ||
| 107 | * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP) | ||
| 108 | * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth) | ||
| 109 | * o ... (for OpenSSL) | ||
| 110 | */ | ||
| 111 | |||
| 112 | #ifdef HAVE_LONG_DOUBLE | ||
| 113 | #define LDOUBLE long double | ||
| 114 | #else | ||
| 115 | #define LDOUBLE double | ||
| 116 | #endif | ||
| 117 | |||
| 118 | #if HAVE_LONG_LONG | ||
| 119 | # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__) | ||
| 120 | # define LLONG __int64 | ||
| 121 | # else | ||
| 122 | # define LLONG long long | ||
| 123 | # endif | ||
| 124 | #else | ||
| 125 | #define LLONG long | ||
| 126 | #endif | ||
| 127 | |||
| 128 | static void fmtstr (char **, char **, size_t *, size_t *, | ||
| 129 | const char *, int, int, int); | ||
| 130 | static void fmtint (char **, char **, size_t *, size_t *, | ||
| 131 | LLONG, int, int, int, int); | ||
| 132 | static void fmtfp (char **, char **, size_t *, size_t *, | ||
| 133 | LDOUBLE, int, int, int); | ||
| 134 | static void doapr_outch (char **, char **, size_t *, size_t *, int); | ||
| 135 | static void _dopr(char **sbuffer, char **buffer, | ||
| 136 | size_t *maxlen, size_t *retlen, int *truncated, | ||
| 137 | const char *format, va_list args); | ||
| 138 | |||
| 139 | /* format read states */ | ||
| 140 | #define DP_S_DEFAULT 0 | ||
| 141 | #define DP_S_FLAGS 1 | ||
| 142 | #define DP_S_MIN 2 | ||
| 143 | #define DP_S_DOT 3 | ||
| 144 | #define DP_S_MAX 4 | ||
| 145 | #define DP_S_MOD 5 | ||
| 146 | #define DP_S_CONV 6 | ||
| 147 | #define DP_S_DONE 7 | ||
| 148 | |||
| 149 | /* format flags - Bits */ | ||
| 150 | #define DP_F_MINUS (1 << 0) | ||
| 151 | #define DP_F_PLUS (1 << 1) | ||
| 152 | #define DP_F_SPACE (1 << 2) | ||
| 153 | #define DP_F_NUM (1 << 3) | ||
| 154 | #define DP_F_ZERO (1 << 4) | ||
| 155 | #define DP_F_UP (1 << 5) | ||
| 156 | #define DP_F_UNSIGNED (1 << 6) | ||
| 157 | |||
| 158 | /* conversion flags */ | ||
| 159 | #define DP_C_SHORT 1 | ||
| 160 | #define DP_C_LONG 2 | ||
| 161 | #define DP_C_LDOUBLE 3 | ||
| 162 | #define DP_C_LLONG 4 | ||
| 163 | |||
| 164 | /* some handy macros */ | ||
| 165 | #define char_to_int(p) (p - '0') | ||
| 166 | #define OSSL_MAX(p,q) ((p >= q) ? p : q) | ||
| 167 | |||
| 168 | static void | ||
| 169 | _dopr( | ||
| 170 | char **sbuffer, | ||
| 171 | char **buffer, | ||
| 172 | size_t *maxlen, | ||
| 173 | size_t *retlen, | ||
| 174 | int *truncated, | ||
| 175 | const char *format, | ||
| 176 | va_list args) | ||
| 177 | { | ||
| 178 | char ch; | ||
| 179 | LLONG value; | ||
| 180 | LDOUBLE fvalue; | ||
| 181 | char *strvalue; | ||
| 182 | int min; | ||
| 183 | int max; | ||
| 184 | int state; | ||
| 185 | int flags; | ||
| 186 | int cflags; | ||
| 187 | size_t currlen; | ||
| 188 | |||
| 189 | state = DP_S_DEFAULT; | ||
| 190 | flags = currlen = cflags = min = 0; | ||
| 191 | max = -1; | ||
| 192 | ch = *format++; | ||
| 193 | |||
| 194 | while (state != DP_S_DONE) { | ||
| 195 | if (ch == '\0' || (buffer == NULL && currlen >= *maxlen)) | ||
| 196 | state = DP_S_DONE; | ||
| 197 | |||
| 198 | switch (state) { | ||
| 199 | case DP_S_DEFAULT: | ||
| 200 | if (ch == '%') | ||
| 201 | state = DP_S_FLAGS; | ||
| 202 | else | ||
| 203 | doapr_outch(sbuffer,buffer, &currlen, maxlen, ch); | ||
| 204 | ch = *format++; | ||
| 205 | break; | ||
| 206 | case DP_S_FLAGS: | ||
| 207 | switch (ch) { | ||
| 208 | case '-': | ||
| 209 | flags |= DP_F_MINUS; | ||
| 210 | ch = *format++; | ||
| 211 | break; | ||
| 212 | case '+': | ||
| 213 | flags |= DP_F_PLUS; | ||
| 214 | ch = *format++; | ||
| 215 | break; | ||
| 216 | case ' ': | ||
| 217 | flags |= DP_F_SPACE; | ||
| 218 | ch = *format++; | ||
| 219 | break; | ||
| 220 | case '#': | ||
| 221 | flags |= DP_F_NUM; | ||
| 222 | ch = *format++; | ||
| 223 | break; | ||
| 224 | case '0': | ||
| 225 | flags |= DP_F_ZERO; | ||
| 226 | ch = *format++; | ||
| 227 | break; | ||
| 228 | default: | ||
| 229 | state = DP_S_MIN; | ||
| 230 | break; | ||
| 231 | } | ||
| 232 | break; | ||
| 233 | case DP_S_MIN: | ||
| 234 | if (isdigit((unsigned char)ch)) { | ||
| 235 | min = 10 * min + char_to_int(ch); | ||
| 236 | ch = *format++; | ||
| 237 | } else if (ch == '*') { | ||
| 238 | min = va_arg(args, int); | ||
| 239 | ch = *format++; | ||
| 240 | state = DP_S_DOT; | ||
| 241 | } else | ||
| 242 | state = DP_S_DOT; | ||
| 243 | break; | ||
| 244 | case DP_S_DOT: | ||
| 245 | if (ch == '.') { | ||
| 246 | state = DP_S_MAX; | ||
| 247 | ch = *format++; | ||
| 248 | } else | ||
| 249 | state = DP_S_MOD; | ||
| 250 | break; | ||
| 251 | case DP_S_MAX: | ||
| 252 | if (isdigit((unsigned char)ch)) { | ||
| 253 | if (max < 0) | ||
| 254 | max = 0; | ||
| 255 | max = 10 * max + char_to_int(ch); | ||
| 256 | ch = *format++; | ||
| 257 | } else if (ch == '*') { | ||
| 258 | max = va_arg(args, int); | ||
| 259 | ch = *format++; | ||
| 260 | state = DP_S_MOD; | ||
| 261 | } else | ||
| 262 | state = DP_S_MOD; | ||
| 263 | break; | ||
| 264 | case DP_S_MOD: | ||
| 265 | switch (ch) { | ||
| 266 | case 'h': | ||
| 267 | cflags = DP_C_SHORT; | ||
| 268 | ch = *format++; | ||
| 269 | break; | ||
| 270 | case 'l': | ||
| 271 | if (*format == 'l') { | ||
| 272 | cflags = DP_C_LLONG; | ||
| 273 | format++; | ||
| 274 | } else | ||
| 275 | cflags = DP_C_LONG; | ||
| 276 | ch = *format++; | ||
| 277 | break; | ||
| 278 | case 'q': | ||
| 279 | cflags = DP_C_LLONG; | ||
| 280 | ch = *format++; | ||
| 281 | break; | ||
| 282 | case 'L': | ||
| 283 | cflags = DP_C_LDOUBLE; | ||
| 284 | ch = *format++; | ||
| 285 | break; | ||
| 286 | default: | ||
| 287 | break; | ||
| 288 | } | ||
| 289 | state = DP_S_CONV; | ||
| 290 | break; | ||
| 291 | case DP_S_CONV: | ||
| 292 | switch (ch) { | ||
| 293 | case 'd': | ||
| 294 | case 'i': | ||
| 295 | switch (cflags) { | ||
| 296 | case DP_C_SHORT: | ||
| 297 | value = (short int)va_arg(args, int); | ||
| 298 | break; | ||
| 299 | case DP_C_LONG: | ||
| 300 | value = va_arg(args, long int); | ||
| 301 | break; | ||
| 302 | case DP_C_LLONG: | ||
| 303 | value = va_arg(args, LLONG); | ||
| 304 | break; | ||
| 305 | default: | ||
| 306 | value = va_arg(args, int); | ||
| 307 | break; | ||
| 308 | } | ||
| 309 | fmtint(sbuffer, buffer, &currlen, maxlen, | ||
| 310 | value, 10, min, max, flags); | ||
| 311 | break; | ||
| 312 | case 'X': | ||
| 313 | flags |= DP_F_UP; | ||
| 314 | /* FALLTHROUGH */ | ||
| 315 | case 'x': | ||
| 316 | case 'o': | ||
| 317 | case 'u': | ||
| 318 | flags |= DP_F_UNSIGNED; | ||
| 319 | switch (cflags) { | ||
| 320 | case DP_C_SHORT: | ||
| 321 | value = (unsigned short int)va_arg(args, unsigned int); | ||
| 322 | break; | ||
| 323 | case DP_C_LONG: | ||
| 324 | value = (LLONG) va_arg(args, | ||
| 325 | unsigned long int); | ||
| 326 | break; | ||
| 327 | case DP_C_LLONG: | ||
| 328 | value = va_arg(args, unsigned LLONG); | ||
| 329 | break; | ||
| 330 | default: | ||
| 331 | value = (LLONG) va_arg(args, | ||
| 332 | unsigned int); | ||
| 333 | break; | ||
| 334 | } | ||
| 335 | fmtint(sbuffer, buffer, &currlen, maxlen, value, | ||
| 336 | ch == 'o' ? 8 : (ch == 'u' ? 10 : 16), | ||
| 337 | min, max, flags); | ||
| 338 | break; | ||
| 339 | case 'f': | ||
| 340 | if (cflags == DP_C_LDOUBLE) | ||
| 341 | fvalue = va_arg(args, LDOUBLE); | ||
| 342 | else | ||
| 343 | fvalue = va_arg(args, double); | ||
| 344 | fmtfp(sbuffer, buffer, &currlen, maxlen, | ||
| 345 | fvalue, min, max, flags); | ||
| 346 | break; | ||
| 347 | case 'E': | ||
| 348 | flags |= DP_F_UP; | ||
| 349 | case 'e': | ||
| 350 | if (cflags == DP_C_LDOUBLE) | ||
| 351 | fvalue = va_arg(args, LDOUBLE); | ||
| 352 | else | ||
| 353 | fvalue = va_arg(args, double); | ||
| 354 | break; | ||
| 355 | case 'G': | ||
| 356 | flags |= DP_F_UP; | ||
| 357 | case 'g': | ||
| 358 | if (cflags == DP_C_LDOUBLE) | ||
| 359 | fvalue = va_arg(args, LDOUBLE); | ||
| 360 | else | ||
| 361 | fvalue = va_arg(args, double); | ||
| 362 | break; | ||
| 363 | case 'c': | ||
| 364 | doapr_outch(sbuffer, buffer, &currlen, maxlen, | ||
| 365 | va_arg(args, int)); | ||
| 366 | break; | ||
| 367 | case 's': | ||
| 368 | strvalue = va_arg(args, char *); | ||
| 369 | if (max < 0) { | ||
| 370 | if (buffer) | ||
| 371 | max = INT_MAX; | ||
| 372 | else | ||
| 373 | max = *maxlen; | ||
| 374 | } | ||
| 375 | fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue, | ||
| 376 | flags, min, max); | ||
| 377 | break; | ||
| 378 | case 'p': | ||
| 379 | value = (long)va_arg(args, void *); | ||
| 380 | fmtint(sbuffer, buffer, &currlen, maxlen, | ||
| 381 | value, 16, min, max, flags|DP_F_NUM); | ||
| 382 | break; | ||
| 383 | case 'n': /* XXX */ | ||
| 384 | if (cflags == DP_C_SHORT) { | ||
| 385 | short int *num; | ||
| 386 | num = va_arg(args, short int *); | ||
| 387 | *num = currlen; | ||
| 388 | } else if (cflags == DP_C_LONG) { /* XXX */ | ||
| 389 | long int *num; | ||
| 390 | num = va_arg(args, long int *); | ||
| 391 | *num = (long int) currlen; | ||
| 392 | } else if (cflags == DP_C_LLONG) { /* XXX */ | ||
| 393 | LLONG *num; | ||
| 394 | num = va_arg(args, LLONG *); | ||
| 395 | *num = (LLONG) currlen; | ||
| 396 | } else { | ||
| 397 | int *num; | ||
| 398 | num = va_arg(args, int *); | ||
| 399 | *num = currlen; | ||
| 400 | } | ||
| 401 | break; | ||
| 402 | case '%': | ||
| 403 | doapr_outch(sbuffer, buffer, &currlen, maxlen, ch); | ||
| 404 | break; | ||
| 405 | case 'w': | ||
| 406 | /* not supported yet, treat as next char */ | ||
| 407 | ch = *format++; | ||
| 408 | break; | ||
| 409 | default: | ||
| 410 | /* unknown, skip */ | ||
| 411 | break; | ||
| 412 | } | ||
| 413 | ch = *format++; | ||
| 414 | state = DP_S_DEFAULT; | ||
| 415 | flags = cflags = min = 0; | ||
| 416 | max = -1; | ||
| 417 | break; | ||
| 418 | case DP_S_DONE: | ||
| 419 | break; | ||
| 420 | default: | ||
| 421 | break; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | *truncated = (currlen > *maxlen - 1); | ||
| 425 | if (*truncated) | ||
| 426 | currlen = *maxlen - 1; | ||
| 427 | doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'); | ||
| 428 | *retlen = currlen - 1; | ||
| 429 | return; | ||
| 430 | } | ||
| 431 | |||
| 432 | static void | ||
| 433 | fmtstr( | ||
| 434 | char **sbuffer, | ||
| 435 | char **buffer, | ||
| 436 | size_t *currlen, | ||
| 437 | size_t *maxlen, | ||
| 438 | const char *value, | ||
| 439 | int flags, | ||
| 440 | int min, | ||
| 441 | int max) | ||
| 442 | { | ||
| 443 | int padlen, strln; | ||
| 444 | int cnt = 0; | ||
| 445 | |||
| 446 | if (value == 0) | ||
| 447 | value = "<NULL>"; | ||
| 448 | for (strln = 0; value[strln]; ++strln) | ||
| 449 | ; | ||
| 450 | padlen = min - strln; | ||
| 451 | if (padlen < 0) | ||
| 452 | padlen = 0; | ||
| 453 | if (flags & DP_F_MINUS) | ||
| 454 | padlen = -padlen; | ||
| 455 | |||
| 456 | while ((padlen > 0) && (cnt < max)) { | ||
| 457 | doapr_outch(sbuffer, buffer, currlen, maxlen, ' '); | ||
| 458 | --padlen; | ||
| 459 | ++cnt; | ||
| 460 | } | ||
| 461 | while (*value && (cnt < max)) { | ||
| 462 | doapr_outch(sbuffer, buffer, currlen, maxlen, *value++); | ||
| 463 | ++cnt; | ||
| 464 | } | ||
| 465 | while ((padlen < 0) && (cnt < max)) { | ||
| 466 | doapr_outch(sbuffer, buffer, currlen, maxlen, ' '); | ||
| 467 | ++padlen; | ||
| 468 | ++cnt; | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | static void | ||
| 473 | fmtint( | ||
| 474 | char **sbuffer, | ||
| 475 | char **buffer, | ||
| 476 | size_t *currlen, | ||
| 477 | size_t *maxlen, | ||
| 478 | LLONG value, | ||
| 479 | int base, | ||
| 480 | int min, | ||
| 481 | int max, | ||
| 482 | int flags) | ||
| 483 | { | ||
| 484 | int signvalue = 0; | ||
| 485 | const char *prefix = ""; | ||
| 486 | unsigned LLONG uvalue; | ||
| 487 | char convert[DECIMAL_SIZE(value)+3]; | ||
| 488 | int place = 0; | ||
| 489 | int spadlen = 0; | ||
| 490 | int zpadlen = 0; | ||
| 491 | int caps = 0; | ||
| 492 | |||
| 493 | if (max < 0) | ||
| 494 | max = 0; | ||
| 495 | uvalue = value; | ||
| 496 | if (!(flags & DP_F_UNSIGNED)) { | ||
| 497 | if (value < 0) { | ||
| 498 | signvalue = '-'; | ||
| 499 | uvalue = -value; | ||
| 500 | } else if (flags & DP_F_PLUS) | ||
| 501 | signvalue = '+'; | ||
| 502 | else if (flags & DP_F_SPACE) | ||
| 503 | signvalue = ' '; | ||
| 504 | } | ||
| 505 | if (flags & DP_F_NUM) { | ||
| 506 | if (base == 8) prefix = "0"; | ||
| 507 | if (base == 16) prefix = "0x"; | ||
| 508 | } | ||
| 509 | if (flags & DP_F_UP) | ||
| 510 | caps = 1; | ||
| 511 | do { | ||
| 512 | convert[place++] = | ||
| 513 | (caps ? "0123456789ABCDEF" : "0123456789abcdef") | ||
| 514 | [uvalue % (unsigned) base]; | ||
| 515 | uvalue = (uvalue / (unsigned) base); | ||
| 516 | } while (uvalue && (place < (int)sizeof(convert))); | ||
| 517 | if (place == sizeof(convert)) | ||
| 518 | place--; | ||
| 519 | convert[place] = 0; | ||
| 520 | |||
| 521 | zpadlen = max - place; | ||
| 522 | spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix); | ||
| 523 | if (zpadlen < 0) | ||
| 524 | zpadlen = 0; | ||
| 525 | if (spadlen < 0) | ||
| 526 | spadlen = 0; | ||
| 527 | if (flags & DP_F_ZERO) { | ||
| 528 | zpadlen = OSSL_MAX(zpadlen, spadlen); | ||
| 529 | spadlen = 0; | ||
| 530 | } | ||
| 531 | if (flags & DP_F_MINUS) | ||
| 532 | spadlen = -spadlen; | ||
| 533 | |||
| 534 | /* spaces */ | ||
| 535 | while (spadlen > 0) { | ||
| 536 | doapr_outch(sbuffer, buffer, currlen, maxlen, ' '); | ||
| 537 | --spadlen; | ||
| 538 | } | ||
| 539 | |||
| 540 | /* sign */ | ||
| 541 | if (signvalue) | ||
| 542 | doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue); | ||
| 543 | |||
| 544 | /* prefix */ | ||
| 545 | while (*prefix) { | ||
| 546 | doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix); | ||
| 547 | prefix++; | ||
| 548 | } | ||
| 549 | |||
| 550 | /* zeros */ | ||
| 551 | if (zpadlen > 0) { | ||
| 552 | while (zpadlen > 0) { | ||
| 553 | doapr_outch(sbuffer, buffer, currlen, maxlen, '0'); | ||
| 554 | --zpadlen; | ||
| 555 | } | ||
| 556 | } | ||
| 557 | /* digits */ | ||
| 558 | while (place > 0) | ||
| 559 | doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]); | ||
| 560 | |||
| 561 | /* left justified spaces */ | ||
| 562 | while (spadlen < 0) { | ||
| 563 | doapr_outch(sbuffer, buffer, currlen, maxlen, ' '); | ||
| 564 | ++spadlen; | ||
| 565 | } | ||
| 566 | return; | ||
| 567 | } | ||
| 568 | |||
| 569 | static LDOUBLE | ||
| 570 | abs_val(LDOUBLE value) | ||
| 571 | { | ||
| 572 | LDOUBLE result = value; | ||
| 573 | if (value < 0) | ||
| 574 | result = -value; | ||
| 575 | return result; | ||
| 576 | } | ||
| 577 | |||
| 578 | static LDOUBLE | ||
| 579 | pow_10(int in_exp) | ||
| 580 | { | ||
| 581 | LDOUBLE result = 1; | ||
| 582 | while (in_exp) { | ||
| 583 | result *= 10; | ||
| 584 | in_exp--; | ||
| 585 | } | ||
| 586 | return result; | ||
| 587 | } | ||
| 588 | |||
| 589 | static long | ||
| 590 | roundv(LDOUBLE value) | ||
| 591 | { | ||
| 592 | long intpart; | ||
| 593 | intpart = (long) value; | ||
| 594 | value = value - intpart; | ||
| 595 | if (value >= 0.5) | ||
| 596 | intpart++; | ||
| 597 | return intpart; | ||
| 598 | } | ||
| 599 | |||
| 600 | static void | ||
| 601 | fmtfp( | ||
| 602 | char **sbuffer, | ||
| 603 | char **buffer, | ||
| 604 | size_t *currlen, | ||
| 605 | size_t *maxlen, | ||
| 606 | LDOUBLE fvalue, | ||
| 607 | int min, | ||
| 608 | int max, | ||
| 609 | int flags) | ||
| 610 | { | ||
| 611 | int signvalue = 0; | ||
| 612 | LDOUBLE ufvalue; | ||
| 613 | char iconvert[20]; | ||
| 614 | char fconvert[20]; | ||
| 615 | int iplace = 0; | ||
| 616 | int fplace = 0; | ||
| 617 | int padlen = 0; | ||
| 618 | int zpadlen = 0; | ||
| 619 | int caps = 0; | ||
| 620 | long intpart; | ||
| 621 | long fracpart; | ||
| 622 | long max10; | ||
| 623 | |||
| 624 | if (max < 0) | ||
| 625 | max = 6; | ||
| 626 | ufvalue = abs_val(fvalue); | ||
| 627 | if (fvalue < 0) | ||
| 628 | signvalue = '-'; | ||
| 629 | else if (flags & DP_F_PLUS) | ||
| 630 | signvalue = '+'; | ||
| 631 | else if (flags & DP_F_SPACE) | ||
| 632 | signvalue = ' '; | ||
| 633 | |||
| 634 | intpart = (long)ufvalue; | ||
| 635 | |||
| 636 | /* sorry, we only support 9 digits past the decimal because of our | ||
| 637 | conversion method */ | ||
| 638 | if (max > 9) | ||
| 639 | max = 9; | ||
| 640 | |||
| 641 | /* we "cheat" by converting the fractional part to integer by | ||
| 642 | multiplying by a factor of 10 */ | ||
| 643 | max10 = roundv(pow_10(max)); | ||
| 644 | fracpart = roundv(pow_10(max) * (ufvalue - intpart)); | ||
| 645 | |||
| 646 | if (fracpart >= max10) { | ||
| 647 | intpart++; | ||
| 648 | fracpart -= max10; | ||
| 649 | } | ||
| 650 | |||
| 651 | /* convert integer part */ | ||
| 652 | do { | ||
| 653 | iconvert[iplace++] = | ||
| 654 | (caps ? "0123456789ABCDEF" | ||
| 655 | : "0123456789abcdef")[intpart % 10]; | ||
| 656 | intpart = (intpart / 10); | ||
| 657 | } while (intpart && (iplace < (int)sizeof(iconvert))); | ||
| 658 | if (iplace == sizeof iconvert) | ||
| 659 | iplace--; | ||
| 660 | iconvert[iplace] = 0; | ||
| 661 | |||
| 662 | /* convert fractional part */ | ||
| 663 | do { | ||
| 664 | fconvert[fplace++] = | ||
| 665 | (caps ? "0123456789ABCDEF" | ||
| 666 | : "0123456789abcdef")[fracpart % 10]; | ||
| 667 | fracpart = (fracpart / 10); | ||
| 668 | } while (fplace < max); | ||
| 669 | if (fplace == sizeof fconvert) | ||
| 670 | fplace--; | ||
| 671 | fconvert[fplace] = 0; | ||
| 672 | |||
| 673 | /* -1 for decimal point, another -1 if we are printing a sign */ | ||
| 674 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); | ||
| 675 | zpadlen = max - fplace; | ||
| 676 | if (zpadlen < 0) | ||
| 677 | zpadlen = 0; | ||
| 678 | if (padlen < 0) | ||
| 679 | padlen = 0; | ||
| 680 | if (flags & DP_F_MINUS) | ||
| 681 | padlen = -padlen; | ||
| 682 | |||
| 683 | if ((flags & DP_F_ZERO) && (padlen > 0)) { | ||
| 684 | if (signvalue) { | ||
| 685 | doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue); | ||
| 686 | --padlen; | ||
| 687 | signvalue = 0; | ||
| 688 | } | ||
| 689 | while (padlen > 0) { | ||
| 690 | doapr_outch(sbuffer, buffer, currlen, maxlen, '0'); | ||
| 691 | --padlen; | ||
| 692 | } | ||
| 693 | } | ||
| 694 | while (padlen > 0) { | ||
| 695 | doapr_outch(sbuffer, buffer, currlen, maxlen, ' '); | ||
| 696 | --padlen; | ||
| 697 | } | ||
| 698 | if (signvalue) | ||
| 699 | doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue); | ||
| 700 | |||
| 701 | while (iplace > 0) | ||
| 702 | doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]); | ||
| 703 | |||
| 704 | /* | ||
| 705 | * Decimal point. This should probably use locale to find the correct | ||
| 706 | * char to print out. | ||
| 707 | */ | ||
| 708 | if (max > 0 || (flags & DP_F_NUM)) { | ||
| 709 | doapr_outch(sbuffer, buffer, currlen, maxlen, '.'); | ||
| 710 | |||
| 711 | while (fplace > 0) | ||
| 712 | doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]); | ||
| 713 | } | ||
| 714 | while (zpadlen > 0) { | ||
| 715 | doapr_outch(sbuffer, buffer, currlen, maxlen, '0'); | ||
| 716 | --zpadlen; | ||
| 717 | } | ||
| 718 | |||
| 719 | while (padlen < 0) { | ||
| 720 | doapr_outch(sbuffer, buffer, currlen, maxlen, ' '); | ||
| 721 | ++padlen; | ||
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | static void | ||
| 726 | doapr_outch( | ||
| 727 | char **sbuffer, | ||
| 728 | char **buffer, | ||
| 729 | size_t *currlen, | ||
| 730 | size_t *maxlen, | ||
| 731 | int c) | ||
| 732 | { | ||
| 733 | /* If we haven't at least one buffer, someone has doe a big booboo */ | ||
| 734 | assert(*sbuffer != NULL || buffer != NULL); | ||
| 735 | |||
| 736 | if (buffer) { | ||
| 737 | while (*currlen >= *maxlen) { | ||
| 738 | if (*buffer == NULL) { | ||
| 739 | if (*maxlen == 0) | ||
| 740 | *maxlen = 1024; | ||
| 741 | *buffer = OPENSSL_malloc(*maxlen); | ||
| 742 | if (*currlen > 0) { | ||
| 743 | assert(*sbuffer != NULL); | ||
| 744 | memcpy(*buffer, *sbuffer, *currlen); | ||
| 745 | } | ||
| 746 | *sbuffer = NULL; | ||
| 747 | } else { | ||
| 748 | *maxlen += 1024; | ||
| 749 | *buffer = OPENSSL_realloc(*buffer, *maxlen); | ||
| 750 | } | ||
| 751 | } | ||
| 752 | /* What to do if *buffer is NULL? */ | ||
| 753 | assert(*sbuffer != NULL || *buffer != NULL); | ||
| 754 | } | ||
| 755 | |||
| 756 | if (*currlen < *maxlen) { | ||
| 757 | if (*sbuffer) | ||
| 758 | (*sbuffer)[(*currlen)++] = (char)c; | ||
| 759 | else | ||
| 760 | (*buffer)[(*currlen)++] = (char)c; | ||
| 761 | } | ||
| 762 | |||
| 763 | return; | ||
| 764 | } | ||
| 765 | |||
| 766 | /***************************************************************************/ | ||
| 767 | |||
| 768 | int BIO_printf (BIO *bio, const char *format, ...) | ||
| 769 | { | ||
| 770 | va_list args; | ||
| 771 | int ret; | ||
| 772 | |||
| 773 | va_start(args, format); | ||
| 774 | |||
| 775 | ret = BIO_vprintf(bio, format, args); | ||
| 776 | |||
| 777 | va_end(args); | ||
| 778 | return(ret); | ||
| 779 | } | ||
| 780 | |||
| 781 | int BIO_vprintf (BIO *bio, const char *format, va_list args) | ||
| 782 | { | ||
| 783 | int ret; | ||
| 784 | size_t retlen; | ||
| 785 | char hugebuf[1024*2]; /* Was previously 10k, which is unreasonable | ||
| 786 | in small-stack environments, like threads | ||
| 787 | or DOS programs. */ | ||
| 788 | char *hugebufp = hugebuf; | ||
| 789 | size_t hugebufsize = sizeof(hugebuf); | ||
| 790 | char *dynbuf = NULL; | ||
| 791 | int ignored; | ||
| 792 | |||
| 793 | dynbuf = NULL; | ||
| 794 | CRYPTO_push_info("doapr()"); | ||
| 795 | _dopr(&hugebufp, &dynbuf, &hugebufsize, | ||
| 796 | &retlen, &ignored, format, args); | ||
| 797 | if (dynbuf) | ||
| 798 | { | ||
| 799 | ret=BIO_write(bio, dynbuf, (int)retlen); | ||
| 800 | OPENSSL_free(dynbuf); | ||
| 801 | } | ||
| 802 | else | ||
| 803 | { | ||
| 804 | ret=BIO_write(bio, hugebuf, (int)retlen); | ||
| 805 | } | ||
| 806 | CRYPTO_pop_info(); | ||
| 807 | return(ret); | ||
| 808 | } | ||
| 809 | |||
| 810 | /* As snprintf is not available everywhere, we provide our own implementation. | ||
| 811 | * In case of overflow or error, this returns -1. | ||
| 812 | * This function has nothing to do with BIOs, but it's closely related | ||
| 813 | * to BIO_printf, and we need *some* name prefix ... | ||
| 814 | * (XXX the function should be renamed, but to what?) */ | ||
| 815 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) | ||
| 816 | { | ||
| 817 | va_list args; | ||
| 818 | int ret; | ||
| 819 | |||
| 820 | va_start(args, format); | ||
| 821 | |||
| 822 | ret = BIO_vsnprintf(buf, n, format, args); | ||
| 823 | |||
| 824 | va_end(args); | ||
| 825 | return(ret); | ||
| 826 | } | ||
| 827 | |||
| 828 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) | ||
| 829 | { | ||
| 830 | size_t retlen; | ||
| 831 | int truncated; | ||
| 832 | |||
| 833 | _dopr(&buf, NULL, &n, &retlen, &truncated, format, args); | ||
| 834 | |||
| 835 | if (truncated) | ||
| 836 | /* In case of truncation, return -1 unlike traditional snprintf. | ||
| 837 | * (Current drafts for ISO/IEC 9899 say snprintf should return | ||
| 838 | * the number of characters that would have been written, | ||
| 839 | * had the buffer been large enough, as it did historically.) */ | ||
| 840 | return -1; | ||
| 841 | else | ||
| 842 | return (retlen <= INT_MAX) ? (int)retlen : -1; | ||
| 843 | } | ||
diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c new file mode 100644 index 0000000000..ead477d8a2 --- /dev/null +++ b/src/lib/libcrypto/bio/b_sock.c | |||
| @@ -0,0 +1,786 @@ | |||
| 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 | #include <stdio.h> | ||
| 60 | #include <stdlib.h> | ||
| 61 | #include <errno.h> | ||
| 62 | #define USE_SOCKETS | ||
| 63 | #include "cryptlib.h" | ||
| 64 | #include <openssl/bio.h> | ||
| 65 | #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK) | ||
| 66 | #include <netdb.h> | ||
| 67 | #if defined(NETWARE_CLIB) | ||
| 68 | #include <sys/ioctl.h> | ||
| 69 | NETDB_DEFINE_CONTEXT | ||
| 70 | #endif | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #ifndef OPENSSL_NO_SOCK | ||
| 74 | |||
| 75 | #ifdef OPENSSL_SYS_WIN16 | ||
| 76 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | ||
| 77 | #else | ||
| 78 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
| 79 | #endif | ||
| 80 | |||
| 81 | #ifdef SO_MAXCONN | ||
| 82 | #define MAX_LISTEN SO_MAXCONN | ||
| 83 | #elif defined(SOMAXCONN) | ||
| 84 | #define MAX_LISTEN SOMAXCONN | ||
| 85 | #else | ||
| 86 | #define MAX_LISTEN 32 | ||
| 87 | #endif | ||
| 88 | |||
| 89 | #if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)) | ||
| 90 | static int wsa_init_done=0; | ||
| 91 | #endif | ||
| 92 | |||
| 93 | #if 0 | ||
| 94 | static unsigned long BIO_ghbn_hits=0L; | ||
| 95 | static unsigned long BIO_ghbn_miss=0L; | ||
| 96 | |||
| 97 | #define GHBN_NUM 4 | ||
| 98 | static struct ghbn_cache_st | ||
| 99 | { | ||
| 100 | char name[129]; | ||
| 101 | struct hostent *ent; | ||
| 102 | unsigned long order; | ||
| 103 | } ghbn_cache[GHBN_NUM]; | ||
| 104 | #endif | ||
| 105 | |||
| 106 | static int get_ip(const char *str,unsigned char *ip); | ||
| 107 | #if 0 | ||
| 108 | static void ghbn_free(struct hostent *a); | ||
| 109 | static struct hostent *ghbn_dup(struct hostent *a); | ||
| 110 | #endif | ||
| 111 | int BIO_get_host_ip(const char *str, unsigned char *ip) | ||
| 112 | { | ||
| 113 | int i; | ||
| 114 | int err = 1; | ||
| 115 | int locked = 0; | ||
| 116 | struct hostent *he; | ||
| 117 | |||
| 118 | i=get_ip(str,ip); | ||
| 119 | if (i < 0) | ||
| 120 | { | ||
| 121 | BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS); | ||
| 122 | goto err; | ||
| 123 | } | ||
| 124 | |||
| 125 | /* At this point, we have something that is most probably correct | ||
| 126 | in some way, so let's init the socket. */ | ||
| 127 | if (BIO_sock_init() != 1) | ||
| 128 | return 0; /* don't generate another error code here */ | ||
| 129 | |||
| 130 | /* If the string actually contained an IP address, we need not do | ||
| 131 | anything more */ | ||
| 132 | if (i > 0) return(1); | ||
| 133 | |||
| 134 | /* do a gethostbyname */ | ||
| 135 | CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME); | ||
| 136 | locked = 1; | ||
| 137 | he=BIO_gethostbyname(str); | ||
| 138 | if (he == NULL) | ||
| 139 | { | ||
| 140 | BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP); | ||
| 141 | goto err; | ||
| 142 | } | ||
| 143 | |||
| 144 | /* cast to short because of win16 winsock definition */ | ||
| 145 | if ((short)he->h_addrtype != AF_INET) | ||
| 146 | { | ||
| 147 | BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); | ||
| 148 | goto err; | ||
| 149 | } | ||
| 150 | for (i=0; i<4; i++) | ||
| 151 | ip[i]=he->h_addr_list[0][i]; | ||
| 152 | err = 0; | ||
| 153 | |||
| 154 | err: | ||
| 155 | if (locked) | ||
| 156 | CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME); | ||
| 157 | if (err) | ||
| 158 | { | ||
| 159 | ERR_add_error_data(2,"host=",str); | ||
| 160 | return 0; | ||
| 161 | } | ||
| 162 | else | ||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | int BIO_get_port(const char *str, unsigned short *port_ptr) | ||
| 167 | { | ||
| 168 | int i; | ||
| 169 | struct servent *s; | ||
| 170 | |||
| 171 | if (str == NULL) | ||
| 172 | { | ||
| 173 | BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED); | ||
| 174 | return(0); | ||
| 175 | } | ||
| 176 | i=atoi(str); | ||
| 177 | if (i != 0) | ||
| 178 | *port_ptr=(unsigned short)i; | ||
| 179 | else | ||
| 180 | { | ||
| 181 | CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME); | ||
| 182 | /* Note: under VMS with SOCKETSHR, it seems like the first | ||
| 183 | * parameter is 'char *', instead of 'const char *' | ||
| 184 | */ | ||
| 185 | #ifndef CONST_STRICT | ||
| 186 | s=getservbyname((char *)str,"tcp"); | ||
| 187 | #else | ||
| 188 | s=getservbyname(str,"tcp"); | ||
| 189 | #endif | ||
| 190 | if(s != NULL) | ||
| 191 | *port_ptr=ntohs((unsigned short)s->s_port); | ||
| 192 | CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME); | ||
| 193 | if(s == NULL) | ||
| 194 | { | ||
| 195 | if (strcmp(str,"http") == 0) | ||
| 196 | *port_ptr=80; | ||
| 197 | else if (strcmp(str,"telnet") == 0) | ||
| 198 | *port_ptr=23; | ||
| 199 | else if (strcmp(str,"socks") == 0) | ||
| 200 | *port_ptr=1080; | ||
| 201 | else if (strcmp(str,"https") == 0) | ||
| 202 | *port_ptr=443; | ||
| 203 | else if (strcmp(str,"ssl") == 0) | ||
| 204 | *port_ptr=443; | ||
| 205 | else if (strcmp(str,"ftp") == 0) | ||
| 206 | *port_ptr=21; | ||
| 207 | else if (strcmp(str,"gopher") == 0) | ||
| 208 | *port_ptr=70; | ||
| 209 | #if 0 | ||
| 210 | else if (strcmp(str,"wais") == 0) | ||
| 211 | *port_ptr=21; | ||
| 212 | #endif | ||
| 213 | else | ||
| 214 | { | ||
| 215 | SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error()); | ||
| 216 | ERR_add_error_data(3,"service='",str,"'"); | ||
| 217 | return(0); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | return(1); | ||
| 222 | } | ||
| 223 | |||
| 224 | int BIO_sock_error(int sock) | ||
| 225 | { | ||
| 226 | int j,i; | ||
| 227 | int size; | ||
| 228 | |||
| 229 | size=sizeof(int); | ||
| 230 | /* Note: under Windows the third parameter is of type (char *) | ||
| 231 | * whereas under other systems it is (void *) if you don't have | ||
| 232 | * a cast it will choke the compiler: if you do have a cast then | ||
| 233 | * you can either go for (char *) or (void *). | ||
| 234 | */ | ||
| 235 | i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void *)&j,(void *)&size); | ||
| 236 | if (i < 0) | ||
| 237 | return(1); | ||
| 238 | else | ||
| 239 | return(j); | ||
| 240 | } | ||
| 241 | |||
| 242 | #if 0 | ||
| 243 | long BIO_ghbn_ctrl(int cmd, int iarg, char *parg) | ||
| 244 | { | ||
| 245 | int i; | ||
| 246 | char **p; | ||
| 247 | |||
| 248 | switch (cmd) | ||
| 249 | { | ||
| 250 | case BIO_GHBN_CTRL_HITS: | ||
| 251 | return(BIO_ghbn_hits); | ||
| 252 | /* break; */ | ||
| 253 | case BIO_GHBN_CTRL_MISSES: | ||
| 254 | return(BIO_ghbn_miss); | ||
| 255 | /* break; */ | ||
| 256 | case BIO_GHBN_CTRL_CACHE_SIZE: | ||
| 257 | return(GHBN_NUM); | ||
| 258 | /* break; */ | ||
| 259 | case BIO_GHBN_CTRL_GET_ENTRY: | ||
| 260 | if ((iarg >= 0) && (iarg <GHBN_NUM) && | ||
| 261 | (ghbn_cache[iarg].order > 0)) | ||
| 262 | { | ||
| 263 | p=(char **)parg; | ||
| 264 | if (p == NULL) return(0); | ||
| 265 | *p=ghbn_cache[iarg].name; | ||
| 266 | ghbn_cache[iarg].name[128]='\0'; | ||
| 267 | return(1); | ||
| 268 | } | ||
| 269 | return(0); | ||
| 270 | /* break; */ | ||
| 271 | case BIO_GHBN_CTRL_FLUSH: | ||
| 272 | for (i=0; i<GHBN_NUM; i++) | ||
| 273 | ghbn_cache[i].order=0; | ||
| 274 | break; | ||
| 275 | default: | ||
| 276 | return(0); | ||
| 277 | } | ||
| 278 | return(1); | ||
| 279 | } | ||
| 280 | #endif | ||
| 281 | |||
| 282 | #if 0 | ||
| 283 | static struct hostent *ghbn_dup(struct hostent *a) | ||
| 284 | { | ||
| 285 | struct hostent *ret; | ||
| 286 | int i,j; | ||
| 287 | |||
| 288 | MemCheck_off(); | ||
| 289 | ret=(struct hostent *)OPENSSL_malloc(sizeof(struct hostent)); | ||
| 290 | if (ret == NULL) return(NULL); | ||
| 291 | memset(ret,0,sizeof(struct hostent)); | ||
| 292 | |||
| 293 | for (i=0; a->h_aliases[i] != NULL; i++) | ||
| 294 | ; | ||
| 295 | i++; | ||
| 296 | ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *)); | ||
| 297 | if (ret->h_aliases == NULL) | ||
| 298 | goto err; | ||
| 299 | memset(ret->h_aliases, 0, i*sizeof(char *)); | ||
| 300 | |||
| 301 | for (i=0; a->h_addr_list[i] != NULL; i++) | ||
| 302 | ; | ||
| 303 | i++; | ||
| 304 | ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *)); | ||
| 305 | if (ret->h_addr_list == NULL) | ||
| 306 | goto err; | ||
| 307 | memset(ret->h_addr_list, 0, i*sizeof(char *)); | ||
| 308 | |||
| 309 | j=strlen(a->h_name)+1; | ||
| 310 | if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err; | ||
| 311 | memcpy((char *)ret->h_name,a->h_name,j); | ||
| 312 | for (i=0; a->h_aliases[i] != NULL; i++) | ||
| 313 | { | ||
| 314 | j=strlen(a->h_aliases[i])+1; | ||
| 315 | if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err; | ||
| 316 | memcpy(ret->h_aliases[i],a->h_aliases[i],j); | ||
| 317 | } | ||
| 318 | ret->h_length=a->h_length; | ||
| 319 | ret->h_addrtype=a->h_addrtype; | ||
| 320 | for (i=0; a->h_addr_list[i] != NULL; i++) | ||
| 321 | { | ||
| 322 | if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL) | ||
| 323 | goto err; | ||
| 324 | memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length); | ||
| 325 | } | ||
| 326 | if (0) | ||
| 327 | { | ||
| 328 | err: | ||
| 329 | if (ret != NULL) | ||
| 330 | ghbn_free(ret); | ||
| 331 | ret=NULL; | ||
| 332 | } | ||
| 333 | MemCheck_on(); | ||
| 334 | return(ret); | ||
| 335 | } | ||
| 336 | |||
| 337 | static void ghbn_free(struct hostent *a) | ||
| 338 | { | ||
| 339 | int i; | ||
| 340 | |||
| 341 | if(a == NULL) | ||
| 342 | return; | ||
| 343 | |||
| 344 | if (a->h_aliases != NULL) | ||
| 345 | { | ||
| 346 | for (i=0; a->h_aliases[i] != NULL; i++) | ||
| 347 | OPENSSL_free(a->h_aliases[i]); | ||
| 348 | OPENSSL_free(a->h_aliases); | ||
| 349 | } | ||
| 350 | if (a->h_addr_list != NULL) | ||
| 351 | { | ||
| 352 | for (i=0; a->h_addr_list[i] != NULL; i++) | ||
| 353 | OPENSSL_free(a->h_addr_list[i]); | ||
| 354 | OPENSSL_free(a->h_addr_list); | ||
| 355 | } | ||
| 356 | if (a->h_name != NULL) OPENSSL_free(a->h_name); | ||
| 357 | OPENSSL_free(a); | ||
| 358 | } | ||
| 359 | |||
| 360 | #endif | ||
| 361 | |||
| 362 | struct hostent *BIO_gethostbyname(const char *name) | ||
| 363 | { | ||
| 364 | #if 1 | ||
| 365 | /* Caching gethostbyname() results forever is wrong, | ||
| 366 | * so we have to let the true gethostbyname() worry about this */ | ||
| 367 | #if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__)) | ||
| 368 | return gethostbyname((char*)name); | ||
| 369 | #else | ||
| 370 | return gethostbyname(name); | ||
| 371 | #endif | ||
| 372 | #else | ||
| 373 | struct hostent *ret; | ||
| 374 | int i,lowi=0,j; | ||
| 375 | unsigned long low= (unsigned long)-1; | ||
| 376 | |||
| 377 | |||
| 378 | # if 0 | ||
| 379 | /* It doesn't make sense to use locking here: The function interface | ||
| 380 | * is not thread-safe, because threads can never be sure when | ||
| 381 | * some other thread destroys the data they were given a pointer to. | ||
| 382 | */ | ||
| 383 | CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME); | ||
| 384 | # endif | ||
| 385 | j=strlen(name); | ||
| 386 | if (j < 128) | ||
| 387 | { | ||
| 388 | for (i=0; i<GHBN_NUM; i++) | ||
| 389 | { | ||
| 390 | if (low > ghbn_cache[i].order) | ||
| 391 | { | ||
| 392 | low=ghbn_cache[i].order; | ||
| 393 | lowi=i; | ||
| 394 | } | ||
| 395 | if (ghbn_cache[i].order > 0) | ||
| 396 | { | ||
| 397 | if (strncmp(name,ghbn_cache[i].name,128) == 0) | ||
| 398 | break; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | } | ||
| 402 | else | ||
| 403 | i=GHBN_NUM; | ||
| 404 | |||
| 405 | if (i == GHBN_NUM) /* no hit*/ | ||
| 406 | { | ||
| 407 | BIO_ghbn_miss++; | ||
| 408 | /* Note: under VMS with SOCKETSHR, it seems like the first | ||
| 409 | * parameter is 'char *', instead of 'const char *' | ||
| 410 | */ | ||
| 411 | # ifndef CONST_STRICT | ||
| 412 | ret=gethostbyname((char *)name); | ||
| 413 | # else | ||
| 414 | ret=gethostbyname(name); | ||
| 415 | # endif | ||
| 416 | |||
| 417 | if (ret == NULL) | ||
| 418 | goto end; | ||
| 419 | if (j > 128) /* too big to cache */ | ||
| 420 | { | ||
| 421 | # if 0 | ||
| 422 | /* If we were trying to make this function thread-safe (which | ||
| 423 | * is bound to fail), we'd have to give up in this case | ||
| 424 | * (or allocate more memory). */ | ||
| 425 | ret = NULL; | ||
| 426 | # endif | ||
| 427 | goto end; | ||
| 428 | } | ||
| 429 | |||
| 430 | /* else add to cache */ | ||
| 431 | if (ghbn_cache[lowi].ent != NULL) | ||
| 432 | ghbn_free(ghbn_cache[lowi].ent); /* XXX not thread-safe */ | ||
| 433 | ghbn_cache[lowi].name[0] = '\0'; | ||
| 434 | |||
| 435 | if((ret=ghbn_cache[lowi].ent=ghbn_dup(ret)) == NULL) | ||
| 436 | { | ||
| 437 | BIOerr(BIO_F_BIO_GETHOSTBYNAME,ERR_R_MALLOC_FAILURE); | ||
| 438 | goto end; | ||
| 439 | } | ||
| 440 | strncpy(ghbn_cache[lowi].name,name,128); | ||
| 441 | ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits; | ||
| 442 | } | ||
| 443 | else | ||
| 444 | { | ||
| 445 | BIO_ghbn_hits++; | ||
| 446 | ret= ghbn_cache[i].ent; | ||
| 447 | ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits; | ||
| 448 | } | ||
| 449 | end: | ||
| 450 | # if 0 | ||
| 451 | CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME); | ||
| 452 | # endif | ||
| 453 | return(ret); | ||
| 454 | #endif | ||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 458 | int BIO_sock_init(void) | ||
| 459 | { | ||
| 460 | #ifdef OPENSSL_SYS_WINDOWS | ||
| 461 | static struct WSAData wsa_state; | ||
| 462 | |||
| 463 | if (!wsa_init_done) | ||
| 464 | { | ||
| 465 | int err; | ||
| 466 | |||
| 467 | wsa_init_done=1; | ||
| 468 | memset(&wsa_state,0,sizeof(wsa_state)); | ||
| 469 | if (WSAStartup(0x0101,&wsa_state)!=0) | ||
| 470 | { | ||
| 471 | err=WSAGetLastError(); | ||
| 472 | SYSerr(SYS_F_WSASTARTUP,err); | ||
| 473 | BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP); | ||
| 474 | return(-1); | ||
| 475 | } | ||
| 476 | } | ||
| 477 | #endif /* OPENSSL_SYS_WINDOWS */ | ||
| 478 | #ifdef WATT32 | ||
| 479 | extern int _watt_do_exit; | ||
| 480 | _watt_do_exit = 0; /* don't make sock_init() call exit() */ | ||
| 481 | if (sock_init()) | ||
| 482 | return (-1); | ||
| 483 | #endif | ||
| 484 | |||
| 485 | #if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK) | ||
| 486 | WORD wVerReq; | ||
| 487 | WSADATA wsaData; | ||
| 488 | int err; | ||
| 489 | |||
| 490 | if (!wsa_init_done) | ||
| 491 | { | ||
| 492 | wsa_init_done=1; | ||
| 493 | wVerReq = MAKEWORD( 2, 0 ); | ||
| 494 | err = WSAStartup(wVerReq,&wsaData); | ||
| 495 | if (err != 0) | ||
| 496 | { | ||
| 497 | SYSerr(SYS_F_WSASTARTUP,err); | ||
| 498 | BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP); | ||
| 499 | return(-1); | ||
| 500 | } | ||
| 501 | } | ||
| 502 | #endif | ||
| 503 | |||
| 504 | return(1); | ||
| 505 | } | ||
| 506 | |||
| 507 | void BIO_sock_cleanup(void) | ||
| 508 | { | ||
| 509 | #ifdef OPENSSL_SYS_WINDOWS | ||
| 510 | if (wsa_init_done) | ||
| 511 | { | ||
| 512 | wsa_init_done=0; | ||
| 513 | #ifndef OPENSSL_SYS_WINCE | ||
| 514 | WSACancelBlockingCall(); /* Winsock 1.1 specific */ | ||
| 515 | #endif | ||
| 516 | WSACleanup(); | ||
| 517 | } | ||
| 518 | #elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK) | ||
| 519 | if (wsa_init_done) | ||
| 520 | { | ||
| 521 | wsa_init_done=0; | ||
| 522 | WSACleanup(); | ||
| 523 | } | ||
| 524 | #endif | ||
| 525 | } | ||
| 526 | |||
| 527 | #if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000 | ||
| 528 | |||
| 529 | int BIO_socket_ioctl(int fd, long type, void *arg) | ||
| 530 | { | ||
| 531 | int i; | ||
| 532 | |||
| 533 | #ifdef __DJGPP__ | ||
| 534 | i=ioctlsocket(fd,type,(char *)arg); | ||
| 535 | #else | ||
| 536 | i=ioctlsocket(fd,type,arg); | ||
| 537 | #endif /* __DJGPP__ */ | ||
| 538 | if (i < 0) | ||
| 539 | SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error()); | ||
| 540 | return(i); | ||
| 541 | } | ||
| 542 | #endif /* __VMS_VER */ | ||
| 543 | |||
| 544 | /* The reason I have implemented this instead of using sscanf is because | ||
| 545 | * Visual C 1.52c gives an unresolved external when linking a DLL :-( */ | ||
| 546 | static int get_ip(const char *str, unsigned char ip[4]) | ||
| 547 | { | ||
| 548 | unsigned int tmp[4]; | ||
| 549 | int num=0,c,ok=0; | ||
| 550 | |||
| 551 | tmp[0]=tmp[1]=tmp[2]=tmp[3]=0; | ||
| 552 | |||
| 553 | for (;;) | ||
| 554 | { | ||
| 555 | c= *(str++); | ||
| 556 | if ((c >= '0') && (c <= '9')) | ||
| 557 | { | ||
| 558 | ok=1; | ||
| 559 | tmp[num]=tmp[num]*10+c-'0'; | ||
| 560 | if (tmp[num] > 255) return(0); | ||
| 561 | } | ||
| 562 | else if (c == '.') | ||
| 563 | { | ||
| 564 | if (!ok) return(-1); | ||
| 565 | if (num == 3) return(0); | ||
| 566 | num++; | ||
| 567 | ok=0; | ||
| 568 | } | ||
| 569 | else if (c == '\0' && (num == 3) && ok) | ||
| 570 | break; | ||
| 571 | else | ||
| 572 | return(0); | ||
| 573 | } | ||
| 574 | ip[0]=tmp[0]; | ||
| 575 | ip[1]=tmp[1]; | ||
| 576 | ip[2]=tmp[2]; | ||
| 577 | ip[3]=tmp[3]; | ||
| 578 | return(1); | ||
| 579 | } | ||
| 580 | |||
| 581 | int BIO_get_accept_socket(char *host, int bind_mode) | ||
| 582 | { | ||
| 583 | int ret=0; | ||
| 584 | struct sockaddr_in server,client; | ||
| 585 | int s=INVALID_SOCKET,cs; | ||
| 586 | unsigned char ip[4]; | ||
| 587 | unsigned short port; | ||
| 588 | char *str=NULL,*e; | ||
| 589 | const char *h,*p; | ||
| 590 | unsigned long l; | ||
| 591 | int err_num; | ||
| 592 | |||
| 593 | if (BIO_sock_init() != 1) return(INVALID_SOCKET); | ||
| 594 | |||
| 595 | if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET); | ||
| 596 | |||
| 597 | h=p=NULL; | ||
| 598 | h=str; | ||
| 599 | for (e=str; *e; e++) | ||
| 600 | { | ||
| 601 | if (*e == ':') | ||
| 602 | { | ||
| 603 | p= &(e[1]); | ||
| 604 | *e='\0'; | ||
| 605 | } | ||
| 606 | else if (*e == '/') | ||
| 607 | { | ||
| 608 | *e='\0'; | ||
| 609 | break; | ||
| 610 | } | ||
| 611 | } | ||
| 612 | |||
| 613 | if (p == NULL) | ||
| 614 | { | ||
| 615 | p=h; | ||
| 616 | h="*"; | ||
| 617 | } | ||
| 618 | |||
| 619 | if (!BIO_get_port(p,&port)) goto err; | ||
| 620 | |||
| 621 | memset((char *)&server,0,sizeof(server)); | ||
| 622 | server.sin_family=AF_INET; | ||
| 623 | server.sin_port=htons(port); | ||
| 624 | |||
| 625 | if (strcmp(h,"*") == 0) | ||
| 626 | server.sin_addr.s_addr=INADDR_ANY; | ||
| 627 | else | ||
| 628 | { | ||
| 629 | if (!BIO_get_host_ip(h,&(ip[0]))) goto err; | ||
| 630 | l=(unsigned long) | ||
| 631 | ((unsigned long)ip[0]<<24L)| | ||
| 632 | ((unsigned long)ip[1]<<16L)| | ||
| 633 | ((unsigned long)ip[2]<< 8L)| | ||
| 634 | ((unsigned long)ip[3]); | ||
| 635 | server.sin_addr.s_addr=htonl(l); | ||
| 636 | } | ||
| 637 | |||
| 638 | again: | ||
| 639 | s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); | ||
| 640 | if (s == INVALID_SOCKET) | ||
| 641 | { | ||
| 642 | SYSerr(SYS_F_SOCKET,get_last_socket_error()); | ||
| 643 | ERR_add_error_data(3,"port='",host,"'"); | ||
| 644 | BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET); | ||
| 645 | goto err; | ||
| 646 | } | ||
| 647 | |||
| 648 | #ifdef SO_REUSEADDR | ||
| 649 | if (bind_mode == BIO_BIND_REUSEADDR) | ||
| 650 | { | ||
| 651 | int i=1; | ||
| 652 | |||
| 653 | ret=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i)); | ||
| 654 | bind_mode=BIO_BIND_NORMAL; | ||
| 655 | } | ||
| 656 | #endif | ||
| 657 | if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) | ||
| 658 | { | ||
| 659 | #ifdef SO_REUSEADDR | ||
| 660 | err_num=get_last_socket_error(); | ||
| 661 | if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) && | ||
| 662 | (err_num == EADDRINUSE)) | ||
| 663 | { | ||
| 664 | memcpy((char *)&client,(char *)&server,sizeof(server)); | ||
| 665 | if (strcmp(h,"*") == 0) | ||
| 666 | client.sin_addr.s_addr=htonl(0x7F000001); | ||
| 667 | cs=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); | ||
| 668 | if (cs != INVALID_SOCKET) | ||
| 669 | { | ||
| 670 | int ii; | ||
| 671 | ii=connect(cs,(struct sockaddr *)&client, | ||
| 672 | sizeof(client)); | ||
| 673 | closesocket(cs); | ||
| 674 | if (ii == INVALID_SOCKET) | ||
| 675 | { | ||
| 676 | bind_mode=BIO_BIND_REUSEADDR; | ||
| 677 | closesocket(s); | ||
| 678 | goto again; | ||
| 679 | } | ||
| 680 | /* else error */ | ||
| 681 | } | ||
| 682 | /* else error */ | ||
| 683 | } | ||
| 684 | #endif | ||
| 685 | SYSerr(SYS_F_BIND,err_num); | ||
| 686 | ERR_add_error_data(3,"port='",host,"'"); | ||
| 687 | BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET); | ||
| 688 | goto err; | ||
| 689 | } | ||
| 690 | if (listen(s,MAX_LISTEN) == -1) | ||
| 691 | { | ||
| 692 | SYSerr(SYS_F_BIND,get_last_socket_error()); | ||
| 693 | ERR_add_error_data(3,"port='",host,"'"); | ||
| 694 | BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET); | ||
| 695 | goto err; | ||
| 696 | } | ||
| 697 | ret=1; | ||
| 698 | err: | ||
| 699 | if (str != NULL) OPENSSL_free(str); | ||
| 700 | if ((ret == 0) && (s != INVALID_SOCKET)) | ||
| 701 | { | ||
| 702 | closesocket(s); | ||
| 703 | s= INVALID_SOCKET; | ||
| 704 | } | ||
| 705 | return(s); | ||
| 706 | } | ||
| 707 | |||
| 708 | int BIO_accept(int sock, char **addr) | ||
| 709 | { | ||
| 710 | int ret=INVALID_SOCKET; | ||
| 711 | static struct sockaddr_in from; | ||
| 712 | unsigned long l; | ||
| 713 | unsigned short port; | ||
| 714 | int len; | ||
| 715 | char *p; | ||
| 716 | |||
| 717 | memset((char *)&from,0,sizeof(from)); | ||
| 718 | len=sizeof(from); | ||
| 719 | /* Note: under VMS with SOCKETSHR the fourth parameter is currently | ||
| 720 | * of type (int *) whereas under other systems it is (void *) if | ||
| 721 | * you don't have a cast it will choke the compiler: if you do | ||
| 722 | * have a cast then you can either go for (int *) or (void *). | ||
| 723 | */ | ||
| 724 | ret=accept(sock,(struct sockaddr *)&from,(void *)&len); | ||
| 725 | if (ret == INVALID_SOCKET) | ||
| 726 | { | ||
| 727 | if(BIO_sock_should_retry(ret)) return -2; | ||
| 728 | SYSerr(SYS_F_ACCEPT,get_last_socket_error()); | ||
| 729 | BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR); | ||
| 730 | goto end; | ||
| 731 | } | ||
| 732 | |||
| 733 | if (addr == NULL) goto end; | ||
| 734 | |||
| 735 | l=ntohl(from.sin_addr.s_addr); | ||
| 736 | port=ntohs(from.sin_port); | ||
| 737 | if (*addr == NULL) | ||
| 738 | { | ||
| 739 | if ((p=OPENSSL_malloc(24)) == NULL) | ||
| 740 | { | ||
| 741 | BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE); | ||
| 742 | goto end; | ||
| 743 | } | ||
| 744 | *addr=p; | ||
| 745 | } | ||
| 746 | BIO_snprintf(*addr,24,"%d.%d.%d.%d:%d", | ||
| 747 | (unsigned char)(l>>24L)&0xff, | ||
| 748 | (unsigned char)(l>>16L)&0xff, | ||
| 749 | (unsigned char)(l>> 8L)&0xff, | ||
| 750 | (unsigned char)(l )&0xff, | ||
| 751 | port); | ||
| 752 | end: | ||
| 753 | return(ret); | ||
| 754 | } | ||
| 755 | |||
| 756 | int BIO_set_tcp_ndelay(int s, int on) | ||
| 757 | { | ||
| 758 | int ret=0; | ||
| 759 | #if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) | ||
| 760 | int opt; | ||
| 761 | |||
| 762 | #ifdef SOL_TCP | ||
| 763 | opt=SOL_TCP; | ||
| 764 | #else | ||
| 765 | #ifdef IPPROTO_TCP | ||
| 766 | opt=IPPROTO_TCP; | ||
| 767 | #endif | ||
| 768 | #endif | ||
| 769 | |||
| 770 | ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on)); | ||
| 771 | #endif | ||
| 772 | return(ret == 0); | ||
| 773 | } | ||
| 774 | #endif | ||
| 775 | |||
| 776 | int BIO_socket_nbio(int s, int mode) | ||
| 777 | { | ||
| 778 | int ret= -1; | ||
| 779 | int l; | ||
| 780 | |||
| 781 | l=mode; | ||
| 782 | #ifdef FIONBIO | ||
| 783 | ret=BIO_socket_ioctl(s,FIONBIO,&l); | ||
| 784 | #endif | ||
| 785 | return(ret == 0); | ||
| 786 | } | ||
diff --git a/src/lib/libcrypto/bio/bf_buff.c b/src/lib/libcrypto/bio/bf_buff.c new file mode 100644 index 0000000000..c1fd75aaad --- /dev/null +++ b/src/lib/libcrypto/bio/bf_buff.c | |||
| @@ -0,0 +1,511 @@ | |||
| 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 <openssl/bio.h> | ||
| 63 | |||
| 64 | static int buffer_write(BIO *h, const char *buf,int num); | ||
| 65 | static int buffer_read(BIO *h, char *buf, int size); | ||
| 66 | static int buffer_puts(BIO *h, const char *str); | ||
| 67 | static int buffer_gets(BIO *h, char *str, int size); | ||
| 68 | static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 69 | static int buffer_new(BIO *h); | ||
| 70 | static int buffer_free(BIO *data); | ||
| 71 | static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); | ||
| 72 | #define DEFAULT_BUFFER_SIZE 4096 | ||
| 73 | |||
| 74 | static BIO_METHOD methods_buffer= | ||
| 75 | { | ||
| 76 | BIO_TYPE_BUFFER, | ||
| 77 | "buffer", | ||
| 78 | buffer_write, | ||
| 79 | buffer_read, | ||
| 80 | buffer_puts, | ||
| 81 | buffer_gets, | ||
| 82 | buffer_ctrl, | ||
| 83 | buffer_new, | ||
| 84 | buffer_free, | ||
| 85 | buffer_callback_ctrl, | ||
| 86 | }; | ||
| 87 | |||
| 88 | BIO_METHOD *BIO_f_buffer(void) | ||
| 89 | { | ||
| 90 | return(&methods_buffer); | ||
| 91 | } | ||
| 92 | |||
| 93 | static int buffer_new(BIO *bi) | ||
| 94 | { | ||
| 95 | BIO_F_BUFFER_CTX *ctx; | ||
| 96 | |||
| 97 | ctx=(BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX)); | ||
| 98 | if (ctx == NULL) return(0); | ||
| 99 | ctx->ibuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE); | ||
| 100 | if (ctx->ibuf == NULL) { OPENSSL_free(ctx); return(0); } | ||
| 101 | ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE); | ||
| 102 | if (ctx->obuf == NULL) { OPENSSL_free(ctx->ibuf); OPENSSL_free(ctx); return(0); } | ||
| 103 | ctx->ibuf_size=DEFAULT_BUFFER_SIZE; | ||
| 104 | ctx->obuf_size=DEFAULT_BUFFER_SIZE; | ||
| 105 | ctx->ibuf_len=0; | ||
| 106 | ctx->ibuf_off=0; | ||
| 107 | ctx->obuf_len=0; | ||
| 108 | ctx->obuf_off=0; | ||
| 109 | |||
| 110 | bi->init=1; | ||
| 111 | bi->ptr=(char *)ctx; | ||
| 112 | bi->flags=0; | ||
| 113 | return(1); | ||
| 114 | } | ||
| 115 | |||
| 116 | static int buffer_free(BIO *a) | ||
| 117 | { | ||
| 118 | BIO_F_BUFFER_CTX *b; | ||
| 119 | |||
| 120 | if (a == NULL) return(0); | ||
| 121 | b=(BIO_F_BUFFER_CTX *)a->ptr; | ||
| 122 | if (b->ibuf != NULL) OPENSSL_free(b->ibuf); | ||
| 123 | if (b->obuf != NULL) OPENSSL_free(b->obuf); | ||
| 124 | OPENSSL_free(a->ptr); | ||
| 125 | a->ptr=NULL; | ||
| 126 | a->init=0; | ||
| 127 | a->flags=0; | ||
| 128 | return(1); | ||
| 129 | } | ||
| 130 | |||
| 131 | static int buffer_read(BIO *b, char *out, int outl) | ||
| 132 | { | ||
| 133 | int i,num=0; | ||
| 134 | BIO_F_BUFFER_CTX *ctx; | ||
| 135 | |||
| 136 | if (out == NULL) return(0); | ||
| 137 | ctx=(BIO_F_BUFFER_CTX *)b->ptr; | ||
| 138 | |||
| 139 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
| 140 | num=0; | ||
| 141 | BIO_clear_retry_flags(b); | ||
| 142 | |||
| 143 | start: | ||
| 144 | i=ctx->ibuf_len; | ||
| 145 | /* If there is stuff left over, grab it */ | ||
| 146 | if (i != 0) | ||
| 147 | { | ||
| 148 | if (i > outl) i=outl; | ||
| 149 | memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i); | ||
| 150 | ctx->ibuf_off+=i; | ||
| 151 | ctx->ibuf_len-=i; | ||
| 152 | num+=i; | ||
| 153 | if (outl == i) return(num); | ||
| 154 | outl-=i; | ||
| 155 | out+=i; | ||
| 156 | } | ||
| 157 | |||
| 158 | /* We may have done a partial read. try to do more. | ||
| 159 | * We have nothing in the buffer. | ||
| 160 | * If we get an error and have read some data, just return it | ||
| 161 | * and let them retry to get the error again. | ||
| 162 | * copy direct to parent address space */ | ||
| 163 | if (outl > ctx->ibuf_size) | ||
| 164 | { | ||
| 165 | for (;;) | ||
| 166 | { | ||
| 167 | i=BIO_read(b->next_bio,out,outl); | ||
| 168 | if (i <= 0) | ||
| 169 | { | ||
| 170 | BIO_copy_next_retry(b); | ||
| 171 | if (i < 0) return((num > 0)?num:i); | ||
| 172 | if (i == 0) return(num); | ||
| 173 | } | ||
| 174 | num+=i; | ||
| 175 | if (outl == i) return(num); | ||
| 176 | out+=i; | ||
| 177 | outl-=i; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | /* else */ | ||
| 181 | |||
| 182 | /* we are going to be doing some buffering */ | ||
| 183 | i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size); | ||
| 184 | if (i <= 0) | ||
| 185 | { | ||
| 186 | BIO_copy_next_retry(b); | ||
| 187 | if (i < 0) return((num > 0)?num:i); | ||
| 188 | if (i == 0) return(num); | ||
| 189 | } | ||
| 190 | ctx->ibuf_off=0; | ||
| 191 | ctx->ibuf_len=i; | ||
| 192 | |||
| 193 | /* Lets re-read using ourselves :-) */ | ||
| 194 | goto start; | ||
| 195 | } | ||
| 196 | |||
| 197 | static int buffer_write(BIO *b, const char *in, int inl) | ||
| 198 | { | ||
| 199 | int i,num=0; | ||
| 200 | BIO_F_BUFFER_CTX *ctx; | ||
| 201 | |||
| 202 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 203 | ctx=(BIO_F_BUFFER_CTX *)b->ptr; | ||
| 204 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
| 205 | |||
| 206 | BIO_clear_retry_flags(b); | ||
| 207 | start: | ||
| 208 | i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off); | ||
| 209 | /* add to buffer and return */ | ||
| 210 | if (i >= inl) | ||
| 211 | { | ||
| 212 | memcpy(&(ctx->obuf[ctx->obuf_len]),in,inl); | ||
| 213 | ctx->obuf_len+=inl; | ||
| 214 | return(num+inl); | ||
| 215 | } | ||
| 216 | /* else */ | ||
| 217 | /* stuff already in buffer, so add to it first, then flush */ | ||
| 218 | if (ctx->obuf_len != 0) | ||
| 219 | { | ||
| 220 | if (i > 0) /* lets fill it up if we can */ | ||
| 221 | { | ||
| 222 | memcpy(&(ctx->obuf[ctx->obuf_len]),in,i); | ||
| 223 | in+=i; | ||
| 224 | inl-=i; | ||
| 225 | num+=i; | ||
| 226 | ctx->obuf_len+=i; | ||
| 227 | } | ||
| 228 | /* we now have a full buffer needing flushing */ | ||
| 229 | for (;;) | ||
| 230 | { | ||
| 231 | i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]), | ||
| 232 | ctx->obuf_len); | ||
| 233 | if (i <= 0) | ||
| 234 | { | ||
| 235 | BIO_copy_next_retry(b); | ||
| 236 | |||
| 237 | if (i < 0) return((num > 0)?num:i); | ||
| 238 | if (i == 0) return(num); | ||
| 239 | } | ||
| 240 | ctx->obuf_off+=i; | ||
| 241 | ctx->obuf_len-=i; | ||
| 242 | if (ctx->obuf_len == 0) break; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | /* we only get here if the buffer has been flushed and we | ||
| 246 | * still have stuff to write */ | ||
| 247 | ctx->obuf_off=0; | ||
| 248 | |||
| 249 | /* we now have inl bytes to write */ | ||
| 250 | while (inl >= ctx->obuf_size) | ||
| 251 | { | ||
| 252 | i=BIO_write(b->next_bio,in,inl); | ||
| 253 | if (i <= 0) | ||
| 254 | { | ||
| 255 | BIO_copy_next_retry(b); | ||
| 256 | if (i < 0) return((num > 0)?num:i); | ||
| 257 | if (i == 0) return(num); | ||
| 258 | } | ||
| 259 | num+=i; | ||
| 260 | in+=i; | ||
| 261 | inl-=i; | ||
| 262 | if (inl == 0) return(num); | ||
| 263 | } | ||
| 264 | |||
| 265 | /* copy the rest into the buffer since we have only a small | ||
| 266 | * amount left */ | ||
| 267 | goto start; | ||
| 268 | } | ||
| 269 | |||
| 270 | static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 271 | { | ||
| 272 | BIO *dbio; | ||
| 273 | BIO_F_BUFFER_CTX *ctx; | ||
| 274 | long ret=1; | ||
| 275 | char *p1,*p2; | ||
| 276 | int r,i,*ip; | ||
| 277 | int ibs,obs; | ||
| 278 | |||
| 279 | ctx=(BIO_F_BUFFER_CTX *)b->ptr; | ||
| 280 | |||
| 281 | switch (cmd) | ||
| 282 | { | ||
| 283 | case BIO_CTRL_RESET: | ||
| 284 | ctx->ibuf_off=0; | ||
| 285 | ctx->ibuf_len=0; | ||
| 286 | ctx->obuf_off=0; | ||
| 287 | ctx->obuf_len=0; | ||
| 288 | if (b->next_bio == NULL) return(0); | ||
| 289 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 290 | break; | ||
| 291 | case BIO_CTRL_INFO: | ||
| 292 | ret=(long)ctx->obuf_len; | ||
| 293 | break; | ||
| 294 | case BIO_C_GET_BUFF_NUM_LINES: | ||
| 295 | ret=0; | ||
| 296 | p1=ctx->ibuf; | ||
| 297 | for (i=ctx->ibuf_off; i<ctx->ibuf_len; i++) | ||
| 298 | { | ||
| 299 | if (p1[i] == '\n') ret++; | ||
| 300 | } | ||
| 301 | break; | ||
| 302 | case BIO_CTRL_WPENDING: | ||
| 303 | ret=(long)ctx->obuf_len; | ||
| 304 | if (ret == 0) | ||
| 305 | { | ||
| 306 | if (b->next_bio == NULL) return(0); | ||
| 307 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 308 | } | ||
| 309 | break; | ||
| 310 | case BIO_CTRL_PENDING: | ||
| 311 | ret=(long)ctx->ibuf_len; | ||
| 312 | if (ret == 0) | ||
| 313 | { | ||
| 314 | if (b->next_bio == NULL) return(0); | ||
| 315 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 316 | } | ||
| 317 | break; | ||
| 318 | case BIO_C_SET_BUFF_READ_DATA: | ||
| 319 | if (num > ctx->ibuf_size) | ||
| 320 | { | ||
| 321 | p1=OPENSSL_malloc((int)num); | ||
| 322 | if (p1 == NULL) goto malloc_error; | ||
| 323 | if (ctx->ibuf != NULL) OPENSSL_free(ctx->ibuf); | ||
| 324 | ctx->ibuf=p1; | ||
| 325 | } | ||
| 326 | ctx->ibuf_off=0; | ||
| 327 | ctx->ibuf_len=(int)num; | ||
| 328 | memcpy(ctx->ibuf,ptr,(int)num); | ||
| 329 | ret=1; | ||
| 330 | break; | ||
| 331 | case BIO_C_SET_BUFF_SIZE: | ||
| 332 | if (ptr != NULL) | ||
| 333 | { | ||
| 334 | ip=(int *)ptr; | ||
| 335 | if (*ip == 0) | ||
| 336 | { | ||
| 337 | ibs=(int)num; | ||
| 338 | obs=ctx->obuf_size; | ||
| 339 | } | ||
| 340 | else /* if (*ip == 1) */ | ||
| 341 | { | ||
| 342 | ibs=ctx->ibuf_size; | ||
| 343 | obs=(int)num; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | else | ||
| 347 | { | ||
| 348 | ibs=(int)num; | ||
| 349 | obs=(int)num; | ||
| 350 | } | ||
| 351 | p1=ctx->ibuf; | ||
| 352 | p2=ctx->obuf; | ||
| 353 | if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) | ||
| 354 | { | ||
| 355 | p1=(char *)OPENSSL_malloc((int)num); | ||
| 356 | if (p1 == NULL) goto malloc_error; | ||
| 357 | } | ||
| 358 | if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) | ||
| 359 | { | ||
| 360 | p2=(char *)OPENSSL_malloc((int)num); | ||
| 361 | if (p2 == NULL) | ||
| 362 | { | ||
| 363 | if (p1 != ctx->ibuf) OPENSSL_free(p1); | ||
| 364 | goto malloc_error; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | if (ctx->ibuf != p1) | ||
| 368 | { | ||
| 369 | OPENSSL_free(ctx->ibuf); | ||
| 370 | ctx->ibuf=p1; | ||
| 371 | ctx->ibuf_off=0; | ||
| 372 | ctx->ibuf_len=0; | ||
| 373 | ctx->ibuf_size=ibs; | ||
| 374 | } | ||
| 375 | if (ctx->obuf != p2) | ||
| 376 | { | ||
| 377 | OPENSSL_free(ctx->obuf); | ||
| 378 | ctx->obuf=p2; | ||
| 379 | ctx->obuf_off=0; | ||
| 380 | ctx->obuf_len=0; | ||
| 381 | ctx->obuf_size=obs; | ||
| 382 | } | ||
| 383 | break; | ||
| 384 | case BIO_C_DO_STATE_MACHINE: | ||
| 385 | if (b->next_bio == NULL) return(0); | ||
| 386 | BIO_clear_retry_flags(b); | ||
| 387 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 388 | BIO_copy_next_retry(b); | ||
| 389 | break; | ||
| 390 | |||
| 391 | case BIO_CTRL_FLUSH: | ||
| 392 | if (b->next_bio == NULL) return(0); | ||
| 393 | if (ctx->obuf_len <= 0) | ||
| 394 | { | ||
| 395 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 396 | break; | ||
| 397 | } | ||
| 398 | |||
| 399 | for (;;) | ||
| 400 | { | ||
| 401 | BIO_clear_retry_flags(b); | ||
| 402 | if (ctx->obuf_len > ctx->obuf_off) | ||
| 403 | { | ||
| 404 | r=BIO_write(b->next_bio, | ||
| 405 | &(ctx->obuf[ctx->obuf_off]), | ||
| 406 | ctx->obuf_len-ctx->obuf_off); | ||
| 407 | #if 0 | ||
| 408 | fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_off,r); | ||
| 409 | #endif | ||
| 410 | BIO_copy_next_retry(b); | ||
| 411 | if (r <= 0) return((long)r); | ||
| 412 | ctx->obuf_off+=r; | ||
| 413 | } | ||
| 414 | else | ||
| 415 | { | ||
| 416 | ctx->obuf_len=0; | ||
| 417 | ctx->obuf_off=0; | ||
| 418 | ret=1; | ||
| 419 | break; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 423 | break; | ||
| 424 | case BIO_CTRL_DUP: | ||
| 425 | dbio=(BIO *)ptr; | ||
| 426 | if ( !BIO_set_read_buffer_size(dbio,ctx->ibuf_size) || | ||
| 427 | !BIO_set_write_buffer_size(dbio,ctx->obuf_size)) | ||
| 428 | ret=0; | ||
| 429 | break; | ||
| 430 | default: | ||
| 431 | if (b->next_bio == NULL) return(0); | ||
| 432 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 433 | break; | ||
| 434 | } | ||
| 435 | return(ret); | ||
| 436 | malloc_error: | ||
| 437 | BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE); | ||
| 438 | return(0); | ||
| 439 | } | ||
| 440 | |||
| 441 | static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 442 | { | ||
| 443 | long ret=1; | ||
| 444 | |||
| 445 | if (b->next_bio == NULL) return(0); | ||
| 446 | switch (cmd) | ||
| 447 | { | ||
| 448 | default: | ||
| 449 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
| 450 | break; | ||
| 451 | } | ||
| 452 | return(ret); | ||
| 453 | } | ||
| 454 | |||
| 455 | static int buffer_gets(BIO *b, char *buf, 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 || size == 0) | ||
| 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 | *buf='\0'; | ||
| 498 | if (i < 0) return((num > 0)?num:i); | ||
| 499 | if (i == 0) return(num); | ||
| 500 | } | ||
| 501 | ctx->ibuf_len=i; | ||
| 502 | ctx->ibuf_off=0; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | static int buffer_puts(BIO *b, const char *str) | ||
| 508 | { | ||
| 509 | return(buffer_write(b,str,strlen(str))); | ||
| 510 | } | ||
| 511 | |||
diff --git a/src/lib/libcrypto/bio/bf_lbuf.c b/src/lib/libcrypto/bio/bf_lbuf.c new file mode 100644 index 0000000000..ec0f7eb0b7 --- /dev/null +++ b/src/lib/libcrypto/bio/bf_lbuf.c | |||
| @@ -0,0 +1,397 @@ | |||
| 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 <openssl/bio.h> | ||
| 63 | #include <openssl/evp.h> | ||
| 64 | |||
| 65 | static int linebuffer_write(BIO *h, const char *buf,int num); | ||
| 66 | static int linebuffer_read(BIO *h, char *buf, int size); | ||
| 67 | static int linebuffer_puts(BIO *h, const char *str); | ||
| 68 | static int linebuffer_gets(BIO *h, char *str, int size); | ||
| 69 | static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 70 | static int linebuffer_new(BIO *h); | ||
| 71 | static int linebuffer_free(BIO *data); | ||
| 72 | static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); | ||
| 73 | |||
| 74 | /* A 10k maximum should be enough for most purposes */ | ||
| 75 | #define DEFAULT_LINEBUFFER_SIZE 1024*10 | ||
| 76 | |||
| 77 | /* #define DEBUG */ | ||
| 78 | |||
| 79 | static BIO_METHOD methods_linebuffer= | ||
| 80 | { | ||
| 81 | BIO_TYPE_LINEBUFFER, | ||
| 82 | "linebuffer", | ||
| 83 | linebuffer_write, | ||
| 84 | linebuffer_read, | ||
| 85 | linebuffer_puts, | ||
| 86 | linebuffer_gets, | ||
| 87 | linebuffer_ctrl, | ||
| 88 | linebuffer_new, | ||
| 89 | linebuffer_free, | ||
| 90 | linebuffer_callback_ctrl, | ||
| 91 | }; | ||
| 92 | |||
| 93 | BIO_METHOD *BIO_f_linebuffer(void) | ||
| 94 | { | ||
| 95 | return(&methods_linebuffer); | ||
| 96 | } | ||
| 97 | |||
| 98 | typedef struct bio_linebuffer_ctx_struct | ||
| 99 | { | ||
| 100 | char *obuf; /* the output char array */ | ||
| 101 | int obuf_size; /* how big is the output buffer */ | ||
| 102 | int obuf_len; /* how many bytes are in it */ | ||
| 103 | } BIO_LINEBUFFER_CTX; | ||
| 104 | |||
| 105 | static int linebuffer_new(BIO *bi) | ||
| 106 | { | ||
| 107 | BIO_LINEBUFFER_CTX *ctx; | ||
| 108 | |||
| 109 | ctx=(BIO_LINEBUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_LINEBUFFER_CTX)); | ||
| 110 | if (ctx == NULL) return(0); | ||
| 111 | ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE); | ||
| 112 | if (ctx->obuf == NULL) { OPENSSL_free(ctx); return(0); } | ||
| 113 | ctx->obuf_size=DEFAULT_LINEBUFFER_SIZE; | ||
| 114 | ctx->obuf_len=0; | ||
| 115 | |||
| 116 | bi->init=1; | ||
| 117 | bi->ptr=(char *)ctx; | ||
| 118 | bi->flags=0; | ||
| 119 | return(1); | ||
| 120 | } | ||
| 121 | |||
| 122 | static int linebuffer_free(BIO *a) | ||
| 123 | { | ||
| 124 | BIO_LINEBUFFER_CTX *b; | ||
| 125 | |||
| 126 | if (a == NULL) return(0); | ||
| 127 | b=(BIO_LINEBUFFER_CTX *)a->ptr; | ||
| 128 | if (b->obuf != NULL) OPENSSL_free(b->obuf); | ||
| 129 | OPENSSL_free(a->ptr); | ||
| 130 | a->ptr=NULL; | ||
| 131 | a->init=0; | ||
| 132 | a->flags=0; | ||
| 133 | return(1); | ||
| 134 | } | ||
| 135 | |||
| 136 | static int linebuffer_read(BIO *b, char *out, int outl) | ||
| 137 | { | ||
| 138 | int ret=0; | ||
| 139 | |||
| 140 | if (out == NULL) return(0); | ||
| 141 | if (b->next_bio == NULL) return(0); | ||
| 142 | ret=BIO_read(b->next_bio,out,outl); | ||
| 143 | BIO_clear_retry_flags(b); | ||
| 144 | BIO_copy_next_retry(b); | ||
| 145 | return(ret); | ||
| 146 | } | ||
| 147 | |||
| 148 | static int linebuffer_write(BIO *b, const char *in, int inl) | ||
| 149 | { | ||
| 150 | int i,num=0,foundnl; | ||
| 151 | BIO_LINEBUFFER_CTX *ctx; | ||
| 152 | |||
| 153 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 154 | ctx=(BIO_LINEBUFFER_CTX *)b->ptr; | ||
| 155 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
| 156 | |||
| 157 | BIO_clear_retry_flags(b); | ||
| 158 | |||
| 159 | do | ||
| 160 | { | ||
| 161 | const char *p; | ||
| 162 | |||
| 163 | for(p = in; p < in + inl && *p != '\n'; p++) | ||
| 164 | ; | ||
| 165 | if (*p == '\n') | ||
| 166 | { | ||
| 167 | p++; | ||
| 168 | foundnl = 1; | ||
| 169 | } | ||
| 170 | else | ||
| 171 | foundnl = 0; | ||
| 172 | |||
| 173 | /* If a NL was found and we already have text in the save | ||
| 174 | buffer, concatenate them and write */ | ||
| 175 | while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len) | ||
| 176 | && ctx->obuf_len > 0) | ||
| 177 | { | ||
| 178 | int orig_olen = ctx->obuf_len; | ||
| 179 | |||
| 180 | i = ctx->obuf_size - ctx->obuf_len; | ||
| 181 | if (p - in > 0) | ||
| 182 | { | ||
| 183 | if (i >= p - in) | ||
| 184 | { | ||
| 185 | memcpy(&(ctx->obuf[ctx->obuf_len]), | ||
| 186 | in,p - in); | ||
| 187 | ctx->obuf_len += p - in; | ||
| 188 | inl -= p - in; | ||
| 189 | num += p - in; | ||
| 190 | in = p; | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | memcpy(&(ctx->obuf[ctx->obuf_len]), | ||
| 195 | in,i); | ||
| 196 | ctx->obuf_len += i; | ||
| 197 | inl -= i; | ||
| 198 | in += i; | ||
| 199 | num += i; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | #if 0 | ||
| 204 | BIO_write(b->next_bio, "<*<", 3); | ||
| 205 | #endif | ||
| 206 | i=BIO_write(b->next_bio, | ||
| 207 | ctx->obuf, ctx->obuf_len); | ||
| 208 | if (i <= 0) | ||
| 209 | { | ||
| 210 | ctx->obuf_len = orig_olen; | ||
| 211 | BIO_copy_next_retry(b); | ||
| 212 | |||
| 213 | #if 0 | ||
| 214 | BIO_write(b->next_bio, ">*>", 3); | ||
| 215 | #endif | ||
| 216 | if (i < 0) return((num > 0)?num:i); | ||
| 217 | if (i == 0) return(num); | ||
| 218 | } | ||
| 219 | #if 0 | ||
| 220 | BIO_write(b->next_bio, ">*>", 3); | ||
| 221 | #endif | ||
| 222 | if (i < ctx->obuf_len) | ||
| 223 | memmove(ctx->obuf, ctx->obuf + i, | ||
| 224 | ctx->obuf_len - i); | ||
| 225 | ctx->obuf_len-=i; | ||
| 226 | } | ||
| 227 | |||
| 228 | /* Now that the save buffer is emptied, let's write the input | ||
| 229 | buffer if a NL was found and there is anything to write. */ | ||
| 230 | if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) | ||
| 231 | { | ||
| 232 | #if 0 | ||
| 233 | BIO_write(b->next_bio, "<*<", 3); | ||
| 234 | #endif | ||
| 235 | i=BIO_write(b->next_bio,in,p - in); | ||
| 236 | if (i <= 0) | ||
| 237 | { | ||
| 238 | BIO_copy_next_retry(b); | ||
| 239 | #if 0 | ||
| 240 | BIO_write(b->next_bio, ">*>", 3); | ||
| 241 | #endif | ||
| 242 | if (i < 0) return((num > 0)?num:i); | ||
| 243 | if (i == 0) return(num); | ||
| 244 | } | ||
| 245 | #if 0 | ||
| 246 | BIO_write(b->next_bio, ">*>", 3); | ||
| 247 | #endif | ||
| 248 | num+=i; | ||
| 249 | in+=i; | ||
| 250 | inl-=i; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | while(foundnl && inl > 0); | ||
| 254 | /* We've written as much as we can. The rest of the input buffer, if | ||
| 255 | any, is text that doesn't and with a NL and therefore needs to be | ||
| 256 | saved for the next trip. */ | ||
| 257 | if (inl > 0) | ||
| 258 | { | ||
| 259 | memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl); | ||
| 260 | ctx->obuf_len += inl; | ||
| 261 | num += inl; | ||
| 262 | } | ||
| 263 | return num; | ||
| 264 | } | ||
| 265 | |||
| 266 | static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 267 | { | ||
| 268 | BIO *dbio; | ||
| 269 | BIO_LINEBUFFER_CTX *ctx; | ||
| 270 | long ret=1; | ||
| 271 | char *p; | ||
| 272 | int r; | ||
| 273 | int obs; | ||
| 274 | |||
| 275 | ctx=(BIO_LINEBUFFER_CTX *)b->ptr; | ||
| 276 | |||
| 277 | switch (cmd) | ||
| 278 | { | ||
| 279 | case BIO_CTRL_RESET: | ||
| 280 | ctx->obuf_len=0; | ||
| 281 | if (b->next_bio == NULL) return(0); | ||
| 282 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 283 | break; | ||
| 284 | case BIO_CTRL_INFO: | ||
| 285 | ret=(long)ctx->obuf_len; | ||
| 286 | break; | ||
| 287 | case BIO_CTRL_WPENDING: | ||
| 288 | ret=(long)ctx->obuf_len; | ||
| 289 | if (ret == 0) | ||
| 290 | { | ||
| 291 | if (b->next_bio == NULL) return(0); | ||
| 292 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 293 | } | ||
| 294 | break; | ||
| 295 | case BIO_C_SET_BUFF_SIZE: | ||
| 296 | obs=(int)num; | ||
| 297 | p=ctx->obuf; | ||
| 298 | if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) | ||
| 299 | { | ||
| 300 | p=(char *)OPENSSL_malloc((int)num); | ||
| 301 | if (p == NULL) | ||
| 302 | goto malloc_error; | ||
| 303 | } | ||
| 304 | if (ctx->obuf != p) | ||
| 305 | { | ||
| 306 | if (ctx->obuf_len > obs) | ||
| 307 | { | ||
| 308 | ctx->obuf_len = obs; | ||
| 309 | } | ||
| 310 | memcpy(p, ctx->obuf, ctx->obuf_len); | ||
| 311 | OPENSSL_free(ctx->obuf); | ||
| 312 | ctx->obuf=p; | ||
| 313 | ctx->obuf_size=obs; | ||
| 314 | } | ||
| 315 | break; | ||
| 316 | case BIO_C_DO_STATE_MACHINE: | ||
| 317 | if (b->next_bio == NULL) return(0); | ||
| 318 | BIO_clear_retry_flags(b); | ||
| 319 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 320 | BIO_copy_next_retry(b); | ||
| 321 | break; | ||
| 322 | |||
| 323 | case BIO_CTRL_FLUSH: | ||
| 324 | if (b->next_bio == NULL) return(0); | ||
| 325 | if (ctx->obuf_len <= 0) | ||
| 326 | { | ||
| 327 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 328 | break; | ||
| 329 | } | ||
| 330 | |||
| 331 | for (;;) | ||
| 332 | { | ||
| 333 | BIO_clear_retry_flags(b); | ||
| 334 | if (ctx->obuf_len > 0) | ||
| 335 | { | ||
| 336 | r=BIO_write(b->next_bio, | ||
| 337 | ctx->obuf, ctx->obuf_len); | ||
| 338 | #if 0 | ||
| 339 | fprintf(stderr,"FLUSH %3d -> %3d\n",ctx->obuf_len,r); | ||
| 340 | #endif | ||
| 341 | BIO_copy_next_retry(b); | ||
| 342 | if (r <= 0) return((long)r); | ||
| 343 | if (r < ctx->obuf_len) | ||
| 344 | memmove(ctx->obuf, ctx->obuf + r, | ||
| 345 | ctx->obuf_len - r); | ||
| 346 | ctx->obuf_len-=r; | ||
| 347 | } | ||
| 348 | else | ||
| 349 | { | ||
| 350 | ctx->obuf_len=0; | ||
| 351 | ret=1; | ||
| 352 | break; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 356 | break; | ||
| 357 | case BIO_CTRL_DUP: | ||
| 358 | dbio=(BIO *)ptr; | ||
| 359 | if ( !BIO_set_write_buffer_size(dbio,ctx->obuf_size)) | ||
| 360 | ret=0; | ||
| 361 | break; | ||
| 362 | default: | ||
| 363 | if (b->next_bio == NULL) return(0); | ||
| 364 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 365 | break; | ||
| 366 | } | ||
| 367 | return(ret); | ||
| 368 | malloc_error: | ||
| 369 | BIOerr(BIO_F_LINEBUFFER_CTRL,ERR_R_MALLOC_FAILURE); | ||
| 370 | return(0); | ||
| 371 | } | ||
| 372 | |||
| 373 | static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 374 | { | ||
| 375 | long ret=1; | ||
| 376 | |||
| 377 | if (b->next_bio == NULL) return(0); | ||
| 378 | switch (cmd) | ||
| 379 | { | ||
| 380 | default: | ||
| 381 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
| 382 | break; | ||
| 383 | } | ||
| 384 | return(ret); | ||
| 385 | } | ||
| 386 | |||
| 387 | static int linebuffer_gets(BIO *b, char *buf, int size) | ||
| 388 | { | ||
| 389 | if (b->next_bio == NULL) return(0); | ||
| 390 | return(BIO_gets(b->next_bio,buf,size)); | ||
| 391 | } | ||
| 392 | |||
| 393 | static int linebuffer_puts(BIO *b, const char *str) | ||
| 394 | { | ||
| 395 | return(linebuffer_write(b,str,strlen(str))); | ||
| 396 | } | ||
| 397 | |||
diff --git a/src/lib/libcrypto/bio/bf_nbio.c b/src/lib/libcrypto/bio/bf_nbio.c new file mode 100644 index 0000000000..c72a23c2e1 --- /dev/null +++ b/src/lib/libcrypto/bio/bf_nbio.c | |||
| @@ -0,0 +1,255 @@ | |||
| 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 <openssl/rand.h> | ||
| 63 | #include <openssl/bio.h> | ||
| 64 | |||
| 65 | /* BIO_put and BIO_get both add to the digest, | ||
| 66 | * BIO_gets returns the digest */ | ||
| 67 | |||
| 68 | static int nbiof_write(BIO *h,const char *buf,int num); | ||
| 69 | static int nbiof_read(BIO *h,char *buf,int size); | ||
| 70 | static int nbiof_puts(BIO *h,const char *str); | ||
| 71 | static int nbiof_gets(BIO *h,char *str,int size); | ||
| 72 | static long nbiof_ctrl(BIO *h,int cmd,long arg1,void *arg2); | ||
| 73 | static int nbiof_new(BIO *h); | ||
| 74 | static int nbiof_free(BIO *data); | ||
| 75 | static long nbiof_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); | ||
| 76 | typedef struct nbio_test_st | ||
| 77 | { | ||
| 78 | /* only set if we sent a 'should retry' error */ | ||
| 79 | int lrn; | ||
| 80 | int lwn; | ||
| 81 | } NBIO_TEST; | ||
| 82 | |||
| 83 | static BIO_METHOD methods_nbiof= | ||
| 84 | { | ||
| 85 | BIO_TYPE_NBIO_TEST, | ||
| 86 | "non-blocking IO test filter", | ||
| 87 | nbiof_write, | ||
| 88 | nbiof_read, | ||
| 89 | nbiof_puts, | ||
| 90 | nbiof_gets, | ||
| 91 | nbiof_ctrl, | ||
| 92 | nbiof_new, | ||
| 93 | nbiof_free, | ||
| 94 | nbiof_callback_ctrl, | ||
| 95 | }; | ||
| 96 | |||
| 97 | BIO_METHOD *BIO_f_nbio_test(void) | ||
| 98 | { | ||
| 99 | return(&methods_nbiof); | ||
| 100 | } | ||
| 101 | |||
| 102 | static int nbiof_new(BIO *bi) | ||
| 103 | { | ||
| 104 | NBIO_TEST *nt; | ||
| 105 | |||
| 106 | if (!(nt=(NBIO_TEST *)OPENSSL_malloc(sizeof(NBIO_TEST)))) return(0); | ||
| 107 | nt->lrn= -1; | ||
| 108 | nt->lwn= -1; | ||
| 109 | bi->ptr=(char *)nt; | ||
| 110 | bi->init=1; | ||
| 111 | bi->flags=0; | ||
| 112 | return(1); | ||
| 113 | } | ||
| 114 | |||
| 115 | static int nbiof_free(BIO *a) | ||
| 116 | { | ||
| 117 | if (a == NULL) return(0); | ||
| 118 | if (a->ptr != NULL) | ||
| 119 | OPENSSL_free(a->ptr); | ||
| 120 | a->ptr=NULL; | ||
| 121 | a->init=0; | ||
| 122 | a->flags=0; | ||
| 123 | return(1); | ||
| 124 | } | ||
| 125 | |||
| 126 | static int nbiof_read(BIO *b, char *out, int outl) | ||
| 127 | { | ||
| 128 | NBIO_TEST *nt; | ||
| 129 | int ret=0; | ||
| 130 | #if 1 | ||
| 131 | int num; | ||
| 132 | unsigned char n; | ||
| 133 | #endif | ||
| 134 | |||
| 135 | if (out == NULL) return(0); | ||
| 136 | if (b->next_bio == NULL) return(0); | ||
| 137 | nt=(NBIO_TEST *)b->ptr; | ||
| 138 | |||
| 139 | BIO_clear_retry_flags(b); | ||
| 140 | #if 1 | ||
| 141 | RAND_pseudo_bytes(&n,1); | ||
| 142 | num=(n&0x07); | ||
| 143 | |||
| 144 | if (outl > num) outl=num; | ||
| 145 | |||
| 146 | if (num == 0) | ||
| 147 | { | ||
| 148 | ret= -1; | ||
| 149 | BIO_set_retry_read(b); | ||
| 150 | } | ||
| 151 | else | ||
| 152 | #endif | ||
| 153 | { | ||
| 154 | ret=BIO_read(b->next_bio,out,outl); | ||
| 155 | if (ret < 0) | ||
| 156 | BIO_copy_next_retry(b); | ||
| 157 | } | ||
| 158 | return(ret); | ||
| 159 | } | ||
| 160 | |||
| 161 | static int nbiof_write(BIO *b, const char *in, int inl) | ||
| 162 | { | ||
| 163 | NBIO_TEST *nt; | ||
| 164 | int ret=0; | ||
| 165 | int num; | ||
| 166 | unsigned char n; | ||
| 167 | |||
| 168 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 169 | if (b->next_bio == NULL) return(0); | ||
| 170 | nt=(NBIO_TEST *)b->ptr; | ||
| 171 | |||
| 172 | BIO_clear_retry_flags(b); | ||
| 173 | |||
| 174 | #if 1 | ||
| 175 | if (nt->lwn > 0) | ||
| 176 | { | ||
| 177 | num=nt->lwn; | ||
| 178 | nt->lwn=0; | ||
| 179 | } | ||
| 180 | else | ||
| 181 | { | ||
| 182 | RAND_pseudo_bytes(&n,1); | ||
| 183 | num=(n&7); | ||
| 184 | } | ||
| 185 | |||
| 186 | if (inl > num) inl=num; | ||
| 187 | |||
| 188 | if (num == 0) | ||
| 189 | { | ||
| 190 | ret= -1; | ||
| 191 | BIO_set_retry_write(b); | ||
| 192 | } | ||
| 193 | else | ||
| 194 | #endif | ||
| 195 | { | ||
| 196 | ret=BIO_write(b->next_bio,in,inl); | ||
| 197 | if (ret < 0) | ||
| 198 | { | ||
| 199 | BIO_copy_next_retry(b); | ||
| 200 | nt->lwn=inl; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | return(ret); | ||
| 204 | } | ||
| 205 | |||
| 206 | static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 207 | { | ||
| 208 | long ret; | ||
| 209 | |||
| 210 | if (b->next_bio == NULL) return(0); | ||
| 211 | switch (cmd) | ||
| 212 | { | ||
| 213 | case BIO_C_DO_STATE_MACHINE: | ||
| 214 | BIO_clear_retry_flags(b); | ||
| 215 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 216 | BIO_copy_next_retry(b); | ||
| 217 | break; | ||
| 218 | case BIO_CTRL_DUP: | ||
| 219 | ret=0L; | ||
| 220 | break; | ||
| 221 | default: | ||
| 222 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 223 | break; | ||
| 224 | } | ||
| 225 | return(ret); | ||
| 226 | } | ||
| 227 | |||
| 228 | static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 229 | { | ||
| 230 | long ret=1; | ||
| 231 | |||
| 232 | if (b->next_bio == NULL) return(0); | ||
| 233 | switch (cmd) | ||
| 234 | { | ||
| 235 | default: | ||
| 236 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
| 237 | break; | ||
| 238 | } | ||
| 239 | return(ret); | ||
| 240 | } | ||
| 241 | |||
| 242 | static int nbiof_gets(BIO *bp, char *buf, int size) | ||
| 243 | { | ||
| 244 | if (bp->next_bio == NULL) return(0); | ||
| 245 | return(BIO_gets(bp->next_bio,buf,size)); | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | static int nbiof_puts(BIO *bp, const char *str) | ||
| 250 | { | ||
| 251 | if (bp->next_bio == NULL) return(0); | ||
| 252 | return(BIO_puts(bp->next_bio,str)); | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
diff --git a/src/lib/libcrypto/bio/bf_null.c b/src/lib/libcrypto/bio/bf_null.c new file mode 100644 index 0000000000..c1bf39a904 --- /dev/null +++ b/src/lib/libcrypto/bio/bf_null.c | |||
| @@ -0,0 +1,183 @@ | |||
| 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 <openssl/bio.h> | ||
| 63 | |||
| 64 | /* BIO_put and BIO_get both add to the digest, | ||
| 65 | * BIO_gets returns the digest */ | ||
| 66 | |||
| 67 | static int nullf_write(BIO *h, const char *buf, int num); | ||
| 68 | static int nullf_read(BIO *h, char *buf, int size); | ||
| 69 | static int nullf_puts(BIO *h, const char *str); | ||
| 70 | static int nullf_gets(BIO *h, char *str, int size); | ||
| 71 | static long nullf_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 72 | static int nullf_new(BIO *h); | ||
| 73 | static int nullf_free(BIO *data); | ||
| 74 | static long nullf_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); | ||
| 75 | static BIO_METHOD methods_nullf= | ||
| 76 | { | ||
| 77 | BIO_TYPE_NULL_FILTER, | ||
| 78 | "NULL filter", | ||
| 79 | nullf_write, | ||
| 80 | nullf_read, | ||
| 81 | nullf_puts, | ||
| 82 | nullf_gets, | ||
| 83 | nullf_ctrl, | ||
| 84 | nullf_new, | ||
| 85 | nullf_free, | ||
| 86 | nullf_callback_ctrl, | ||
| 87 | }; | ||
| 88 | |||
| 89 | BIO_METHOD *BIO_f_null(void) | ||
| 90 | { | ||
| 91 | return(&methods_nullf); | ||
| 92 | } | ||
| 93 | |||
| 94 | static int nullf_new(BIO *bi) | ||
| 95 | { | ||
| 96 | bi->init=1; | ||
| 97 | bi->ptr=NULL; | ||
| 98 | bi->flags=0; | ||
| 99 | return(1); | ||
| 100 | } | ||
| 101 | |||
| 102 | static int nullf_free(BIO *a) | ||
| 103 | { | ||
| 104 | if (a == NULL) return(0); | ||
| 105 | /* a->ptr=NULL; | ||
| 106 | a->init=0; | ||
| 107 | a->flags=0;*/ | ||
| 108 | return(1); | ||
| 109 | } | ||
| 110 | |||
| 111 | static int nullf_read(BIO *b, char *out, int outl) | ||
| 112 | { | ||
| 113 | int ret=0; | ||
| 114 | |||
| 115 | if (out == NULL) return(0); | ||
| 116 | if (b->next_bio == NULL) return(0); | ||
| 117 | ret=BIO_read(b->next_bio,out,outl); | ||
| 118 | BIO_clear_retry_flags(b); | ||
| 119 | BIO_copy_next_retry(b); | ||
| 120 | return(ret); | ||
| 121 | } | ||
| 122 | |||
| 123 | static int nullf_write(BIO *b, const char *in, int inl) | ||
| 124 | { | ||
| 125 | int ret=0; | ||
| 126 | |||
| 127 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 128 | if (b->next_bio == NULL) return(0); | ||
| 129 | ret=BIO_write(b->next_bio,in,inl); | ||
| 130 | BIO_clear_retry_flags(b); | ||
| 131 | BIO_copy_next_retry(b); | ||
| 132 | return(ret); | ||
| 133 | } | ||
| 134 | |||
| 135 | static long nullf_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 136 | { | ||
| 137 | long ret; | ||
| 138 | |||
| 139 | if (b->next_bio == NULL) return(0); | ||
| 140 | switch(cmd) | ||
| 141 | { | ||
| 142 | case BIO_C_DO_STATE_MACHINE: | ||
| 143 | BIO_clear_retry_flags(b); | ||
| 144 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 145 | BIO_copy_next_retry(b); | ||
| 146 | break; | ||
| 147 | case BIO_CTRL_DUP: | ||
| 148 | ret=0L; | ||
| 149 | break; | ||
| 150 | default: | ||
| 151 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 152 | } | ||
| 153 | return(ret); | ||
| 154 | } | ||
| 155 | |||
| 156 | static long nullf_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 157 | { | ||
| 158 | long ret=1; | ||
| 159 | |||
| 160 | if (b->next_bio == NULL) return(0); | ||
| 161 | switch (cmd) | ||
| 162 | { | ||
| 163 | default: | ||
| 164 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
| 165 | break; | ||
| 166 | } | ||
| 167 | return(ret); | ||
| 168 | } | ||
| 169 | |||
| 170 | static int nullf_gets(BIO *bp, char *buf, int size) | ||
| 171 | { | ||
| 172 | if (bp->next_bio == NULL) return(0); | ||
| 173 | return(BIO_gets(bp->next_bio,buf,size)); | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | static int nullf_puts(BIO *bp, const char *str) | ||
| 178 | { | ||
| 179 | if (bp->next_bio == NULL) return(0); | ||
| 180 | return(BIO_puts(bp->next_bio,str)); | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
diff --git a/src/lib/libcrypto/bio/bio.h b/src/lib/libcrypto/bio/bio.h new file mode 100644 index 0000000000..cecb6a7207 --- /dev/null +++ b/src/lib/libcrypto/bio/bio.h | |||
| @@ -0,0 +1,776 @@ | |||
| 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 | #include <openssl/e_os2.h> | ||
| 63 | |||
| 64 | #ifndef OPENSSL_NO_FP_API | ||
| 65 | # include <stdio.h> | ||
| 66 | #endif | ||
| 67 | #include <stdarg.h> | ||
| 68 | |||
| 69 | #include <openssl/crypto.h> | ||
| 70 | |||
| 71 | #ifdef __cplusplus | ||
| 72 | extern "C" { | ||
| 73 | #endif | ||
| 74 | |||
| 75 | /* These are the 'types' of BIOs */ | ||
| 76 | #define BIO_TYPE_NONE 0 | ||
| 77 | #define BIO_TYPE_MEM (1|0x0400) | ||
| 78 | #define BIO_TYPE_FILE (2|0x0400) | ||
| 79 | |||
| 80 | #define BIO_TYPE_FD (4|0x0400|0x0100) | ||
| 81 | #define BIO_TYPE_SOCKET (5|0x0400|0x0100) | ||
| 82 | #define BIO_TYPE_NULL (6|0x0400) | ||
| 83 | #define BIO_TYPE_SSL (7|0x0200) | ||
| 84 | #define BIO_TYPE_MD (8|0x0200) /* passive filter */ | ||
| 85 | #define BIO_TYPE_BUFFER (9|0x0200) /* filter */ | ||
| 86 | #define BIO_TYPE_CIPHER (10|0x0200) /* filter */ | ||
| 87 | #define BIO_TYPE_BASE64 (11|0x0200) /* filter */ | ||
| 88 | #define BIO_TYPE_CONNECT (12|0x0400|0x0100) /* socket - connect */ | ||
| 89 | #define BIO_TYPE_ACCEPT (13|0x0400|0x0100) /* socket for accept */ | ||
| 90 | #define BIO_TYPE_PROXY_CLIENT (14|0x0200) /* client proxy BIO */ | ||
| 91 | #define BIO_TYPE_PROXY_SERVER (15|0x0200) /* server proxy BIO */ | ||
| 92 | #define BIO_TYPE_NBIO_TEST (16|0x0200) /* server proxy BIO */ | ||
| 93 | #define BIO_TYPE_NULL_FILTER (17|0x0200) | ||
| 94 | #define BIO_TYPE_BER (18|0x0200) /* BER -> bin filter */ | ||
| 95 | #define BIO_TYPE_BIO (19|0x0400) /* (half a) BIO pair */ | ||
| 96 | #define BIO_TYPE_LINEBUFFER (20|0x0200) /* filter */ | ||
| 97 | #define BIO_TYPE_DGRAM (21|0x0400|0x0100) | ||
| 98 | #define BIO_TYPE_COMP (23|0x0200) /* filter */ | ||
| 99 | |||
| 100 | #define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */ | ||
| 101 | #define BIO_TYPE_FILTER 0x0200 | ||
| 102 | #define BIO_TYPE_SOURCE_SINK 0x0400 | ||
| 103 | |||
| 104 | /* BIO_FILENAME_READ|BIO_CLOSE to open or close on free. | ||
| 105 | * BIO_set_fp(in,stdin,BIO_NOCLOSE); */ | ||
| 106 | #define BIO_NOCLOSE 0x00 | ||
| 107 | #define BIO_CLOSE 0x01 | ||
| 108 | |||
| 109 | /* These are used in the following macros and are passed to | ||
| 110 | * BIO_ctrl() */ | ||
| 111 | #define BIO_CTRL_RESET 1 /* opt - rewind/zero etc */ | ||
| 112 | #define BIO_CTRL_EOF 2 /* opt - are we at the eof */ | ||
| 113 | #define BIO_CTRL_INFO 3 /* opt - extra tit-bits */ | ||
| 114 | #define BIO_CTRL_SET 4 /* man - set the 'IO' type */ | ||
| 115 | #define BIO_CTRL_GET 5 /* man - get the 'IO' type */ | ||
| 116 | #define BIO_CTRL_PUSH 6 /* opt - internal, used to signify change */ | ||
| 117 | #define BIO_CTRL_POP 7 /* opt - internal, used to signify change */ | ||
| 118 | #define BIO_CTRL_GET_CLOSE 8 /* man - set the 'close' on free */ | ||
| 119 | #define BIO_CTRL_SET_CLOSE 9 /* man - set the 'close' on free */ | ||
| 120 | #define BIO_CTRL_PENDING 10 /* opt - is their more data buffered */ | ||
| 121 | #define BIO_CTRL_FLUSH 11 /* opt - 'flush' buffered output */ | ||
| 122 | #define BIO_CTRL_DUP 12 /* man - extra stuff for 'duped' BIO */ | ||
| 123 | #define BIO_CTRL_WPENDING 13 /* opt - number of bytes still to write */ | ||
| 124 | /* callback is int cb(BIO *bio,state,ret); */ | ||
| 125 | #define BIO_CTRL_SET_CALLBACK 14 /* opt - set callback function */ | ||
| 126 | #define BIO_CTRL_GET_CALLBACK 15 /* opt - set callback function */ | ||
| 127 | |||
| 128 | #define BIO_CTRL_SET_FILENAME 30 /* BIO_s_file special */ | ||
| 129 | |||
| 130 | /* dgram BIO stuff */ | ||
| 131 | #define BIO_CTRL_DGRAM_CONNECT 31 /* BIO dgram special */ | ||
| 132 | #define BIO_CTRL_DGRAM_SET_CONNECTED 32 /* allow for an externally | ||
| 133 | * connected socket to be | ||
| 134 | * passed in */ | ||
| 135 | #define BIO_CTRL_DGRAM_SET_RECV_TIMEOUT 33 /* setsockopt, essentially */ | ||
| 136 | #define BIO_CTRL_DGRAM_GET_RECV_TIMEOUT 34 /* getsockopt, essentially */ | ||
| 137 | #define BIO_CTRL_DGRAM_SET_SEND_TIMEOUT 35 /* setsockopt, essentially */ | ||
| 138 | #define BIO_CTRL_DGRAM_GET_SEND_TIMEOUT 36 /* getsockopt, essentially */ | ||
| 139 | |||
| 140 | #define BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP 37 /* flag whether the last */ | ||
| 141 | #define BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP 38 /* I/O operation tiemd out */ | ||
| 142 | |||
| 143 | /* #ifdef IP_MTU_DISCOVER */ | ||
| 144 | #define BIO_CTRL_DGRAM_MTU_DISCOVER 39 /* set DF bit on egress packets */ | ||
| 145 | /* #endif */ | ||
| 146 | |||
| 147 | #define BIO_CTRL_DGRAM_QUERY_MTU 40 /* as kernel for current MTU */ | ||
| 148 | #define BIO_CTRL_DGRAM_GET_MTU 41 /* get cached value for MTU */ | ||
| 149 | #define BIO_CTRL_DGRAM_SET_MTU 42 /* set cached value for | ||
| 150 | * MTU. want to use this | ||
| 151 | * if asking the kernel | ||
| 152 | * fails */ | ||
| 153 | |||
| 154 | #define BIO_CTRL_DGRAM_MTU_EXCEEDED 43 /* check whether the MTU | ||
| 155 | * was exceed in the | ||
| 156 | * previous write | ||
| 157 | * operation */ | ||
| 158 | |||
| 159 | #define BIO_CTRL_DGRAM_SET_PEER 44 /* Destination for the data */ | ||
| 160 | |||
| 161 | |||
| 162 | /* modifiers */ | ||
| 163 | #define BIO_FP_READ 0x02 | ||
| 164 | #define BIO_FP_WRITE 0x04 | ||
| 165 | #define BIO_FP_APPEND 0x08 | ||
| 166 | #define BIO_FP_TEXT 0x10 | ||
| 167 | |||
| 168 | #define BIO_FLAGS_READ 0x01 | ||
| 169 | #define BIO_FLAGS_WRITE 0x02 | ||
| 170 | #define BIO_FLAGS_IO_SPECIAL 0x04 | ||
| 171 | #define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL) | ||
| 172 | #define BIO_FLAGS_SHOULD_RETRY 0x08 | ||
| 173 | #ifndef BIO_FLAGS_UPLINK | ||
| 174 | /* "UPLINK" flag denotes file descriptors provided by application. | ||
| 175 | It defaults to 0, as most platforms don't require UPLINK interface. */ | ||
| 176 | #define BIO_FLAGS_UPLINK 0 | ||
| 177 | #endif | ||
| 178 | |||
| 179 | /* Used in BIO_gethostbyname() */ | ||
| 180 | #define BIO_GHBN_CTRL_HITS 1 | ||
| 181 | #define BIO_GHBN_CTRL_MISSES 2 | ||
| 182 | #define BIO_GHBN_CTRL_CACHE_SIZE 3 | ||
| 183 | #define BIO_GHBN_CTRL_GET_ENTRY 4 | ||
| 184 | #define BIO_GHBN_CTRL_FLUSH 5 | ||
| 185 | |||
| 186 | /* Mostly used in the SSL BIO */ | ||
| 187 | /* Not used anymore | ||
| 188 | * #define BIO_FLAGS_PROTOCOL_DELAYED_READ 0x10 | ||
| 189 | * #define BIO_FLAGS_PROTOCOL_DELAYED_WRITE 0x20 | ||
| 190 | * #define BIO_FLAGS_PROTOCOL_STARTUP 0x40 | ||
| 191 | */ | ||
| 192 | |||
| 193 | #define BIO_FLAGS_BASE64_NO_NL 0x100 | ||
| 194 | |||
| 195 | /* This is used with memory BIOs: it means we shouldn't free up or change the | ||
| 196 | * data in any way. | ||
| 197 | */ | ||
| 198 | #define BIO_FLAGS_MEM_RDONLY 0x200 | ||
| 199 | |||
| 200 | typedef struct bio_st BIO; | ||
| 201 | |||
| 202 | void BIO_set_flags(BIO *b, int flags); | ||
| 203 | int BIO_test_flags(const BIO *b, int flags); | ||
| 204 | void BIO_clear_flags(BIO *b, int flags); | ||
| 205 | |||
| 206 | #define BIO_get_flags(b) BIO_test_flags(b, ~(0x0)) | ||
| 207 | #define BIO_set_retry_special(b) \ | ||
| 208 | BIO_set_flags(b, (BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY)) | ||
| 209 | #define BIO_set_retry_read(b) \ | ||
| 210 | BIO_set_flags(b, (BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY)) | ||
| 211 | #define BIO_set_retry_write(b) \ | ||
| 212 | BIO_set_flags(b, (BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY)) | ||
| 213 | |||
| 214 | /* These are normally used internally in BIOs */ | ||
| 215 | #define BIO_clear_retry_flags(b) \ | ||
| 216 | BIO_clear_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) | ||
| 217 | #define BIO_get_retry_flags(b) \ | ||
| 218 | BIO_test_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) | ||
| 219 | |||
| 220 | /* These should be used by the application to tell why we should retry */ | ||
| 221 | #define BIO_should_read(a) BIO_test_flags(a, BIO_FLAGS_READ) | ||
| 222 | #define BIO_should_write(a) BIO_test_flags(a, BIO_FLAGS_WRITE) | ||
| 223 | #define BIO_should_io_special(a) BIO_test_flags(a, BIO_FLAGS_IO_SPECIAL) | ||
| 224 | #define BIO_retry_type(a) BIO_test_flags(a, BIO_FLAGS_RWS) | ||
| 225 | #define BIO_should_retry(a) BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY) | ||
| 226 | |||
| 227 | /* The next three are used in conjunction with the | ||
| 228 | * BIO_should_io_special() condition. After this returns true, | ||
| 229 | * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO | ||
| 230 | * stack and return the 'reason' for the special and the offending BIO. | ||
| 231 | * Given a BIO, BIO_get_retry_reason(bio) will return the code. */ | ||
| 232 | /* Returned from the SSL bio when the certificate retrieval code had an error */ | ||
| 233 | #define BIO_RR_SSL_X509_LOOKUP 0x01 | ||
| 234 | /* Returned from the connect BIO when a connect would have blocked */ | ||
| 235 | #define BIO_RR_CONNECT 0x02 | ||
| 236 | /* Returned from the accept BIO when an accept would have blocked */ | ||
| 237 | #define BIO_RR_ACCEPT 0x03 | ||
| 238 | |||
| 239 | /* These are passed by the BIO callback */ | ||
| 240 | #define BIO_CB_FREE 0x01 | ||
| 241 | #define BIO_CB_READ 0x02 | ||
| 242 | #define BIO_CB_WRITE 0x03 | ||
| 243 | #define BIO_CB_PUTS 0x04 | ||
| 244 | #define BIO_CB_GETS 0x05 | ||
| 245 | #define BIO_CB_CTRL 0x06 | ||
| 246 | |||
| 247 | /* The callback is called before and after the underling operation, | ||
| 248 | * The BIO_CB_RETURN flag indicates if it is after the call */ | ||
| 249 | #define BIO_CB_RETURN 0x80 | ||
| 250 | #define BIO_CB_return(a) ((a)|BIO_CB_RETURN)) | ||
| 251 | #define BIO_cb_pre(a) (!((a)&BIO_CB_RETURN)) | ||
| 252 | #define BIO_cb_post(a) ((a)&BIO_CB_RETURN) | ||
| 253 | |||
| 254 | long (*BIO_get_callback(const BIO *b)) (struct bio_st *,int,const char *,int, long,long); | ||
| 255 | void BIO_set_callback(BIO *b, | ||
| 256 | long (*callback)(struct bio_st *,int,const char *,int, long,long)); | ||
| 257 | char *BIO_get_callback_arg(const BIO *b); | ||
| 258 | void BIO_set_callback_arg(BIO *b, char *arg); | ||
| 259 | |||
| 260 | const char * BIO_method_name(const BIO *b); | ||
| 261 | int BIO_method_type(const BIO *b); | ||
| 262 | |||
| 263 | typedef void bio_info_cb(struct bio_st *, int, const char *, int, long, long); | ||
| 264 | |||
| 265 | #ifndef OPENSSL_SYS_WIN16 | ||
| 266 | typedef struct bio_method_st | ||
| 267 | { | ||
| 268 | int type; | ||
| 269 | const char *name; | ||
| 270 | int (*bwrite)(BIO *, const char *, int); | ||
| 271 | int (*bread)(BIO *, char *, int); | ||
| 272 | int (*bputs)(BIO *, const char *); | ||
| 273 | int (*bgets)(BIO *, char *, int); | ||
| 274 | long (*ctrl)(BIO *, int, long, void *); | ||
| 275 | int (*create)(BIO *); | ||
| 276 | int (*destroy)(BIO *); | ||
| 277 | long (*callback_ctrl)(BIO *, int, bio_info_cb *); | ||
| 278 | } BIO_METHOD; | ||
| 279 | #else | ||
| 280 | typedef struct bio_method_st | ||
| 281 | { | ||
| 282 | int type; | ||
| 283 | const char *name; | ||
| 284 | int (_far *bwrite)(); | ||
| 285 | int (_far *bread)(); | ||
| 286 | int (_far *bputs)(); | ||
| 287 | int (_far *bgets)(); | ||
| 288 | long (_far *ctrl)(); | ||
| 289 | int (_far *create)(); | ||
| 290 | int (_far *destroy)(); | ||
| 291 | long (_far *callback_ctrl)(); | ||
| 292 | } BIO_METHOD; | ||
| 293 | #endif | ||
| 294 | |||
| 295 | struct bio_st | ||
| 296 | { | ||
| 297 | BIO_METHOD *method; | ||
| 298 | /* bio, mode, argp, argi, argl, ret */ | ||
| 299 | long (*callback)(struct bio_st *,int,const char *,int, long,long); | ||
| 300 | char *cb_arg; /* first argument for the callback */ | ||
| 301 | |||
| 302 | int init; | ||
| 303 | int shutdown; | ||
| 304 | int flags; /* extra storage */ | ||
| 305 | int retry_reason; | ||
| 306 | int num; | ||
| 307 | void *ptr; | ||
| 308 | struct bio_st *next_bio; /* used by filter BIOs */ | ||
| 309 | struct bio_st *prev_bio; /* used by filter BIOs */ | ||
| 310 | int references; | ||
| 311 | unsigned long num_read; | ||
| 312 | unsigned long num_write; | ||
| 313 | |||
| 314 | CRYPTO_EX_DATA ex_data; | ||
| 315 | }; | ||
| 316 | |||
| 317 | DECLARE_STACK_OF(BIO) | ||
| 318 | |||
| 319 | typedef struct bio_f_buffer_ctx_struct | ||
| 320 | { | ||
| 321 | /* BIO *bio; */ /* this is now in the BIO struct */ | ||
| 322 | int ibuf_size; /* how big is the input buffer */ | ||
| 323 | int obuf_size; /* how big is the output buffer */ | ||
| 324 | |||
| 325 | char *ibuf; /* the char array */ | ||
| 326 | int ibuf_len; /* how many bytes are in it */ | ||
| 327 | int ibuf_off; /* write/read offset */ | ||
| 328 | |||
| 329 | char *obuf; /* the char array */ | ||
| 330 | int obuf_len; /* how many bytes are in it */ | ||
| 331 | int obuf_off; /* write/read offset */ | ||
| 332 | } BIO_F_BUFFER_CTX; | ||
| 333 | |||
| 334 | /* connect BIO stuff */ | ||
| 335 | #define BIO_CONN_S_BEFORE 1 | ||
| 336 | #define BIO_CONN_S_GET_IP 2 | ||
| 337 | #define BIO_CONN_S_GET_PORT 3 | ||
| 338 | #define BIO_CONN_S_CREATE_SOCKET 4 | ||
| 339 | #define BIO_CONN_S_CONNECT 5 | ||
| 340 | #define BIO_CONN_S_OK 6 | ||
| 341 | #define BIO_CONN_S_BLOCKED_CONNECT 7 | ||
| 342 | #define BIO_CONN_S_NBIO 8 | ||
| 343 | /*#define BIO_CONN_get_param_hostname BIO_ctrl */ | ||
| 344 | |||
| 345 | #define BIO_C_SET_CONNECT 100 | ||
| 346 | #define BIO_C_DO_STATE_MACHINE 101 | ||
| 347 | #define BIO_C_SET_NBIO 102 | ||
| 348 | #define BIO_C_SET_PROXY_PARAM 103 | ||
| 349 | #define BIO_C_SET_FD 104 | ||
| 350 | #define BIO_C_GET_FD 105 | ||
| 351 | #define BIO_C_SET_FILE_PTR 106 | ||
| 352 | #define BIO_C_GET_FILE_PTR 107 | ||
| 353 | #define BIO_C_SET_FILENAME 108 | ||
| 354 | #define BIO_C_SET_SSL 109 | ||
| 355 | #define BIO_C_GET_SSL 110 | ||
| 356 | #define BIO_C_SET_MD 111 | ||
| 357 | #define BIO_C_GET_MD 112 | ||
| 358 | #define BIO_C_GET_CIPHER_STATUS 113 | ||
| 359 | #define BIO_C_SET_BUF_MEM 114 | ||
| 360 | #define BIO_C_GET_BUF_MEM_PTR 115 | ||
| 361 | #define BIO_C_GET_BUFF_NUM_LINES 116 | ||
| 362 | #define BIO_C_SET_BUFF_SIZE 117 | ||
| 363 | #define BIO_C_SET_ACCEPT 118 | ||
| 364 | #define BIO_C_SSL_MODE 119 | ||
| 365 | #define BIO_C_GET_MD_CTX 120 | ||
| 366 | #define BIO_C_GET_PROXY_PARAM 121 | ||
| 367 | #define BIO_C_SET_BUFF_READ_DATA 122 /* data to read first */ | ||
| 368 | #define BIO_C_GET_CONNECT 123 | ||
| 369 | #define BIO_C_GET_ACCEPT 124 | ||
| 370 | #define BIO_C_SET_SSL_RENEGOTIATE_BYTES 125 | ||
| 371 | #define BIO_C_GET_SSL_NUM_RENEGOTIATES 126 | ||
| 372 | #define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT 127 | ||
| 373 | #define BIO_C_FILE_SEEK 128 | ||
| 374 | #define BIO_C_GET_CIPHER_CTX 129 | ||
| 375 | #define BIO_C_SET_BUF_MEM_EOF_RETURN 130/*return end of input value*/ | ||
| 376 | #define BIO_C_SET_BIND_MODE 131 | ||
| 377 | #define BIO_C_GET_BIND_MODE 132 | ||
| 378 | #define BIO_C_FILE_TELL 133 | ||
| 379 | #define BIO_C_GET_SOCKS 134 | ||
| 380 | #define BIO_C_SET_SOCKS 135 | ||
| 381 | |||
| 382 | #define BIO_C_SET_WRITE_BUF_SIZE 136/* for BIO_s_bio */ | ||
| 383 | #define BIO_C_GET_WRITE_BUF_SIZE 137 | ||
| 384 | #define BIO_C_MAKE_BIO_PAIR 138 | ||
| 385 | #define BIO_C_DESTROY_BIO_PAIR 139 | ||
| 386 | #define BIO_C_GET_WRITE_GUARANTEE 140 | ||
| 387 | #define BIO_C_GET_READ_REQUEST 141 | ||
| 388 | #define BIO_C_SHUTDOWN_WR 142 | ||
| 389 | #define BIO_C_NREAD0 143 | ||
| 390 | #define BIO_C_NREAD 144 | ||
| 391 | #define BIO_C_NWRITE0 145 | ||
| 392 | #define BIO_C_NWRITE 146 | ||
| 393 | #define BIO_C_RESET_READ_REQUEST 147 | ||
| 394 | #define BIO_C_SET_MD_CTX 148 | ||
| 395 | |||
| 396 | |||
| 397 | #define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,arg) | ||
| 398 | #define BIO_get_app_data(s) BIO_get_ex_data(s,0) | ||
| 399 | |||
| 400 | /* BIO_s_connect() and BIO_s_socks4a_connect() */ | ||
| 401 | #define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name) | ||
| 402 | #define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port) | ||
| 403 | #define BIO_set_conn_ip(b,ip) BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip) | ||
| 404 | #define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port) | ||
| 405 | #define BIO_get_conn_hostname(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0) | ||
| 406 | #define BIO_get_conn_port(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1) | ||
| 407 | #define BIO_get_conn_ip(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,2) | ||
| 408 | #define BIO_get_conn_int_port(b) BIO_int_ctrl(b,BIO_C_GET_CONNECT,3) | ||
| 409 | |||
| 410 | |||
| 411 | #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) | ||
| 412 | |||
| 413 | /* BIO_s_accept_socket() */ | ||
| 414 | #define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name) | ||
| 415 | #define BIO_get_accept_port(b) BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0) | ||
| 416 | /* #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */ | ||
| 417 | #define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?"a":NULL) | ||
| 418 | #define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio) | ||
| 419 | |||
| 420 | #define BIO_BIND_NORMAL 0 | ||
| 421 | #define BIO_BIND_REUSEADDR_IF_UNUSED 1 | ||
| 422 | #define BIO_BIND_REUSEADDR 2 | ||
| 423 | #define BIO_set_bind_mode(b,mode) BIO_ctrl(b,BIO_C_SET_BIND_MODE,mode,NULL) | ||
| 424 | #define BIO_get_bind_mode(b,mode) BIO_ctrl(b,BIO_C_GET_BIND_MODE,0,NULL) | ||
| 425 | |||
| 426 | #define BIO_do_connect(b) BIO_do_handshake(b) | ||
| 427 | #define BIO_do_accept(b) BIO_do_handshake(b) | ||
| 428 | #define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL) | ||
| 429 | |||
| 430 | /* BIO_s_proxy_client() */ | ||
| 431 | #define BIO_set_url(b,url) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,0,(char *)(url)) | ||
| 432 | #define BIO_set_proxies(b,p) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,1,(char *)(p)) | ||
| 433 | /* BIO_set_nbio(b,n) */ | ||
| 434 | #define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s)) | ||
| 435 | /* BIO *BIO_get_filter_bio(BIO *bio); */ | ||
| 436 | #define BIO_set_proxy_cb(b,cb) BIO_callback_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(void *(*cb)())) | ||
| 437 | #define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk) | ||
| 438 | #define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool) | ||
| 439 | |||
| 440 | #define BIO_get_proxy_header(b,skp) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,0,(char *)skp) | ||
| 441 | #define BIO_get_proxies(b,pxy_p) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,1,(char *)(pxy_p)) | ||
| 442 | #define BIO_get_url(b,url) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,2,(char *)(url)) | ||
| 443 | #define BIO_get_no_connect_return(b) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,5,NULL) | ||
| 444 | |||
| 445 | #define BIO_set_fd(b,fd,c) BIO_int_ctrl(b,BIO_C_SET_FD,c,fd) | ||
| 446 | #define BIO_get_fd(b,c) BIO_ctrl(b,BIO_C_GET_FD,0,(char *)c) | ||
| 447 | |||
| 448 | #define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp) | ||
| 449 | #define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp) | ||
| 450 | |||
| 451 | #define BIO_seek(b,ofs) (int)BIO_ctrl(b,BIO_C_FILE_SEEK,ofs,NULL) | ||
| 452 | #define BIO_tell(b) (int)BIO_ctrl(b,BIO_C_FILE_TELL,0,NULL) | ||
| 453 | |||
| 454 | /* name is cast to lose const, but might be better to route through a function | ||
| 455 | so we can do it safely */ | ||
| 456 | #ifdef CONST_STRICT | ||
| 457 | /* If you are wondering why this isn't defined, its because CONST_STRICT is | ||
| 458 | * purely a compile-time kludge to allow const to be checked. | ||
| 459 | */ | ||
| 460 | int BIO_read_filename(BIO *b,const char *name); | ||
| 461 | #else | ||
| 462 | #define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ | ||
| 463 | BIO_CLOSE|BIO_FP_READ,(char *)name) | ||
| 464 | #endif | ||
| 465 | #define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ | ||
| 466 | BIO_CLOSE|BIO_FP_WRITE,name) | ||
| 467 | #define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ | ||
| 468 | BIO_CLOSE|BIO_FP_APPEND,name) | ||
| 469 | #define BIO_rw_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ | ||
| 470 | BIO_CLOSE|BIO_FP_READ|BIO_FP_WRITE,name) | ||
| 471 | |||
| 472 | /* WARNING WARNING, this ups the reference count on the read bio of the | ||
| 473 | * SSL structure. This is because the ssl read BIO is now pointed to by | ||
| 474 | * the next_bio field in the bio. So when you free the BIO, make sure | ||
| 475 | * you are doing a BIO_free_all() to catch the underlying BIO. */ | ||
| 476 | #define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl) | ||
| 477 | #define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp) | ||
| 478 | #define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL) | ||
| 479 | #define BIO_set_ssl_renegotiate_bytes(b,num) \ | ||
| 480 | BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL); | ||
| 481 | #define BIO_get_num_renegotiates(b) \ | ||
| 482 | BIO_ctrl(b,BIO_C_GET_SSL_NUM_RENEGOTIATES,0,NULL); | ||
| 483 | #define BIO_set_ssl_renegotiate_timeout(b,seconds) \ | ||
| 484 | BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL); | ||
| 485 | |||
| 486 | /* defined in evp.h */ | ||
| 487 | /* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */ | ||
| 488 | |||
| 489 | #define BIO_get_mem_data(b,pp) BIO_ctrl(b,BIO_CTRL_INFO,0,(char *)pp) | ||
| 490 | #define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm) | ||
| 491 | #define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp) | ||
| 492 | #define BIO_set_mem_eof_return(b,v) \ | ||
| 493 | BIO_ctrl(b,BIO_C_SET_BUF_MEM_EOF_RETURN,v,NULL) | ||
| 494 | |||
| 495 | /* For the BIO_f_buffer() type */ | ||
| 496 | #define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL) | ||
| 497 | #define BIO_set_buffer_size(b,size) BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL) | ||
| 498 | #define BIO_set_read_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,0) | ||
| 499 | #define BIO_set_write_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,1) | ||
| 500 | #define BIO_set_buffer_read_data(b,buf,num) BIO_ctrl(b,BIO_C_SET_BUFF_READ_DATA,num,buf) | ||
| 501 | |||
| 502 | /* Don't use the next one unless you know what you are doing :-) */ | ||
| 503 | #define BIO_dup_state(b,ret) BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)(ret)) | ||
| 504 | |||
| 505 | #define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL) | ||
| 506 | #define BIO_eof(b) (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL) | ||
| 507 | #define BIO_set_close(b,c) (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL) | ||
| 508 | #define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL) | ||
| 509 | #define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL) | ||
| 510 | #define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL) | ||
| 511 | /* ...pending macros have inappropriate return type */ | ||
| 512 | size_t BIO_ctrl_pending(BIO *b); | ||
| 513 | size_t BIO_ctrl_wpending(BIO *b); | ||
| 514 | #define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL) | ||
| 515 | #define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0, \ | ||
| 516 | cbp) | ||
| 517 | #define BIO_set_info_callback(b,cb) (int)BIO_callback_ctrl(b,BIO_CTRL_SET_CALLBACK,cb) | ||
| 518 | |||
| 519 | /* For the BIO_f_buffer() type */ | ||
| 520 | #define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL) | ||
| 521 | |||
| 522 | /* For BIO_s_bio() */ | ||
| 523 | #define BIO_set_write_buf_size(b,size) (int)BIO_ctrl(b,BIO_C_SET_WRITE_BUF_SIZE,size,NULL) | ||
| 524 | #define BIO_get_write_buf_size(b,size) (size_t)BIO_ctrl(b,BIO_C_GET_WRITE_BUF_SIZE,size,NULL) | ||
| 525 | #define BIO_make_bio_pair(b1,b2) (int)BIO_ctrl(b1,BIO_C_MAKE_BIO_PAIR,0,b2) | ||
| 526 | #define BIO_destroy_bio_pair(b) (int)BIO_ctrl(b,BIO_C_DESTROY_BIO_PAIR,0,NULL) | ||
| 527 | #define BIO_shutdown_wr(b) (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL) | ||
| 528 | /* macros with inappropriate type -- but ...pending macros use int too: */ | ||
| 529 | #define BIO_get_write_guarantee(b) (int)BIO_ctrl(b,BIO_C_GET_WRITE_GUARANTEE,0,NULL) | ||
| 530 | #define BIO_get_read_request(b) (int)BIO_ctrl(b,BIO_C_GET_READ_REQUEST,0,NULL) | ||
| 531 | size_t BIO_ctrl_get_write_guarantee(BIO *b); | ||
| 532 | size_t BIO_ctrl_get_read_request(BIO *b); | ||
| 533 | int BIO_ctrl_reset_read_request(BIO *b); | ||
| 534 | |||
| 535 | /* ctrl macros for dgram */ | ||
| 536 | #define BIO_ctrl_dgram_connect(b,peer) \ | ||
| 537 | (int)BIO_ctrl(b,BIO_CTRL_DGRAM_CONNECT,0, (char *)peer) | ||
| 538 | #define BIO_ctrl_set_connected(b, state, peer) \ | ||
| 539 | (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_CONNECTED, state, (char *)peer) | ||
| 540 | #define BIO_dgram_recv_timedout(b) \ | ||
| 541 | (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP, 0, NULL) | ||
| 542 | #define BIO_dgram_send_timedout(b) \ | ||
| 543 | (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP, 0, NULL) | ||
| 544 | #define BIO_dgram_set_peer(b,peer) \ | ||
| 545 | (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, (char *)peer) | ||
| 546 | |||
| 547 | /* These two aren't currently implemented */ | ||
| 548 | /* int BIO_get_ex_num(BIO *bio); */ | ||
| 549 | /* void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)()); */ | ||
| 550 | int BIO_set_ex_data(BIO *bio,int idx,void *data); | ||
| 551 | void *BIO_get_ex_data(BIO *bio,int idx); | ||
| 552 | int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 553 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | ||
| 554 | unsigned long BIO_number_read(BIO *bio); | ||
| 555 | unsigned long BIO_number_written(BIO *bio); | ||
| 556 | |||
| 557 | # ifndef OPENSSL_NO_FP_API | ||
| 558 | # if defined(OPENSSL_SYS_WIN16) && defined(_WINDLL) | ||
| 559 | BIO_METHOD *BIO_s_file_internal(void); | ||
| 560 | BIO *BIO_new_file_internal(char *filename, char *mode); | ||
| 561 | BIO *BIO_new_fp_internal(FILE *stream, int close_flag); | ||
| 562 | # define BIO_s_file BIO_s_file_internal | ||
| 563 | # define BIO_new_file BIO_new_file_internal | ||
| 564 | # define BIO_new_fp BIO_new_fp_internal | ||
| 565 | # else /* FP_API */ | ||
| 566 | BIO_METHOD *BIO_s_file(void ); | ||
| 567 | BIO *BIO_new_file(const char *filename, const char *mode); | ||
| 568 | BIO *BIO_new_fp(FILE *stream, int close_flag); | ||
| 569 | # define BIO_s_file_internal BIO_s_file | ||
| 570 | # define BIO_new_file_internal BIO_new_file | ||
| 571 | # define BIO_new_fp_internal BIO_s_file | ||
| 572 | # endif /* FP_API */ | ||
| 573 | # endif | ||
| 574 | BIO * BIO_new(BIO_METHOD *type); | ||
| 575 | int BIO_set(BIO *a,BIO_METHOD *type); | ||
| 576 | int BIO_free(BIO *a); | ||
| 577 | void BIO_vfree(BIO *a); | ||
| 578 | int BIO_read(BIO *b, void *data, int len); | ||
| 579 | int BIO_gets(BIO *bp,char *buf, int size); | ||
| 580 | int BIO_write(BIO *b, const void *data, int len); | ||
| 581 | int BIO_puts(BIO *bp,const char *buf); | ||
| 582 | int BIO_indent(BIO *b,int indent,int max); | ||
| 583 | long BIO_ctrl(BIO *bp,int cmd,long larg,void *parg); | ||
| 584 | long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long)); | ||
| 585 | char * BIO_ptr_ctrl(BIO *bp,int cmd,long larg); | ||
| 586 | long BIO_int_ctrl(BIO *bp,int cmd,long larg,int iarg); | ||
| 587 | BIO * BIO_push(BIO *b,BIO *append); | ||
| 588 | BIO * BIO_pop(BIO *b); | ||
| 589 | void BIO_free_all(BIO *a); | ||
| 590 | BIO * BIO_find_type(BIO *b,int bio_type); | ||
| 591 | BIO * BIO_next(BIO *b); | ||
| 592 | BIO * BIO_get_retry_BIO(BIO *bio, int *reason); | ||
| 593 | int BIO_get_retry_reason(BIO *bio); | ||
| 594 | BIO * BIO_dup_chain(BIO *in); | ||
| 595 | |||
| 596 | int BIO_nread0(BIO *bio, char **buf); | ||
| 597 | int BIO_nread(BIO *bio, char **buf, int num); | ||
| 598 | int BIO_nwrite0(BIO *bio, char **buf); | ||
| 599 | int BIO_nwrite(BIO *bio, char **buf, int num); | ||
| 600 | |||
| 601 | #ifndef OPENSSL_SYS_WIN16 | ||
| 602 | long BIO_debug_callback(BIO *bio,int cmd,const char *argp,int argi, | ||
| 603 | long argl,long ret); | ||
| 604 | #else | ||
| 605 | long _far _loadds BIO_debug_callback(BIO *bio,int cmd,const char *argp,int argi, | ||
| 606 | long argl,long ret); | ||
| 607 | #endif | ||
| 608 | |||
| 609 | BIO_METHOD *BIO_s_mem(void); | ||
| 610 | BIO *BIO_new_mem_buf(void *buf, int len); | ||
| 611 | BIO_METHOD *BIO_s_socket(void); | ||
| 612 | BIO_METHOD *BIO_s_connect(void); | ||
| 613 | BIO_METHOD *BIO_s_accept(void); | ||
| 614 | BIO_METHOD *BIO_s_fd(void); | ||
| 615 | #ifndef OPENSSL_SYS_OS2 | ||
| 616 | BIO_METHOD *BIO_s_log(void); | ||
| 617 | #endif | ||
| 618 | BIO_METHOD *BIO_s_bio(void); | ||
| 619 | BIO_METHOD *BIO_s_null(void); | ||
| 620 | BIO_METHOD *BIO_f_null(void); | ||
| 621 | BIO_METHOD *BIO_f_buffer(void); | ||
| 622 | #ifdef OPENSSL_SYS_VMS | ||
| 623 | BIO_METHOD *BIO_f_linebuffer(void); | ||
| 624 | #endif | ||
| 625 | BIO_METHOD *BIO_f_nbio_test(void); | ||
| 626 | #ifndef OPENSSL_NO_DGRAM | ||
| 627 | BIO_METHOD *BIO_s_datagram(void); | ||
| 628 | #endif | ||
| 629 | |||
| 630 | /* BIO_METHOD *BIO_f_ber(void); */ | ||
| 631 | |||
| 632 | int BIO_sock_should_retry(int i); | ||
| 633 | int BIO_sock_non_fatal_error(int error); | ||
| 634 | int BIO_dgram_non_fatal_error(int error); | ||
| 635 | |||
| 636 | int BIO_fd_should_retry(int i); | ||
| 637 | int BIO_fd_non_fatal_error(int error); | ||
| 638 | int BIO_dump_cb(int (*cb)(const void *data, size_t len, void *u), | ||
| 639 | void *u, const char *s, int len); | ||
| 640 | int BIO_dump_indent_cb(int (*cb)(const void *data, size_t len, void *u), | ||
| 641 | void *u, const char *s, int len, int indent); | ||
| 642 | int BIO_dump(BIO *b,const char *bytes,int len); | ||
| 643 | int BIO_dump_indent(BIO *b,const char *bytes,int len,int indent); | ||
| 644 | #ifndef OPENSSL_NO_FP_API | ||
| 645 | int BIO_dump_fp(FILE *fp, const char *s, int len); | ||
| 646 | int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent); | ||
| 647 | #endif | ||
| 648 | struct hostent *BIO_gethostbyname(const char *name); | ||
| 649 | /* We might want a thread-safe interface too: | ||
| 650 | * struct hostent *BIO_gethostbyname_r(const char *name, | ||
| 651 | * struct hostent *result, void *buffer, size_t buflen); | ||
| 652 | * or something similar (caller allocates a struct hostent, | ||
| 653 | * pointed to by "result", and additional buffer space for the various | ||
| 654 | * substructures; if the buffer does not suffice, NULL is returned | ||
| 655 | * and an appropriate error code is set). | ||
| 656 | */ | ||
| 657 | int BIO_sock_error(int sock); | ||
| 658 | int BIO_socket_ioctl(int fd, long type, void *arg); | ||
| 659 | int BIO_socket_nbio(int fd,int mode); | ||
| 660 | int BIO_get_port(const char *str, unsigned short *port_ptr); | ||
| 661 | int BIO_get_host_ip(const char *str, unsigned char *ip); | ||
| 662 | int BIO_get_accept_socket(char *host_port,int mode); | ||
| 663 | int BIO_accept(int sock,char **ip_port); | ||
| 664 | int BIO_sock_init(void ); | ||
| 665 | void BIO_sock_cleanup(void); | ||
| 666 | int BIO_set_tcp_ndelay(int sock,int turn_on); | ||
| 667 | |||
| 668 | BIO *BIO_new_socket(int sock, int close_flag); | ||
| 669 | BIO *BIO_new_dgram(int fd, int close_flag); | ||
| 670 | BIO *BIO_new_fd(int fd, int close_flag); | ||
| 671 | BIO *BIO_new_connect(char *host_port); | ||
| 672 | BIO *BIO_new_accept(char *host_port); | ||
| 673 | |||
| 674 | int BIO_new_bio_pair(BIO **bio1, size_t writebuf1, | ||
| 675 | BIO **bio2, size_t writebuf2); | ||
| 676 | /* If successful, returns 1 and in *bio1, *bio2 two BIO pair endpoints. | ||
| 677 | * Otherwise returns 0 and sets *bio1 and *bio2 to NULL. | ||
| 678 | * Size 0 uses default value. | ||
| 679 | */ | ||
| 680 | |||
| 681 | void BIO_copy_next_retry(BIO *b); | ||
| 682 | |||
| 683 | /*long BIO_ghbn_ctrl(int cmd,int iarg,char *parg);*/ | ||
| 684 | |||
| 685 | #ifdef __GNUC__ | ||
| 686 | # define __bio_h__attr__ __attribute__ | ||
| 687 | #else | ||
| 688 | # define __bio_h__attr__(x) | ||
| 689 | #endif | ||
| 690 | int BIO_printf(BIO *bio, const char *format, ...) | ||
| 691 | __bio_h__attr__((__format__(__printf__,2,3))); | ||
| 692 | int BIO_vprintf(BIO *bio, const char *format, va_list args) | ||
| 693 | __bio_h__attr__((__format__(__printf__,2,0))); | ||
| 694 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) | ||
| 695 | __bio_h__attr__((__format__(__printf__,3,4))); | ||
| 696 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) | ||
| 697 | __bio_h__attr__((__format__(__printf__,3,0))); | ||
| 698 | #undef __bio_h__attr__ | ||
| 699 | |||
| 700 | /* BEGIN ERROR CODES */ | ||
| 701 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 702 | * made after this point may be overwritten when the script is next run. | ||
| 703 | */ | ||
| 704 | void ERR_load_BIO_strings(void); | ||
| 705 | |||
| 706 | /* Error codes for the BIO functions. */ | ||
| 707 | |||
| 708 | /* Function codes. */ | ||
| 709 | #define BIO_F_ACPT_STATE 100 | ||
| 710 | #define BIO_F_BIO_ACCEPT 101 | ||
| 711 | #define BIO_F_BIO_BER_GET_HEADER 102 | ||
| 712 | #define BIO_F_BIO_CALLBACK_CTRL 131 | ||
| 713 | #define BIO_F_BIO_CTRL 103 | ||
| 714 | #define BIO_F_BIO_GETHOSTBYNAME 120 | ||
| 715 | #define BIO_F_BIO_GETS 104 | ||
| 716 | #define BIO_F_BIO_GET_ACCEPT_SOCKET 105 | ||
| 717 | #define BIO_F_BIO_GET_HOST_IP 106 | ||
| 718 | #define BIO_F_BIO_GET_PORT 107 | ||
| 719 | #define BIO_F_BIO_MAKE_PAIR 121 | ||
| 720 | #define BIO_F_BIO_NEW 108 | ||
| 721 | #define BIO_F_BIO_NEW_FILE 109 | ||
| 722 | #define BIO_F_BIO_NEW_MEM_BUF 126 | ||
| 723 | #define BIO_F_BIO_NREAD 123 | ||
| 724 | #define BIO_F_BIO_NREAD0 124 | ||
| 725 | #define BIO_F_BIO_NWRITE 125 | ||
| 726 | #define BIO_F_BIO_NWRITE0 122 | ||
| 727 | #define BIO_F_BIO_PUTS 110 | ||
| 728 | #define BIO_F_BIO_READ 111 | ||
| 729 | #define BIO_F_BIO_SOCK_INIT 112 | ||
| 730 | #define BIO_F_BIO_WRITE 113 | ||
| 731 | #define BIO_F_BUFFER_CTRL 114 | ||
| 732 | #define BIO_F_CONN_CTRL 127 | ||
| 733 | #define BIO_F_CONN_STATE 115 | ||
| 734 | #define BIO_F_FILE_CTRL 116 | ||
| 735 | #define BIO_F_FILE_READ 130 | ||
| 736 | #define BIO_F_LINEBUFFER_CTRL 129 | ||
| 737 | #define BIO_F_MEM_READ 128 | ||
| 738 | #define BIO_F_MEM_WRITE 117 | ||
| 739 | #define BIO_F_SSL_NEW 118 | ||
| 740 | #define BIO_F_WSASTARTUP 119 | ||
| 741 | |||
| 742 | /* Reason codes. */ | ||
| 743 | #define BIO_R_ACCEPT_ERROR 100 | ||
| 744 | #define BIO_R_BAD_FOPEN_MODE 101 | ||
| 745 | #define BIO_R_BAD_HOSTNAME_LOOKUP 102 | ||
| 746 | #define BIO_R_BROKEN_PIPE 124 | ||
| 747 | #define BIO_R_CONNECT_ERROR 103 | ||
| 748 | #define BIO_R_EOF_ON_MEMORY_BIO 127 | ||
| 749 | #define BIO_R_ERROR_SETTING_NBIO 104 | ||
| 750 | #define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105 | ||
| 751 | #define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106 | ||
| 752 | #define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107 | ||
| 753 | #define BIO_R_INVALID_ARGUMENT 125 | ||
| 754 | #define BIO_R_INVALID_IP_ADDRESS 108 | ||
| 755 | #define BIO_R_IN_USE 123 | ||
| 756 | #define BIO_R_KEEPALIVE 109 | ||
| 757 | #define BIO_R_NBIO_CONNECT_ERROR 110 | ||
| 758 | #define BIO_R_NO_ACCEPT_PORT_SPECIFIED 111 | ||
| 759 | #define BIO_R_NO_HOSTNAME_SPECIFIED 112 | ||
| 760 | #define BIO_R_NO_PORT_DEFINED 113 | ||
| 761 | #define BIO_R_NO_PORT_SPECIFIED 114 | ||
| 762 | #define BIO_R_NO_SUCH_FILE 128 | ||
| 763 | #define BIO_R_NULL_PARAMETER 115 | ||
| 764 | #define BIO_R_TAG_MISMATCH 116 | ||
| 765 | #define BIO_R_UNABLE_TO_BIND_SOCKET 117 | ||
| 766 | #define BIO_R_UNABLE_TO_CREATE_SOCKET 118 | ||
| 767 | #define BIO_R_UNABLE_TO_LISTEN_SOCKET 119 | ||
| 768 | #define BIO_R_UNINITIALIZED 120 | ||
| 769 | #define BIO_R_UNSUPPORTED_METHOD 121 | ||
| 770 | #define BIO_R_WRITE_TO_READ_ONLY_BIO 126 | ||
| 771 | #define BIO_R_WSASTARTUP 122 | ||
| 772 | |||
| 773 | #ifdef __cplusplus | ||
| 774 | } | ||
| 775 | #endif | ||
| 776 | #endif | ||
diff --git a/src/lib/libcrypto/bio/bio_cb.c b/src/lib/libcrypto/bio/bio_cb.c new file mode 100644 index 0000000000..6f4254a114 --- /dev/null +++ b/src/lib/libcrypto/bio/bio_cb.c | |||
| @@ -0,0 +1,139 @@ | |||
| 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 <openssl/bio.h> | ||
| 64 | #include <openssl/err.h> | ||
| 65 | |||
| 66 | long MS_CALLBACK BIO_debug_callback(BIO *bio, int cmd, const char *argp, | ||
| 67 | int argi, long argl, long ret) | ||
| 68 | { | ||
| 69 | BIO *b; | ||
| 70 | MS_STATIC char buf[256]; | ||
| 71 | char *p; | ||
| 72 | long r=1; | ||
| 73 | size_t p_maxlen; | ||
| 74 | |||
| 75 | if (BIO_CB_RETURN & cmd) | ||
| 76 | r=ret; | ||
| 77 | |||
| 78 | BIO_snprintf(buf,sizeof buf,"BIO[%08lX]:",(unsigned long)bio); | ||
| 79 | p= &(buf[14]); | ||
| 80 | p_maxlen = sizeof buf - 14; | ||
| 81 | switch (cmd) | ||
| 82 | { | ||
| 83 | case BIO_CB_FREE: | ||
| 84 | BIO_snprintf(p,p_maxlen,"Free - %s\n",bio->method->name); | ||
| 85 | break; | ||
| 86 | case BIO_CB_READ: | ||
| 87 | if (bio->method->type & BIO_TYPE_DESCRIPTOR) | ||
| 88 | BIO_snprintf(p,p_maxlen,"read(%d,%d) - %s fd=%d\n", | ||
| 89 | bio->num,argi,bio->method->name,bio->num); | ||
| 90 | else | ||
| 91 | BIO_snprintf(p,p_maxlen,"read(%d,%d) - %s\n", | ||
| 92 | bio->num,argi,bio->method->name); | ||
| 93 | break; | ||
| 94 | case BIO_CB_WRITE: | ||
| 95 | if (bio->method->type & BIO_TYPE_DESCRIPTOR) | ||
| 96 | BIO_snprintf(p,p_maxlen,"write(%d,%d) - %s fd=%d\n", | ||
| 97 | bio->num,argi,bio->method->name,bio->num); | ||
| 98 | else | ||
| 99 | BIO_snprintf(p,p_maxlen,"write(%d,%d) - %s\n", | ||
| 100 | bio->num,argi,bio->method->name); | ||
| 101 | break; | ||
| 102 | case BIO_CB_PUTS: | ||
| 103 | BIO_snprintf(p,p_maxlen,"puts() - %s\n",bio->method->name); | ||
| 104 | break; | ||
| 105 | case BIO_CB_GETS: | ||
| 106 | BIO_snprintf(p,p_maxlen,"gets(%d) - %s\n",argi,bio->method->name); | ||
| 107 | break; | ||
| 108 | case BIO_CB_CTRL: | ||
| 109 | BIO_snprintf(p,p_maxlen,"ctrl(%d) - %s\n",argi,bio->method->name); | ||
| 110 | break; | ||
| 111 | case BIO_CB_RETURN|BIO_CB_READ: | ||
| 112 | BIO_snprintf(p,p_maxlen,"read return %ld\n",ret); | ||
| 113 | break; | ||
| 114 | case BIO_CB_RETURN|BIO_CB_WRITE: | ||
| 115 | BIO_snprintf(p,p_maxlen,"write return %ld\n",ret); | ||
| 116 | break; | ||
| 117 | case BIO_CB_RETURN|BIO_CB_GETS: | ||
| 118 | BIO_snprintf(p,p_maxlen,"gets return %ld\n",ret); | ||
| 119 | break; | ||
| 120 | case BIO_CB_RETURN|BIO_CB_PUTS: | ||
| 121 | BIO_snprintf(p,p_maxlen,"puts return %ld\n",ret); | ||
| 122 | break; | ||
| 123 | case BIO_CB_RETURN|BIO_CB_CTRL: | ||
| 124 | BIO_snprintf(p,p_maxlen,"ctrl return %ld\n",ret); | ||
| 125 | break; | ||
| 126 | default: | ||
| 127 | BIO_snprintf(p,p_maxlen,"bio callback - unknown type (%d)\n",cmd); | ||
| 128 | break; | ||
| 129 | } | ||
| 130 | |||
| 131 | b=(BIO *)bio->cb_arg; | ||
| 132 | if (b != NULL) | ||
| 133 | BIO_write(b,buf,strlen(buf)); | ||
| 134 | #if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16) | ||
| 135 | else | ||
| 136 | fputs(buf,stderr); | ||
| 137 | #endif | ||
| 138 | return(r); | ||
| 139 | } | ||
diff --git a/src/lib/libcrypto/bio/bio_err.c b/src/lib/libcrypto/bio/bio_err.c new file mode 100644 index 0000000000..6603f1c74d --- /dev/null +++ b/src/lib/libcrypto/bio/bio_err.c | |||
| @@ -0,0 +1,154 @@ | |||
| 1 | /* crypto/bio/bio_err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@OpenSSL.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* NOTE: this file was auto generated by the mkerr.pl script: any changes | ||
| 57 | * made to it will be overwritten when the script next updates this file, | ||
| 58 | * only reason strings will be preserved. | ||
| 59 | */ | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <openssl/err.h> | ||
| 63 | #include <openssl/bio.h> | ||
| 64 | |||
| 65 | /* BEGIN ERROR CODES */ | ||
| 66 | #ifndef OPENSSL_NO_ERR | ||
| 67 | |||
| 68 | #define ERR_FUNC(func) ERR_PACK(ERR_LIB_BIO,func,0) | ||
| 69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_BIO,0,reason) | ||
| 70 | |||
| 71 | static ERR_STRING_DATA BIO_str_functs[]= | ||
| 72 | { | ||
| 73 | {ERR_FUNC(BIO_F_ACPT_STATE), "ACPT_STATE"}, | ||
| 74 | {ERR_FUNC(BIO_F_BIO_ACCEPT), "BIO_accept"}, | ||
| 75 | {ERR_FUNC(BIO_F_BIO_BER_GET_HEADER), "BIO_BER_GET_HEADER"}, | ||
| 76 | {ERR_FUNC(BIO_F_BIO_CALLBACK_CTRL), "BIO_callback_ctrl"}, | ||
| 77 | {ERR_FUNC(BIO_F_BIO_CTRL), "BIO_ctrl"}, | ||
| 78 | {ERR_FUNC(BIO_F_BIO_GETHOSTBYNAME), "BIO_gethostbyname"}, | ||
| 79 | {ERR_FUNC(BIO_F_BIO_GETS), "BIO_gets"}, | ||
| 80 | {ERR_FUNC(BIO_F_BIO_GET_ACCEPT_SOCKET), "BIO_get_accept_socket"}, | ||
| 81 | {ERR_FUNC(BIO_F_BIO_GET_HOST_IP), "BIO_get_host_ip"}, | ||
| 82 | {ERR_FUNC(BIO_F_BIO_GET_PORT), "BIO_get_port"}, | ||
| 83 | {ERR_FUNC(BIO_F_BIO_MAKE_PAIR), "BIO_MAKE_PAIR"}, | ||
| 84 | {ERR_FUNC(BIO_F_BIO_NEW), "BIO_new"}, | ||
| 85 | {ERR_FUNC(BIO_F_BIO_NEW_FILE), "BIO_new_file"}, | ||
| 86 | {ERR_FUNC(BIO_F_BIO_NEW_MEM_BUF), "BIO_new_mem_buf"}, | ||
| 87 | {ERR_FUNC(BIO_F_BIO_NREAD), "BIO_nread"}, | ||
| 88 | {ERR_FUNC(BIO_F_BIO_NREAD0), "BIO_nread0"}, | ||
| 89 | {ERR_FUNC(BIO_F_BIO_NWRITE), "BIO_nwrite"}, | ||
| 90 | {ERR_FUNC(BIO_F_BIO_NWRITE0), "BIO_nwrite0"}, | ||
| 91 | {ERR_FUNC(BIO_F_BIO_PUTS), "BIO_puts"}, | ||
| 92 | {ERR_FUNC(BIO_F_BIO_READ), "BIO_read"}, | ||
| 93 | {ERR_FUNC(BIO_F_BIO_SOCK_INIT), "BIO_sock_init"}, | ||
| 94 | {ERR_FUNC(BIO_F_BIO_WRITE), "BIO_write"}, | ||
| 95 | {ERR_FUNC(BIO_F_BUFFER_CTRL), "BUFFER_CTRL"}, | ||
| 96 | {ERR_FUNC(BIO_F_CONN_CTRL), "CONN_CTRL"}, | ||
| 97 | {ERR_FUNC(BIO_F_CONN_STATE), "CONN_STATE"}, | ||
| 98 | {ERR_FUNC(BIO_F_FILE_CTRL), "FILE_CTRL"}, | ||
| 99 | {ERR_FUNC(BIO_F_FILE_READ), "FILE_READ"}, | ||
| 100 | {ERR_FUNC(BIO_F_LINEBUFFER_CTRL), "LINEBUFFER_CTRL"}, | ||
| 101 | {ERR_FUNC(BIO_F_MEM_READ), "MEM_READ"}, | ||
| 102 | {ERR_FUNC(BIO_F_MEM_WRITE), "MEM_WRITE"}, | ||
| 103 | {ERR_FUNC(BIO_F_SSL_NEW), "SSL_new"}, | ||
| 104 | {ERR_FUNC(BIO_F_WSASTARTUP), "WSASTARTUP"}, | ||
| 105 | {0,NULL} | ||
| 106 | }; | ||
| 107 | |||
| 108 | static ERR_STRING_DATA BIO_str_reasons[]= | ||
| 109 | { | ||
| 110 | {ERR_REASON(BIO_R_ACCEPT_ERROR) ,"accept error"}, | ||
| 111 | {ERR_REASON(BIO_R_BAD_FOPEN_MODE) ,"bad fopen mode"}, | ||
| 112 | {ERR_REASON(BIO_R_BAD_HOSTNAME_LOOKUP) ,"bad hostname lookup"}, | ||
| 113 | {ERR_REASON(BIO_R_BROKEN_PIPE) ,"broken pipe"}, | ||
| 114 | {ERR_REASON(BIO_R_CONNECT_ERROR) ,"connect error"}, | ||
| 115 | {ERR_REASON(BIO_R_EOF_ON_MEMORY_BIO) ,"EOF on memory BIO"}, | ||
| 116 | {ERR_REASON(BIO_R_ERROR_SETTING_NBIO) ,"error setting nbio"}, | ||
| 117 | {ERR_REASON(BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET),"error setting nbio on accepted socket"}, | ||
| 118 | {ERR_REASON(BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET),"error setting nbio on accept socket"}, | ||
| 119 | {ERR_REASON(BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET),"gethostbyname addr is not af inet"}, | ||
| 120 | {ERR_REASON(BIO_R_INVALID_ARGUMENT) ,"invalid argument"}, | ||
| 121 | {ERR_REASON(BIO_R_INVALID_IP_ADDRESS) ,"invalid ip address"}, | ||
| 122 | {ERR_REASON(BIO_R_IN_USE) ,"in use"}, | ||
| 123 | {ERR_REASON(BIO_R_KEEPALIVE) ,"keepalive"}, | ||
| 124 | {ERR_REASON(BIO_R_NBIO_CONNECT_ERROR) ,"nbio connect error"}, | ||
| 125 | {ERR_REASON(BIO_R_NO_ACCEPT_PORT_SPECIFIED),"no accept port specified"}, | ||
| 126 | {ERR_REASON(BIO_R_NO_HOSTNAME_SPECIFIED) ,"no hostname specified"}, | ||
| 127 | {ERR_REASON(BIO_R_NO_PORT_DEFINED) ,"no port defined"}, | ||
| 128 | {ERR_REASON(BIO_R_NO_PORT_SPECIFIED) ,"no port specified"}, | ||
| 129 | {ERR_REASON(BIO_R_NO_SUCH_FILE) ,"no such file"}, | ||
| 130 | {ERR_REASON(BIO_R_NULL_PARAMETER) ,"null parameter"}, | ||
| 131 | {ERR_REASON(BIO_R_TAG_MISMATCH) ,"tag mismatch"}, | ||
| 132 | {ERR_REASON(BIO_R_UNABLE_TO_BIND_SOCKET) ,"unable to bind socket"}, | ||
| 133 | {ERR_REASON(BIO_R_UNABLE_TO_CREATE_SOCKET),"unable to create socket"}, | ||
| 134 | {ERR_REASON(BIO_R_UNABLE_TO_LISTEN_SOCKET),"unable to listen socket"}, | ||
| 135 | {ERR_REASON(BIO_R_UNINITIALIZED) ,"uninitialized"}, | ||
| 136 | {ERR_REASON(BIO_R_UNSUPPORTED_METHOD) ,"unsupported method"}, | ||
| 137 | {ERR_REASON(BIO_R_WRITE_TO_READ_ONLY_BIO),"write to read only BIO"}, | ||
| 138 | {ERR_REASON(BIO_R_WSASTARTUP) ,"WSAStartup"}, | ||
| 139 | {0,NULL} | ||
| 140 | }; | ||
| 141 | |||
| 142 | #endif | ||
| 143 | |||
| 144 | void ERR_load_BIO_strings(void) | ||
| 145 | { | ||
| 146 | #ifndef OPENSSL_NO_ERR | ||
| 147 | |||
| 148 | if (ERR_func_error_string(BIO_str_functs[0].error) == NULL) | ||
| 149 | { | ||
| 150 | ERR_load_strings(0,BIO_str_functs); | ||
| 151 | ERR_load_strings(0,BIO_str_reasons); | ||
| 152 | } | ||
| 153 | #endif | ||
| 154 | } | ||
diff --git a/src/lib/libcrypto/bio/bio_lib.c b/src/lib/libcrypto/bio/bio_lib.c new file mode 100644 index 0000000000..3f52ae953c --- /dev/null +++ b/src/lib/libcrypto/bio/bio_lib.c | |||
| @@ -0,0 +1,602 @@ | |||
| 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 <openssl/crypto.h> | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include <openssl/bio.h> | ||
| 64 | #include <openssl/stack.h> | ||
| 65 | |||
| 66 | BIO *BIO_new(BIO_METHOD *method) | ||
| 67 | { | ||
| 68 | BIO *ret=NULL; | ||
| 69 | |||
| 70 | ret=(BIO *)OPENSSL_malloc(sizeof(BIO)); | ||
| 71 | if (ret == NULL) | ||
| 72 | { | ||
| 73 | BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE); | ||
| 74 | return(NULL); | ||
| 75 | } | ||
| 76 | if (!BIO_set(ret,method)) | ||
| 77 | { | ||
| 78 | OPENSSL_free(ret); | ||
| 79 | ret=NULL; | ||
| 80 | } | ||
| 81 | return(ret); | ||
| 82 | } | ||
| 83 | |||
| 84 | int BIO_set(BIO *bio, BIO_METHOD *method) | ||
| 85 | { | ||
| 86 | bio->method=method; | ||
| 87 | bio->callback=NULL; | ||
| 88 | bio->cb_arg=NULL; | ||
| 89 | bio->init=0; | ||
| 90 | bio->shutdown=1; | ||
| 91 | bio->flags=0; | ||
| 92 | bio->retry_reason=0; | ||
| 93 | bio->num=0; | ||
| 94 | bio->ptr=NULL; | ||
| 95 | bio->prev_bio=NULL; | ||
| 96 | bio->next_bio=NULL; | ||
| 97 | bio->references=1; | ||
| 98 | bio->num_read=0L; | ||
| 99 | bio->num_write=0L; | ||
| 100 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); | ||
| 101 | if (method->create != NULL) | ||
| 102 | if (!method->create(bio)) | ||
| 103 | { | ||
| 104 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, | ||
| 105 | &bio->ex_data); | ||
| 106 | return(0); | ||
| 107 | } | ||
| 108 | return(1); | ||
| 109 | } | ||
| 110 | |||
| 111 | int BIO_free(BIO *a) | ||
| 112 | { | ||
| 113 | int ret=0,i; | ||
| 114 | |||
| 115 | if (a == NULL) return(0); | ||
| 116 | |||
| 117 | i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO); | ||
| 118 | #ifdef REF_PRINT | ||
| 119 | REF_PRINT("BIO",a); | ||
| 120 | #endif | ||
| 121 | if (i > 0) return(1); | ||
| 122 | #ifdef REF_CHECK | ||
| 123 | if (i < 0) | ||
| 124 | { | ||
| 125 | fprintf(stderr,"BIO_free, bad reference count\n"); | ||
| 126 | abort(); | ||
| 127 | } | ||
| 128 | #endif | ||
| 129 | if ((a->callback != NULL) && | ||
| 130 | ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0)) | ||
| 131 | return(i); | ||
| 132 | |||
| 133 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); | ||
| 134 | |||
| 135 | if ((a->method == NULL) || (a->method->destroy == NULL)) return(1); | ||
| 136 | ret=a->method->destroy(a); | ||
| 137 | OPENSSL_free(a); | ||
| 138 | return(1); | ||
| 139 | } | ||
| 140 | |||
| 141 | void BIO_vfree(BIO *a) | ||
| 142 | { BIO_free(a); } | ||
| 143 | |||
| 144 | void BIO_clear_flags(BIO *b, int flags) | ||
| 145 | { | ||
| 146 | b->flags &= ~flags; | ||
| 147 | } | ||
| 148 | |||
| 149 | int BIO_test_flags(const BIO *b, int flags) | ||
| 150 | { | ||
| 151 | return (b->flags & flags); | ||
| 152 | } | ||
| 153 | |||
| 154 | void BIO_set_flags(BIO *b, int flags) | ||
| 155 | { | ||
| 156 | b->flags |= flags; | ||
| 157 | } | ||
| 158 | |||
| 159 | long (*BIO_get_callback(const BIO *b))(struct bio_st *,int,const char *,int, long,long) | ||
| 160 | { | ||
| 161 | return b->callback; | ||
| 162 | } | ||
| 163 | |||
| 164 | void BIO_set_callback(BIO *b, long (*cb)(struct bio_st *,int,const char *,int, long,long)) | ||
| 165 | { | ||
| 166 | b->callback = cb; | ||
| 167 | } | ||
| 168 | |||
| 169 | void BIO_set_callback_arg(BIO *b, char *arg) | ||
| 170 | { | ||
| 171 | b->cb_arg = arg; | ||
| 172 | } | ||
| 173 | |||
| 174 | char * BIO_get_callback_arg(const BIO *b) | ||
| 175 | { | ||
| 176 | return b->cb_arg; | ||
| 177 | } | ||
| 178 | |||
| 179 | const char * BIO_method_name(const BIO *b) | ||
| 180 | { | ||
| 181 | return b->method->name; | ||
| 182 | } | ||
| 183 | |||
| 184 | int BIO_method_type(const BIO *b) | ||
| 185 | { | ||
| 186 | return b->method->type; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | int BIO_read(BIO *b, void *out, int outl) | ||
| 191 | { | ||
| 192 | int i; | ||
| 193 | long (*cb)(BIO *,int,const char *,int,long,long); | ||
| 194 | |||
| 195 | if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) | ||
| 196 | { | ||
| 197 | BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD); | ||
| 198 | return(-2); | ||
| 199 | } | ||
| 200 | |||
| 201 | cb=b->callback; | ||
| 202 | if ((cb != NULL) && | ||
| 203 | ((i=(int)cb(b,BIO_CB_READ,out,outl,0L,1L)) <= 0)) | ||
| 204 | return(i); | ||
| 205 | |||
| 206 | if (!b->init) | ||
| 207 | { | ||
| 208 | BIOerr(BIO_F_BIO_READ,BIO_R_UNINITIALIZED); | ||
| 209 | return(-2); | ||
| 210 | } | ||
| 211 | |||
| 212 | i=b->method->bread(b,out,outl); | ||
| 213 | |||
| 214 | if (i > 0) b->num_read+=(unsigned long)i; | ||
| 215 | |||
| 216 | if (cb != NULL) | ||
| 217 | i=(int)cb(b,BIO_CB_READ|BIO_CB_RETURN,out,outl, | ||
| 218 | 0L,(long)i); | ||
| 219 | return(i); | ||
| 220 | } | ||
| 221 | |||
| 222 | int BIO_write(BIO *b, const void *in, int inl) | ||
| 223 | { | ||
| 224 | int i; | ||
| 225 | long (*cb)(BIO *,int,const char *,int,long,long); | ||
| 226 | |||
| 227 | if (b == NULL) | ||
| 228 | return(0); | ||
| 229 | |||
| 230 | cb=b->callback; | ||
| 231 | if ((b->method == NULL) || (b->method->bwrite == NULL)) | ||
| 232 | { | ||
| 233 | BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD); | ||
| 234 | return(-2); | ||
| 235 | } | ||
| 236 | |||
| 237 | if ((cb != NULL) && | ||
| 238 | ((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0)) | ||
| 239 | return(i); | ||
| 240 | |||
| 241 | if (!b->init) | ||
| 242 | { | ||
| 243 | BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED); | ||
| 244 | return(-2); | ||
| 245 | } | ||
| 246 | |||
| 247 | i=b->method->bwrite(b,in,inl); | ||
| 248 | |||
| 249 | if (i > 0) b->num_write+=(unsigned long)i; | ||
| 250 | |||
| 251 | if (cb != NULL) | ||
| 252 | i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl, | ||
| 253 | 0L,(long)i); | ||
| 254 | return(i); | ||
| 255 | } | ||
| 256 | |||
| 257 | int BIO_puts(BIO *b, const char *in) | ||
| 258 | { | ||
| 259 | int i; | ||
| 260 | long (*cb)(BIO *,int,const char *,int,long,long); | ||
| 261 | |||
| 262 | if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) | ||
| 263 | { | ||
| 264 | BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD); | ||
| 265 | return(-2); | ||
| 266 | } | ||
| 267 | |||
| 268 | cb=b->callback; | ||
| 269 | |||
| 270 | if ((cb != NULL) && | ||
| 271 | ((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0)) | ||
| 272 | return(i); | ||
| 273 | |||
| 274 | if (!b->init) | ||
| 275 | { | ||
| 276 | BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED); | ||
| 277 | return(-2); | ||
| 278 | } | ||
| 279 | |||
| 280 | i=b->method->bputs(b,in); | ||
| 281 | |||
| 282 | if (i > 0) b->num_write+=(unsigned long)i; | ||
| 283 | |||
| 284 | if (cb != NULL) | ||
| 285 | i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0, | ||
| 286 | 0L,(long)i); | ||
| 287 | return(i); | ||
| 288 | } | ||
| 289 | |||
| 290 | int BIO_gets(BIO *b, char *in, int inl) | ||
| 291 | { | ||
| 292 | int i; | ||
| 293 | long (*cb)(BIO *,int,const char *,int,long,long); | ||
| 294 | |||
| 295 | if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) | ||
| 296 | { | ||
| 297 | BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD); | ||
| 298 | return(-2); | ||
| 299 | } | ||
| 300 | |||
| 301 | cb=b->callback; | ||
| 302 | |||
| 303 | if ((cb != NULL) && | ||
| 304 | ((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0)) | ||
| 305 | return(i); | ||
| 306 | |||
| 307 | if (!b->init) | ||
| 308 | { | ||
| 309 | BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED); | ||
| 310 | return(-2); | ||
| 311 | } | ||
| 312 | |||
| 313 | i=b->method->bgets(b,in,inl); | ||
| 314 | |||
| 315 | if (cb != NULL) | ||
| 316 | i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl, | ||
| 317 | 0L,(long)i); | ||
| 318 | return(i); | ||
| 319 | } | ||
| 320 | |||
| 321 | int BIO_indent(BIO *b,int indent,int max) | ||
| 322 | { | ||
| 323 | if(indent < 0) | ||
| 324 | indent=0; | ||
| 325 | if(indent > max) | ||
| 326 | indent=max; | ||
| 327 | while(indent--) | ||
| 328 | if(BIO_puts(b," ") != 1) | ||
| 329 | return 0; | ||
| 330 | return 1; | ||
| 331 | } | ||
| 332 | |||
| 333 | long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) | ||
| 334 | { | ||
| 335 | int i; | ||
| 336 | |||
| 337 | i=iarg; | ||
| 338 | return(BIO_ctrl(b,cmd,larg,(char *)&i)); | ||
| 339 | } | ||
| 340 | |||
| 341 | char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) | ||
| 342 | { | ||
| 343 | char *p=NULL; | ||
| 344 | |||
| 345 | if (BIO_ctrl(b,cmd,larg,(char *)&p) <= 0) | ||
| 346 | return(NULL); | ||
| 347 | else | ||
| 348 | return(p); | ||
| 349 | } | ||
| 350 | |||
| 351 | long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) | ||
| 352 | { | ||
| 353 | long ret; | ||
| 354 | long (*cb)(BIO *,int,const char *,int,long,long); | ||
| 355 | |||
| 356 | if (b == NULL) return(0); | ||
| 357 | |||
| 358 | if ((b->method == NULL) || (b->method->ctrl == NULL)) | ||
| 359 | { | ||
| 360 | BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD); | ||
| 361 | return(-2); | ||
| 362 | } | ||
| 363 | |||
| 364 | cb=b->callback; | ||
| 365 | |||
| 366 | if ((cb != NULL) && | ||
| 367 | ((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0)) | ||
| 368 | return(ret); | ||
| 369 | |||
| 370 | ret=b->method->ctrl(b,cmd,larg,parg); | ||
| 371 | |||
| 372 | if (cb != NULL) | ||
| 373 | ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd, | ||
| 374 | larg,ret); | ||
| 375 | return(ret); | ||
| 376 | } | ||
| 377 | |||
| 378 | long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long)) | ||
| 379 | { | ||
| 380 | long ret; | ||
| 381 | long (*cb)(BIO *,int,const char *,int,long,long); | ||
| 382 | |||
| 383 | if (b == NULL) return(0); | ||
| 384 | |||
| 385 | if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) | ||
| 386 | { | ||
| 387 | BIOerr(BIO_F_BIO_CALLBACK_CTRL,BIO_R_UNSUPPORTED_METHOD); | ||
| 388 | return(-2); | ||
| 389 | } | ||
| 390 | |||
| 391 | cb=b->callback; | ||
| 392 | |||
| 393 | if ((cb != NULL) && | ||
| 394 | ((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0)) | ||
| 395 | return(ret); | ||
| 396 | |||
| 397 | ret=b->method->callback_ctrl(b,cmd,fp); | ||
| 398 | |||
| 399 | if (cb != NULL) | ||
| 400 | ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd, | ||
| 401 | 0,ret); | ||
| 402 | return(ret); | ||
| 403 | } | ||
| 404 | |||
| 405 | /* It is unfortunate to duplicate in functions what the BIO_(w)pending macros | ||
| 406 | * do; but those macros have inappropriate return type, and for interfacing | ||
| 407 | * from other programming languages, C macros aren't much of a help anyway. */ | ||
| 408 | size_t BIO_ctrl_pending(BIO *bio) | ||
| 409 | { | ||
| 410 | return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL); | ||
| 411 | } | ||
| 412 | |||
| 413 | size_t BIO_ctrl_wpending(BIO *bio) | ||
| 414 | { | ||
| 415 | return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL); | ||
| 416 | } | ||
| 417 | |||
| 418 | |||
| 419 | /* put the 'bio' on the end of b's list of operators */ | ||
| 420 | BIO *BIO_push(BIO *b, BIO *bio) | ||
| 421 | { | ||
| 422 | BIO *lb; | ||
| 423 | |||
| 424 | if (b == NULL) return(bio); | ||
| 425 | lb=b; | ||
| 426 | while (lb->next_bio != NULL) | ||
| 427 | lb=lb->next_bio; | ||
| 428 | lb->next_bio=bio; | ||
| 429 | if (bio != NULL) | ||
| 430 | bio->prev_bio=lb; | ||
| 431 | /* called to do internal processing */ | ||
| 432 | BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL); | ||
| 433 | return(b); | ||
| 434 | } | ||
| 435 | |||
| 436 | /* Remove the first and return the rest */ | ||
| 437 | BIO *BIO_pop(BIO *b) | ||
| 438 | { | ||
| 439 | BIO *ret; | ||
| 440 | |||
| 441 | if (b == NULL) return(NULL); | ||
| 442 | ret=b->next_bio; | ||
| 443 | |||
| 444 | BIO_ctrl(b,BIO_CTRL_POP,0,NULL); | ||
| 445 | |||
| 446 | if (b->prev_bio != NULL) | ||
| 447 | b->prev_bio->next_bio=b->next_bio; | ||
| 448 | if (b->next_bio != NULL) | ||
| 449 | b->next_bio->prev_bio=b->prev_bio; | ||
| 450 | |||
| 451 | b->next_bio=NULL; | ||
| 452 | b->prev_bio=NULL; | ||
| 453 | return(ret); | ||
| 454 | } | ||
| 455 | |||
| 456 | BIO *BIO_get_retry_BIO(BIO *bio, int *reason) | ||
| 457 | { | ||
| 458 | BIO *b,*last; | ||
| 459 | |||
| 460 | b=last=bio; | ||
| 461 | for (;;) | ||
| 462 | { | ||
| 463 | if (!BIO_should_retry(b)) break; | ||
| 464 | last=b; | ||
| 465 | b=b->next_bio; | ||
| 466 | if (b == NULL) break; | ||
| 467 | } | ||
| 468 | if (reason != NULL) *reason=last->retry_reason; | ||
| 469 | return(last); | ||
| 470 | } | ||
| 471 | |||
| 472 | int BIO_get_retry_reason(BIO *bio) | ||
| 473 | { | ||
| 474 | return(bio->retry_reason); | ||
| 475 | } | ||
| 476 | |||
| 477 | BIO *BIO_find_type(BIO *bio, int type) | ||
| 478 | { | ||
| 479 | int mt,mask; | ||
| 480 | |||
| 481 | if(!bio) return NULL; | ||
| 482 | mask=type&0xff; | ||
| 483 | do { | ||
| 484 | if (bio->method != NULL) | ||
| 485 | { | ||
| 486 | mt=bio->method->type; | ||
| 487 | |||
| 488 | if (!mask) | ||
| 489 | { | ||
| 490 | if (mt & type) return(bio); | ||
| 491 | } | ||
| 492 | else if (mt == type) | ||
| 493 | return(bio); | ||
| 494 | } | ||
| 495 | bio=bio->next_bio; | ||
| 496 | } while (bio != NULL); | ||
| 497 | return(NULL); | ||
| 498 | } | ||
| 499 | |||
| 500 | BIO *BIO_next(BIO *b) | ||
| 501 | { | ||
| 502 | if(!b) return NULL; | ||
| 503 | return b->next_bio; | ||
| 504 | } | ||
| 505 | |||
| 506 | void BIO_free_all(BIO *bio) | ||
| 507 | { | ||
| 508 | BIO *b; | ||
| 509 | int ref; | ||
| 510 | |||
| 511 | while (bio != NULL) | ||
| 512 | { | ||
| 513 | b=bio; | ||
| 514 | ref=b->references; | ||
| 515 | bio=bio->next_bio; | ||
| 516 | BIO_free(b); | ||
| 517 | /* Since ref count > 1, don't free anyone else. */ | ||
| 518 | if (ref > 1) break; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | BIO *BIO_dup_chain(BIO *in) | ||
| 523 | { | ||
| 524 | BIO *ret=NULL,*eoc=NULL,*bio,*new; | ||
| 525 | |||
| 526 | for (bio=in; bio != NULL; bio=bio->next_bio) | ||
| 527 | { | ||
| 528 | if ((new=BIO_new(bio->method)) == NULL) goto err; | ||
| 529 | new->callback=bio->callback; | ||
| 530 | new->cb_arg=bio->cb_arg; | ||
| 531 | new->init=bio->init; | ||
| 532 | new->shutdown=bio->shutdown; | ||
| 533 | new->flags=bio->flags; | ||
| 534 | |||
| 535 | /* This will let SSL_s_sock() work with stdin/stdout */ | ||
| 536 | new->num=bio->num; | ||
| 537 | |||
| 538 | if (!BIO_dup_state(bio,(char *)new)) | ||
| 539 | { | ||
| 540 | BIO_free(new); | ||
| 541 | goto err; | ||
| 542 | } | ||
| 543 | |||
| 544 | /* copy app data */ | ||
| 545 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new->ex_data, | ||
| 546 | &bio->ex_data)) | ||
| 547 | goto err; | ||
| 548 | |||
| 549 | if (ret == NULL) | ||
| 550 | { | ||
| 551 | eoc=new; | ||
| 552 | ret=eoc; | ||
| 553 | } | ||
| 554 | else | ||
| 555 | { | ||
| 556 | BIO_push(eoc,new); | ||
| 557 | eoc=new; | ||
| 558 | } | ||
| 559 | } | ||
| 560 | return(ret); | ||
| 561 | err: | ||
| 562 | if (ret != NULL) | ||
| 563 | BIO_free(ret); | ||
| 564 | return(NULL); | ||
| 565 | } | ||
| 566 | |||
| 567 | void BIO_copy_next_retry(BIO *b) | ||
| 568 | { | ||
| 569 | BIO_set_flags(b,BIO_get_retry_flags(b->next_bio)); | ||
| 570 | b->retry_reason=b->next_bio->retry_reason; | ||
| 571 | } | ||
| 572 | |||
| 573 | int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 574 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
| 575 | { | ||
| 576 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp, | ||
| 577 | new_func, dup_func, free_func); | ||
| 578 | } | ||
| 579 | |||
| 580 | int BIO_set_ex_data(BIO *bio, int idx, void *data) | ||
| 581 | { | ||
| 582 | return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data)); | ||
| 583 | } | ||
| 584 | |||
| 585 | void *BIO_get_ex_data(BIO *bio, int idx) | ||
| 586 | { | ||
| 587 | return(CRYPTO_get_ex_data(&(bio->ex_data),idx)); | ||
| 588 | } | ||
| 589 | |||
| 590 | unsigned long BIO_number_read(BIO *bio) | ||
| 591 | { | ||
| 592 | if(bio) return bio->num_read; | ||
| 593 | return 0; | ||
| 594 | } | ||
| 595 | |||
| 596 | unsigned long BIO_number_written(BIO *bio) | ||
| 597 | { | ||
| 598 | if(bio) return bio->num_write; | ||
| 599 | return 0; | ||
| 600 | } | ||
| 601 | |||
| 602 | IMPLEMENT_STACK_OF(BIO) | ||
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c new file mode 100644 index 0000000000..d090b7272f --- /dev/null +++ b/src/lib/libcrypto/bio/bss_acpt.c | |||
| @@ -0,0 +1,479 @@ | |||
| 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 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #define USE_SOCKETS | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include <openssl/bio.h> | ||
| 64 | |||
| 65 | #ifndef OPENSSL_NO_SOCK | ||
| 66 | |||
| 67 | #ifdef OPENSSL_SYS_WIN16 | ||
| 68 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | ||
| 69 | #else | ||
| 70 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) | ||
| 74 | /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ | ||
| 75 | #undef FIONBIO | ||
| 76 | #endif | ||
| 77 | |||
| 78 | typedef struct bio_accept_st | ||
| 79 | { | ||
| 80 | int state; | ||
| 81 | char *param_addr; | ||
| 82 | |||
| 83 | int accept_sock; | ||
| 84 | int accept_nbio; | ||
| 85 | |||
| 86 | char *addr; | ||
| 87 | int nbio; | ||
| 88 | /* If 0, it means normal, if 1, do a connect on bind failure, | ||
| 89 | * and if there is no-one listening, bind with SO_REUSEADDR. | ||
| 90 | * If 2, always use SO_REUSEADDR. */ | ||
| 91 | int bind_mode; | ||
| 92 | BIO *bio_chain; | ||
| 93 | } BIO_ACCEPT; | ||
| 94 | |||
| 95 | static int acpt_write(BIO *h, const char *buf, int num); | ||
| 96 | static int acpt_read(BIO *h, char *buf, int size); | ||
| 97 | static int acpt_puts(BIO *h, const char *str); | ||
| 98 | static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 99 | static int acpt_new(BIO *h); | ||
| 100 | static int acpt_free(BIO *data); | ||
| 101 | static int acpt_state(BIO *b, BIO_ACCEPT *c); | ||
| 102 | static void acpt_close_socket(BIO *data); | ||
| 103 | BIO_ACCEPT *BIO_ACCEPT_new(void ); | ||
| 104 | void BIO_ACCEPT_free(BIO_ACCEPT *a); | ||
| 105 | |||
| 106 | #define ACPT_S_BEFORE 1 | ||
| 107 | #define ACPT_S_GET_ACCEPT_SOCKET 2 | ||
| 108 | #define ACPT_S_OK 3 | ||
| 109 | |||
| 110 | static BIO_METHOD methods_acceptp= | ||
| 111 | { | ||
| 112 | BIO_TYPE_ACCEPT, | ||
| 113 | "socket accept", | ||
| 114 | acpt_write, | ||
| 115 | acpt_read, | ||
| 116 | acpt_puts, | ||
| 117 | NULL, /* connect_gets, */ | ||
| 118 | acpt_ctrl, | ||
| 119 | acpt_new, | ||
| 120 | acpt_free, | ||
| 121 | NULL, | ||
| 122 | }; | ||
| 123 | |||
| 124 | BIO_METHOD *BIO_s_accept(void) | ||
| 125 | { | ||
| 126 | return(&methods_acceptp); | ||
| 127 | } | ||
| 128 | |||
| 129 | static int acpt_new(BIO *bi) | ||
| 130 | { | ||
| 131 | BIO_ACCEPT *ba; | ||
| 132 | |||
| 133 | bi->init=0; | ||
| 134 | bi->num=INVALID_SOCKET; | ||
| 135 | bi->flags=0; | ||
| 136 | if ((ba=BIO_ACCEPT_new()) == NULL) | ||
| 137 | return(0); | ||
| 138 | bi->ptr=(char *)ba; | ||
| 139 | ba->state=ACPT_S_BEFORE; | ||
| 140 | bi->shutdown=1; | ||
| 141 | return(1); | ||
| 142 | } | ||
| 143 | |||
| 144 | BIO_ACCEPT *BIO_ACCEPT_new(void) | ||
| 145 | { | ||
| 146 | BIO_ACCEPT *ret; | ||
| 147 | |||
| 148 | if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL) | ||
| 149 | return(NULL); | ||
| 150 | |||
| 151 | memset(ret,0,sizeof(BIO_ACCEPT)); | ||
| 152 | ret->accept_sock=INVALID_SOCKET; | ||
| 153 | ret->bind_mode=BIO_BIND_NORMAL; | ||
| 154 | return(ret); | ||
| 155 | } | ||
| 156 | |||
| 157 | void BIO_ACCEPT_free(BIO_ACCEPT *a) | ||
| 158 | { | ||
| 159 | if(a == NULL) | ||
| 160 | return; | ||
| 161 | |||
| 162 | if (a->param_addr != NULL) OPENSSL_free(a->param_addr); | ||
| 163 | if (a->addr != NULL) OPENSSL_free(a->addr); | ||
| 164 | if (a->bio_chain != NULL) BIO_free(a->bio_chain); | ||
| 165 | OPENSSL_free(a); | ||
| 166 | } | ||
| 167 | |||
| 168 | static void acpt_close_socket(BIO *bio) | ||
| 169 | { | ||
| 170 | BIO_ACCEPT *c; | ||
| 171 | |||
| 172 | c=(BIO_ACCEPT *)bio->ptr; | ||
| 173 | if (c->accept_sock != INVALID_SOCKET) | ||
| 174 | { | ||
| 175 | shutdown(c->accept_sock,2); | ||
| 176 | closesocket(c->accept_sock); | ||
| 177 | c->accept_sock=INVALID_SOCKET; | ||
| 178 | bio->num=INVALID_SOCKET; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | static int acpt_free(BIO *a) | ||
| 183 | { | ||
| 184 | BIO_ACCEPT *data; | ||
| 185 | |||
| 186 | if (a == NULL) return(0); | ||
| 187 | data=(BIO_ACCEPT *)a->ptr; | ||
| 188 | |||
| 189 | if (a->shutdown) | ||
| 190 | { | ||
| 191 | acpt_close_socket(a); | ||
| 192 | BIO_ACCEPT_free(data); | ||
| 193 | a->ptr=NULL; | ||
| 194 | a->flags=0; | ||
| 195 | a->init=0; | ||
| 196 | } | ||
| 197 | return(1); | ||
| 198 | } | ||
| 199 | |||
| 200 | static int acpt_state(BIO *b, BIO_ACCEPT *c) | ||
| 201 | { | ||
| 202 | BIO *bio=NULL,*dbio; | ||
| 203 | int s= -1; | ||
| 204 | int i; | ||
| 205 | |||
| 206 | again: | ||
| 207 | switch (c->state) | ||
| 208 | { | ||
| 209 | case ACPT_S_BEFORE: | ||
| 210 | if (c->param_addr == NULL) | ||
| 211 | { | ||
| 212 | BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); | ||
| 213 | return(-1); | ||
| 214 | } | ||
| 215 | s=BIO_get_accept_socket(c->param_addr,c->bind_mode); | ||
| 216 | if (s == INVALID_SOCKET) | ||
| 217 | return(-1); | ||
| 218 | |||
| 219 | if (c->accept_nbio) | ||
| 220 | { | ||
| 221 | if (!BIO_socket_nbio(s,1)) | ||
| 222 | { | ||
| 223 | closesocket(s); | ||
| 224 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); | ||
| 225 | return(-1); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | c->accept_sock=s; | ||
| 229 | b->num=s; | ||
| 230 | c->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
| 231 | return(1); | ||
| 232 | /* break; */ | ||
| 233 | case ACPT_S_GET_ACCEPT_SOCKET: | ||
| 234 | if (b->next_bio != NULL) | ||
| 235 | { | ||
| 236 | c->state=ACPT_S_OK; | ||
| 237 | goto again; | ||
| 238 | } | ||
| 239 | BIO_clear_retry_flags(b); | ||
| 240 | b->retry_reason=0; | ||
| 241 | i=BIO_accept(c->accept_sock,&(c->addr)); | ||
| 242 | |||
| 243 | /* -2 return means we should retry */ | ||
| 244 | if(i == -2) | ||
| 245 | { | ||
| 246 | BIO_set_retry_special(b); | ||
| 247 | b->retry_reason=BIO_RR_ACCEPT; | ||
| 248 | return -1; | ||
| 249 | } | ||
| 250 | |||
| 251 | if (i < 0) return(i); | ||
| 252 | |||
| 253 | bio=BIO_new_socket(i,BIO_CLOSE); | ||
| 254 | if (bio == NULL) goto err; | ||
| 255 | |||
| 256 | BIO_set_callback(bio,BIO_get_callback(b)); | ||
| 257 | BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); | ||
| 258 | |||
| 259 | if (c->nbio) | ||
| 260 | { | ||
| 261 | if (!BIO_socket_nbio(i,1)) | ||
| 262 | { | ||
| 263 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); | ||
| 264 | goto err; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | /* If the accept BIO has an bio_chain, we dup it and | ||
| 269 | * put the new socket at the end. */ | ||
| 270 | if (c->bio_chain != NULL) | ||
| 271 | { | ||
| 272 | if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL) | ||
| 273 | goto err; | ||
| 274 | if (!BIO_push(dbio,bio)) goto err; | ||
| 275 | bio=dbio; | ||
| 276 | } | ||
| 277 | if (BIO_push(b,bio) == NULL) goto err; | ||
| 278 | |||
| 279 | c->state=ACPT_S_OK; | ||
| 280 | return(1); | ||
| 281 | err: | ||
| 282 | if (bio != NULL) | ||
| 283 | BIO_free(bio); | ||
| 284 | else if (s >= 0) | ||
| 285 | closesocket(s); | ||
| 286 | return(0); | ||
| 287 | /* break; */ | ||
| 288 | case ACPT_S_OK: | ||
| 289 | if (b->next_bio == NULL) | ||
| 290 | { | ||
| 291 | c->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
| 292 | goto again; | ||
| 293 | } | ||
| 294 | return(1); | ||
| 295 | /* break; */ | ||
| 296 | default: | ||
| 297 | return(0); | ||
| 298 | /* break; */ | ||
| 299 | } | ||
| 300 | |||
| 301 | } | ||
| 302 | |||
| 303 | static int acpt_read(BIO *b, char *out, int outl) | ||
| 304 | { | ||
| 305 | int ret=0; | ||
| 306 | BIO_ACCEPT *data; | ||
| 307 | |||
| 308 | BIO_clear_retry_flags(b); | ||
| 309 | data=(BIO_ACCEPT *)b->ptr; | ||
| 310 | |||
| 311 | while (b->next_bio == NULL) | ||
| 312 | { | ||
| 313 | ret=acpt_state(b,data); | ||
| 314 | if (ret <= 0) return(ret); | ||
| 315 | } | ||
| 316 | |||
| 317 | ret=BIO_read(b->next_bio,out,outl); | ||
| 318 | BIO_copy_next_retry(b); | ||
| 319 | return(ret); | ||
| 320 | } | ||
| 321 | |||
| 322 | static int acpt_write(BIO *b, const char *in, int inl) | ||
| 323 | { | ||
| 324 | int ret; | ||
| 325 | BIO_ACCEPT *data; | ||
| 326 | |||
| 327 | BIO_clear_retry_flags(b); | ||
| 328 | data=(BIO_ACCEPT *)b->ptr; | ||
| 329 | |||
| 330 | while (b->next_bio == NULL) | ||
| 331 | { | ||
| 332 | ret=acpt_state(b,data); | ||
| 333 | if (ret <= 0) return(ret); | ||
| 334 | } | ||
| 335 | |||
| 336 | ret=BIO_write(b->next_bio,in,inl); | ||
| 337 | BIO_copy_next_retry(b); | ||
| 338 | return(ret); | ||
| 339 | } | ||
| 340 | |||
| 341 | static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 342 | { | ||
| 343 | BIO *dbio; | ||
| 344 | int *ip; | ||
| 345 | long ret=1; | ||
| 346 | BIO_ACCEPT *data; | ||
| 347 | char **pp; | ||
| 348 | |||
| 349 | data=(BIO_ACCEPT *)b->ptr; | ||
| 350 | |||
| 351 | switch (cmd) | ||
| 352 | { | ||
| 353 | case BIO_CTRL_RESET: | ||
| 354 | ret=0; | ||
| 355 | data->state=ACPT_S_BEFORE; | ||
| 356 | acpt_close_socket(b); | ||
| 357 | b->flags=0; | ||
| 358 | break; | ||
| 359 | case BIO_C_DO_STATE_MACHINE: | ||
| 360 | /* use this one to start the connection */ | ||
| 361 | ret=(long)acpt_state(b,data); | ||
| 362 | break; | ||
| 363 | case BIO_C_SET_ACCEPT: | ||
| 364 | if (ptr != NULL) | ||
| 365 | { | ||
| 366 | if (num == 0) | ||
| 367 | { | ||
| 368 | b->init=1; | ||
| 369 | if (data->param_addr != NULL) | ||
| 370 | OPENSSL_free(data->param_addr); | ||
| 371 | data->param_addr=BUF_strdup(ptr); | ||
| 372 | } | ||
| 373 | else if (num == 1) | ||
| 374 | { | ||
| 375 | data->accept_nbio=(ptr != NULL); | ||
| 376 | } | ||
| 377 | else if (num == 2) | ||
| 378 | { | ||
| 379 | if (data->bio_chain != NULL) | ||
| 380 | BIO_free(data->bio_chain); | ||
| 381 | data->bio_chain=(BIO *)ptr; | ||
| 382 | } | ||
| 383 | } | ||
| 384 | break; | ||
| 385 | case BIO_C_SET_NBIO: | ||
| 386 | data->nbio=(int)num; | ||
| 387 | break; | ||
| 388 | case BIO_C_SET_FD: | ||
| 389 | b->init=1; | ||
| 390 | b->num= *((int *)ptr); | ||
| 391 | data->accept_sock=b->num; | ||
| 392 | data->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
| 393 | b->shutdown=(int)num; | ||
| 394 | b->init=1; | ||
| 395 | break; | ||
| 396 | case BIO_C_GET_FD: | ||
| 397 | if (b->init) | ||
| 398 | { | ||
| 399 | ip=(int *)ptr; | ||
| 400 | if (ip != NULL) | ||
| 401 | *ip=data->accept_sock; | ||
| 402 | ret=data->accept_sock; | ||
| 403 | } | ||
| 404 | else | ||
| 405 | ret= -1; | ||
| 406 | break; | ||
| 407 | case BIO_C_GET_ACCEPT: | ||
| 408 | if (b->init) | ||
| 409 | { | ||
| 410 | if (ptr != NULL) | ||
| 411 | { | ||
| 412 | pp=(char **)ptr; | ||
| 413 | *pp=data->param_addr; | ||
| 414 | } | ||
| 415 | else | ||
| 416 | ret= -1; | ||
| 417 | } | ||
| 418 | else | ||
| 419 | ret= -1; | ||
| 420 | break; | ||
| 421 | case BIO_CTRL_GET_CLOSE: | ||
| 422 | ret=b->shutdown; | ||
| 423 | break; | ||
| 424 | case BIO_CTRL_SET_CLOSE: | ||
| 425 | b->shutdown=(int)num; | ||
| 426 | break; | ||
| 427 | case BIO_CTRL_PENDING: | ||
| 428 | case BIO_CTRL_WPENDING: | ||
| 429 | ret=0; | ||
| 430 | break; | ||
| 431 | case BIO_CTRL_FLUSH: | ||
| 432 | break; | ||
| 433 | case BIO_C_SET_BIND_MODE: | ||
| 434 | data->bind_mode=(int)num; | ||
| 435 | break; | ||
| 436 | case BIO_C_GET_BIND_MODE: | ||
| 437 | ret=(long)data->bind_mode; | ||
| 438 | break; | ||
| 439 | case BIO_CTRL_DUP: | ||
| 440 | dbio=(BIO *)ptr; | ||
| 441 | /* if (data->param_port) EAY EAY | ||
| 442 | BIO_set_port(dbio,data->param_port); | ||
| 443 | if (data->param_hostname) | ||
| 444 | BIO_set_hostname(dbio,data->param_hostname); | ||
| 445 | BIO_set_nbio(dbio,data->nbio); */ | ||
| 446 | break; | ||
| 447 | |||
| 448 | default: | ||
| 449 | ret=0; | ||
| 450 | break; | ||
| 451 | } | ||
| 452 | return(ret); | ||
| 453 | } | ||
| 454 | |||
| 455 | static int acpt_puts(BIO *bp, const char *str) | ||
| 456 | { | ||
| 457 | int n,ret; | ||
| 458 | |||
| 459 | n=strlen(str); | ||
| 460 | ret=acpt_write(bp,str,n); | ||
| 461 | return(ret); | ||
| 462 | } | ||
| 463 | |||
| 464 | BIO *BIO_new_accept(char *str) | ||
| 465 | { | ||
| 466 | BIO *ret; | ||
| 467 | |||
| 468 | ret=BIO_new(BIO_s_accept()); | ||
| 469 | if (ret == NULL) return(NULL); | ||
| 470 | if (BIO_set_accept_port(ret,str)) | ||
| 471 | return(ret); | ||
| 472 | else | ||
| 473 | { | ||
| 474 | BIO_free(ret); | ||
| 475 | return(NULL); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | #endif | ||
diff --git a/src/lib/libcrypto/bio/bss_bio.c b/src/lib/libcrypto/bio/bss_bio.c new file mode 100644 index 0000000000..0f9f0955b4 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_bio.c | |||
| @@ -0,0 +1,924 @@ | |||
| 1 | /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@openssl.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* Special method for a BIO where the other endpoint is also a BIO | ||
| 57 | * of this kind, handled by the same thread (i.e. the "peer" is actually | ||
| 58 | * ourselves, wearing a different hat). | ||
| 59 | * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces | ||
| 60 | * for which no specific BIO method is available. | ||
| 61 | * See ssl/ssltest.c for some hints on how this can be used. */ | ||
| 62 | |||
| 63 | /* BIO_DEBUG implies BIO_PAIR_DEBUG */ | ||
| 64 | #ifdef BIO_DEBUG | ||
| 65 | # ifndef BIO_PAIR_DEBUG | ||
| 66 | # define BIO_PAIR_DEBUG | ||
| 67 | # endif | ||
| 68 | #endif | ||
| 69 | |||
| 70 | /* disable assert() unless BIO_PAIR_DEBUG has been defined */ | ||
| 71 | #ifndef BIO_PAIR_DEBUG | ||
| 72 | # ifndef NDEBUG | ||
| 73 | # define NDEBUG | ||
| 74 | # endif | ||
| 75 | #endif | ||
| 76 | |||
| 77 | #include <assert.h> | ||
| 78 | #include <limits.h> | ||
| 79 | #include <stdlib.h> | ||
| 80 | #include <string.h> | ||
| 81 | |||
| 82 | #include <openssl/bio.h> | ||
| 83 | #include <openssl/err.h> | ||
| 84 | #include <openssl/crypto.h> | ||
| 85 | |||
| 86 | #include "e_os.h" | ||
| 87 | |||
| 88 | /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */ | ||
| 89 | #if defined(OPENSSL_SYS_VXWORKS) | ||
| 90 | # undef SSIZE_MAX | ||
| 91 | #endif | ||
| 92 | #ifndef SSIZE_MAX | ||
| 93 | # define SSIZE_MAX INT_MAX | ||
| 94 | #endif | ||
| 95 | |||
| 96 | static int bio_new(BIO *bio); | ||
| 97 | static int bio_free(BIO *bio); | ||
| 98 | static int bio_read(BIO *bio, char *buf, int size); | ||
| 99 | static int bio_write(BIO *bio, const char *buf, int num); | ||
| 100 | static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr); | ||
| 101 | static int bio_puts(BIO *bio, const char *str); | ||
| 102 | |||
| 103 | static int bio_make_pair(BIO *bio1, BIO *bio2); | ||
| 104 | static void bio_destroy_pair(BIO *bio); | ||
| 105 | |||
| 106 | static BIO_METHOD methods_biop = | ||
| 107 | { | ||
| 108 | BIO_TYPE_BIO, | ||
| 109 | "BIO pair", | ||
| 110 | bio_write, | ||
| 111 | bio_read, | ||
| 112 | bio_puts, | ||
| 113 | NULL /* no bio_gets */, | ||
| 114 | bio_ctrl, | ||
| 115 | bio_new, | ||
| 116 | bio_free, | ||
| 117 | NULL /* no bio_callback_ctrl */ | ||
| 118 | }; | ||
| 119 | |||
| 120 | BIO_METHOD *BIO_s_bio(void) | ||
| 121 | { | ||
| 122 | return &methods_biop; | ||
| 123 | } | ||
| 124 | |||
| 125 | struct bio_bio_st | ||
| 126 | { | ||
| 127 | BIO *peer; /* NULL if buf == NULL. | ||
| 128 | * If peer != NULL, then peer->ptr is also a bio_bio_st, | ||
| 129 | * and its "peer" member points back to us. | ||
| 130 | * peer != NULL iff init != 0 in the BIO. */ | ||
| 131 | |||
| 132 | /* This is for what we write (i.e. reading uses peer's struct): */ | ||
| 133 | int closed; /* valid iff peer != NULL */ | ||
| 134 | size_t len; /* valid iff buf != NULL; 0 if peer == NULL */ | ||
| 135 | size_t offset; /* valid iff buf != NULL; 0 if len == 0 */ | ||
| 136 | size_t size; | ||
| 137 | char *buf; /* "size" elements (if != NULL) */ | ||
| 138 | |||
| 139 | size_t request; /* valid iff peer != NULL; 0 if len != 0, | ||
| 140 | * otherwise set by peer to number of bytes | ||
| 141 | * it (unsuccessfully) tried to read, | ||
| 142 | * never more than buffer space (size-len) warrants. */ | ||
| 143 | }; | ||
| 144 | |||
| 145 | static int bio_new(BIO *bio) | ||
| 146 | { | ||
| 147 | struct bio_bio_st *b; | ||
| 148 | |||
| 149 | b = OPENSSL_malloc(sizeof *b); | ||
| 150 | if (b == NULL) | ||
| 151 | return 0; | ||
| 152 | |||
| 153 | b->peer = NULL; | ||
| 154 | b->size = 17*1024; /* enough for one TLS record (just a default) */ | ||
| 155 | b->buf = NULL; | ||
| 156 | |||
| 157 | bio->ptr = b; | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | static int bio_free(BIO *bio) | ||
| 163 | { | ||
| 164 | struct bio_bio_st *b; | ||
| 165 | |||
| 166 | if (bio == NULL) | ||
| 167 | return 0; | ||
| 168 | b = bio->ptr; | ||
| 169 | |||
| 170 | assert(b != NULL); | ||
| 171 | |||
| 172 | if (b->peer) | ||
| 173 | bio_destroy_pair(bio); | ||
| 174 | |||
| 175 | if (b->buf != NULL) | ||
| 176 | { | ||
| 177 | OPENSSL_free(b->buf); | ||
| 178 | } | ||
| 179 | |||
| 180 | OPENSSL_free(b); | ||
| 181 | |||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | static int bio_read(BIO *bio, char *buf, int size_) | ||
| 188 | { | ||
| 189 | size_t size = size_; | ||
| 190 | size_t rest; | ||
| 191 | struct bio_bio_st *b, *peer_b; | ||
| 192 | |||
| 193 | BIO_clear_retry_flags(bio); | ||
| 194 | |||
| 195 | if (!bio->init) | ||
| 196 | return 0; | ||
| 197 | |||
| 198 | b = bio->ptr; | ||
| 199 | assert(b != NULL); | ||
| 200 | assert(b->peer != NULL); | ||
| 201 | peer_b = b->peer->ptr; | ||
| 202 | assert(peer_b != NULL); | ||
| 203 | assert(peer_b->buf != NULL); | ||
| 204 | |||
| 205 | peer_b->request = 0; /* will be set in "retry_read" situation */ | ||
| 206 | |||
| 207 | if (buf == NULL || size == 0) | ||
| 208 | return 0; | ||
| 209 | |||
| 210 | if (peer_b->len == 0) | ||
| 211 | { | ||
| 212 | if (peer_b->closed) | ||
| 213 | return 0; /* writer has closed, and no data is left */ | ||
| 214 | else | ||
| 215 | { | ||
| 216 | BIO_set_retry_read(bio); /* buffer is empty */ | ||
| 217 | if (size <= peer_b->size) | ||
| 218 | peer_b->request = size; | ||
| 219 | else | ||
| 220 | /* don't ask for more than the peer can | ||
| 221 | * deliver in one write */ | ||
| 222 | peer_b->request = peer_b->size; | ||
| 223 | return -1; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | /* we can read */ | ||
| 228 | if (peer_b->len < size) | ||
| 229 | size = peer_b->len; | ||
| 230 | |||
| 231 | /* now read "size" bytes */ | ||
| 232 | |||
| 233 | rest = size; | ||
| 234 | |||
| 235 | assert(rest > 0); | ||
| 236 | do /* one or two iterations */ | ||
| 237 | { | ||
| 238 | size_t chunk; | ||
| 239 | |||
| 240 | assert(rest <= peer_b->len); | ||
| 241 | if (peer_b->offset + rest <= peer_b->size) | ||
| 242 | chunk = rest; | ||
| 243 | else | ||
| 244 | /* wrap around ring buffer */ | ||
| 245 | chunk = peer_b->size - peer_b->offset; | ||
| 246 | assert(peer_b->offset + chunk <= peer_b->size); | ||
| 247 | |||
| 248 | memcpy(buf, peer_b->buf + peer_b->offset, chunk); | ||
| 249 | |||
| 250 | peer_b->len -= chunk; | ||
| 251 | if (peer_b->len) | ||
| 252 | { | ||
| 253 | peer_b->offset += chunk; | ||
| 254 | assert(peer_b->offset <= peer_b->size); | ||
| 255 | if (peer_b->offset == peer_b->size) | ||
| 256 | peer_b->offset = 0; | ||
| 257 | buf += chunk; | ||
| 258 | } | ||
| 259 | else | ||
| 260 | { | ||
| 261 | /* buffer now empty, no need to advance "buf" */ | ||
| 262 | assert(chunk == rest); | ||
| 263 | peer_b->offset = 0; | ||
| 264 | } | ||
| 265 | rest -= chunk; | ||
| 266 | } | ||
| 267 | while (rest); | ||
| 268 | |||
| 269 | return size; | ||
| 270 | } | ||
| 271 | |||
| 272 | /* non-copying interface: provide pointer to available data in buffer | ||
| 273 | * bio_nread0: return number of available bytes | ||
| 274 | * bio_nread: also advance index | ||
| 275 | * (example usage: bio_nread0(), read from buffer, bio_nread() | ||
| 276 | * or just bio_nread(), read from buffer) | ||
| 277 | */ | ||
| 278 | /* WARNING: The non-copying interface is largely untested as of yet | ||
| 279 | * and may contain bugs. */ | ||
| 280 | static ssize_t bio_nread0(BIO *bio, char **buf) | ||
| 281 | { | ||
| 282 | struct bio_bio_st *b, *peer_b; | ||
| 283 | ssize_t num; | ||
| 284 | |||
| 285 | BIO_clear_retry_flags(bio); | ||
| 286 | |||
| 287 | if (!bio->init) | ||
| 288 | return 0; | ||
| 289 | |||
| 290 | b = bio->ptr; | ||
| 291 | assert(b != NULL); | ||
| 292 | assert(b->peer != NULL); | ||
| 293 | peer_b = b->peer->ptr; | ||
| 294 | assert(peer_b != NULL); | ||
| 295 | assert(peer_b->buf != NULL); | ||
| 296 | |||
| 297 | peer_b->request = 0; | ||
| 298 | |||
| 299 | if (peer_b->len == 0) | ||
| 300 | { | ||
| 301 | char dummy; | ||
| 302 | |||
| 303 | /* avoid code duplication -- nothing available for reading */ | ||
| 304 | return bio_read(bio, &dummy, 1); /* returns 0 or -1 */ | ||
| 305 | } | ||
| 306 | |||
| 307 | num = peer_b->len; | ||
| 308 | if (peer_b->size < peer_b->offset + num) | ||
| 309 | /* no ring buffer wrap-around for non-copying interface */ | ||
| 310 | num = peer_b->size - peer_b->offset; | ||
| 311 | assert(num > 0); | ||
| 312 | |||
| 313 | if (buf != NULL) | ||
| 314 | *buf = peer_b->buf + peer_b->offset; | ||
| 315 | return num; | ||
| 316 | } | ||
| 317 | |||
| 318 | static ssize_t bio_nread(BIO *bio, char **buf, size_t num_) | ||
| 319 | { | ||
| 320 | struct bio_bio_st *b, *peer_b; | ||
| 321 | ssize_t num, available; | ||
| 322 | |||
| 323 | if (num_ > SSIZE_MAX) | ||
| 324 | num = SSIZE_MAX; | ||
| 325 | else | ||
| 326 | num = (ssize_t)num_; | ||
| 327 | |||
| 328 | available = bio_nread0(bio, buf); | ||
| 329 | if (num > available) | ||
| 330 | num = available; | ||
| 331 | if (num <= 0) | ||
| 332 | return num; | ||
| 333 | |||
| 334 | b = bio->ptr; | ||
| 335 | peer_b = b->peer->ptr; | ||
| 336 | |||
| 337 | peer_b->len -= num; | ||
| 338 | if (peer_b->len) | ||
| 339 | { | ||
| 340 | peer_b->offset += num; | ||
| 341 | assert(peer_b->offset <= peer_b->size); | ||
| 342 | if (peer_b->offset == peer_b->size) | ||
| 343 | peer_b->offset = 0; | ||
| 344 | } | ||
| 345 | else | ||
| 346 | peer_b->offset = 0; | ||
| 347 | |||
| 348 | return num; | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | static int bio_write(BIO *bio, const char *buf, int num_) | ||
| 353 | { | ||
| 354 | size_t num = num_; | ||
| 355 | size_t rest; | ||
| 356 | struct bio_bio_st *b; | ||
| 357 | |||
| 358 | BIO_clear_retry_flags(bio); | ||
| 359 | |||
| 360 | if (!bio->init || buf == NULL || num == 0) | ||
| 361 | return 0; | ||
| 362 | |||
| 363 | b = bio->ptr; | ||
| 364 | assert(b != NULL); | ||
| 365 | assert(b->peer != NULL); | ||
| 366 | assert(b->buf != NULL); | ||
| 367 | |||
| 368 | b->request = 0; | ||
| 369 | if (b->closed) | ||
| 370 | { | ||
| 371 | /* we already closed */ | ||
| 372 | BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE); | ||
| 373 | return -1; | ||
| 374 | } | ||
| 375 | |||
| 376 | assert(b->len <= b->size); | ||
| 377 | |||
| 378 | if (b->len == b->size) | ||
| 379 | { | ||
| 380 | BIO_set_retry_write(bio); /* buffer is full */ | ||
| 381 | return -1; | ||
| 382 | } | ||
| 383 | |||
| 384 | /* we can write */ | ||
| 385 | if (num > b->size - b->len) | ||
| 386 | num = b->size - b->len; | ||
| 387 | |||
| 388 | /* now write "num" bytes */ | ||
| 389 | |||
| 390 | rest = num; | ||
| 391 | |||
| 392 | assert(rest > 0); | ||
| 393 | do /* one or two iterations */ | ||
| 394 | { | ||
| 395 | size_t write_offset; | ||
| 396 | size_t chunk; | ||
| 397 | |||
| 398 | assert(b->len + rest <= b->size); | ||
| 399 | |||
| 400 | write_offset = b->offset + b->len; | ||
| 401 | if (write_offset >= b->size) | ||
| 402 | write_offset -= b->size; | ||
| 403 | /* b->buf[write_offset] is the first byte we can write to. */ | ||
| 404 | |||
| 405 | if (write_offset + rest <= b->size) | ||
| 406 | chunk = rest; | ||
| 407 | else | ||
| 408 | /* wrap around ring buffer */ | ||
| 409 | chunk = b->size - write_offset; | ||
| 410 | |||
| 411 | memcpy(b->buf + write_offset, buf, chunk); | ||
| 412 | |||
| 413 | b->len += chunk; | ||
| 414 | |||
| 415 | assert(b->len <= b->size); | ||
| 416 | |||
| 417 | rest -= chunk; | ||
| 418 | buf += chunk; | ||
| 419 | } | ||
| 420 | while (rest); | ||
| 421 | |||
| 422 | return num; | ||
| 423 | } | ||
| 424 | |||
| 425 | /* non-copying interface: provide pointer to region to write to | ||
| 426 | * bio_nwrite0: check how much space is available | ||
| 427 | * bio_nwrite: also increase length | ||
| 428 | * (example usage: bio_nwrite0(), write to buffer, bio_nwrite() | ||
| 429 | * or just bio_nwrite(), write to buffer) | ||
| 430 | */ | ||
| 431 | static ssize_t bio_nwrite0(BIO *bio, char **buf) | ||
| 432 | { | ||
| 433 | struct bio_bio_st *b; | ||
| 434 | size_t num; | ||
| 435 | size_t write_offset; | ||
| 436 | |||
| 437 | BIO_clear_retry_flags(bio); | ||
| 438 | |||
| 439 | if (!bio->init) | ||
| 440 | return 0; | ||
| 441 | |||
| 442 | b = bio->ptr; | ||
| 443 | assert(b != NULL); | ||
| 444 | assert(b->peer != NULL); | ||
| 445 | assert(b->buf != NULL); | ||
| 446 | |||
| 447 | b->request = 0; | ||
| 448 | if (b->closed) | ||
| 449 | { | ||
| 450 | BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE); | ||
| 451 | return -1; | ||
| 452 | } | ||
| 453 | |||
| 454 | assert(b->len <= b->size); | ||
| 455 | |||
| 456 | if (b->len == b->size) | ||
| 457 | { | ||
| 458 | BIO_set_retry_write(bio); | ||
| 459 | return -1; | ||
| 460 | } | ||
| 461 | |||
| 462 | num = b->size - b->len; | ||
| 463 | write_offset = b->offset + b->len; | ||
| 464 | if (write_offset >= b->size) | ||
| 465 | write_offset -= b->size; | ||
| 466 | if (write_offset + num > b->size) | ||
| 467 | /* no ring buffer wrap-around for non-copying interface | ||
| 468 | * (to fulfil the promise by BIO_ctrl_get_write_guarantee, | ||
| 469 | * BIO_nwrite may have to be called twice) */ | ||
| 470 | num = b->size - write_offset; | ||
| 471 | |||
| 472 | if (buf != NULL) | ||
| 473 | *buf = b->buf + write_offset; | ||
| 474 | assert(write_offset + num <= b->size); | ||
| 475 | |||
| 476 | return num; | ||
| 477 | } | ||
| 478 | |||
| 479 | static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_) | ||
| 480 | { | ||
| 481 | struct bio_bio_st *b; | ||
| 482 | ssize_t num, space; | ||
| 483 | |||
| 484 | if (num_ > SSIZE_MAX) | ||
| 485 | num = SSIZE_MAX; | ||
| 486 | else | ||
| 487 | num = (ssize_t)num_; | ||
| 488 | |||
| 489 | space = bio_nwrite0(bio, buf); | ||
| 490 | if (num > space) | ||
| 491 | num = space; | ||
| 492 | if (num <= 0) | ||
| 493 | return num; | ||
| 494 | b = bio->ptr; | ||
| 495 | assert(b != NULL); | ||
| 496 | b->len += num; | ||
| 497 | assert(b->len <= b->size); | ||
| 498 | |||
| 499 | return num; | ||
| 500 | } | ||
| 501 | |||
| 502 | |||
| 503 | static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) | ||
| 504 | { | ||
| 505 | long ret; | ||
| 506 | struct bio_bio_st *b = bio->ptr; | ||
| 507 | |||
| 508 | assert(b != NULL); | ||
| 509 | |||
| 510 | switch (cmd) | ||
| 511 | { | ||
| 512 | /* specific CTRL codes */ | ||
| 513 | |||
| 514 | case BIO_C_SET_WRITE_BUF_SIZE: | ||
| 515 | if (b->peer) | ||
| 516 | { | ||
| 517 | BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE); | ||
| 518 | ret = 0; | ||
| 519 | } | ||
| 520 | else if (num == 0) | ||
| 521 | { | ||
| 522 | BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT); | ||
| 523 | ret = 0; | ||
| 524 | } | ||
| 525 | else | ||
| 526 | { | ||
| 527 | size_t new_size = num; | ||
| 528 | |||
| 529 | if (b->size != new_size) | ||
| 530 | { | ||
| 531 | if (b->buf) | ||
| 532 | { | ||
| 533 | OPENSSL_free(b->buf); | ||
| 534 | b->buf = NULL; | ||
| 535 | } | ||
| 536 | b->size = new_size; | ||
| 537 | } | ||
| 538 | ret = 1; | ||
| 539 | } | ||
| 540 | break; | ||
| 541 | |||
| 542 | case BIO_C_GET_WRITE_BUF_SIZE: | ||
| 543 | ret = (long) b->size; | ||
| 544 | break; | ||
| 545 | |||
| 546 | case BIO_C_MAKE_BIO_PAIR: | ||
| 547 | { | ||
| 548 | BIO *other_bio = ptr; | ||
| 549 | |||
| 550 | if (bio_make_pair(bio, other_bio)) | ||
| 551 | ret = 1; | ||
| 552 | else | ||
| 553 | ret = 0; | ||
| 554 | } | ||
| 555 | break; | ||
| 556 | |||
| 557 | case BIO_C_DESTROY_BIO_PAIR: | ||
| 558 | /* Affects both BIOs in the pair -- call just once! | ||
| 559 | * Or let BIO_free(bio1); BIO_free(bio2); do the job. */ | ||
| 560 | bio_destroy_pair(bio); | ||
| 561 | ret = 1; | ||
| 562 | break; | ||
| 563 | |||
| 564 | case BIO_C_GET_WRITE_GUARANTEE: | ||
| 565 | /* How many bytes can the caller feed to the next write | ||
| 566 | * without having to keep any? */ | ||
| 567 | if (b->peer == NULL || b->closed) | ||
| 568 | ret = 0; | ||
| 569 | else | ||
| 570 | ret = (long) b->size - b->len; | ||
| 571 | break; | ||
| 572 | |||
| 573 | case BIO_C_GET_READ_REQUEST: | ||
| 574 | /* If the peer unsuccessfully tried to read, how many bytes | ||
| 575 | * were requested? (As with BIO_CTRL_PENDING, that number | ||
| 576 | * can usually be treated as boolean.) */ | ||
| 577 | ret = (long) b->request; | ||
| 578 | break; | ||
| 579 | |||
| 580 | case BIO_C_RESET_READ_REQUEST: | ||
| 581 | /* Reset request. (Can be useful after read attempts | ||
| 582 | * at the other side that are meant to be non-blocking, | ||
| 583 | * e.g. when probing SSL_read to see if any data is | ||
| 584 | * available.) */ | ||
| 585 | b->request = 0; | ||
| 586 | ret = 1; | ||
| 587 | break; | ||
| 588 | |||
| 589 | case BIO_C_SHUTDOWN_WR: | ||
| 590 | /* similar to shutdown(..., SHUT_WR) */ | ||
| 591 | b->closed = 1; | ||
| 592 | ret = 1; | ||
| 593 | break; | ||
| 594 | |||
| 595 | case BIO_C_NREAD0: | ||
| 596 | /* prepare for non-copying read */ | ||
| 597 | ret = (long) bio_nread0(bio, ptr); | ||
| 598 | break; | ||
| 599 | |||
| 600 | case BIO_C_NREAD: | ||
| 601 | /* non-copying read */ | ||
| 602 | ret = (long) bio_nread(bio, ptr, (size_t) num); | ||
| 603 | break; | ||
| 604 | |||
| 605 | case BIO_C_NWRITE0: | ||
| 606 | /* prepare for non-copying write */ | ||
| 607 | ret = (long) bio_nwrite0(bio, ptr); | ||
| 608 | break; | ||
| 609 | |||
| 610 | case BIO_C_NWRITE: | ||
| 611 | /* non-copying write */ | ||
| 612 | ret = (long) bio_nwrite(bio, ptr, (size_t) num); | ||
| 613 | break; | ||
| 614 | |||
| 615 | |||
| 616 | /* standard CTRL codes follow */ | ||
| 617 | |||
| 618 | case BIO_CTRL_RESET: | ||
| 619 | if (b->buf != NULL) | ||
| 620 | { | ||
| 621 | b->len = 0; | ||
| 622 | b->offset = 0; | ||
| 623 | } | ||
| 624 | ret = 0; | ||
| 625 | break; | ||
| 626 | |||
| 627 | case BIO_CTRL_GET_CLOSE: | ||
| 628 | ret = bio->shutdown; | ||
| 629 | break; | ||
| 630 | |||
| 631 | case BIO_CTRL_SET_CLOSE: | ||
| 632 | bio->shutdown = (int) num; | ||
| 633 | ret = 1; | ||
| 634 | break; | ||
| 635 | |||
| 636 | case BIO_CTRL_PENDING: | ||
| 637 | if (b->peer != NULL) | ||
| 638 | { | ||
| 639 | struct bio_bio_st *peer_b = b->peer->ptr; | ||
| 640 | |||
| 641 | ret = (long) peer_b->len; | ||
| 642 | } | ||
| 643 | else | ||
| 644 | ret = 0; | ||
| 645 | break; | ||
| 646 | |||
| 647 | case BIO_CTRL_WPENDING: | ||
| 648 | if (b->buf != NULL) | ||
| 649 | ret = (long) b->len; | ||
| 650 | else | ||
| 651 | ret = 0; | ||
| 652 | break; | ||
| 653 | |||
| 654 | case BIO_CTRL_DUP: | ||
| 655 | /* See BIO_dup_chain for circumstances we have to expect. */ | ||
| 656 | { | ||
| 657 | BIO *other_bio = ptr; | ||
| 658 | struct bio_bio_st *other_b; | ||
| 659 | |||
| 660 | assert(other_bio != NULL); | ||
| 661 | other_b = other_bio->ptr; | ||
| 662 | assert(other_b != NULL); | ||
| 663 | |||
| 664 | assert(other_b->buf == NULL); /* other_bio is always fresh */ | ||
| 665 | |||
| 666 | other_b->size = b->size; | ||
| 667 | } | ||
| 668 | |||
| 669 | ret = 1; | ||
| 670 | break; | ||
| 671 | |||
| 672 | case BIO_CTRL_FLUSH: | ||
| 673 | ret = 1; | ||
| 674 | break; | ||
| 675 | |||
| 676 | case BIO_CTRL_EOF: | ||
| 677 | { | ||
| 678 | BIO *other_bio = ptr; | ||
| 679 | |||
| 680 | if (other_bio) | ||
| 681 | { | ||
| 682 | struct bio_bio_st *other_b = other_bio->ptr; | ||
| 683 | |||
| 684 | assert(other_b != NULL); | ||
| 685 | ret = other_b->len == 0 && other_b->closed; | ||
| 686 | } | ||
| 687 | else | ||
| 688 | ret = 1; | ||
| 689 | } | ||
| 690 | break; | ||
| 691 | |||
| 692 | default: | ||
| 693 | ret = 0; | ||
| 694 | } | ||
| 695 | return ret; | ||
| 696 | } | ||
| 697 | |||
| 698 | static int bio_puts(BIO *bio, const char *str) | ||
| 699 | { | ||
| 700 | return bio_write(bio, str, strlen(str)); | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | static int bio_make_pair(BIO *bio1, BIO *bio2) | ||
| 705 | { | ||
| 706 | struct bio_bio_st *b1, *b2; | ||
| 707 | |||
| 708 | assert(bio1 != NULL); | ||
| 709 | assert(bio2 != NULL); | ||
| 710 | |||
| 711 | b1 = bio1->ptr; | ||
| 712 | b2 = bio2->ptr; | ||
| 713 | |||
| 714 | if (b1->peer != NULL || b2->peer != NULL) | ||
| 715 | { | ||
| 716 | BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE); | ||
| 717 | return 0; | ||
| 718 | } | ||
| 719 | |||
| 720 | if (b1->buf == NULL) | ||
| 721 | { | ||
| 722 | b1->buf = OPENSSL_malloc(b1->size); | ||
| 723 | if (b1->buf == NULL) | ||
| 724 | { | ||
| 725 | BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); | ||
| 726 | return 0; | ||
| 727 | } | ||
| 728 | b1->len = 0; | ||
| 729 | b1->offset = 0; | ||
| 730 | } | ||
| 731 | |||
| 732 | if (b2->buf == NULL) | ||
| 733 | { | ||
| 734 | b2->buf = OPENSSL_malloc(b2->size); | ||
| 735 | if (b2->buf == NULL) | ||
| 736 | { | ||
| 737 | BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); | ||
| 738 | return 0; | ||
| 739 | } | ||
| 740 | b2->len = 0; | ||
| 741 | b2->offset = 0; | ||
| 742 | } | ||
| 743 | |||
| 744 | b1->peer = bio2; | ||
| 745 | b1->closed = 0; | ||
| 746 | b1->request = 0; | ||
| 747 | b2->peer = bio1; | ||
| 748 | b2->closed = 0; | ||
| 749 | b2->request = 0; | ||
| 750 | |||
| 751 | bio1->init = 1; | ||
| 752 | bio2->init = 1; | ||
| 753 | |||
| 754 | return 1; | ||
| 755 | } | ||
| 756 | |||
| 757 | static void bio_destroy_pair(BIO *bio) | ||
| 758 | { | ||
| 759 | struct bio_bio_st *b = bio->ptr; | ||
| 760 | |||
| 761 | if (b != NULL) | ||
| 762 | { | ||
| 763 | BIO *peer_bio = b->peer; | ||
| 764 | |||
| 765 | if (peer_bio != NULL) | ||
| 766 | { | ||
| 767 | struct bio_bio_st *peer_b = peer_bio->ptr; | ||
| 768 | |||
| 769 | assert(peer_b != NULL); | ||
| 770 | assert(peer_b->peer == bio); | ||
| 771 | |||
| 772 | peer_b->peer = NULL; | ||
| 773 | peer_bio->init = 0; | ||
| 774 | assert(peer_b->buf != NULL); | ||
| 775 | peer_b->len = 0; | ||
| 776 | peer_b->offset = 0; | ||
| 777 | |||
| 778 | b->peer = NULL; | ||
| 779 | bio->init = 0; | ||
| 780 | assert(b->buf != NULL); | ||
| 781 | b->len = 0; | ||
| 782 | b->offset = 0; | ||
| 783 | } | ||
| 784 | } | ||
| 785 | } | ||
| 786 | |||
| 787 | |||
| 788 | /* Exported convenience functions */ | ||
| 789 | int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1, | ||
| 790 | BIO **bio2_p, size_t writebuf2) | ||
| 791 | { | ||
| 792 | BIO *bio1 = NULL, *bio2 = NULL; | ||
| 793 | long r; | ||
| 794 | int ret = 0; | ||
| 795 | |||
| 796 | bio1 = BIO_new(BIO_s_bio()); | ||
| 797 | if (bio1 == NULL) | ||
| 798 | goto err; | ||
| 799 | bio2 = BIO_new(BIO_s_bio()); | ||
| 800 | if (bio2 == NULL) | ||
| 801 | goto err; | ||
| 802 | |||
| 803 | if (writebuf1) | ||
| 804 | { | ||
| 805 | r = BIO_set_write_buf_size(bio1, writebuf1); | ||
| 806 | if (!r) | ||
| 807 | goto err; | ||
| 808 | } | ||
| 809 | if (writebuf2) | ||
| 810 | { | ||
| 811 | r = BIO_set_write_buf_size(bio2, writebuf2); | ||
| 812 | if (!r) | ||
| 813 | goto err; | ||
| 814 | } | ||
| 815 | |||
| 816 | r = BIO_make_bio_pair(bio1, bio2); | ||
| 817 | if (!r) | ||
| 818 | goto err; | ||
| 819 | ret = 1; | ||
| 820 | |||
| 821 | err: | ||
| 822 | if (ret == 0) | ||
| 823 | { | ||
| 824 | if (bio1) | ||
| 825 | { | ||
| 826 | BIO_free(bio1); | ||
| 827 | bio1 = NULL; | ||
| 828 | } | ||
| 829 | if (bio2) | ||
| 830 | { | ||
| 831 | BIO_free(bio2); | ||
| 832 | bio2 = NULL; | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | *bio1_p = bio1; | ||
| 837 | *bio2_p = bio2; | ||
| 838 | return ret; | ||
| 839 | } | ||
| 840 | |||
| 841 | size_t BIO_ctrl_get_write_guarantee(BIO *bio) | ||
| 842 | { | ||
| 843 | return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL); | ||
| 844 | } | ||
| 845 | |||
| 846 | size_t BIO_ctrl_get_read_request(BIO *bio) | ||
| 847 | { | ||
| 848 | return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL); | ||
| 849 | } | ||
| 850 | |||
| 851 | int BIO_ctrl_reset_read_request(BIO *bio) | ||
| 852 | { | ||
| 853 | return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0); | ||
| 854 | } | ||
| 855 | |||
| 856 | |||
| 857 | /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now | ||
| 858 | * (conceivably some other BIOs could allow non-copying reads and writes too.) | ||
| 859 | */ | ||
| 860 | int BIO_nread0(BIO *bio, char **buf) | ||
| 861 | { | ||
| 862 | long ret; | ||
| 863 | |||
| 864 | if (!bio->init) | ||
| 865 | { | ||
| 866 | BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED); | ||
| 867 | return -2; | ||
| 868 | } | ||
| 869 | |||
| 870 | ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf); | ||
| 871 | if (ret > INT_MAX) | ||
| 872 | return INT_MAX; | ||
| 873 | else | ||
| 874 | return (int) ret; | ||
| 875 | } | ||
| 876 | |||
| 877 | int BIO_nread(BIO *bio, char **buf, int num) | ||
| 878 | { | ||
| 879 | int ret; | ||
| 880 | |||
| 881 | if (!bio->init) | ||
| 882 | { | ||
| 883 | BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED); | ||
| 884 | return -2; | ||
| 885 | } | ||
| 886 | |||
| 887 | ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf); | ||
| 888 | if (ret > 0) | ||
| 889 | bio->num_read += ret; | ||
| 890 | return ret; | ||
| 891 | } | ||
| 892 | |||
| 893 | int BIO_nwrite0(BIO *bio, char **buf) | ||
| 894 | { | ||
| 895 | long ret; | ||
| 896 | |||
| 897 | if (!bio->init) | ||
| 898 | { | ||
| 899 | BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED); | ||
| 900 | return -2; | ||
| 901 | } | ||
| 902 | |||
| 903 | ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf); | ||
| 904 | if (ret > INT_MAX) | ||
| 905 | return INT_MAX; | ||
| 906 | else | ||
| 907 | return (int) ret; | ||
| 908 | } | ||
| 909 | |||
| 910 | int BIO_nwrite(BIO *bio, char **buf, int num) | ||
| 911 | { | ||
| 912 | int ret; | ||
| 913 | |||
| 914 | if (!bio->init) | ||
| 915 | { | ||
| 916 | BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED); | ||
| 917 | return -2; | ||
| 918 | } | ||
| 919 | |||
| 920 | ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf); | ||
| 921 | if (ret > 0) | ||
| 922 | bio->num_read += ret; | ||
| 923 | return ret; | ||
| 924 | } | ||
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c new file mode 100644 index 0000000000..c14727855b --- /dev/null +++ b/src/lib/libcrypto/bio/bss_conn.c | |||
| @@ -0,0 +1,652 @@ | |||
| 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 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #define USE_SOCKETS | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include <openssl/bio.h> | ||
| 64 | |||
| 65 | #ifndef OPENSSL_NO_SOCK | ||
| 66 | |||
| 67 | #ifdef OPENSSL_SYS_WIN16 | ||
| 68 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | ||
| 69 | #else | ||
| 70 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) | ||
| 74 | /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ | ||
| 75 | #undef FIONBIO | ||
| 76 | #endif | ||
| 77 | |||
| 78 | |||
| 79 | typedef struct bio_connect_st | ||
| 80 | { | ||
| 81 | int state; | ||
| 82 | |||
| 83 | char *param_hostname; | ||
| 84 | char *param_port; | ||
| 85 | int nbio; | ||
| 86 | |||
| 87 | unsigned char ip[4]; | ||
| 88 | unsigned short port; | ||
| 89 | |||
| 90 | struct sockaddr_in them; | ||
| 91 | |||
| 92 | /* int socket; this will be kept in bio->num so that it is | ||
| 93 | * compatible with the bss_sock bio */ | ||
| 94 | |||
| 95 | /* called when the connection is initially made | ||
| 96 | * callback(BIO,state,ret); The callback should return | ||
| 97 | * 'ret'. state is for compatibility with the ssl info_callback */ | ||
| 98 | int (*info_callback)(const BIO *bio,int state,int ret); | ||
| 99 | } BIO_CONNECT; | ||
| 100 | |||
| 101 | static int conn_write(BIO *h, const char *buf, int num); | ||
| 102 | static int conn_read(BIO *h, char *buf, int size); | ||
| 103 | static int conn_puts(BIO *h, const char *str); | ||
| 104 | static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 105 | static int conn_new(BIO *h); | ||
| 106 | static int conn_free(BIO *data); | ||
| 107 | static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *); | ||
| 108 | |||
| 109 | static int conn_state(BIO *b, BIO_CONNECT *c); | ||
| 110 | static void conn_close_socket(BIO *data); | ||
| 111 | BIO_CONNECT *BIO_CONNECT_new(void ); | ||
| 112 | void BIO_CONNECT_free(BIO_CONNECT *a); | ||
| 113 | |||
| 114 | static BIO_METHOD methods_connectp= | ||
| 115 | { | ||
| 116 | BIO_TYPE_CONNECT, | ||
| 117 | "socket connect", | ||
| 118 | conn_write, | ||
| 119 | conn_read, | ||
| 120 | conn_puts, | ||
| 121 | NULL, /* connect_gets, */ | ||
| 122 | conn_ctrl, | ||
| 123 | conn_new, | ||
| 124 | conn_free, | ||
| 125 | conn_callback_ctrl, | ||
| 126 | }; | ||
| 127 | |||
| 128 | static int conn_state(BIO *b, BIO_CONNECT *c) | ||
| 129 | { | ||
| 130 | int ret= -1,i; | ||
| 131 | unsigned long l; | ||
| 132 | char *p,*q; | ||
| 133 | int (*cb)(const BIO *,int,int)=NULL; | ||
| 134 | |||
| 135 | if (c->info_callback != NULL) | ||
| 136 | cb=c->info_callback; | ||
| 137 | |||
| 138 | for (;;) | ||
| 139 | { | ||
| 140 | switch (c->state) | ||
| 141 | { | ||
| 142 | case BIO_CONN_S_BEFORE: | ||
| 143 | p=c->param_hostname; | ||
| 144 | if (p == NULL) | ||
| 145 | { | ||
| 146 | BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED); | ||
| 147 | goto exit_loop; | ||
| 148 | } | ||
| 149 | for ( ; *p != '\0'; p++) | ||
| 150 | { | ||
| 151 | if ((*p == ':') || (*p == '/')) break; | ||
| 152 | } | ||
| 153 | |||
| 154 | i= *p; | ||
| 155 | if ((i == ':') || (i == '/')) | ||
| 156 | { | ||
| 157 | |||
| 158 | *(p++)='\0'; | ||
| 159 | if (i == ':') | ||
| 160 | { | ||
| 161 | for (q=p; *q; q++) | ||
| 162 | if (*q == '/') | ||
| 163 | { | ||
| 164 | *q='\0'; | ||
| 165 | break; | ||
| 166 | } | ||
| 167 | if (c->param_port != NULL) | ||
| 168 | OPENSSL_free(c->param_port); | ||
| 169 | c->param_port=BUF_strdup(p); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | if (c->param_port == NULL) | ||
| 174 | { | ||
| 175 | BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED); | ||
| 176 | ERR_add_error_data(2,"host=",c->param_hostname); | ||
| 177 | goto exit_loop; | ||
| 178 | } | ||
| 179 | c->state=BIO_CONN_S_GET_IP; | ||
| 180 | break; | ||
| 181 | |||
| 182 | case BIO_CONN_S_GET_IP: | ||
| 183 | if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0) | ||
| 184 | goto exit_loop; | ||
| 185 | c->state=BIO_CONN_S_GET_PORT; | ||
| 186 | break; | ||
| 187 | |||
| 188 | case BIO_CONN_S_GET_PORT: | ||
| 189 | if (c->param_port == NULL) | ||
| 190 | { | ||
| 191 | /* abort(); */ | ||
| 192 | goto exit_loop; | ||
| 193 | } | ||
| 194 | else if (BIO_get_port(c->param_port,&c->port) <= 0) | ||
| 195 | goto exit_loop; | ||
| 196 | c->state=BIO_CONN_S_CREATE_SOCKET; | ||
| 197 | break; | ||
| 198 | |||
| 199 | case BIO_CONN_S_CREATE_SOCKET: | ||
| 200 | /* now setup address */ | ||
| 201 | memset((char *)&c->them,0,sizeof(c->them)); | ||
| 202 | c->them.sin_family=AF_INET; | ||
| 203 | c->them.sin_port=htons((unsigned short)c->port); | ||
| 204 | l=(unsigned long) | ||
| 205 | ((unsigned long)c->ip[0]<<24L)| | ||
| 206 | ((unsigned long)c->ip[1]<<16L)| | ||
| 207 | ((unsigned long)c->ip[2]<< 8L)| | ||
| 208 | ((unsigned long)c->ip[3]); | ||
| 209 | c->them.sin_addr.s_addr=htonl(l); | ||
| 210 | c->state=BIO_CONN_S_CREATE_SOCKET; | ||
| 211 | |||
| 212 | ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); | ||
| 213 | if (ret == INVALID_SOCKET) | ||
| 214 | { | ||
| 215 | SYSerr(SYS_F_SOCKET,get_last_socket_error()); | ||
| 216 | ERR_add_error_data(4,"host=",c->param_hostname, | ||
| 217 | ":",c->param_port); | ||
| 218 | BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET); | ||
| 219 | goto exit_loop; | ||
| 220 | } | ||
| 221 | b->num=ret; | ||
| 222 | c->state=BIO_CONN_S_NBIO; | ||
| 223 | break; | ||
| 224 | |||
| 225 | case BIO_CONN_S_NBIO: | ||
| 226 | if (c->nbio) | ||
| 227 | { | ||
| 228 | if (!BIO_socket_nbio(b->num,1)) | ||
| 229 | { | ||
| 230 | BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO); | ||
| 231 | ERR_add_error_data(4,"host=", | ||
| 232 | c->param_hostname, | ||
| 233 | ":",c->param_port); | ||
| 234 | goto exit_loop; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | c->state=BIO_CONN_S_CONNECT; | ||
| 238 | |||
| 239 | #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE) | ||
| 240 | i=1; | ||
| 241 | i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); | ||
| 242 | if (i < 0) | ||
| 243 | { | ||
| 244 | SYSerr(SYS_F_SOCKET,get_last_socket_error()); | ||
| 245 | ERR_add_error_data(4,"host=",c->param_hostname, | ||
| 246 | ":",c->param_port); | ||
| 247 | BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE); | ||
| 248 | goto exit_loop; | ||
| 249 | } | ||
| 250 | #endif | ||
| 251 | break; | ||
| 252 | |||
| 253 | case BIO_CONN_S_CONNECT: | ||
| 254 | BIO_clear_retry_flags(b); | ||
| 255 | ret=connect(b->num, | ||
| 256 | (struct sockaddr *)&c->them, | ||
| 257 | sizeof(c->them)); | ||
| 258 | b->retry_reason=0; | ||
| 259 | if (ret < 0) | ||
| 260 | { | ||
| 261 | if (BIO_sock_should_retry(ret)) | ||
| 262 | { | ||
| 263 | BIO_set_retry_special(b); | ||
| 264 | c->state=BIO_CONN_S_BLOCKED_CONNECT; | ||
| 265 | b->retry_reason=BIO_RR_CONNECT; | ||
| 266 | } | ||
| 267 | else | ||
| 268 | { | ||
| 269 | SYSerr(SYS_F_CONNECT,get_last_socket_error()); | ||
| 270 | ERR_add_error_data(4,"host=", | ||
| 271 | c->param_hostname, | ||
| 272 | ":",c->param_port); | ||
| 273 | BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR); | ||
| 274 | } | ||
| 275 | goto exit_loop; | ||
| 276 | } | ||
| 277 | else | ||
| 278 | c->state=BIO_CONN_S_OK; | ||
| 279 | break; | ||
| 280 | |||
| 281 | case BIO_CONN_S_BLOCKED_CONNECT: | ||
| 282 | i=BIO_sock_error(b->num); | ||
| 283 | if (i) | ||
| 284 | { | ||
| 285 | BIO_clear_retry_flags(b); | ||
| 286 | SYSerr(SYS_F_CONNECT,i); | ||
| 287 | ERR_add_error_data(4,"host=", | ||
| 288 | c->param_hostname, | ||
| 289 | ":",c->param_port); | ||
| 290 | BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR); | ||
| 291 | ret=0; | ||
| 292 | goto exit_loop; | ||
| 293 | } | ||
| 294 | else | ||
| 295 | c->state=BIO_CONN_S_OK; | ||
| 296 | break; | ||
| 297 | |||
| 298 | case BIO_CONN_S_OK: | ||
| 299 | ret=1; | ||
| 300 | goto exit_loop; | ||
| 301 | default: | ||
| 302 | /* abort(); */ | ||
| 303 | goto exit_loop; | ||
| 304 | } | ||
| 305 | |||
| 306 | if (cb != NULL) | ||
| 307 | { | ||
| 308 | if (!(ret=cb((BIO *)b,c->state,ret))) | ||
| 309 | goto end; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | /* Loop does not exit */ | ||
| 314 | exit_loop: | ||
| 315 | if (cb != NULL) | ||
| 316 | ret=cb((BIO *)b,c->state,ret); | ||
| 317 | end: | ||
| 318 | return(ret); | ||
| 319 | } | ||
| 320 | |||
| 321 | BIO_CONNECT *BIO_CONNECT_new(void) | ||
| 322 | { | ||
| 323 | BIO_CONNECT *ret; | ||
| 324 | |||
| 325 | if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL) | ||
| 326 | return(NULL); | ||
| 327 | ret->state=BIO_CONN_S_BEFORE; | ||
| 328 | ret->param_hostname=NULL; | ||
| 329 | ret->param_port=NULL; | ||
| 330 | ret->info_callback=NULL; | ||
| 331 | ret->nbio=0; | ||
| 332 | ret->ip[0]=0; | ||
| 333 | ret->ip[1]=0; | ||
| 334 | ret->ip[2]=0; | ||
| 335 | ret->ip[3]=0; | ||
| 336 | ret->port=0; | ||
| 337 | memset((char *)&ret->them,0,sizeof(ret->them)); | ||
| 338 | return(ret); | ||
| 339 | } | ||
| 340 | |||
| 341 | void BIO_CONNECT_free(BIO_CONNECT *a) | ||
| 342 | { | ||
| 343 | if(a == NULL) | ||
| 344 | return; | ||
| 345 | |||
| 346 | if (a->param_hostname != NULL) | ||
| 347 | OPENSSL_free(a->param_hostname); | ||
| 348 | if (a->param_port != NULL) | ||
| 349 | OPENSSL_free(a->param_port); | ||
| 350 | OPENSSL_free(a); | ||
| 351 | } | ||
| 352 | |||
| 353 | BIO_METHOD *BIO_s_connect(void) | ||
| 354 | { | ||
| 355 | return(&methods_connectp); | ||
| 356 | } | ||
| 357 | |||
| 358 | static int conn_new(BIO *bi) | ||
| 359 | { | ||
| 360 | bi->init=0; | ||
| 361 | bi->num=INVALID_SOCKET; | ||
| 362 | bi->flags=0; | ||
| 363 | if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL) | ||
| 364 | return(0); | ||
| 365 | else | ||
| 366 | return(1); | ||
| 367 | } | ||
| 368 | |||
| 369 | static void conn_close_socket(BIO *bio) | ||
| 370 | { | ||
| 371 | BIO_CONNECT *c; | ||
| 372 | |||
| 373 | c=(BIO_CONNECT *)bio->ptr; | ||
| 374 | if (bio->num != INVALID_SOCKET) | ||
| 375 | { | ||
| 376 | /* Only do a shutdown if things were established */ | ||
| 377 | if (c->state == BIO_CONN_S_OK) | ||
| 378 | shutdown(bio->num,2); | ||
| 379 | closesocket(bio->num); | ||
| 380 | bio->num=INVALID_SOCKET; | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | static int conn_free(BIO *a) | ||
| 385 | { | ||
| 386 | BIO_CONNECT *data; | ||
| 387 | |||
| 388 | if (a == NULL) return(0); | ||
| 389 | data=(BIO_CONNECT *)a->ptr; | ||
| 390 | |||
| 391 | if (a->shutdown) | ||
| 392 | { | ||
| 393 | conn_close_socket(a); | ||
| 394 | BIO_CONNECT_free(data); | ||
| 395 | a->ptr=NULL; | ||
| 396 | a->flags=0; | ||
| 397 | a->init=0; | ||
| 398 | } | ||
| 399 | return(1); | ||
| 400 | } | ||
| 401 | |||
| 402 | static int conn_read(BIO *b, char *out, int outl) | ||
| 403 | { | ||
| 404 | int ret=0; | ||
| 405 | BIO_CONNECT *data; | ||
| 406 | |||
| 407 | data=(BIO_CONNECT *)b->ptr; | ||
| 408 | if (data->state != BIO_CONN_S_OK) | ||
| 409 | { | ||
| 410 | ret=conn_state(b,data); | ||
| 411 | if (ret <= 0) | ||
| 412 | return(ret); | ||
| 413 | } | ||
| 414 | |||
| 415 | if (out != NULL) | ||
| 416 | { | ||
| 417 | clear_socket_error(); | ||
| 418 | ret=readsocket(b->num,out,outl); | ||
| 419 | BIO_clear_retry_flags(b); | ||
| 420 | if (ret <= 0) | ||
| 421 | { | ||
| 422 | if (BIO_sock_should_retry(ret)) | ||
| 423 | BIO_set_retry_read(b); | ||
| 424 | } | ||
| 425 | } | ||
| 426 | return(ret); | ||
| 427 | } | ||
| 428 | |||
| 429 | static int conn_write(BIO *b, const char *in, int inl) | ||
| 430 | { | ||
| 431 | int ret; | ||
| 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) return(ret); | ||
| 439 | } | ||
| 440 | |||
| 441 | clear_socket_error(); | ||
| 442 | ret=writesocket(b->num,in,inl); | ||
| 443 | BIO_clear_retry_flags(b); | ||
| 444 | if (ret <= 0) | ||
| 445 | { | ||
| 446 | if (BIO_sock_should_retry(ret)) | ||
| 447 | BIO_set_retry_write(b); | ||
| 448 | } | ||
| 449 | return(ret); | ||
| 450 | } | ||
| 451 | |||
| 452 | static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 453 | { | ||
| 454 | BIO *dbio; | ||
| 455 | int *ip; | ||
| 456 | const char **pptr; | ||
| 457 | long ret=1; | ||
| 458 | BIO_CONNECT *data; | ||
| 459 | |||
| 460 | data=(BIO_CONNECT *)b->ptr; | ||
| 461 | |||
| 462 | switch (cmd) | ||
| 463 | { | ||
| 464 | case BIO_CTRL_RESET: | ||
| 465 | ret=0; | ||
| 466 | data->state=BIO_CONN_S_BEFORE; | ||
| 467 | conn_close_socket(b); | ||
| 468 | b->flags=0; | ||
| 469 | break; | ||
| 470 | case BIO_C_DO_STATE_MACHINE: | ||
| 471 | /* use this one to start the connection */ | ||
| 472 | if (data->state != BIO_CONN_S_OK) | ||
| 473 | ret=(long)conn_state(b,data); | ||
| 474 | else | ||
| 475 | ret=1; | ||
| 476 | break; | ||
| 477 | case BIO_C_GET_CONNECT: | ||
| 478 | if (ptr != NULL) | ||
| 479 | { | ||
| 480 | pptr=(const char **)ptr; | ||
| 481 | if (num == 0) | ||
| 482 | { | ||
| 483 | *pptr=data->param_hostname; | ||
| 484 | |||
| 485 | } | ||
| 486 | else if (num == 1) | ||
| 487 | { | ||
| 488 | *pptr=data->param_port; | ||
| 489 | } | ||
| 490 | else if (num == 2) | ||
| 491 | { | ||
| 492 | *pptr= (char *)&(data->ip[0]); | ||
| 493 | } | ||
| 494 | else if (num == 3) | ||
| 495 | { | ||
| 496 | *((int *)ptr)=data->port; | ||
| 497 | } | ||
| 498 | if ((!b->init) || (ptr == NULL)) | ||
| 499 | *pptr="not initialized"; | ||
| 500 | ret=1; | ||
| 501 | } | ||
| 502 | break; | ||
| 503 | case BIO_C_SET_CONNECT: | ||
| 504 | if (ptr != NULL) | ||
| 505 | { | ||
| 506 | b->init=1; | ||
| 507 | if (num == 0) | ||
| 508 | { | ||
| 509 | if (data->param_hostname != NULL) | ||
| 510 | OPENSSL_free(data->param_hostname); | ||
| 511 | data->param_hostname=BUF_strdup(ptr); | ||
| 512 | } | ||
| 513 | else if (num == 1) | ||
| 514 | { | ||
| 515 | if (data->param_port != NULL) | ||
| 516 | OPENSSL_free(data->param_port); | ||
| 517 | data->param_port=BUF_strdup(ptr); | ||
| 518 | } | ||
| 519 | else if (num == 2) | ||
| 520 | { | ||
| 521 | char buf[16]; | ||
| 522 | unsigned char *p = ptr; | ||
| 523 | |||
| 524 | BIO_snprintf(buf,sizeof buf,"%d.%d.%d.%d", | ||
| 525 | p[0],p[1],p[2],p[3]); | ||
| 526 | if (data->param_hostname != NULL) | ||
| 527 | OPENSSL_free(data->param_hostname); | ||
| 528 | data->param_hostname=BUF_strdup(buf); | ||
| 529 | memcpy(&(data->ip[0]),ptr,4); | ||
| 530 | } | ||
| 531 | else if (num == 3) | ||
| 532 | { | ||
| 533 | char buf[DECIMAL_SIZE(int)+1]; | ||
| 534 | |||
| 535 | BIO_snprintf(buf,sizeof buf,"%d",*(int *)ptr); | ||
| 536 | if (data->param_port != NULL) | ||
| 537 | OPENSSL_free(data->param_port); | ||
| 538 | data->param_port=BUF_strdup(buf); | ||
| 539 | data->port= *(int *)ptr; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | break; | ||
| 543 | case BIO_C_SET_NBIO: | ||
| 544 | data->nbio=(int)num; | ||
| 545 | break; | ||
| 546 | case BIO_C_GET_FD: | ||
| 547 | if (b->init) | ||
| 548 | { | ||
| 549 | ip=(int *)ptr; | ||
| 550 | if (ip != NULL) | ||
| 551 | *ip=b->num; | ||
| 552 | ret=b->num; | ||
| 553 | } | ||
| 554 | else | ||
| 555 | ret= -1; | ||
| 556 | break; | ||
| 557 | case BIO_CTRL_GET_CLOSE: | ||
| 558 | ret=b->shutdown; | ||
| 559 | break; | ||
| 560 | case BIO_CTRL_SET_CLOSE: | ||
| 561 | b->shutdown=(int)num; | ||
| 562 | break; | ||
| 563 | case BIO_CTRL_PENDING: | ||
| 564 | case BIO_CTRL_WPENDING: | ||
| 565 | ret=0; | ||
| 566 | break; | ||
| 567 | case BIO_CTRL_FLUSH: | ||
| 568 | break; | ||
| 569 | case BIO_CTRL_DUP: | ||
| 570 | { | ||
| 571 | dbio=(BIO *)ptr; | ||
| 572 | if (data->param_port) | ||
| 573 | BIO_set_conn_port(dbio,data->param_port); | ||
| 574 | if (data->param_hostname) | ||
| 575 | BIO_set_conn_hostname(dbio,data->param_hostname); | ||
| 576 | BIO_set_nbio(dbio,data->nbio); | ||
| 577 | /* FIXME: the cast of the function seems unlikely to be a good idea */ | ||
| 578 | (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback); | ||
| 579 | } | ||
| 580 | break; | ||
| 581 | case BIO_CTRL_SET_CALLBACK: | ||
| 582 | { | ||
| 583 | #if 0 /* FIXME: Should this be used? -- Richard Levitte */ | ||
| 584 | BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | ||
| 585 | ret = -1; | ||
| 586 | #else | ||
| 587 | ret=0; | ||
| 588 | #endif | ||
| 589 | } | ||
| 590 | break; | ||
| 591 | case BIO_CTRL_GET_CALLBACK: | ||
| 592 | { | ||
| 593 | int (**fptr)(const BIO *bio,int state,int xret); | ||
| 594 | |||
| 595 | fptr=(int (**)(const BIO *bio,int state,int xret))ptr; | ||
| 596 | *fptr=data->info_callback; | ||
| 597 | } | ||
| 598 | break; | ||
| 599 | default: | ||
| 600 | ret=0; | ||
| 601 | break; | ||
| 602 | } | ||
| 603 | return(ret); | ||
| 604 | } | ||
| 605 | |||
| 606 | static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 607 | { | ||
| 608 | long ret=1; | ||
| 609 | BIO_CONNECT *data; | ||
| 610 | |||
| 611 | data=(BIO_CONNECT *)b->ptr; | ||
| 612 | |||
| 613 | switch (cmd) | ||
| 614 | { | ||
| 615 | case BIO_CTRL_SET_CALLBACK: | ||
| 616 | { | ||
| 617 | data->info_callback=(int (*)(const struct bio_st *, int, int))fp; | ||
| 618 | } | ||
| 619 | break; | ||
| 620 | default: | ||
| 621 | ret=0; | ||
| 622 | break; | ||
| 623 | } | ||
| 624 | return(ret); | ||
| 625 | } | ||
| 626 | |||
| 627 | static int conn_puts(BIO *bp, const char *str) | ||
| 628 | { | ||
| 629 | int n,ret; | ||
| 630 | |||
| 631 | n=strlen(str); | ||
| 632 | ret=conn_write(bp,str,n); | ||
| 633 | return(ret); | ||
| 634 | } | ||
| 635 | |||
| 636 | BIO *BIO_new_connect(char *str) | ||
| 637 | { | ||
| 638 | BIO *ret; | ||
| 639 | |||
| 640 | ret=BIO_new(BIO_s_connect()); | ||
| 641 | if (ret == NULL) return(NULL); | ||
| 642 | if (BIO_set_conn_hostname(ret,str)) | ||
| 643 | return(ret); | ||
| 644 | else | ||
| 645 | { | ||
| 646 | BIO_free(ret); | ||
| 647 | return(NULL); | ||
| 648 | } | ||
| 649 | } | ||
| 650 | |||
| 651 | #endif | ||
| 652 | |||
diff --git a/src/lib/libcrypto/bio/bss_dgram.c b/src/lib/libcrypto/bio/bss_dgram.c new file mode 100644 index 0000000000..c3da6dc82f --- /dev/null +++ b/src/lib/libcrypto/bio/bss_dgram.c | |||
| @@ -0,0 +1,548 @@ | |||
| 1 | /* crypto/bio/bio_dgram.c */ | ||
| 2 | /* | ||
| 3 | * DTLS implementation written by Nagendra Modadugu | ||
| 4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. | ||
| 5 | */ | ||
| 6 | /* ==================================================================== | ||
| 7 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. | ||
| 8 | * | ||
| 9 | * Redistribution and use in source and binary forms, with or without | ||
| 10 | * modification, are permitted provided that the following conditions | ||
| 11 | * are met: | ||
| 12 | * | ||
| 13 | * 1. Redistributions of source code must retain the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer. | ||
| 15 | * | ||
| 16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 17 | * notice, this list of conditions and the following disclaimer in | ||
| 18 | * the documentation and/or other materials provided with the | ||
| 19 | * distribution. | ||
| 20 | * | ||
| 21 | * 3. All advertising materials mentioning features or use of this | ||
| 22 | * software must display the following acknowledgment: | ||
| 23 | * "This product includes software developed by the OpenSSL Project | ||
| 24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 25 | * | ||
| 26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 27 | * endorse or promote products derived from this software without | ||
| 28 | * prior written permission. For written permission, please contact | ||
| 29 | * openssl-core@OpenSSL.org. | ||
| 30 | * | ||
| 31 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 32 | * nor may "OpenSSL" appear in their names without prior written | ||
| 33 | * permission of the OpenSSL Project. | ||
| 34 | * | ||
| 35 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 36 | * acknowledgment: | ||
| 37 | * "This product includes software developed by the OpenSSL Project | ||
| 38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 39 | * | ||
| 40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 52 | * ==================================================================== | ||
| 53 | * | ||
| 54 | * This product includes cryptographic software written by Eric Young | ||
| 55 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 56 | * Hudson (tjh@cryptsoft.com). | ||
| 57 | * | ||
| 58 | */ | ||
| 59 | |||
| 60 | #ifndef OPENSSL_NO_DGRAM | ||
| 61 | |||
| 62 | #include <stdio.h> | ||
| 63 | #include <errno.h> | ||
| 64 | #define USE_SOCKETS | ||
| 65 | #include "cryptlib.h" | ||
| 66 | |||
| 67 | #include <openssl/bio.h> | ||
| 68 | |||
| 69 | #define IP_MTU 14 /* linux is lame */ | ||
| 70 | |||
| 71 | #ifdef WATT32 | ||
| 72 | #define sock_write SockWrite /* Watt-32 uses same names */ | ||
| 73 | #define sock_read SockRead | ||
| 74 | #define sock_puts SockPuts | ||
| 75 | #endif | ||
| 76 | |||
| 77 | static int dgram_write(BIO *h, const char *buf, int num); | ||
| 78 | static int dgram_read(BIO *h, char *buf, int size); | ||
| 79 | static int dgram_puts(BIO *h, const char *str); | ||
| 80 | static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 81 | static int dgram_new(BIO *h); | ||
| 82 | static int dgram_free(BIO *data); | ||
| 83 | static int dgram_clear(BIO *bio); | ||
| 84 | |||
| 85 | static int BIO_dgram_should_retry(int s); | ||
| 86 | |||
| 87 | static BIO_METHOD methods_dgramp= | ||
| 88 | { | ||
| 89 | BIO_TYPE_DGRAM, | ||
| 90 | "datagram socket", | ||
| 91 | dgram_write, | ||
| 92 | dgram_read, | ||
| 93 | dgram_puts, | ||
| 94 | NULL, /* dgram_gets, */ | ||
| 95 | dgram_ctrl, | ||
| 96 | dgram_new, | ||
| 97 | dgram_free, | ||
| 98 | NULL, | ||
| 99 | }; | ||
| 100 | |||
| 101 | typedef struct bio_dgram_data_st | ||
| 102 | { | ||
| 103 | struct sockaddr peer; | ||
| 104 | unsigned int connected; | ||
| 105 | unsigned int _errno; | ||
| 106 | unsigned int mtu; | ||
| 107 | } bio_dgram_data; | ||
| 108 | |||
| 109 | BIO_METHOD *BIO_s_datagram(void) | ||
| 110 | { | ||
| 111 | return(&methods_dgramp); | ||
| 112 | } | ||
| 113 | |||
| 114 | BIO *BIO_new_dgram(int fd, int close_flag) | ||
| 115 | { | ||
| 116 | BIO *ret; | ||
| 117 | |||
| 118 | ret=BIO_new(BIO_s_datagram()); | ||
| 119 | if (ret == NULL) return(NULL); | ||
| 120 | BIO_set_fd(ret,fd,close_flag); | ||
| 121 | return(ret); | ||
| 122 | } | ||
| 123 | |||
| 124 | static int dgram_new(BIO *bi) | ||
| 125 | { | ||
| 126 | bio_dgram_data *data = NULL; | ||
| 127 | |||
| 128 | bi->init=0; | ||
| 129 | bi->num=0; | ||
| 130 | data = OPENSSL_malloc(sizeof(bio_dgram_data)); | ||
| 131 | if (data == NULL) | ||
| 132 | return 0; | ||
| 133 | memset(data, 0x00, sizeof(bio_dgram_data)); | ||
| 134 | bi->ptr = data; | ||
| 135 | |||
| 136 | bi->flags=0; | ||
| 137 | return(1); | ||
| 138 | } | ||
| 139 | |||
| 140 | static int dgram_free(BIO *a) | ||
| 141 | { | ||
| 142 | bio_dgram_data *data; | ||
| 143 | |||
| 144 | if (a == NULL) return(0); | ||
| 145 | if ( ! dgram_clear(a)) | ||
| 146 | return 0; | ||
| 147 | |||
| 148 | data = (bio_dgram_data *)a->ptr; | ||
| 149 | if(data != NULL) OPENSSL_free(data); | ||
| 150 | |||
| 151 | return(1); | ||
| 152 | } | ||
| 153 | |||
| 154 | static int dgram_clear(BIO *a) | ||
| 155 | { | ||
| 156 | if (a == NULL) return(0); | ||
| 157 | if (a->shutdown) | ||
| 158 | { | ||
| 159 | if (a->init) | ||
| 160 | { | ||
| 161 | SHUTDOWN2(a->num); | ||
| 162 | } | ||
| 163 | a->init=0; | ||
| 164 | a->flags=0; | ||
| 165 | } | ||
| 166 | return(1); | ||
| 167 | } | ||
| 168 | |||
| 169 | static int dgram_read(BIO *b, char *out, int outl) | ||
| 170 | { | ||
| 171 | int ret=0; | ||
| 172 | bio_dgram_data *data = (bio_dgram_data *)b->ptr; | ||
| 173 | |||
| 174 | struct sockaddr peer; | ||
| 175 | int peerlen = sizeof(peer); | ||
| 176 | |||
| 177 | if (out != NULL) | ||
| 178 | { | ||
| 179 | clear_socket_error(); | ||
| 180 | memset(&peer, 0x00, peerlen); | ||
| 181 | /* Last arg in recvfrom is signed on some platforms and | ||
| 182 | * unsigned on others. It is of type socklen_t on some | ||
| 183 | * but this is not universal. Cast to (void *) to avoid | ||
| 184 | * compiler warnings. | ||
| 185 | */ | ||
| 186 | ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen); | ||
| 187 | |||
| 188 | if ( ! data->connected && ret > 0) | ||
| 189 | BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer); | ||
| 190 | |||
| 191 | BIO_clear_retry_flags(b); | ||
| 192 | if (ret <= 0) | ||
| 193 | { | ||
| 194 | if (BIO_dgram_should_retry(ret)) | ||
| 195 | { | ||
| 196 | BIO_set_retry_read(b); | ||
| 197 | data->_errno = get_last_socket_error(); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | } | ||
| 201 | return(ret); | ||
| 202 | } | ||
| 203 | |||
| 204 | static int dgram_write(BIO *b, const char *in, int inl) | ||
| 205 | { | ||
| 206 | int ret; | ||
| 207 | bio_dgram_data *data = (bio_dgram_data *)b->ptr; | ||
| 208 | clear_socket_error(); | ||
| 209 | |||
| 210 | if ( data->connected ) | ||
| 211 | ret=writesocket(b->num,in,inl); | ||
| 212 | else | ||
| 213 | #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK) | ||
| 214 | ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer)); | ||
| 215 | #else | ||
| 216 | ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer)); | ||
| 217 | #endif | ||
| 218 | |||
| 219 | BIO_clear_retry_flags(b); | ||
| 220 | if (ret <= 0) | ||
| 221 | { | ||
| 222 | if (BIO_sock_should_retry(ret)) | ||
| 223 | { | ||
| 224 | BIO_set_retry_write(b); | ||
| 225 | data->_errno = get_last_socket_error(); | ||
| 226 | |||
| 227 | #if 0 /* higher layers are responsible for querying MTU, if necessary */ | ||
| 228 | if ( data->_errno == EMSGSIZE) | ||
| 229 | /* retrieve the new MTU */ | ||
| 230 | BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); | ||
| 231 | #endif | ||
| 232 | } | ||
| 233 | } | ||
| 234 | return(ret); | ||
| 235 | } | ||
| 236 | |||
| 237 | static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 238 | { | ||
| 239 | long ret=1; | ||
| 240 | int *ip; | ||
| 241 | struct sockaddr *to = NULL; | ||
| 242 | bio_dgram_data *data = NULL; | ||
| 243 | long sockopt_val = 0; | ||
| 244 | unsigned int sockopt_len = 0; | ||
| 245 | |||
| 246 | data = (bio_dgram_data *)b->ptr; | ||
| 247 | |||
| 248 | switch (cmd) | ||
| 249 | { | ||
| 250 | case BIO_CTRL_RESET: | ||
| 251 | num=0; | ||
| 252 | case BIO_C_FILE_SEEK: | ||
| 253 | ret=0; | ||
| 254 | break; | ||
| 255 | case BIO_C_FILE_TELL: | ||
| 256 | case BIO_CTRL_INFO: | ||
| 257 | ret=0; | ||
| 258 | break; | ||
| 259 | case BIO_C_SET_FD: | ||
| 260 | dgram_clear(b); | ||
| 261 | b->num= *((int *)ptr); | ||
| 262 | b->shutdown=(int)num; | ||
| 263 | b->init=1; | ||
| 264 | break; | ||
| 265 | case BIO_C_GET_FD: | ||
| 266 | if (b->init) | ||
| 267 | { | ||
| 268 | ip=(int *)ptr; | ||
| 269 | if (ip != NULL) *ip=b->num; | ||
| 270 | ret=b->num; | ||
| 271 | } | ||
| 272 | else | ||
| 273 | ret= -1; | ||
| 274 | break; | ||
| 275 | case BIO_CTRL_GET_CLOSE: | ||
| 276 | ret=b->shutdown; | ||
| 277 | break; | ||
| 278 | case BIO_CTRL_SET_CLOSE: | ||
| 279 | b->shutdown=(int)num; | ||
| 280 | break; | ||
| 281 | case BIO_CTRL_PENDING: | ||
| 282 | case BIO_CTRL_WPENDING: | ||
| 283 | ret=0; | ||
| 284 | break; | ||
| 285 | case BIO_CTRL_DUP: | ||
| 286 | case BIO_CTRL_FLUSH: | ||
| 287 | ret=1; | ||
| 288 | break; | ||
| 289 | case BIO_CTRL_DGRAM_CONNECT: | ||
| 290 | to = (struct sockaddr *)ptr; | ||
| 291 | #if 0 | ||
| 292 | if (connect(b->num, to, sizeof(struct sockaddr)) < 0) | ||
| 293 | { perror("connect"); ret = 0; } | ||
| 294 | else | ||
| 295 | { | ||
| 296 | #endif | ||
| 297 | memcpy(&(data->peer),to, sizeof(struct sockaddr)); | ||
| 298 | #if 0 | ||
| 299 | } | ||
| 300 | #endif | ||
| 301 | break; | ||
| 302 | /* (Linux)kernel sets DF bit on outgoing IP packets */ | ||
| 303 | #ifdef IP_MTU_DISCOVER | ||
| 304 | case BIO_CTRL_DGRAM_MTU_DISCOVER: | ||
| 305 | sockopt_val = IP_PMTUDISC_DO; | ||
| 306 | if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER, | ||
| 307 | &sockopt_val, sizeof(sockopt_val))) < 0) | ||
| 308 | perror("setsockopt"); | ||
| 309 | break; | ||
| 310 | #endif | ||
| 311 | case BIO_CTRL_DGRAM_QUERY_MTU: | ||
| 312 | sockopt_len = sizeof(sockopt_val); | ||
| 313 | if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val, | ||
| 314 | &sockopt_len)) < 0 || sockopt_val < 0) | ||
| 315 | { ret = 0; } | ||
| 316 | else | ||
| 317 | { | ||
| 318 | data->mtu = sockopt_val; | ||
| 319 | ret = data->mtu; | ||
| 320 | } | ||
| 321 | break; | ||
| 322 | case BIO_CTRL_DGRAM_GET_MTU: | ||
| 323 | return data->mtu; | ||
| 324 | break; | ||
| 325 | case BIO_CTRL_DGRAM_SET_MTU: | ||
| 326 | data->mtu = num; | ||
| 327 | ret = num; | ||
| 328 | break; | ||
| 329 | case BIO_CTRL_DGRAM_SET_CONNECTED: | ||
| 330 | to = (struct sockaddr *)ptr; | ||
| 331 | |||
| 332 | if ( to != NULL) | ||
| 333 | { | ||
| 334 | data->connected = 1; | ||
| 335 | memcpy(&(data->peer),to, sizeof(struct sockaddr)); | ||
| 336 | } | ||
| 337 | else | ||
| 338 | { | ||
| 339 | data->connected = 0; | ||
| 340 | memset(&(data->peer), 0x00, sizeof(struct sockaddr)); | ||
| 341 | } | ||
| 342 | break; | ||
| 343 | case BIO_CTRL_DGRAM_SET_PEER: | ||
| 344 | to = (struct sockaddr *) ptr; | ||
| 345 | |||
| 346 | memcpy(&(data->peer), to, sizeof(struct sockaddr)); | ||
| 347 | break; | ||
| 348 | #if defined(SO_RCVTIMEO) | ||
| 349 | case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT: | ||
| 350 | #ifdef OPENSSL_SYS_WINDOWS | ||
| 351 | { | ||
| 352 | struct timeval *tv = (struct timeval *)ptr; | ||
| 353 | int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000; | ||
| 354 | if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, | ||
| 355 | (void*)&timeout, sizeof(timeout)) < 0) | ||
| 356 | { perror("setsockopt"); ret = -1; } | ||
| 357 | } | ||
| 358 | #else | ||
| 359 | if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr, | ||
| 360 | sizeof(struct timeval)) < 0) | ||
| 361 | { perror("setsockopt"); ret = -1; } | ||
| 362 | #endif | ||
| 363 | break; | ||
| 364 | case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT: | ||
| 365 | #ifdef OPENSSL_SYS_WINDOWS | ||
| 366 | { | ||
| 367 | int timeout, sz = sizeof(timeout); | ||
| 368 | struct timeval *tv = (struct timeval *)ptr; | ||
| 369 | if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, | ||
| 370 | (void*)&timeout, &sz) < 0) | ||
| 371 | { perror("getsockopt"); ret = -1; } | ||
| 372 | else | ||
| 373 | { | ||
| 374 | tv->tv_sec = timeout / 1000; | ||
| 375 | tv->tv_usec = (timeout % 1000) * 1000; | ||
| 376 | ret = sizeof(*tv); | ||
| 377 | } | ||
| 378 | } | ||
| 379 | #else | ||
| 380 | if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, | ||
| 381 | ptr, (void *)&ret) < 0) | ||
| 382 | { perror("getsockopt"); ret = -1; } | ||
| 383 | #endif | ||
| 384 | break; | ||
| 385 | #endif | ||
| 386 | #if defined(SO_SNDTIMEO) | ||
| 387 | case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT: | ||
| 388 | #ifdef OPENSSL_SYS_WINDOWS | ||
| 389 | { | ||
| 390 | struct timeval *tv = (struct timeval *)ptr; | ||
| 391 | int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000; | ||
| 392 | if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, | ||
| 393 | (void*)&timeout, sizeof(timeout)) < 0) | ||
| 394 | { perror("setsockopt"); ret = -1; } | ||
| 395 | } | ||
| 396 | #else | ||
| 397 | if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr, | ||
| 398 | sizeof(struct timeval)) < 0) | ||
| 399 | { perror("setsockopt"); ret = -1; } | ||
| 400 | #endif | ||
| 401 | break; | ||
| 402 | case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT: | ||
| 403 | #ifdef OPENSSL_SYS_WINDOWS | ||
| 404 | { | ||
| 405 | int timeout, sz = sizeof(timeout); | ||
| 406 | struct timeval *tv = (struct timeval *)ptr; | ||
| 407 | if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, | ||
| 408 | (void*)&timeout, &sz) < 0) | ||
| 409 | { perror("getsockopt"); ret = -1; } | ||
| 410 | else | ||
| 411 | { | ||
| 412 | tv->tv_sec = timeout / 1000; | ||
| 413 | tv->tv_usec = (timeout % 1000) * 1000; | ||
| 414 | ret = sizeof(*tv); | ||
| 415 | } | ||
| 416 | } | ||
| 417 | #else | ||
| 418 | if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, | ||
| 419 | ptr, (void *)&ret) < 0) | ||
| 420 | { perror("getsockopt"); ret = -1; } | ||
| 421 | #endif | ||
| 422 | break; | ||
| 423 | #endif | ||
| 424 | case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP: | ||
| 425 | /* fall-through */ | ||
| 426 | case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP: | ||
| 427 | #ifdef OPENSSL_SYS_WINDOWS | ||
| 428 | if ( data->_errno == WSAETIMEDOUT) | ||
| 429 | #else | ||
| 430 | if ( data->_errno == EAGAIN) | ||
| 431 | #endif | ||
| 432 | { | ||
| 433 | ret = 1; | ||
| 434 | data->_errno = 0; | ||
| 435 | } | ||
| 436 | else | ||
| 437 | ret = 0; | ||
| 438 | break; | ||
| 439 | #ifdef EMSGSIZE | ||
| 440 | case BIO_CTRL_DGRAM_MTU_EXCEEDED: | ||
| 441 | if ( data->_errno == EMSGSIZE) | ||
| 442 | { | ||
| 443 | ret = 1; | ||
| 444 | data->_errno = 0; | ||
| 445 | } | ||
| 446 | else | ||
| 447 | ret = 0; | ||
| 448 | break; | ||
| 449 | #endif | ||
| 450 | default: | ||
| 451 | ret=0; | ||
| 452 | break; | ||
| 453 | } | ||
| 454 | return(ret); | ||
| 455 | } | ||
| 456 | |||
| 457 | static int dgram_puts(BIO *bp, const char *str) | ||
| 458 | { | ||
| 459 | int n,ret; | ||
| 460 | |||
| 461 | n=strlen(str); | ||
| 462 | ret=dgram_write(bp,str,n); | ||
| 463 | return(ret); | ||
| 464 | } | ||
| 465 | |||
| 466 | static int BIO_dgram_should_retry(int i) | ||
| 467 | { | ||
| 468 | int err; | ||
| 469 | |||
| 470 | if ((i == 0) || (i == -1)) | ||
| 471 | { | ||
| 472 | err=get_last_socket_error(); | ||
| 473 | |||
| 474 | #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */ | ||
| 475 | if ((i == -1) && (err == 0)) | ||
| 476 | return(1); | ||
| 477 | #endif | ||
| 478 | |||
| 479 | return(BIO_dgram_non_fatal_error(err)); | ||
| 480 | } | ||
| 481 | return(0); | ||
| 482 | } | ||
| 483 | |||
| 484 | int BIO_dgram_non_fatal_error(int err) | ||
| 485 | { | ||
| 486 | switch (err) | ||
| 487 | { | ||
| 488 | #if defined(OPENSSL_SYS_WINDOWS) | ||
| 489 | # if defined(WSAEWOULDBLOCK) | ||
| 490 | case WSAEWOULDBLOCK: | ||
| 491 | # endif | ||
| 492 | |||
| 493 | # if 0 /* This appears to always be an error */ | ||
| 494 | # if defined(WSAENOTCONN) | ||
| 495 | case WSAENOTCONN: | ||
| 496 | # endif | ||
| 497 | # endif | ||
| 498 | #endif | ||
| 499 | |||
| 500 | #ifdef EWOULDBLOCK | ||
| 501 | # ifdef WSAEWOULDBLOCK | ||
| 502 | # if WSAEWOULDBLOCK != EWOULDBLOCK | ||
| 503 | case EWOULDBLOCK: | ||
| 504 | # endif | ||
| 505 | # else | ||
| 506 | case EWOULDBLOCK: | ||
| 507 | # endif | ||
| 508 | #endif | ||
| 509 | |||
| 510 | #if defined(ENOTCONN) | ||
| 511 | case ENOTCONN: | ||
| 512 | #endif | ||
| 513 | |||
| 514 | #ifdef EINTR | ||
| 515 | case EINTR: | ||
| 516 | #endif | ||
| 517 | |||
| 518 | #ifdef EAGAIN | ||
| 519 | #if EWOULDBLOCK != EAGAIN | ||
| 520 | case EAGAIN: | ||
| 521 | # endif | ||
| 522 | #endif | ||
| 523 | |||
| 524 | #ifdef EPROTO | ||
| 525 | case EPROTO: | ||
| 526 | #endif | ||
| 527 | |||
| 528 | #ifdef EINPROGRESS | ||
| 529 | case EINPROGRESS: | ||
| 530 | #endif | ||
| 531 | |||
| 532 | #ifdef EALREADY | ||
| 533 | case EALREADY: | ||
| 534 | #endif | ||
| 535 | |||
| 536 | /* DF bit set, and packet larger than MTU */ | ||
| 537 | #ifdef EMSGSIZE | ||
| 538 | case EMSGSIZE: | ||
| 539 | #endif | ||
| 540 | |||
| 541 | return(1); | ||
| 542 | /* break; */ | ||
| 543 | default: | ||
| 544 | break; | ||
| 545 | } | ||
| 546 | return(0); | ||
| 547 | } | ||
| 548 | #endif | ||
diff --git a/src/lib/libcrypto/bio/bss_fd.c b/src/lib/libcrypto/bio/bss_fd.c new file mode 100644 index 0000000000..4c229bf641 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_fd.c | |||
| @@ -0,0 +1,294 @@ | |||
| 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 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #define USE_SOCKETS | ||
| 62 | #include "cryptlib.h" | ||
| 63 | /* | ||
| 64 | * As for unconditional usage of "UPLINK" interface in this module. | ||
| 65 | * Trouble is that unlike Unix file descriptors [which are indexes | ||
| 66 | * in kernel-side per-process table], corresponding descriptors on | ||
| 67 | * platforms which require "UPLINK" interface seem to be indexes | ||
| 68 | * in a user-land, non-global table. Well, in fact they are indexes | ||
| 69 | * in stdio _iob[], and recall that _iob[] was the very reason why | ||
| 70 | * "UPLINK" interface was introduced in first place. But one way on | ||
| 71 | * another. Neither libcrypto or libssl use this BIO meaning that | ||
| 72 | * file descriptors can only be provided by application. Therefore | ||
| 73 | * "UPLINK" calls are due... | ||
| 74 | */ | ||
| 75 | #include "bio_lcl.h" | ||
| 76 | |||
| 77 | static int fd_write(BIO *h, const char *buf, int num); | ||
| 78 | static int fd_read(BIO *h, char *buf, int size); | ||
| 79 | static int fd_puts(BIO *h, const char *str); | ||
| 80 | static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 81 | static int fd_new(BIO *h); | ||
| 82 | static int fd_free(BIO *data); | ||
| 83 | int BIO_fd_should_retry(int s); | ||
| 84 | |||
| 85 | static BIO_METHOD methods_fdp= | ||
| 86 | { | ||
| 87 | BIO_TYPE_FD,"file descriptor", | ||
| 88 | fd_write, | ||
| 89 | fd_read, | ||
| 90 | fd_puts, | ||
| 91 | NULL, /* fd_gets, */ | ||
| 92 | fd_ctrl, | ||
| 93 | fd_new, | ||
| 94 | fd_free, | ||
| 95 | NULL, | ||
| 96 | }; | ||
| 97 | |||
| 98 | BIO_METHOD *BIO_s_fd(void) | ||
| 99 | { | ||
| 100 | return(&methods_fdp); | ||
| 101 | } | ||
| 102 | |||
| 103 | BIO *BIO_new_fd(int fd,int close_flag) | ||
| 104 | { | ||
| 105 | BIO *ret; | ||
| 106 | ret=BIO_new(BIO_s_fd()); | ||
| 107 | if (ret == NULL) return(NULL); | ||
| 108 | BIO_set_fd(ret,fd,close_flag); | ||
| 109 | return(ret); | ||
| 110 | } | ||
| 111 | |||
| 112 | static int fd_new(BIO *bi) | ||
| 113 | { | ||
| 114 | bi->init=0; | ||
| 115 | bi->num=-1; | ||
| 116 | bi->ptr=NULL; | ||
| 117 | bi->flags=BIO_FLAGS_UPLINK; /* essentially redundant */ | ||
| 118 | return(1); | ||
| 119 | } | ||
| 120 | |||
| 121 | static int fd_free(BIO *a) | ||
| 122 | { | ||
| 123 | if (a == NULL) return(0); | ||
| 124 | if (a->shutdown) | ||
| 125 | { | ||
| 126 | if (a->init) | ||
| 127 | { | ||
| 128 | UP_close(a->num); | ||
| 129 | } | ||
| 130 | a->init=0; | ||
| 131 | a->flags=BIO_FLAGS_UPLINK; | ||
| 132 | } | ||
| 133 | return(1); | ||
| 134 | } | ||
| 135 | |||
| 136 | static int fd_read(BIO *b, char *out,int outl) | ||
| 137 | { | ||
| 138 | int ret=0; | ||
| 139 | |||
| 140 | if (out != NULL) | ||
| 141 | { | ||
| 142 | clear_sys_error(); | ||
| 143 | ret=UP_read(b->num,out,outl); | ||
| 144 | BIO_clear_retry_flags(b); | ||
| 145 | if (ret <= 0) | ||
| 146 | { | ||
| 147 | if (BIO_fd_should_retry(ret)) | ||
| 148 | BIO_set_retry_read(b); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | return(ret); | ||
| 152 | } | ||
| 153 | |||
| 154 | static int fd_write(BIO *b, const char *in, int inl) | ||
| 155 | { | ||
| 156 | int ret; | ||
| 157 | clear_sys_error(); | ||
| 158 | ret=UP_write(b->num,in,inl); | ||
| 159 | BIO_clear_retry_flags(b); | ||
| 160 | if (ret <= 0) | ||
| 161 | { | ||
| 162 | if (BIO_fd_should_retry(ret)) | ||
| 163 | BIO_set_retry_write(b); | ||
| 164 | } | ||
| 165 | return(ret); | ||
| 166 | } | ||
| 167 | |||
| 168 | static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 169 | { | ||
| 170 | long ret=1; | ||
| 171 | int *ip; | ||
| 172 | |||
| 173 | switch (cmd) | ||
| 174 | { | ||
| 175 | case BIO_CTRL_RESET: | ||
| 176 | num=0; | ||
| 177 | case BIO_C_FILE_SEEK: | ||
| 178 | ret=(long)UP_lseek(b->num,num,0); | ||
| 179 | break; | ||
| 180 | case BIO_C_FILE_TELL: | ||
| 181 | case BIO_CTRL_INFO: | ||
| 182 | ret=(long)UP_lseek(b->num,0,1); | ||
| 183 | break; | ||
| 184 | case BIO_C_SET_FD: | ||
| 185 | fd_free(b); | ||
| 186 | b->num= *((int *)ptr); | ||
| 187 | b->shutdown=(int)num; | ||
| 188 | b->init=1; | ||
| 189 | break; | ||
| 190 | case BIO_C_GET_FD: | ||
| 191 | if (b->init) | ||
| 192 | { | ||
| 193 | ip=(int *)ptr; | ||
| 194 | if (ip != NULL) *ip=b->num; | ||
| 195 | ret=b->num; | ||
| 196 | } | ||
| 197 | else | ||
| 198 | ret= -1; | ||
| 199 | break; | ||
| 200 | case BIO_CTRL_GET_CLOSE: | ||
| 201 | ret=b->shutdown; | ||
| 202 | break; | ||
| 203 | case BIO_CTRL_SET_CLOSE: | ||
| 204 | b->shutdown=(int)num; | ||
| 205 | break; | ||
| 206 | case BIO_CTRL_PENDING: | ||
| 207 | case BIO_CTRL_WPENDING: | ||
| 208 | ret=0; | ||
| 209 | break; | ||
| 210 | case BIO_CTRL_DUP: | ||
| 211 | case BIO_CTRL_FLUSH: | ||
| 212 | ret=1; | ||
| 213 | break; | ||
| 214 | default: | ||
| 215 | ret=0; | ||
| 216 | break; | ||
| 217 | } | ||
| 218 | return(ret); | ||
| 219 | } | ||
| 220 | |||
| 221 | static int fd_puts(BIO *bp, const char *str) | ||
| 222 | { | ||
| 223 | int n,ret; | ||
| 224 | |||
| 225 | n=strlen(str); | ||
| 226 | ret=fd_write(bp,str,n); | ||
| 227 | return(ret); | ||
| 228 | } | ||
| 229 | |||
| 230 | int BIO_fd_should_retry(int i) | ||
| 231 | { | ||
| 232 | int err; | ||
| 233 | |||
| 234 | if ((i == 0) || (i == -1)) | ||
| 235 | { | ||
| 236 | err=get_last_sys_error(); | ||
| 237 | |||
| 238 | #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */ | ||
| 239 | if ((i == -1) && (err == 0)) | ||
| 240 | return(1); | ||
| 241 | #endif | ||
| 242 | |||
| 243 | return(BIO_fd_non_fatal_error(err)); | ||
| 244 | } | ||
| 245 | return(0); | ||
| 246 | } | ||
| 247 | |||
| 248 | int BIO_fd_non_fatal_error(int err) | ||
| 249 | { | ||
| 250 | switch (err) | ||
| 251 | { | ||
| 252 | |||
| 253 | #ifdef EWOULDBLOCK | ||
| 254 | # ifdef WSAEWOULDBLOCK | ||
| 255 | # if WSAEWOULDBLOCK != EWOULDBLOCK | ||
| 256 | case EWOULDBLOCK: | ||
| 257 | # endif | ||
| 258 | # else | ||
| 259 | case EWOULDBLOCK: | ||
| 260 | # endif | ||
| 261 | #endif | ||
| 262 | |||
| 263 | #if defined(ENOTCONN) | ||
| 264 | case ENOTCONN: | ||
| 265 | #endif | ||
| 266 | |||
| 267 | #ifdef EINTR | ||
| 268 | case EINTR: | ||
| 269 | #endif | ||
| 270 | |||
| 271 | #ifdef EAGAIN | ||
| 272 | #if EWOULDBLOCK != EAGAIN | ||
| 273 | case EAGAIN: | ||
| 274 | # endif | ||
| 275 | #endif | ||
| 276 | |||
| 277 | #ifdef EPROTO | ||
| 278 | case EPROTO: | ||
| 279 | #endif | ||
| 280 | |||
| 281 | #ifdef EINPROGRESS | ||
| 282 | case EINPROGRESS: | ||
| 283 | #endif | ||
| 284 | |||
| 285 | #ifdef EALREADY | ||
| 286 | case EALREADY: | ||
| 287 | #endif | ||
| 288 | return(1); | ||
| 289 | /* break; */ | ||
| 290 | default: | ||
| 291 | break; | ||
| 292 | } | ||
| 293 | return(0); | ||
| 294 | } | ||
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c new file mode 100644 index 0000000000..0c8c8115fa --- /dev/null +++ b/src/lib/libcrypto/bio/bss_file.c | |||
| @@ -0,0 +1,428 @@ | |||
| 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 | #if defined(__linux) || defined(__sun) || defined(__hpux) | ||
| 69 | /* Following definition aliases fopen to fopen64 on above mentioned | ||
| 70 | * platforms. This makes it possible to open and sequentially access | ||
| 71 | * files larger than 2GB from 32-bit application. It does not allow to | ||
| 72 | * traverse them beyond 2GB with fseek/ftell, but on the other hand *no* | ||
| 73 | * 32-bit platform permits that, not with fseek/ftell. Not to mention | ||
| 74 | * that breaking 2GB limit for seeking would require surgery to *our* | ||
| 75 | * API. But sequential access suffices for practical cases when you | ||
| 76 | * can run into large files, such as fingerprinting, so we can let API | ||
| 77 | * alone. For reference, the list of 32-bit platforms which allow for | ||
| 78 | * sequential access of large files without extra "magic" comprise *BSD, | ||
| 79 | * Darwin, IRIX... | ||
| 80 | */ | ||
| 81 | #ifndef _FILE_OFFSET_BITS | ||
| 82 | #define _FILE_OFFSET_BITS 64 | ||
| 83 | #endif | ||
| 84 | #endif | ||
| 85 | |||
| 86 | #include <stdio.h> | ||
| 87 | #include <errno.h> | ||
| 88 | #include "cryptlib.h" | ||
| 89 | #include "bio_lcl.h" | ||
| 90 | #include <openssl/err.h> | ||
| 91 | |||
| 92 | #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB) | ||
| 93 | #include <nwfileio.h> | ||
| 94 | #endif | ||
| 95 | |||
| 96 | #if !defined(OPENSSL_NO_STDIO) | ||
| 97 | |||
| 98 | static int MS_CALLBACK file_write(BIO *h, const char *buf, int num); | ||
| 99 | static int MS_CALLBACK file_read(BIO *h, char *buf, int size); | ||
| 100 | static int MS_CALLBACK file_puts(BIO *h, const char *str); | ||
| 101 | static int MS_CALLBACK file_gets(BIO *h, char *str, int size); | ||
| 102 | static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 103 | static int MS_CALLBACK file_new(BIO *h); | ||
| 104 | static int MS_CALLBACK file_free(BIO *data); | ||
| 105 | static BIO_METHOD methods_filep= | ||
| 106 | { | ||
| 107 | BIO_TYPE_FILE, | ||
| 108 | "FILE pointer", | ||
| 109 | file_write, | ||
| 110 | file_read, | ||
| 111 | file_puts, | ||
| 112 | file_gets, | ||
| 113 | file_ctrl, | ||
| 114 | file_new, | ||
| 115 | file_free, | ||
| 116 | NULL, | ||
| 117 | }; | ||
| 118 | |||
| 119 | BIO *BIO_new_file(const char *filename, const char *mode) | ||
| 120 | { | ||
| 121 | BIO *ret; | ||
| 122 | FILE *file; | ||
| 123 | |||
| 124 | if ((file=fopen(filename,mode)) == NULL) | ||
| 125 | { | ||
| 126 | SYSerr(SYS_F_FOPEN,get_last_sys_error()); | ||
| 127 | ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); | ||
| 128 | if (errno == ENOENT) | ||
| 129 | BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE); | ||
| 130 | else | ||
| 131 | BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); | ||
| 132 | return(NULL); | ||
| 133 | } | ||
| 134 | if ((ret=BIO_new(BIO_s_file_internal())) == NULL) | ||
| 135 | { | ||
| 136 | fclose(file); | ||
| 137 | return(NULL); | ||
| 138 | } | ||
| 139 | |||
| 140 | BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */ | ||
| 141 | BIO_set_fp(ret,file,BIO_CLOSE); | ||
| 142 | return(ret); | ||
| 143 | } | ||
| 144 | |||
| 145 | BIO *BIO_new_fp(FILE *stream, int close_flag) | ||
| 146 | { | ||
| 147 | BIO *ret; | ||
| 148 | |||
| 149 | if ((ret=BIO_new(BIO_s_file())) == NULL) | ||
| 150 | return(NULL); | ||
| 151 | |||
| 152 | BIO_set_flags(ret,BIO_FLAGS_UPLINK); /* redundant, left for documentation puposes */ | ||
| 153 | BIO_set_fp(ret,stream,close_flag); | ||
| 154 | return(ret); | ||
| 155 | } | ||
| 156 | |||
| 157 | BIO_METHOD *BIO_s_file(void) | ||
| 158 | { | ||
| 159 | return(&methods_filep); | ||
| 160 | } | ||
| 161 | |||
| 162 | static int MS_CALLBACK file_new(BIO *bi) | ||
| 163 | { | ||
| 164 | bi->init=0; | ||
| 165 | bi->num=0; | ||
| 166 | bi->ptr=NULL; | ||
| 167 | bi->flags=BIO_FLAGS_UPLINK; /* default to UPLINK */ | ||
| 168 | return(1); | ||
| 169 | } | ||
| 170 | |||
| 171 | static int MS_CALLBACK file_free(BIO *a) | ||
| 172 | { | ||
| 173 | if (a == NULL) return(0); | ||
| 174 | if (a->shutdown) | ||
| 175 | { | ||
| 176 | if ((a->init) && (a->ptr != NULL)) | ||
| 177 | { | ||
| 178 | if (a->flags&BIO_FLAGS_UPLINK) | ||
| 179 | UP_fclose (a->ptr); | ||
| 180 | else | ||
| 181 | fclose (a->ptr); | ||
| 182 | a->ptr=NULL; | ||
| 183 | a->flags=BIO_FLAGS_UPLINK; | ||
| 184 | } | ||
| 185 | a->init=0; | ||
| 186 | } | ||
| 187 | return(1); | ||
| 188 | } | ||
| 189 | |||
| 190 | static int MS_CALLBACK file_read(BIO *b, char *out, int outl) | ||
| 191 | { | ||
| 192 | int ret=0; | ||
| 193 | |||
| 194 | if (b->init && (out != NULL)) | ||
| 195 | { | ||
| 196 | if (b->flags&BIO_FLAGS_UPLINK) | ||
| 197 | ret=UP_fread(out,1,(int)outl,b->ptr); | ||
| 198 | else | ||
| 199 | ret=fread(out,1,(int)outl,(FILE *)b->ptr); | ||
| 200 | if(ret == 0 && (b->flags&BIO_FLAGS_UPLINK)?UP_ferror((FILE *)b->ptr):ferror((FILE *)b->ptr)) | ||
| 201 | { | ||
| 202 | SYSerr(SYS_F_FREAD,get_last_sys_error()); | ||
| 203 | BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB); | ||
| 204 | ret=-1; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | return(ret); | ||
| 208 | } | ||
| 209 | |||
| 210 | static int MS_CALLBACK file_write(BIO *b, const char *in, int inl) | ||
| 211 | { | ||
| 212 | int ret=0; | ||
| 213 | |||
| 214 | if (b->init && (in != NULL)) | ||
| 215 | { | ||
| 216 | if (b->flags&BIO_FLAGS_UPLINK) | ||
| 217 | ret=UP_fwrite(in,(int)inl,1,b->ptr); | ||
| 218 | else | ||
| 219 | ret=fwrite(in,(int)inl,1,(FILE *)b->ptr); | ||
| 220 | if (ret) | ||
| 221 | ret=inl; | ||
| 222 | /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ | ||
| 223 | /* according to Tim Hudson <tjh@cryptsoft.com>, the commented | ||
| 224 | * out version above can cause 'inl' write calls under | ||
| 225 | * some stupid stdio implementations (VMS) */ | ||
| 226 | } | ||
| 227 | return(ret); | ||
| 228 | } | ||
| 229 | |||
| 230 | static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 231 | { | ||
| 232 | long ret=1; | ||
| 233 | FILE *fp=(FILE *)b->ptr; | ||
| 234 | FILE **fpp; | ||
| 235 | char p[4]; | ||
| 236 | |||
| 237 | switch (cmd) | ||
| 238 | { | ||
| 239 | case BIO_C_FILE_SEEK: | ||
| 240 | case BIO_CTRL_RESET: | ||
| 241 | if (b->flags&BIO_FLAGS_UPLINK) | ||
| 242 | ret=(long)UP_fseek(b->ptr,num,0); | ||
| 243 | else | ||
| 244 | ret=(long)fseek(fp,num,SEEK_SET); | ||
| 245 | break; | ||
| 246 | case BIO_CTRL_EOF: | ||
| 247 | if (b->flags&BIO_FLAGS_UPLINK) | ||
| 248 | ret=(long)UP_feof(fp); | ||
| 249 | else | ||
| 250 | ret=(long)feof(fp); | ||
| 251 | break; | ||
| 252 | case BIO_C_FILE_TELL: | ||
| 253 | case BIO_CTRL_INFO: | ||
| 254 | if (b->flags&BIO_FLAGS_UPLINK) | ||
| 255 | ret=UP_ftell(b->ptr); | ||
| 256 | else | ||
| 257 | ret=ftell(fp); | ||
| 258 | break; | ||
| 259 | case BIO_C_SET_FILE_PTR: | ||
| 260 | file_free(b); | ||
| 261 | b->shutdown=(int)num&BIO_CLOSE; | ||
| 262 | b->ptr=ptr; | ||
| 263 | b->init=1; | ||
| 264 | #if BIO_FLAGS_UPLINK!=0 | ||
| 265 | #if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES) | ||
| 266 | #define _IOB_ENTRIES 20 | ||
| 267 | #endif | ||
| 268 | #if defined(_IOB_ENTRIES) | ||
| 269 | /* Safety net to catch purely internal BIO_set_fp calls */ | ||
| 270 | if ((size_t)ptr >= (size_t)stdin && | ||
| 271 | (size_t)ptr < (size_t)(stdin+_IOB_ENTRIES)) | ||
| 272 | BIO_clear_flags(b,BIO_FLAGS_UPLINK); | ||
| 273 | #endif | ||
| 274 | #endif | ||
| 275 | #ifdef UP_fsetmode | ||
| 276 | if (b->flags&BIO_FLAGS_UPLINK) | ||
| 277 | UP_fsetmode(b->ptr,num&BIO_FP_TEXT?'t':'b'); | ||
| 278 | else | ||
| 279 | #endif | ||
| 280 | { | ||
| 281 | #if defined(OPENSSL_SYS_WINDOWS) | ||
| 282 | int fd = fileno((FILE*)ptr); | ||
| 283 | if (num & BIO_FP_TEXT) | ||
| 284 | _setmode(fd,_O_TEXT); | ||
| 285 | else | ||
| 286 | _setmode(fd,_O_BINARY); | ||
| 287 | #elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB) | ||
| 288 | int fd = fileno((FILE*)ptr); | ||
| 289 | /* Under CLib there are differences in file modes | ||
| 290 | */ | ||
| 291 | if (num & BIO_FP_TEXT) | ||
| 292 | setmode(fd,O_TEXT); | ||
| 293 | else | ||
| 294 | setmode(fd,O_BINARY); | ||
| 295 | #elif defined(OPENSSL_SYS_MSDOS) | ||
| 296 | int fd = fileno((FILE*)ptr); | ||
| 297 | /* Set correct text/binary mode */ | ||
| 298 | if (num & BIO_FP_TEXT) | ||
| 299 | _setmode(fd,_O_TEXT); | ||
| 300 | /* Dangerous to set stdin/stdout to raw (unless redirected) */ | ||
| 301 | else | ||
| 302 | { | ||
| 303 | if (fd == STDIN_FILENO || fd == STDOUT_FILENO) | ||
| 304 | { | ||
| 305 | if (isatty(fd) <= 0) | ||
| 306 | _setmode(fd,_O_BINARY); | ||
| 307 | } | ||
| 308 | else | ||
| 309 | _setmode(fd,_O_BINARY); | ||
| 310 | } | ||
| 311 | #elif defined(OPENSSL_SYS_OS2) | ||
| 312 | int fd = fileno((FILE*)ptr); | ||
| 313 | if (num & BIO_FP_TEXT) | ||
| 314 | setmode(fd, O_TEXT); | ||
| 315 | else | ||
| 316 | setmode(fd, O_BINARY); | ||
| 317 | #endif | ||
| 318 | } | ||
| 319 | break; | ||
| 320 | case BIO_C_SET_FILENAME: | ||
| 321 | file_free(b); | ||
| 322 | b->shutdown=(int)num&BIO_CLOSE; | ||
| 323 | if (num & BIO_FP_APPEND) | ||
| 324 | { | ||
| 325 | if (num & BIO_FP_READ) | ||
| 326 | BUF_strlcpy(p,"a+",sizeof p); | ||
| 327 | else BUF_strlcpy(p,"a",sizeof p); | ||
| 328 | } | ||
| 329 | else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) | ||
| 330 | BUF_strlcpy(p,"r+",sizeof p); | ||
| 331 | else if (num & BIO_FP_WRITE) | ||
| 332 | BUF_strlcpy(p,"w",sizeof p); | ||
| 333 | else if (num & BIO_FP_READ) | ||
| 334 | BUF_strlcpy(p,"r",sizeof p); | ||
| 335 | else | ||
| 336 | { | ||
| 337 | BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE); | ||
| 338 | ret=0; | ||
| 339 | break; | ||
| 340 | } | ||
| 341 | #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN) | ||
| 342 | if (!(num & BIO_FP_TEXT)) | ||
| 343 | strcat(p,"b"); | ||
| 344 | else | ||
| 345 | strcat(p,"t"); | ||
| 346 | #endif | ||
| 347 | #if defined(OPENSSL_SYS_NETWARE) | ||
| 348 | if (!(num & BIO_FP_TEXT)) | ||
| 349 | strcat(p,"b"); | ||
| 350 | else | ||
| 351 | strcat(p,"t"); | ||
| 352 | #endif | ||
| 353 | fp=fopen(ptr,p); | ||
| 354 | if (fp == NULL) | ||
| 355 | { | ||
| 356 | SYSerr(SYS_F_FOPEN,get_last_sys_error()); | ||
| 357 | ERR_add_error_data(5,"fopen('",ptr,"','",p,"')"); | ||
| 358 | BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB); | ||
| 359 | ret=0; | ||
| 360 | break; | ||
| 361 | } | ||
| 362 | b->ptr=fp; | ||
| 363 | b->init=1; | ||
| 364 | BIO_clear_flags(b,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */ | ||
| 365 | break; | ||
| 366 | case BIO_C_GET_FILE_PTR: | ||
| 367 | /* the ptr parameter is actually a FILE ** in this case. */ | ||
| 368 | if (ptr != NULL) | ||
| 369 | { | ||
| 370 | fpp=(FILE **)ptr; | ||
| 371 | *fpp=(FILE *)b->ptr; | ||
| 372 | } | ||
| 373 | break; | ||
| 374 | case BIO_CTRL_GET_CLOSE: | ||
| 375 | ret=(long)b->shutdown; | ||
| 376 | break; | ||
| 377 | case BIO_CTRL_SET_CLOSE: | ||
| 378 | b->shutdown=(int)num; | ||
| 379 | break; | ||
| 380 | case BIO_CTRL_FLUSH: | ||
| 381 | if (b->flags&BIO_FLAGS_UPLINK) | ||
| 382 | UP_fflush(b->ptr); | ||
| 383 | else | ||
| 384 | fflush((FILE *)b->ptr); | ||
| 385 | break; | ||
| 386 | case BIO_CTRL_DUP: | ||
| 387 | ret=1; | ||
| 388 | break; | ||
| 389 | |||
| 390 | case BIO_CTRL_WPENDING: | ||
| 391 | case BIO_CTRL_PENDING: | ||
| 392 | case BIO_CTRL_PUSH: | ||
| 393 | case BIO_CTRL_POP: | ||
| 394 | default: | ||
| 395 | ret=0; | ||
| 396 | break; | ||
| 397 | } | ||
| 398 | return(ret); | ||
| 399 | } | ||
| 400 | |||
| 401 | static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size) | ||
| 402 | { | ||
| 403 | int ret=0; | ||
| 404 | |||
| 405 | buf[0]='\0'; | ||
| 406 | if (bp->flags&BIO_FLAGS_UPLINK) | ||
| 407 | UP_fgets(buf,size,bp->ptr); | ||
| 408 | else | ||
| 409 | fgets(buf,size,(FILE *)bp->ptr); | ||
| 410 | if (buf[0] != '\0') | ||
| 411 | ret=strlen(buf); | ||
| 412 | return(ret); | ||
| 413 | } | ||
| 414 | |||
| 415 | static int MS_CALLBACK file_puts(BIO *bp, const char *str) | ||
| 416 | { | ||
| 417 | int n,ret; | ||
| 418 | |||
| 419 | n=strlen(str); | ||
| 420 | ret=file_write(bp,str,n); | ||
| 421 | return(ret); | ||
| 422 | } | ||
| 423 | |||
| 424 | #endif /* OPENSSL_NO_STDIO */ | ||
| 425 | |||
| 426 | #endif /* HEADER_BSS_FILE_C */ | ||
| 427 | |||
| 428 | |||
diff --git a/src/lib/libcrypto/bio/bss_log.c b/src/lib/libcrypto/bio/bss_log.c new file mode 100644 index 0000000000..6360dbc820 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_log.c | |||
| @@ -0,0 +1,402 @@ | |||
| 1 | /* crypto/bio/bss_log.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * licensing@OpenSSL.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* | ||
| 57 | Why BIO_s_log? | ||
| 58 | |||
| 59 | BIO_s_log is useful for system daemons (or services under NT). | ||
| 60 | It is one-way BIO, it sends all stuff to syslogd (on system that | ||
| 61 | commonly use that), or event log (on NT), or OPCOM (on OpenVMS). | ||
| 62 | |||
| 63 | */ | ||
| 64 | |||
| 65 | |||
| 66 | #include <stdio.h> | ||
| 67 | #include <errno.h> | ||
| 68 | |||
| 69 | #include "cryptlib.h" | ||
| 70 | |||
| 71 | #if defined(OPENSSL_SYS_WINCE) | ||
| 72 | #elif defined(OPENSSL_SYS_WIN32) | ||
| 73 | # include <process.h> | ||
| 74 | #elif defined(OPENSSL_SYS_VMS) | ||
| 75 | # include <opcdef.h> | ||
| 76 | # include <descrip.h> | ||
| 77 | # include <lib$routines.h> | ||
| 78 | # include <starlet.h> | ||
| 79 | #elif defined(__ultrix) | ||
| 80 | # include <sys/syslog.h> | ||
| 81 | #elif defined(OPENSSL_SYS_NETWARE) | ||
| 82 | # define NO_SYSLOG | ||
| 83 | #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG) | ||
| 84 | # include <syslog.h> | ||
| 85 | #endif | ||
| 86 | |||
| 87 | #include <openssl/buffer.h> | ||
| 88 | #include <openssl/err.h> | ||
| 89 | |||
| 90 | #ifndef NO_SYSLOG | ||
| 91 | |||
| 92 | #if defined(OPENSSL_SYS_WIN32) | ||
| 93 | #define LOG_EMERG 0 | ||
| 94 | #define LOG_ALERT 1 | ||
| 95 | #define LOG_CRIT 2 | ||
| 96 | #define LOG_ERR 3 | ||
| 97 | #define LOG_WARNING 4 | ||
| 98 | #define LOG_NOTICE 5 | ||
| 99 | #define LOG_INFO 6 | ||
| 100 | #define LOG_DEBUG 7 | ||
| 101 | |||
| 102 | #define LOG_DAEMON (3<<3) | ||
| 103 | #elif defined(OPENSSL_SYS_VMS) | ||
| 104 | /* On VMS, we don't really care about these, but we need them to compile */ | ||
| 105 | #define LOG_EMERG 0 | ||
| 106 | #define LOG_ALERT 1 | ||
| 107 | #define LOG_CRIT 2 | ||
| 108 | #define LOG_ERR 3 | ||
| 109 | #define LOG_WARNING 4 | ||
| 110 | #define LOG_NOTICE 5 | ||
| 111 | #define LOG_INFO 6 | ||
| 112 | #define LOG_DEBUG 7 | ||
| 113 | |||
| 114 | #define LOG_DAEMON OPC$M_NM_NTWORK | ||
| 115 | #endif | ||
| 116 | |||
| 117 | static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num); | ||
| 118 | static int MS_CALLBACK slg_puts(BIO *h, const char *str); | ||
| 119 | static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 120 | static int MS_CALLBACK slg_new(BIO *h); | ||
| 121 | static int MS_CALLBACK slg_free(BIO *data); | ||
| 122 | static void xopenlog(BIO* bp, char* name, int level); | ||
| 123 | static void xsyslog(BIO* bp, int priority, const char* string); | ||
| 124 | static void xcloselog(BIO* bp); | ||
| 125 | #ifdef OPENSSL_SYS_WIN32 | ||
| 126 | LONG (WINAPI *go_for_advapi)() = RegOpenKeyEx; | ||
| 127 | HANDLE (WINAPI *register_event_source)() = NULL; | ||
| 128 | BOOL (WINAPI *deregister_event_source)() = NULL; | ||
| 129 | BOOL (WINAPI *report_event)() = NULL; | ||
| 130 | #define DL_PROC(m,f) (GetProcAddress( m, f )) | ||
| 131 | #ifdef UNICODE | ||
| 132 | #define DL_PROC_X(m,f) DL_PROC( m, f "W" ) | ||
| 133 | #else | ||
| 134 | #define DL_PROC_X(m,f) DL_PROC( m, f "A" ) | ||
| 135 | #endif | ||
| 136 | #endif | ||
| 137 | |||
| 138 | static BIO_METHOD methods_slg= | ||
| 139 | { | ||
| 140 | BIO_TYPE_MEM,"syslog", | ||
| 141 | slg_write, | ||
| 142 | NULL, | ||
| 143 | slg_puts, | ||
| 144 | NULL, | ||
| 145 | slg_ctrl, | ||
| 146 | slg_new, | ||
| 147 | slg_free, | ||
| 148 | NULL, | ||
| 149 | }; | ||
| 150 | |||
| 151 | BIO_METHOD *BIO_s_log(void) | ||
| 152 | { | ||
| 153 | return(&methods_slg); | ||
| 154 | } | ||
| 155 | |||
| 156 | static int MS_CALLBACK slg_new(BIO *bi) | ||
| 157 | { | ||
| 158 | bi->init=1; | ||
| 159 | bi->num=0; | ||
| 160 | bi->ptr=NULL; | ||
| 161 | xopenlog(bi, "application", LOG_DAEMON); | ||
| 162 | return(1); | ||
| 163 | } | ||
| 164 | |||
| 165 | static int MS_CALLBACK slg_free(BIO *a) | ||
| 166 | { | ||
| 167 | if (a == NULL) return(0); | ||
| 168 | xcloselog(a); | ||
| 169 | return(1); | ||
| 170 | } | ||
| 171 | |||
| 172 | static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl) | ||
| 173 | { | ||
| 174 | int ret= inl; | ||
| 175 | char* buf; | ||
| 176 | char* pp; | ||
| 177 | int priority, i; | ||
| 178 | static struct | ||
| 179 | { | ||
| 180 | int strl; | ||
| 181 | char str[10]; | ||
| 182 | int log_level; | ||
| 183 | } | ||
| 184 | mapping[] = | ||
| 185 | { | ||
| 186 | { 6, "PANIC ", LOG_EMERG }, | ||
| 187 | { 6, "EMERG ", LOG_EMERG }, | ||
| 188 | { 4, "EMR ", LOG_EMERG }, | ||
| 189 | { 6, "ALERT ", LOG_ALERT }, | ||
| 190 | { 4, "ALR ", LOG_ALERT }, | ||
| 191 | { 5, "CRIT ", LOG_CRIT }, | ||
| 192 | { 4, "CRI ", LOG_CRIT }, | ||
| 193 | { 6, "ERROR ", LOG_ERR }, | ||
| 194 | { 4, "ERR ", LOG_ERR }, | ||
| 195 | { 8, "WARNING ", LOG_WARNING }, | ||
| 196 | { 5, "WARN ", LOG_WARNING }, | ||
| 197 | { 4, "WAR ", LOG_WARNING }, | ||
| 198 | { 7, "NOTICE ", LOG_NOTICE }, | ||
| 199 | { 5, "NOTE ", LOG_NOTICE }, | ||
| 200 | { 4, "NOT ", LOG_NOTICE }, | ||
| 201 | { 5, "INFO ", LOG_INFO }, | ||
| 202 | { 4, "INF ", LOG_INFO }, | ||
| 203 | { 6, "DEBUG ", LOG_DEBUG }, | ||
| 204 | { 4, "DBG ", LOG_DEBUG }, | ||
| 205 | { 0, "", LOG_ERR } /* The default */ | ||
| 206 | }; | ||
| 207 | |||
| 208 | if((buf= (char *)OPENSSL_malloc(inl+ 1)) == NULL){ | ||
| 209 | return(0); | ||
| 210 | } | ||
| 211 | strncpy(buf, in, inl); | ||
| 212 | buf[inl]= '\0'; | ||
| 213 | |||
| 214 | i = 0; | ||
| 215 | while(strncmp(buf, mapping[i].str, mapping[i].strl) != 0) i++; | ||
| 216 | priority = mapping[i].log_level; | ||
| 217 | pp = buf + mapping[i].strl; | ||
| 218 | |||
| 219 | xsyslog(b, priority, pp); | ||
| 220 | |||
| 221 | OPENSSL_free(buf); | ||
| 222 | return(ret); | ||
| 223 | } | ||
| 224 | |||
| 225 | static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 226 | { | ||
| 227 | switch (cmd) | ||
| 228 | { | ||
| 229 | case BIO_CTRL_SET: | ||
| 230 | xcloselog(b); | ||
| 231 | xopenlog(b, ptr, num); | ||
| 232 | break; | ||
| 233 | default: | ||
| 234 | break; | ||
| 235 | } | ||
| 236 | return(0); | ||
| 237 | } | ||
| 238 | |||
| 239 | static int MS_CALLBACK slg_puts(BIO *bp, const char *str) | ||
| 240 | { | ||
| 241 | int n,ret; | ||
| 242 | |||
| 243 | n=strlen(str); | ||
| 244 | ret=slg_write(bp,str,n); | ||
| 245 | return(ret); | ||
| 246 | } | ||
| 247 | |||
| 248 | #if defined(OPENSSL_SYS_WIN32) | ||
| 249 | |||
| 250 | static void xopenlog(BIO* bp, char* name, int level) | ||
| 251 | { | ||
| 252 | if ( !register_event_source ) | ||
| 253 | { | ||
| 254 | HANDLE advapi; | ||
| 255 | if ( !(advapi = GetModuleHandle("advapi32")) ) | ||
| 256 | return; | ||
| 257 | register_event_source = (HANDLE (WINAPI *)())DL_PROC_X(advapi, | ||
| 258 | "RegisterEventSource" ); | ||
| 259 | deregister_event_source = (BOOL (WINAPI *)())DL_PROC(advapi, | ||
| 260 | "DeregisterEventSource"); | ||
| 261 | report_event = (BOOL (WINAPI *)())DL_PROC_X(advapi, | ||
| 262 | "ReportEvent" ); | ||
| 263 | if ( !(register_event_source && deregister_event_source && | ||
| 264 | report_event) ) | ||
| 265 | { | ||
| 266 | register_event_source = NULL; | ||
| 267 | deregister_event_source = NULL; | ||
| 268 | report_event = NULL; | ||
| 269 | return; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | bp->ptr= (char *)register_event_source(NULL, name); | ||
| 273 | } | ||
| 274 | |||
| 275 | static void xsyslog(BIO *bp, int priority, const char *string) | ||
| 276 | { | ||
| 277 | LPCSTR lpszStrings[2]; | ||
| 278 | WORD evtype= EVENTLOG_ERROR_TYPE; | ||
| 279 | int pid = _getpid(); | ||
| 280 | char pidbuf[DECIMAL_SIZE(pid)+4]; | ||
| 281 | |||
| 282 | switch (priority) | ||
| 283 | { | ||
| 284 | case LOG_EMERG: | ||
| 285 | case LOG_ALERT: | ||
| 286 | case LOG_CRIT: | ||
| 287 | case LOG_ERR: | ||
| 288 | evtype = EVENTLOG_ERROR_TYPE; | ||
| 289 | break; | ||
| 290 | case LOG_WARNING: | ||
| 291 | evtype = EVENTLOG_WARNING_TYPE; | ||
| 292 | break; | ||
| 293 | case LOG_NOTICE: | ||
| 294 | case LOG_INFO: | ||
| 295 | case LOG_DEBUG: | ||
| 296 | evtype = EVENTLOG_INFORMATION_TYPE; | ||
| 297 | break; | ||
| 298 | default: /* Should never happen, but set it | ||
| 299 | as error anyway. */ | ||
| 300 | evtype = EVENTLOG_ERROR_TYPE; | ||
| 301 | break; | ||
| 302 | } | ||
| 303 | |||
| 304 | sprintf(pidbuf, "[%d] ", pid); | ||
| 305 | lpszStrings[0] = pidbuf; | ||
| 306 | lpszStrings[1] = string; | ||
| 307 | |||
| 308 | if(report_event && bp->ptr) | ||
| 309 | report_event(bp->ptr, evtype, 0, 1024, NULL, 2, 0, | ||
| 310 | lpszStrings, NULL); | ||
| 311 | } | ||
| 312 | |||
| 313 | static void xcloselog(BIO* bp) | ||
| 314 | { | ||
| 315 | if(deregister_event_source && bp->ptr) | ||
| 316 | deregister_event_source((HANDLE)(bp->ptr)); | ||
| 317 | bp->ptr= NULL; | ||
| 318 | } | ||
| 319 | |||
| 320 | #elif defined(OPENSSL_SYS_VMS) | ||
| 321 | |||
| 322 | static int VMS_OPC_target = LOG_DAEMON; | ||
| 323 | |||
| 324 | static void xopenlog(BIO* bp, char* name, int level) | ||
| 325 | { | ||
| 326 | VMS_OPC_target = level; | ||
| 327 | } | ||
| 328 | |||
| 329 | static void xsyslog(BIO *bp, int priority, const char *string) | ||
| 330 | { | ||
| 331 | struct dsc$descriptor_s opc_dsc; | ||
| 332 | struct opcdef *opcdef_p; | ||
| 333 | char buf[10240]; | ||
| 334 | unsigned int len; | ||
| 335 | struct dsc$descriptor_s buf_dsc; | ||
| 336 | $DESCRIPTOR(fao_cmd, "!AZ: !AZ"); | ||
| 337 | char *priority_tag; | ||
| 338 | |||
| 339 | switch (priority) | ||
| 340 | { | ||
| 341 | case LOG_EMERG: priority_tag = "Emergency"; break; | ||
| 342 | case LOG_ALERT: priority_tag = "Alert"; break; | ||
| 343 | case LOG_CRIT: priority_tag = "Critical"; break; | ||
| 344 | case LOG_ERR: priority_tag = "Error"; break; | ||
| 345 | case LOG_WARNING: priority_tag = "Warning"; break; | ||
| 346 | case LOG_NOTICE: priority_tag = "Notice"; break; | ||
| 347 | case LOG_INFO: priority_tag = "Info"; break; | ||
| 348 | case LOG_DEBUG: priority_tag = "DEBUG"; break; | ||
| 349 | } | ||
| 350 | |||
| 351 | buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T; | ||
| 352 | buf_dsc.dsc$b_class = DSC$K_CLASS_S; | ||
| 353 | buf_dsc.dsc$a_pointer = buf; | ||
| 354 | buf_dsc.dsc$w_length = sizeof(buf) - 1; | ||
| 355 | |||
| 356 | lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string); | ||
| 357 | |||
| 358 | /* we know there's an 8 byte header. That's documented */ | ||
| 359 | opcdef_p = (struct opcdef *) OPENSSL_malloc(8 + len); | ||
| 360 | opcdef_p->opc$b_ms_type = OPC$_RQ_RQST; | ||
| 361 | memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3); | ||
| 362 | opcdef_p->opc$l_ms_rqstid = 0; | ||
| 363 | memcpy(&opcdef_p->opc$l_ms_text, buf, len); | ||
| 364 | |||
| 365 | opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T; | ||
| 366 | opc_dsc.dsc$b_class = DSC$K_CLASS_S; | ||
| 367 | opc_dsc.dsc$a_pointer = (char *)opcdef_p; | ||
| 368 | opc_dsc.dsc$w_length = len + 8; | ||
| 369 | |||
| 370 | sys$sndopr(opc_dsc, 0); | ||
| 371 | |||
| 372 | OPENSSL_free(opcdef_p); | ||
| 373 | } | ||
| 374 | |||
| 375 | static void xcloselog(BIO* bp) | ||
| 376 | { | ||
| 377 | } | ||
| 378 | |||
| 379 | #else /* Unix/Watt32 */ | ||
| 380 | |||
| 381 | static void xopenlog(BIO* bp, char* name, int level) | ||
| 382 | { | ||
| 383 | #ifdef WATT32 /* djgpp/DOS */ | ||
| 384 | openlog(name, LOG_PID|LOG_CONS|LOG_NDELAY, level); | ||
| 385 | #else | ||
| 386 | openlog(name, LOG_PID|LOG_CONS, level); | ||
| 387 | #endif | ||
| 388 | } | ||
| 389 | |||
| 390 | static void xsyslog(BIO *bp, int priority, const char *string) | ||
| 391 | { | ||
| 392 | syslog(priority, "%s", string); | ||
| 393 | } | ||
| 394 | |||
| 395 | static void xcloselog(BIO* bp) | ||
| 396 | { | ||
| 397 | closelog(); | ||
| 398 | } | ||
| 399 | |||
| 400 | #endif /* Unix */ | ||
| 401 | |||
| 402 | #endif /* NO_SYSLOG */ | ||
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c new file mode 100644 index 0000000000..a4edb711ae --- /dev/null +++ b/src/lib/libcrypto/bio/bss_mem.c | |||
| @@ -0,0 +1,321 @@ | |||
| 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 <openssl/bio.h> | ||
| 63 | |||
| 64 | static int mem_write(BIO *h, const char *buf, int num); | ||
| 65 | static int mem_read(BIO *h, char *buf, int size); | ||
| 66 | static int mem_puts(BIO *h, const char *str); | ||
| 67 | static int mem_gets(BIO *h, char *str, int size); | ||
| 68 | static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 69 | static int mem_new(BIO *h); | ||
| 70 | static int mem_free(BIO *data); | ||
| 71 | static BIO_METHOD mem_method= | ||
| 72 | { | ||
| 73 | BIO_TYPE_MEM, | ||
| 74 | "memory buffer", | ||
| 75 | mem_write, | ||
| 76 | mem_read, | ||
| 77 | mem_puts, | ||
| 78 | mem_gets, | ||
| 79 | mem_ctrl, | ||
| 80 | mem_new, | ||
| 81 | mem_free, | ||
| 82 | NULL, | ||
| 83 | }; | ||
| 84 | |||
| 85 | /* bio->num is used to hold the value to return on 'empty', if it is | ||
| 86 | * 0, should_retry is not set */ | ||
| 87 | |||
| 88 | BIO_METHOD *BIO_s_mem(void) | ||
| 89 | { | ||
| 90 | return(&mem_method); | ||
| 91 | } | ||
| 92 | |||
| 93 | BIO *BIO_new_mem_buf(void *buf, int len) | ||
| 94 | { | ||
| 95 | BIO *ret; | ||
| 96 | BUF_MEM *b; | ||
| 97 | if (!buf) { | ||
| 98 | BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER); | ||
| 99 | return NULL; | ||
| 100 | } | ||
| 101 | if(len == -1) len = strlen(buf); | ||
| 102 | if(!(ret = BIO_new(BIO_s_mem())) ) return NULL; | ||
| 103 | b = (BUF_MEM *)ret->ptr; | ||
| 104 | b->data = buf; | ||
| 105 | b->length = len; | ||
| 106 | b->max = len; | ||
| 107 | ret->flags |= BIO_FLAGS_MEM_RDONLY; | ||
| 108 | /* Since this is static data retrying wont help */ | ||
| 109 | ret->num = 0; | ||
| 110 | return ret; | ||
| 111 | } | ||
| 112 | |||
| 113 | static int mem_new(BIO *bi) | ||
| 114 | { | ||
| 115 | BUF_MEM *b; | ||
| 116 | |||
| 117 | if ((b=BUF_MEM_new()) == NULL) | ||
| 118 | return(0); | ||
| 119 | bi->shutdown=1; | ||
| 120 | bi->init=1; | ||
| 121 | bi->num= -1; | ||
| 122 | bi->ptr=(char *)b; | ||
| 123 | return(1); | ||
| 124 | } | ||
| 125 | |||
| 126 | static int mem_free(BIO *a) | ||
| 127 | { | ||
| 128 | if (a == NULL) return(0); | ||
| 129 | if (a->shutdown) | ||
| 130 | { | ||
| 131 | if ((a->init) && (a->ptr != NULL)) | ||
| 132 | { | ||
| 133 | BUF_MEM *b; | ||
| 134 | b = (BUF_MEM *)a->ptr; | ||
| 135 | if(a->flags & BIO_FLAGS_MEM_RDONLY) b->data = NULL; | ||
| 136 | BUF_MEM_free(b); | ||
| 137 | a->ptr=NULL; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | return(1); | ||
| 141 | } | ||
| 142 | |||
| 143 | static int mem_read(BIO *b, char *out, int outl) | ||
| 144 | { | ||
| 145 | int ret= -1; | ||
| 146 | BUF_MEM *bm; | ||
| 147 | int i; | ||
| 148 | char *from,*to; | ||
| 149 | |||
| 150 | bm=(BUF_MEM *)b->ptr; | ||
| 151 | BIO_clear_retry_flags(b); | ||
| 152 | ret=(outl > bm->length)?bm->length:outl; | ||
| 153 | if ((out != NULL) && (ret > 0)) { | ||
| 154 | memcpy(out,bm->data,ret); | ||
| 155 | bm->length-=ret; | ||
| 156 | /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */ | ||
| 157 | if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret; | ||
| 158 | else { | ||
| 159 | from=(char *)&(bm->data[ret]); | ||
| 160 | to=(char *)&(bm->data[0]); | ||
| 161 | for (i=0; i<bm->length; i++) | ||
| 162 | to[i]=from[i]; | ||
| 163 | } | ||
| 164 | } else if (bm->length == 0) | ||
| 165 | { | ||
| 166 | ret = b->num; | ||
| 167 | if (ret != 0) | ||
| 168 | BIO_set_retry_read(b); | ||
| 169 | } | ||
| 170 | return(ret); | ||
| 171 | } | ||
| 172 | |||
| 173 | static int mem_write(BIO *b, const char *in, int inl) | ||
| 174 | { | ||
| 175 | int ret= -1; | ||
| 176 | int blen; | ||
| 177 | BUF_MEM *bm; | ||
| 178 | |||
| 179 | bm=(BUF_MEM *)b->ptr; | ||
| 180 | if (in == NULL) | ||
| 181 | { | ||
| 182 | BIOerr(BIO_F_MEM_WRITE,BIO_R_NULL_PARAMETER); | ||
| 183 | goto end; | ||
| 184 | } | ||
| 185 | |||
| 186 | if(b->flags & BIO_FLAGS_MEM_RDONLY) { | ||
| 187 | BIOerr(BIO_F_MEM_WRITE,BIO_R_WRITE_TO_READ_ONLY_BIO); | ||
| 188 | goto end; | ||
| 189 | } | ||
| 190 | |||
| 191 | BIO_clear_retry_flags(b); | ||
| 192 | blen=bm->length; | ||
| 193 | if (BUF_MEM_grow_clean(bm,blen+inl) != (blen+inl)) | ||
| 194 | goto end; | ||
| 195 | memcpy(&(bm->data[blen]),in,inl); | ||
| 196 | ret=inl; | ||
| 197 | end: | ||
| 198 | return(ret); | ||
| 199 | } | ||
| 200 | |||
| 201 | static long mem_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 202 | { | ||
| 203 | long ret=1; | ||
| 204 | char **pptr; | ||
| 205 | |||
| 206 | BUF_MEM *bm=(BUF_MEM *)b->ptr; | ||
| 207 | |||
| 208 | switch (cmd) | ||
| 209 | { | ||
| 210 | case BIO_CTRL_RESET: | ||
| 211 | if (bm->data != NULL) | ||
| 212 | { | ||
| 213 | /* For read only case reset to the start again */ | ||
| 214 | if(b->flags & BIO_FLAGS_MEM_RDONLY) | ||
| 215 | { | ||
| 216 | bm->data -= bm->max - bm->length; | ||
| 217 | bm->length = bm->max; | ||
| 218 | } | ||
| 219 | else | ||
| 220 | { | ||
| 221 | memset(bm->data,0,bm->max); | ||
| 222 | bm->length=0; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | break; | ||
| 226 | case BIO_CTRL_EOF: | ||
| 227 | ret=(long)(bm->length == 0); | ||
| 228 | break; | ||
| 229 | case BIO_C_SET_BUF_MEM_EOF_RETURN: | ||
| 230 | b->num=(int)num; | ||
| 231 | break; | ||
| 232 | case BIO_CTRL_INFO: | ||
| 233 | ret=(long)bm->length; | ||
| 234 | if (ptr != NULL) | ||
| 235 | { | ||
| 236 | pptr=(char **)ptr; | ||
| 237 | *pptr=(char *)&(bm->data[0]); | ||
| 238 | } | ||
| 239 | break; | ||
| 240 | case BIO_C_SET_BUF_MEM: | ||
| 241 | mem_free(b); | ||
| 242 | b->shutdown=(int)num; | ||
| 243 | b->ptr=ptr; | ||
| 244 | break; | ||
| 245 | case BIO_C_GET_BUF_MEM_PTR: | ||
| 246 | if (ptr != NULL) | ||
| 247 | { | ||
| 248 | pptr=(char **)ptr; | ||
| 249 | *pptr=(char *)bm; | ||
| 250 | } | ||
| 251 | break; | ||
| 252 | case BIO_CTRL_GET_CLOSE: | ||
| 253 | ret=(long)b->shutdown; | ||
| 254 | break; | ||
| 255 | case BIO_CTRL_SET_CLOSE: | ||
| 256 | b->shutdown=(int)num; | ||
| 257 | break; | ||
| 258 | |||
| 259 | case BIO_CTRL_WPENDING: | ||
| 260 | ret=0L; | ||
| 261 | break; | ||
| 262 | case BIO_CTRL_PENDING: | ||
| 263 | ret=(long)bm->length; | ||
| 264 | break; | ||
| 265 | case BIO_CTRL_DUP: | ||
| 266 | case BIO_CTRL_FLUSH: | ||
| 267 | ret=1; | ||
| 268 | break; | ||
| 269 | case BIO_CTRL_PUSH: | ||
| 270 | case BIO_CTRL_POP: | ||
| 271 | default: | ||
| 272 | ret=0; | ||
| 273 | break; | ||
| 274 | } | ||
| 275 | return(ret); | ||
| 276 | } | ||
| 277 | |||
| 278 | static int mem_gets(BIO *bp, char *buf, int size) | ||
| 279 | { | ||
| 280 | int i,j; | ||
| 281 | int ret= -1; | ||
| 282 | char *p; | ||
| 283 | BUF_MEM *bm=(BUF_MEM *)bp->ptr; | ||
| 284 | |||
| 285 | BIO_clear_retry_flags(bp); | ||
| 286 | j=bm->length; | ||
| 287 | if (j <= 0) | ||
| 288 | { | ||
| 289 | *buf='\0'; | ||
| 290 | return 0; | ||
| 291 | } | ||
| 292 | p=bm->data; | ||
| 293 | for (i=0; i<j; i++) | ||
| 294 | { | ||
| 295 | if (p[i] == '\n') break; | ||
| 296 | } | ||
| 297 | if (i == j) | ||
| 298 | { | ||
| 299 | BIO_set_retry_read(bp); | ||
| 300 | /* return(-1); change the semantics 0.6.6a */ | ||
| 301 | } | ||
| 302 | else | ||
| 303 | i++; | ||
| 304 | /* i is the max to copy */ | ||
| 305 | if ((size-1) < i) i=size-1; | ||
| 306 | i=mem_read(bp,buf,i); | ||
| 307 | if (i > 0) buf[i]='\0'; | ||
| 308 | ret=i; | ||
| 309 | return(ret); | ||
| 310 | } | ||
| 311 | |||
| 312 | static int mem_puts(BIO *bp, const char *str) | ||
| 313 | { | ||
| 314 | int n,ret; | ||
| 315 | |||
| 316 | n=strlen(str); | ||
| 317 | ret=mem_write(bp,str,n); | ||
| 318 | /* memory semantics is that it will always work */ | ||
| 319 | return(ret); | ||
| 320 | } | ||
| 321 | |||
diff --git a/src/lib/libcrypto/bio/bss_null.c b/src/lib/libcrypto/bio/bss_null.c new file mode 100644 index 0000000000..46b73339df --- /dev/null +++ b/src/lib/libcrypto/bio/bss_null.c | |||
| @@ -0,0 +1,150 @@ | |||
| 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 <openssl/bio.h> | ||
| 63 | |||
| 64 | static int null_write(BIO *h, const char *buf, int num); | ||
| 65 | static int null_read(BIO *h, char *buf, int size); | ||
| 66 | static int null_puts(BIO *h, const char *str); | ||
| 67 | static int null_gets(BIO *h, char *str, int size); | ||
| 68 | static long null_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 69 | static int null_new(BIO *h); | ||
| 70 | static int null_free(BIO *data); | ||
| 71 | static BIO_METHOD null_method= | ||
| 72 | { | ||
| 73 | BIO_TYPE_NULL, | ||
| 74 | "NULL", | ||
| 75 | null_write, | ||
| 76 | null_read, | ||
| 77 | null_puts, | ||
| 78 | null_gets, | ||
| 79 | null_ctrl, | ||
| 80 | null_new, | ||
| 81 | null_free, | ||
| 82 | NULL, | ||
| 83 | }; | ||
| 84 | |||
| 85 | BIO_METHOD *BIO_s_null(void) | ||
| 86 | { | ||
| 87 | return(&null_method); | ||
| 88 | } | ||
| 89 | |||
| 90 | static int null_new(BIO *bi) | ||
| 91 | { | ||
| 92 | bi->init=1; | ||
| 93 | bi->num=0; | ||
| 94 | bi->ptr=(NULL); | ||
| 95 | return(1); | ||
| 96 | } | ||
| 97 | |||
| 98 | static int null_free(BIO *a) | ||
| 99 | { | ||
| 100 | if (a == NULL) return(0); | ||
| 101 | return(1); | ||
| 102 | } | ||
| 103 | |||
| 104 | static int null_read(BIO *b, char *out, int outl) | ||
| 105 | { | ||
| 106 | return(0); | ||
| 107 | } | ||
| 108 | |||
| 109 | static int null_write(BIO *b, const char *in, int inl) | ||
| 110 | { | ||
| 111 | return(inl); | ||
| 112 | } | ||
| 113 | |||
| 114 | static long null_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 115 | { | ||
| 116 | long ret=1; | ||
| 117 | |||
| 118 | switch (cmd) | ||
| 119 | { | ||
| 120 | case BIO_CTRL_RESET: | ||
| 121 | case BIO_CTRL_EOF: | ||
| 122 | case BIO_CTRL_SET: | ||
| 123 | case BIO_CTRL_SET_CLOSE: | ||
| 124 | case BIO_CTRL_FLUSH: | ||
| 125 | case BIO_CTRL_DUP: | ||
| 126 | ret=1; | ||
| 127 | break; | ||
| 128 | case BIO_CTRL_GET_CLOSE: | ||
| 129 | case BIO_CTRL_INFO: | ||
| 130 | case BIO_CTRL_GET: | ||
| 131 | case BIO_CTRL_PENDING: | ||
| 132 | case BIO_CTRL_WPENDING: | ||
| 133 | default: | ||
| 134 | ret=0; | ||
| 135 | break; | ||
| 136 | } | ||
| 137 | return(ret); | ||
| 138 | } | ||
| 139 | |||
| 140 | static int null_gets(BIO *bp, char *buf, int size) | ||
| 141 | { | ||
| 142 | return(0); | ||
| 143 | } | ||
| 144 | |||
| 145 | static int null_puts(BIO *bp, const char *str) | ||
| 146 | { | ||
| 147 | if (str == NULL) return(0); | ||
| 148 | return(strlen(str)); | ||
| 149 | } | ||
| 150 | |||
diff --git a/src/lib/libcrypto/bio/bss_rtcp.c b/src/lib/libcrypto/bio/bss_rtcp.c new file mode 100644 index 0000000000..7dae485564 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_rtcp.c | |||
| @@ -0,0 +1,294 @@ | |||
| 1 | /* crypto/bio/bss_rtcp.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 | /* Written by David L. Jones <jonesd@kcgl1.eng.ohio-state.edu> | ||
| 60 | * Date: 22-JUL-1996 | ||
| 61 | * Revised: 25-SEP-1997 Update for 0.8.1, BIO_CTRL_SET -> BIO_C_SET_FD | ||
| 62 | */ | ||
| 63 | /* VMS */ | ||
| 64 | #include <stdio.h> | ||
| 65 | #include <stdlib.h> | ||
| 66 | #include <string.h> | ||
| 67 | #include <errno.h> | ||
| 68 | #include "cryptlib.h" | ||
| 69 | #include <openssl/bio.h> | ||
| 70 | |||
| 71 | #include <iodef.h> /* VMS IO$_ definitions */ | ||
| 72 | #include <starlet.h> | ||
| 73 | |||
| 74 | typedef unsigned short io_channel; | ||
| 75 | /*************************************************************************/ | ||
| 76 | struct io_status { short status, count; long flags; }; | ||
| 77 | |||
| 78 | struct rpc_msg { /* Should have member alignment inhibited */ | ||
| 79 | char channel; /* 'A'-app data. 'R'-remote client 'G'-global */ | ||
| 80 | char function; /* 'G'-get, 'P'-put, 'C'-confirm, 'X'-close */ | ||
| 81 | unsigned short int length; /* Amount of data returned or max to return */ | ||
| 82 | char data[4092]; /* variable data */ | ||
| 83 | }; | ||
| 84 | #define RPC_HDR_SIZE (sizeof(struct rpc_msg) - 4092) | ||
| 85 | |||
| 86 | struct rpc_ctx { | ||
| 87 | int filled, pos; | ||
| 88 | struct rpc_msg msg; | ||
| 89 | }; | ||
| 90 | |||
| 91 | static int rtcp_write(BIO *h,const char *buf,int num); | ||
| 92 | static int rtcp_read(BIO *h,char *buf,int size); | ||
| 93 | static int rtcp_puts(BIO *h,const char *str); | ||
| 94 | static int rtcp_gets(BIO *h,char *str,int size); | ||
| 95 | static long rtcp_ctrl(BIO *h,int cmd,long arg1,void *arg2); | ||
| 96 | static int rtcp_new(BIO *h); | ||
| 97 | static int rtcp_free(BIO *data); | ||
| 98 | |||
| 99 | static BIO_METHOD rtcp_method= | ||
| 100 | { | ||
| 101 | BIO_TYPE_FD, | ||
| 102 | "RTCP", | ||
| 103 | rtcp_write, | ||
| 104 | rtcp_read, | ||
| 105 | rtcp_puts, | ||
| 106 | rtcp_gets, | ||
| 107 | rtcp_ctrl, | ||
| 108 | rtcp_new, | ||
| 109 | rtcp_free, | ||
| 110 | NULL, | ||
| 111 | }; | ||
| 112 | |||
| 113 | BIO_METHOD *BIO_s_rtcp(void) | ||
| 114 | { | ||
| 115 | return(&rtcp_method); | ||
| 116 | } | ||
| 117 | /*****************************************************************************/ | ||
| 118 | /* Decnet I/O routines. | ||
| 119 | */ | ||
| 120 | |||
| 121 | #ifdef __DECC | ||
| 122 | #pragma message save | ||
| 123 | #pragma message disable DOLLARID | ||
| 124 | #endif | ||
| 125 | |||
| 126 | static int get ( io_channel chan, char *buffer, int maxlen, int *length ) | ||
| 127 | { | ||
| 128 | int status; | ||
| 129 | struct io_status iosb; | ||
| 130 | status = sys$qiow ( 0, chan, IO$_READVBLK, &iosb, 0, 0, | ||
| 131 | buffer, maxlen, 0, 0, 0, 0 ); | ||
| 132 | if ( (status&1) == 1 ) status = iosb.status; | ||
| 133 | if ( (status&1) == 1 ) *length = iosb.count; | ||
| 134 | return status; | ||
| 135 | } | ||
| 136 | |||
| 137 | static int put ( io_channel chan, char *buffer, int length ) | ||
| 138 | { | ||
| 139 | int status; | ||
| 140 | struct io_status iosb; | ||
| 141 | status = sys$qiow ( 0, chan, IO$_WRITEVBLK, &iosb, 0, 0, | ||
| 142 | buffer, length, 0, 0, 0, 0 ); | ||
| 143 | if ( (status&1) == 1 ) status = iosb.status; | ||
| 144 | return status; | ||
| 145 | } | ||
| 146 | |||
| 147 | #ifdef __DECC | ||
| 148 | #pragma message restore | ||
| 149 | #endif | ||
| 150 | |||
| 151 | /***************************************************************************/ | ||
| 152 | |||
| 153 | static int rtcp_new(BIO *bi) | ||
| 154 | { | ||
| 155 | struct rpc_ctx *ctx; | ||
| 156 | bi->init=1; | ||
| 157 | bi->num=0; | ||
| 158 | bi->flags = 0; | ||
| 159 | bi->ptr=OPENSSL_malloc(sizeof(struct rpc_ctx)); | ||
| 160 | ctx = (struct rpc_ctx *) bi->ptr; | ||
| 161 | ctx->filled = 0; | ||
| 162 | ctx->pos = 0; | ||
| 163 | return(1); | ||
| 164 | } | ||
| 165 | |||
| 166 | static int rtcp_free(BIO *a) | ||
| 167 | { | ||
| 168 | if (a == NULL) return(0); | ||
| 169 | if ( a->ptr ) OPENSSL_free ( a->ptr ); | ||
| 170 | a->ptr = NULL; | ||
| 171 | return(1); | ||
| 172 | } | ||
| 173 | |||
| 174 | static int rtcp_read(BIO *b, char *out, int outl) | ||
| 175 | { | ||
| 176 | int status, length; | ||
| 177 | struct rpc_ctx *ctx; | ||
| 178 | /* | ||
| 179 | * read data, return existing. | ||
| 180 | */ | ||
| 181 | ctx = (struct rpc_ctx *) b->ptr; | ||
| 182 | if ( ctx->pos < ctx->filled ) { | ||
| 183 | length = ctx->filled - ctx->pos; | ||
| 184 | if ( length > outl ) length = outl; | ||
| 185 | memmove ( out, &ctx->msg.data[ctx->pos], length ); | ||
| 186 | ctx->pos += length; | ||
| 187 | return length; | ||
| 188 | } | ||
| 189 | /* | ||
| 190 | * Requst more data from R channel. | ||
| 191 | */ | ||
| 192 | ctx->msg.channel = 'R'; | ||
| 193 | ctx->msg.function = 'G'; | ||
| 194 | ctx->msg.length = sizeof(ctx->msg.data); | ||
| 195 | status = put ( b->num, (char *) &ctx->msg, RPC_HDR_SIZE ); | ||
| 196 | if ( (status&1) == 0 ) { | ||
| 197 | return -1; | ||
| 198 | } | ||
| 199 | /* | ||
| 200 | * Read. | ||
| 201 | */ | ||
| 202 | ctx->pos = ctx->filled = 0; | ||
| 203 | status = get ( b->num, (char *) &ctx->msg, sizeof(ctx->msg), &length ); | ||
| 204 | if ( (status&1) == 0 ) length = -1; | ||
| 205 | if ( ctx->msg.channel != 'R' || ctx->msg.function != 'C' ) { | ||
| 206 | length = -1; | ||
| 207 | } | ||
| 208 | ctx->filled = length - RPC_HDR_SIZE; | ||
| 209 | |||
| 210 | if ( ctx->pos < ctx->filled ) { | ||
| 211 | length = ctx->filled - ctx->pos; | ||
| 212 | if ( length > outl ) length = outl; | ||
| 213 | memmove ( out, ctx->msg.data, length ); | ||
| 214 | ctx->pos += length; | ||
| 215 | return length; | ||
| 216 | } | ||
| 217 | |||
| 218 | return length; | ||
| 219 | } | ||
| 220 | |||
| 221 | static int rtcp_write(BIO *b, const char *in, int inl) | ||
| 222 | { | ||
| 223 | int status, i, segment, length; | ||
| 224 | struct rpc_ctx *ctx; | ||
| 225 | /* | ||
| 226 | * Output data, send in chunks no larger that sizeof(ctx->msg.data). | ||
| 227 | */ | ||
| 228 | ctx = (struct rpc_ctx *) b->ptr; | ||
| 229 | for ( i = 0; i < inl; i += segment ) { | ||
| 230 | segment = inl - i; | ||
| 231 | if ( segment > sizeof(ctx->msg.data) ) segment = sizeof(ctx->msg.data); | ||
| 232 | ctx->msg.channel = 'R'; | ||
| 233 | ctx->msg.function = 'P'; | ||
| 234 | ctx->msg.length = segment; | ||
| 235 | memmove ( ctx->msg.data, &in[i], segment ); | ||
| 236 | status = put ( b->num, (char *) &ctx->msg, segment + RPC_HDR_SIZE ); | ||
| 237 | if ((status&1) == 0 ) { i = -1; break; } | ||
| 238 | |||
| 239 | status = get ( b->num, (char *) &ctx->msg, sizeof(ctx->msg), &length ); | ||
| 240 | if ( ((status&1) == 0) || (length < RPC_HDR_SIZE) ) { i = -1; break; } | ||
| 241 | if ( (ctx->msg.channel != 'R') || (ctx->msg.function != 'C') ) { | ||
| 242 | printf("unexpected response when confirming put %c %c\n", | ||
| 243 | ctx->msg.channel, ctx->msg.function ); | ||
| 244 | |||
| 245 | } | ||
| 246 | } | ||
| 247 | return(i); | ||
| 248 | } | ||
| 249 | |||
| 250 | static long rtcp_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 251 | { | ||
| 252 | long ret=1; | ||
| 253 | |||
| 254 | switch (cmd) | ||
| 255 | { | ||
| 256 | case BIO_CTRL_RESET: | ||
| 257 | case BIO_CTRL_EOF: | ||
| 258 | ret = 1; | ||
| 259 | break; | ||
| 260 | case BIO_C_SET_FD: | ||
| 261 | b->num = num; | ||
| 262 | ret = 1; | ||
| 263 | break; | ||
| 264 | case BIO_CTRL_SET_CLOSE: | ||
| 265 | case BIO_CTRL_FLUSH: | ||
| 266 | case BIO_CTRL_DUP: | ||
| 267 | ret=1; | ||
| 268 | break; | ||
| 269 | case BIO_CTRL_GET_CLOSE: | ||
| 270 | case BIO_CTRL_INFO: | ||
| 271 | case BIO_CTRL_GET: | ||
| 272 | case BIO_CTRL_PENDING: | ||
| 273 | case BIO_CTRL_WPENDING: | ||
| 274 | default: | ||
| 275 | ret=0; | ||
| 276 | break; | ||
| 277 | } | ||
| 278 | return(ret); | ||
| 279 | } | ||
| 280 | |||
| 281 | static int rtcp_gets(BIO *bp, char *buf, int size) | ||
| 282 | { | ||
| 283 | return(0); | ||
| 284 | } | ||
| 285 | |||
| 286 | static int rtcp_puts(BIO *bp, const char *str) | ||
| 287 | { | ||
| 288 | int length; | ||
| 289 | if (str == NULL) return(0); | ||
| 290 | length = strlen ( str ); | ||
| 291 | if ( length == 0 ) return (0); | ||
| 292 | return rtcp_write ( bp,str, length ); | ||
| 293 | } | ||
| 294 | |||
diff --git a/src/lib/libcrypto/bio/bss_sock.c b/src/lib/libcrypto/bio/bss_sock.c new file mode 100644 index 0000000000..472dd75821 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_sock.c | |||
| @@ -0,0 +1,302 @@ | |||
| 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 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #define USE_SOCKETS | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include <openssl/bio.h> | ||
| 64 | |||
| 65 | #ifdef WATT32 | ||
| 66 | #define sock_write SockWrite /* Watt-32 uses same names */ | ||
| 67 | #define sock_read SockRead | ||
| 68 | #define sock_puts SockPuts | ||
| 69 | #endif | ||
| 70 | |||
| 71 | static int sock_write(BIO *h, const char *buf, int num); | ||
| 72 | static int sock_read(BIO *h, char *buf, int size); | ||
| 73 | static int sock_puts(BIO *h, const char *str); | ||
| 74 | static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 75 | static int sock_new(BIO *h); | ||
| 76 | static int sock_free(BIO *data); | ||
| 77 | int BIO_sock_should_retry(int s); | ||
| 78 | |||
| 79 | static BIO_METHOD methods_sockp= | ||
| 80 | { | ||
| 81 | BIO_TYPE_SOCKET, | ||
| 82 | "socket", | ||
| 83 | sock_write, | ||
| 84 | sock_read, | ||
| 85 | sock_puts, | ||
| 86 | NULL, /* sock_gets, */ | ||
| 87 | sock_ctrl, | ||
| 88 | sock_new, | ||
| 89 | sock_free, | ||
| 90 | NULL, | ||
| 91 | }; | ||
| 92 | |||
| 93 | BIO_METHOD *BIO_s_socket(void) | ||
| 94 | { | ||
| 95 | return(&methods_sockp); | ||
| 96 | } | ||
| 97 | |||
| 98 | BIO *BIO_new_socket(int fd, int close_flag) | ||
| 99 | { | ||
| 100 | BIO *ret; | ||
| 101 | |||
| 102 | ret=BIO_new(BIO_s_socket()); | ||
| 103 | if (ret == NULL) return(NULL); | ||
| 104 | BIO_set_fd(ret,fd,close_flag); | ||
| 105 | return(ret); | ||
| 106 | } | ||
| 107 | |||
| 108 | static int sock_new(BIO *bi) | ||
| 109 | { | ||
| 110 | bi->init=0; | ||
| 111 | bi->num=0; | ||
| 112 | bi->ptr=NULL; | ||
| 113 | bi->flags=0; | ||
| 114 | return(1); | ||
| 115 | } | ||
| 116 | |||
| 117 | static int sock_free(BIO *a) | ||
| 118 | { | ||
| 119 | if (a == NULL) return(0); | ||
| 120 | if (a->shutdown) | ||
| 121 | { | ||
| 122 | if (a->init) | ||
| 123 | { | ||
| 124 | SHUTDOWN2(a->num); | ||
| 125 | } | ||
| 126 | a->init=0; | ||
| 127 | a->flags=0; | ||
| 128 | } | ||
| 129 | return(1); | ||
| 130 | } | ||
| 131 | |||
| 132 | static int sock_read(BIO *b, char *out, int outl) | ||
| 133 | { | ||
| 134 | int ret=0; | ||
| 135 | |||
| 136 | if (out != NULL) | ||
| 137 | { | ||
| 138 | clear_socket_error(); | ||
| 139 | ret=readsocket(b->num,out,outl); | ||
| 140 | BIO_clear_retry_flags(b); | ||
| 141 | if (ret <= 0) | ||
| 142 | { | ||
| 143 | if (BIO_sock_should_retry(ret)) | ||
| 144 | BIO_set_retry_read(b); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | return(ret); | ||
| 148 | } | ||
| 149 | |||
| 150 | static int sock_write(BIO *b, const char *in, int inl) | ||
| 151 | { | ||
| 152 | int ret; | ||
| 153 | |||
| 154 | clear_socket_error(); | ||
| 155 | ret=writesocket(b->num,in,inl); | ||
| 156 | BIO_clear_retry_flags(b); | ||
| 157 | if (ret <= 0) | ||
| 158 | { | ||
| 159 | if (BIO_sock_should_retry(ret)) | ||
| 160 | BIO_set_retry_write(b); | ||
| 161 | } | ||
| 162 | return(ret); | ||
| 163 | } | ||
| 164 | |||
| 165 | static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 166 | { | ||
| 167 | long ret=1; | ||
| 168 | int *ip; | ||
| 169 | |||
| 170 | switch (cmd) | ||
| 171 | { | ||
| 172 | case BIO_CTRL_RESET: | ||
| 173 | num=0; | ||
| 174 | case BIO_C_FILE_SEEK: | ||
| 175 | ret=0; | ||
| 176 | break; | ||
| 177 | case BIO_C_FILE_TELL: | ||
| 178 | case BIO_CTRL_INFO: | ||
| 179 | ret=0; | ||
| 180 | break; | ||
| 181 | case BIO_C_SET_FD: | ||
| 182 | sock_free(b); | ||
| 183 | b->num= *((int *)ptr); | ||
| 184 | b->shutdown=(int)num; | ||
| 185 | b->init=1; | ||
| 186 | break; | ||
| 187 | case BIO_C_GET_FD: | ||
| 188 | if (b->init) | ||
| 189 | { | ||
| 190 | ip=(int *)ptr; | ||
| 191 | if (ip != NULL) *ip=b->num; | ||
| 192 | ret=b->num; | ||
| 193 | } | ||
| 194 | else | ||
| 195 | ret= -1; | ||
| 196 | break; | ||
| 197 | case BIO_CTRL_GET_CLOSE: | ||
| 198 | ret=b->shutdown; | ||
| 199 | break; | ||
| 200 | case BIO_CTRL_SET_CLOSE: | ||
| 201 | b->shutdown=(int)num; | ||
| 202 | break; | ||
| 203 | case BIO_CTRL_PENDING: | ||
| 204 | case BIO_CTRL_WPENDING: | ||
| 205 | ret=0; | ||
| 206 | break; | ||
| 207 | case BIO_CTRL_DUP: | ||
| 208 | case BIO_CTRL_FLUSH: | ||
| 209 | ret=1; | ||
| 210 | break; | ||
| 211 | default: | ||
| 212 | ret=0; | ||
| 213 | break; | ||
| 214 | } | ||
| 215 | return(ret); | ||
| 216 | } | ||
| 217 | |||
| 218 | static int sock_puts(BIO *bp, const char *str) | ||
| 219 | { | ||
| 220 | int n,ret; | ||
| 221 | |||
| 222 | n=strlen(str); | ||
| 223 | ret=sock_write(bp,str,n); | ||
| 224 | return(ret); | ||
| 225 | } | ||
| 226 | |||
| 227 | int BIO_sock_should_retry(int i) | ||
| 228 | { | ||
| 229 | int err; | ||
| 230 | |||
| 231 | if ((i == 0) || (i == -1)) | ||
| 232 | { | ||
| 233 | err=get_last_socket_error(); | ||
| 234 | |||
| 235 | #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */ | ||
| 236 | if ((i == -1) && (err == 0)) | ||
| 237 | return(1); | ||
| 238 | #endif | ||
| 239 | |||
| 240 | return(BIO_sock_non_fatal_error(err)); | ||
| 241 | } | ||
| 242 | return(0); | ||
| 243 | } | ||
| 244 | |||
| 245 | int BIO_sock_non_fatal_error(int err) | ||
| 246 | { | ||
| 247 | switch (err) | ||
| 248 | { | ||
| 249 | #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE) | ||
| 250 | # if defined(WSAEWOULDBLOCK) | ||
| 251 | case WSAEWOULDBLOCK: | ||
| 252 | # endif | ||
| 253 | |||
| 254 | # if 0 /* This appears to always be an error */ | ||
| 255 | # if defined(WSAENOTCONN) | ||
| 256 | case WSAENOTCONN: | ||
| 257 | # endif | ||
| 258 | # endif | ||
| 259 | #endif | ||
| 260 | |||
| 261 | #ifdef EWOULDBLOCK | ||
| 262 | # ifdef WSAEWOULDBLOCK | ||
| 263 | # if WSAEWOULDBLOCK != EWOULDBLOCK | ||
| 264 | case EWOULDBLOCK: | ||
| 265 | # endif | ||
| 266 | # else | ||
| 267 | case EWOULDBLOCK: | ||
| 268 | # endif | ||
| 269 | #endif | ||
| 270 | |||
| 271 | #if defined(ENOTCONN) | ||
| 272 | case ENOTCONN: | ||
| 273 | #endif | ||
| 274 | |||
| 275 | #ifdef EINTR | ||
| 276 | case EINTR: | ||
| 277 | #endif | ||
| 278 | |||
| 279 | #ifdef EAGAIN | ||
| 280 | # if EWOULDBLOCK != EAGAIN | ||
| 281 | case EAGAIN: | ||
| 282 | # endif | ||
| 283 | #endif | ||
| 284 | |||
| 285 | #ifdef EPROTO | ||
| 286 | case EPROTO: | ||
| 287 | #endif | ||
| 288 | |||
| 289 | #ifdef EINPROGRESS | ||
| 290 | case EINPROGRESS: | ||
| 291 | #endif | ||
| 292 | |||
| 293 | #ifdef EALREADY | ||
| 294 | case EALREADY: | ||
| 295 | #endif | ||
| 296 | return(1); | ||
| 297 | /* break; */ | ||
| 298 | default: | ||
| 299 | break; | ||
| 300 | } | ||
| 301 | return(0); | ||
| 302 | } | ||
