summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bio')
-rw-r--r--src/lib/libcrypto/bio/b_dump.c187
-rw-r--r--src/lib/libcrypto/bio/b_print.c842
-rw-r--r--src/lib/libcrypto/bio/b_sock.c975
-rw-r--r--src/lib/libcrypto/bio/bf_buff.c512
-rw-r--r--src/lib/libcrypto/bio/bf_lbuf.c397
-rw-r--r--src/lib/libcrypto/bio/bf_nbio.c253
-rw-r--r--src/lib/libcrypto/bio/bf_null.c183
-rw-r--r--src/lib/libcrypto/bio/bio.h779
-rw-r--r--src/lib/libcrypto/bio/bio_cb.c143
-rw-r--r--src/lib/libcrypto/bio/bio_err.c154
-rw-r--r--src/lib/libcrypto/bio/bio_lib.c602
-rw-r--r--src/lib/libcrypto/bio/bss_acpt.c478
-rw-r--r--src/lib/libcrypto/bio/bss_bio.c924
-rw-r--r--src/lib/libcrypto/bio/bss_conn.c652
-rw-r--r--src/lib/libcrypto/bio/bss_dgram.c836
-rw-r--r--src/lib/libcrypto/bio/bss_fd.c319
-rw-r--r--src/lib/libcrypto/bio/bss_file.c477
-rw-r--r--src/lib/libcrypto/bio/bss_log.c399
-rw-r--r--src/lib/libcrypto/bio/bss_mem.c319
-rw-r--r--src/lib/libcrypto/bio/bss_null.c150
-rw-r--r--src/lib/libcrypto/bio/bss_sock.c294
21 files changed, 0 insertions, 9875 deletions
diff --git a/src/lib/libcrypto/bio/b_dump.c b/src/lib/libcrypto/bio/b_dump.c
deleted file mode 100644
index c80ecc4295..0000000000
--- a/src/lib/libcrypto/bio/b_dump.c
+++ /dev/null
@@ -1,187 +0,0 @@
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
71int 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
77int 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
161static int write_fp(const void *data, size_t len, void *fp)
162 {
163 return UP_fwrite(data, len, 1, fp);
164 }
165int BIO_dump_fp(FILE *fp, const char *s, int len)
166 {
167 return BIO_dump_cb(write_fp, fp, s, len);
168 }
169int 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
175static int write_bio(const void *data, size_t len, void *bp)
176 {
177 return BIO_write((BIO *)bp, (const char *)data, len);
178 }
179int BIO_dump(BIO *bp, const char *s, int len)
180 {
181 return BIO_dump_cb(write_bio, bp, s, len);
182 }
183int 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
deleted file mode 100644
index 143a7cfefa..0000000000
--- a/src/lib/libcrypto/bio/b_print.c
+++ /dev/null
@@ -1,842 +0,0 @@
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#ifdef HAVE_LONG_LONG
119# if defined(_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
128static void fmtstr (char **, char **, size_t *, size_t *,
129 const char *, int, int, int);
130static void fmtint (char **, char **, size_t *, size_t *,
131 LLONG, int, int, int, int);
132static void fmtfp (char **, char **, size_t *, size_t *,
133 LDOUBLE, int, int, int);
134static void doapr_outch (char **, char **, size_t *, size_t *, int);
135static 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
168static 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
432static void
433fmtstr(
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
472static void
473fmtint(
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
569static LDOUBLE
570abs_val(LDOUBLE value)
571{
572 LDOUBLE result = value;
573 if (value < 0)
574 result = -value;
575 return result;
576}
577
578static LDOUBLE
579pow_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
589static long
590roundv(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
600static void
601fmtfp(
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
725static void
726doapr_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
768int 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
781int 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 * This function has nothing to do with BIOs, but it's closely related
812 * to BIO_printf, and we need *some* name prefix ...
813 * (XXX the function should be renamed, but to what?) */
814int BIO_snprintf(char *buf, size_t n, const char *format, ...)
815 {
816 va_list args;
817 int ret;
818
819 va_start(args, format);
820
821 ret = BIO_vsnprintf(buf, n, format, args);
822
823 va_end(args);
824 return(ret);
825 }
826
827int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
828 {
829 size_t retlen;
830 int truncated;
831
832 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
833
834 if (truncated)
835 /* In case of truncation, return -1 like traditional snprintf.
836 * (Current drafts for ISO/IEC 9899 say snprintf should return
837 * the number of characters that would have been written,
838 * had the buffer been large enough.) */
839 return -1;
840 else
841 return (retlen <= INT_MAX) ? (int)retlen : -1;
842 }
diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c
deleted file mode 100644
index d47310d650..0000000000
--- a/src/lib/libcrypto/bio/b_sock.c
+++ /dev/null
@@ -1,975 +0,0 @@
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>
69NETDB_DEFINE_CONTEXT
70#endif
71#endif
72
73#ifndef OPENSSL_NO_SOCK
74
75#include <openssl/dso.h>
76
77#define SOCKET_PROTOCOL IPPROTO_TCP
78
79#ifdef SO_MAXCONN
80#define MAX_LISTEN SO_MAXCONN
81#elif defined(SOMAXCONN)
82#define MAX_LISTEN SOMAXCONN
83#else
84#define MAX_LISTEN 32
85#endif
86
87#if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
88static int wsa_init_done=0;
89#endif
90
91/*
92 * WSAAPI specifier is required to make indirect calls to run-time
93 * linked WinSock 2 functions used in this module, to be specific
94 * [get|free]addrinfo and getnameinfo. This is because WinSock uses
95 * uses non-C calling convention, __stdcall vs. __cdecl, on x86
96 * Windows. On non-WinSock platforms WSAAPI needs to be void.
97 */
98#ifndef WSAAPI
99#define WSAAPI
100#endif
101
102#if 0
103static unsigned long BIO_ghbn_hits=0L;
104static unsigned long BIO_ghbn_miss=0L;
105
106#define GHBN_NUM 4
107static struct ghbn_cache_st
108 {
109 char name[129];
110 struct hostent *ent;
111 unsigned long order;
112 } ghbn_cache[GHBN_NUM];
113#endif
114
115static int get_ip(const char *str,unsigned char *ip);
116#if 0
117static void ghbn_free(struct hostent *a);
118static struct hostent *ghbn_dup(struct hostent *a);
119#endif
120int BIO_get_host_ip(const char *str, unsigned char *ip)
121 {
122 int i;
123 int err = 1;
124 int locked = 0;
125 struct hostent *he;
126
127 i=get_ip(str,ip);
128 if (i < 0)
129 {
130 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS);
131 goto err;
132 }
133
134 /* At this point, we have something that is most probably correct
135 in some way, so let's init the socket. */
136 if (BIO_sock_init() != 1)
137 return 0; /* don't generate another error code here */
138
139 /* If the string actually contained an IP address, we need not do
140 anything more */
141 if (i > 0) return(1);
142
143 /* do a gethostbyname */
144 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
145 locked = 1;
146 he=BIO_gethostbyname(str);
147 if (he == NULL)
148 {
149 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP);
150 goto err;
151 }
152
153 /* cast to short because of win16 winsock definition */
154 if ((short)he->h_addrtype != AF_INET)
155 {
156 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
157 goto err;
158 }
159 for (i=0; i<4; i++)
160 ip[i]=he->h_addr_list[0][i];
161 err = 0;
162
163 err:
164 if (locked)
165 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
166 if (err)
167 {
168 ERR_add_error_data(2,"host=",str);
169 return 0;
170 }
171 else
172 return 1;
173 }
174
175int BIO_get_port(const char *str, unsigned short *port_ptr)
176 {
177 int i;
178 struct servent *s;
179
180 if (str == NULL)
181 {
182 BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED);
183 return(0);
184 }
185 i=atoi(str);
186 if (i != 0)
187 *port_ptr=(unsigned short)i;
188 else
189 {
190 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
191 /* Note: under VMS with SOCKETSHR, it seems like the first
192 * parameter is 'char *', instead of 'const char *'
193 */
194#ifndef CONST_STRICT
195 s=getservbyname((char *)str,"tcp");
196#else
197 s=getservbyname(str,"tcp");
198#endif
199 if(s != NULL)
200 *port_ptr=ntohs((unsigned short)s->s_port);
201 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
202 if(s == NULL)
203 {
204 if (strcmp(str,"http") == 0)
205 *port_ptr=80;
206 else if (strcmp(str,"telnet") == 0)
207 *port_ptr=23;
208 else if (strcmp(str,"socks") == 0)
209 *port_ptr=1080;
210 else if (strcmp(str,"https") == 0)
211 *port_ptr=443;
212 else if (strcmp(str,"ssl") == 0)
213 *port_ptr=443;
214 else if (strcmp(str,"ftp") == 0)
215 *port_ptr=21;
216 else if (strcmp(str,"gopher") == 0)
217 *port_ptr=70;
218#if 0
219 else if (strcmp(str,"wais") == 0)
220 *port_ptr=21;
221#endif
222 else
223 {
224 SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error());
225 ERR_add_error_data(3,"service='",str,"'");
226 return(0);
227 }
228 }
229 }
230 return(1);
231 }
232
233int BIO_sock_error(int sock)
234 {
235 int j,i;
236 int size;
237
238#if defined(OPENSSL_SYS_BEOS_R5)
239 return 0;
240#endif
241
242 size=sizeof(int);
243 /* Note: under Windows the third parameter is of type (char *)
244 * whereas under other systems it is (void *) if you don't have
245 * a cast it will choke the compiler: if you do have a cast then
246 * you can either go for (char *) or (void *).
247 */
248 i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void *)&j,(void *)&size);
249 if (i < 0)
250 return(1);
251 else
252 return(j);
253 }
254
255#if 0
256long BIO_ghbn_ctrl(int cmd, int iarg, char *parg)
257 {
258 int i;
259 char **p;
260
261 switch (cmd)
262 {
263 case BIO_GHBN_CTRL_HITS:
264 return(BIO_ghbn_hits);
265 /* break; */
266 case BIO_GHBN_CTRL_MISSES:
267 return(BIO_ghbn_miss);
268 /* break; */
269 case BIO_GHBN_CTRL_CACHE_SIZE:
270 return(GHBN_NUM);
271 /* break; */
272 case BIO_GHBN_CTRL_GET_ENTRY:
273 if ((iarg >= 0) && (iarg <GHBN_NUM) &&
274 (ghbn_cache[iarg].order > 0))
275 {
276 p=(char **)parg;
277 if (p == NULL) return(0);
278 *p=ghbn_cache[iarg].name;
279 ghbn_cache[iarg].name[128]='\0';
280 return(1);
281 }
282 return(0);
283 /* break; */
284 case BIO_GHBN_CTRL_FLUSH:
285 for (i=0; i<GHBN_NUM; i++)
286 ghbn_cache[i].order=0;
287 break;
288 default:
289 return(0);
290 }
291 return(1);
292 }
293#endif
294
295#if 0
296static struct hostent *ghbn_dup(struct hostent *a)
297 {
298 struct hostent *ret;
299 int i,j;
300
301 MemCheck_off();
302 ret=(struct hostent *)OPENSSL_malloc(sizeof(struct hostent));
303 if (ret == NULL) return(NULL);
304 memset(ret,0,sizeof(struct hostent));
305
306 for (i=0; a->h_aliases[i] != NULL; i++)
307 ;
308 i++;
309 ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *));
310 if (ret->h_aliases == NULL)
311 goto err;
312 memset(ret->h_aliases, 0, i*sizeof(char *));
313
314 for (i=0; a->h_addr_list[i] != NULL; i++)
315 ;
316 i++;
317 ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *));
318 if (ret->h_addr_list == NULL)
319 goto err;
320 memset(ret->h_addr_list, 0, i*sizeof(char *));
321
322 j=strlen(a->h_name)+1;
323 if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err;
324 memcpy((char *)ret->h_name,a->h_name,j);
325 for (i=0; a->h_aliases[i] != NULL; i++)
326 {
327 j=strlen(a->h_aliases[i])+1;
328 if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err;
329 memcpy(ret->h_aliases[i],a->h_aliases[i],j);
330 }
331 ret->h_length=a->h_length;
332 ret->h_addrtype=a->h_addrtype;
333 for (i=0; a->h_addr_list[i] != NULL; i++)
334 {
335 if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL)
336 goto err;
337 memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);
338 }
339 if (0)
340 {
341err:
342 if (ret != NULL)
343 ghbn_free(ret);
344 ret=NULL;
345 }
346 MemCheck_on();
347 return(ret);
348 }
349
350static void ghbn_free(struct hostent *a)
351 {
352 int i;
353
354 if(a == NULL)
355 return;
356
357 if (a->h_aliases != NULL)
358 {
359 for (i=0; a->h_aliases[i] != NULL; i++)
360 OPENSSL_free(a->h_aliases[i]);
361 OPENSSL_free(a->h_aliases);
362 }
363 if (a->h_addr_list != NULL)
364 {
365 for (i=0; a->h_addr_list[i] != NULL; i++)
366 OPENSSL_free(a->h_addr_list[i]);
367 OPENSSL_free(a->h_addr_list);
368 }
369 if (a->h_name != NULL) OPENSSL_free(a->h_name);
370 OPENSSL_free(a);
371 }
372
373#endif
374
375struct hostent *BIO_gethostbyname(const char *name)
376 {
377#if 1
378 /* Caching gethostbyname() results forever is wrong,
379 * so we have to let the true gethostbyname() worry about this */
380#if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
381 return gethostbyname((char*)name);
382#else
383 return gethostbyname(name);
384#endif
385#else
386 struct hostent *ret;
387 int i,lowi=0,j;
388 unsigned long low= (unsigned long)-1;
389
390
391# if 0
392 /* It doesn't make sense to use locking here: The function interface
393 * is not thread-safe, because threads can never be sure when
394 * some other thread destroys the data they were given a pointer to.
395 */
396 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
397# endif
398 j=strlen(name);
399 if (j < 128)
400 {
401 for (i=0; i<GHBN_NUM; i++)
402 {
403 if (low > ghbn_cache[i].order)
404 {
405 low=ghbn_cache[i].order;
406 lowi=i;
407 }
408 if (ghbn_cache[i].order > 0)
409 {
410 if (strncmp(name,ghbn_cache[i].name,128) == 0)
411 break;
412 }
413 }
414 }
415 else
416 i=GHBN_NUM;
417
418 if (i == GHBN_NUM) /* no hit*/
419 {
420 BIO_ghbn_miss++;
421 /* Note: under VMS with SOCKETSHR, it seems like the first
422 * parameter is 'char *', instead of 'const char *'
423 */
424# ifndef CONST_STRICT
425 ret=gethostbyname((char *)name);
426# else
427 ret=gethostbyname(name);
428# endif
429
430 if (ret == NULL)
431 goto end;
432 if (j > 128) /* too big to cache */
433 {
434# if 0
435 /* If we were trying to make this function thread-safe (which
436 * is bound to fail), we'd have to give up in this case
437 * (or allocate more memory). */
438 ret = NULL;
439# endif
440 goto end;
441 }
442
443 /* else add to cache */
444 if (ghbn_cache[lowi].ent != NULL)
445 ghbn_free(ghbn_cache[lowi].ent); /* XXX not thread-safe */
446 ghbn_cache[lowi].name[0] = '\0';
447
448 if((ret=ghbn_cache[lowi].ent=ghbn_dup(ret)) == NULL)
449 {
450 BIOerr(BIO_F_BIO_GETHOSTBYNAME,ERR_R_MALLOC_FAILURE);
451 goto end;
452 }
453 strncpy(ghbn_cache[lowi].name,name,128);
454 ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits;
455 }
456 else
457 {
458 BIO_ghbn_hits++;
459 ret= ghbn_cache[i].ent;
460 ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits;
461 }
462end:
463# if 0
464 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
465# endif
466 return(ret);
467#endif
468 }
469
470
471int BIO_sock_init(void)
472 {
473#ifdef OPENSSL_SYS_WINDOWS
474 static struct WSAData wsa_state;
475
476 if (!wsa_init_done)
477 {
478 int err;
479
480 wsa_init_done=1;
481 memset(&wsa_state,0,sizeof(wsa_state));
482 /* Not making wsa_state available to the rest of the
483 * code is formally wrong. But the structures we use
484 * are [beleived to be] invariable among Winsock DLLs,
485 * while API availability is [expected to be] probed
486 * at run-time with DSO_global_lookup. */
487 if (WSAStartup(0x0202,&wsa_state)!=0)
488 {
489 err=WSAGetLastError();
490 SYSerr(SYS_F_WSASTARTUP,err);
491 BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
492 return(-1);
493 }
494 }
495#endif /* OPENSSL_SYS_WINDOWS */
496#ifdef WATT32
497 extern int _watt_do_exit;
498 _watt_do_exit = 0; /* don't make sock_init() call exit() */
499 if (sock_init())
500 return (-1);
501#endif
502
503#if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
504 WORD wVerReq;
505 WSADATA wsaData;
506 int err;
507
508 if (!wsa_init_done)
509 {
510 wsa_init_done=1;
511 wVerReq = MAKEWORD( 2, 0 );
512 err = WSAStartup(wVerReq,&wsaData);
513 if (err != 0)
514 {
515 SYSerr(SYS_F_WSASTARTUP,err);
516 BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
517 return(-1);
518 }
519 }
520#endif
521
522 return(1);
523 }
524
525void BIO_sock_cleanup(void)
526 {
527#ifdef OPENSSL_SYS_WINDOWS
528 if (wsa_init_done)
529 {
530 wsa_init_done=0;
531#if 0 /* this call is claimed to be non-present in Winsock2 */
532 WSACancelBlockingCall();
533#endif
534 WSACleanup();
535 }
536#elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
537 if (wsa_init_done)
538 {
539 wsa_init_done=0;
540 WSACleanup();
541 }
542#endif
543 }
544
545#if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
546
547int BIO_socket_ioctl(int fd, long type, void *arg)
548 {
549 int i;
550
551#ifdef __DJGPP__
552 i=ioctlsocket(fd,type,(char *)arg);
553#else
554# if defined(OPENSSL_SYS_VMS)
555 /* 2011-02-18 SMS.
556 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
557 * observe that all the consumers pass in an "unsigned long *",
558 * so we arrange a local copy with a short pointer, and use
559 * that, instead.
560 */
561# if __INITIAL_POINTER_SIZE == 64
562# define ARG arg_32p
563# pragma pointer_size save
564# pragma pointer_size 32
565 unsigned long arg_32;
566 unsigned long *arg_32p;
567# pragma pointer_size restore
568 arg_32p = &arg_32;
569 arg_32 = *((unsigned long *) arg);
570# else /* __INITIAL_POINTER_SIZE == 64 */
571# define ARG arg
572# endif /* __INITIAL_POINTER_SIZE == 64 [else] */
573# else /* defined(OPENSSL_SYS_VMS) */
574# define ARG arg
575# endif /* defined(OPENSSL_SYS_VMS) [else] */
576
577 i=ioctlsocket(fd,type,ARG);
578#endif /* __DJGPP__ */
579 if (i < 0)
580 SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error());
581 return(i);
582 }
583#endif /* __VMS_VER */
584
585/* The reason I have implemented this instead of using sscanf is because
586 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
587static int get_ip(const char *str, unsigned char ip[4])
588 {
589 unsigned int tmp[4];
590 int num=0,c,ok=0;
591
592 tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
593
594 for (;;)
595 {
596 c= *(str++);
597 if ((c >= '0') && (c <= '9'))
598 {
599 ok=1;
600 tmp[num]=tmp[num]*10+c-'0';
601 if (tmp[num] > 255) return(0);
602 }
603 else if (c == '.')
604 {
605 if (!ok) return(-1);
606 if (num == 3) return(0);
607 num++;
608 ok=0;
609 }
610 else if (c == '\0' && (num == 3) && ok)
611 break;
612 else
613 return(0);
614 }
615 ip[0]=tmp[0];
616 ip[1]=tmp[1];
617 ip[2]=tmp[2];
618 ip[3]=tmp[3];
619 return(1);
620 }
621
622int BIO_get_accept_socket(char *host, int bind_mode)
623 {
624 int ret=0;
625 union {
626 struct sockaddr sa;
627 struct sockaddr_in sa_in;
628#if OPENSSL_USE_IPV6
629 struct sockaddr_in6 sa_in6;
630#endif
631 } server,client;
632 int s=INVALID_SOCKET,cs,addrlen;
633 unsigned char ip[4];
634 unsigned short port;
635 char *str=NULL,*e;
636 char *h,*p;
637 unsigned long l;
638 int err_num;
639
640 if (BIO_sock_init() != 1) return(INVALID_SOCKET);
641
642 if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET);
643
644 h=p=NULL;
645 h=str;
646 for (e=str; *e; e++)
647 {
648 if (*e == ':')
649 {
650 p=e;
651 }
652 else if (*e == '/')
653 {
654 *e='\0';
655 break;
656 }
657 }
658 if (p) *p++='\0'; /* points at last ':', '::port' is special [see below] */
659 else p=h,h=NULL;
660
661#ifdef EAI_FAMILY
662 do {
663 static union { void *p;
664 int (WSAAPI *f)(const char *,const char *,
665 const struct addrinfo *,
666 struct addrinfo **);
667 } p_getaddrinfo = {NULL};
668 static union { void *p;
669 void (WSAAPI *f)(struct addrinfo *);
670 } p_freeaddrinfo = {NULL};
671 struct addrinfo *res,hint;
672
673 if (p_getaddrinfo.p==NULL)
674 {
675 if ((p_getaddrinfo.p=DSO_global_lookup("getaddrinfo"))==NULL ||
676 (p_freeaddrinfo.p=DSO_global_lookup("freeaddrinfo"))==NULL)
677 p_getaddrinfo.p=(void*)-1;
678 }
679 if (p_getaddrinfo.p==(void *)-1) break;
680
681 /* '::port' enforces IPv6 wildcard listener. Some OSes,
682 * e.g. Solaris, default to IPv6 without any hint. Also
683 * note that commonly IPv6 wildchard socket can service
684 * IPv4 connections just as well... */
685 memset(&hint,0,sizeof(hint));
686 hint.ai_flags = AI_PASSIVE;
687 if (h)
688 {
689 if (strchr(h,':'))
690 {
691 if (h[1]=='\0') h=NULL;
692#if OPENSSL_USE_IPV6
693 hint.ai_family = AF_INET6;
694#else
695 h=NULL;
696#endif
697 }
698 else if (h[0]=='*' && h[1]=='\0')
699 {
700 hint.ai_family = AF_INET;
701 h=NULL;
702 }
703 }
704
705 if ((*p_getaddrinfo.f)(h,p,&hint,&res)) break;
706
707 addrlen = res->ai_addrlen<=sizeof(server) ?
708 res->ai_addrlen :
709 sizeof(server);
710 memcpy(&server, res->ai_addr, addrlen);
711
712 (*p_freeaddrinfo.f)(res);
713 goto again;
714 } while (0);
715#endif
716
717 if (!BIO_get_port(p,&port)) goto err;
718
719 memset((char *)&server,0,sizeof(server));
720 server.sa_in.sin_family=AF_INET;
721 server.sa_in.sin_port=htons(port);
722 addrlen = sizeof(server.sa_in);
723
724 if (h == NULL || strcmp(h,"*") == 0)
725 server.sa_in.sin_addr.s_addr=INADDR_ANY;
726 else
727 {
728 if (!BIO_get_host_ip(h,&(ip[0]))) goto err;
729 l=(unsigned long)
730 ((unsigned long)ip[0]<<24L)|
731 ((unsigned long)ip[1]<<16L)|
732 ((unsigned long)ip[2]<< 8L)|
733 ((unsigned long)ip[3]);
734 server.sa_in.sin_addr.s_addr=htonl(l);
735 }
736
737again:
738 s=socket(server.sa.sa_family,SOCK_STREAM,SOCKET_PROTOCOL);
739 if (s == INVALID_SOCKET)
740 {
741 SYSerr(SYS_F_SOCKET,get_last_socket_error());
742 ERR_add_error_data(3,"port='",host,"'");
743 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET);
744 goto err;
745 }
746
747#ifdef SO_REUSEADDR
748 if (bind_mode == BIO_BIND_REUSEADDR)
749 {
750 int i=1;
751
752 ret=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i));
753 bind_mode=BIO_BIND_NORMAL;
754 }
755#endif
756 if (bind(s,&server.sa,addrlen) == -1)
757 {
758#ifdef SO_REUSEADDR
759 err_num=get_last_socket_error();
760 if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
761#ifdef OPENSSL_SYS_WINDOWS
762 /* Some versions of Windows define EADDRINUSE to
763 * a dummy value.
764 */
765 (err_num == WSAEADDRINUSE))
766#else
767 (err_num == EADDRINUSE))
768#endif
769 {
770 client = server;
771 if (h == NULL || strcmp(h,"*") == 0)
772 {
773#if OPENSSL_USE_IPV6
774 if (client.sa.sa_family == AF_INET6)
775 {
776 memset(&client.sa_in6.sin6_addr,0,sizeof(client.sa_in6.sin6_addr));
777 client.sa_in6.sin6_addr.s6_addr[15]=1;
778 }
779 else
780#endif
781 if (client.sa.sa_family == AF_INET)
782 {
783 client.sa_in.sin_addr.s_addr=htonl(0x7F000001);
784 }
785 else goto err;
786 }
787 cs=socket(client.sa.sa_family,SOCK_STREAM,SOCKET_PROTOCOL);
788 if (cs != INVALID_SOCKET)
789 {
790 int ii;
791 ii=connect(cs,&client.sa,addrlen);
792 closesocket(cs);
793 if (ii == INVALID_SOCKET)
794 {
795 bind_mode=BIO_BIND_REUSEADDR;
796 closesocket(s);
797 goto again;
798 }
799 /* else error */
800 }
801 /* else error */
802 }
803#endif
804 SYSerr(SYS_F_BIND,err_num);
805 ERR_add_error_data(3,"port='",host,"'");
806 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
807 goto err;
808 }
809 if (listen(s,MAX_LISTEN) == -1)
810 {
811 SYSerr(SYS_F_BIND,get_last_socket_error());
812 ERR_add_error_data(3,"port='",host,"'");
813 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET);
814 goto err;
815 }
816 ret=1;
817err:
818 if (str != NULL) OPENSSL_free(str);
819 if ((ret == 0) && (s != INVALID_SOCKET))
820 {
821 closesocket(s);
822 s= INVALID_SOCKET;
823 }
824 return(s);
825 }
826
827int BIO_accept(int sock, char **addr)
828 {
829 int ret=INVALID_SOCKET;
830 unsigned long l;
831 unsigned short port;
832 char *p;
833
834 struct {
835 /*
836 * As for following union. Trouble is that there are platforms
837 * that have socklen_t and there are platforms that don't, on
838 * some platforms socklen_t is int and on some size_t. So what
839 * one can do? One can cook #ifdef spaghetti, which is nothing
840 * but masochistic. Or one can do union between int and size_t.
841 * One naturally does it primarily for 64-bit platforms where
842 * sizeof(int) != sizeof(size_t). But would it work? Note that
843 * if size_t member is initialized to 0, then later int member
844 * assignment naturally does the job on little-endian platforms
845 * regardless accept's expectations! What about big-endians?
846 * If accept expects int*, then it works, and if size_t*, then
847 * length value would appear as unreasonably large. But this
848 * won't prevent it from filling in the address structure. The
849 * trouble of course would be if accept returns more data than
850 * actual buffer can accomodate and overwrite stack... That's
851 * where early OPENSSL_assert comes into picture. Besides, the
852 * only 64-bit big-endian platform found so far that expects
853 * size_t* is HP-UX, where stack grows towards higher address.
854 * <appro>
855 */
856 union { size_t s; int i; } len;
857 union {
858 struct sockaddr sa;
859 struct sockaddr_in sa_in;
860#if OPENSSL_USE_IPV6
861 struct sockaddr_in6 sa_in6;
862#endif
863 } from;
864 } sa;
865
866 sa.len.s=0;
867 sa.len.i=sizeof(sa.from);
868 memset(&sa.from,0,sizeof(sa.from));
869 ret=accept(sock,&sa.from.sa,(void *)&sa.len);
870 if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
871 {
872 OPENSSL_assert(sa.len.s<=sizeof(sa.from));
873 sa.len.i = (int)sa.len.s;
874 /* use sa.len.i from this point */
875 }
876 if (ret == INVALID_SOCKET)
877 {
878 if(BIO_sock_should_retry(ret)) return -2;
879 SYSerr(SYS_F_ACCEPT,get_last_socket_error());
880 BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR);
881 goto end;
882 }
883
884 if (addr == NULL) goto end;
885
886#ifdef EAI_FAMILY
887 do {
888 char h[NI_MAXHOST],s[NI_MAXSERV];
889 size_t nl;
890 static union { void *p;
891 int (WSAAPI *f)(const struct sockaddr *,size_t/*socklen_t*/,
892 char *,size_t,char *,size_t,int);
893 } p_getnameinfo = {NULL};
894 /* 2nd argument to getnameinfo is specified to
895 * be socklen_t. Unfortunately there is a number
896 * of environments where socklen_t is not defined.
897 * As it's passed by value, it's safe to pass it
898 * as size_t... <appro> */
899
900 if (p_getnameinfo.p==NULL)
901 {
902 if ((p_getnameinfo.p=DSO_global_lookup("getnameinfo"))==NULL)
903 p_getnameinfo.p=(void*)-1;
904 }
905 if (p_getnameinfo.p==(void *)-1) break;
906
907 if ((*p_getnameinfo.f)(&sa.from.sa,sa.len.i,h,sizeof(h),s,sizeof(s),
908 NI_NUMERICHOST|NI_NUMERICSERV)) break;
909 nl = strlen(h)+strlen(s)+2;
910 p = *addr;
911 if (p) { *p = '\0'; p = OPENSSL_realloc(p,nl); }
912 else { p = OPENSSL_malloc(nl); }
913 if (p==NULL)
914 {
915 BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
916 goto end;
917 }
918 *addr = p;
919 BIO_snprintf(*addr,nl,"%s:%s",h,s);
920 goto end;
921 } while(0);
922#endif
923 if (sa.from.sa.sa_family != AF_INET) goto end;
924 l=ntohl(sa.from.sa_in.sin_addr.s_addr);
925 port=ntohs(sa.from.sa_in.sin_port);
926 if (*addr == NULL)
927 {
928 if ((p=OPENSSL_malloc(24)) == NULL)
929 {
930 BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
931 goto end;
932 }
933 *addr=p;
934 }
935 BIO_snprintf(*addr,24,"%d.%d.%d.%d:%d",
936 (unsigned char)(l>>24L)&0xff,
937 (unsigned char)(l>>16L)&0xff,
938 (unsigned char)(l>> 8L)&0xff,
939 (unsigned char)(l )&0xff,
940 port);
941end:
942 return(ret);
943 }
944
945int BIO_set_tcp_ndelay(int s, int on)
946 {
947 int ret=0;
948#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
949 int opt;
950
951#ifdef SOL_TCP
952 opt=SOL_TCP;
953#else
954#ifdef IPPROTO_TCP
955 opt=IPPROTO_TCP;
956#endif
957#endif
958
959 ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on));
960#endif
961 return(ret == 0);
962 }
963#endif
964
965int BIO_socket_nbio(int s, int mode)
966 {
967 int ret= -1;
968 int l;
969
970 l=mode;
971#ifdef FIONBIO
972 ret=BIO_socket_ioctl(s,FIONBIO,&l);
973#endif
974 return(ret == 0);
975 }
diff --git a/src/lib/libcrypto/bio/bf_buff.c b/src/lib/libcrypto/bio/bf_buff.c
deleted file mode 100644
index 4b5a132d8a..0000000000
--- a/src/lib/libcrypto/bio/bf_buff.c
+++ /dev/null
@@ -1,512 +0,0 @@
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
64static int buffer_write(BIO *h, const char *buf,int num);
65static int buffer_read(BIO *h, char *buf, int size);
66static int buffer_puts(BIO *h, const char *str);
67static int buffer_gets(BIO *h, char *str, int size);
68static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static int buffer_new(BIO *h);
70static int buffer_free(BIO *data);
71static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72#define DEFAULT_BUFFER_SIZE 4096
73
74static 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
88BIO_METHOD *BIO_f_buffer(void)
89 {
90 return(&methods_buffer);
91 }
92
93static 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
116static 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
131static 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
143start:
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
197static 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);
207start:
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_off+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_off+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
270static 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=0; i<ctx->ibuf_len; i++)
298 {
299 if (p1[ctx->ibuf_off + 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 > 0)
403 {
404 r=BIO_write(b->next_bio,
405 &(ctx->obuf[ctx->obuf_off]),
406 ctx->obuf_len);
407#if 0
408fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len,r);
409#endif
410 BIO_copy_next_retry(b);
411 if (r <= 0) return((long)r);
412 ctx->obuf_off+=r;
413 ctx->obuf_len-=r;
414 }
415 else
416 {
417 ctx->obuf_len=0;
418 ctx->obuf_off=0;
419 ret=1;
420 break;
421 }
422 }
423 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
424 break;
425 case BIO_CTRL_DUP:
426 dbio=(BIO *)ptr;
427 if ( !BIO_set_read_buffer_size(dbio,ctx->ibuf_size) ||
428 !BIO_set_write_buffer_size(dbio,ctx->obuf_size))
429 ret=0;
430 break;
431 default:
432 if (b->next_bio == NULL) return(0);
433 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
434 break;
435 }
436 return(ret);
437malloc_error:
438 BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
439 return(0);
440 }
441
442static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
443 {
444 long ret=1;
445
446 if (b->next_bio == NULL) return(0);
447 switch (cmd)
448 {
449 default:
450 ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
451 break;
452 }
453 return(ret);
454 }
455
456static int buffer_gets(BIO *b, char *buf, int size)
457 {
458 BIO_F_BUFFER_CTX *ctx;
459 int num=0,i,flag;
460 char *p;
461
462 ctx=(BIO_F_BUFFER_CTX *)b->ptr;
463 size--; /* reserve space for a '\0' */
464 BIO_clear_retry_flags(b);
465
466 for (;;)
467 {
468 if (ctx->ibuf_len > 0)
469 {
470 p= &(ctx->ibuf[ctx->ibuf_off]);
471 flag=0;
472 for (i=0; (i<ctx->ibuf_len) && (i<size); i++)
473 {
474 *(buf++)=p[i];
475 if (p[i] == '\n')
476 {
477 flag=1;
478 i++;
479 break;
480 }
481 }
482 num+=i;
483 size-=i;
484 ctx->ibuf_len-=i;
485 ctx->ibuf_off+=i;
486 if (flag || size == 0)
487 {
488 *buf='\0';
489 return(num);
490 }
491 }
492 else /* read another chunk */
493 {
494 i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
495 if (i <= 0)
496 {
497 BIO_copy_next_retry(b);
498 *buf='\0';
499 if (i < 0) return((num > 0)?num:i);
500 if (i == 0) return(num);
501 }
502 ctx->ibuf_len=i;
503 ctx->ibuf_off=0;
504 }
505 }
506 }
507
508static int buffer_puts(BIO *b, const char *str)
509 {
510 return(buffer_write(b,str,strlen(str)));
511 }
512
diff --git a/src/lib/libcrypto/bio/bf_lbuf.c b/src/lib/libcrypto/bio/bf_lbuf.c
deleted file mode 100644
index ec0f7eb0b7..0000000000
--- a/src/lib/libcrypto/bio/bf_lbuf.c
+++ /dev/null
@@ -1,397 +0,0 @@
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
65static int linebuffer_write(BIO *h, const char *buf,int num);
66static int linebuffer_read(BIO *h, char *buf, int size);
67static int linebuffer_puts(BIO *h, const char *str);
68static int linebuffer_gets(BIO *h, char *str, int size);
69static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
70static int linebuffer_new(BIO *h);
71static int linebuffer_free(BIO *data);
72static 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
79static 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
93BIO_METHOD *BIO_f_linebuffer(void)
94 {
95 return(&methods_linebuffer);
96 }
97
98typedef 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
105static 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
122static 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
136static 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
148static 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
204BIO_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
214BIO_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
220BIO_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
233BIO_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
240BIO_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
246BIO_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
266static 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
339fprintf(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);
368malloc_error:
369 BIOerr(BIO_F_LINEBUFFER_CTRL,ERR_R_MALLOC_FAILURE);
370 return(0);
371 }
372
373static 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
387static 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
393static 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
deleted file mode 100644
index 028616c064..0000000000
--- a/src/lib/libcrypto/bio/bf_nbio.c
+++ /dev/null
@@ -1,253 +0,0 @@
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
68static int nbiof_write(BIO *h,const char *buf,int num);
69static int nbiof_read(BIO *h,char *buf,int size);
70static int nbiof_puts(BIO *h,const char *str);
71static int nbiof_gets(BIO *h,char *str,int size);
72static long nbiof_ctrl(BIO *h,int cmd,long arg1,void *arg2);
73static int nbiof_new(BIO *h);
74static int nbiof_free(BIO *data);
75static long nbiof_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
76typedef 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
83static 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
97BIO_METHOD *BIO_f_nbio_test(void)
98 {
99 return(&methods_nbiof);
100 }
101
102static 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
115static 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
126static int nbiof_read(BIO *b, char *out, int outl)
127 {
128 int ret=0;
129#if 1
130 int num;
131 unsigned char n;
132#endif
133
134 if (out == NULL) return(0);
135 if (b->next_bio == NULL) return(0);
136
137 BIO_clear_retry_flags(b);
138#if 1
139 RAND_pseudo_bytes(&n,1);
140 num=(n&0x07);
141
142 if (outl > num) outl=num;
143
144 if (num == 0)
145 {
146 ret= -1;
147 BIO_set_retry_read(b);
148 }
149 else
150#endif
151 {
152 ret=BIO_read(b->next_bio,out,outl);
153 if (ret < 0)
154 BIO_copy_next_retry(b);
155 }
156 return(ret);
157 }
158
159static int nbiof_write(BIO *b, const char *in, int inl)
160 {
161 NBIO_TEST *nt;
162 int ret=0;
163 int num;
164 unsigned char n;
165
166 if ((in == NULL) || (inl <= 0)) return(0);
167 if (b->next_bio == NULL) return(0);
168 nt=(NBIO_TEST *)b->ptr;
169
170 BIO_clear_retry_flags(b);
171
172#if 1
173 if (nt->lwn > 0)
174 {
175 num=nt->lwn;
176 nt->lwn=0;
177 }
178 else
179 {
180 RAND_pseudo_bytes(&n,1);
181 num=(n&7);
182 }
183
184 if (inl > num) inl=num;
185
186 if (num == 0)
187 {
188 ret= -1;
189 BIO_set_retry_write(b);
190 }
191 else
192#endif
193 {
194 ret=BIO_write(b->next_bio,in,inl);
195 if (ret < 0)
196 {
197 BIO_copy_next_retry(b);
198 nt->lwn=inl;
199 }
200 }
201 return(ret);
202 }
203
204static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
205 {
206 long ret;
207
208 if (b->next_bio == NULL) return(0);
209 switch (cmd)
210 {
211 case BIO_C_DO_STATE_MACHINE:
212 BIO_clear_retry_flags(b);
213 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
214 BIO_copy_next_retry(b);
215 break;
216 case BIO_CTRL_DUP:
217 ret=0L;
218 break;
219 default:
220 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
221 break;
222 }
223 return(ret);
224 }
225
226static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
227 {
228 long ret=1;
229
230 if (b->next_bio == NULL) return(0);
231 switch (cmd)
232 {
233 default:
234 ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
235 break;
236 }
237 return(ret);
238 }
239
240static int nbiof_gets(BIO *bp, char *buf, int size)
241 {
242 if (bp->next_bio == NULL) return(0);
243 return(BIO_gets(bp->next_bio,buf,size));
244 }
245
246
247static int nbiof_puts(BIO *bp, const char *str)
248 {
249 if (bp->next_bio == NULL) return(0);
250 return(BIO_puts(bp->next_bio,str));
251 }
252
253
diff --git a/src/lib/libcrypto/bio/bf_null.c b/src/lib/libcrypto/bio/bf_null.c
deleted file mode 100644
index c1bf39a904..0000000000
--- a/src/lib/libcrypto/bio/bf_null.c
+++ /dev/null
@@ -1,183 +0,0 @@
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
67static int nullf_write(BIO *h, const char *buf, int num);
68static int nullf_read(BIO *h, char *buf, int size);
69static int nullf_puts(BIO *h, const char *str);
70static int nullf_gets(BIO *h, char *str, int size);
71static long nullf_ctrl(BIO *h, int cmd, long arg1, void *arg2);
72static int nullf_new(BIO *h);
73static int nullf_free(BIO *data);
74static long nullf_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
75static 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
89BIO_METHOD *BIO_f_null(void)
90 {
91 return(&methods_nullf);
92 }
93
94static int nullf_new(BIO *bi)
95 {
96 bi->init=1;
97 bi->ptr=NULL;
98 bi->flags=0;
99 return(1);
100 }
101
102static 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
111static 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
123static 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
135static 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
156static 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
170static 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
177static 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
deleted file mode 100644
index ab47abcf14..0000000000
--- a/src/lib/libcrypto/bio/bio.h
+++ /dev/null
@@ -1,779 +0,0 @@
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
72extern "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_ASN1 (22|0x0200) /* filter */
99#define BIO_TYPE_COMP (23|0x0200) /* filter */
100
101#define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */
102#define BIO_TYPE_FILTER 0x0200
103#define BIO_TYPE_SOURCE_SINK 0x0400
104
105/* BIO_FILENAME_READ|BIO_CLOSE to open or close on free.
106 * BIO_set_fp(in,stdin,BIO_NOCLOSE); */
107#define BIO_NOCLOSE 0x00
108#define BIO_CLOSE 0x01
109
110/* These are used in the following macros and are passed to
111 * BIO_ctrl() */
112#define BIO_CTRL_RESET 1 /* opt - rewind/zero etc */
113#define BIO_CTRL_EOF 2 /* opt - are we at the eof */
114#define BIO_CTRL_INFO 3 /* opt - extra tit-bits */
115#define BIO_CTRL_SET 4 /* man - set the 'IO' type */
116#define BIO_CTRL_GET 5 /* man - get the 'IO' type */
117#define BIO_CTRL_PUSH 6 /* opt - internal, used to signify change */
118#define BIO_CTRL_POP 7 /* opt - internal, used to signify change */
119#define BIO_CTRL_GET_CLOSE 8 /* man - set the 'close' on free */
120#define BIO_CTRL_SET_CLOSE 9 /* man - set the 'close' on free */
121#define BIO_CTRL_PENDING 10 /* opt - is their more data buffered */
122#define BIO_CTRL_FLUSH 11 /* opt - 'flush' buffered output */
123#define BIO_CTRL_DUP 12 /* man - extra stuff for 'duped' BIO */
124#define BIO_CTRL_WPENDING 13 /* opt - number of bytes still to write */
125/* callback is int cb(BIO *bio,state,ret); */
126#define BIO_CTRL_SET_CALLBACK 14 /* opt - set callback function */
127#define BIO_CTRL_GET_CALLBACK 15 /* opt - set callback function */
128
129#define BIO_CTRL_SET_FILENAME 30 /* BIO_s_file special */
130
131/* dgram BIO stuff */
132#define BIO_CTRL_DGRAM_CONNECT 31 /* BIO dgram special */
133#define BIO_CTRL_DGRAM_SET_CONNECTED 32 /* allow for an externally
134 * connected socket to be
135 * passed in */
136#define BIO_CTRL_DGRAM_SET_RECV_TIMEOUT 33 /* setsockopt, essentially */
137#define BIO_CTRL_DGRAM_GET_RECV_TIMEOUT 34 /* getsockopt, essentially */
138#define BIO_CTRL_DGRAM_SET_SEND_TIMEOUT 35 /* setsockopt, essentially */
139#define BIO_CTRL_DGRAM_GET_SEND_TIMEOUT 36 /* getsockopt, essentially */
140
141#define BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP 37 /* flag whether the last */
142#define BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP 38 /* I/O operation tiemd out */
143
144/* #ifdef IP_MTU_DISCOVER */
145#define BIO_CTRL_DGRAM_MTU_DISCOVER 39 /* set DF bit on egress packets */
146/* #endif */
147
148#define BIO_CTRL_DGRAM_QUERY_MTU 40 /* as kernel for current MTU */
149#define BIO_CTRL_DGRAM_GET_MTU 41 /* get cached value for MTU */
150#define BIO_CTRL_DGRAM_SET_MTU 42 /* set cached value for
151 * MTU. want to use this
152 * if asking the kernel
153 * fails */
154
155#define BIO_CTRL_DGRAM_MTU_EXCEEDED 43 /* check whether the MTU
156 * was exceed in the
157 * previous write
158 * operation */
159
160#define BIO_CTRL_DGRAM_GET_PEER 46
161#define BIO_CTRL_DGRAM_SET_PEER 44 /* Destination for the data */
162
163#define BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT 45 /* Next DTLS handshake timeout to
164 * adjust socket timeouts */
165
166/* modifiers */
167#define BIO_FP_READ 0x02
168#define BIO_FP_WRITE 0x04
169#define BIO_FP_APPEND 0x08
170#define BIO_FP_TEXT 0x10
171
172#define BIO_FLAGS_READ 0x01
173#define BIO_FLAGS_WRITE 0x02
174#define BIO_FLAGS_IO_SPECIAL 0x04
175#define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
176#define BIO_FLAGS_SHOULD_RETRY 0x08
177#ifndef BIO_FLAGS_UPLINK
178/* "UPLINK" flag denotes file descriptors provided by application.
179 It defaults to 0, as most platforms don't require UPLINK interface. */
180#define BIO_FLAGS_UPLINK 0
181#endif
182
183/* Used in BIO_gethostbyname() */
184#define BIO_GHBN_CTRL_HITS 1
185#define BIO_GHBN_CTRL_MISSES 2
186#define BIO_GHBN_CTRL_CACHE_SIZE 3
187#define BIO_GHBN_CTRL_GET_ENTRY 4
188#define BIO_GHBN_CTRL_FLUSH 5
189
190/* Mostly used in the SSL BIO */
191/* Not used anymore
192 * #define BIO_FLAGS_PROTOCOL_DELAYED_READ 0x10
193 * #define BIO_FLAGS_PROTOCOL_DELAYED_WRITE 0x20
194 * #define BIO_FLAGS_PROTOCOL_STARTUP 0x40
195 */
196
197#define BIO_FLAGS_BASE64_NO_NL 0x100
198
199/* This is used with memory BIOs: it means we shouldn't free up or change the
200 * data in any way.
201 */
202#define BIO_FLAGS_MEM_RDONLY 0x200
203
204typedef struct bio_st BIO;
205
206void BIO_set_flags(BIO *b, int flags);
207int BIO_test_flags(const BIO *b, int flags);
208void BIO_clear_flags(BIO *b, int flags);
209
210#define BIO_get_flags(b) BIO_test_flags(b, ~(0x0))
211#define BIO_set_retry_special(b) \
212 BIO_set_flags(b, (BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY))
213#define BIO_set_retry_read(b) \
214 BIO_set_flags(b, (BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY))
215#define BIO_set_retry_write(b) \
216 BIO_set_flags(b, (BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY))
217
218/* These are normally used internally in BIOs */
219#define BIO_clear_retry_flags(b) \
220 BIO_clear_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
221#define BIO_get_retry_flags(b) \
222 BIO_test_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
223
224/* These should be used by the application to tell why we should retry */
225#define BIO_should_read(a) BIO_test_flags(a, BIO_FLAGS_READ)
226#define BIO_should_write(a) BIO_test_flags(a, BIO_FLAGS_WRITE)
227#define BIO_should_io_special(a) BIO_test_flags(a, BIO_FLAGS_IO_SPECIAL)
228#define BIO_retry_type(a) BIO_test_flags(a, BIO_FLAGS_RWS)
229#define BIO_should_retry(a) BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY)
230
231/* The next three are used in conjunction with the
232 * BIO_should_io_special() condition. After this returns true,
233 * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO
234 * stack and return the 'reason' for the special and the offending BIO.
235 * Given a BIO, BIO_get_retry_reason(bio) will return the code. */
236/* Returned from the SSL bio when the certificate retrieval code had an error */
237#define BIO_RR_SSL_X509_LOOKUP 0x01
238/* Returned from the connect BIO when a connect would have blocked */
239#define BIO_RR_CONNECT 0x02
240/* Returned from the accept BIO when an accept would have blocked */
241#define BIO_RR_ACCEPT 0x03
242
243/* These are passed by the BIO callback */
244#define BIO_CB_FREE 0x01
245#define BIO_CB_READ 0x02
246#define BIO_CB_WRITE 0x03
247#define BIO_CB_PUTS 0x04
248#define BIO_CB_GETS 0x05
249#define BIO_CB_CTRL 0x06
250
251/* The callback is called before and after the underling operation,
252 * The BIO_CB_RETURN flag indicates if it is after the call */
253#define BIO_CB_RETURN 0x80
254#define BIO_CB_return(a) ((a)|BIO_CB_RETURN))
255#define BIO_cb_pre(a) (!((a)&BIO_CB_RETURN))
256#define BIO_cb_post(a) ((a)&BIO_CB_RETURN)
257
258long (*BIO_get_callback(const BIO *b)) (struct bio_st *,int,const char *,int, long,long);
259void BIO_set_callback(BIO *b,
260 long (*callback)(struct bio_st *,int,const char *,int, long,long));
261char *BIO_get_callback_arg(const BIO *b);
262void BIO_set_callback_arg(BIO *b, char *arg);
263
264const char * BIO_method_name(const BIO *b);
265int BIO_method_type(const BIO *b);
266
267typedef void bio_info_cb(struct bio_st *, int, const char *, int, long, long);
268
269typedef struct bio_method_st
270 {
271 int type;
272 const char *name;
273 int (*bwrite)(BIO *, const char *, int);
274 int (*bread)(BIO *, char *, int);
275 int (*bputs)(BIO *, const char *);
276 int (*bgets)(BIO *, char *, int);
277 long (*ctrl)(BIO *, int, long, void *);
278 int (*create)(BIO *);
279 int (*destroy)(BIO *);
280 long (*callback_ctrl)(BIO *, int, bio_info_cb *);
281 } BIO_METHOD;
282
283struct bio_st
284 {
285 BIO_METHOD *method;
286 /* bio, mode, argp, argi, argl, ret */
287 long (*callback)(struct bio_st *,int,const char *,int, long,long);
288 char *cb_arg; /* first argument for the callback */
289
290 int init;
291 int shutdown;
292 int flags; /* extra storage */
293 int retry_reason;
294 int num;
295 void *ptr;
296 struct bio_st *next_bio; /* used by filter BIOs */
297 struct bio_st *prev_bio; /* used by filter BIOs */
298 int references;
299 unsigned long num_read;
300 unsigned long num_write;
301
302 CRYPTO_EX_DATA ex_data;
303 };
304
305DECLARE_STACK_OF(BIO)
306
307typedef struct bio_f_buffer_ctx_struct
308 {
309 /* Buffers are setup like this:
310 *
311 * <---------------------- size ----------------------->
312 * +---------------------------------------------------+
313 * | consumed | remaining | free space |
314 * +---------------------------------------------------+
315 * <-- off --><------- len ------->
316 */
317
318 /* BIO *bio; */ /* this is now in the BIO struct */
319 int ibuf_size; /* how big is the input buffer */
320 int obuf_size; /* how big is the output buffer */
321
322 char *ibuf; /* the char array */
323 int ibuf_len; /* how many bytes are in it */
324 int ibuf_off; /* write/read offset */
325
326 char *obuf; /* the char array */
327 int obuf_len; /* how many bytes are in it */
328 int obuf_off; /* write/read offset */
329 } BIO_F_BUFFER_CTX;
330
331/* Prefix and suffix callback in ASN1 BIO */
332typedef int asn1_ps_func(BIO *b, unsigned char **pbuf, int *plen, void *parg);
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#define BIO_C_SET_PREFIX 149
397#define BIO_C_GET_PREFIX 150
398#define BIO_C_SET_SUFFIX 151
399#define BIO_C_GET_SUFFIX 152
400
401#define BIO_C_SET_EX_ARG 153
402#define BIO_C_GET_EX_ARG 154
403
404#define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,arg)
405#define BIO_get_app_data(s) BIO_get_ex_data(s,0)
406
407/* BIO_s_connect() and BIO_s_socks4a_connect() */
408#define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name)
409#define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port)
410#define BIO_set_conn_ip(b,ip) BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip)
411#define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port)
412#define BIO_get_conn_hostname(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0)
413#define BIO_get_conn_port(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1)
414#define BIO_get_conn_ip(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,2)
415#define BIO_get_conn_int_port(b) BIO_int_ctrl(b,BIO_C_GET_CONNECT,3,0)
416
417
418#define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL)
419
420/* BIO_s_accept_socket() */
421#define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name)
422#define BIO_get_accept_port(b) BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0)
423/* #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */
424#define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?(void *)"a":NULL)
425#define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio)
426
427#define BIO_BIND_NORMAL 0
428#define BIO_BIND_REUSEADDR_IF_UNUSED 1
429#define BIO_BIND_REUSEADDR 2
430#define BIO_set_bind_mode(b,mode) BIO_ctrl(b,BIO_C_SET_BIND_MODE,mode,NULL)
431#define BIO_get_bind_mode(b,mode) BIO_ctrl(b,BIO_C_GET_BIND_MODE,0,NULL)
432
433#define BIO_do_connect(b) BIO_do_handshake(b)
434#define BIO_do_accept(b) BIO_do_handshake(b)
435#define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
436
437/* BIO_s_proxy_client() */
438#define BIO_set_url(b,url) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,0,(char *)(url))
439#define BIO_set_proxies(b,p) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,1,(char *)(p))
440/* BIO_set_nbio(b,n) */
441#define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s))
442/* BIO *BIO_get_filter_bio(BIO *bio); */
443#define BIO_set_proxy_cb(b,cb) BIO_callback_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(void *(*cb)()))
444#define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk)
445#define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool)
446
447#define BIO_get_proxy_header(b,skp) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,0,(char *)skp)
448#define BIO_get_proxies(b,pxy_p) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,1,(char *)(pxy_p))
449#define BIO_get_url(b,url) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,2,(char *)(url))
450#define BIO_get_no_connect_return(b) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,5,NULL)
451
452#define BIO_set_fd(b,fd,c) BIO_int_ctrl(b,BIO_C_SET_FD,c,fd)
453#define BIO_get_fd(b,c) BIO_ctrl(b,BIO_C_GET_FD,0,(char *)c)
454
455#define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp)
456#define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp)
457
458#define BIO_seek(b,ofs) (int)BIO_ctrl(b,BIO_C_FILE_SEEK,ofs,NULL)
459#define BIO_tell(b) (int)BIO_ctrl(b,BIO_C_FILE_TELL,0,NULL)
460
461/* name is cast to lose const, but might be better to route through a function
462 so we can do it safely */
463#ifdef CONST_STRICT
464/* If you are wondering why this isn't defined, its because CONST_STRICT is
465 * purely a compile-time kludge to allow const to be checked.
466 */
467int BIO_read_filename(BIO *b,const char *name);
468#else
469#define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
470 BIO_CLOSE|BIO_FP_READ,(char *)name)
471#endif
472#define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
473 BIO_CLOSE|BIO_FP_WRITE,name)
474#define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
475 BIO_CLOSE|BIO_FP_APPEND,name)
476#define BIO_rw_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
477 BIO_CLOSE|BIO_FP_READ|BIO_FP_WRITE,name)
478
479/* WARNING WARNING, this ups the reference count on the read bio of the
480 * SSL structure. This is because the ssl read BIO is now pointed to by
481 * the next_bio field in the bio. So when you free the BIO, make sure
482 * you are doing a BIO_free_all() to catch the underlying BIO. */
483#define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
484#define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
485#define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
486#define BIO_set_ssl_renegotiate_bytes(b,num) \
487 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
488#define BIO_get_num_renegotiates(b) \
489 BIO_ctrl(b,BIO_C_GET_SSL_NUM_RENEGOTIATES,0,NULL);
490#define BIO_set_ssl_renegotiate_timeout(b,seconds) \
491 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
492
493/* defined in evp.h */
494/* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */
495
496#define BIO_get_mem_data(b,pp) BIO_ctrl(b,BIO_CTRL_INFO,0,(char *)pp)
497#define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm)
498#define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp)
499#define BIO_set_mem_eof_return(b,v) \
500 BIO_ctrl(b,BIO_C_SET_BUF_MEM_EOF_RETURN,v,NULL)
501
502/* For the BIO_f_buffer() type */
503#define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL)
504#define BIO_set_buffer_size(b,size) BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL)
505#define BIO_set_read_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,0)
506#define BIO_set_write_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,1)
507#define BIO_set_buffer_read_data(b,buf,num) BIO_ctrl(b,BIO_C_SET_BUFF_READ_DATA,num,buf)
508
509/* Don't use the next one unless you know what you are doing :-) */
510#define BIO_dup_state(b,ret) BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)(ret))
511
512#define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL)
513#define BIO_eof(b) (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL)
514#define BIO_set_close(b,c) (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL)
515#define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL)
516#define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL)
517#define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL)
518/* ...pending macros have inappropriate return type */
519size_t BIO_ctrl_pending(BIO *b);
520size_t BIO_ctrl_wpending(BIO *b);
521#define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL)
522#define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0, \
523 cbp)
524#define BIO_set_info_callback(b,cb) (int)BIO_callback_ctrl(b,BIO_CTRL_SET_CALLBACK,cb)
525
526/* For the BIO_f_buffer() type */
527#define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL)
528
529/* For BIO_s_bio() */
530#define BIO_set_write_buf_size(b,size) (int)BIO_ctrl(b,BIO_C_SET_WRITE_BUF_SIZE,size,NULL)
531#define BIO_get_write_buf_size(b,size) (size_t)BIO_ctrl(b,BIO_C_GET_WRITE_BUF_SIZE,size,NULL)
532#define BIO_make_bio_pair(b1,b2) (int)BIO_ctrl(b1,BIO_C_MAKE_BIO_PAIR,0,b2)
533#define BIO_destroy_bio_pair(b) (int)BIO_ctrl(b,BIO_C_DESTROY_BIO_PAIR,0,NULL)
534#define BIO_shutdown_wr(b) (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL)
535/* macros with inappropriate type -- but ...pending macros use int too: */
536#define BIO_get_write_guarantee(b) (int)BIO_ctrl(b,BIO_C_GET_WRITE_GUARANTEE,0,NULL)
537#define BIO_get_read_request(b) (int)BIO_ctrl(b,BIO_C_GET_READ_REQUEST,0,NULL)
538size_t BIO_ctrl_get_write_guarantee(BIO *b);
539size_t BIO_ctrl_get_read_request(BIO *b);
540int BIO_ctrl_reset_read_request(BIO *b);
541
542/* ctrl macros for dgram */
543#define BIO_ctrl_dgram_connect(b,peer) \
544 (int)BIO_ctrl(b,BIO_CTRL_DGRAM_CONNECT,0, (char *)peer)
545#define BIO_ctrl_set_connected(b, state, peer) \
546 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_CONNECTED, state, (char *)peer)
547#define BIO_dgram_recv_timedout(b) \
548 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP, 0, NULL)
549#define BIO_dgram_send_timedout(b) \
550 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP, 0, NULL)
551#define BIO_dgram_get_peer(b,peer) \
552 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_PEER, 0, (char *)peer)
553#define BIO_dgram_set_peer(b,peer) \
554 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, (char *)peer)
555
556/* These two aren't currently implemented */
557/* int BIO_get_ex_num(BIO *bio); */
558/* void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)()); */
559int BIO_set_ex_data(BIO *bio,int idx,void *data);
560void *BIO_get_ex_data(BIO *bio,int idx);
561int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
562 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
563unsigned long BIO_number_read(BIO *bio);
564unsigned long BIO_number_written(BIO *bio);
565
566/* For BIO_f_asn1() */
567int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
568 asn1_ps_func *prefix_free);
569int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
570 asn1_ps_func **pprefix_free);
571int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
572 asn1_ps_func *suffix_free);
573int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
574 asn1_ps_func **psuffix_free);
575
576# ifndef OPENSSL_NO_FP_API
577BIO_METHOD *BIO_s_file(void );
578BIO *BIO_new_file(const char *filename, const char *mode);
579BIO *BIO_new_fp(FILE *stream, int close_flag);
580# define BIO_s_file_internal BIO_s_file
581# endif
582BIO * BIO_new(BIO_METHOD *type);
583int BIO_set(BIO *a,BIO_METHOD *type);
584int BIO_free(BIO *a);
585void BIO_vfree(BIO *a);
586int BIO_read(BIO *b, void *data, int len);
587int BIO_gets(BIO *bp,char *buf, int size);
588int BIO_write(BIO *b, const void *data, int len);
589int BIO_puts(BIO *bp,const char *buf);
590int BIO_indent(BIO *b,int indent,int max);
591long BIO_ctrl(BIO *bp,int cmd,long larg,void *parg);
592long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long));
593char * BIO_ptr_ctrl(BIO *bp,int cmd,long larg);
594long BIO_int_ctrl(BIO *bp,int cmd,long larg,int iarg);
595BIO * BIO_push(BIO *b,BIO *append);
596BIO * BIO_pop(BIO *b);
597void BIO_free_all(BIO *a);
598BIO * BIO_find_type(BIO *b,int bio_type);
599BIO * BIO_next(BIO *b);
600BIO * BIO_get_retry_BIO(BIO *bio, int *reason);
601int BIO_get_retry_reason(BIO *bio);
602BIO * BIO_dup_chain(BIO *in);
603
604int BIO_nread0(BIO *bio, char **buf);
605int BIO_nread(BIO *bio, char **buf, int num);
606int BIO_nwrite0(BIO *bio, char **buf);
607int BIO_nwrite(BIO *bio, char **buf, int num);
608
609long BIO_debug_callback(BIO *bio,int cmd,const char *argp,int argi,
610 long argl,long ret);
611
612BIO_METHOD *BIO_s_mem(void);
613BIO *BIO_new_mem_buf(void *buf, int len);
614BIO_METHOD *BIO_s_socket(void);
615BIO_METHOD *BIO_s_connect(void);
616BIO_METHOD *BIO_s_accept(void);
617BIO_METHOD *BIO_s_fd(void);
618#ifndef OPENSSL_SYS_OS2
619BIO_METHOD *BIO_s_log(void);
620#endif
621BIO_METHOD *BIO_s_bio(void);
622BIO_METHOD *BIO_s_null(void);
623BIO_METHOD *BIO_f_null(void);
624BIO_METHOD *BIO_f_buffer(void);
625#ifdef OPENSSL_SYS_VMS
626BIO_METHOD *BIO_f_linebuffer(void);
627#endif
628BIO_METHOD *BIO_f_nbio_test(void);
629#ifndef OPENSSL_NO_DGRAM
630BIO_METHOD *BIO_s_datagram(void);
631#endif
632
633/* BIO_METHOD *BIO_f_ber(void); */
634
635int BIO_sock_should_retry(int i);
636int BIO_sock_non_fatal_error(int error);
637int BIO_dgram_non_fatal_error(int error);
638
639int BIO_fd_should_retry(int i);
640int BIO_fd_non_fatal_error(int error);
641int BIO_dump_cb(int (*cb)(const void *data, size_t len, void *u),
642 void *u, const char *s, int len);
643int BIO_dump_indent_cb(int (*cb)(const void *data, size_t len, void *u),
644 void *u, const char *s, int len, int indent);
645int BIO_dump(BIO *b,const char *bytes,int len);
646int BIO_dump_indent(BIO *b,const char *bytes,int len,int indent);
647#ifndef OPENSSL_NO_FP_API
648int BIO_dump_fp(FILE *fp, const char *s, int len);
649int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent);
650#endif
651struct hostent *BIO_gethostbyname(const char *name);
652/* We might want a thread-safe interface too:
653 * struct hostent *BIO_gethostbyname_r(const char *name,
654 * struct hostent *result, void *buffer, size_t buflen);
655 * or something similar (caller allocates a struct hostent,
656 * pointed to by "result", and additional buffer space for the various
657 * substructures; if the buffer does not suffice, NULL is returned
658 * and an appropriate error code is set).
659 */
660int BIO_sock_error(int sock);
661int BIO_socket_ioctl(int fd, long type, void *arg);
662int BIO_socket_nbio(int fd,int mode);
663int BIO_get_port(const char *str, unsigned short *port_ptr);
664int BIO_get_host_ip(const char *str, unsigned char *ip);
665int BIO_get_accept_socket(char *host_port,int mode);
666int BIO_accept(int sock,char **ip_port);
667int BIO_sock_init(void );
668void BIO_sock_cleanup(void);
669int BIO_set_tcp_ndelay(int sock,int turn_on);
670
671BIO *BIO_new_socket(int sock, int close_flag);
672BIO *BIO_new_dgram(int fd, int close_flag);
673BIO *BIO_new_fd(int fd, int close_flag);
674BIO *BIO_new_connect(char *host_port);
675BIO *BIO_new_accept(char *host_port);
676
677int BIO_new_bio_pair(BIO **bio1, size_t writebuf1,
678 BIO **bio2, size_t writebuf2);
679/* If successful, returns 1 and in *bio1, *bio2 two BIO pair endpoints.
680 * Otherwise returns 0 and sets *bio1 and *bio2 to NULL.
681 * Size 0 uses default value.
682 */
683
684void BIO_copy_next_retry(BIO *b);
685
686/*long BIO_ghbn_ctrl(int cmd,int iarg,char *parg);*/
687
688#ifdef __GNUC__
689# define __bio_h__attr__ __attribute__
690#else
691# define __bio_h__attr__(x)
692#endif
693int BIO_printf(BIO *bio, const char *format, ...)
694 __bio_h__attr__((__format__(__printf__,2,3)));
695int BIO_vprintf(BIO *bio, const char *format, va_list args)
696 __bio_h__attr__((__format__(__printf__,2,0)));
697int BIO_snprintf(char *buf, size_t n, const char *format, ...)
698 __bio_h__attr__((__format__(__printf__,3,4)));
699int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
700 __bio_h__attr__((__format__(__printf__,3,0)));
701#undef __bio_h__attr__
702
703/* BEGIN ERROR CODES */
704/* The following lines are auto generated by the script mkerr.pl. Any changes
705 * made after this point may be overwritten when the script is next run.
706 */
707void ERR_load_BIO_strings(void);
708
709/* Error codes for the BIO functions. */
710
711/* Function codes. */
712#define BIO_F_ACPT_STATE 100
713#define BIO_F_BIO_ACCEPT 101
714#define BIO_F_BIO_BER_GET_HEADER 102
715#define BIO_F_BIO_CALLBACK_CTRL 131
716#define BIO_F_BIO_CTRL 103
717#define BIO_F_BIO_GETHOSTBYNAME 120
718#define BIO_F_BIO_GETS 104
719#define BIO_F_BIO_GET_ACCEPT_SOCKET 105
720#define BIO_F_BIO_GET_HOST_IP 106
721#define BIO_F_BIO_GET_PORT 107
722#define BIO_F_BIO_MAKE_PAIR 121
723#define BIO_F_BIO_NEW 108
724#define BIO_F_BIO_NEW_FILE 109
725#define BIO_F_BIO_NEW_MEM_BUF 126
726#define BIO_F_BIO_NREAD 123
727#define BIO_F_BIO_NREAD0 124
728#define BIO_F_BIO_NWRITE 125
729#define BIO_F_BIO_NWRITE0 122
730#define BIO_F_BIO_PUTS 110
731#define BIO_F_BIO_READ 111
732#define BIO_F_BIO_SOCK_INIT 112
733#define BIO_F_BIO_WRITE 113
734#define BIO_F_BUFFER_CTRL 114
735#define BIO_F_CONN_CTRL 127
736#define BIO_F_CONN_STATE 115
737#define BIO_F_FILE_CTRL 116
738#define BIO_F_FILE_READ 130
739#define BIO_F_LINEBUFFER_CTRL 129
740#define BIO_F_MEM_READ 128
741#define BIO_F_MEM_WRITE 117
742#define BIO_F_SSL_NEW 118
743#define BIO_F_WSASTARTUP 119
744
745/* Reason codes. */
746#define BIO_R_ACCEPT_ERROR 100
747#define BIO_R_BAD_FOPEN_MODE 101
748#define BIO_R_BAD_HOSTNAME_LOOKUP 102
749#define BIO_R_BROKEN_PIPE 124
750#define BIO_R_CONNECT_ERROR 103
751#define BIO_R_EOF_ON_MEMORY_BIO 127
752#define BIO_R_ERROR_SETTING_NBIO 104
753#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105
754#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106
755#define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107
756#define BIO_R_INVALID_ARGUMENT 125
757#define BIO_R_INVALID_IP_ADDRESS 108
758#define BIO_R_IN_USE 123
759#define BIO_R_KEEPALIVE 109
760#define BIO_R_NBIO_CONNECT_ERROR 110
761#define BIO_R_NO_ACCEPT_PORT_SPECIFIED 111
762#define BIO_R_NO_HOSTNAME_SPECIFIED 112
763#define BIO_R_NO_PORT_DEFINED 113
764#define BIO_R_NO_PORT_SPECIFIED 114
765#define BIO_R_NO_SUCH_FILE 128
766#define BIO_R_NULL_PARAMETER 115
767#define BIO_R_TAG_MISMATCH 116
768#define BIO_R_UNABLE_TO_BIND_SOCKET 117
769#define BIO_R_UNABLE_TO_CREATE_SOCKET 118
770#define BIO_R_UNABLE_TO_LISTEN_SOCKET 119
771#define BIO_R_UNINITIALIZED 120
772#define BIO_R_UNSUPPORTED_METHOD 121
773#define BIO_R_WRITE_TO_READ_ONLY_BIO 126
774#define BIO_R_WSASTARTUP 122
775
776#ifdef __cplusplus
777}
778#endif
779#endif
diff --git a/src/lib/libcrypto/bio/bio_cb.c b/src/lib/libcrypto/bio/bio_cb.c
deleted file mode 100644
index 9bcbc321d9..0000000000
--- a/src/lib/libcrypto/bio/bio_cb.c
+++ /dev/null
@@ -1,143 +0,0 @@
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
66long 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,%lu) - %s fd=%d\n",
89 bio->num,(unsigned long)argi,
90 bio->method->name,bio->num);
91 else
92 BIO_snprintf(p,p_maxlen,"read(%d,%lu) - %s\n",
93 bio->num,(unsigned long)argi,
94 bio->method->name);
95 break;
96 case BIO_CB_WRITE:
97 if (bio->method->type & BIO_TYPE_DESCRIPTOR)
98 BIO_snprintf(p,p_maxlen,"write(%d,%lu) - %s fd=%d\n",
99 bio->num,(unsigned long)argi,
100 bio->method->name,bio->num);
101 else
102 BIO_snprintf(p,p_maxlen,"write(%d,%lu) - %s\n",
103 bio->num,(unsigned long)argi,
104 bio->method->name);
105 break;
106 case BIO_CB_PUTS:
107 BIO_snprintf(p,p_maxlen,"puts() - %s\n",bio->method->name);
108 break;
109 case BIO_CB_GETS:
110 BIO_snprintf(p,p_maxlen,"gets(%lu) - %s\n",(unsigned long)argi,bio->method->name);
111 break;
112 case BIO_CB_CTRL:
113 BIO_snprintf(p,p_maxlen,"ctrl(%lu) - %s\n",(unsigned long)argi,bio->method->name);
114 break;
115 case BIO_CB_RETURN|BIO_CB_READ:
116 BIO_snprintf(p,p_maxlen,"read return %ld\n",ret);
117 break;
118 case BIO_CB_RETURN|BIO_CB_WRITE:
119 BIO_snprintf(p,p_maxlen,"write return %ld\n",ret);
120 break;
121 case BIO_CB_RETURN|BIO_CB_GETS:
122 BIO_snprintf(p,p_maxlen,"gets return %ld\n",ret);
123 break;
124 case BIO_CB_RETURN|BIO_CB_PUTS:
125 BIO_snprintf(p,p_maxlen,"puts return %ld\n",ret);
126 break;
127 case BIO_CB_RETURN|BIO_CB_CTRL:
128 BIO_snprintf(p,p_maxlen,"ctrl return %ld\n",ret);
129 break;
130 default:
131 BIO_snprintf(p,p_maxlen,"bio callback - unknown type (%d)\n",cmd);
132 break;
133 }
134
135 b=(BIO *)bio->cb_arg;
136 if (b != NULL)
137 BIO_write(b,buf,strlen(buf));
138#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16)
139 else
140 fputs(buf,stderr);
141#endif
142 return(r);
143 }
diff --git a/src/lib/libcrypto/bio/bio_err.c b/src/lib/libcrypto/bio/bio_err.c
deleted file mode 100644
index a224edd5a0..0000000000
--- a/src/lib/libcrypto/bio/bio_err.c
+++ /dev/null
@@ -1,154 +0,0 @@
1/* crypto/bio/bio_err.c */
2/* ====================================================================
3 * Copyright (c) 1999-2006 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
71static 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
108static 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
144void 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
deleted file mode 100644
index e12bc3a2ca..0000000000
--- a/src/lib/libcrypto/bio/bio_lib.c
+++ /dev/null
@@ -1,602 +0,0 @@
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
66BIO *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
84int 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
111int BIO_free(BIO *a)
112 {
113 int 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 a->method->destroy(a);
137 OPENSSL_free(a);
138 return(1);
139 }
140
141void BIO_vfree(BIO *a)
142 { BIO_free(a); }
143
144void BIO_clear_flags(BIO *b, int flags)
145 {
146 b->flags &= ~flags;
147 }
148
149int BIO_test_flags(const BIO *b, int flags)
150 {
151 return (b->flags & flags);
152 }
153
154void BIO_set_flags(BIO *b, int flags)
155 {
156 b->flags |= flags;
157 }
158
159long (*BIO_get_callback(const BIO *b))(struct bio_st *,int,const char *,int, long,long)
160 {
161 return b->callback;
162 }
163
164void BIO_set_callback(BIO *b, long (*cb)(struct bio_st *,int,const char *,int, long,long))
165 {
166 b->callback = cb;
167 }
168
169void BIO_set_callback_arg(BIO *b, char *arg)
170 {
171 b->cb_arg = arg;
172 }
173
174char * BIO_get_callback_arg(const BIO *b)
175 {
176 return b->cb_arg;
177 }
178
179const char * BIO_method_name(const BIO *b)
180 {
181 return b->method->name;
182 }
183
184int BIO_method_type(const BIO *b)
185 {
186 return b->method->type;
187 }
188
189
190int 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
222int 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
257int 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
290int 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
321int 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
333long 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
341char *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
351long 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
378long 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. */
408size_t BIO_ctrl_pending(BIO *bio)
409 {
410 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
411 }
412
413size_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 */
420BIO *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,lb);
433 return(b);
434 }
435
436/* Remove the first and return the rest */
437BIO *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,b);
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
456BIO *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
472int BIO_get_retry_reason(BIO *bio)
473 {
474 return(bio->retry_reason);
475 }
476
477BIO *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
500BIO *BIO_next(BIO *b)
501 {
502 if(!b) return NULL;
503 return b->next_bio;
504 }
505
506void 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
522BIO *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);
561err:
562 if (ret != NULL)
563 BIO_free(ret);
564 return(NULL);
565 }
566
567void 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
573int 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
580int BIO_set_ex_data(BIO *bio, int idx, void *data)
581 {
582 return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
583 }
584
585void *BIO_get_ex_data(BIO *bio, int idx)
586 {
587 return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
588 }
589
590unsigned long BIO_number_read(BIO *bio)
591{
592 if(bio) return bio->num_read;
593 return 0;
594}
595
596unsigned long BIO_number_written(BIO *bio)
597{
598 if(bio) return bio->num_write;
599 return 0;
600}
601
602IMPLEMENT_STACK_OF(BIO)
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c
deleted file mode 100644
index 5d49e1a72b..0000000000
--- a/src/lib/libcrypto/bio/bss_acpt.c
+++ /dev/null
@@ -1,478 +0,0 @@
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
78typedef 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
95static int acpt_write(BIO *h, const char *buf, int num);
96static int acpt_read(BIO *h, char *buf, int size);
97static int acpt_puts(BIO *h, const char *str);
98static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
99static int acpt_new(BIO *h);
100static int acpt_free(BIO *data);
101static int acpt_state(BIO *b, BIO_ACCEPT *c);
102static void acpt_close_socket(BIO *data);
103static BIO_ACCEPT *BIO_ACCEPT_new(void );
104static 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
110static 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
124BIO_METHOD *BIO_s_accept(void)
125 {
126 return(&methods_acceptp);
127 }
128
129static 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
144static 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
157static 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
168static 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
182static 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
200static int acpt_state(BIO *b, BIO_ACCEPT *c)
201 {
202 BIO *bio=NULL,*dbio;
203 int s= -1;
204 int i;
205
206again:
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);
281err:
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
303static 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
322static 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
341static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
342 {
343 int *ip;
344 long ret=1;
345 BIO_ACCEPT *data;
346 char **pp;
347
348 data=(BIO_ACCEPT *)b->ptr;
349
350 switch (cmd)
351 {
352 case BIO_CTRL_RESET:
353 ret=0;
354 data->state=ACPT_S_BEFORE;
355 acpt_close_socket(b);
356 b->flags=0;
357 break;
358 case BIO_C_DO_STATE_MACHINE:
359 /* use this one to start the connection */
360 ret=(long)acpt_state(b,data);
361 break;
362 case BIO_C_SET_ACCEPT:
363 if (ptr != NULL)
364 {
365 if (num == 0)
366 {
367 b->init=1;
368 if (data->param_addr != NULL)
369 OPENSSL_free(data->param_addr);
370 data->param_addr=BUF_strdup(ptr);
371 }
372 else if (num == 1)
373 {
374 data->accept_nbio=(ptr != NULL);
375 }
376 else if (num == 2)
377 {
378 if (data->bio_chain != NULL)
379 BIO_free(data->bio_chain);
380 data->bio_chain=(BIO *)ptr;
381 }
382 }
383 break;
384 case BIO_C_SET_NBIO:
385 data->nbio=(int)num;
386 break;
387 case BIO_C_SET_FD:
388 b->init=1;
389 b->num= *((int *)ptr);
390 data->accept_sock=b->num;
391 data->state=ACPT_S_GET_ACCEPT_SOCKET;
392 b->shutdown=(int)num;
393 b->init=1;
394 break;
395 case BIO_C_GET_FD:
396 if (b->init)
397 {
398 ip=(int *)ptr;
399 if (ip != NULL)
400 *ip=data->accept_sock;
401 ret=data->accept_sock;
402 }
403 else
404 ret= -1;
405 break;
406 case BIO_C_GET_ACCEPT:
407 if (b->init)
408 {
409 if (ptr != NULL)
410 {
411 pp=(char **)ptr;
412 *pp=data->param_addr;
413 }
414 else
415 ret= -1;
416 }
417 else
418 ret= -1;
419 break;
420 case BIO_CTRL_GET_CLOSE:
421 ret=b->shutdown;
422 break;
423 case BIO_CTRL_SET_CLOSE:
424 b->shutdown=(int)num;
425 break;
426 case BIO_CTRL_PENDING:
427 case BIO_CTRL_WPENDING:
428 ret=0;
429 break;
430 case BIO_CTRL_FLUSH:
431 break;
432 case BIO_C_SET_BIND_MODE:
433 data->bind_mode=(int)num;
434 break;
435 case BIO_C_GET_BIND_MODE:
436 ret=(long)data->bind_mode;
437 break;
438 case BIO_CTRL_DUP:
439/* dbio=(BIO *)ptr;
440 if (data->param_port) EAY EAY
441 BIO_set_port(dbio,data->param_port);
442 if (data->param_hostname)
443 BIO_set_hostname(dbio,data->param_hostname);
444 BIO_set_nbio(dbio,data->nbio); */
445 break;
446
447 default:
448 ret=0;
449 break;
450 }
451 return(ret);
452 }
453
454static int acpt_puts(BIO *bp, const char *str)
455 {
456 int n,ret;
457
458 n=strlen(str);
459 ret=acpt_write(bp,str,n);
460 return(ret);
461 }
462
463BIO *BIO_new_accept(char *str)
464 {
465 BIO *ret;
466
467 ret=BIO_new(BIO_s_accept());
468 if (ret == NULL) return(NULL);
469 if (BIO_set_accept_port(ret,str))
470 return(ret);
471 else
472 {
473 BIO_free(ret);
474 return(NULL);
475 }
476 }
477
478#endif
diff --git a/src/lib/libcrypto/bio/bss_bio.c b/src/lib/libcrypto/bio/bss_bio.c
deleted file mode 100644
index 76bd48e767..0000000000
--- a/src/lib/libcrypto/bio/bss_bio.c
+++ /dev/null
@@ -1,924 +0,0 @@
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
96static int bio_new(BIO *bio);
97static int bio_free(BIO *bio);
98static int bio_read(BIO *bio, char *buf, int size);
99static int bio_write(BIO *bio, const char *buf, int num);
100static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
101static int bio_puts(BIO *bio, const char *str);
102
103static int bio_make_pair(BIO *bio1, BIO *bio2);
104static void bio_destroy_pair(BIO *bio);
105
106static 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
120BIO_METHOD *BIO_s_bio(void)
121 {
122 return &methods_biop;
123 }
124
125struct 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
145static 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
162static 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
187static 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. */
280static 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
318static 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
352static 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 */
431static 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
479static 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
503static 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
698static int bio_puts(BIO *bio, const char *str)
699 {
700 return bio_write(bio, str, strlen(str));
701 }
702
703
704static 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
757static 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 */
789int 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
841size_t BIO_ctrl_get_write_guarantee(BIO *bio)
842 {
843 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
844 }
845
846size_t BIO_ctrl_get_read_request(BIO *bio)
847 {
848 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
849 }
850
851int 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 */
860int 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
877int 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
893int 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
910int 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_write += ret;
923 return ret;
924 }
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c
deleted file mode 100644
index c14727855b..0000000000
--- a/src/lib/libcrypto/bio/bss_conn.c
+++ /dev/null
@@ -1,652 +0,0 @@
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
79typedef 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
101static int conn_write(BIO *h, const char *buf, int num);
102static int conn_read(BIO *h, char *buf, int size);
103static int conn_puts(BIO *h, const char *str);
104static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
105static int conn_new(BIO *h);
106static int conn_free(BIO *data);
107static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
108
109static int conn_state(BIO *b, BIO_CONNECT *c);
110static void conn_close_socket(BIO *data);
111BIO_CONNECT *BIO_CONNECT_new(void );
112void BIO_CONNECT_free(BIO_CONNECT *a);
113
114static 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
128static 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 */
314exit_loop:
315 if (cb != NULL)
316 ret=cb((BIO *)b,c->state,ret);
317end:
318 return(ret);
319 }
320
321BIO_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
341void 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
353BIO_METHOD *BIO_s_connect(void)
354 {
355 return(&methods_connectp);
356 }
357
358static 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
369static 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
384static 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
402static 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
429static 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
452static 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
606static 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
627static 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
636BIO *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
deleted file mode 100644
index 71ebe987b6..0000000000
--- a/src/lib/libcrypto/bio/bss_dgram.c
+++ /dev/null
@@ -1,836 +0,0 @@
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
61#include <stdio.h>
62#include <errno.h>
63#define USE_SOCKETS
64#include "cryptlib.h"
65
66#include <openssl/bio.h>
67#ifndef OPENSSL_NO_DGRAM
68
69#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
70#include <sys/timeb.h>
71#endif
72
73#ifdef OPENSSL_SYS_LINUX
74#define IP_MTU 14 /* linux is lame */
75#endif
76
77#ifdef WATT32
78#define sock_write SockWrite /* Watt-32 uses same names */
79#define sock_read SockRead
80#define sock_puts SockPuts
81#endif
82
83static int dgram_write(BIO *h, const char *buf, int num);
84static int dgram_read(BIO *h, char *buf, int size);
85static int dgram_puts(BIO *h, const char *str);
86static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
87static int dgram_new(BIO *h);
88static int dgram_free(BIO *data);
89static int dgram_clear(BIO *bio);
90
91static int BIO_dgram_should_retry(int s);
92
93static void get_current_time(struct timeval *t);
94
95static BIO_METHOD methods_dgramp=
96 {
97 BIO_TYPE_DGRAM,
98 "datagram socket",
99 dgram_write,
100 dgram_read,
101 dgram_puts,
102 NULL, /* dgram_gets, */
103 dgram_ctrl,
104 dgram_new,
105 dgram_free,
106 NULL,
107 };
108
109typedef struct bio_dgram_data_st
110 {
111 union {
112 struct sockaddr sa;
113 struct sockaddr_in sa_in;
114#if OPENSSL_USE_IPV6
115 struct sockaddr_in6 sa_in6;
116#endif
117 } peer;
118 unsigned int connected;
119 unsigned int _errno;
120 unsigned int mtu;
121 struct timeval next_timeout;
122 struct timeval socket_timeout;
123 } bio_dgram_data;
124
125BIO_METHOD *BIO_s_datagram(void)
126 {
127 return(&methods_dgramp);
128 }
129
130BIO *BIO_new_dgram(int fd, int close_flag)
131 {
132 BIO *ret;
133
134 ret=BIO_new(BIO_s_datagram());
135 if (ret == NULL) return(NULL);
136 BIO_set_fd(ret,fd,close_flag);
137 return(ret);
138 }
139
140static int dgram_new(BIO *bi)
141 {
142 bio_dgram_data *data = NULL;
143
144 bi->init=0;
145 bi->num=0;
146 data = OPENSSL_malloc(sizeof(bio_dgram_data));
147 if (data == NULL)
148 return 0;
149 memset(data, 0x00, sizeof(bio_dgram_data));
150 bi->ptr = data;
151
152 bi->flags=0;
153 return(1);
154 }
155
156static int dgram_free(BIO *a)
157 {
158 bio_dgram_data *data;
159
160 if (a == NULL) return(0);
161 if ( ! dgram_clear(a))
162 return 0;
163
164 data = (bio_dgram_data *)a->ptr;
165 if(data != NULL) OPENSSL_free(data);
166
167 return(1);
168 }
169
170static int dgram_clear(BIO *a)
171 {
172 if (a == NULL) return(0);
173 if (a->shutdown)
174 {
175 if (a->init)
176 {
177 SHUTDOWN2(a->num);
178 }
179 a->init=0;
180 a->flags=0;
181 }
182 return(1);
183 }
184
185static void dgram_adjust_rcv_timeout(BIO *b)
186 {
187#if defined(SO_RCVTIMEO)
188 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
189 int sz = sizeof(int);
190
191 /* Is a timer active? */
192 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
193 {
194 struct timeval timenow, timeleft;
195
196 /* Read current socket timeout */
197#ifdef OPENSSL_SYS_WINDOWS
198 int timeout;
199 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
200 (void*)&timeout, &sz) < 0)
201 { perror("getsockopt"); }
202 else
203 {
204 data->socket_timeout.tv_sec = timeout / 1000;
205 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
206 }
207#else
208 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
209 &(data->socket_timeout), (void *)&sz) < 0)
210 { perror("getsockopt"); }
211#endif
212
213 /* Get current time */
214 get_current_time(&timenow);
215
216 /* Calculate time left until timer expires */
217 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
218 timeleft.tv_sec -= timenow.tv_sec;
219 timeleft.tv_usec -= timenow.tv_usec;
220 if (timeleft.tv_usec < 0)
221 {
222 timeleft.tv_sec--;
223 timeleft.tv_usec += 1000000;
224 }
225
226 if (timeleft.tv_sec < 0)
227 {
228 timeleft.tv_sec = 0;
229 timeleft.tv_usec = 1;
230 }
231
232 /* Adjust socket timeout if next handhake message timer
233 * will expire earlier.
234 */
235 if ((data->socket_timeout.tv_sec == 0 && data->socket_timeout.tv_usec == 0) ||
236 (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
237 (data->socket_timeout.tv_sec == timeleft.tv_sec &&
238 data->socket_timeout.tv_usec >= timeleft.tv_usec))
239 {
240#ifdef OPENSSL_SYS_WINDOWS
241 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
242 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
243 (void*)&timeout, sizeof(timeout)) < 0)
244 { perror("setsockopt"); }
245#else
246 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
247 sizeof(struct timeval)) < 0)
248 { perror("setsockopt"); }
249#endif
250 }
251 }
252#endif
253 }
254
255static void dgram_reset_rcv_timeout(BIO *b)
256 {
257#if defined(SO_RCVTIMEO)
258 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
259
260 /* Is a timer active? */
261 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
262 {
263#ifdef OPENSSL_SYS_WINDOWS
264 int timeout = data->socket_timeout.tv_sec * 1000 +
265 data->socket_timeout.tv_usec / 1000;
266 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
267 (void*)&timeout, sizeof(timeout)) < 0)
268 { perror("setsockopt"); }
269#else
270 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
271 sizeof(struct timeval)) < 0)
272 { perror("setsockopt"); }
273#endif
274 }
275#endif
276 }
277
278static int dgram_read(BIO *b, char *out, int outl)
279 {
280 int ret=0;
281 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
282
283 struct {
284 /*
285 * See commentary in b_sock.c. <appro>
286 */
287 union { size_t s; int i; } len;
288 union {
289 struct sockaddr sa;
290 struct sockaddr_in sa_in;
291#if OPENSSL_USE_IPV6
292 struct sockaddr_in6 sa_in6;
293#endif
294 } peer;
295 } sa;
296
297 sa.len.s=0;
298 sa.len.i=sizeof(sa.peer);
299
300 if (out != NULL)
301 {
302 clear_socket_error();
303 memset(&sa.peer, 0x00, sizeof(sa.peer));
304 dgram_adjust_rcv_timeout(b);
305 ret=recvfrom(b->num,out,outl,0,&sa.peer.sa,(void *)&sa.len);
306 if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
307 {
308 OPENSSL_assert(sa.len.s<=sizeof(sa.peer));
309 sa.len.i = (int)sa.len.s;
310 }
311
312 if ( ! data->connected && ret >= 0)
313 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
314
315 BIO_clear_retry_flags(b);
316 if (ret < 0)
317 {
318 if (BIO_dgram_should_retry(ret))
319 {
320 BIO_set_retry_read(b);
321 data->_errno = get_last_socket_error();
322 }
323 }
324
325 dgram_reset_rcv_timeout(b);
326 }
327 return(ret);
328 }
329
330static int dgram_write(BIO *b, const char *in, int inl)
331 {
332 int ret;
333 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
334 clear_socket_error();
335
336 if ( data->connected )
337 ret=writesocket(b->num,in,inl);
338 else
339 {
340 int peerlen = sizeof(data->peer);
341
342 if (data->peer.sa.sa_family == AF_INET)
343 peerlen = sizeof(data->peer.sa_in);
344#if OPENSSL_USE_IPV6
345 else if (data->peer.sa.sa_family == AF_INET6)
346 peerlen = sizeof(data->peer.sa_in6);
347#endif
348#if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
349 ret=sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
350#else
351 ret=sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
352#endif
353 }
354
355 BIO_clear_retry_flags(b);
356 if (ret <= 0)
357 {
358 if (BIO_dgram_should_retry(ret))
359 {
360 BIO_set_retry_write(b);
361 data->_errno = get_last_socket_error();
362
363#if 0 /* higher layers are responsible for querying MTU, if necessary */
364 if ( data->_errno == EMSGSIZE)
365 /* retrieve the new MTU */
366 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
367#endif
368 }
369 }
370 return(ret);
371 }
372
373static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
374 {
375 long ret=1;
376 int *ip;
377 struct sockaddr *to = NULL;
378 bio_dgram_data *data = NULL;
379#if defined(IP_MTU_DISCOVER) || defined(IP_MTU)
380 long sockopt_val = 0;
381 unsigned int sockopt_len = 0;
382#endif
383#ifdef OPENSSL_SYS_LINUX
384 socklen_t addr_len;
385 union {
386 struct sockaddr sa;
387 struct sockaddr_in s4;
388#if OPENSSL_USE_IPV6
389 struct sockaddr_in6 s6;
390#endif
391 } addr;
392#endif
393
394 data = (bio_dgram_data *)b->ptr;
395
396 switch (cmd)
397 {
398 case BIO_CTRL_RESET:
399 num=0;
400 case BIO_C_FILE_SEEK:
401 ret=0;
402 break;
403 case BIO_C_FILE_TELL:
404 case BIO_CTRL_INFO:
405 ret=0;
406 break;
407 case BIO_C_SET_FD:
408 dgram_clear(b);
409 b->num= *((int *)ptr);
410 b->shutdown=(int)num;
411 b->init=1;
412 break;
413 case BIO_C_GET_FD:
414 if (b->init)
415 {
416 ip=(int *)ptr;
417 if (ip != NULL) *ip=b->num;
418 ret=b->num;
419 }
420 else
421 ret= -1;
422 break;
423 case BIO_CTRL_GET_CLOSE:
424 ret=b->shutdown;
425 break;
426 case BIO_CTRL_SET_CLOSE:
427 b->shutdown=(int)num;
428 break;
429 case BIO_CTRL_PENDING:
430 case BIO_CTRL_WPENDING:
431 ret=0;
432 break;
433 case BIO_CTRL_DUP:
434 case BIO_CTRL_FLUSH:
435 ret=1;
436 break;
437 case BIO_CTRL_DGRAM_CONNECT:
438 to = (struct sockaddr *)ptr;
439#if 0
440 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
441 { perror("connect"); ret = 0; }
442 else
443 {
444#endif
445 switch (to->sa_family)
446 {
447 case AF_INET:
448 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
449 break;
450#if OPENSSL_USE_IPV6
451 case AF_INET6:
452 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
453 break;
454#endif
455 default:
456 memcpy(&data->peer,to,sizeof(data->peer.sa));
457 break;
458 }
459#if 0
460 }
461#endif
462 break;
463 /* (Linux)kernel sets DF bit on outgoing IP packets */
464 case BIO_CTRL_DGRAM_MTU_DISCOVER:
465#ifdef OPENSSL_SYS_LINUX
466 addr_len = (socklen_t)sizeof(addr);
467 memset((void *)&addr, 0, sizeof(addr));
468 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
469 {
470 ret = 0;
471 break;
472 }
473 sockopt_len = sizeof(sockopt_val);
474 switch (addr.sa.sa_family)
475 {
476 case AF_INET:
477 sockopt_val = IP_PMTUDISC_DO;
478 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
479 &sockopt_val, sizeof(sockopt_val))) < 0)
480 perror("setsockopt");
481 break;
482#if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER)
483 case AF_INET6:
484 sockopt_val = IPV6_PMTUDISC_DO;
485 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
486 &sockopt_val, sizeof(sockopt_val))) < 0)
487 perror("setsockopt");
488 break;
489#endif
490 default:
491 ret = -1;
492 break;
493 }
494 ret = -1;
495#else
496 break;
497#endif
498 case BIO_CTRL_DGRAM_QUERY_MTU:
499#ifdef OPENSSL_SYS_LINUX
500 addr_len = (socklen_t)sizeof(addr);
501 memset((void *)&addr, 0, sizeof(addr));
502 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
503 {
504 ret = 0;
505 break;
506 }
507 sockopt_len = sizeof(sockopt_val);
508 switch (addr.sa.sa_family)
509 {
510 case AF_INET:
511 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
512 &sockopt_len)) < 0 || sockopt_val < 0)
513 {
514 ret = 0;
515 }
516 else
517 {
518 /* we assume that the transport protocol is UDP and no
519 * IP options are used.
520 */
521 data->mtu = sockopt_val - 8 - 20;
522 ret = data->mtu;
523 }
524 break;
525#if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
526 case AF_INET6:
527 if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
528 &sockopt_len)) < 0 || sockopt_val < 0)
529 {
530 ret = 0;
531 }
532 else
533 {
534 /* we assume that the transport protocol is UDP and no
535 * IPV6 options are used.
536 */
537 data->mtu = sockopt_val - 8 - 40;
538 ret = data->mtu;
539 }
540 break;
541#endif
542 default:
543 ret = 0;
544 break;
545 }
546#else
547 ret = 0;
548#endif
549 break;
550 case BIO_CTRL_DGRAM_GET_MTU:
551 return data->mtu;
552 break;
553 case BIO_CTRL_DGRAM_SET_MTU:
554 data->mtu = num;
555 ret = num;
556 break;
557 case BIO_CTRL_DGRAM_SET_CONNECTED:
558 to = (struct sockaddr *)ptr;
559
560 if ( to != NULL)
561 {
562 data->connected = 1;
563 switch (to->sa_family)
564 {
565 case AF_INET:
566 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
567 break;
568#if OPENSSL_USE_IPV6
569 case AF_INET6:
570 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
571 break;
572#endif
573 default:
574 memcpy(&data->peer,to,sizeof(data->peer.sa));
575 break;
576 }
577 }
578 else
579 {
580 data->connected = 0;
581 memset(&(data->peer), 0x00, sizeof(data->peer));
582 }
583 break;
584 case BIO_CTRL_DGRAM_GET_PEER:
585 switch (data->peer.sa.sa_family)
586 {
587 case AF_INET:
588 ret=sizeof(data->peer.sa_in);
589 break;
590#if OPENSSL_USE_IPV6
591 case AF_INET6:
592 ret=sizeof(data->peer.sa_in6);
593 break;
594#endif
595 default:
596 ret=sizeof(data->peer.sa);
597 break;
598 }
599 if (num==0 || num>ret)
600 num=ret;
601 memcpy(ptr,&data->peer,(ret=num));
602 break;
603 case BIO_CTRL_DGRAM_SET_PEER:
604 to = (struct sockaddr *) ptr;
605 switch (to->sa_family)
606 {
607 case AF_INET:
608 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
609 break;
610#if OPENSSL_USE_IPV6
611 case AF_INET6:
612 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
613 break;
614#endif
615 default:
616 memcpy(&data->peer,to,sizeof(data->peer.sa));
617 break;
618 }
619 break;
620 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
621 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
622 break;
623#if defined(SO_RCVTIMEO)
624 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
625#ifdef OPENSSL_SYS_WINDOWS
626 {
627 struct timeval *tv = (struct timeval *)ptr;
628 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
629 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
630 (void*)&timeout, sizeof(timeout)) < 0)
631 { perror("setsockopt"); ret = -1; }
632 }
633#else
634 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
635 sizeof(struct timeval)) < 0)
636 { perror("setsockopt"); ret = -1; }
637#endif
638 break;
639 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
640#ifdef OPENSSL_SYS_WINDOWS
641 {
642 int timeout, sz = sizeof(timeout);
643 struct timeval *tv = (struct timeval *)ptr;
644 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
645 (void*)&timeout, &sz) < 0)
646 { perror("getsockopt"); ret = -1; }
647 else
648 {
649 tv->tv_sec = timeout / 1000;
650 tv->tv_usec = (timeout % 1000) * 1000;
651 ret = sizeof(*tv);
652 }
653 }
654#else
655 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
656 ptr, (void *)&ret) < 0)
657 { perror("getsockopt"); ret = -1; }
658#endif
659 break;
660#endif
661#if defined(SO_SNDTIMEO)
662 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
663#ifdef OPENSSL_SYS_WINDOWS
664 {
665 struct timeval *tv = (struct timeval *)ptr;
666 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
667 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
668 (void*)&timeout, sizeof(timeout)) < 0)
669 { perror("setsockopt"); ret = -1; }
670 }
671#else
672 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
673 sizeof(struct timeval)) < 0)
674 { perror("setsockopt"); ret = -1; }
675#endif
676 break;
677 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
678#ifdef OPENSSL_SYS_WINDOWS
679 {
680 int timeout, sz = sizeof(timeout);
681 struct timeval *tv = (struct timeval *)ptr;
682 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
683 (void*)&timeout, &sz) < 0)
684 { perror("getsockopt"); ret = -1; }
685 else
686 {
687 tv->tv_sec = timeout / 1000;
688 tv->tv_usec = (timeout % 1000) * 1000;
689 ret = sizeof(*tv);
690 }
691 }
692#else
693 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
694 ptr, (void *)&ret) < 0)
695 { perror("getsockopt"); ret = -1; }
696#endif
697 break;
698#endif
699 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
700 /* fall-through */
701 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
702#ifdef OPENSSL_SYS_WINDOWS
703 if ( data->_errno == WSAETIMEDOUT)
704#else
705 if ( data->_errno == EAGAIN)
706#endif
707 {
708 ret = 1;
709 data->_errno = 0;
710 }
711 else
712 ret = 0;
713 break;
714#ifdef EMSGSIZE
715 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
716 if ( data->_errno == EMSGSIZE)
717 {
718 ret = 1;
719 data->_errno = 0;
720 }
721 else
722 ret = 0;
723 break;
724#endif
725 default:
726 ret=0;
727 break;
728 }
729 return(ret);
730 }
731
732static int dgram_puts(BIO *bp, const char *str)
733 {
734 int n,ret;
735
736 n=strlen(str);
737 ret=dgram_write(bp,str,n);
738 return(ret);
739 }
740
741static int BIO_dgram_should_retry(int i)
742 {
743 int err;
744
745 if ((i == 0) || (i == -1))
746 {
747 err=get_last_socket_error();
748
749#if defined(OPENSSL_SYS_WINDOWS)
750 /* If the socket return value (i) is -1
751 * and err is unexpectedly 0 at this point,
752 * the error code was overwritten by
753 * another system call before this error
754 * handling is called.
755 */
756#endif
757
758 return(BIO_dgram_non_fatal_error(err));
759 }
760 return(0);
761 }
762
763int BIO_dgram_non_fatal_error(int err)
764 {
765 switch (err)
766 {
767#if defined(OPENSSL_SYS_WINDOWS)
768# if defined(WSAEWOULDBLOCK)
769 case WSAEWOULDBLOCK:
770# endif
771
772# if 0 /* This appears to always be an error */
773# if defined(WSAENOTCONN)
774 case WSAENOTCONN:
775# endif
776# endif
777#endif
778
779#ifdef EWOULDBLOCK
780# ifdef WSAEWOULDBLOCK
781# if WSAEWOULDBLOCK != EWOULDBLOCK
782 case EWOULDBLOCK:
783# endif
784# else
785 case EWOULDBLOCK:
786# endif
787#endif
788
789#ifdef EINTR
790 case EINTR:
791#endif
792
793#ifdef EAGAIN
794#if EWOULDBLOCK != EAGAIN
795 case EAGAIN:
796# endif
797#endif
798
799#ifdef EPROTO
800 case EPROTO:
801#endif
802
803#ifdef EINPROGRESS
804 case EINPROGRESS:
805#endif
806
807#ifdef EALREADY
808 case EALREADY:
809#endif
810
811 return(1);
812 /* break; */
813 default:
814 break;
815 }
816 return(0);
817 }
818
819static void get_current_time(struct timeval *t)
820 {
821#ifdef OPENSSL_SYS_WIN32
822 struct _timeb tb;
823 _ftime(&tb);
824 t->tv_sec = (long)tb.time;
825 t->tv_usec = (long)tb.millitm * 1000;
826#elif defined(OPENSSL_SYS_VMS)
827 struct timeb tb;
828 ftime(&tb);
829 t->tv_sec = (long)tb.time;
830 t->tv_usec = (long)tb.millitm * 1000;
831#else
832 gettimeofday(t, NULL);
833#endif
834 }
835
836#endif
diff --git a/src/lib/libcrypto/bio/bss_fd.c b/src/lib/libcrypto/bio/bss_fd.c
deleted file mode 100644
index d1bf85aae1..0000000000
--- a/src/lib/libcrypto/bio/bss_fd.c
+++ /dev/null
@@ -1,319 +0,0 @@
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#if defined(OPENSSL_NO_POSIX_IO)
65/*
66 * One can argue that one should implement dummy placeholder for
67 * BIO_s_fd here...
68 */
69#else
70/*
71 * As for unconditional usage of "UPLINK" interface in this module.
72 * Trouble is that unlike Unix file descriptors [which are indexes
73 * in kernel-side per-process table], corresponding descriptors on
74 * platforms which require "UPLINK" interface seem to be indexes
75 * in a user-land, non-global table. Well, in fact they are indexes
76 * in stdio _iob[], and recall that _iob[] was the very reason why
77 * "UPLINK" interface was introduced in first place. But one way on
78 * another. Neither libcrypto or libssl use this BIO meaning that
79 * file descriptors can only be provided by application. Therefore
80 * "UPLINK" calls are due...
81 */
82#include "bio_lcl.h"
83
84static int fd_write(BIO *h, const char *buf, int num);
85static int fd_read(BIO *h, char *buf, int size);
86static int fd_puts(BIO *h, const char *str);
87static int fd_gets(BIO *h, char *buf, int size);
88static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
89static int fd_new(BIO *h);
90static int fd_free(BIO *data);
91int BIO_fd_should_retry(int s);
92
93static BIO_METHOD methods_fdp=
94 {
95 BIO_TYPE_FD,"file descriptor",
96 fd_write,
97 fd_read,
98 fd_puts,
99 fd_gets,
100 fd_ctrl,
101 fd_new,
102 fd_free,
103 NULL,
104 };
105
106BIO_METHOD *BIO_s_fd(void)
107 {
108 return(&methods_fdp);
109 }
110
111BIO *BIO_new_fd(int fd,int close_flag)
112 {
113 BIO *ret;
114 ret=BIO_new(BIO_s_fd());
115 if (ret == NULL) return(NULL);
116 BIO_set_fd(ret,fd,close_flag);
117 return(ret);
118 }
119
120static int fd_new(BIO *bi)
121 {
122 bi->init=0;
123 bi->num=-1;
124 bi->ptr=NULL;
125 bi->flags=BIO_FLAGS_UPLINK; /* essentially redundant */
126 return(1);
127 }
128
129static int fd_free(BIO *a)
130 {
131 if (a == NULL) return(0);
132 if (a->shutdown)
133 {
134 if (a->init)
135 {
136 UP_close(a->num);
137 }
138 a->init=0;
139 a->flags=BIO_FLAGS_UPLINK;
140 }
141 return(1);
142 }
143
144static int fd_read(BIO *b, char *out,int outl)
145 {
146 int ret=0;
147
148 if (out != NULL)
149 {
150 clear_sys_error();
151 ret=UP_read(b->num,out,outl);
152 BIO_clear_retry_flags(b);
153 if (ret <= 0)
154 {
155 if (BIO_fd_should_retry(ret))
156 BIO_set_retry_read(b);
157 }
158 }
159 return(ret);
160 }
161
162static int fd_write(BIO *b, const char *in, int inl)
163 {
164 int ret;
165 clear_sys_error();
166 ret=UP_write(b->num,in,inl);
167 BIO_clear_retry_flags(b);
168 if (ret <= 0)
169 {
170 if (BIO_fd_should_retry(ret))
171 BIO_set_retry_write(b);
172 }
173 return(ret);
174 }
175
176static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
177 {
178 long ret=1;
179 int *ip;
180
181 switch (cmd)
182 {
183 case BIO_CTRL_RESET:
184 num=0;
185 case BIO_C_FILE_SEEK:
186 ret=(long)UP_lseek(b->num,num,0);
187 break;
188 case BIO_C_FILE_TELL:
189 case BIO_CTRL_INFO:
190 ret=(long)UP_lseek(b->num,0,1);
191 break;
192 case BIO_C_SET_FD:
193 fd_free(b);
194 b->num= *((int *)ptr);
195 b->shutdown=(int)num;
196 b->init=1;
197 break;
198 case BIO_C_GET_FD:
199 if (b->init)
200 {
201 ip=(int *)ptr;
202 if (ip != NULL) *ip=b->num;
203 ret=b->num;
204 }
205 else
206 ret= -1;
207 break;
208 case BIO_CTRL_GET_CLOSE:
209 ret=b->shutdown;
210 break;
211 case BIO_CTRL_SET_CLOSE:
212 b->shutdown=(int)num;
213 break;
214 case BIO_CTRL_PENDING:
215 case BIO_CTRL_WPENDING:
216 ret=0;
217 break;
218 case BIO_CTRL_DUP:
219 case BIO_CTRL_FLUSH:
220 ret=1;
221 break;
222 default:
223 ret=0;
224 break;
225 }
226 return(ret);
227 }
228
229static int fd_puts(BIO *bp, const char *str)
230 {
231 int n,ret;
232
233 n=strlen(str);
234 ret=fd_write(bp,str,n);
235 return(ret);
236 }
237
238static int fd_gets(BIO *bp, char *buf, int size)
239 {
240 int ret=0;
241 char *ptr=buf;
242 char *end=buf+size-1;
243
244 while ( (ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n') )
245 ptr++;
246
247 ptr[0]='\0';
248
249 if (buf[0] != '\0')
250 ret=strlen(buf);
251 return(ret);
252 }
253
254int BIO_fd_should_retry(int i)
255 {
256 int err;
257
258 if ((i == 0) || (i == -1))
259 {
260 err=get_last_sys_error();
261
262#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
263 if ((i == -1) && (err == 0))
264 return(1);
265#endif
266
267 return(BIO_fd_non_fatal_error(err));
268 }
269 return(0);
270 }
271
272int BIO_fd_non_fatal_error(int err)
273 {
274 switch (err)
275 {
276
277#ifdef EWOULDBLOCK
278# ifdef WSAEWOULDBLOCK
279# if WSAEWOULDBLOCK != EWOULDBLOCK
280 case EWOULDBLOCK:
281# endif
282# else
283 case EWOULDBLOCK:
284# endif
285#endif
286
287#if defined(ENOTCONN)
288 case ENOTCONN:
289#endif
290
291#ifdef EINTR
292 case EINTR:
293#endif
294
295#ifdef EAGAIN
296#if EWOULDBLOCK != EAGAIN
297 case EAGAIN:
298# endif
299#endif
300
301#ifdef EPROTO
302 case EPROTO:
303#endif
304
305#ifdef EINPROGRESS
306 case EINPROGRESS:
307#endif
308
309#ifdef EALREADY
310 case EALREADY:
311#endif
312 return(1);
313 /* break; */
314 default:
315 break;
316 }
317 return(0);
318 }
319#endif
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c
deleted file mode 100644
index b954fe7ebc..0000000000
--- a/src/lib/libcrypto/bio/bss_file.c
+++ /dev/null
@@ -1,477 +0,0 @@
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
98static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
99static int MS_CALLBACK file_read(BIO *h, char *buf, int size);
100static int MS_CALLBACK file_puts(BIO *h, const char *str);
101static int MS_CALLBACK file_gets(BIO *h, char *str, int size);
102static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
103static int MS_CALLBACK file_new(BIO *h);
104static int MS_CALLBACK file_free(BIO *data);
105static 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
119BIO *BIO_new_file(const char *filename, const char *mode)
120 {
121 BIO *ret;
122 FILE *file=NULL;
123
124#if defined(_WIN32) && defined(CP_UTF8)
125 int sz, len_0 = (int)strlen(filename)+1;
126 DWORD flags;
127
128 /*
129 * Basically there are three cases to cover: a) filename is
130 * pure ASCII string; b) actual UTF-8 encoded string and
131 * c) locale-ized string, i.e. one containing 8-bit
132 * characters that are meaningful in current system locale.
133 * If filename is pure ASCII or real UTF-8 encoded string,
134 * MultiByteToWideChar succeeds and _wfopen works. If
135 * filename is locale-ized string, chances are that
136 * MultiByteToWideChar fails reporting
137 * ERROR_NO_UNICODE_TRANSLATION, in which case we fall
138 * back to fopen...
139 */
140 if ((sz=MultiByteToWideChar(CP_UTF8,(flags=MB_ERR_INVALID_CHARS),
141 filename,len_0,NULL,0))>0 ||
142 (GetLastError()==ERROR_INVALID_FLAGS &&
143 (sz=MultiByteToWideChar(CP_UTF8,(flags=0),
144 filename,len_0,NULL,0))>0)
145 )
146 {
147 WCHAR wmode[8];
148 WCHAR *wfilename = _alloca(sz*sizeof(WCHAR));
149
150 if (MultiByteToWideChar(CP_UTF8,flags,
151 filename,len_0,wfilename,sz) &&
152 MultiByteToWideChar(CP_UTF8,0,mode,strlen(mode)+1,
153 wmode,sizeof(wmode)/sizeof(wmode[0])) &&
154 (file=_wfopen(wfilename,wmode))==NULL &&
155 (errno==ENOENT || errno==EBADF)
156 ) /* UTF-8 decode succeeded, but no file, filename
157 * could still have been locale-ized... */
158 file = fopen(filename,mode);
159 }
160 else if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION)
161 {
162 file = fopen(filename,mode);
163 }
164#else
165 file=fopen(filename,mode);
166#endif
167 if (file == NULL)
168 {
169 SYSerr(SYS_F_FOPEN,get_last_sys_error());
170 ERR_add_error_data(5,"fopen('",filename,"','",mode,"')");
171 if (errno == ENOENT)
172 BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE);
173 else
174 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
175 return(NULL);
176 }
177 if ((ret=BIO_new(BIO_s_file())) == NULL)
178 {
179 fclose(file);
180 return(NULL);
181 }
182
183 BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
184 BIO_set_fp(ret,file,BIO_CLOSE);
185 return(ret);
186 }
187
188BIO *BIO_new_fp(FILE *stream, int close_flag)
189 {
190 BIO *ret;
191
192 if ((ret=BIO_new(BIO_s_file())) == NULL)
193 return(NULL);
194
195 BIO_set_flags(ret,BIO_FLAGS_UPLINK); /* redundant, left for documentation puposes */
196 BIO_set_fp(ret,stream,close_flag);
197 return(ret);
198 }
199
200BIO_METHOD *BIO_s_file(void)
201 {
202 return(&methods_filep);
203 }
204
205static int MS_CALLBACK file_new(BIO *bi)
206 {
207 bi->init=0;
208 bi->num=0;
209 bi->ptr=NULL;
210 bi->flags=BIO_FLAGS_UPLINK; /* default to UPLINK */
211 return(1);
212 }
213
214static int MS_CALLBACK file_free(BIO *a)
215 {
216 if (a == NULL) return(0);
217 if (a->shutdown)
218 {
219 if ((a->init) && (a->ptr != NULL))
220 {
221 if (a->flags&BIO_FLAGS_UPLINK)
222 UP_fclose (a->ptr);
223 else
224 fclose (a->ptr);
225 a->ptr=NULL;
226 a->flags=BIO_FLAGS_UPLINK;
227 }
228 a->init=0;
229 }
230 return(1);
231 }
232
233static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
234 {
235 int ret=0;
236
237 if (b->init && (out != NULL))
238 {
239 if (b->flags&BIO_FLAGS_UPLINK)
240 ret=UP_fread(out,1,(int)outl,b->ptr);
241 else
242 ret=fread(out,1,(int)outl,(FILE *)b->ptr);
243 if(ret == 0 && (b->flags&BIO_FLAGS_UPLINK)?UP_ferror((FILE *)b->ptr):ferror((FILE *)b->ptr))
244 {
245 SYSerr(SYS_F_FREAD,get_last_sys_error());
246 BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB);
247 ret=-1;
248 }
249 }
250 return(ret);
251 }
252
253static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
254 {
255 int ret=0;
256
257 if (b->init && (in != NULL))
258 {
259 if (b->flags&BIO_FLAGS_UPLINK)
260 ret=UP_fwrite(in,(int)inl,1,b->ptr);
261 else
262 ret=fwrite(in,(int)inl,1,(FILE *)b->ptr);
263 if (ret)
264 ret=inl;
265 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
266 /* according to Tim Hudson <tjh@cryptsoft.com>, the commented
267 * out version above can cause 'inl' write calls under
268 * some stupid stdio implementations (VMS) */
269 }
270 return(ret);
271 }
272
273static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
274 {
275 long ret=1;
276 FILE *fp=(FILE *)b->ptr;
277 FILE **fpp;
278 char p[4];
279
280 switch (cmd)
281 {
282 case BIO_C_FILE_SEEK:
283 case BIO_CTRL_RESET:
284 if (b->flags&BIO_FLAGS_UPLINK)
285 ret=(long)UP_fseek(b->ptr,num,0);
286 else
287 ret=(long)fseek(fp,num,0);
288 break;
289 case BIO_CTRL_EOF:
290 if (b->flags&BIO_FLAGS_UPLINK)
291 ret=(long)UP_feof(fp);
292 else
293 ret=(long)feof(fp);
294 break;
295 case BIO_C_FILE_TELL:
296 case BIO_CTRL_INFO:
297 if (b->flags&BIO_FLAGS_UPLINK)
298 ret=UP_ftell(b->ptr);
299 else
300 ret=ftell(fp);
301 break;
302 case BIO_C_SET_FILE_PTR:
303 file_free(b);
304 b->shutdown=(int)num&BIO_CLOSE;
305 b->ptr=ptr;
306 b->init=1;
307#if BIO_FLAGS_UPLINK!=0
308#if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
309#define _IOB_ENTRIES 20
310#endif
311#if defined(_IOB_ENTRIES)
312 /* Safety net to catch purely internal BIO_set_fp calls */
313 if ((size_t)ptr >= (size_t)stdin &&
314 (size_t)ptr < (size_t)(stdin+_IOB_ENTRIES))
315 BIO_clear_flags(b,BIO_FLAGS_UPLINK);
316#endif
317#endif
318#ifdef UP_fsetmod
319 if (b->flags&BIO_FLAGS_UPLINK)
320 UP_fsetmod(b->ptr,(char)((num&BIO_FP_TEXT)?'t':'b'));
321 else
322#endif
323 {
324#if defined(OPENSSL_SYS_WINDOWS)
325 int fd = _fileno((FILE*)ptr);
326 if (num & BIO_FP_TEXT)
327 _setmode(fd,_O_TEXT);
328 else
329 _setmode(fd,_O_BINARY);
330#elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
331 int fd = fileno((FILE*)ptr);
332 /* Under CLib there are differences in file modes */
333 if (num & BIO_FP_TEXT)
334 setmode(fd,O_TEXT);
335 else
336 setmode(fd,O_BINARY);
337#elif defined(OPENSSL_SYS_MSDOS)
338 int fd = fileno((FILE*)ptr);
339 /* Set correct text/binary mode */
340 if (num & BIO_FP_TEXT)
341 _setmode(fd,_O_TEXT);
342 /* Dangerous to set stdin/stdout to raw (unless redirected) */
343 else
344 {
345 if (fd == STDIN_FILENO || fd == STDOUT_FILENO)
346 {
347 if (isatty(fd) <= 0)
348 _setmode(fd,_O_BINARY);
349 }
350 else
351 _setmode(fd,_O_BINARY);
352 }
353#elif defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
354 int fd = fileno((FILE*)ptr);
355 if (num & BIO_FP_TEXT)
356 setmode(fd, O_TEXT);
357 else
358 setmode(fd, O_BINARY);
359#endif
360 }
361 break;
362 case BIO_C_SET_FILENAME:
363 file_free(b);
364 b->shutdown=(int)num&BIO_CLOSE;
365 if (num & BIO_FP_APPEND)
366 {
367 if (num & BIO_FP_READ)
368 BUF_strlcpy(p,"a+",sizeof p);
369 else BUF_strlcpy(p,"a",sizeof p);
370 }
371 else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
372 BUF_strlcpy(p,"r+",sizeof p);
373 else if (num & BIO_FP_WRITE)
374 BUF_strlcpy(p,"w",sizeof p);
375 else if (num & BIO_FP_READ)
376 BUF_strlcpy(p,"r",sizeof p);
377 else
378 {
379 BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE);
380 ret=0;
381 break;
382 }
383#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
384 if (!(num & BIO_FP_TEXT))
385 strcat(p,"b");
386 else
387 strcat(p,"t");
388#endif
389#if defined(OPENSSL_SYS_NETWARE)
390 if (!(num & BIO_FP_TEXT))
391 strcat(p,"b");
392 else
393 strcat(p,"t");
394#endif
395 fp=fopen(ptr,p);
396 if (fp == NULL)
397 {
398 SYSerr(SYS_F_FOPEN,get_last_sys_error());
399 ERR_add_error_data(5,"fopen('",ptr,"','",p,"')");
400 BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB);
401 ret=0;
402 break;
403 }
404 b->ptr=fp;
405 b->init=1;
406 BIO_clear_flags(b,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
407 break;
408 case BIO_C_GET_FILE_PTR:
409 /* the ptr parameter is actually a FILE ** in this case. */
410 if (ptr != NULL)
411 {
412 fpp=(FILE **)ptr;
413 *fpp=(FILE *)b->ptr;
414 }
415 break;
416 case BIO_CTRL_GET_CLOSE:
417 ret=(long)b->shutdown;
418 break;
419 case BIO_CTRL_SET_CLOSE:
420 b->shutdown=(int)num;
421 break;
422 case BIO_CTRL_FLUSH:
423 if (b->flags&BIO_FLAGS_UPLINK)
424 UP_fflush(b->ptr);
425 else
426 fflush((FILE *)b->ptr);
427 break;
428 case BIO_CTRL_DUP:
429 ret=1;
430 break;
431
432 case BIO_CTRL_WPENDING:
433 case BIO_CTRL_PENDING:
434 case BIO_CTRL_PUSH:
435 case BIO_CTRL_POP:
436 default:
437 ret=0;
438 break;
439 }
440 return(ret);
441 }
442
443static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
444 {
445 int ret=0;
446
447 buf[0]='\0';
448 if (bp->flags&BIO_FLAGS_UPLINK)
449 {
450 if (!UP_fgets(buf,size,bp->ptr))
451 goto err;
452 }
453 else
454 {
455 if (!fgets(buf,size,(FILE *)bp->ptr))
456 goto err;
457 }
458 if (buf[0] != '\0')
459 ret=strlen(buf);
460 err:
461 return(ret);
462 }
463
464static int MS_CALLBACK file_puts(BIO *bp, const char *str)
465 {
466 int n,ret;
467
468 n=strlen(str);
469 ret=file_write(bp,str,n);
470 return(ret);
471 }
472
473#endif /* OPENSSL_NO_STDIO */
474
475#endif /* HEADER_BSS_FILE_C */
476
477
diff --git a/src/lib/libcrypto/bio/bss_log.c b/src/lib/libcrypto/bio/bss_log.c
deleted file mode 100644
index b7dce5c1a2..0000000000
--- a/src/lib/libcrypto/bio/bss_log.c
+++ /dev/null
@@ -1,399 +0,0 @@
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#elif defined(OPENSSL_SYS_VMS)
74# include <opcdef.h>
75# include <descrip.h>
76# include <lib$routines.h>
77# include <starlet.h>
78/* Some compiler options may mask the declaration of "_malloc32". */
79# if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
80# if __INITIAL_POINTER_SIZE == 64
81# pragma pointer_size save
82# pragma pointer_size 32
83 void * _malloc32 (__size_t);
84# pragma pointer_size restore
85# endif /* __INITIAL_POINTER_SIZE == 64 */
86# endif /* __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE */
87#elif defined(__ultrix)
88# include <sys/syslog.h>
89#elif defined(OPENSSL_SYS_NETWARE)
90# define NO_SYSLOG
91#elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
92# include <syslog.h>
93#endif
94
95#include <openssl/buffer.h>
96#include <openssl/err.h>
97
98#ifndef NO_SYSLOG
99
100#if defined(OPENSSL_SYS_WIN32)
101#define LOG_EMERG 0
102#define LOG_ALERT 1
103#define LOG_CRIT 2
104#define LOG_ERR 3
105#define LOG_WARNING 4
106#define LOG_NOTICE 5
107#define LOG_INFO 6
108#define LOG_DEBUG 7
109
110#define LOG_DAEMON (3<<3)
111#elif defined(OPENSSL_SYS_VMS)
112/* On VMS, we don't really care about these, but we need them to compile */
113#define LOG_EMERG 0
114#define LOG_ALERT 1
115#define LOG_CRIT 2
116#define LOG_ERR 3
117#define LOG_WARNING 4
118#define LOG_NOTICE 5
119#define LOG_INFO 6
120#define LOG_DEBUG 7
121
122#define LOG_DAEMON OPC$M_NM_NTWORK
123#endif
124
125static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num);
126static int MS_CALLBACK slg_puts(BIO *h, const char *str);
127static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
128static int MS_CALLBACK slg_new(BIO *h);
129static int MS_CALLBACK slg_free(BIO *data);
130static void xopenlog(BIO* bp, char* name, int level);
131static void xsyslog(BIO* bp, int priority, const char* string);
132static void xcloselog(BIO* bp);
133
134static BIO_METHOD methods_slg=
135 {
136 BIO_TYPE_MEM,"syslog",
137 slg_write,
138 NULL,
139 slg_puts,
140 NULL,
141 slg_ctrl,
142 slg_new,
143 slg_free,
144 NULL,
145 };
146
147BIO_METHOD *BIO_s_log(void)
148 {
149 return(&methods_slg);
150 }
151
152static int MS_CALLBACK slg_new(BIO *bi)
153 {
154 bi->init=1;
155 bi->num=0;
156 bi->ptr=NULL;
157 xopenlog(bi, "application", LOG_DAEMON);
158 return(1);
159 }
160
161static int MS_CALLBACK slg_free(BIO *a)
162 {
163 if (a == NULL) return(0);
164 xcloselog(a);
165 return(1);
166 }
167
168static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl)
169 {
170 int ret= inl;
171 char* buf;
172 char* pp;
173 int priority, i;
174 static const struct
175 {
176 int strl;
177 char str[10];
178 int log_level;
179 }
180 mapping[] =
181 {
182 { 6, "PANIC ", LOG_EMERG },
183 { 6, "EMERG ", LOG_EMERG },
184 { 4, "EMR ", LOG_EMERG },
185 { 6, "ALERT ", LOG_ALERT },
186 { 4, "ALR ", LOG_ALERT },
187 { 5, "CRIT ", LOG_CRIT },
188 { 4, "CRI ", LOG_CRIT },
189 { 6, "ERROR ", LOG_ERR },
190 { 4, "ERR ", LOG_ERR },
191 { 8, "WARNING ", LOG_WARNING },
192 { 5, "WARN ", LOG_WARNING },
193 { 4, "WAR ", LOG_WARNING },
194 { 7, "NOTICE ", LOG_NOTICE },
195 { 5, "NOTE ", LOG_NOTICE },
196 { 4, "NOT ", LOG_NOTICE },
197 { 5, "INFO ", LOG_INFO },
198 { 4, "INF ", LOG_INFO },
199 { 6, "DEBUG ", LOG_DEBUG },
200 { 4, "DBG ", LOG_DEBUG },
201 { 0, "", LOG_ERR } /* The default */
202 };
203
204 if((buf= (char *)OPENSSL_malloc(inl+ 1)) == NULL){
205 return(0);
206 }
207 strncpy(buf, in, inl);
208 buf[inl]= '\0';
209
210 i = 0;
211 while(strncmp(buf, mapping[i].str, mapping[i].strl) != 0) i++;
212 priority = mapping[i].log_level;
213 pp = buf + mapping[i].strl;
214
215 xsyslog(b, priority, pp);
216
217 OPENSSL_free(buf);
218 return(ret);
219 }
220
221static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr)
222 {
223 switch (cmd)
224 {
225 case BIO_CTRL_SET:
226 xcloselog(b);
227 xopenlog(b, ptr, num);
228 break;
229 default:
230 break;
231 }
232 return(0);
233 }
234
235static int MS_CALLBACK slg_puts(BIO *bp, const char *str)
236 {
237 int n,ret;
238
239 n=strlen(str);
240 ret=slg_write(bp,str,n);
241 return(ret);
242 }
243
244#if defined(OPENSSL_SYS_WIN32)
245
246static void xopenlog(BIO* bp, char* name, int level)
247{
248 if (GetVersion() < 0x80000000)
249 bp->ptr = RegisterEventSourceA(NULL,name);
250 else
251 bp->ptr = NULL;
252}
253
254static void xsyslog(BIO *bp, int priority, const char *string)
255{
256 LPCSTR lpszStrings[2];
257 WORD evtype= EVENTLOG_ERROR_TYPE;
258 char pidbuf[DECIMAL_SIZE(DWORD)+4];
259
260 if (bp->ptr == NULL)
261 return;
262
263 switch (priority)
264 {
265 case LOG_EMERG:
266 case LOG_ALERT:
267 case LOG_CRIT:
268 case LOG_ERR:
269 evtype = EVENTLOG_ERROR_TYPE;
270 break;
271 case LOG_WARNING:
272 evtype = EVENTLOG_WARNING_TYPE;
273 break;
274 case LOG_NOTICE:
275 case LOG_INFO:
276 case LOG_DEBUG:
277 evtype = EVENTLOG_INFORMATION_TYPE;
278 break;
279 default: /* Should never happen, but set it
280 as error anyway. */
281 evtype = EVENTLOG_ERROR_TYPE;
282 break;
283 }
284
285 sprintf(pidbuf, "[%u] ", GetCurrentProcessId());
286 lpszStrings[0] = pidbuf;
287 lpszStrings[1] = string;
288
289 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0,
290 lpszStrings, NULL);
291}
292
293static void xcloselog(BIO* bp)
294{
295 if(bp->ptr)
296 DeregisterEventSource((HANDLE)(bp->ptr));
297 bp->ptr= NULL;
298}
299
300#elif defined(OPENSSL_SYS_VMS)
301
302static int VMS_OPC_target = LOG_DAEMON;
303
304static void xopenlog(BIO* bp, char* name, int level)
305{
306 VMS_OPC_target = level;
307}
308
309static void xsyslog(BIO *bp, int priority, const char *string)
310{
311 struct dsc$descriptor_s opc_dsc;
312
313/* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
314#if __INITIAL_POINTER_SIZE == 64
315# pragma pointer_size save
316# pragma pointer_size 32
317# define OPCDEF_TYPE __char_ptr32
318# define OPCDEF_MALLOC _malloc32
319#else /* __INITIAL_POINTER_SIZE == 64 */
320# define OPCDEF_TYPE char *
321# define OPCDEF_MALLOC OPENSSL_malloc
322#endif /* __INITIAL_POINTER_SIZE == 64 [else] */
323
324 struct opcdef *opcdef_p;
325
326#if __INITIAL_POINTER_SIZE == 64
327# pragma pointer_size restore
328#endif /* __INITIAL_POINTER_SIZE == 64 */
329
330 char buf[10240];
331 unsigned int len;
332 struct dsc$descriptor_s buf_dsc;
333 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
334 char *priority_tag;
335
336 switch (priority)
337 {
338 case LOG_EMERG: priority_tag = "Emergency"; break;
339 case LOG_ALERT: priority_tag = "Alert"; break;
340 case LOG_CRIT: priority_tag = "Critical"; break;
341 case LOG_ERR: priority_tag = "Error"; break;
342 case LOG_WARNING: priority_tag = "Warning"; break;
343 case LOG_NOTICE: priority_tag = "Notice"; break;
344 case LOG_INFO: priority_tag = "Info"; break;
345 case LOG_DEBUG: priority_tag = "DEBUG"; break;
346 }
347
348 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
349 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
350 buf_dsc.dsc$a_pointer = buf;
351 buf_dsc.dsc$w_length = sizeof(buf) - 1;
352
353 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
354
355 /* We know there's an 8-byte header. That's documented. */
356 opcdef_p = OPCDEF_MALLOC( 8+ len);
357 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
358 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
359 opcdef_p->opc$l_ms_rqstid = 0;
360 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
361
362 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
363 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
364 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
365 opc_dsc.dsc$w_length = len + 8;
366
367 sys$sndopr(opc_dsc, 0);
368
369 OPENSSL_free(opcdef_p);
370}
371
372static void xcloselog(BIO* bp)
373{
374}
375
376#else /* Unix/Watt32 */
377
378static void xopenlog(BIO* bp, char* name, int level)
379{
380#ifdef WATT32 /* djgpp/DOS */
381 openlog(name, LOG_PID|LOG_CONS|LOG_NDELAY, level);
382#else
383 openlog(name, LOG_PID|LOG_CONS, level);
384#endif
385}
386
387static void xsyslog(BIO *bp, int priority, const char *string)
388{
389 syslog(priority, "%s", string);
390}
391
392static void xcloselog(BIO* bp)
393{
394 closelog();
395}
396
397#endif /* Unix */
398
399#endif /* NO_SYSLOG */
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c
deleted file mode 100644
index 37d4194e4b..0000000000
--- a/src/lib/libcrypto/bio/bss_mem.c
+++ /dev/null
@@ -1,319 +0,0 @@
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
64static int mem_write(BIO *h, const char *buf, int num);
65static int mem_read(BIO *h, char *buf, int size);
66static int mem_puts(BIO *h, const char *str);
67static int mem_gets(BIO *h, char *str, int size);
68static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static int mem_new(BIO *h);
70static int mem_free(BIO *data);
71static 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
88BIO_METHOD *BIO_s_mem(void)
89 {
90 return(&mem_method);
91 }
92
93BIO *BIO_new_mem_buf(void *buf, int len)
94{
95 BIO *ret;
96 BUF_MEM *b;
97 size_t sz;
98
99 if (!buf) {
100 BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER);
101 return NULL;
102 }
103 sz = (len<0) ? strlen(buf) : (size_t)len;
104 if(!(ret = BIO_new(BIO_s_mem())) ) return NULL;
105 b = (BUF_MEM *)ret->ptr;
106 b->data = buf;
107 b->length = sz;
108 b->max = sz;
109 ret->flags |= BIO_FLAGS_MEM_RDONLY;
110 /* Since this is static data retrying wont help */
111 ret->num = 0;
112 return ret;
113}
114
115static int mem_new(BIO *bi)
116 {
117 BUF_MEM *b;
118
119 if ((b=BUF_MEM_new()) == NULL)
120 return(0);
121 bi->shutdown=1;
122 bi->init=1;
123 bi->num= -1;
124 bi->ptr=(char *)b;
125 return(1);
126 }
127
128static int mem_free(BIO *a)
129 {
130 if (a == NULL) return(0);
131 if (a->shutdown)
132 {
133 if ((a->init) && (a->ptr != NULL))
134 {
135 BUF_MEM *b;
136 b = (BUF_MEM *)a->ptr;
137 if(a->flags & BIO_FLAGS_MEM_RDONLY) b->data = NULL;
138 BUF_MEM_free(b);
139 a->ptr=NULL;
140 }
141 }
142 return(1);
143 }
144
145static int mem_read(BIO *b, char *out, int outl)
146 {
147 int ret= -1;
148 BUF_MEM *bm;
149
150 bm=(BUF_MEM *)b->ptr;
151 BIO_clear_retry_flags(b);
152 ret=(outl >=0 && (size_t)outl > bm->length)?(int)bm->length:outl;
153 if ((out != NULL) && (ret > 0)) {
154 memcpy(out,bm->data,ret);
155 bm->length-=ret;
156 if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret;
157 else {
158 memmove(&(bm->data[0]),&(bm->data[ret]),bm->length);
159 }
160 } else if (bm->length == 0)
161 {
162 ret = b->num;
163 if (ret != 0)
164 BIO_set_retry_read(b);
165 }
166 return(ret);
167 }
168
169static int mem_write(BIO *b, const char *in, int inl)
170 {
171 int ret= -1;
172 int blen;
173 BUF_MEM *bm;
174
175 bm=(BUF_MEM *)b->ptr;
176 if (in == NULL)
177 {
178 BIOerr(BIO_F_MEM_WRITE,BIO_R_NULL_PARAMETER);
179 goto end;
180 }
181
182 if(b->flags & BIO_FLAGS_MEM_RDONLY) {
183 BIOerr(BIO_F_MEM_WRITE,BIO_R_WRITE_TO_READ_ONLY_BIO);
184 goto end;
185 }
186
187 BIO_clear_retry_flags(b);
188 blen=bm->length;
189 if (BUF_MEM_grow_clean(bm,blen+inl) != (blen+inl))
190 goto end;
191 memcpy(&(bm->data[blen]),in,inl);
192 ret=inl;
193end:
194 return(ret);
195 }
196
197static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
198 {
199 long ret=1;
200 char **pptr;
201
202 BUF_MEM *bm=(BUF_MEM *)b->ptr;
203
204 switch (cmd)
205 {
206 case BIO_CTRL_RESET:
207 if (bm->data != NULL)
208 {
209 /* For read only case reset to the start again */
210 if(b->flags & BIO_FLAGS_MEM_RDONLY)
211 {
212 bm->data -= bm->max - bm->length;
213 bm->length = bm->max;
214 }
215 else
216 {
217 memset(bm->data,0,bm->max);
218 bm->length=0;
219 }
220 }
221 break;
222 case BIO_CTRL_EOF:
223 ret=(long)(bm->length == 0);
224 break;
225 case BIO_C_SET_BUF_MEM_EOF_RETURN:
226 b->num=(int)num;
227 break;
228 case BIO_CTRL_INFO:
229 ret=(long)bm->length;
230 if (ptr != NULL)
231 {
232 pptr=(char **)ptr;
233 *pptr=(char *)&(bm->data[0]);
234 }
235 break;
236 case BIO_C_SET_BUF_MEM:
237 mem_free(b);
238 b->shutdown=(int)num;
239 b->ptr=ptr;
240 break;
241 case BIO_C_GET_BUF_MEM_PTR:
242 if (ptr != NULL)
243 {
244 pptr=(char **)ptr;
245 *pptr=(char *)bm;
246 }
247 break;
248 case BIO_CTRL_GET_CLOSE:
249 ret=(long)b->shutdown;
250 break;
251 case BIO_CTRL_SET_CLOSE:
252 b->shutdown=(int)num;
253 break;
254
255 case BIO_CTRL_WPENDING:
256 ret=0L;
257 break;
258 case BIO_CTRL_PENDING:
259 ret=(long)bm->length;
260 break;
261 case BIO_CTRL_DUP:
262 case BIO_CTRL_FLUSH:
263 ret=1;
264 break;
265 case BIO_CTRL_PUSH:
266 case BIO_CTRL_POP:
267 default:
268 ret=0;
269 break;
270 }
271 return(ret);
272 }
273
274static int mem_gets(BIO *bp, char *buf, int size)
275 {
276 int i,j;
277 int ret= -1;
278 char *p;
279 BUF_MEM *bm=(BUF_MEM *)bp->ptr;
280
281 BIO_clear_retry_flags(bp);
282 j=bm->length;
283 if ((size-1) < j) j=size-1;
284 if (j <= 0)
285 {
286 *buf='\0';
287 return 0;
288 }
289 p=bm->data;
290 for (i=0; i<j; i++)
291 {
292 if (p[i] == '\n')
293 {
294 i++;
295 break;
296 }
297 }
298
299 /*
300 * i is now the max num of bytes to copy, either j or up to
301 * and including the first newline
302 */
303
304 i=mem_read(bp,buf,i);
305 if (i > 0) buf[i]='\0';
306 ret=i;
307 return(ret);
308 }
309
310static int mem_puts(BIO *bp, const char *str)
311 {
312 int n,ret;
313
314 n=strlen(str);
315 ret=mem_write(bp,str,n);
316 /* memory semantics is that it will always work */
317 return(ret);
318 }
319
diff --git a/src/lib/libcrypto/bio/bss_null.c b/src/lib/libcrypto/bio/bss_null.c
deleted file mode 100644
index 46b73339df..0000000000
--- a/src/lib/libcrypto/bio/bss_null.c
+++ /dev/null
@@ -1,150 +0,0 @@
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
64static int null_write(BIO *h, const char *buf, int num);
65static int null_read(BIO *h, char *buf, int size);
66static int null_puts(BIO *h, const char *str);
67static int null_gets(BIO *h, char *str, int size);
68static long null_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static int null_new(BIO *h);
70static int null_free(BIO *data);
71static 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
85BIO_METHOD *BIO_s_null(void)
86 {
87 return(&null_method);
88 }
89
90static int null_new(BIO *bi)
91 {
92 bi->init=1;
93 bi->num=0;
94 bi->ptr=(NULL);
95 return(1);
96 }
97
98static int null_free(BIO *a)
99 {
100 if (a == NULL) return(0);
101 return(1);
102 }
103
104static int null_read(BIO *b, char *out, int outl)
105 {
106 return(0);
107 }
108
109static int null_write(BIO *b, const char *in, int inl)
110 {
111 return(inl);
112 }
113
114static 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
140static int null_gets(BIO *bp, char *buf, int size)
141 {
142 return(0);
143 }
144
145static 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_sock.c b/src/lib/libcrypto/bio/bss_sock.c
deleted file mode 100644
index 3df31938c1..0000000000
--- a/src/lib/libcrypto/bio/bss_sock.c
+++ /dev/null
@@ -1,294 +0,0 @@
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
64#ifndef OPENSSL_NO_SOCK
65
66#include <openssl/bio.h>
67
68#ifdef WATT32
69#define sock_write SockWrite /* Watt-32 uses same names */
70#define sock_read SockRead
71#define sock_puts SockPuts
72#endif
73
74static int sock_write(BIO *h, const char *buf, int num);
75static int sock_read(BIO *h, char *buf, int size);
76static int sock_puts(BIO *h, const char *str);
77static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
78static int sock_new(BIO *h);
79static int sock_free(BIO *data);
80int BIO_sock_should_retry(int s);
81
82static BIO_METHOD methods_sockp=
83 {
84 BIO_TYPE_SOCKET,
85 "socket",
86 sock_write,
87 sock_read,
88 sock_puts,
89 NULL, /* sock_gets, */
90 sock_ctrl,
91 sock_new,
92 sock_free,
93 NULL,
94 };
95
96BIO_METHOD *BIO_s_socket(void)
97 {
98 return(&methods_sockp);
99 }
100
101BIO *BIO_new_socket(int fd, int close_flag)
102 {
103 BIO *ret;
104
105 ret=BIO_new(BIO_s_socket());
106 if (ret == NULL) return(NULL);
107 BIO_set_fd(ret,fd,close_flag);
108 return(ret);
109 }
110
111static int sock_new(BIO *bi)
112 {
113 bi->init=0;
114 bi->num=0;
115 bi->ptr=NULL;
116 bi->flags=0;
117 return(1);
118 }
119
120static int sock_free(BIO *a)
121 {
122 if (a == NULL) return(0);
123 if (a->shutdown)
124 {
125 if (a->init)
126 {
127 SHUTDOWN2(a->num);
128 }
129 a->init=0;
130 a->flags=0;
131 }
132 return(1);
133 }
134
135static int sock_read(BIO *b, char *out, int outl)
136 {
137 int ret=0;
138
139 if (out != NULL)
140 {
141 clear_socket_error();
142 ret=readsocket(b->num,out,outl);
143 BIO_clear_retry_flags(b);
144 if (ret <= 0)
145 {
146 if (BIO_sock_should_retry(ret))
147 BIO_set_retry_read(b);
148 }
149 }
150 return(ret);
151 }
152
153static int sock_write(BIO *b, const char *in, int inl)
154 {
155 int ret;
156
157 clear_socket_error();
158 ret=writesocket(b->num,in,inl);
159 BIO_clear_retry_flags(b);
160 if (ret <= 0)
161 {
162 if (BIO_sock_should_retry(ret))
163 BIO_set_retry_write(b);
164 }
165 return(ret);
166 }
167
168static long sock_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_C_SET_FD:
176 sock_free(b);
177 b->num= *((int *)ptr);
178 b->shutdown=(int)num;
179 b->init=1;
180 break;
181 case BIO_C_GET_FD:
182 if (b->init)
183 {
184 ip=(int *)ptr;
185 if (ip != NULL) *ip=b->num;
186 ret=b->num;
187 }
188 else
189 ret= -1;
190 break;
191 case BIO_CTRL_GET_CLOSE:
192 ret=b->shutdown;
193 break;
194 case BIO_CTRL_SET_CLOSE:
195 b->shutdown=(int)num;
196 break;
197 case BIO_CTRL_DUP:
198 case BIO_CTRL_FLUSH:
199 ret=1;
200 break;
201 default:
202 ret=0;
203 break;
204 }
205 return(ret);
206 }
207
208static int sock_puts(BIO *bp, const char *str)
209 {
210 int n,ret;
211
212 n=strlen(str);
213 ret=sock_write(bp,str,n);
214 return(ret);
215 }
216
217int BIO_sock_should_retry(int i)
218 {
219 int err;
220
221 if ((i == 0) || (i == -1))
222 {
223 err=get_last_socket_error();
224
225#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
226 if ((i == -1) && (err == 0))
227 return(1);
228#endif
229
230 return(BIO_sock_non_fatal_error(err));
231 }
232 return(0);
233 }
234
235int BIO_sock_non_fatal_error(int err)
236 {
237 switch (err)
238 {
239#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
240# if defined(WSAEWOULDBLOCK)
241 case WSAEWOULDBLOCK:
242# endif
243
244# if 0 /* This appears to always be an error */
245# if defined(WSAENOTCONN)
246 case WSAENOTCONN:
247# endif
248# endif
249#endif
250
251#ifdef EWOULDBLOCK
252# ifdef WSAEWOULDBLOCK
253# if WSAEWOULDBLOCK != EWOULDBLOCK
254 case EWOULDBLOCK:
255# endif
256# else
257 case EWOULDBLOCK:
258# endif
259#endif
260
261#if defined(ENOTCONN)
262 case ENOTCONN:
263#endif
264
265#ifdef EINTR
266 case EINTR:
267#endif
268
269#ifdef EAGAIN
270# if EWOULDBLOCK != EAGAIN
271 case EAGAIN:
272# endif
273#endif
274
275#ifdef EPROTO
276 case EPROTO:
277#endif
278
279#ifdef EINPROGRESS
280 case EINPROGRESS:
281#endif
282
283#ifdef EALREADY
284 case EALREADY:
285#endif
286 return(1);
287 /* break; */
288 default:
289 break;
290 }
291 return(0);
292 }
293
294#endif /* #ifndef OPENSSL_NO_SOCK */