summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:50 +0000
committermarkus <>2002-09-05 12:51:50 +0000
commit15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch)
treebf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/bio
parent027351f729b9e837200dae6e1520cda6577ab930 (diff)
downloadopenbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/bio')
-rw-r--r--src/lib/libcrypto/bio/b_dump.c129
-rw-r--r--src/lib/libcrypto/bio/b_print.c771
-rw-r--r--src/lib/libcrypto/bio/b_sock.c386
-rw-r--r--src/lib/libcrypto/bio/bf_buff.c120
-rw-r--r--src/lib/libcrypto/bio/bf_lbuf.c12
-rw-r--r--src/lib/libcrypto/bio/bf_nbio.c79
-rw-r--r--src/lib/libcrypto/bio/bf_null.c73
-rw-r--r--src/lib/libcrypto/bio/bio.h456
-rw-r--r--src/lib/libcrypto/bio/bio_cb.c15
-rw-r--r--src/lib/libcrypto/bio/bio_err.c149
-rw-r--r--src/lib/libcrypto/bio/bio_lib.c208
-rw-r--r--src/lib/libcrypto/bio/bss_acpt.c167
-rw-r--r--src/lib/libcrypto/bio/bss_bio.c318
-rw-r--r--src/lib/libcrypto/bio/bss_conn.c220
-rw-r--r--src/lib/libcrypto/bio/bss_fd.c226
-rw-r--r--src/lib/libcrypto/bio/bss_file.c91
-rw-r--r--src/lib/libcrypto/bio/bss_log.c325
-rw-r--r--src/lib/libcrypto/bio/bss_mem.c130
-rw-r--r--src/lib/libcrypto/bio/bss_null.c57
-rw-r--r--src/lib/libcrypto/bio/bss_sock.c220
20 files changed, 2749 insertions, 1403 deletions
diff --git a/src/lib/libcrypto/bio/b_dump.c b/src/lib/libcrypto/bio/b_dump.c
index db84ad3d47..8397cfab6a 100644
--- a/src/lib/libcrypto/bio/b_dump.c
+++ b/src/lib/libcrypto/bio/b_dump.c
@@ -62,64 +62,91 @@
62 62
63#include <stdio.h> 63#include <stdio.h>
64#include "cryptlib.h" 64#include "cryptlib.h"
65#include "bio.h" 65#include <openssl/bio.h>
66 66
67#define TRUNCATE 67#define TRUNCATE
68#define DUMP_WIDTH 16 68#define DUMP_WIDTH 16
69#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
69 70
70int BIO_dump(bio,s,len) 71int BIO_dump(BIO *bio, const char *s, int len)
71BIO *bio; 72 {
72char *s; 73 return BIO_dump_indent(bio, s, len, 0);
73int len; 74 }
74{
75 int ret=0;
76 char buf[160+1],tmp[20];
77 int i,j,rows,trunc;
78 unsigned char ch;
79
80 trunc=0;
81 75
76int BIO_dump_indent(BIO *bio, const char *s, int len, int indent)
77 {
78 int ret=0;
79 char buf[288+1],tmp[20],str[128+1];
80 int i,j,rows,trunc;
81 unsigned char ch;
82 int dump_width;
83
84 trunc=0;
85
82#ifdef TRUNCATE 86#ifdef TRUNCATE
83 for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--) 87 for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--)
84 trunc++; 88 trunc++;
85#endif 89#endif
86 90
87 rows=(len/DUMP_WIDTH); 91 if (indent < 0)
88 if ((rows*DUMP_WIDTH)<len) 92 indent = 0;
89 rows++; 93 if (indent)
90 for(i=0;i<rows;i++) { 94 {
91 buf[0]='\0'; /* start with empty string */ 95 if (indent > 128) indent=128;
92 sprintf(tmp,"%04x - ",i*DUMP_WIDTH); 96 memset(str,' ',indent);
93 strcpy(buf,tmp); 97 }
94 for(j=0;j<DUMP_WIDTH;j++) { 98 str[indent]='\0';
95 if (((i*DUMP_WIDTH)+j)>=len) { 99
96 strcat(buf," "); 100 dump_width=DUMP_WIDTH_LESS_INDENT(indent);
97 } else { 101 rows=(len/dump_width);
98 ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff; 102 if ((rows*dump_width)<len)
99 sprintf(tmp,"%02x%c",ch,j==7?'-':' '); 103 rows++;
100 strcat(buf,tmp); 104 for(i=0;i<rows;i++)
101 } 105 {
102 } 106 buf[0]='\0'; /* start with empty string */
103 strcat(buf," "); 107 strcpy(buf,str);
104 for(j=0;j<DUMP_WIDTH;j++) { 108 sprintf(tmp,"%04x - ",i*dump_width);
105 if (((i*DUMP_WIDTH)+j)>=len) 109 strcat(buf,tmp);
106 break; 110 for(j=0;j<dump_width;j++)
107 ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff; 111 {
108 sprintf(tmp,"%c",((ch>=' ')&&(ch<='~'))?ch:'.'); 112 if (((i*dump_width)+j)>=len)
109 strcat(buf,tmp); 113 {
110 } 114 strcat(buf," ");
111 strcat(buf,"\n"); 115 }
112 /* if this is the last call then update the ddt_dump thing so that 116 else
113 * we will move the selection point in the debug window 117 {
114 */ 118 ch=((unsigned char)*(s+i*dump_width+j)) & 0xff;
115 ret+=BIO_write(bio,(char *)buf,strlen(buf)); 119 sprintf(tmp,"%02x%c",ch,j==7?'-':' ');
116 } 120 strcat(buf,tmp);
121 }
122 }
123 strcat(buf," ");
124 for(j=0;j<dump_width;j++)
125 {
126 if (((i*dump_width)+j)>=len)
127 break;
128 ch=((unsigned char)*(s+i*dump_width+j)) & 0xff;
129#ifndef CHARSET_EBCDIC
130 sprintf(tmp,"%c",((ch>=' ')&&(ch<='~'))?ch:'.');
131#else
132 sprintf(tmp,"%c",((ch>=os_toascii[' '])&&(ch<=os_toascii['~']))
133 ? os_toebcdic[ch]
134 : '.');
135#endif
136 strcat(buf,tmp);
137 }
138 strcat(buf,"\n");
139 /* if this is the last call then update the ddt_dump thing so that
140 * we will move the selection point in the debug window
141 */
142 ret+=BIO_write(bio,(char *)buf,strlen(buf));
143 }
117#ifdef TRUNCATE 144#ifdef TRUNCATE
118 if (trunc > 0) { 145 if (trunc > 0)
119 sprintf(buf,"%04x - <SPACES/NULS>\n",len+trunc); 146 {
120 ret+=BIO_write(bio,(char *)buf,strlen(buf)); 147 sprintf(buf,"%s%04x - <SPACES/NULS>\n",str,len+trunc);
121 } 148 ret+=BIO_write(bio,(char *)buf,strlen(buf));
149 }
122#endif 150#endif
123 return(ret); 151 return(ret);
124} 152 }
125
diff --git a/src/lib/libcrypto/bio/b_print.c b/src/lib/libcrypto/bio/b_print.c
index cdadeb839a..3ce1290772 100644
--- a/src/lib/libcrypto/bio/b_print.c
+++ b/src/lib/libcrypto/bio/b_print.c
@@ -56,37 +56,774 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 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
59/* 66/*
60 * Stolen from tjh's ssl/ssl_trc.c stuff. 67 * Stolen from tjh's ssl/ssl_trc.c stuff.
61 */ 68 */
62 69
63#include <stdio.h> 70#include <stdio.h>
71#include <string.h>
72#include <ctype.h>
73#include <assert.h>
74#include <limits.h>
64#include "cryptlib.h" 75#include "cryptlib.h"
65#include "bio.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#ifdef BN_LLONG
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#if HAVE_LONG_DOUBLE
113#define LDOUBLE long double
114#else
115#define LDOUBLE double
116#endif
117
118#if HAVE_LONG_LONG
119# if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
120# define LLONG _int64
121# else
122# define LLONG long long
123# endif
124#else
125#define LLONG long
126#endif
127
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);
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 unsigned LLONG uvalue;
486 char convert[20];
487 int place = 0;
488 int spadlen = 0;
489 int zpadlen = 0;
490 int caps = 0;
491
492 if (max < 0)
493 max = 0;
494 uvalue = value;
495 if (!(flags & DP_F_UNSIGNED)) {
496 if (value < 0) {
497 signvalue = '-';
498 uvalue = -value;
499 } else if (flags & DP_F_PLUS)
500 signvalue = '+';
501 else if (flags & DP_F_SPACE)
502 signvalue = ' ';
503 }
504 if (flags & DP_F_UP)
505 caps = 1;
506 do {
507 convert[place++] =
508 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
509 [uvalue % (unsigned) base];
510 uvalue = (uvalue / (unsigned) base);
511 } while (uvalue && (place < 20));
512 if (place == 20)
513 place--;
514 convert[place] = 0;
515
516 zpadlen = max - place;
517 spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0);
518 if (zpadlen < 0)
519 zpadlen = 0;
520 if (spadlen < 0)
521 spadlen = 0;
522 if (flags & DP_F_ZERO) {
523 zpadlen = OSSL_MAX(zpadlen, spadlen);
524 spadlen = 0;
525 }
526 if (flags & DP_F_MINUS)
527 spadlen = -spadlen;
528
529 /* spaces */
530 while (spadlen > 0) {
531 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
532 --spadlen;
533 }
534
535 /* sign */
536 if (signvalue)
537 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
538
539 /* zeros */
540 if (zpadlen > 0) {
541 while (zpadlen > 0) {
542 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
543 --zpadlen;
544 }
545 }
546 /* digits */
547 while (place > 0)
548 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
549
550 /* left justified spaces */
551 while (spadlen < 0) {
552 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
553 ++spadlen;
554 }
555 return;
556}
557
558static LDOUBLE
559abs_val(LDOUBLE value)
560{
561 LDOUBLE result = value;
562 if (value < 0)
563 result = -value;
564 return result;
565}
566
567static LDOUBLE
568pow10(int exp)
569{
570 LDOUBLE result = 1;
571 while (exp) {
572 result *= 10;
573 exp--;
574 }
575 return result;
576}
577
578static long
579roundv(LDOUBLE value)
580{
581 long intpart;
582 intpart = (long) value;
583 value = value - intpart;
584 if (value >= 0.5)
585 intpart++;
586 return intpart;
587}
588
589static void
590fmtfp(
591 char **sbuffer,
592 char **buffer,
593 size_t *currlen,
594 size_t *maxlen,
595 LDOUBLE fvalue,
596 int min,
597 int max,
598 int flags)
599{
600 int signvalue = 0;
601 LDOUBLE ufvalue;
602 char iconvert[20];
603 char fconvert[20];
604 int iplace = 0;
605 int fplace = 0;
606 int padlen = 0;
607 int zpadlen = 0;
608 int caps = 0;
609 long intpart;
610 long fracpart;
611
612 if (max < 0)
613 max = 6;
614 ufvalue = abs_val(fvalue);
615 if (fvalue < 0)
616 signvalue = '-';
617 else if (flags & DP_F_PLUS)
618 signvalue = '+';
619 else if (flags & DP_F_SPACE)
620 signvalue = ' ';
66 621
67int BIO_printf ( VAR_PLIST( BIO *, bio ) ) 622 intpart = (long)ufvalue;
68VAR_ALIST 623
624 /* sorry, we only support 9 digits past the decimal because of our
625 conversion method */
626 if (max > 9)
627 max = 9;
628
629 /* we "cheat" by converting the fractional part to integer by
630 multiplying by a factor of 10 */
631 fracpart = roundv((pow10(max)) * (ufvalue - intpart));
632
633 if (fracpart >= pow10(max)) {
634 intpart++;
635 fracpart -= (long)pow10(max);
636 }
637
638 /* convert integer part */
639 do {
640 iconvert[iplace++] =
641 (caps ? "0123456789ABCDEF"
642 : "0123456789abcdef")[intpart % 10];
643 intpart = (intpart / 10);
644 } while (intpart && (iplace < 20));
645 if (iplace == 20)
646 iplace--;
647 iconvert[iplace] = 0;
648
649 /* convert fractional part */
650 do {
651 fconvert[fplace++] =
652 (caps ? "0123456789ABCDEF"
653 : "0123456789abcdef")[fracpart % 10];
654 fracpart = (fracpart / 10);
655 } while (fplace < max);
656 if (fplace == 20)
657 fplace--;
658 fconvert[fplace] = 0;
659
660 /* -1 for decimal point, another -1 if we are printing a sign */
661 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
662 zpadlen = max - fplace;
663 if (zpadlen < 0)
664 zpadlen = 0;
665 if (padlen < 0)
666 padlen = 0;
667 if (flags & DP_F_MINUS)
668 padlen = -padlen;
669
670 if ((flags & DP_F_ZERO) && (padlen > 0)) {
671 if (signvalue) {
672 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
673 --padlen;
674 signvalue = 0;
675 }
676 while (padlen > 0) {
677 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
678 --padlen;
679 }
680 }
681 while (padlen > 0) {
682 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
683 --padlen;
684 }
685 if (signvalue)
686 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
687
688 while (iplace > 0)
689 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
690
691 /*
692 * Decimal point. This should probably use locale to find the correct
693 * char to print out.
694 */
695 if (max > 0) {
696 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
697
698 while (fplace > 0)
699 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
700 }
701 while (zpadlen > 0) {
702 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
703 --zpadlen;
704 }
705
706 while (padlen < 0) {
707 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
708 ++padlen;
709 }
710}
711
712static void
713doapr_outch(
714 char **sbuffer,
715 char **buffer,
716 size_t *currlen,
717 size_t *maxlen,
718 int c)
719{
720 /* If we haven't at least one buffer, someone has doe a big booboo */
721 assert(*sbuffer != NULL || buffer != NULL);
722
723 if (buffer) {
724 while (*currlen >= *maxlen) {
725 if (*buffer == NULL) {
726 if (*maxlen == 0)
727 *maxlen = 1024;
728 *buffer = OPENSSL_malloc(*maxlen);
729 if (*currlen > 0) {
730 assert(*sbuffer != NULL);
731 memcpy(*buffer, *sbuffer, *currlen);
732 }
733 *sbuffer = NULL;
734 } else {
735 *maxlen += 1024;
736 *buffer = OPENSSL_realloc(*buffer, *maxlen);
737 }
738 }
739 /* What to do if *buffer is NULL? */
740 assert(*sbuffer != NULL || *buffer != NULL);
741 }
742
743 if (*currlen < *maxlen) {
744 if (*sbuffer)
745 (*sbuffer)[(*currlen)++] = (char)c;
746 else
747 (*buffer)[(*currlen)++] = (char)c;
748 }
749
750 return;
751}
752
753/***************************************************************************/
754
755int BIO_printf (BIO *bio, const char *format, ...)
69 { 756 {
70 VAR_BDEFN(args, BIO *, bio); 757 va_list args;
71 char *format;
72 int ret; 758 int ret;
73 MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */
74 759
75 VAR_INIT(args, BIO *, bio); 760 va_start(args, format);
76 VAR_ARG(args, char *, format);
77 761
78 hugebuf[0]='\0'; 762 ret = BIO_vprintf(bio, format, args);
79 763
80/* no-one uses _doprnt anymore and it appears to be broken under SunOS 4.1.4 */ 764 va_end(args);
81#if 0 && defined(sun) && !defined(VAR_ANSI) /**/ 765 return(ret);
82 _doprnt(hugebuf,format,args); 766 }
83#else /* !sun */
84 vsprintf(hugebuf,format,args);
85#endif /* sun */
86 767
87 ret=BIO_write(bio,hugebuf,strlen(hugebuf)); 768int BIO_vprintf (BIO *bio, const char *format, va_list args)
769 {
770 int ret;
771 size_t retlen;
772 char hugebuf[1024*2]; /* Was previously 10k, which is unreasonable
773 in small-stack environments, like threads
774 or DOS programs. */
775 char *hugebufp = hugebuf;
776 size_t hugebufsize = sizeof(hugebuf);
777 char *dynbuf = NULL;
778 int ignored;
88 779
89 VAR_END( args ); 780 dynbuf = NULL;
781 CRYPTO_push_info("doapr()");
782 _dopr(&hugebufp, &dynbuf, &hugebufsize,
783 &retlen, &ignored, format, args);
784 if (dynbuf)
785 {
786 ret=BIO_write(bio, dynbuf, (int)retlen);
787 OPENSSL_free(dynbuf);
788 }
789 else
790 {
791 ret=BIO_write(bio, hugebuf, (int)retlen);
792 }
793 CRYPTO_pop_info();
90 return(ret); 794 return(ret);
91 } 795 }
92 796
797/* As snprintf is not available everywhere, we provide our own implementation.
798 * This function has nothing to do with BIOs, but it's closely related
799 * to BIO_printf, and we need *some* name prefix ...
800 * (XXX the function should be renamed, but to what?) */
801int BIO_snprintf(char *buf, size_t n, const char *format, ...)
802 {
803 va_list args;
804 int ret;
805
806 va_start(args, format);
807
808 ret = BIO_vsnprintf(buf, n, format, args);
809
810 va_end(args);
811 return(ret);
812 }
813
814int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
815 {
816 size_t retlen;
817 int truncated;
818
819 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
820
821 if (truncated)
822 /* In case of truncation, return -1 like traditional snprintf.
823 * (Current drafts for ISO/IEC 9899 say snprintf should return
824 * the number of characters that would have been written,
825 * had the buffer been large enough.) */
826 return -1;
827 else
828 return (retlen <= INT_MAX) ? retlen : -1;
829 }
diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c
index a45909527c..dcaef68ea7 100644
--- a/src/lib/libcrypto/bio/b_sock.c
+++ b/src/lib/libcrypto/bio/b_sock.c
@@ -56,32 +56,30 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#ifndef NO_SOCK 59#ifndef OPENSSL_NO_SOCK
60 60
61#include <stdio.h> 61#include <stdio.h>
62#include <stdlib.h> 62#include <stdlib.h>
63#include <errno.h> 63#include <errno.h>
64#define USE_SOCKETS 64#define USE_SOCKETS
65#include "cryptlib.h" 65#include "cryptlib.h"
66#include "bio.h" 66#include <openssl/bio.h>
67 67
68/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ 68#ifdef OPENSSL_SYS_WIN16
69
70#ifdef WIN16
71#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ 69#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
72#else 70#else
73#define SOCKET_PROTOCOL IPPROTO_TCP 71#define SOCKET_PROTOCOL IPPROTO_TCP
74#endif 72#endif
75 73
76#ifdef SO_MAXCONN 74#ifdef SO_MAXCONN
77#define MAX_LISTEN SOMAXCONN
78#elif defined(SO_MAXCONN)
79#define MAX_LISTEN SO_MAXCONN 75#define MAX_LISTEN SO_MAXCONN
76#elif defined(SOMAXCONN)
77#define MAX_LISTEN SOMAXCONN
80#else 78#else
81#define MAX_LISTEN 32 79#define MAX_LISTEN 32
82#endif 80#endif
83 81
84#ifdef WINDOWS 82#ifdef OPENSSL_SYS_WINDOWS
85static int wsa_init_done=0; 83static int wsa_init_done=0;
86#endif 84#endif
87 85
@@ -96,59 +94,67 @@ static struct ghbn_cache_st
96 unsigned long order; 94 unsigned long order;
97 } ghbn_cache[GHBN_NUM]; 95 } ghbn_cache[GHBN_NUM];
98 96
99#ifndef NOPROTO 97static int get_ip(const char *str,unsigned char *ip);
100static int get_ip(char *str,unsigned char *ip); 98#if 0
101static void ghbn_free(struct hostent *a); 99static void ghbn_free(struct hostent *a);
102static struct hostent *ghbn_dup(struct hostent *a); 100static struct hostent *ghbn_dup(struct hostent *a);
103#else
104static int get_ip();
105static void ghbn_free();
106static struct hostent *ghbn_dup();
107#endif 101#endif
108 102int BIO_get_host_ip(const char *str, unsigned char *ip)
109int BIO_get_host_ip(str,ip)
110char *str;
111unsigned char *ip;
112 { 103 {
113 int i; 104 int i;
105 int err = 1;
106 int locked = 0;
114 struct hostent *he; 107 struct hostent *he;
115 108
116 i=get_ip(str,ip); 109 i=get_ip(str,ip);
117 if (i > 0) return(1);
118 if (i < 0) 110 if (i < 0)
119 { 111 {
120 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS); 112 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS);
121 ERR_add_error_data(2,"host=",str); 113 goto err;
122 return(0);
123 } 114 }
124 else
125 { /* do a gethostbyname */
126 if (!BIO_sock_init()) return(0);
127 115
128 he=BIO_gethostbyname(str); 116 /* At this point, we have something that is most probably correct
129 if (he == NULL) 117 in some way, so let's init the socket. */
130 { 118 if (BIO_sock_init() != 1)
131 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP); 119 return 0; /* don't generate another error code here */
132 ERR_add_error_data(2,"host=",str);
133 return(0);
134 }
135 120
136 /* cast to short because of win16 winsock definition */ 121 /* If the string actually contained an IP address, we need not do
137 if ((short)he->h_addrtype != AF_INET) 122 anything more */
138 { 123 if (i > 0) return(1);
139 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); 124
140 ERR_add_error_data(2,"host=",str); 125 /* do a gethostbyname */
141 return(0); 126 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
142 } 127 locked = 1;
143 for (i=0; i<4; i++) 128 he=BIO_gethostbyname(str);
144 ip[i]=he->h_addr_list[0][i]; 129 if (he == NULL)
130 {
131 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP);
132 goto err;
145 } 133 }
146 return(1); 134
135 /* cast to short because of win16 winsock definition */
136 if ((short)he->h_addrtype != AF_INET)
137 {
138 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
139 goto err;
140 }
141 for (i=0; i<4; i++)
142 ip[i]=he->h_addr_list[0][i];
143 err = 0;
144
145 err:
146 if (locked)
147 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
148 if (err)
149 {
150 ERR_add_error_data(2,"host=",str);
151 return 0;
152 }
153 else
154 return 1;
147 } 155 }
148 156
149int BIO_get_port(str,port_ptr) 157int BIO_get_port(const char *str, unsigned short *port_ptr)
150char *str;
151short *port_ptr;
152 { 158 {
153 int i; 159 int i;
154 struct servent *s; 160 struct servent *s;
@@ -163,8 +169,19 @@ short *port_ptr;
163 *port_ptr=(unsigned short)i; 169 *port_ptr=(unsigned short)i;
164 else 170 else
165 { 171 {
166 s=getservbyname(str,"tcp"); 172 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
167 if (s == NULL) 173 /* Note: under VMS with SOCKETSHR, it seems like the first
174 * parameter is 'char *', instead of 'const char *'
175 */
176 s=getservbyname(
177#ifndef CONST_STRICT
178 (char *)
179#endif
180 str,"tcp");
181 if(s != NULL)
182 *port_ptr=ntohs((unsigned short)s->s_port);
183 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
184 if(s == NULL)
168 { 185 {
169 if (strcmp(str,"http") == 0) 186 if (strcmp(str,"http") == 0)
170 *port_ptr=80; 187 *port_ptr=80;
@@ -190,31 +207,30 @@ short *port_ptr;
190 ERR_add_error_data(3,"service='",str,"'"); 207 ERR_add_error_data(3,"service='",str,"'");
191 return(0); 208 return(0);
192 } 209 }
193 return(1);
194 } 210 }
195 *port_ptr=htons((unsigned short)s->s_port);
196 } 211 }
197 return(1); 212 return(1);
198 } 213 }
199 214
200int BIO_sock_error(sock) 215int BIO_sock_error(int sock)
201int sock;
202 { 216 {
203 int j,i,size; 217 int j,i;
218 int size;
204 219
205 size=sizeof(int); 220 size=sizeof(int);
206 221 /* Note: under Windows the third parameter is of type (char *)
207 i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(char *)&j,&size); 222 * whereas under other systems it is (void *) if you don't have
223 * a cast it will choke the compiler: if you do have a cast then
224 * you can either go for (char *) or (void *).
225 */
226 i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void *)&j,(void *)&size);
208 if (i < 0) 227 if (i < 0)
209 return(1); 228 return(1);
210 else 229 else
211 return(j); 230 return(j);
212 } 231 }
213 232
214long BIO_ghbn_ctrl(cmd,iarg,parg) 233long BIO_ghbn_ctrl(int cmd, int iarg, char *parg)
215int cmd;
216int iarg;
217char *parg;
218 { 234 {
219 int i; 235 int i;
220 char **p; 236 char **p;
@@ -223,13 +239,13 @@ char *parg;
223 { 239 {
224 case BIO_GHBN_CTRL_HITS: 240 case BIO_GHBN_CTRL_HITS:
225 return(BIO_ghbn_hits); 241 return(BIO_ghbn_hits);
226 break; 242 /* break; */
227 case BIO_GHBN_CTRL_MISSES: 243 case BIO_GHBN_CTRL_MISSES:
228 return(BIO_ghbn_miss); 244 return(BIO_ghbn_miss);
229 break; 245 /* break; */
230 case BIO_GHBN_CTRL_CACHE_SIZE: 246 case BIO_GHBN_CTRL_CACHE_SIZE:
231 return(GHBN_NUM); 247 return(GHBN_NUM);
232 break; 248 /* break; */
233 case BIO_GHBN_CTRL_GET_ENTRY: 249 case BIO_GHBN_CTRL_GET_ENTRY:
234 if ((iarg >= 0) && (iarg <GHBN_NUM) && 250 if ((iarg >= 0) && (iarg <GHBN_NUM) &&
235 (ghbn_cache[iarg].order > 0)) 251 (ghbn_cache[iarg].order > 0))
@@ -241,7 +257,7 @@ char *parg;
241 return(1); 257 return(1);
242 } 258 }
243 return(0); 259 return(0);
244 break; 260 /* break; */
245 case BIO_GHBN_CTRL_FLUSH: 261 case BIO_GHBN_CTRL_FLUSH:
246 for (i=0; i<GHBN_NUM; i++) 262 for (i=0; i<GHBN_NUM; i++)
247 ghbn_cache[i].order=0; 263 ghbn_cache[i].order=0;
@@ -252,85 +268,105 @@ char *parg;
252 return(1); 268 return(1);
253 } 269 }
254 270
255static struct hostent *ghbn_dup(a) 271#if 0
256struct hostent *a; 272static struct hostent *ghbn_dup(struct hostent *a)
257 { 273 {
258 struct hostent *ret; 274 struct hostent *ret;
259 int i,j; 275 int i,j;
260 276
261 ret=(struct hostent *)malloc(sizeof(struct hostent)); 277 MemCheck_off();
278 ret=(struct hostent *)OPENSSL_malloc(sizeof(struct hostent));
262 if (ret == NULL) return(NULL); 279 if (ret == NULL) return(NULL);
263 memset(ret,0,sizeof(struct hostent)); 280 memset(ret,0,sizeof(struct hostent));
264 281
265 for (i=0; a->h_aliases[i] != NULL; i++) 282 for (i=0; a->h_aliases[i] != NULL; i++)
266 ; 283 ;
267 i++; 284 i++;
268 ret->h_aliases=(char **)malloc(sizeof(char *)*i); 285 ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *));
269 memset(ret->h_aliases,0,sizeof(char *)*i); 286 if (ret->h_aliases == NULL)
270 if (ret == NULL) goto err; 287 goto err;
288 memset(ret->h_aliases, 0, i*sizeof(char *));
271 289
272 for (i=0; a->h_addr_list[i] != NULL; i++) 290 for (i=0; a->h_addr_list[i] != NULL; i++)
273 ; 291 ;
274 i++; 292 i++;
275 ret->h_addr_list=(char **)malloc(sizeof(char *)*i); 293 ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *));
276 memset(ret->h_addr_list,0,sizeof(char *)*i); 294 if (ret->h_addr_list == NULL)
277 if (ret->h_addr_list == NULL) goto err; 295 goto err;
296 memset(ret->h_addr_list, 0, i*sizeof(char *));
278 297
279 j=strlen(a->h_name)+1; 298 j=strlen(a->h_name)+1;
280 if ((ret->h_name=malloc(j)) == NULL) goto err; 299 if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err;
281 memcpy((char *)ret->h_name,a->h_name,j); 300 memcpy((char *)ret->h_name,a->h_name,j);
282 for (i=0; a->h_aliases[i] != NULL; i++) 301 for (i=0; a->h_aliases[i] != NULL; i++)
283 { 302 {
284 j=strlen(a->h_aliases[i])+1; 303 j=strlen(a->h_aliases[i])+1;
285 if ((ret->h_aliases[i]=malloc(j)) == NULL) goto err; 304 if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err;
286 memcpy(ret->h_aliases[i],a->h_aliases[i],j); 305 memcpy(ret->h_aliases[i],a->h_aliases[i],j);
287 } 306 }
288 ret->h_length=a->h_length; 307 ret->h_length=a->h_length;
289 ret->h_addrtype=a->h_addrtype; 308 ret->h_addrtype=a->h_addrtype;
290 for (i=0; a->h_addr_list[i] != NULL; i++) 309 for (i=0; a->h_addr_list[i] != NULL; i++)
291 { 310 {
292 if ((ret->h_addr_list[i]=malloc(a->h_length)) == NULL) 311 if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL)
293 goto err; 312 goto err;
294 memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length); 313 memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);
295 } 314 }
296 return(ret); 315 if (0)
316 {
297err: 317err:
298 if (ret != NULL) 318 if (ret != NULL)
299 ghbn_free(ret); 319 ghbn_free(ret);
300 return(NULL); 320 ret=NULL;
321 }
322 MemCheck_on();
323 return(ret);
301 } 324 }
302 325
303static void ghbn_free(a) 326static void ghbn_free(struct hostent *a)
304struct hostent *a;
305 { 327 {
306 int i; 328 int i;
307 329
330 if(a == NULL)
331 return;
332
308 if (a->h_aliases != NULL) 333 if (a->h_aliases != NULL)
309 { 334 {
310 for (i=0; a->h_aliases[i] != NULL; i++) 335 for (i=0; a->h_aliases[i] != NULL; i++)
311 free(a->h_aliases[i]); 336 OPENSSL_free(a->h_aliases[i]);
312 free(a->h_aliases); 337 OPENSSL_free(a->h_aliases);
313 } 338 }
314 if (a->h_addr_list != NULL) 339 if (a->h_addr_list != NULL)
315 { 340 {
316 for (i=0; a->h_addr_list[i] != NULL; i++) 341 for (i=0; a->h_addr_list[i] != NULL; i++)
317 free(a->h_addr_list[i]); 342 OPENSSL_free(a->h_addr_list[i]);
318 free(a->h_addr_list); 343 OPENSSL_free(a->h_addr_list);
319 } 344 }
320 if (a->h_name != NULL) free((char *)a->h_name); 345 if (a->h_name != NULL) OPENSSL_free(a->h_name);
321 free(a); 346 OPENSSL_free(a);
322 } 347 }
323 348
324struct hostent *BIO_gethostbyname(name) 349#endif
325char *name; 350
351struct hostent *BIO_gethostbyname(const char *name)
326 { 352 {
353#if 1
354 /* Caching gethostbyname() results forever is wrong,
355 * so we have to let the true gethostbyname() worry about this */
356 return gethostbyname(name);
357#else
327 struct hostent *ret; 358 struct hostent *ret;
328 int i,lowi=0,j; 359 int i,lowi=0,j;
329 unsigned long low= (unsigned long)-1; 360 unsigned long low= (unsigned long)-1;
330 361
331/* return(gethostbyname(name)); */
332 362
333 CRYPTO_w_lock(CRYPTO_LOCK_BIO_GETHOSTBYNAME); 363# if 0
364 /* It doesn't make sense to use locking here: The function interface
365 * is not thread-safe, because threads can never be sure when
366 * some other thread destroys the data they were given a pointer to.
367 */
368 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
369# endif
334 j=strlen(name); 370 j=strlen(name);
335 if (j < 128) 371 if (j < 128)
336 { 372 {
@@ -354,17 +390,39 @@ char *name;
354 if (i == GHBN_NUM) /* no hit*/ 390 if (i == GHBN_NUM) /* no hit*/
355 { 391 {
356 BIO_ghbn_miss++; 392 BIO_ghbn_miss++;
357 ret=gethostbyname(name); 393 /* Note: under VMS with SOCKETSHR, it seems like the first
358 394 * parameter is 'char *', instead of 'const char *'
359 if (ret == NULL) return(NULL); 395 */
360 if (j > 128) return(ret); /* too big to cache */ 396 ret=gethostbyname(
397# ifndef CONST_STRICT
398 (char *)
399# endif
400 name);
401
402 if (ret == NULL)
403 goto end;
404 if (j > 128) /* too big to cache */
405 {
406# if 0
407 /* If we were trying to make this function thread-safe (which
408 * is bound to fail), we'd have to give up in this case
409 * (or allocate more memory). */
410 ret = NULL;
411# endif
412 goto end;
413 }
361 414
362 /* else add to cache */ 415 /* else add to cache */
363 if (ghbn_cache[lowi].ent != NULL) 416 if (ghbn_cache[lowi].ent != NULL)
364 ghbn_free(ghbn_cache[lowi].ent); 417 ghbn_free(ghbn_cache[lowi].ent); /* XXX not thread-safe */
418 ghbn_cache[lowi].name[0] = '\0';
365 419
420 if((ret=ghbn_cache[lowi].ent=ghbn_dup(ret)) == NULL)
421 {
422 BIOerr(BIO_F_BIO_GETHOSTBYNAME,ERR_R_MALLOC_FAILURE);
423 goto end;
424 }
366 strncpy(ghbn_cache[lowi].name,name,128); 425 strncpy(ghbn_cache[lowi].name,name,128);
367 ghbn_cache[lowi].ent=ghbn_dup(ret);
368 ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits; 426 ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits;
369 } 427 }
370 else 428 else
@@ -373,13 +431,18 @@ char *name;
373 ret= ghbn_cache[i].ent; 431 ret= ghbn_cache[i].ent;
374 ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits; 432 ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits;
375 } 433 }
376 CRYPTO_w_unlock(CRYPTO_LOCK_BIO_GETHOSTBYNAME); 434end:
435# if 0
436 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
437# endif
377 return(ret); 438 return(ret);
439#endif
378 } 440 }
379 441
380int BIO_sock_init() 442
443int BIO_sock_init(void)
381 { 444 {
382#ifdef WINDOWS 445#ifdef OPENSSL_SYS_WINDOWS
383 static struct WSAData wsa_state; 446 static struct WSAData wsa_state;
384 447
385 if (!wsa_init_done) 448 if (!wsa_init_done)
@@ -399,13 +462,13 @@ int BIO_sock_init()
399 return(-1); 462 return(-1);
400 } 463 }
401 } 464 }
402#endif /* WINDOWS */ 465#endif /* OPENSSL_SYS_WINDOWS */
403 return(1); 466 return(1);
404 } 467 }
405 468
406void BIO_sock_cleanup() 469void BIO_sock_cleanup(void)
407 { 470 {
408#ifdef WINDOWS 471#ifdef OPENSSL_SYS_WINDOWS
409 if (wsa_init_done) 472 if (wsa_init_done)
410 { 473 {
411 wsa_init_done=0; 474 wsa_init_done=0;
@@ -415,10 +478,9 @@ void BIO_sock_cleanup()
415#endif 478#endif
416 } 479 }
417 480
418int BIO_socket_ioctl(fd,type,arg) 481#if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
419int fd; 482
420long type; 483int BIO_socket_ioctl(int fd, long type, unsigned long *arg)
421unsigned long *arg;
422 { 484 {
423 int i; 485 int i;
424 486
@@ -427,12 +489,11 @@ unsigned long *arg;
427 SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error()); 489 SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error());
428 return(i); 490 return(i);
429 } 491 }
492#endif /* __VMS_VER */
430 493
431/* The reason I have implemented this instead of using sscanf is because 494/* The reason I have implemented this instead of using sscanf is because
432 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */ 495 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
433static int get_ip(str,ip) 496static int get_ip(const char *str, unsigned char ip[4])
434char *str;
435unsigned char ip[4];
436 { 497 {
437 unsigned int tmp[4]; 498 unsigned int tmp[4];
438 int num=0,c,ok=0; 499 int num=0,c,ok=0;
@@ -446,16 +507,16 @@ unsigned char ip[4];
446 { 507 {
447 ok=1; 508 ok=1;
448 tmp[num]=tmp[num]*10+c-'0'; 509 tmp[num]=tmp[num]*10+c-'0';
449 if (tmp[num] > 255) return(-1); 510 if (tmp[num] > 255) return(0);
450 } 511 }
451 else if (c == '.') 512 else if (c == '.')
452 { 513 {
453 if (!ok) return(-1); 514 if (!ok) return(-1);
454 if (num == 3) break; 515 if (num == 3) return(0);
455 num++; 516 num++;
456 ok=0; 517 ok=0;
457 } 518 }
458 else if ((num == 3) && ok) 519 else if (c == '\0' && (num == 3) && ok)
459 break; 520 break;
460 else 521 else
461 return(0); 522 return(0);
@@ -467,18 +528,19 @@ unsigned char ip[4];
467 return(1); 528 return(1);
468 } 529 }
469 530
470int BIO_get_accept_socket(host) 531int BIO_get_accept_socket(char *host, int bind_mode)
471char *host;
472 { 532 {
473 int ret=0; 533 int ret=0;
474 struct sockaddr_in server; 534 struct sockaddr_in server,client;
475 int s= -1; 535 int s=INVALID_SOCKET,cs;
476 unsigned char ip[4]; 536 unsigned char ip[4];
477 short port; 537 unsigned short port;
478 char *str,*h,*p,*e; 538 char *str=NULL,*e;
539 const char *h,*p;
479 unsigned long l; 540 unsigned long l;
541 int err_num;
480 542
481 if (!BIO_sock_init()) return(INVALID_SOCKET); 543 if (BIO_sock_init() != 1) return(INVALID_SOCKET);
482 544
483 if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET); 545 if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET);
484 546
@@ -504,25 +566,26 @@ char *host;
504 h="*"; 566 h="*";
505 } 567 }
506 568
507 if (!BIO_get_port(p,&port)) return(INVALID_SOCKET); 569 if (!BIO_get_port(p,&port)) goto err;
508 570
509 memset((char *)&server,0,sizeof(server)); 571 memset((char *)&server,0,sizeof(server));
510 server.sin_family=AF_INET; 572 server.sin_family=AF_INET;
511 server.sin_port=htons((unsigned short)port); 573 server.sin_port=htons(port);
512 574
513 if (strcmp(h,"*") == 0) 575 if (strcmp(h,"*") == 0)
514 server.sin_addr.s_addr=INADDR_ANY; 576 server.sin_addr.s_addr=INADDR_ANY;
515 else 577 else
516 { 578 {
517 if (!BIO_get_host_ip(h,&(ip[0]))) return(INVALID_SOCKET); 579 if (!BIO_get_host_ip(h,&(ip[0]))) goto err;
518 l=(unsigned long) 580 l=(unsigned long)
519 ((unsigned long)ip[0]<<24L)| 581 ((unsigned long)ip[0]<<24L)|
520 ((unsigned long)ip[0]<<16L)| 582 ((unsigned long)ip[1]<<16L)|
521 ((unsigned long)ip[0]<< 8L)| 583 ((unsigned long)ip[2]<< 8L)|
522 ((unsigned long)ip[0]); 584 ((unsigned long)ip[3]);
523 server.sin_addr.s_addr=htonl(l); 585 server.sin_addr.s_addr=htonl(l);
524 } 586 }
525 587
588again:
526 s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); 589 s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
527 if (s == INVALID_SOCKET) 590 if (s == INVALID_SOCKET)
528 { 591 {
@@ -531,9 +594,45 @@ char *host;
531 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET); 594 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET);
532 goto err; 595 goto err;
533 } 596 }
597
598#ifdef SO_REUSEADDR
599 if (bind_mode == BIO_BIND_REUSEADDR)
600 {
601 int i=1;
602
603 ret=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i));
604 bind_mode=BIO_BIND_NORMAL;
605 }
606#endif
534 if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) 607 if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
535 { 608 {
536 SYSerr(SYS_F_BIND,get_last_socket_error()); 609#ifdef SO_REUSEADDR
610 err_num=get_last_socket_error();
611 if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
612 (err_num == EADDRINUSE))
613 {
614 memcpy((char *)&client,(char *)&server,sizeof(server));
615 if (strcmp(h,"*") == 0)
616 client.sin_addr.s_addr=htonl(0x7F000001);
617 cs=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
618 if (cs != INVALID_SOCKET)
619 {
620 int ii;
621 ii=connect(cs,(struct sockaddr *)&client,
622 sizeof(client));
623 closesocket(cs);
624 if (ii == INVALID_SOCKET)
625 {
626 bind_mode=BIO_BIND_REUSEADDR;
627 closesocket(s);
628 goto again;
629 }
630 /* else error */
631 }
632 /* else error */
633 }
634#endif
635 SYSerr(SYS_F_BIND,err_num);
537 ERR_add_error_data(3,"port='",host,"'"); 636 ERR_add_error_data(3,"port='",host,"'");
538 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET); 637 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
539 goto err; 638 goto err;
@@ -547,35 +646,35 @@ char *host;
547 } 646 }
548 ret=1; 647 ret=1;
549err: 648err:
550 if (str != NULL) Free(str); 649 if (str != NULL) OPENSSL_free(str);
551 if ((ret == 0) && (s != INVALID_SOCKET)) 650 if ((ret == 0) && (s != INVALID_SOCKET))
552 { 651 {
553#ifdef WINDOWS
554 closesocket(s); 652 closesocket(s);
555#else
556 close(s);
557#endif
558 s= INVALID_SOCKET; 653 s= INVALID_SOCKET;
559 } 654 }
560 return(s); 655 return(s);
561 } 656 }
562 657
563int BIO_accept(sock,addr) 658int BIO_accept(int sock, char **addr)
564int sock;
565char **addr;
566 { 659 {
567 int ret=INVALID_SOCKET; 660 int ret=INVALID_SOCKET;
568 static struct sockaddr_in from; 661 static struct sockaddr_in from;
569 unsigned long l; 662 unsigned long l;
570 short port; 663 unsigned short port;
571 int len; 664 int len;
572 char *p; 665 char *p;
573 666
574 memset((char *)&from,0,sizeof(from)); 667 memset((char *)&from,0,sizeof(from));
575 len=sizeof(from); 668 len=sizeof(from);
576 ret=accept(sock,(struct sockaddr *)&from,&len); 669 /* Note: under VMS with SOCKETSHR the fourth parameter is currently
670 * of type (int *) whereas under other systems it is (void *) if
671 * you don't have a cast it will choke the compiler: if you do
672 * have a cast then you can either go for (int *) or (void *).
673 */
674 ret=accept(sock,(struct sockaddr *)&from,(void *)&len);
577 if (ret == INVALID_SOCKET) 675 if (ret == INVALID_SOCKET)
578 { 676 {
677 if(BIO_sock_should_retry(ret)) return -2;
579 SYSerr(SYS_F_ACCEPT,get_last_socket_error()); 678 SYSerr(SYS_F_ACCEPT,get_last_socket_error());
580 BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR); 679 BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR);
581 goto end; 680 goto end;
@@ -587,7 +686,7 @@ char **addr;
587 port=ntohs(from.sin_port); 686 port=ntohs(from.sin_port);
588 if (*addr == NULL) 687 if (*addr == NULL)
589 { 688 {
590 if ((p=Malloc(24)) == NULL) 689 if ((p=OPENSSL_malloc(24)) == NULL)
591 { 690 {
592 BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE); 691 BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
593 goto end; 692 goto end;
@@ -604,9 +703,7 @@ end:
604 return(ret); 703 return(ret);
605 } 704 }
606 705
607int BIO_set_tcp_ndelay(s,on) 706int BIO_set_tcp_ndelay(int s, int on)
608int s;
609int on;
610 { 707 {
611 int ret=0; 708 int ret=0;
612#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) 709#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
@@ -626,3 +723,14 @@ int on;
626 } 723 }
627#endif 724#endif
628 725
726int BIO_socket_nbio(int s, int mode)
727 {
728 int ret= -1;
729 unsigned long l;
730
731 l=mode;
732#ifdef FIONBIO
733 ret=BIO_socket_ioctl(s,FIONBIO,&l);
734#endif
735 return(ret == 0);
736 }
diff --git a/src/lib/libcrypto/bio/bf_buff.c b/src/lib/libcrypto/bio/bf_buff.c
index 7912b88473..6ccda06596 100644
--- a/src/lib/libcrypto/bio/bf_buff.c
+++ b/src/lib/libcrypto/bio/bf_buff.c
@@ -59,28 +59,17 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <errno.h> 60#include <errno.h>
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include "bio.h" 62#include <openssl/bio.h>
63#include "evp.h" 63
64 64static int buffer_write(BIO *h, const char *buf,int num);
65#ifndef NOPROTO 65static int buffer_read(BIO *h, char *buf, int size);
66static int buffer_write(BIO *h,char *buf,int num); 66static int buffer_puts(BIO *h, const char *str);
67static int buffer_read(BIO *h,char *buf,int size); 67static int buffer_gets(BIO *h, char *str, int size);
68static int buffer_puts(BIO *h,char *str); 68static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static int buffer_gets(BIO *h,char *str,int size);
70static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2);
71static int buffer_new(BIO *h); 69static int buffer_new(BIO *h);
72static int buffer_free(BIO *data); 70static int buffer_free(BIO *data);
73#else 71static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
74static int buffer_write(); 72#define DEFAULT_BUFFER_SIZE 4096
75static int buffer_read();
76static int buffer_puts();
77static int buffer_gets();
78static long buffer_ctrl();
79static int buffer_new();
80static int buffer_free();
81#endif
82
83#define DEFAULT_BUFFER_SIZE 1024
84 73
85static BIO_METHOD methods_buffer= 74static BIO_METHOD methods_buffer=
86 { 75 {
@@ -93,24 +82,24 @@ static BIO_METHOD methods_buffer=
93 buffer_ctrl, 82 buffer_ctrl,
94 buffer_new, 83 buffer_new,
95 buffer_free, 84 buffer_free,
85 buffer_callback_ctrl,
96 }; 86 };
97 87
98BIO_METHOD *BIO_f_buffer() 88BIO_METHOD *BIO_f_buffer(void)
99 { 89 {
100 return(&methods_buffer); 90 return(&methods_buffer);
101 } 91 }
102 92
103static int buffer_new(bi) 93static int buffer_new(BIO *bi)
104BIO *bi;
105 { 94 {
106 BIO_F_BUFFER_CTX *ctx; 95 BIO_F_BUFFER_CTX *ctx;
107 96
108 ctx=(BIO_F_BUFFER_CTX *)Malloc(sizeof(BIO_F_BUFFER_CTX)); 97 ctx=(BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
109 if (ctx == NULL) return(0); 98 if (ctx == NULL) return(0);
110 ctx->ibuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); 99 ctx->ibuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
111 if (ctx->ibuf == NULL) { Free(ctx); return(0); } 100 if (ctx->ibuf == NULL) { OPENSSL_free(ctx); return(0); }
112 ctx->obuf=(char *)Malloc(DEFAULT_BUFFER_SIZE); 101 ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
113 if (ctx->obuf == NULL) { Free(ctx->ibuf); Free(ctx); return(0); } 102 if (ctx->obuf == NULL) { OPENSSL_free(ctx->ibuf); OPENSSL_free(ctx); return(0); }
114 ctx->ibuf_size=DEFAULT_BUFFER_SIZE; 103 ctx->ibuf_size=DEFAULT_BUFFER_SIZE;
115 ctx->obuf_size=DEFAULT_BUFFER_SIZE; 104 ctx->obuf_size=DEFAULT_BUFFER_SIZE;
116 ctx->ibuf_len=0; 105 ctx->ibuf_len=0;
@@ -124,26 +113,22 @@ BIO *bi;
124 return(1); 113 return(1);
125 } 114 }
126 115
127static int buffer_free(a) 116static int buffer_free(BIO *a)
128BIO *a;
129 { 117 {
130 BIO_F_BUFFER_CTX *b; 118 BIO_F_BUFFER_CTX *b;
131 119
132 if (a == NULL) return(0); 120 if (a == NULL) return(0);
133 b=(BIO_F_BUFFER_CTX *)a->ptr; 121 b=(BIO_F_BUFFER_CTX *)a->ptr;
134 if (b->ibuf != NULL) Free(b->ibuf); 122 if (b->ibuf != NULL) OPENSSL_free(b->ibuf);
135 if (b->obuf != NULL) Free(b->obuf); 123 if (b->obuf != NULL) OPENSSL_free(b->obuf);
136 Free(a->ptr); 124 OPENSSL_free(a->ptr);
137 a->ptr=NULL; 125 a->ptr=NULL;
138 a->init=0; 126 a->init=0;
139 a->flags=0; 127 a->flags=0;
140 return(1); 128 return(1);
141 } 129 }
142 130
143static int buffer_read(b,out,outl) 131static int buffer_read(BIO *b, char *out, int outl)
144BIO *b;
145char *out;
146int outl;
147 { 132 {
148 int i,num=0; 133 int i,num=0;
149 BIO_F_BUFFER_CTX *ctx; 134 BIO_F_BUFFER_CTX *ctx;
@@ -209,10 +194,7 @@ start:
209 goto start; 194 goto start;
210 } 195 }
211 196
212static int buffer_write(b,in,inl) 197static int buffer_write(BIO *b, const char *in, int inl)
213BIO *b;
214char *in;
215int inl;
216 { 198 {
217 int i,num=0; 199 int i,num=0;
218 BIO_F_BUFFER_CTX *ctx; 200 BIO_F_BUFFER_CTX *ctx;
@@ -285,11 +267,7 @@ start:
285 goto start; 267 goto start;
286 } 268 }
287 269
288static long buffer_ctrl(b,cmd,num,ptr) 270static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
289BIO *b;
290int cmd;
291long num;
292char *ptr;
293 { 271 {
294 BIO *dbio; 272 BIO *dbio;
295 BIO_F_BUFFER_CTX *ctx; 273 BIO_F_BUFFER_CTX *ctx;
@@ -307,6 +285,7 @@ char *ptr;
307 ctx->ibuf_len=0; 285 ctx->ibuf_len=0;
308 ctx->obuf_off=0; 286 ctx->obuf_off=0;
309 ctx->obuf_len=0; 287 ctx->obuf_len=0;
288 if (b->next_bio == NULL) return(0);
310 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 289 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
311 break; 290 break;
312 case BIO_CTRL_INFO: 291 case BIO_CTRL_INFO:
@@ -323,19 +302,25 @@ char *ptr;
323 case BIO_CTRL_WPENDING: 302 case BIO_CTRL_WPENDING:
324 ret=(long)ctx->obuf_len; 303 ret=(long)ctx->obuf_len;
325 if (ret == 0) 304 if (ret == 0)
305 {
306 if (b->next_bio == NULL) return(0);
326 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 307 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
308 }
327 break; 309 break;
328 case BIO_CTRL_PENDING: 310 case BIO_CTRL_PENDING:
329 ret=(long)ctx->ibuf_len; 311 ret=(long)ctx->ibuf_len;
330 if (ret == 0) 312 if (ret == 0)
313 {
314 if (b->next_bio == NULL) return(0);
331 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 315 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
316 }
332 break; 317 break;
333 case BIO_C_SET_BUFF_READ_DATA: 318 case BIO_C_SET_BUFF_READ_DATA:
334 if (num > ctx->ibuf_size) 319 if (num > ctx->ibuf_size)
335 { 320 {
336 p1=Malloc((int)num); 321 p1=OPENSSL_malloc((int)num);
337 if (p1 == NULL) goto malloc_error; 322 if (p1 == NULL) goto malloc_error;
338 if (ctx->ibuf != NULL) Free(ctx->ibuf); 323 if (ctx->ibuf != NULL) OPENSSL_free(ctx->ibuf);
339 ctx->ibuf=p1; 324 ctx->ibuf=p1;
340 } 325 }
341 ctx->ibuf_off=0; 326 ctx->ibuf_off=0;
@@ -367,21 +352,21 @@ char *ptr;
367 p2=ctx->obuf; 352 p2=ctx->obuf;
368 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) 353 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
369 { 354 {
370 p1=(char *)Malloc((int)num); 355 p1=(char *)OPENSSL_malloc((int)num);
371 if (p1 == NULL) goto malloc_error; 356 if (p1 == NULL) goto malloc_error;
372 } 357 }
373 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) 358 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
374 { 359 {
375 p2=(char *)Malloc((int)num); 360 p2=(char *)OPENSSL_malloc((int)num);
376 if (p2 == NULL) 361 if (p2 == NULL)
377 { 362 {
378 if (p1 != ctx->ibuf) Free(p1); 363 if (p1 != ctx->ibuf) OPENSSL_free(p1);
379 goto malloc_error; 364 goto malloc_error;
380 } 365 }
381 } 366 }
382 if (ctx->ibuf != p1) 367 if (ctx->ibuf != p1)
383 { 368 {
384 Free(ctx->ibuf); 369 OPENSSL_free(ctx->ibuf);
385 ctx->ibuf=p1; 370 ctx->ibuf=p1;
386 ctx->ibuf_off=0; 371 ctx->ibuf_off=0;
387 ctx->ibuf_len=0; 372 ctx->ibuf_len=0;
@@ -389,7 +374,7 @@ char *ptr;
389 } 374 }
390 if (ctx->obuf != p2) 375 if (ctx->obuf != p2)
391 { 376 {
392 Free(ctx->obuf); 377 OPENSSL_free(ctx->obuf);
393 ctx->obuf=p2; 378 ctx->obuf=p2;
394 ctx->obuf_off=0; 379 ctx->obuf_off=0;
395 ctx->obuf_len=0; 380 ctx->obuf_len=0;
@@ -397,12 +382,14 @@ char *ptr;
397 } 382 }
398 break; 383 break;
399 case BIO_C_DO_STATE_MACHINE: 384 case BIO_C_DO_STATE_MACHINE:
385 if (b->next_bio == NULL) return(0);
400 BIO_clear_retry_flags(b); 386 BIO_clear_retry_flags(b);
401 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 387 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
402 BIO_copy_next_retry(b); 388 BIO_copy_next_retry(b);
403 break; 389 break;
404 390
405 case BIO_CTRL_FLUSH: 391 case BIO_CTRL_FLUSH:
392 if (b->next_bio == NULL) return(0);
406 if (ctx->obuf_len <= 0) 393 if (ctx->obuf_len <= 0)
407 { 394 {
408 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 395 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
@@ -432,6 +419,7 @@ fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_
432 break; 419 break;
433 } 420 }
434 } 421 }
422 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
435 break; 423 break;
436 case BIO_CTRL_DUP: 424 case BIO_CTRL_DUP:
437 dbio=(BIO *)ptr; 425 dbio=(BIO *)ptr;
@@ -440,6 +428,7 @@ fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_
440 ret=0; 428 ret=0;
441 break; 429 break;
442 default: 430 default:
431 if (b->next_bio == NULL) return(0);
443 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 432 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
444 break; 433 break;
445 } 434 }
@@ -449,10 +438,21 @@ malloc_error:
449 return(0); 438 return(0);
450 } 439 }
451 440
452static int buffer_gets(b,buf,size) 441static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
453BIO *b; 442 {
454char *buf; 443 long ret=1;
455int size; 444
445 if (b->next_bio == NULL) return(0);
446 switch (cmd)
447 {
448 default:
449 ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
450 break;
451 }
452 return(ret);
453 }
454
455static int buffer_gets(BIO *b, char *buf, int size)
456 { 456 {
457 BIO_F_BUFFER_CTX *ctx; 457 BIO_F_BUFFER_CTX *ctx;
458 int num=0,i,flag; 458 int num=0,i,flag;
@@ -503,10 +503,8 @@ int size;
503 } 503 }
504 } 504 }
505 505
506static int buffer_puts(b,str) 506static int buffer_puts(BIO *b, const char *str)
507BIO *b;
508char *str;
509 { 507 {
510 return(BIO_write(b,str,strlen(str))); 508 return(buffer_write(b,str,strlen(str)));
511 } 509 }
512 510
diff --git a/src/lib/libcrypto/bio/bf_lbuf.c b/src/lib/libcrypto/bio/bf_lbuf.c
index 7bcf8ed941..ec0f7eb0b7 100644
--- a/src/lib/libcrypto/bio/bf_lbuf.c
+++ b/src/lib/libcrypto/bio/bf_lbuf.c
@@ -200,7 +200,7 @@ static int linebuffer_write(BIO *b, const char *in, int inl)
200 } 200 }
201 } 201 }
202 202
203#ifdef DEBUG 203#if 0
204BIO_write(b->next_bio, "<*<", 3); 204BIO_write(b->next_bio, "<*<", 3);
205#endif 205#endif
206 i=BIO_write(b->next_bio, 206 i=BIO_write(b->next_bio,
@@ -210,13 +210,13 @@ BIO_write(b->next_bio, "<*<", 3);
210 ctx->obuf_len = orig_olen; 210 ctx->obuf_len = orig_olen;
211 BIO_copy_next_retry(b); 211 BIO_copy_next_retry(b);
212 212
213#ifdef DEBUG 213#if 0
214BIO_write(b->next_bio, ">*>", 3); 214BIO_write(b->next_bio, ">*>", 3);
215#endif 215#endif
216 if (i < 0) return((num > 0)?num:i); 216 if (i < 0) return((num > 0)?num:i);
217 if (i == 0) return(num); 217 if (i == 0) return(num);
218 } 218 }
219#ifdef DEBUG 219#if 0
220BIO_write(b->next_bio, ">*>", 3); 220BIO_write(b->next_bio, ">*>", 3);
221#endif 221#endif
222 if (i < ctx->obuf_len) 222 if (i < ctx->obuf_len)
@@ -229,20 +229,20 @@ BIO_write(b->next_bio, ">*>", 3);
229 buffer if a NL was found and there is anything to write. */ 229 buffer if a NL was found and there is anything to write. */
230 if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) 230 if ((foundnl || p - in > ctx->obuf_size) && p - in > 0)
231 { 231 {
232#ifdef DEBUG 232#if 0
233BIO_write(b->next_bio, "<*<", 3); 233BIO_write(b->next_bio, "<*<", 3);
234#endif 234#endif
235 i=BIO_write(b->next_bio,in,p - in); 235 i=BIO_write(b->next_bio,in,p - in);
236 if (i <= 0) 236 if (i <= 0)
237 { 237 {
238 BIO_copy_next_retry(b); 238 BIO_copy_next_retry(b);
239#ifdef DEBUG 239#if 0
240BIO_write(b->next_bio, ">*>", 3); 240BIO_write(b->next_bio, ">*>", 3);
241#endif 241#endif
242 if (i < 0) return((num > 0)?num:i); 242 if (i < 0) return((num > 0)?num:i);
243 if (i == 0) return(num); 243 if (i == 0) return(num);
244 } 244 }
245#ifdef DEBUG 245#if 0
246BIO_write(b->next_bio, ">*>", 3); 246BIO_write(b->next_bio, ">*>", 3);
247#endif 247#endif
248 num+=i; 248 num+=i;
diff --git a/src/lib/libcrypto/bio/bf_nbio.c b/src/lib/libcrypto/bio/bf_nbio.c
index 034b3024df..1ce2bfacc0 100644
--- a/src/lib/libcrypto/bio/bf_nbio.c
+++ b/src/lib/libcrypto/bio/bf_nbio.c
@@ -59,31 +59,20 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <errno.h> 60#include <errno.h>
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include "rand.h" 62#include <openssl/rand.h>
63#include "bio.h" 63#include <openssl/bio.h>
64#include "evp.h"
65 64
66/* BIO_put and BIO_get both add to the digest, 65/* BIO_put and BIO_get both add to the digest,
67 * BIO_gets returns the digest */ 66 * BIO_gets returns the digest */
68 67
69#ifndef NOPROTO 68static int nbiof_write(BIO *h,const char *buf,int num);
70static int nbiof_write(BIO *h,char *buf,int num);
71static int nbiof_read(BIO *h,char *buf,int size); 69static int nbiof_read(BIO *h,char *buf,int size);
72static int nbiof_puts(BIO *h,char *str); 70static int nbiof_puts(BIO *h,const char *str);
73static int nbiof_gets(BIO *h,char *str,int size); 71static int nbiof_gets(BIO *h,char *str,int size);
74static long nbiof_ctrl(BIO *h,int cmd,long arg1,char *arg2); 72static long nbiof_ctrl(BIO *h,int cmd,long arg1,void *arg2);
75static int nbiof_new(BIO *h); 73static int nbiof_new(BIO *h);
76static int nbiof_free(BIO *data); 74static int nbiof_free(BIO *data);
77#else 75static long nbiof_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
78static int nbiof_write();
79static int nbiof_read();
80static int nbiof_puts();
81static int nbiof_gets();
82static long nbiof_ctrl();
83static int nbiof_new();
84static int nbiof_free();
85#endif
86
87typedef struct nbio_test_st 76typedef struct nbio_test_st
88 { 77 {
89 /* only set if we sent a 'should retry' error */ 78 /* only set if we sent a 'should retry' error */
@@ -102,19 +91,19 @@ static BIO_METHOD methods_nbiof=
102 nbiof_ctrl, 91 nbiof_ctrl,
103 nbiof_new, 92 nbiof_new,
104 nbiof_free, 93 nbiof_free,
94 nbiof_callback_ctrl,
105 }; 95 };
106 96
107BIO_METHOD *BIO_f_nbio_test() 97BIO_METHOD *BIO_f_nbio_test(void)
108 { 98 {
109 return(&methods_nbiof); 99 return(&methods_nbiof);
110 } 100 }
111 101
112static int nbiof_new(bi) 102static int nbiof_new(BIO *bi)
113BIO *bi;
114 { 103 {
115 NBIO_TEST *nt; 104 NBIO_TEST *nt;
116 105
117 nt=(NBIO_TEST *)Malloc(sizeof(NBIO_TEST)); 106 if (!(nt=(NBIO_TEST *)OPENSSL_malloc(sizeof(NBIO_TEST)))) return(0);
118 nt->lrn= -1; 107 nt->lrn= -1;
119 nt->lwn= -1; 108 nt->lwn= -1;
120 bi->ptr=(char *)nt; 109 bi->ptr=(char *)nt;
@@ -123,22 +112,18 @@ BIO *bi;
123 return(1); 112 return(1);
124 } 113 }
125 114
126static int nbiof_free(a) 115static int nbiof_free(BIO *a)
127BIO *a;
128 { 116 {
129 if (a == NULL) return(0); 117 if (a == NULL) return(0);
130 if (a->ptr != NULL) 118 if (a->ptr != NULL)
131 Free(a->ptr); 119 OPENSSL_free(a->ptr);
132 a->ptr=NULL; 120 a->ptr=NULL;
133 a->init=0; 121 a->init=0;
134 a->flags=0; 122 a->flags=0;
135 return(1); 123 return(1);
136 } 124 }
137 125
138static int nbiof_read(b,out,outl) 126static int nbiof_read(BIO *b, char *out, int outl)
139BIO *b;
140char *out;
141int outl;
142 { 127 {
143 NBIO_TEST *nt; 128 NBIO_TEST *nt;
144 int ret=0; 129 int ret=0;
@@ -153,7 +138,7 @@ int outl;
153 138
154 BIO_clear_retry_flags(b); 139 BIO_clear_retry_flags(b);
155#if 0 140#if 0
156 RAND_bytes(&n,1); 141 RAND_pseudo_bytes(&n,1);
157 num=(n&0x07); 142 num=(n&0x07);
158 143
159 if (outl > num) outl=num; 144 if (outl > num) outl=num;
@@ -173,10 +158,7 @@ int outl;
173 return(ret); 158 return(ret);
174 } 159 }
175 160
176static int nbiof_write(b,in,inl) 161static int nbiof_write(BIO *b, const char *in, int inl)
177BIO *b;
178char *in;
179int inl;
180 { 162 {
181 NBIO_TEST *nt; 163 NBIO_TEST *nt;
182 int ret=0; 164 int ret=0;
@@ -197,7 +179,7 @@ int inl;
197 } 179 }
198 else 180 else
199 { 181 {
200 RAND_bytes(&n,1); 182 RAND_pseudo_bytes(&n,1);
201 num=(n&7); 183 num=(n&7);
202 } 184 }
203 185
@@ -221,11 +203,7 @@ int inl;
221 return(ret); 203 return(ret);
222 } 204 }
223 205
224static long nbiof_ctrl(b,cmd,num,ptr) 206static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
225BIO *b;
226int cmd;
227long num;
228char *ptr;
229 { 207 {
230 long ret; 208 long ret;
231 209
@@ -247,19 +225,28 @@ char *ptr;
247 return(ret); 225 return(ret);
248 } 226 }
249 227
250static int nbiof_gets(bp,buf,size) 228static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
251BIO *bp; 229 {
252char *buf; 230 long ret=1;
253int size; 231
232 if (b->next_bio == NULL) return(0);
233 switch (cmd)
234 {
235 default:
236 ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
237 break;
238 }
239 return(ret);
240 }
241
242static int nbiof_gets(BIO *bp, char *buf, int size)
254 { 243 {
255 if (bp->next_bio == NULL) return(0); 244 if (bp->next_bio == NULL) return(0);
256 return(BIO_gets(bp->next_bio,buf,size)); 245 return(BIO_gets(bp->next_bio,buf,size));
257 } 246 }
258 247
259 248
260static int nbiof_puts(bp,str) 249static int nbiof_puts(BIO *bp, const char *str)
261BIO *bp;
262char *str;
263 { 250 {
264 if (bp->next_bio == NULL) return(0); 251 if (bp->next_bio == NULL) return(0);
265 return(BIO_puts(bp->next_bio,str)); 252 return(BIO_puts(bp->next_bio,str));
diff --git a/src/lib/libcrypto/bio/bf_null.c b/src/lib/libcrypto/bio/bf_null.c
index a47a65741a..c1bf39a904 100644
--- a/src/lib/libcrypto/bio/bf_null.c
+++ b/src/lib/libcrypto/bio/bf_null.c
@@ -59,30 +59,19 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <errno.h> 60#include <errno.h>
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include "bio.h" 62#include <openssl/bio.h>
63#include "evp.h"
64 63
65/* BIO_put and BIO_get both add to the digest, 64/* BIO_put and BIO_get both add to the digest,
66 * BIO_gets returns the digest */ 65 * BIO_gets returns the digest */
67 66
68#ifndef NOPROTO 67static int nullf_write(BIO *h, const char *buf, int num);
69static int nullf_write(BIO *h,char *buf,int num); 68static int nullf_read(BIO *h, char *buf, int size);
70static int nullf_read(BIO *h,char *buf,int size); 69static int nullf_puts(BIO *h, const char *str);
71static int nullf_puts(BIO *h,char *str); 70static int nullf_gets(BIO *h, char *str, int size);
72static int nullf_gets(BIO *h,char *str,int size); 71static long nullf_ctrl(BIO *h, int cmd, long arg1, void *arg2);
73static long nullf_ctrl(BIO *h,int cmd,long arg1,char *arg2);
74static int nullf_new(BIO *h); 72static int nullf_new(BIO *h);
75static int nullf_free(BIO *data); 73static int nullf_free(BIO *data);
76#else 74static long nullf_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
77static int nullf_write();
78static int nullf_read();
79static int nullf_puts();
80static int nullf_gets();
81static long nullf_ctrl();
82static int nullf_new();
83static int nullf_free();
84#endif
85
86static BIO_METHOD methods_nullf= 75static BIO_METHOD methods_nullf=
87 { 76 {
88 BIO_TYPE_NULL_FILTER, 77 BIO_TYPE_NULL_FILTER,
@@ -94,15 +83,15 @@ static BIO_METHOD methods_nullf=
94 nullf_ctrl, 83 nullf_ctrl,
95 nullf_new, 84 nullf_new,
96 nullf_free, 85 nullf_free,
86 nullf_callback_ctrl,
97 }; 87 };
98 88
99BIO_METHOD *BIO_f_null() 89BIO_METHOD *BIO_f_null(void)
100 { 90 {
101 return(&methods_nullf); 91 return(&methods_nullf);
102 } 92 }
103 93
104static int nullf_new(bi) 94static int nullf_new(BIO *bi)
105BIO *bi;
106 { 95 {
107 bi->init=1; 96 bi->init=1;
108 bi->ptr=NULL; 97 bi->ptr=NULL;
@@ -110,8 +99,7 @@ BIO *bi;
110 return(1); 99 return(1);
111 } 100 }
112 101
113static int nullf_free(a) 102static int nullf_free(BIO *a)
114BIO *a;
115 { 103 {
116 if (a == NULL) return(0); 104 if (a == NULL) return(0);
117/* a->ptr=NULL; 105/* a->ptr=NULL;
@@ -120,10 +108,7 @@ BIO *a;
120 return(1); 108 return(1);
121 } 109 }
122 110
123static int nullf_read(b,out,outl) 111static int nullf_read(BIO *b, char *out, int outl)
124BIO *b;
125char *out;
126int outl;
127 { 112 {
128 int ret=0; 113 int ret=0;
129 114
@@ -135,10 +120,7 @@ int outl;
135 return(ret); 120 return(ret);
136 } 121 }
137 122
138static int nullf_write(b,in,inl) 123static int nullf_write(BIO *b, const char *in, int inl)
139BIO *b;
140char *in;
141int inl;
142 { 124 {
143 int ret=0; 125 int ret=0;
144 126
@@ -150,11 +132,7 @@ int inl;
150 return(ret); 132 return(ret);
151 } 133 }
152 134
153static long nullf_ctrl(b,cmd,num,ptr) 135static long nullf_ctrl(BIO *b, int cmd, long num, void *ptr)
154BIO *b;
155int cmd;
156long num;
157char *ptr;
158 { 136 {
159 long ret; 137 long ret;
160 138
@@ -175,19 +153,28 @@ char *ptr;
175 return(ret); 153 return(ret);
176 } 154 }
177 155
178static int nullf_gets(bp,buf,size) 156static long nullf_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
179BIO *bp; 157 {
180char *buf; 158 long ret=1;
181int size; 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)
182 { 171 {
183 if (bp->next_bio == NULL) return(0); 172 if (bp->next_bio == NULL) return(0);
184 return(BIO_gets(bp->next_bio,buf,size)); 173 return(BIO_gets(bp->next_bio,buf,size));
185 } 174 }
186 175
187 176
188static int nullf_puts(bp,str) 177static int nullf_puts(BIO *bp, const char *str)
189BIO *bp;
190char *str;
191 { 178 {
192 if (bp->next_bio == NULL) return(0); 179 if (bp->next_bio == NULL) return(0);
193 return(BIO_puts(bp->next_bio,str)); 180 return(BIO_puts(bp->next_bio,str));
diff --git a/src/lib/libcrypto/bio/bio.h b/src/lib/libcrypto/bio/bio.h
index 300b330e00..b122c7069d 100644
--- a/src/lib/libcrypto/bio/bio.h
+++ b/src/lib/libcrypto/bio/bio.h
@@ -59,12 +59,18 @@
59#ifndef HEADER_BIO_H 59#ifndef HEADER_BIO_H
60#define HEADER_BIO_H 60#define HEADER_BIO_H
61 61
62#ifndef OPENSSL_NO_FP_API
63# include <stdio.h>
64#endif
65#include <stdarg.h>
66
67#include <openssl/crypto.h>
68#include <openssl/e_os2.h>
69
62#ifdef __cplusplus 70#ifdef __cplusplus
63extern "C" { 71extern "C" {
64#endif 72#endif
65 73
66#include "crypto.h"
67
68/* These are the 'types' of BIOs */ 74/* These are the 'types' of BIOs */
69#define BIO_TYPE_NONE 0 75#define BIO_TYPE_NONE 0
70#define BIO_TYPE_MEM (1|0x0400) 76#define BIO_TYPE_MEM (1|0x0400)
@@ -74,7 +80,7 @@ extern "C" {
74#define BIO_TYPE_SOCKET (5|0x0400|0x0100) 80#define BIO_TYPE_SOCKET (5|0x0400|0x0100)
75#define BIO_TYPE_NULL (6|0x0400) 81#define BIO_TYPE_NULL (6|0x0400)
76#define BIO_TYPE_SSL (7|0x0200) 82#define BIO_TYPE_SSL (7|0x0200)
77#define BIO_TYPE_MD (8|0x0200) /* pasive filter */ 83#define BIO_TYPE_MD (8|0x0200) /* passive filter */
78#define BIO_TYPE_BUFFER (9|0x0200) /* filter */ 84#define BIO_TYPE_BUFFER (9|0x0200) /* filter */
79#define BIO_TYPE_CIPHER (10|0x0200) /* filter */ 85#define BIO_TYPE_CIPHER (10|0x0200) /* filter */
80#define BIO_TYPE_BASE64 (11|0x0200) /* filter */ 86#define BIO_TYPE_BASE64 (11|0x0200) /* filter */
@@ -84,6 +90,9 @@ extern "C" {
84#define BIO_TYPE_PROXY_SERVER (15|0x0200) /* server proxy BIO */ 90#define BIO_TYPE_PROXY_SERVER (15|0x0200) /* server proxy BIO */
85#define BIO_TYPE_NBIO_TEST (16|0x0200) /* server proxy BIO */ 91#define BIO_TYPE_NBIO_TEST (16|0x0200) /* server proxy BIO */
86#define BIO_TYPE_NULL_FILTER (17|0x0200) 92#define BIO_TYPE_NULL_FILTER (17|0x0200)
93#define BIO_TYPE_BER (18|0x0200) /* BER -> bin filter */
94#define BIO_TYPE_BIO (19|0x0400) /* (half a) BIO pair */
95#define BIO_TYPE_LINEBUFFER (20|0x0200) /* filter */
87 96
88#define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */ 97#define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */
89#define BIO_TYPE_FILTER 0x0200 98#define BIO_TYPE_FILTER 0x0200
@@ -143,6 +152,11 @@ extern "C" {
143 152
144#define BIO_FLAGS_BASE64_NO_NL 0x100 153#define BIO_FLAGS_BASE64_NO_NL 0x100
145 154
155/* This is used with memory BIOs: it means we shouldn't free up or change the
156 * data in any way.
157 */
158#define BIO_FLAGS_MEM_RDONLY 0x200
159
146#define BIO_set_flags(b,f) ((b)->flags|=(f)) 160#define BIO_set_flags(b,f) ((b)->flags|=(f))
147#define BIO_get_flags(b) ((b)->flags) 161#define BIO_get_flags(b) ((b)->flags)
148#define BIO_set_retry_special(b) \ 162#define BIO_set_retry_special(b) \
@@ -159,14 +173,14 @@ extern "C" {
159#define BIO_get_retry_flags(b) \ 173#define BIO_get_retry_flags(b) \
160 ((b)->flags&(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) 174 ((b)->flags&(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
161 175
162/* These shouldbe used by the application to tell why we should retry */ 176/* These should be used by the application to tell why we should retry */
163#define BIO_should_read(a) ((a)->flags & BIO_FLAGS_READ) 177#define BIO_should_read(a) ((a)->flags & BIO_FLAGS_READ)
164#define BIO_should_write(a) ((a)->flags & BIO_FLAGS_WRITE) 178#define BIO_should_write(a) ((a)->flags & BIO_FLAGS_WRITE)
165#define BIO_should_io_special(a) ((a)->flags & BIO_FLAGS_IO_SPECIAL) 179#define BIO_should_io_special(a) ((a)->flags & BIO_FLAGS_IO_SPECIAL)
166#define BIO_retry_type(a) ((a)->flags & BIO_FLAGS_RWS) 180#define BIO_retry_type(a) ((a)->flags & BIO_FLAGS_RWS)
167#define BIO_should_retry(a) ((a)->flags & BIO_FLAGS_SHOULD_RETRY) 181#define BIO_should_retry(a) ((a)->flags & BIO_FLAGS_SHOULD_RETRY)
168 182
169/* The next two are used in conjunction with the 183/* The next three are used in conjunction with the
170 * BIO_should_io_special() condition. After this returns true, 184 * BIO_should_io_special() condition. After this returns true,
171 * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO 185 * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO
172 * stack and return the 'reason' for the special and the offending BIO. 186 * stack and return the 'reason' for the special and the offending BIO.
@@ -175,6 +189,8 @@ extern "C" {
175#define BIO_RR_SSL_X509_LOOKUP 0x01 189#define BIO_RR_SSL_X509_LOOKUP 0x01
176/* Returned from the connect BIO when a connect would have blocked */ 190/* Returned from the connect BIO when a connect would have blocked */
177#define BIO_RR_CONNECT 0x02 191#define BIO_RR_CONNECT 0x02
192/* Returned from the accept BIO when an accept would have blocked */
193#define BIO_RR_ACCEPT 0x03
178 194
179/* These are passed by the BIO callback */ 195/* These are passed by the BIO callback */
180#define BIO_CB_FREE 0x01 196#define BIO_CB_FREE 0x01
@@ -198,24 +214,29 @@ extern "C" {
198#define BIO_method_name(b) ((b)->method->name) 214#define BIO_method_name(b) ((b)->method->name)
199#define BIO_method_type(b) ((b)->method->type) 215#define BIO_method_type(b) ((b)->method->type)
200 216
201#ifndef WIN16 217typedef struct bio_st BIO;
218
219typedef void bio_info_cb(struct bio_st *, int, const char *, int, long, long);
220
221#ifndef OPENSSL_SYS_WIN16
202typedef struct bio_method_st 222typedef struct bio_method_st
203 { 223 {
204 int type; 224 int type;
205 char *name; 225 const char *name;
206 int (*bwrite)(); 226 int (*bwrite)(BIO *, const char *, int);
207 int (*bread)(); 227 int (*bread)(BIO *, char *, int);
208 int (*bputs)(); 228 int (*bputs)(BIO *, const char *);
209 int (*bgets)(); 229 int (*bgets)(BIO *, char *, int);
210 long (*ctrl)(); 230 long (*ctrl)(BIO *, int, long, void *);
211 int (*create)(); 231 int (*create)(BIO *);
212 int (*destroy)(); 232 int (*destroy)(BIO *);
233 long (*callback_ctrl)(BIO *, int, bio_info_cb *);
213 } BIO_METHOD; 234 } BIO_METHOD;
214#else 235#else
215typedef struct bio_method_st 236typedef struct bio_method_st
216 { 237 {
217 int type; 238 int type;
218 char *name; 239 const char *name;
219 int (_far *bwrite)(); 240 int (_far *bwrite)();
220 int (_far *bread)(); 241 int (_far *bread)();
221 int (_far *bputs)(); 242 int (_far *bputs)();
@@ -223,18 +244,15 @@ typedef struct bio_method_st
223 long (_far *ctrl)(); 244 long (_far *ctrl)();
224 int (_far *create)(); 245 int (_far *create)();
225 int (_far *destroy)(); 246 int (_far *destroy)();
247 long (_fat *callback_ctrl)();
226 } BIO_METHOD; 248 } BIO_METHOD;
227#endif 249#endif
228 250
229typedef struct bio_st 251struct bio_st
230 { 252 {
231 BIO_METHOD *method; 253 BIO_METHOD *method;
232#ifndef NOPROTO
233 /* bio, mode, argp, argi, argl, ret */ 254 /* bio, mode, argp, argi, argl, ret */
234 long (*callback)(struct bio_st *,int,char *,int, long,long); 255 long (*callback)(struct bio_st *,int,const char *,int, long,long);
235#else
236 long (*callback)();
237#endif
238 char *cb_arg; /* first argument for the callback */ 256 char *cb_arg; /* first argument for the callback */
239 257
240 int init; 258 int init;
@@ -242,7 +260,7 @@ typedef struct bio_st
242 int flags; /* extra storage */ 260 int flags; /* extra storage */
243 int retry_reason; 261 int retry_reason;
244 int num; 262 int num;
245 char *ptr; 263 void *ptr;
246 struct bio_st *next_bio; /* used by filter BIOs */ 264 struct bio_st *next_bio; /* used by filter BIOs */
247 struct bio_st *prev_bio; /* used by filter BIOs */ 265 struct bio_st *prev_bio; /* used by filter BIOs */
248 int references; 266 int references;
@@ -250,7 +268,9 @@ typedef struct bio_st
250 unsigned long num_write; 268 unsigned long num_write;
251 269
252 CRYPTO_EX_DATA ex_data; 270 CRYPTO_EX_DATA ex_data;
253 } BIO; 271 };
272
273DECLARE_STACK_OF(BIO)
254 274
255typedef struct bio_f_buffer_ctx_struct 275typedef struct bio_f_buffer_ctx_struct
256 { 276 {
@@ -276,10 +296,7 @@ typedef struct bio_f_buffer_ctx_struct
276#define BIO_CONN_S_OK 6 296#define BIO_CONN_S_OK 6
277#define BIO_CONN_S_BLOCKED_CONNECT 7 297#define BIO_CONN_S_BLOCKED_CONNECT 7
278#define BIO_CONN_S_NBIO 8 298#define BIO_CONN_S_NBIO 8
279#define BIO_CONN_get_param_hostname BIO_ctrl 299/*#define BIO_CONN_get_param_hostname BIO_ctrl */
280
281#define BIO_number_read(b) ((b)->num_read)
282#define BIO_number_written(b) ((b)->num_write)
283 300
284#define BIO_C_SET_CONNECT 100 301#define BIO_C_SET_CONNECT 100
285#define BIO_C_DO_STATE_MACHINE 101 302#define BIO_C_DO_STATE_MACHINE 101
@@ -309,26 +326,42 @@ typedef struct bio_f_buffer_ctx_struct
309#define BIO_C_SET_SSL_RENEGOTIATE_BYTES 125 326#define BIO_C_SET_SSL_RENEGOTIATE_BYTES 125
310#define BIO_C_GET_SSL_NUM_RENEGOTIATES 126 327#define BIO_C_GET_SSL_NUM_RENEGOTIATES 126
311#define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT 127 328#define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT 127
312 329#define BIO_C_FILE_SEEK 128
313#define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,(char *)arg) 330#define BIO_C_GET_CIPHER_CTX 129
331#define BIO_C_SET_BUF_MEM_EOF_RETURN 130/*return end of input value*/
332#define BIO_C_SET_BIND_MODE 131
333#define BIO_C_GET_BIND_MODE 132
334#define BIO_C_FILE_TELL 133
335#define BIO_C_GET_SOCKS 134
336#define BIO_C_SET_SOCKS 135
337
338#define BIO_C_SET_WRITE_BUF_SIZE 136/* for BIO_s_bio */
339#define BIO_C_GET_WRITE_BUF_SIZE 137
340#define BIO_C_MAKE_BIO_PAIR 138
341#define BIO_C_DESTROY_BIO_PAIR 139
342#define BIO_C_GET_WRITE_GUARANTEE 140
343#define BIO_C_GET_READ_REQUEST 141
344#define BIO_C_SHUTDOWN_WR 142
345#define BIO_C_NREAD0 143
346#define BIO_C_NREAD 144
347#define BIO_C_NWRITE0 145
348#define BIO_C_NWRITE 146
349#define BIO_C_RESET_READ_REQUEST 147
350
351
352#define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,arg)
314#define BIO_get_app_data(s) BIO_get_ex_data(s,0) 353#define BIO_get_app_data(s) BIO_get_ex_data(s,0)
315 354
316int BIO_get_ex_num(BIO *bio); 355/* BIO_s_connect() and BIO_s_socks4a_connect() */
317int BIO_set_ex_data(BIO *bio,int idx,char *data);
318char *BIO_get_ex_data(BIO *bio,int idx);
319void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)());
320int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
321 int (*dup_func)(), void (*free_func)());
322
323/* BIO_s_connect_socket() */
324#define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name) 356#define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name)
325#define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port) 357#define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port)
326#define BIO_set_conn_ip(b,ip) BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip) 358#define BIO_set_conn_ip(b,ip) BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip)
327#define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port) 359#define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port)
328#define BIO_get_conn_hostname(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0) 360#define BIO_get_conn_hostname(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0)
329#define BIO_get_conn_port(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1) 361#define BIO_get_conn_port(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1)
330#define BIO_get_conn_ip(b,ip) BIO_ptr_ctrl(b,BIO_C_SET_CONNECT,2) 362#define BIO_get_conn_ip(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,2)
331#define BIO_get_conn_int port(b,port) BIO_int_ctrl(b,BIO_C_SET_CONNECT,3,port) 363#define BIO_get_conn_int_port(b) BIO_int_ctrl(b,BIO_C_GET_CONNECT,3)
364
332 365
333#define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) 366#define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL)
334 367
@@ -339,6 +372,12 @@ int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
339#define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?"a":NULL) 372#define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?"a":NULL)
340#define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio) 373#define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio)
341 374
375#define BIO_BIND_NORMAL 0
376#define BIO_BIND_REUSEADDR_IF_UNUSED 1
377#define BIO_BIND_REUSEADDR 2
378#define BIO_set_bind_mode(b,mode) BIO_ctrl(b,BIO_C_SET_BIND_MODE,mode,NULL)
379#define BIO_get_bind_mode(b,mode) BIO_ctrl(b,BIO_C_GET_BIND_MODE,0,NULL)
380
342#define BIO_do_connect(b) BIO_do_handshake(b) 381#define BIO_do_connect(b) BIO_do_handshake(b)
343#define BIO_do_accept(b) BIO_do_handshake(b) 382#define BIO_do_accept(b) BIO_do_handshake(b)
344#define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL) 383#define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
@@ -349,7 +388,7 @@ int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
349/* BIO_set_nbio(b,n) */ 388/* BIO_set_nbio(b,n) */
350#define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s)) 389#define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s))
351/* BIO *BIO_get_filter_bio(BIO *bio); */ 390/* BIO *BIO_get_filter_bio(BIO *bio); */
352#define BIO_set_proxy_cb(b,cb) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(char *)(cb)) 391#define BIO_set_proxy_cb(b,cb) BIO_callback_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(void *(*cb)()))
353#define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk) 392#define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk)
354#define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool) 393#define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool)
355 394
@@ -364,12 +403,26 @@ int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
364#define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp) 403#define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp)
365#define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp) 404#define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp)
366 405
406#define BIO_seek(b,ofs) (int)BIO_ctrl(b,BIO_C_FILE_SEEK,ofs,NULL)
407#define BIO_tell(b) (int)BIO_ctrl(b,BIO_C_FILE_TELL,0,NULL)
408
409/* name is cast to lose const, but might be better to route through a function
410 so we can do it safely */
411#ifdef CONST_STRICT
412/* If you are wondering why this isn't defined, its because CONST_STRICT is
413 * purely a compile-time kludge to allow const to be checked.
414 */
415int BIO_read_filename(BIO *b,const char *name);
416#else
367#define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ 417#define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
368 BIO_CLOSE|BIO_FP_READ,name) 418 BIO_CLOSE|BIO_FP_READ,(char *)name)
419#endif
369#define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ 420#define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
370 BIO_CLOSE|BIO_FP_WRITE,name) 421 BIO_CLOSE|BIO_FP_WRITE,name)
371#define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \ 422#define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
372 BIO_CLOSE|BIO_FP_APPEND,name) 423 BIO_CLOSE|BIO_FP_APPEND,name)
424#define BIO_rw_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
425 BIO_CLOSE|BIO_FP_READ|BIO_FP_WRITE,name)
373 426
374/* WARNING WARNING, this ups the reference count on the read bio of the 427/* WARNING WARNING, this ups the reference count on the read bio of the
375 * SSL structure. This is because the ssl read BIO is now pointed to by 428 * SSL structure. This is because the ssl read BIO is now pointed to by
@@ -381,15 +434,18 @@ int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
381#define BIO_set_ssl_renegotiate_bytes(b,num) \ 434#define BIO_set_ssl_renegotiate_bytes(b,num) \
382 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL); 435 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
383#define BIO_get_num_renegotiates(b) \ 436#define BIO_get_num_renegotiates(b) \
384 BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL); 437 BIO_ctrl(b,BIO_C_GET_SSL_NUM_RENEGOTIATES,0,NULL);
385#define BIO_set_ssl_renegotiate_timeout(b,seconds) \ 438#define BIO_set_ssl_renegotiate_timeout(b,seconds) \
386 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL); 439 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
387 440
388/* defined in evp.h */ 441/* defined in evp.h */
389/* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */ 442/* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */
390 443
444#define BIO_get_mem_data(b,pp) BIO_ctrl(b,BIO_CTRL_INFO,0,(char *)pp)
391#define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm) 445#define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm)
392#define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp) 446#define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp)
447#define BIO_set_mem_eof_return(b,v) \
448 BIO_ctrl(b,BIO_C_SET_BUF_MEM_EOF_RETURN,v,NULL)
393 449
394/* For the BIO_f_buffer() type */ 450/* For the BIO_f_buffer() type */
395#define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL) 451#define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL)
@@ -407,19 +463,42 @@ int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
407#define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL) 463#define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL)
408#define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL) 464#define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL)
409#define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL) 465#define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL)
466/* ...pending macros have inappropriate return type */
467size_t BIO_ctrl_pending(BIO *b);
468size_t BIO_ctrl_wpending(BIO *b);
410#define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL) 469#define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL)
411#define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0,(char *)cbp) 470#define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0, \
412#define BIO_set_info_callback(b,cb) (int)BIO_ctrl(b,BIO_CTRL_SET_CALLBACK,0,(char *)cb) 471 cbp)
472#define BIO_set_info_callback(b,cb) (int)BIO_callback_ctrl(b,BIO_CTRL_SET_CALLBACK,cb)
413 473
414/* For the BIO_f_buffer() type */ 474/* For the BIO_f_buffer() type */
415#define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL) 475#define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL)
416 476
417#ifdef NO_STDIO 477/* For BIO_s_bio() */
418#define NO_FP_API 478#define BIO_set_write_buf_size(b,size) (int)BIO_ctrl(b,BIO_C_SET_WRITE_BUF_SIZE,size,NULL)
419#endif 479#define BIO_get_write_buf_size(b,size) (size_t)BIO_ctrl(b,BIO_C_GET_WRITE_BUF_SIZE,size,NULL)
420 480#define BIO_make_bio_pair(b1,b2) (int)BIO_ctrl(b1,BIO_C_MAKE_BIO_PAIR,0,b2)
421#ifndef NOPROTO 481#define BIO_destroy_bio_pair(b) (int)BIO_ctrl(b,BIO_C_DESTROY_BIO_PAIR,0,NULL)
422# if defined(WIN16) && defined(_WINDLL) 482#define BIO_shutdown_wr(b) (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL)
483/* macros with inappropriate type -- but ...pending macros use int too: */
484#define BIO_get_write_guarantee(b) (int)BIO_ctrl(b,BIO_C_GET_WRITE_GUARANTEE,0,NULL)
485#define BIO_get_read_request(b) (int)BIO_ctrl(b,BIO_C_GET_READ_REQUEST,0,NULL)
486size_t BIO_ctrl_get_write_guarantee(BIO *b);
487size_t BIO_ctrl_get_read_request(BIO *b);
488int BIO_ctrl_reset_read_request(BIO *b);
489
490/* These two aren't currently implemented */
491/* int BIO_get_ex_num(BIO *bio); */
492/* void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)()); */
493int BIO_set_ex_data(BIO *bio,int idx,void *data);
494void *BIO_get_ex_data(BIO *bio,int idx);
495int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
496 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
497unsigned long BIO_number_read(BIO *bio);
498unsigned long BIO_number_written(BIO *bio);
499
500# ifndef OPENSSL_NO_FP_API
501# if defined(OPENSSL_SYS_WIN16) && defined(_WINDLL)
423BIO_METHOD *BIO_s_file_internal(void); 502BIO_METHOD *BIO_s_file_internal(void);
424BIO *BIO_new_file_internal(char *filename, char *mode); 503BIO *BIO_new_file_internal(char *filename, char *mode);
425BIO *BIO_new_fp_internal(FILE *stream, int close_flag); 504BIO *BIO_new_fp_internal(FILE *stream, int close_flag);
@@ -428,261 +507,184 @@ BIO *BIO_new_fp_internal(FILE *stream, int close_flag);
428# define BIO_new_fp BIO_new_fp_internal 507# define BIO_new_fp BIO_new_fp_internal
429# else /* FP_API */ 508# else /* FP_API */
430BIO_METHOD *BIO_s_file(void ); 509BIO_METHOD *BIO_s_file(void );
431BIO *BIO_new_file(char *filename, char *mode); 510BIO *BIO_new_file(const char *filename, const char *mode);
432BIO *BIO_new_fp(FILE *stream, int close_flag); 511BIO *BIO_new_fp(FILE *stream, int close_flag);
433# define BIO_s_file_internal BIO_s_file 512# define BIO_s_file_internal BIO_s_file
434# define BIO_new_file_internal BIO_new_file 513# define BIO_new_file_internal BIO_new_file
435# define BIO_new_fp_internal BIO_s_file 514# define BIO_new_fp_internal BIO_s_file
436# endif /* FP_API */ 515# endif /* FP_API */
437#else 516# endif
438# if defined(WIN16) && defined(_WINDLL)
439BIO_METHOD *BIO_s_file_internal();
440BIO *BIO_new_file_internal();
441BIO *BIO_new_fp_internal();
442# define BIO_s_file BIO_s_file_internal
443# define BIO_new_file BIO_new_file_internal
444# define BIO_new_fp BIO_new_fp_internal
445# else /* FP_API */
446BIO_METHOD *BIO_s_file();
447BIO *BIO_new_file();
448BIO *BIO_new_fp();
449# define BIO_s_file_internal BIO_s_file
450# define BIO_new_file_internal BIO_new_file
451# define BIO_new_fp_internal BIO_s_file
452# endif /* FP_API */
453#endif
454
455#ifndef NOPROTO
456BIO * BIO_new(BIO_METHOD *type); 517BIO * BIO_new(BIO_METHOD *type);
457int BIO_set(BIO *a,BIO_METHOD *type); 518int BIO_set(BIO *a,BIO_METHOD *type);
458int BIO_free(BIO *a); 519int BIO_free(BIO *a);
459int BIO_read(BIO *b, char *data, int len); 520void BIO_vfree(BIO *a);
521int BIO_read(BIO *b, void *data, int len);
460int BIO_gets(BIO *bp,char *buf, int size); 522int BIO_gets(BIO *bp,char *buf, int size);
461int BIO_write(BIO *b, char *data, int len); 523int BIO_write(BIO *b, const void *data, int len);
462int BIO_puts(BIO *bp,char *buf); 524int BIO_puts(BIO *bp,const char *buf);
463long BIO_ctrl(BIO *bp,int cmd,long larg,char *parg); 525long BIO_ctrl(BIO *bp,int cmd,long larg,void *parg);
526long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long));
464char * BIO_ptr_ctrl(BIO *bp,int cmd,long larg); 527char * BIO_ptr_ctrl(BIO *bp,int cmd,long larg);
465long BIO_int_ctrl(BIO *bp,int cmd,long larg,int iarg); 528long BIO_int_ctrl(BIO *bp,int cmd,long larg,int iarg);
466BIO * BIO_push(BIO *b,BIO *append); 529BIO * BIO_push(BIO *b,BIO *append);
467BIO * BIO_pop(BIO *b); 530BIO * BIO_pop(BIO *b);
468void BIO_free_all(BIO *a); 531void BIO_free_all(BIO *a);
469BIO * BIO_find_type(BIO *b,int bio_type); 532BIO * BIO_find_type(BIO *b,int bio_type);
533BIO * BIO_next(BIO *b);
470BIO * BIO_get_retry_BIO(BIO *bio, int *reason); 534BIO * BIO_get_retry_BIO(BIO *bio, int *reason);
471int BIO_get_retry_reason(BIO *bio); 535int BIO_get_retry_reason(BIO *bio);
472BIO * BIO_dup_chain(BIO *in); 536BIO * BIO_dup_chain(BIO *in);
473 537
474#ifndef WIN16 538int BIO_nread0(BIO *bio, char **buf);
475long BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi, 539int BIO_nread(BIO *bio, char **buf, int num);
540int BIO_nwrite0(BIO *bio, char **buf);
541int BIO_nwrite(BIO *bio, char **buf, int num);
542
543#ifndef OPENSSL_SYS_WIN16
544long BIO_debug_callback(BIO *bio,int cmd,const char *argp,int argi,
476 long argl,long ret); 545 long argl,long ret);
477#else 546#else
478long _far _loadds BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi, 547long _far _loadds BIO_debug_callback(BIO *bio,int cmd,const char *argp,int argi,
479 long argl,long ret); 548 long argl,long ret);
480#endif 549#endif
481 550
482BIO_METHOD *BIO_s_mem(void); 551BIO_METHOD *BIO_s_mem(void);
552BIO *BIO_new_mem_buf(void *buf, int len);
483BIO_METHOD *BIO_s_socket(void); 553BIO_METHOD *BIO_s_socket(void);
484BIO_METHOD *BIO_s_connect(void); 554BIO_METHOD *BIO_s_connect(void);
485BIO_METHOD *BIO_s_accept(void); 555BIO_METHOD *BIO_s_accept(void);
486BIO_METHOD *BIO_s_fd(void); 556BIO_METHOD *BIO_s_fd(void);
557BIO_METHOD *BIO_s_log(void);
558BIO_METHOD *BIO_s_bio(void);
487BIO_METHOD *BIO_s_null(void); 559BIO_METHOD *BIO_s_null(void);
488BIO_METHOD *BIO_f_null(void); 560BIO_METHOD *BIO_f_null(void);
489BIO_METHOD *BIO_f_nbio_test(void);
490BIO_METHOD *BIO_f_buffer(void); 561BIO_METHOD *BIO_f_buffer(void);
562#ifdef OPENSSL_SYS_VMS
563BIO_METHOD *BIO_f_linebuffer(void);
564#endif
565BIO_METHOD *BIO_f_nbio_test(void);
566/* BIO_METHOD *BIO_f_ber(void); */
491 567
492int BIO_sock_should_retry(int i); 568int BIO_sock_should_retry(int i);
493int BIO_sock_non_fatal_error(int error); 569int BIO_sock_non_fatal_error(int error);
494int BIO_fd_should_retry(int i); 570int BIO_fd_should_retry(int i);
495int BIO_fd_non_fatal_error(int error); 571int BIO_fd_non_fatal_error(int error);
496int BIO_dump(BIO *b,char *bytes,int len); 572int BIO_dump(BIO *b,const char *bytes,int len);
497 573int BIO_dump_indent(BIO *b,const char *bytes,int len,int indent);
498struct hostent *BIO_gethostbyname(char *name); 574
575struct hostent *BIO_gethostbyname(const char *name);
576/* We might want a thread-safe interface too:
577 * struct hostent *BIO_gethostbyname_r(const char *name,
578 * struct hostent *result, void *buffer, size_t buflen);
579 * or something similar (caller allocates a struct hostent,
580 * pointed to by "result", and additional buffer space for the various
581 * substructures; if the buffer does not suffice, NULL is returned
582 * and an appropriate error code is set).
583 */
499int BIO_sock_error(int sock); 584int BIO_sock_error(int sock);
500int BIO_socket_ioctl(int fd, long type, unsigned long *arg); 585int BIO_socket_ioctl(int fd, long type, unsigned long *arg);
501int BIO_get_port(char *str, short *port_ptr); 586int BIO_socket_nbio(int fd,int mode);
502int BIO_get_host_ip(char *str, unsigned char *ip); 587int BIO_get_port(const char *str, unsigned short *port_ptr);
503int BIO_get_accept_socket(char *host_port); 588int BIO_get_host_ip(const char *str, unsigned char *ip);
589int BIO_get_accept_socket(char *host_port,int mode);
504int BIO_accept(int sock,char **ip_port); 590int BIO_accept(int sock,char **ip_port);
505int BIO_sock_init(void ); 591int BIO_sock_init(void );
506void BIO_sock_cleanup(void); 592void BIO_sock_cleanup(void);
507int BIO_set_tcp_ndelay(int sock,int turn_on); 593int BIO_set_tcp_ndelay(int sock,int turn_on);
508 594
509void ERR_load_BIO_strings(void );
510
511BIO *BIO_new_socket(int sock, int close_flag); 595BIO *BIO_new_socket(int sock, int close_flag);
512BIO *BIO_new_fd(int fd, int close_flag); 596BIO *BIO_new_fd(int fd, int close_flag);
513BIO *BIO_new_connect(char *host_port); 597BIO *BIO_new_connect(char *host_port);
514BIO *BIO_new_accept(char *host_port); 598BIO *BIO_new_accept(char *host_port);
515 599
600int BIO_new_bio_pair(BIO **bio1, size_t writebuf1,
601 BIO **bio2, size_t writebuf2);
602/* If successful, returns 1 and in *bio1, *bio2 two BIO pair endpoints.
603 * Otherwise returns 0 and sets *bio1 and *bio2 to NULL.
604 * Size 0 uses default value.
605 */
606
516void BIO_copy_next_retry(BIO *b); 607void BIO_copy_next_retry(BIO *b);
517 608
518long BIO_ghbn_ctrl(int cmd,int iarg,char *parg); 609long BIO_ghbn_ctrl(int cmd,int iarg,char *parg);
519 610
520#else 611int BIO_printf(BIO *bio, const char *format, ...);
521 612int BIO_vprintf(BIO *bio, const char *format, va_list args);
522BIO * BIO_new(); 613int BIO_snprintf(char *buf, size_t n, const char *format, ...);
523int BIO_set(); 614int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args);
524int BIO_free();
525int BIO_read();
526int BIO_gets();
527int BIO_write();
528int BIO_puts();
529char * BIO_ptr_ctrl();
530long BIO_ctrl();
531long BIO_int_ctrl();
532BIO * BIO_push();
533BIO * BIO_pop();
534void BIO_free_all();
535BIO * BIO_find_type();
536BIO * BIO_get_retry_BIO();
537int BIO_get_retry_reason();
538BIO * BIO_dup_chain();
539
540#ifndef WIN16
541long BIO_debug_callback();
542#else
543long _far _loadds BIO_debug_callback();
544#endif
545
546BIO_METHOD *BIO_s_mem();
547BIO_METHOD *BIO_s_socket();
548BIO_METHOD *BIO_s_connect();
549BIO_METHOD *BIO_s_accept();
550BIO_METHOD *BIO_s_fd();
551BIO_METHOD *BIO_s_null();
552BIO_METHOD *BIO_f_null();
553BIO_METHOD *BIO_f_buffer();
554BIO_METHOD *BIO_f_nbio_test();
555
556int BIO_sock_should_retry();
557int BIO_sock_non_fatal_error();
558int BIO_fd_should_retry();
559int BIO_fd_non_fatal_error();
560int BIO_dump();
561
562struct hostent *BIO_gethostbyname();
563int BIO_sock_error();
564int BIO_socket_ioctl();
565int BIO_get_port();
566int BIO_get_host_ip();
567int BIO_get_accept_socket();
568int BIO_accept();
569int BIO_sock_init();
570void BIO_sock_cleanup();
571int BIO_set_tcp_ndelay();
572
573void ERR_load_BIO_strings();
574
575BIO *BIO_new_socket();
576BIO *BIO_new_fd();
577BIO *BIO_new_connect();
578BIO *BIO_new_accept();
579
580void BIO_copy_next_retry();
581
582int BIO_ghbn_ctrl();
583
584#endif
585
586/* Tim Hudson's portable varargs stuff */
587
588#ifndef NOPROTO
589#define VAR_ANSI /* select ANSI version by default */
590#endif
591
592#ifdef VAR_ANSI
593/* ANSI version of a "portable" macro set for variable length args */
594#ifndef __STDARG_H__ /**/
595#include <stdarg.h>
596#endif /**/
597
598#define VAR_PLIST(arg1type,arg1) arg1type arg1, ...
599#define VAR_PLIST2(arg1type,arg1,arg2type,arg2) arg1type arg1,arg2type arg2,...
600#define VAR_ALIST
601#define VAR_BDEFN(args,arg1type,arg1) va_list args
602#define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args
603#define VAR_INIT(args,arg1type,arg1) va_start(args,arg1);
604#define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args,arg2);
605#define VAR_ARG(args,type,arg) arg=va_arg(args,type)
606#define VAR_END(args) va_end(args);
607
608#else
609
610/* K&R version of a "portable" macro set for variable length args */
611#ifndef __VARARGS_H__
612#include <varargs.h>
613#endif
614
615#define VAR_PLIST(arg1type,arg1) va_alist
616#define VAR_PLIST2(arg1type,arg1,arg2type,arg2) va_alist
617#define VAR_ALIST va_dcl
618#define VAR_BDEFN(args,arg1type,arg1) va_list args; arg1type arg1
619#define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args; \
620 arg1type arg1; arg2type arg2
621#define VAR_INIT(args,arg1type,arg1) va_start(args); \
622 arg1=va_arg(args,arg1type);
623#define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args); \
624 arg1=va_arg(args,arg1type); arg2=va_arg(args,arg2type);
625#define VAR_ARG(args,type,arg) arg=va_arg(args,type)
626#define VAR_END(args) va_end(args);
627
628#endif
629
630#ifndef NOPROTO
631int BIO_printf( VAR_PLIST( BIO *, bio ) );
632#else
633int BIO_printf();
634#endif
635 615
636/* BEGIN ERROR CODES */ 616/* BEGIN ERROR CODES */
617/* The following lines are auto generated by the script mkerr.pl. Any changes
618 * made after this point may be overwritten when the script is next run.
619 */
620void ERR_load_BIO_strings(void);
621
637/* Error codes for the BIO functions. */ 622/* Error codes for the BIO functions. */
638 623
639/* Function codes. */ 624/* Function codes. */
640#define BIO_F_ACPT_STATE 100 625#define BIO_F_ACPT_STATE 100
641#define BIO_F_BIO_ACCEPT 101 626#define BIO_F_BIO_ACCEPT 101
642#define BIO_F_BIO_CTRL 102 627#define BIO_F_BIO_BER_GET_HEADER 102
643#define BIO_F_BIO_GETS 103 628#define BIO_F_BIO_CTRL 103
644#define BIO_F_BIO_GET_ACCEPT_SOCKET 104 629#define BIO_F_BIO_GETHOSTBYNAME 120
645#define BIO_F_BIO_GET_HOST_IP 105 630#define BIO_F_BIO_GETS 104
646#define BIO_F_BIO_GET_PORT 106 631#define BIO_F_BIO_GET_ACCEPT_SOCKET 105
647#define BIO_F_BIO_NEW 107 632#define BIO_F_BIO_GET_HOST_IP 106
648#define BIO_F_BIO_NEW_FILE 108 633#define BIO_F_BIO_GET_PORT 107
649#define BIO_F_BIO_PUTS 109 634#define BIO_F_BIO_MAKE_PAIR 121
650#define BIO_F_BIO_READ 110 635#define BIO_F_BIO_NEW 108
651#define BIO_F_BIO_SOCK_INIT 111 636#define BIO_F_BIO_NEW_FILE 109
652#define BIO_F_BIO_WRITE 112 637#define BIO_F_BIO_NEW_MEM_BUF 126
653#define BIO_F_BUFFER_CTRL 113 638#define BIO_F_BIO_NREAD 123
654#define BIO_F_CONN_STATE 114 639#define BIO_F_BIO_NREAD0 124
655#define BIO_F_FILE_CTRL 115 640#define BIO_F_BIO_NWRITE 125
656#define BIO_F_MEM_WRITE 116 641#define BIO_F_BIO_NWRITE0 122
657#define BIO_F_SSL_NEW 117 642#define BIO_F_BIO_PUTS 110
658#define BIO_F_WSASTARTUP 118 643#define BIO_F_BIO_READ 111
644#define BIO_F_BIO_SOCK_INIT 112
645#define BIO_F_BIO_WRITE 113
646#define BIO_F_BUFFER_CTRL 114
647#define BIO_F_CONN_CTRL 127
648#define BIO_F_CONN_STATE 115
649#define BIO_F_FILE_CTRL 116
650#define BIO_F_LINEBUFFER_CTRL 129
651#define BIO_F_MEM_READ 128
652#define BIO_F_MEM_WRITE 117
653#define BIO_F_SSL_NEW 118
654#define BIO_F_WSASTARTUP 119
659 655
660/* Reason codes. */ 656/* Reason codes. */
661#define BIO_R_ACCEPT_ERROR 100 657#define BIO_R_ACCEPT_ERROR 100
662#define BIO_R_BAD_FOPEN_MODE 101 658#define BIO_R_BAD_FOPEN_MODE 101
663#define BIO_R_BAD_HOSTNAME_LOOKUP 102 659#define BIO_R_BAD_HOSTNAME_LOOKUP 102
660#define BIO_R_BROKEN_PIPE 124
664#define BIO_R_CONNECT_ERROR 103 661#define BIO_R_CONNECT_ERROR 103
662#define BIO_R_EOF_ON_MEMORY_BIO 127
665#define BIO_R_ERROR_SETTING_NBIO 104 663#define BIO_R_ERROR_SETTING_NBIO 104
666#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105 664#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105
667#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106 665#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106
668#define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107 666#define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107
667#define BIO_R_INVALID_ARGUMENT 125
669#define BIO_R_INVALID_IP_ADDRESS 108 668#define BIO_R_INVALID_IP_ADDRESS 108
669#define BIO_R_IN_USE 123
670#define BIO_R_KEEPALIVE 109 670#define BIO_R_KEEPALIVE 109
671#define BIO_R_NBIO_CONNECT_ERROR 110 671#define BIO_R_NBIO_CONNECT_ERROR 110
672#define BIO_R_NO_ACCEPT_PORT_SPECIFIED 111 672#define BIO_R_NO_ACCEPT_PORT_SPECIFIED 111
673#define BIO_R_NO_HOSTHNAME_SPECIFIED 112 673#define BIO_R_NO_HOSTNAME_SPECIFIED 112
674#define BIO_R_NO_PORT_DEFINED 113 674#define BIO_R_NO_PORT_DEFINED 113
675#define BIO_R_NO_PORT_SPECIFIED 114 675#define BIO_R_NO_PORT_SPECIFIED 114
676#define BIO_R_NO_SUCH_FILE 128
676#define BIO_R_NULL_PARAMETER 115 677#define BIO_R_NULL_PARAMETER 115
677#define BIO_R_UNABLE_TO_BIND_SOCKET 116 678#define BIO_R_TAG_MISMATCH 116
678#define BIO_R_UNABLE_TO_CREATE_SOCKET 117 679#define BIO_R_UNABLE_TO_BIND_SOCKET 117
679#define BIO_R_UNABLE_TO_LISTEN_SOCKET 118 680#define BIO_R_UNABLE_TO_CREATE_SOCKET 118
680#define BIO_R_UNINITALISED 119 681#define BIO_R_UNABLE_TO_LISTEN_SOCKET 119
681#define BIO_R_UNSUPPORTED_METHOD 120 682#define BIO_R_UNINITIALIZED 120
682#define BIO_R_WSASTARTUP 121 683#define BIO_R_UNSUPPORTED_METHOD 121
683 684#define BIO_R_WRITE_TO_READ_ONLY_BIO 126
685#define BIO_R_WSASTARTUP 122
686
684#ifdef __cplusplus 687#ifdef __cplusplus
685} 688}
686#endif 689#endif
687#endif 690#endif
688
diff --git a/src/lib/libcrypto/bio/bio_cb.c b/src/lib/libcrypto/bio/bio_cb.c
index bc6ed9eda1..0ffa4d2136 100644
--- a/src/lib/libcrypto/bio/bio_cb.c
+++ b/src/lib/libcrypto/bio/bio_cb.c
@@ -60,16 +60,11 @@
60#include <string.h> 60#include <string.h>
61#include <stdlib.h> 61#include <stdlib.h>
62#include "cryptlib.h" 62#include "cryptlib.h"
63#include "bio.h" 63#include <openssl/bio.h>
64#include "err.h" 64#include <openssl/err.h>
65 65
66long MS_CALLBACK BIO_debug_callback(bio,cmd,argp,argi,argl,ret) 66long MS_CALLBACK BIO_debug_callback(BIO *bio, int cmd, const char *argp,
67BIO *bio; 67 int argi, long argl, long ret)
68int cmd;
69char *argp;
70int argi;
71long argl;
72long ret;
73 { 68 {
74 BIO *b; 69 BIO *b;
75 MS_STATIC char buf[256]; 70 MS_STATIC char buf[256];
@@ -130,7 +125,7 @@ long ret;
130 b=(BIO *)bio->cb_arg; 125 b=(BIO *)bio->cb_arg;
131 if (b != NULL) 126 if (b != NULL)
132 BIO_write(b,buf,strlen(buf)); 127 BIO_write(b,buf,strlen(buf));
133#if !defined(NO_STDIO) && !defined(WIN16) 128#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16)
134 else 129 else
135 fputs(buf,stderr); 130 fputs(buf,stderr);
136#endif 131#endif
diff --git a/src/lib/libcrypto/bio/bio_err.c b/src/lib/libcrypto/bio/bio_err.c
index 37e14ca107..99ca3cd0da 100644
--- a/src/lib/libcrypto/bio/bio_err.c
+++ b/src/lib/libcrypto/bio/bio_err.c
@@ -1,88 +1,102 @@
1/* lib/bio/bio_err.c */ 1/* crypto/bio/bio_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) 2/* ====================================================================
3 * All rights reserved. 3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * 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 5 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
25 * are met: 7 * are met:
26 * 1. Redistributions of source code must retain the copyright 8 *
27 * notice, this list of conditions and the following disclaimer. 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
28 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in
30 * documentation and/or other materials provided with the distribution. 14 * the documentation and/or other materials provided with the
31 * 3. All advertising materials mentioning features or use of this software 15 * distribution.
32 * must display the following acknowledgement: 16 *
33 * "This product includes cryptographic software written by 17 * 3. All advertising materials mentioning features or use of this
34 * Eric Young (eay@cryptsoft.com)" 18 * software must display the following acknowledgment:
35 * The word 'cryptographic' can be left out if the rouines from the library 19 * "This product includes software developed by the OpenSSL Project
36 * being used are not cryptographic related :-). 20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
37 * 4. If you include any Windows specific code (or a derivative thereof) from 21 *
38 * the apps directory (application code) you must include an acknowledgement: 22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 23 * endorse or promote products derived from this software without
40 * 24 * prior written permission. For written permission, please contact
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 25 * openssl-core@OpenSSL.org.
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 *
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * 5. Products derived from this software may not be called "OpenSSL"
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * nor may "OpenSSL" appear in their names without prior written
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * permission of the OpenSSL Project.
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 *
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * 6. Redistributions of any form whatsoever must retain the following
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * acknowledgment:
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * "This product includes software developed by the OpenSSL Project
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
51 * SUCH DAMAGE. 35 *
52 * 36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
53 * The licence and distribution terms for any publically available version or 37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * copied and put under another distribution licence 39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
56 * [including the GNU Public Licence.] 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 *
57 */ 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
58#include <stdio.h> 61#include <stdio.h>
59#include "err.h" 62#include <openssl/err.h>
60#include "bio.h" 63#include <openssl/bio.h>
61 64
62/* BEGIN ERROR CODES */ 65/* BEGIN ERROR CODES */
63#ifndef NO_ERR 66#ifndef OPENSSL_NO_ERR
64static ERR_STRING_DATA BIO_str_functs[]= 67static ERR_STRING_DATA BIO_str_functs[]=
65 { 68 {
66{ERR_PACK(0,BIO_F_ACPT_STATE,0), "ACPT_STATE"}, 69{ERR_PACK(0,BIO_F_ACPT_STATE,0), "ACPT_STATE"},
67{ERR_PACK(0,BIO_F_BIO_ACCEPT,0), "BIO_accept"}, 70{ERR_PACK(0,BIO_F_BIO_ACCEPT,0), "BIO_accept"},
71{ERR_PACK(0,BIO_F_BIO_BER_GET_HEADER,0), "BIO_BER_GET_HEADER"},
68{ERR_PACK(0,BIO_F_BIO_CTRL,0), "BIO_ctrl"}, 72{ERR_PACK(0,BIO_F_BIO_CTRL,0), "BIO_ctrl"},
73{ERR_PACK(0,BIO_F_BIO_GETHOSTBYNAME,0), "BIO_gethostbyname"},
69{ERR_PACK(0,BIO_F_BIO_GETS,0), "BIO_gets"}, 74{ERR_PACK(0,BIO_F_BIO_GETS,0), "BIO_gets"},
70{ERR_PACK(0,BIO_F_BIO_GET_ACCEPT_SOCKET,0), "BIO_get_accept_socket"}, 75{ERR_PACK(0,BIO_F_BIO_GET_ACCEPT_SOCKET,0), "BIO_get_accept_socket"},
71{ERR_PACK(0,BIO_F_BIO_GET_HOST_IP,0), "BIO_get_host_ip"}, 76{ERR_PACK(0,BIO_F_BIO_GET_HOST_IP,0), "BIO_get_host_ip"},
72{ERR_PACK(0,BIO_F_BIO_GET_PORT,0), "BIO_get_port"}, 77{ERR_PACK(0,BIO_F_BIO_GET_PORT,0), "BIO_get_port"},
78{ERR_PACK(0,BIO_F_BIO_MAKE_PAIR,0), "BIO_MAKE_PAIR"},
73{ERR_PACK(0,BIO_F_BIO_NEW,0), "BIO_new"}, 79{ERR_PACK(0,BIO_F_BIO_NEW,0), "BIO_new"},
74{ERR_PACK(0,BIO_F_BIO_NEW_FILE,0), "BIO_new_file"}, 80{ERR_PACK(0,BIO_F_BIO_NEW_FILE,0), "BIO_new_file"},
81{ERR_PACK(0,BIO_F_BIO_NEW_MEM_BUF,0), "BIO_new_mem_buf"},
82{ERR_PACK(0,BIO_F_BIO_NREAD,0), "BIO_nread"},
83{ERR_PACK(0,BIO_F_BIO_NREAD0,0), "BIO_nread0"},
84{ERR_PACK(0,BIO_F_BIO_NWRITE,0), "BIO_nwrite"},
85{ERR_PACK(0,BIO_F_BIO_NWRITE0,0), "BIO_nwrite0"},
75{ERR_PACK(0,BIO_F_BIO_PUTS,0), "BIO_puts"}, 86{ERR_PACK(0,BIO_F_BIO_PUTS,0), "BIO_puts"},
76{ERR_PACK(0,BIO_F_BIO_READ,0), "BIO_read"}, 87{ERR_PACK(0,BIO_F_BIO_READ,0), "BIO_read"},
77{ERR_PACK(0,BIO_F_BIO_SOCK_INIT,0), "BIO_sock_init"}, 88{ERR_PACK(0,BIO_F_BIO_SOCK_INIT,0), "BIO_sock_init"},
78{ERR_PACK(0,BIO_F_BIO_WRITE,0), "BIO_write"}, 89{ERR_PACK(0,BIO_F_BIO_WRITE,0), "BIO_write"},
79{ERR_PACK(0,BIO_F_BUFFER_CTRL,0), "BUFFER_CTRL"}, 90{ERR_PACK(0,BIO_F_BUFFER_CTRL,0), "BUFFER_CTRL"},
91{ERR_PACK(0,BIO_F_CONN_CTRL,0), "CONN_CTRL"},
80{ERR_PACK(0,BIO_F_CONN_STATE,0), "CONN_STATE"}, 92{ERR_PACK(0,BIO_F_CONN_STATE,0), "CONN_STATE"},
81{ERR_PACK(0,BIO_F_FILE_CTRL,0), "FILE_CTRL"}, 93{ERR_PACK(0,BIO_F_FILE_CTRL,0), "FILE_CTRL"},
94{ERR_PACK(0,BIO_F_LINEBUFFER_CTRL,0), "LINEBUFFER_CTRL"},
95{ERR_PACK(0,BIO_F_MEM_READ,0), "MEM_READ"},
82{ERR_PACK(0,BIO_F_MEM_WRITE,0), "MEM_WRITE"}, 96{ERR_PACK(0,BIO_F_MEM_WRITE,0), "MEM_WRITE"},
83{ERR_PACK(0,BIO_F_SSL_NEW,0), "SSL_NEW"}, 97{ERR_PACK(0,BIO_F_SSL_NEW,0), "SSL_new"},
84{ERR_PACK(0,BIO_F_WSASTARTUP,0), "WSASTARTUP"}, 98{ERR_PACK(0,BIO_F_WSASTARTUP,0), "WSASTARTUP"},
85{0,NULL}, 99{0,NULL}
86 }; 100 };
87 101
88static ERR_STRING_DATA BIO_str_reasons[]= 102static ERR_STRING_DATA BIO_str_reasons[]=
@@ -90,38 +104,45 @@ static ERR_STRING_DATA BIO_str_reasons[]=
90{BIO_R_ACCEPT_ERROR ,"accept error"}, 104{BIO_R_ACCEPT_ERROR ,"accept error"},
91{BIO_R_BAD_FOPEN_MODE ,"bad fopen mode"}, 105{BIO_R_BAD_FOPEN_MODE ,"bad fopen mode"},
92{BIO_R_BAD_HOSTNAME_LOOKUP ,"bad hostname lookup"}, 106{BIO_R_BAD_HOSTNAME_LOOKUP ,"bad hostname lookup"},
107{BIO_R_BROKEN_PIPE ,"broken pipe"},
93{BIO_R_CONNECT_ERROR ,"connect error"}, 108{BIO_R_CONNECT_ERROR ,"connect error"},
109{BIO_R_EOF_ON_MEMORY_BIO ,"EOF on memory BIO"},
94{BIO_R_ERROR_SETTING_NBIO ,"error setting nbio"}, 110{BIO_R_ERROR_SETTING_NBIO ,"error setting nbio"},
95{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET,"error setting nbio on accepted socket"}, 111{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET,"error setting nbio on accepted socket"},
96{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET,"error setting nbio on accept socket"}, 112{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET,"error setting nbio on accept socket"},
97{BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET ,"gethostbyname addr is not af inet"}, 113{BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET ,"gethostbyname addr is not af inet"},
114{BIO_R_INVALID_ARGUMENT ,"invalid argument"},
98{BIO_R_INVALID_IP_ADDRESS ,"invalid ip address"}, 115{BIO_R_INVALID_IP_ADDRESS ,"invalid ip address"},
116{BIO_R_IN_USE ,"in use"},
99{BIO_R_KEEPALIVE ,"keepalive"}, 117{BIO_R_KEEPALIVE ,"keepalive"},
100{BIO_R_NBIO_CONNECT_ERROR ,"nbio connect error"}, 118{BIO_R_NBIO_CONNECT_ERROR ,"nbio connect error"},
101{BIO_R_NO_ACCEPT_PORT_SPECIFIED ,"no accept port specified"}, 119{BIO_R_NO_ACCEPT_PORT_SPECIFIED ,"no accept port specified"},
102{BIO_R_NO_HOSTHNAME_SPECIFIED ,"no hosthname specified"}, 120{BIO_R_NO_HOSTNAME_SPECIFIED ,"no hostname specified"},
103{BIO_R_NO_PORT_DEFINED ,"no port defined"}, 121{BIO_R_NO_PORT_DEFINED ,"no port defined"},
104{BIO_R_NO_PORT_SPECIFIED ,"no port specified"}, 122{BIO_R_NO_PORT_SPECIFIED ,"no port specified"},
123{BIO_R_NO_SUCH_FILE ,"no such file"},
105{BIO_R_NULL_PARAMETER ,"null parameter"}, 124{BIO_R_NULL_PARAMETER ,"null parameter"},
125{BIO_R_TAG_MISMATCH ,"tag mismatch"},
106{BIO_R_UNABLE_TO_BIND_SOCKET ,"unable to bind socket"}, 126{BIO_R_UNABLE_TO_BIND_SOCKET ,"unable to bind socket"},
107{BIO_R_UNABLE_TO_CREATE_SOCKET ,"unable to create socket"}, 127{BIO_R_UNABLE_TO_CREATE_SOCKET ,"unable to create socket"},
108{BIO_R_UNABLE_TO_LISTEN_SOCKET ,"unable to listen socket"}, 128{BIO_R_UNABLE_TO_LISTEN_SOCKET ,"unable to listen socket"},
109{BIO_R_UNINITALISED ,"uninitalised"}, 129{BIO_R_UNINITIALIZED ,"uninitialized"},
110{BIO_R_UNSUPPORTED_METHOD ,"unsupported method"}, 130{BIO_R_UNSUPPORTED_METHOD ,"unsupported method"},
111{BIO_R_WSASTARTUP ,"wsastartup"}, 131{BIO_R_WRITE_TO_READ_ONLY_BIO ,"write to read only BIO"},
112{0,NULL}, 132{BIO_R_WSASTARTUP ,"WSAStartup"},
133{0,NULL}
113 }; 134 };
114 135
115#endif 136#endif
116 137
117void ERR_load_BIO_strings() 138void ERR_load_BIO_strings(void)
118 { 139 {
119 static int init=1; 140 static int init=1;
120 141
121 if (init); 142 if (init)
122 {; 143 {
123 init=0; 144 init=0;
124#ifndef NO_ERR 145#ifndef OPENSSL_NO_ERR
125 ERR_load_strings(ERR_LIB_BIO,BIO_str_functs); 146 ERR_load_strings(ERR_LIB_BIO,BIO_str_functs);
126 ERR_load_strings(ERR_LIB_BIO,BIO_str_reasons); 147 ERR_load_strings(ERR_LIB_BIO,BIO_str_reasons);
127#endif 148#endif
diff --git a/src/lib/libcrypto/bio/bio_lib.c b/src/lib/libcrypto/bio/bio_lib.c
index 7a66b0892e..50df2238fa 100644
--- a/src/lib/libcrypto/bio/bio_lib.c
+++ b/src/lib/libcrypto/bio/bio_lib.c
@@ -58,20 +58,16 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include <errno.h> 60#include <errno.h>
61#include "crypto.h" 61#include <openssl/crypto.h>
62#include "cryptlib.h" 62#include "cryptlib.h"
63#include "bio.h" 63#include <openssl/bio.h>
64#include "stack.h" 64#include <openssl/stack.h>
65 65
66static STACK *bio_meth=NULL; 66BIO *BIO_new(BIO_METHOD *method)
67static int bio_meth_num=0;
68
69BIO *BIO_new(method)
70BIO_METHOD *method;
71 { 67 {
72 BIO *ret=NULL; 68 BIO *ret=NULL;
73 69
74 ret=(BIO *)Malloc(sizeof(BIO)); 70 ret=(BIO *)OPENSSL_malloc(sizeof(BIO));
75 if (ret == NULL) 71 if (ret == NULL)
76 { 72 {
77 BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE); 73 BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);
@@ -79,15 +75,13 @@ BIO_METHOD *method;
79 } 75 }
80 if (!BIO_set(ret,method)) 76 if (!BIO_set(ret,method))
81 { 77 {
82 Free(ret); 78 OPENSSL_free(ret);
83 ret=NULL; 79 ret=NULL;
84 } 80 }
85 return(ret); 81 return(ret);
86 } 82 }
87 83
88int BIO_set(bio,method) 84int BIO_set(BIO *bio, BIO_METHOD *method)
89BIO *bio;
90BIO_METHOD *method;
91 { 85 {
92 bio->method=method; 86 bio->method=method;
93 bio->callback=NULL; 87 bio->callback=NULL;
@@ -103,15 +97,18 @@ BIO_METHOD *method;
103 bio->references=1; 97 bio->references=1;
104 bio->num_read=0L; 98 bio->num_read=0L;
105 bio->num_write=0L; 99 bio->num_write=0L;
106 CRYPTO_new_ex_data(bio_meth,(char *)bio,&bio->ex_data); 100 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
107 if (method->create != NULL) 101 if (method->create != NULL)
108 if (!method->create(bio)) 102 if (!method->create(bio))
103 {
104 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
105 &bio->ex_data);
109 return(0); 106 return(0);
107 }
110 return(1); 108 return(1);
111 } 109 }
112 110
113int BIO_free(a) 111int BIO_free(BIO *a)
114BIO *a;
115 { 112 {
116 int ret=0,i; 113 int ret=0,i;
117 114
@@ -121,7 +118,7 @@ BIO *a;
121#ifdef REF_PRINT 118#ifdef REF_PRINT
122 REF_PRINT("BIO",a); 119 REF_PRINT("BIO",a);
123#endif 120#endif
124 if (i > 0) return(1); 121 if (i > 0) return(1);
125#ifdef REF_CHECK 122#ifdef REF_CHECK
126 if (i < 0) 123 if (i < 0)
127 { 124 {
@@ -133,18 +130,18 @@ BIO *a;
133 ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0)) 130 ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0))
134 return(i); 131 return(i);
135 132
136 CRYPTO_free_ex_data(bio_meth,(char *)a,&a->ex_data); 133 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
137 134
138 if ((a->method == NULL) || (a->method->destroy == NULL)) return(1); 135 if ((a->method == NULL) || (a->method->destroy == NULL)) return(1);
139 ret=a->method->destroy(a); 136 ret=a->method->destroy(a);
140 Free(a); 137 OPENSSL_free(a);
141 return(1); 138 return(1);
142 } 139 }
143 140
144int BIO_read(b,out,outl) 141void BIO_vfree(BIO *a)
145BIO *b; 142 { BIO_free(a); }
146char *out; 143
147int outl; 144int BIO_read(BIO *b, void *out, int outl)
148 { 145 {
149 int i; 146 int i;
150 long (*cb)(); 147 long (*cb)();
@@ -162,11 +159,12 @@ int outl;
162 159
163 if (!b->init) 160 if (!b->init)
164 { 161 {
165 BIOerr(BIO_F_BIO_READ,BIO_R_UNINITALISED); 162 BIOerr(BIO_F_BIO_READ,BIO_R_UNINITIALIZED);
166 return(-2); 163 return(-2);
167 } 164 }
168 165
169 i=b->method->bread(b,out,outl); 166 i=b->method->bread(b,out,outl);
167
170 if (i > 0) b->num_read+=(unsigned long)i; 168 if (i > 0) b->num_read+=(unsigned long)i;
171 169
172 if (cb != NULL) 170 if (cb != NULL)
@@ -175,10 +173,7 @@ int outl;
175 return(i); 173 return(i);
176 } 174 }
177 175
178int BIO_write(b,in,inl) 176int BIO_write(BIO *b, const void *in, int inl)
179BIO *b;
180char *in;
181int inl;
182 { 177 {
183 int i; 178 int i;
184 long (*cb)(); 179 long (*cb)();
@@ -199,11 +194,12 @@ int inl;
199 194
200 if (!b->init) 195 if (!b->init)
201 { 196 {
202 BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITALISED); 197 BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED);
203 return(-2); 198 return(-2);
204 } 199 }
205 200
206 i=b->method->bwrite(b,in,inl); 201 i=b->method->bwrite(b,in,inl);
202
207 if (i > 0) b->num_write+=(unsigned long)i; 203 if (i > 0) b->num_write+=(unsigned long)i;
208 204
209 if (cb != NULL) 205 if (cb != NULL)
@@ -212,9 +208,7 @@ int inl;
212 return(i); 208 return(i);
213 } 209 }
214 210
215int BIO_puts(b,in) 211int BIO_puts(BIO *b, const char *in)
216BIO *b;
217char *in;
218 { 212 {
219 int i; 213 int i;
220 long (*cb)(); 214 long (*cb)();
@@ -233,22 +227,21 @@ char *in;
233 227
234 if (!b->init) 228 if (!b->init)
235 { 229 {
236 BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITALISED); 230 BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED);
237 return(-2); 231 return(-2);
238 } 232 }
239 233
240 i=b->method->bputs(b,in); 234 i=b->method->bputs(b,in);
241 235
236 if (i > 0) b->num_write+=(unsigned long)i;
237
242 if (cb != NULL) 238 if (cb != NULL)
243 i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0, 239 i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,
244 0L,(long)i); 240 0L,(long)i);
245 return(i); 241 return(i);
246 } 242 }
247 243
248int BIO_gets(b,in,inl) 244int BIO_gets(BIO *b, char *in, int inl)
249BIO *b;
250char *in;
251int inl;
252 { 245 {
253 int i; 246 int i;
254 long (*cb)(); 247 long (*cb)();
@@ -267,7 +260,7 @@ int inl;
267 260
268 if (!b->init) 261 if (!b->init)
269 { 262 {
270 BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITALISED); 263 BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED);
271 return(-2); 264 return(-2);
272 } 265 }
273 266
@@ -279,11 +272,7 @@ int inl;
279 return(i); 272 return(i);
280 } 273 }
281 274
282long BIO_int_ctrl(b,cmd,larg,iarg) 275long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
283BIO *b;
284int cmd;
285long larg;
286int iarg;
287 { 276 {
288 int i; 277 int i;
289 278
@@ -291,10 +280,7 @@ int iarg;
291 return(BIO_ctrl(b,cmd,larg,(char *)&i)); 280 return(BIO_ctrl(b,cmd,larg,(char *)&i));
292 } 281 }
293 282
294char *BIO_ptr_ctrl(b,cmd,larg) 283char *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
295BIO *b;
296int cmd;
297long larg;
298 { 284 {
299 char *p=NULL; 285 char *p=NULL;
300 286
@@ -304,11 +290,7 @@ long larg;
304 return(p); 290 return(p);
305 } 291 }
306 292
307long BIO_ctrl(b,cmd,larg,parg) 293long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
308BIO *b;
309int cmd;
310long larg;
311char *parg;
312 { 294 {
313 long ret; 295 long ret;
314 long (*cb)(); 296 long (*cb)();
@@ -335,9 +317,49 @@ char *parg;
335 return(ret); 317 return(ret);
336 } 318 }
337 319
320long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long))
321 {
322 long ret;
323 long (*cb)();
324
325 if (b == NULL) return(0);
326
327 if ((b->method == NULL) || (b->method->callback_ctrl == NULL))
328 {
329 BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);
330 return(-2);
331 }
332
333 cb=b->callback;
334
335 if ((cb != NULL) &&
336 ((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0))
337 return(ret);
338
339 ret=b->method->callback_ctrl(b,cmd,fp);
340
341 if (cb != NULL)
342 ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd,
343 0,ret);
344 return(ret);
345 }
346
347/* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
348 * do; but those macros have inappropriate return type, and for interfacing
349 * from other programming languages, C macros aren't much of a help anyway. */
350size_t BIO_ctrl_pending(BIO *bio)
351 {
352 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
353 }
354
355size_t BIO_ctrl_wpending(BIO *bio)
356 {
357 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
358 }
359
360
338/* put the 'bio' on the end of b's list of operators */ 361/* put the 'bio' on the end of b's list of operators */
339BIO *BIO_push(b,bio) 362BIO *BIO_push(BIO *b, BIO *bio)
340BIO *b,*bio;
341 { 363 {
342 BIO *lb; 364 BIO *lb;
343 365
@@ -354,8 +376,7 @@ BIO *b,*bio;
354 } 376 }
355 377
356/* Remove the first and return the rest */ 378/* Remove the first and return the rest */
357BIO *BIO_pop(b) 379BIO *BIO_pop(BIO *b)
358BIO *b;
359 { 380 {
360 BIO *ret; 381 BIO *ret;
361 382
@@ -373,9 +394,7 @@ BIO *b;
373 return(ret); 394 return(ret);
374 } 395 }
375 396
376BIO *BIO_get_retry_BIO(bio,reason) 397BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
377BIO *bio;
378int *reason;
379 { 398 {
380 BIO *b,*last; 399 BIO *b,*last;
381 400
@@ -391,18 +410,16 @@ int *reason;
391 return(last); 410 return(last);
392 } 411 }
393 412
394int BIO_get_retry_reason(bio) 413int BIO_get_retry_reason(BIO *bio)
395BIO *bio;
396 { 414 {
397 return(bio->retry_reason); 415 return(bio->retry_reason);
398 } 416 }
399 417
400BIO *BIO_find_type(bio,type) 418BIO *BIO_find_type(BIO *bio, int type)
401BIO *bio;
402int type;
403 { 419 {
404 int mt,mask; 420 int mt,mask;
405 421
422 if(!bio) return NULL;
406 mask=type&0xff; 423 mask=type&0xff;
407 do { 424 do {
408 if (bio->method != NULL) 425 if (bio->method != NULL)
@@ -421,8 +438,13 @@ int type;
421 return(NULL); 438 return(NULL);
422 } 439 }
423 440
424void BIO_free_all(bio) 441BIO *BIO_next(BIO *b)
425BIO *bio; 442 {
443 if(!b) return NULL;
444 return b->next_bio;
445 }
446
447void BIO_free_all(BIO *bio)
426 { 448 {
427 BIO *b; 449 BIO *b;
428 int ref; 450 int ref;
@@ -438,8 +460,7 @@ BIO *bio;
438 } 460 }
439 } 461 }
440 462
441BIO *BIO_dup_chain(in) 463BIO *BIO_dup_chain(BIO *in)
442BIO *in;
443 { 464 {
444 BIO *ret=NULL,*eoc=NULL,*bio,*new; 465 BIO *ret=NULL,*eoc=NULL,*bio,*new;
445 466
@@ -461,9 +482,10 @@ BIO *in;
461 goto err; 482 goto err;
462 } 483 }
463 484
464 /* copy app data */ 485 /* copy app data */
465 if (!CRYPTO_dup_ex_data(bio_meth,&new->ex_data,&bio->ex_data)) 486 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new->ex_data,
466 goto err; 487 &bio->ex_data))
488 goto err;
467 489
468 if (ret == NULL) 490 if (ret == NULL)
469 { 491 {
@@ -483,37 +505,39 @@ err:
483 return(NULL); 505 return(NULL);
484 } 506 }
485 507
486void BIO_copy_next_retry(b) 508void BIO_copy_next_retry(BIO *b)
487BIO *b;
488 { 509 {
489 BIO_set_flags(b,BIO_get_retry_flags(b->next_bio)); 510 BIO_set_flags(b,BIO_get_retry_flags(b->next_bio));
490 b->retry_reason=b->next_bio->retry_reason; 511 b->retry_reason=b->next_bio->retry_reason;
491 } 512 }
492 513
493int BIO_get_ex_new_index(argl,argp,new_func,dup_func,free_func) 514int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
494long argl; 515 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
495char *argp; 516 {
496int (*new_func)(); 517 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
497int (*dup_func)(); 518 new_func, dup_func, free_func);
498void (*free_func)(); 519 }
499 { 520
500 bio_meth_num++; 521int BIO_set_ex_data(BIO *bio, int idx, void *data)
501 return(CRYPTO_get_ex_new_index(bio_meth_num-1,&bio_meth,
502 argl,argp,new_func,dup_func,free_func));
503 }
504
505int BIO_set_ex_data(bio,idx,data)
506BIO *bio;
507int idx;
508char *data;
509 { 522 {
510 return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data)); 523 return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
511 } 524 }
512 525
513char *BIO_get_ex_data(bio,idx) 526void *BIO_get_ex_data(BIO *bio, int idx)
514BIO *bio;
515int idx;
516 { 527 {
517 return(CRYPTO_get_ex_data(&(bio->ex_data),idx)); 528 return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
518 } 529 }
519 530
531unsigned long BIO_number_read(BIO *bio)
532{
533 if(bio) return bio->num_read;
534 return 0;
535}
536
537unsigned long BIO_number_written(BIO *bio)
538{
539 if(bio) return bio->num_write;
540 return 0;
541}
542
543IMPLEMENT_STACK_OF(BIO)
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c
index e49902fa9f..8ea1db158b 100644
--- a/src/lib/libcrypto/bio/bss_acpt.c
+++ b/src/lib/libcrypto/bio/bss_acpt.c
@@ -56,22 +56,25 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#ifndef NO_SOCK 59#ifndef OPENSSL_NO_SOCK
60 60
61#include <stdio.h> 61#include <stdio.h>
62#include <errno.h> 62#include <errno.h>
63#define USE_SOCKETS 63#define USE_SOCKETS
64#include "cryptlib.h" 64#include "cryptlib.h"
65#include "bio.h" 65#include <openssl/bio.h>
66 66
67/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ 67#ifdef OPENSSL_SYS_WIN16
68
69#ifdef WIN16
70#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ 68#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71#else 69#else
72#define SOCKET_PROTOCOL IPPROTO_TCP 70#define SOCKET_PROTOCOL IPPROTO_TCP
73#endif 71#endif
74 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
75typedef struct bio_accept_st 78typedef struct bio_accept_st
76 { 79 {
77 int state; 80 int state;
@@ -82,39 +85,24 @@ typedef struct bio_accept_st
82 85
83 char *addr; 86 char *addr;
84 int nbio; 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;
85 BIO *bio_chain; 92 BIO *bio_chain;
86 } BIO_ACCEPT; 93 } BIO_ACCEPT;
87 94
88#ifndef NOPROTO 95static int acpt_write(BIO *h, const char *buf, int num);
89static int acpt_write(BIO *h,char *buf,int num); 96static int acpt_read(BIO *h, char *buf, int size);
90static int acpt_read(BIO *h,char *buf,int size); 97static int acpt_puts(BIO *h, const char *str);
91static int acpt_puts(BIO *h,char *str); 98static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
92static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2);
93static int acpt_new(BIO *h); 99static int acpt_new(BIO *h);
94static int acpt_free(BIO *data); 100static int acpt_free(BIO *data);
95#else
96static int acpt_write();
97static int acpt_read();
98static int acpt_puts();
99static long acpt_ctrl();
100static int acpt_new();
101static int acpt_free();
102#endif
103
104#ifndef NOPROTO
105static int acpt_state(BIO *b, BIO_ACCEPT *c); 101static int acpt_state(BIO *b, BIO_ACCEPT *c);
106static void acpt_close_socket(BIO *data); 102static void acpt_close_socket(BIO *data);
107BIO_ACCEPT *BIO_ACCEPT_new(void ); 103BIO_ACCEPT *BIO_ACCEPT_new(void );
108void BIO_ACCEPT_free(BIO_ACCEPT *a); 104void BIO_ACCEPT_free(BIO_ACCEPT *a);
109 105
110#else
111
112static int acpt_state();
113static void acpt_close_socket();
114BIO_ACCEPT *BIO_ACCEPT_new();
115void BIO_ACCEPT_free();
116#endif
117
118#define ACPT_S_BEFORE 1 106#define ACPT_S_BEFORE 1
119#define ACPT_S_GET_ACCEPT_SOCKET 2 107#define ACPT_S_GET_ACCEPT_SOCKET 2
120#define ACPT_S_OK 3 108#define ACPT_S_OK 3
@@ -130,15 +118,15 @@ static BIO_METHOD methods_acceptp=
130 acpt_ctrl, 118 acpt_ctrl,
131 acpt_new, 119 acpt_new,
132 acpt_free, 120 acpt_free,
121 NULL,
133 }; 122 };
134 123
135BIO_METHOD *BIO_s_accept() 124BIO_METHOD *BIO_s_accept(void)
136 { 125 {
137 return(&methods_acceptp); 126 return(&methods_acceptp);
138 } 127 }
139 128
140static int acpt_new(bi) 129static int acpt_new(BIO *bi)
141BIO *bi;
142 { 130 {
143 BIO_ACCEPT *ba; 131 BIO_ACCEPT *ba;
144 132
@@ -153,29 +141,31 @@ BIO *bi;
153 return(1); 141 return(1);
154 } 142 }
155 143
156BIO_ACCEPT *BIO_ACCEPT_new() 144BIO_ACCEPT *BIO_ACCEPT_new(void)
157 { 145 {
158 BIO_ACCEPT *ret; 146 BIO_ACCEPT *ret;
159 147
160 if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL) 148 if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
161 return(NULL); 149 return(NULL);
162 150
163 memset(ret,0,sizeof(BIO_ACCEPT)); 151 memset(ret,0,sizeof(BIO_ACCEPT));
164 ret->accept_sock=INVALID_SOCKET; 152 ret->accept_sock=INVALID_SOCKET;
153 ret->bind_mode=BIO_BIND_NORMAL;
165 return(ret); 154 return(ret);
166 } 155 }
167 156
168void BIO_ACCEPT_free(a) 157void BIO_ACCEPT_free(BIO_ACCEPT *a)
169BIO_ACCEPT *a;
170 { 158 {
171 if (a->param_addr != NULL) Free(a->param_addr); 159 if(a == NULL)
172 if (a->addr != NULL) Free(a->addr); 160 return;
161
162 if (a->param_addr != NULL) OPENSSL_free(a->param_addr);
163 if (a->addr != NULL) OPENSSL_free(a->addr);
173 if (a->bio_chain != NULL) BIO_free(a->bio_chain); 164 if (a->bio_chain != NULL) BIO_free(a->bio_chain);
174 Free(a); 165 OPENSSL_free(a);
175 } 166 }
176 167
177static void acpt_close_socket(bio) 168static void acpt_close_socket(BIO *bio)
178BIO *bio;
179 { 169 {
180 BIO_ACCEPT *c; 170 BIO_ACCEPT *c;
181 171
@@ -183,18 +173,13 @@ BIO *bio;
183 if (c->accept_sock != INVALID_SOCKET) 173 if (c->accept_sock != INVALID_SOCKET)
184 { 174 {
185 shutdown(c->accept_sock,2); 175 shutdown(c->accept_sock,2);
186# ifdef WINDOWS
187 closesocket(c->accept_sock); 176 closesocket(c->accept_sock);
188# else
189 close(c->accept_sock);
190# endif
191 c->accept_sock=INVALID_SOCKET; 177 c->accept_sock=INVALID_SOCKET;
192 bio->num=INVALID_SOCKET; 178 bio->num=INVALID_SOCKET;
193 } 179 }
194 } 180 }
195 181
196static int acpt_free(a) 182static int acpt_free(BIO *a)
197BIO *a;
198 { 183 {
199 BIO_ACCEPT *data; 184 BIO_ACCEPT *data;
200 185
@@ -212,12 +197,9 @@ BIO *a;
212 return(1); 197 return(1);
213 } 198 }
214 199
215static int acpt_state(b,c) 200static int acpt_state(BIO *b, BIO_ACCEPT *c)
216BIO *b;
217BIO_ACCEPT *c;
218 { 201 {
219 BIO *bio=NULL,*dbio; 202 BIO *bio=NULL,*dbio;
220 unsigned long l=1;
221 int s= -1; 203 int s= -1;
222 int i; 204 int i;
223 205
@@ -230,56 +212,58 @@ again:
230 BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); 212 BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
231 return(-1); 213 return(-1);
232 } 214 }
233 s=BIO_get_accept_socket(c->param_addr); 215 s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
234 if (s == INVALID_SOCKET) 216 if (s == INVALID_SOCKET)
235 return(-1); 217 return(-1);
236 218
237#ifdef FIONBIO
238 if (c->accept_nbio) 219 if (c->accept_nbio)
239 { 220 {
240 i=BIO_socket_ioctl(b->num,FIONBIO,&l); 221 if (!BIO_socket_nbio(s,1))
241 if (i < 0)
242 { 222 {
243#ifdef WINDOWS
244 closesocket(s); 223 closesocket(s);
245#else
246 close(s);
247# endif
248 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); 224 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
249 return(-1); 225 return(-1);
250 } 226 }
251 } 227 }
252#endif
253 c->accept_sock=s; 228 c->accept_sock=s;
254 b->num=s; 229 b->num=s;
255 c->state=ACPT_S_GET_ACCEPT_SOCKET; 230 c->state=ACPT_S_GET_ACCEPT_SOCKET;
256 return(1); 231 return(1);
257 break; 232 /* break; */
258 case ACPT_S_GET_ACCEPT_SOCKET: 233 case ACPT_S_GET_ACCEPT_SOCKET:
259 if (b->next_bio != NULL) 234 if (b->next_bio != NULL)
260 { 235 {
261 c->state=ACPT_S_OK; 236 c->state=ACPT_S_OK;
262 goto again; 237 goto again;
263 } 238 }
239 BIO_clear_retry_flags(b);
240 b->retry_reason=0;
264 i=BIO_accept(c->accept_sock,&(c->addr)); 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
265 if (i < 0) return(i); 251 if (i < 0) return(i);
252
266 bio=BIO_new_socket(i,BIO_CLOSE); 253 bio=BIO_new_socket(i,BIO_CLOSE);
267 if (bio == NULL) goto err; 254 if (bio == NULL) goto err;
268 255
269 BIO_set_callback(bio,BIO_get_callback(b)); 256 BIO_set_callback(bio,BIO_get_callback(b));
270 BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); 257 BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
271 258
272#ifdef FIONBIO
273 if (c->nbio) 259 if (c->nbio)
274 { 260 {
275 i=BIO_socket_ioctl(i,FIONBIO,&l); 261 if (!BIO_socket_nbio(i,1))
276 if (i < 0)
277 { 262 {
278 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); 263 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
279 goto err; 264 goto err;
280 } 265 }
281 } 266 }
282#endif
283 267
284 /* If the accept BIO has an bio_chain, we dup it and 268 /* If the accept BIO has an bio_chain, we dup it and
285 * put the new socket at the end. */ 269 * put the new socket at the end. */
@@ -298,15 +282,9 @@ err:
298 if (bio != NULL) 282 if (bio != NULL)
299 BIO_free(bio); 283 BIO_free(bio);
300 else if (s >= 0) 284 else if (s >= 0)
301 {
302#ifdef WINDOWS
303 closesocket(s); 285 closesocket(s);
304#else
305 close(s);
306# endif
307 }
308 return(0); 286 return(0);
309 break; 287 /* break; */
310 case ACPT_S_OK: 288 case ACPT_S_OK:
311 if (b->next_bio == NULL) 289 if (b->next_bio == NULL)
312 { 290 {
@@ -314,23 +292,20 @@ err:
314 goto again; 292 goto again;
315 } 293 }
316 return(1); 294 return(1);
317 break; 295 /* break; */
318 default: 296 default:
319 return(0); 297 return(0);
320 break; 298 /* break; */
321 } 299 }
322 300
323 } 301 }
324 302
325static int acpt_read(b,out,outl) 303static int acpt_read(BIO *b, char *out, int outl)
326BIO *b;
327char *out;
328int outl;
329 { 304 {
330 int ret=0; 305 int ret=0;
331 BIO_ACCEPT *data; 306 BIO_ACCEPT *data;
332 307
333 BIO_clear_retry_flags(b); 308 BIO_clear_retry_flags(b);
334 data=(BIO_ACCEPT *)b->ptr; 309 data=(BIO_ACCEPT *)b->ptr;
335 310
336 while (b->next_bio == NULL) 311 while (b->next_bio == NULL)
@@ -344,10 +319,7 @@ int outl;
344 return(ret); 319 return(ret);
345 } 320 }
346 321
347static int acpt_write(b,in,inl) 322static int acpt_write(BIO *b, const char *in, int inl)
348BIO *b;
349char *in;
350int inl;
351 { 323 {
352 int ret; 324 int ret;
353 BIO_ACCEPT *data; 325 BIO_ACCEPT *data;
@@ -366,11 +338,7 @@ int inl;
366 return(ret); 338 return(ret);
367 } 339 }
368 340
369static long acpt_ctrl(b,cmd,num,ptr) 341static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
370BIO *b;
371int cmd;
372long num;
373char *ptr;
374 { 342 {
375 BIO *dbio; 343 BIO *dbio;
376 int *ip; 344 int *ip;
@@ -399,7 +367,7 @@ char *ptr;
399 { 367 {
400 b->init=1; 368 b->init=1;
401 if (data->param_addr != NULL) 369 if (data->param_addr != NULL)
402 Free(data->param_addr); 370 OPENSSL_free(data->param_addr);
403 data->param_addr=BUF_strdup(ptr); 371 data->param_addr=BUF_strdup(ptr);
404 } 372 }
405 else if (num == 1) 373 else if (num == 1)
@@ -417,13 +385,21 @@ char *ptr;
417 case BIO_C_SET_NBIO: 385 case BIO_C_SET_NBIO:
418 data->nbio=(int)num; 386 data->nbio=(int)num;
419 break; 387 break;
388 case BIO_C_SET_FD:
389 b->init=1;
390 b->num= *((int *)ptr);
391 data->accept_sock=b->num;
392 data->state=ACPT_S_GET_ACCEPT_SOCKET;
393 b->shutdown=(int)num;
394 b->init=1;
395 break;
420 case BIO_C_GET_FD: 396 case BIO_C_GET_FD:
421 if (b->init) 397 if (b->init)
422 { 398 {
423 ip=(int *)ptr; 399 ip=(int *)ptr;
424 if (ip != NULL) 400 if (ip != NULL)
425 *ip=data->accept_sock; 401 *ip=data->accept_sock;
426 ret=b->num; 402 ret=data->accept_sock;
427 } 403 }
428 else 404 else
429 ret= -1; 405 ret= -1;
@@ -454,6 +430,12 @@ char *ptr;
454 break; 430 break;
455 case BIO_CTRL_FLUSH: 431 case BIO_CTRL_FLUSH:
456 break; 432 break;
433 case BIO_C_SET_BIND_MODE:
434 data->bind_mode=(int)num;
435 break;
436 case BIO_C_GET_BIND_MODE:
437 ret=(long)data->bind_mode;
438 break;
457 case BIO_CTRL_DUP: 439 case BIO_CTRL_DUP:
458 dbio=(BIO *)ptr; 440 dbio=(BIO *)ptr;
459/* if (data->param_port) EAY EAY 441/* if (data->param_port) EAY EAY
@@ -470,9 +452,7 @@ char *ptr;
470 return(ret); 452 return(ret);
471 } 453 }
472 454
473static int acpt_puts(bp,str) 455static int acpt_puts(BIO *bp, const char *str)
474BIO *bp;
475char *str;
476 { 456 {
477 int n,ret; 457 int n,ret;
478 458
@@ -481,8 +461,7 @@ char *str;
481 return(ret); 461 return(ret);
482 } 462 }
483 463
484BIO *BIO_new_accept(str) 464BIO *BIO_new_accept(char *str)
485char *str;
486 { 465 {
487 BIO *ret; 466 BIO *ret;
488 467
diff --git a/src/lib/libcrypto/bio/bss_bio.c b/src/lib/libcrypto/bio/bss_bio.c
index 562e9d8de2..1c485a4479 100644
--- a/src/lib/libcrypto/bio/bss_bio.c
+++ b/src/lib/libcrypto/bio/bss_bio.c
@@ -7,25 +7,46 @@
7 * for which no specific BIO method is available. 7 * for which no specific BIO method is available.
8 * See ssl/ssltest.c for some hints on how this can be used. */ 8 * See ssl/ssltest.c for some hints on how this can be used. */
9 9
10/* BIO_DEBUG implies BIO_PAIR_DEBUG */
11#ifdef BIO_DEBUG
12# ifndef BIO_PAIR_DEBUG
13# define BIO_PAIR_DEBUG
14# endif
15#endif
16
17/* disable assert() unless BIO_PAIR_DEBUG has been defined */
10#ifndef BIO_PAIR_DEBUG 18#ifndef BIO_PAIR_DEBUG
11# undef NDEBUG /* avoid conflicting definitions */ 19# ifndef NDEBUG
12# define NDEBUG 20# define NDEBUG
21# endif
13#endif 22#endif
14 23
15#include <assert.h> 24#include <assert.h>
25#include <limits.h>
16#include <stdlib.h> 26#include <stdlib.h>
17#include <string.h> 27#include <string.h>
18 28
19#include <openssl/bio.h> 29#include <openssl/bio.h>
20#include <openssl/err.h> 30#include <openssl/err.h>
31#include <openssl/err.h>
21#include <openssl/crypto.h> 32#include <openssl/crypto.h>
22 33
34#include "e_os.h"
35
36/* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
37#if defined(OPENSSL_SYS_VSWORKS)
38# undef SSIZE_MAX
39#endif
40#ifndef SSIZE_MAX
41# define SSIZE_MAX INT_MAX
42#endif
43
23static int bio_new(BIO *bio); 44static int bio_new(BIO *bio);
24static int bio_free(BIO *bio); 45static int bio_free(BIO *bio);
25static int bio_read(BIO *bio, char *buf, int size); 46static int bio_read(BIO *bio, char *buf, int size);
26static int bio_write(BIO *bio, char *buf, int num); 47static int bio_write(BIO *bio, const char *buf, int num);
27static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr); 48static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
28static int bio_puts(BIO *bio, char *str); 49static int bio_puts(BIO *bio, const char *str);
29 50
30static int bio_make_pair(BIO *bio1, BIO *bio2); 51static int bio_make_pair(BIO *bio1, BIO *bio2);
31static void bio_destroy_pair(BIO *bio); 52static void bio_destroy_pair(BIO *bio);
@@ -40,7 +61,8 @@ static BIO_METHOD methods_biop =
40 NULL /* no bio_gets */, 61 NULL /* no bio_gets */,
41 bio_ctrl, 62 bio_ctrl,
42 bio_new, 63 bio_new,
43 bio_free 64 bio_free,
65 NULL /* no bio_callback_ctrl */
44}; 66};
45 67
46BIO_METHOD *BIO_s_bio(void) 68BIO_METHOD *BIO_s_bio(void)
@@ -64,7 +86,7 @@ struct bio_bio_st
64 86
65 size_t request; /* valid iff peer != NULL; 0 if len != 0, 87 size_t request; /* valid iff peer != NULL; 0 if len != 0,
66 * otherwise set by peer to number of bytes 88 * otherwise set by peer to number of bytes
67 * it (unsuccesfully) tried to read, 89 * it (unsuccessfully) tried to read,
68 * never more than buffer space (size-len) warrants. */ 90 * never more than buffer space (size-len) warrants. */
69}; 91};
70 92
@@ -72,7 +94,7 @@ static int bio_new(BIO *bio)
72 { 94 {
73 struct bio_bio_st *b; 95 struct bio_bio_st *b;
74 96
75 b = Malloc(sizeof *b); 97 b = OPENSSL_malloc(sizeof *b);
76 if (b == NULL) 98 if (b == NULL)
77 return 0; 99 return 0;
78 100
@@ -100,10 +122,10 @@ static int bio_free(BIO *bio)
100 122
101 if (b->buf != NULL) 123 if (b->buf != NULL)
102 { 124 {
103 Free(b->buf); 125 OPENSSL_free(b->buf);
104 } 126 }
105 127
106 Free(b); 128 OPENSSL_free(b);
107 129
108 return 1; 130 return 1;
109 } 131 }
@@ -195,7 +217,87 @@ static int bio_read(BIO *bio, char *buf, int size_)
195 return size; 217 return size;
196 } 218 }
197 219
198static int bio_write(BIO *bio, char *buf, int num_) 220/* non-copying interface: provide pointer to available data in buffer
221 * bio_nread0: return number of available bytes
222 * bio_nread: also advance index
223 * (example usage: bio_nread0(), read from buffer, bio_nread()
224 * or just bio_nread(), read from buffer)
225 */
226/* WARNING: The non-copying interface is largely untested as of yet
227 * and may contain bugs. */
228static ssize_t bio_nread0(BIO *bio, char **buf)
229 {
230 struct bio_bio_st *b, *peer_b;
231 ssize_t num;
232
233 BIO_clear_retry_flags(bio);
234
235 if (!bio->init)
236 return 0;
237
238 b = bio->ptr;
239 assert(b != NULL);
240 assert(b->peer != NULL);
241 peer_b = b->peer->ptr;
242 assert(peer_b != NULL);
243 assert(peer_b->buf != NULL);
244
245 peer_b->request = 0;
246
247 if (peer_b->len == 0)
248 {
249 char dummy;
250
251 /* avoid code duplication -- nothing available for reading */
252 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
253 }
254
255 num = peer_b->len;
256 if (peer_b->size < peer_b->offset + num)
257 /* no ring buffer wrap-around for non-copying interface */
258 num = peer_b->size - peer_b->offset;
259 assert(num > 0);
260
261 if (buf != NULL)
262 *buf = peer_b->buf + peer_b->offset;
263 return num;
264 }
265
266static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
267 {
268 struct bio_bio_st *b, *peer_b;
269 ssize_t num, available;
270
271 if (num_ > SSIZE_MAX)
272 num = SSIZE_MAX;
273 else
274 num = (ssize_t)num_;
275
276 available = bio_nread0(bio, buf);
277 if (num > available)
278 num = available;
279 if (num <= 0)
280 return num;
281
282 b = bio->ptr;
283 peer_b = b->peer->ptr;
284
285 peer_b->len -= num;
286 if (peer_b->len)
287 {
288 peer_b->offset += num;
289 assert(peer_b->offset <= peer_b->size);
290 if (peer_b->offset == peer_b->size)
291 peer_b->offset = 0;
292 }
293 else
294 peer_b->offset = 0;
295
296 return num;
297 }
298
299
300static int bio_write(BIO *bio, const char *buf, int num_)
199 { 301 {
200 size_t num = num_; 302 size_t num = num_;
201 size_t rest; 303 size_t rest;
@@ -268,6 +370,83 @@ static int bio_write(BIO *bio, char *buf, int num_)
268 return num; 370 return num;
269 } 371 }
270 372
373/* non-copying interface: provide pointer to region to write to
374 * bio_nwrite0: check how much space is available
375 * bio_nwrite: also increase length
376 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
377 * or just bio_nwrite(), write to buffer)
378 */
379static ssize_t bio_nwrite0(BIO *bio, char **buf)
380 {
381 struct bio_bio_st *b;
382 size_t num;
383 size_t write_offset;
384
385 BIO_clear_retry_flags(bio);
386
387 if (!bio->init)
388 return 0;
389
390 b = bio->ptr;
391 assert(b != NULL);
392 assert(b->peer != NULL);
393 assert(b->buf != NULL);
394
395 b->request = 0;
396 if (b->closed)
397 {
398 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
399 return -1;
400 }
401
402 assert(b->len <= b->size);
403
404 if (b->len == b->size)
405 {
406 BIO_set_retry_write(bio);
407 return -1;
408 }
409
410 num = b->size - b->len;
411 write_offset = b->offset + b->len;
412 if (write_offset >= b->size)
413 write_offset -= b->size;
414 if (write_offset + num > b->size)
415 /* no ring buffer wrap-around for non-copying interface
416 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
417 * BIO_nwrite may have to be called twice) */
418 num = b->size - write_offset;
419
420 if (buf != NULL)
421 *buf = b->buf + write_offset;
422 assert(write_offset + num <= b->size);
423
424 return num;
425 }
426
427static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
428 {
429 struct bio_bio_st *b;
430 ssize_t num, space;
431
432 if (num_ > SSIZE_MAX)
433 num = SSIZE_MAX;
434 else
435 num = (ssize_t)num_;
436
437 space = bio_nwrite0(bio, buf);
438 if (num > space)
439 num = space;
440 if (num <= 0)
441 return num;
442 b = bio->ptr;
443 assert(b != NULL);
444 b->len += num;
445 assert(b->len <= b->size);
446
447 return num;
448 }
449
271 450
272static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) 451static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
273 { 452 {
@@ -299,7 +478,7 @@ static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
299 { 478 {
300 if (b->buf) 479 if (b->buf)
301 { 480 {
302 Free(b->buf); 481 OPENSSL_free(b->buf);
303 b->buf = NULL; 482 b->buf = NULL;
304 } 483 }
305 b->size = new_size; 484 b->size = new_size;
@@ -309,7 +488,8 @@ static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
309 break; 488 break;
310 489
311 case BIO_C_GET_WRITE_BUF_SIZE: 490 case BIO_C_GET_WRITE_BUF_SIZE:
312 num = (long) b->size; 491 ret = (long) b->size;
492 break;
313 493
314 case BIO_C_MAKE_BIO_PAIR: 494 case BIO_C_MAKE_BIO_PAIR:
315 { 495 {
@@ -331,7 +511,7 @@ static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
331 511
332 case BIO_C_GET_WRITE_GUARANTEE: 512 case BIO_C_GET_WRITE_GUARANTEE:
333 /* How many bytes can the caller feed to the next write 513 /* How many bytes can the caller feed to the next write
334 * withouth having to keep any? */ 514 * without having to keep any? */
335 if (b->peer == NULL || b->closed) 515 if (b->peer == NULL || b->closed)
336 ret = 0; 516 ret = 0;
337 else 517 else
@@ -339,18 +519,47 @@ static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
339 break; 519 break;
340 520
341 case BIO_C_GET_READ_REQUEST: 521 case BIO_C_GET_READ_REQUEST:
342 /* If the peer unsuccesfully tried to read, how many bytes 522 /* If the peer unsuccessfully tried to read, how many bytes
343 * were requested? (As with BIO_CTRL_PENDING, that number 523 * were requested? (As with BIO_CTRL_PENDING, that number
344 * can usually be treated as boolean.) */ 524 * can usually be treated as boolean.) */
345 ret = (long) b->request; 525 ret = (long) b->request;
346 break; 526 break;
347 527
528 case BIO_C_RESET_READ_REQUEST:
529 /* Reset request. (Can be useful after read attempts
530 * at the other side that are meant to be non-blocking,
531 * e.g. when probing SSL_read to see if any data is
532 * available.) */
533 b->request = 0;
534 ret = 1;
535 break;
536
348 case BIO_C_SHUTDOWN_WR: 537 case BIO_C_SHUTDOWN_WR:
349 /* similar to shutdown(..., SHUT_WR) */ 538 /* similar to shutdown(..., SHUT_WR) */
350 b->closed = 1; 539 b->closed = 1;
351 ret = 1; 540 ret = 1;
352 break; 541 break;
353 542
543 case BIO_C_NREAD0:
544 /* prepare for non-copying read */
545 ret = (long) bio_nread0(bio, ptr);
546 break;
547
548 case BIO_C_NREAD:
549 /* non-copying read */
550 ret = (long) bio_nread(bio, ptr, (size_t) num);
551 break;
552
553 case BIO_C_NWRITE0:
554 /* prepare for non-copying write */
555 ret = (long) bio_nwrite0(bio, ptr);
556 break;
557
558 case BIO_C_NWRITE:
559 /* non-copying write */
560 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
561 break;
562
354 563
355 /* standard CTRL codes follow */ 564 /* standard CTRL codes follow */
356 565
@@ -434,7 +643,7 @@ static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
434 return ret; 643 return ret;
435 } 644 }
436 645
437static int bio_puts(BIO *bio, char *str) 646static int bio_puts(BIO *bio, const char *str)
438 { 647 {
439 return bio_write(bio, str, strlen(str)); 648 return bio_write(bio, str, strlen(str));
440 } 649 }
@@ -458,7 +667,7 @@ static int bio_make_pair(BIO *bio1, BIO *bio2)
458 667
459 if (b1->buf == NULL) 668 if (b1->buf == NULL)
460 { 669 {
461 b1->buf = Malloc(b1->size); 670 b1->buf = OPENSSL_malloc(b1->size);
462 if (b1->buf == NULL) 671 if (b1->buf == NULL)
463 { 672 {
464 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); 673 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
@@ -470,7 +679,7 @@ static int bio_make_pair(BIO *bio1, BIO *bio2)
470 679
471 if (b2->buf == NULL) 680 if (b2->buf == NULL)
472 { 681 {
473 b2->buf = Malloc(b2->size); 682 b2->buf = OPENSSL_malloc(b2->size);
474 if (b2->buf == NULL) 683 if (b2->buf == NULL)
475 { 684 {
476 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); 685 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
@@ -586,3 +795,78 @@ size_t BIO_ctrl_get_read_request(BIO *bio)
586 { 795 {
587 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL); 796 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
588 } 797 }
798
799int BIO_ctrl_reset_read_request(BIO *bio)
800 {
801 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
802 }
803
804
805/* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
806 * (conceivably some other BIOs could allow non-copying reads and writes too.)
807 */
808int BIO_nread0(BIO *bio, char **buf)
809 {
810 long ret;
811
812 if (!bio->init)
813 {
814 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
815 return -2;
816 }
817
818 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
819 if (ret > INT_MAX)
820 return INT_MAX;
821 else
822 return (int) ret;
823 }
824
825int BIO_nread(BIO *bio, char **buf, int num)
826 {
827 int ret;
828
829 if (!bio->init)
830 {
831 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
832 return -2;
833 }
834
835 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
836 if (ret > 0)
837 bio->num_read += ret;
838 return ret;
839 }
840
841int BIO_nwrite0(BIO *bio, char **buf)
842 {
843 long ret;
844
845 if (!bio->init)
846 {
847 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
848 return -2;
849 }
850
851 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
852 if (ret > INT_MAX)
853 return INT_MAX;
854 else
855 return (int) ret;
856 }
857
858int BIO_nwrite(BIO *bio, char **buf, int num)
859 {
860 int ret;
861
862 if (!bio->init)
863 {
864 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
865 return -2;
866 }
867
868 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
869 if (ret > 0)
870 bio->num_read += ret;
871 return ret;
872 }
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c
index 6e547bf866..f91ae4c8c6 100644
--- a/src/lib/libcrypto/bio/bss_conn.c
+++ b/src/lib/libcrypto/bio/bss_conn.c
@@ -56,22 +56,26 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#ifndef NO_SOCK 59#ifndef OPENSSL_NO_SOCK
60 60
61#include <stdio.h> 61#include <stdio.h>
62#include <errno.h> 62#include <errno.h>
63#define USE_SOCKETS 63#define USE_SOCKETS
64#include "cryptlib.h" 64#include "cryptlib.h"
65#include "bio.h" 65#include <openssl/bio.h>
66 66
67/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ 67#ifdef OPENSSL_SYS_WIN16
68
69#ifdef WIN16
70#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ 68#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71#else 69#else
72#define SOCKET_PROTOCOL IPPROTO_TCP 70#define SOCKET_PROTOCOL IPPROTO_TCP
73#endif 71#endif
74 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
75typedef struct bio_connect_st 79typedef struct bio_connect_st
76 { 80 {
77 int state; 81 int state;
@@ -81,52 +85,32 @@ typedef struct bio_connect_st
81 int nbio; 85 int nbio;
82 86
83 unsigned char ip[4]; 87 unsigned char ip[4];
84 short port; 88 unsigned short port;
85 89
86 struct sockaddr_in them; 90 struct sockaddr_in them;
87 91
88 /* int socket; this will be kept in bio->num so that it is 92 /* int socket; this will be kept in bio->num so that it is
89 * compatable with the bss_sock bio */ 93 * compatible with the bss_sock bio */
90 int error;
91 94
92 /* called when the connection is initially made 95 /* called when the connection is initially made
93 * callback(BIO,state,ret); The callback should return 96 * callback(BIO,state,ret); The callback should return
94 * 'ret'. state is for compatablity with the ssl info_callback */ 97 * 'ret'. state is for compatibility with the ssl info_callback */
95 int (*info_callback)(); 98 int (*info_callback)(const BIO *bio,int state,int ret);
96 } BIO_CONNECT; 99 } BIO_CONNECT;
97 100
98#ifndef NOPROTO 101static int conn_write(BIO *h, const char *buf, int num);
99static int conn_write(BIO *h,char *buf,int num); 102static int conn_read(BIO *h, char *buf, int size);
100static int conn_read(BIO *h,char *buf,int size); 103static int conn_puts(BIO *h, const char *str);
101static int conn_puts(BIO *h,char *str); 104static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
102static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2);
103static int conn_new(BIO *h); 105static int conn_new(BIO *h);
104static int conn_free(BIO *data); 106static int conn_free(BIO *data);
105#else 107static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
106static int conn_write();
107static int conn_read();
108static int conn_puts();
109static long conn_ctrl();
110static int conn_new();
111static int conn_free();
112#endif
113
114#ifndef NOPROTO
115 108
116static int conn_state(BIO *b, BIO_CONNECT *c); 109static int conn_state(BIO *b, BIO_CONNECT *c);
117static void conn_close_socket(BIO *data); 110static void conn_close_socket(BIO *data);
118BIO_CONNECT *BIO_CONNECT_new(void ); 111BIO_CONNECT *BIO_CONNECT_new(void );
119void BIO_CONNECT_free(BIO_CONNECT *a); 112void BIO_CONNECT_free(BIO_CONNECT *a);
120 113
121#else
122
123static int conn_state();
124static void conn_close_socket();
125BIO_CONNECT *BIO_CONNECT_new();
126void BIO_CONNECT_free();
127
128#endif
129
130static BIO_METHOD methods_connectp= 114static BIO_METHOD methods_connectp=
131 { 115 {
132 BIO_TYPE_CONNECT, 116 BIO_TYPE_CONNECT,
@@ -138,11 +122,10 @@ static BIO_METHOD methods_connectp=
138 conn_ctrl, 122 conn_ctrl,
139 conn_new, 123 conn_new,
140 conn_free, 124 conn_free,
125 conn_callback_ctrl,
141 }; 126 };
142 127
143static int conn_state(b,c) 128static int conn_state(BIO *b, BIO_CONNECT *c)
144BIO *b;
145BIO_CONNECT *c;
146 { 129 {
147 int ret= -1,i; 130 int ret= -1,i;
148 unsigned long l; 131 unsigned long l;
@@ -160,7 +143,7 @@ BIO_CONNECT *c;
160 p=c->param_hostname; 143 p=c->param_hostname;
161 if (p == NULL) 144 if (p == NULL)
162 { 145 {
163 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTHNAME_SPECIFIED); 146 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
164 goto exit_loop; 147 goto exit_loop;
165 } 148 }
166 for ( ; *p != '\0'; p++) 149 for ( ; *p != '\0'; p++)
@@ -182,12 +165,12 @@ BIO_CONNECT *c;
182 break; 165 break;
183 } 166 }
184 if (c->param_port != NULL) 167 if (c->param_port != NULL)
185 Free(c->param_port); 168 OPENSSL_free(c->param_port);
186 c->param_port=BUF_strdup(p); 169 c->param_port=BUF_strdup(p);
187 } 170 }
188 } 171 }
189 172
190 if (p == NULL) 173 if (c->param_port == NULL)
191 { 174 {
192 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED); 175 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
193 ERR_add_error_data(2,"host=",c->param_hostname); 176 ERR_add_error_data(2,"host=",c->param_hostname);
@@ -203,7 +186,12 @@ BIO_CONNECT *c;
203 break; 186 break;
204 187
205 case BIO_CONN_S_GET_PORT: 188 case BIO_CONN_S_GET_PORT:
206 if (BIO_get_port(c->param_port,&c->port) <= 0) 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)
207 goto exit_loop; 195 goto exit_loop;
208 c->state=BIO_CONN_S_CREATE_SOCKET; 196 c->state=BIO_CONN_S_CREATE_SOCKET;
209 break; 197 break;
@@ -235,12 +223,9 @@ BIO_CONNECT *c;
235 break; 223 break;
236 224
237 case BIO_CONN_S_NBIO: 225 case BIO_CONN_S_NBIO:
238#ifdef FIONBIO
239 if (c->nbio) 226 if (c->nbio)
240 { 227 {
241 l=1; 228 if (!BIO_socket_nbio(b->num,1))
242 ret=BIO_socket_ioctl(b->num,FIONBIO,&l);
243 if (ret < 0)
244 { 229 {
245 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO); 230 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
246 ERR_add_error_data(4,"host=", 231 ERR_add_error_data(4,"host=",
@@ -249,10 +234,9 @@ BIO_CONNECT *c;
249 goto exit_loop; 234 goto exit_loop;
250 } 235 }
251 } 236 }
252#endif
253 c->state=BIO_CONN_S_CONNECT; 237 c->state=BIO_CONN_S_CONNECT;
254 238
255#ifdef SO_KEEPALIVE 239#if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
256 i=1; 240 i=1;
257 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); 241 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
258 if (i < 0) 242 if (i < 0)
@@ -315,7 +299,7 @@ BIO_CONNECT *c;
315 ret=1; 299 ret=1;
316 goto exit_loop; 300 goto exit_loop;
317 default: 301 default:
318 abort(); 302 /* abort(); */
319 goto exit_loop; 303 goto exit_loop;
320 } 304 }
321 305
@@ -326,21 +310,19 @@ BIO_CONNECT *c;
326 } 310 }
327 } 311 }
328 312
329 if (1) 313 /* Loop does not exit */
330 {
331exit_loop: 314exit_loop:
332 if (cb != NULL) 315 if (cb != NULL)
333 ret=cb((BIO *)b,c->state,ret); 316 ret=cb((BIO *)b,c->state,ret);
334 }
335end: 317end:
336 return(ret); 318 return(ret);
337 } 319 }
338 320
339BIO_CONNECT *BIO_CONNECT_new() 321BIO_CONNECT *BIO_CONNECT_new(void)
340 { 322 {
341 BIO_CONNECT *ret; 323 BIO_CONNECT *ret;
342 324
343 if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL) 325 if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
344 return(NULL); 326 return(NULL);
345 ret->state=BIO_CONN_S_BEFORE; 327 ret->state=BIO_CONN_S_BEFORE;
346 ret->param_hostname=NULL; 328 ret->param_hostname=NULL;
@@ -353,27 +335,27 @@ BIO_CONNECT *BIO_CONNECT_new()
353 ret->ip[3]=0; 335 ret->ip[3]=0;
354 ret->port=0; 336 ret->port=0;
355 memset((char *)&ret->them,0,sizeof(ret->them)); 337 memset((char *)&ret->them,0,sizeof(ret->them));
356 ret->error=0;
357 return(ret); 338 return(ret);
358 } 339 }
359 340
360void BIO_CONNECT_free(a) 341void BIO_CONNECT_free(BIO_CONNECT *a)
361BIO_CONNECT *a;
362 { 342 {
343 if(a == NULL)
344 return;
345
363 if (a->param_hostname != NULL) 346 if (a->param_hostname != NULL)
364 Free(a->param_hostname); 347 OPENSSL_free(a->param_hostname);
365 if (a->param_port != NULL) 348 if (a->param_port != NULL)
366 Free(a->param_port); 349 OPENSSL_free(a->param_port);
367 Free(a); 350 OPENSSL_free(a);
368 } 351 }
369 352
370BIO_METHOD *BIO_s_connect() 353BIO_METHOD *BIO_s_connect(void)
371 { 354 {
372 return(&methods_connectp); 355 return(&methods_connectp);
373 } 356 }
374 357
375static int conn_new(bi) 358static int conn_new(BIO *bi)
376BIO *bi;
377 { 359 {
378 bi->init=0; 360 bi->init=0;
379 bi->num=INVALID_SOCKET; 361 bi->num=INVALID_SOCKET;
@@ -384,8 +366,7 @@ BIO *bi;
384 return(1); 366 return(1);
385 } 367 }
386 368
387static void conn_close_socket(bio) 369static void conn_close_socket(BIO *bio)
388BIO *bio;
389 { 370 {
390 BIO_CONNECT *c; 371 BIO_CONNECT *c;
391 372
@@ -395,17 +376,12 @@ BIO *bio;
395 /* Only do a shutdown if things were established */ 376 /* Only do a shutdown if things were established */
396 if (c->state == BIO_CONN_S_OK) 377 if (c->state == BIO_CONN_S_OK)
397 shutdown(bio->num,2); 378 shutdown(bio->num,2);
398# ifdef WINDOWS
399 closesocket(bio->num); 379 closesocket(bio->num);
400# else
401 close(bio->num);
402# endif
403 bio->num=INVALID_SOCKET; 380 bio->num=INVALID_SOCKET;
404 } 381 }
405 } 382 }
406 383
407static int conn_free(a) 384static int conn_free(BIO *a)
408BIO *a;
409 { 385 {
410 BIO_CONNECT *data; 386 BIO_CONNECT *data;
411 387
@@ -423,10 +399,7 @@ BIO *a;
423 return(1); 399 return(1);
424 } 400 }
425 401
426static int conn_read(b,out,outl) 402static int conn_read(BIO *b, char *out, int outl)
427BIO *b;
428char *out;
429int outl;
430 { 403 {
431 int ret=0; 404 int ret=0;
432 BIO_CONNECT *data; 405 BIO_CONNECT *data;
@@ -442,11 +415,7 @@ int outl;
442 if (out != NULL) 415 if (out != NULL)
443 { 416 {
444 clear_socket_error(); 417 clear_socket_error();
445#if defined(WINDOWS) 418 ret=readsocket(b->num,out,outl);
446 ret=recv(b->num,out,outl,0);
447#else
448 ret=read(b->num,out,outl);
449#endif
450 BIO_clear_retry_flags(b); 419 BIO_clear_retry_flags(b);
451 if (ret <= 0) 420 if (ret <= 0)
452 { 421 {
@@ -457,10 +426,7 @@ int outl;
457 return(ret); 426 return(ret);
458 } 427 }
459 428
460static int conn_write(b,in,inl) 429static int conn_write(BIO *b, const char *in, int inl)
461BIO *b;
462char *in;
463int inl;
464 { 430 {
465 int ret; 431 int ret;
466 BIO_CONNECT *data; 432 BIO_CONNECT *data;
@@ -473,11 +439,7 @@ int inl;
473 } 439 }
474 440
475 clear_socket_error(); 441 clear_socket_error();
476#if defined(WINDOWS) 442 ret=writesocket(b->num,in,inl);
477 ret=send(b->num,in,inl,0);
478#else
479 ret=write(b->num,in,inl);
480#endif
481 BIO_clear_retry_flags(b); 443 BIO_clear_retry_flags(b);
482 if (ret <= 0) 444 if (ret <= 0)
483 { 445 {
@@ -487,15 +449,11 @@ int inl;
487 return(ret); 449 return(ret);
488 } 450 }
489 451
490static long conn_ctrl(b,cmd,num,ptr) 452static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
491BIO *b;
492int cmd;
493long num;
494char *ptr;
495 { 453 {
496 BIO *dbio; 454 BIO *dbio;
497 int *ip; 455 int *ip;
498 char **pptr; 456 const char **pptr;
499 long ret=1; 457 long ret=1;
500 BIO_CONNECT *data; 458 BIO_CONNECT *data;
501 459
@@ -519,7 +477,7 @@ char *ptr;
519 case BIO_C_GET_CONNECT: 477 case BIO_C_GET_CONNECT:
520 if (ptr != NULL) 478 if (ptr != NULL)
521 { 479 {
522 pptr=(char **)ptr; 480 pptr=(const char **)ptr;
523 if (num == 0) 481 if (num == 0)
524 { 482 {
525 *pptr=data->param_hostname; 483 *pptr=data->param_hostname;
@@ -538,7 +496,7 @@ char *ptr;
538 *((int *)ptr)=data->port; 496 *((int *)ptr)=data->port;
539 } 497 }
540 if ((!b->init) || (ptr == NULL)) 498 if ((!b->init) || (ptr == NULL))
541 *pptr="not initalised"; 499 *pptr="not initialized";
542 ret=1; 500 ret=1;
543 } 501 }
544 break; 502 break;
@@ -549,19 +507,37 @@ char *ptr;
549 if (num == 0) 507 if (num == 0)
550 { 508 {
551 if (data->param_hostname != NULL) 509 if (data->param_hostname != NULL)
552 Free(data->param_hostname); 510 OPENSSL_free(data->param_hostname);
553 data->param_hostname=BUF_strdup(ptr); 511 data->param_hostname=BUF_strdup(ptr);
554 } 512 }
555 else if (num == 1) 513 else if (num == 1)
556 { 514 {
557 if (data->param_port != NULL) 515 if (data->param_port != NULL)
558 Free(data->param_port); 516 OPENSSL_free(data->param_port);
559 data->param_port=BUF_strdup(ptr); 517 data->param_port=BUF_strdup(ptr);
560 } 518 }
561 else if (num == 2) 519 else if (num == 2)
562 memcpy(data->ip,ptr,4); 520 {
521 char buf[16];
522 char *p = ptr;
523
524 sprintf(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 }
563 else if (num == 3) 531 else if (num == 3)
532 {
533 char buf[16];
534
535 sprintf(buf,"%d",*(int *)ptr);
536 if (data->param_port != NULL)
537 OPENSSL_free(data->param_port);
538 data->param_port=BUF_strdup(buf);
564 data->port= *(int *)ptr; 539 data->port= *(int *)ptr;
540 }
565 } 541 }
566 break; 542 break;
567 case BIO_C_SET_NBIO: 543 case BIO_C_SET_NBIO:
@@ -591,16 +567,26 @@ char *ptr;
591 case BIO_CTRL_FLUSH: 567 case BIO_CTRL_FLUSH:
592 break; 568 break;
593 case BIO_CTRL_DUP: 569 case BIO_CTRL_DUP:
570 {
594 dbio=(BIO *)ptr; 571 dbio=(BIO *)ptr;
595 if (data->param_port) 572 if (data->param_port)
596 BIO_set_conn_port(dbio,data->param_port); 573 BIO_set_conn_port(dbio,data->param_port);
597 if (data->param_hostname) 574 if (data->param_hostname)
598 BIO_set_conn_hostname(dbio,data->param_hostname); 575 BIO_set_conn_hostname(dbio,data->param_hostname);
599 BIO_set_nbio(dbio,data->nbio); 576 BIO_set_nbio(dbio,data->nbio);
600 BIO_set_info_callback(dbio,data->info_callback); 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 }
601 break; 580 break;
602 case BIO_CTRL_SET_CALLBACK: 581 case BIO_CTRL_SET_CALLBACK:
603 data->info_callback=(int (*)())ptr; 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 }
604 break; 590 break;
605 case BIO_CTRL_GET_CALLBACK: 591 case BIO_CTRL_GET_CALLBACK:
606 { 592 {
@@ -617,9 +603,28 @@ char *ptr;
617 return(ret); 603 return(ret);
618 } 604 }
619 605
620static int conn_puts(bp,str) 606static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
621BIO *bp; 607 {
622char *str; 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)
623 { 628 {
624 int n,ret; 629 int n,ret;
625 630
@@ -628,8 +633,7 @@ char *str;
628 return(ret); 633 return(ret);
629 } 634 }
630 635
631BIO *BIO_new_connect(str) 636BIO *BIO_new_connect(char *str)
632char *str;
633 { 637 {
634 BIO *ret; 638 BIO *ret;
635 639
diff --git a/src/lib/libcrypto/bio/bss_fd.c b/src/lib/libcrypto/bio/bss_fd.c
index 686c4909a2..5e3e187de6 100644
--- a/src/lib/libcrypto/bio/bss_fd.c
+++ b/src/lib/libcrypto/bio/bss_fd.c
@@ -56,7 +56,227 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#define BIO_FD 59#include <stdio.h>
60#include "bss_sock.c" 60#include <errno.h>
61#undef BIO_FD 61#define USE_SOCKETS
62#include "cryptlib.h"
63#include <openssl/bio.h>
62 64
65static int fd_write(BIO *h, const char *buf, int num);
66static int fd_read(BIO *h, char *buf, int size);
67static int fd_puts(BIO *h, const char *str);
68static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static int fd_new(BIO *h);
70static int fd_free(BIO *data);
71int BIO_fd_should_retry(int s);
72
73static BIO_METHOD methods_fdp=
74 {
75 BIO_TYPE_FD,"file descriptor",
76 fd_write,
77 fd_read,
78 fd_puts,
79 NULL, /* fd_gets, */
80 fd_ctrl,
81 fd_new,
82 fd_free,
83 NULL,
84 };
85
86BIO_METHOD *BIO_s_fd(void)
87 {
88 return(&methods_fdp);
89 }
90
91BIO *BIO_new_fd(int fd,int close_flag)
92 {
93 BIO *ret;
94 ret=BIO_new(BIO_s_fd());
95 if (ret == NULL) return(NULL);
96 BIO_set_fd(ret,fd,close_flag);
97 return(ret);
98 }
99
100static int fd_new(BIO *bi)
101 {
102 bi->init=0;
103 bi->num=0;
104 bi->ptr=NULL;
105 bi->flags=0;
106 return(1);
107 }
108
109static int fd_free(BIO *a)
110 {
111 if (a == NULL) return(0);
112 if (a->shutdown)
113 {
114 if (a->init)
115 {
116 close(a->num);
117 }
118 a->init=0;
119 a->flags=0;
120 }
121 return(1);
122 }
123
124static int fd_read(BIO *b, char *out,int outl)
125 {
126 int ret=0;
127
128 if (out != NULL)
129 {
130 clear_sys_error();
131 ret=read(b->num,out,outl);
132 BIO_clear_retry_flags(b);
133 if (ret <= 0)
134 {
135 if (BIO_fd_should_retry(ret))
136 BIO_set_retry_read(b);
137 }
138 }
139 return(ret);
140 }
141
142static int fd_write(BIO *b, const char *in, int inl)
143 {
144 int ret;
145 clear_sys_error();
146 ret=write(b->num,in,inl);
147 BIO_clear_retry_flags(b);
148 if (ret <= 0)
149 {
150 if (BIO_fd_should_retry(ret))
151 BIO_set_retry_write(b);
152 }
153 return(ret);
154 }
155
156static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
157 {
158 long ret=1;
159 int *ip;
160
161 switch (cmd)
162 {
163 case BIO_CTRL_RESET:
164 num=0;
165 case BIO_C_FILE_SEEK:
166 ret=(long)lseek(b->num,num,0);
167 break;
168 case BIO_C_FILE_TELL:
169 case BIO_CTRL_INFO:
170 ret=(long)lseek(b->num,0,1);
171 break;
172 case BIO_C_SET_FD:
173 fd_free(b);
174 b->num= *((int *)ptr);
175 b->shutdown=(int)num;
176 b->init=1;
177 break;
178 case BIO_C_GET_FD:
179 if (b->init)
180 {
181 ip=(int *)ptr;
182 if (ip != NULL) *ip=b->num;
183 ret=b->num;
184 }
185 else
186 ret= -1;
187 break;
188 case BIO_CTRL_GET_CLOSE:
189 ret=b->shutdown;
190 break;
191 case BIO_CTRL_SET_CLOSE:
192 b->shutdown=(int)num;
193 break;
194 case BIO_CTRL_PENDING:
195 case BIO_CTRL_WPENDING:
196 ret=0;
197 break;
198 case BIO_CTRL_DUP:
199 case BIO_CTRL_FLUSH:
200 ret=1;
201 break;
202 default:
203 ret=0;
204 break;
205 }
206 return(ret);
207 }
208
209static int fd_puts(BIO *bp, const char *str)
210 {
211 int n,ret;
212
213 n=strlen(str);
214 ret=fd_write(bp,str,n);
215 return(ret);
216 }
217
218int BIO_fd_should_retry(int i)
219 {
220 int err;
221
222 if ((i == 0) || (i == -1))
223 {
224 err=get_last_sys_error();
225
226#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
227 if ((i == -1) && (err == 0))
228 return(1);
229#endif
230
231 return(BIO_fd_non_fatal_error(err));
232 }
233 return(0);
234 }
235
236int BIO_fd_non_fatal_error(int err)
237 {
238 switch (err)
239 {
240
241#ifdef EWOULDBLOCK
242# ifdef WSAEWOULDBLOCK
243# if WSAEWOULDBLOCK != EWOULDBLOCK
244 case EWOULDBLOCK:
245# endif
246# else
247 case EWOULDBLOCK:
248# endif
249#endif
250
251#if defined(ENOTCONN)
252 case ENOTCONN:
253#endif
254
255#ifdef EINTR
256 case EINTR:
257#endif
258
259#ifdef EAGAIN
260#if EWOULDBLOCK != EAGAIN
261 case EAGAIN:
262# endif
263#endif
264
265#ifdef EPROTO
266 case EPROTO:
267#endif
268
269#ifdef EINPROGRESS
270 case EINPROGRESS:
271#endif
272
273#ifdef EALREADY
274 case EALREADY:
275#endif
276 return(1);
277 /* break; */
278 default:
279 break;
280 }
281 return(0);
282 }
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c
index 1484cf849e..8b3ff278d9 100644
--- a/src/lib/libcrypto/bio/bss_file.c
+++ b/src/lib/libcrypto/bio/bss_file.c
@@ -68,29 +68,18 @@
68#include <stdio.h> 68#include <stdio.h>
69#include <errno.h> 69#include <errno.h>
70#include "cryptlib.h" 70#include "cryptlib.h"
71#include "bio.h" 71#include <openssl/bio.h>
72#include "err.h" 72#include <openssl/err.h>
73 73
74#if !defined(NO_STDIO) 74#if !defined(OPENSSL_NO_STDIO)
75 75
76#ifndef NOPROTO 76static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
77static int MS_CALLBACK file_write(BIO *h,char *buf,int num); 77static int MS_CALLBACK file_read(BIO *h, char *buf, int size);
78static int MS_CALLBACK file_read(BIO *h,char *buf,int size); 78static int MS_CALLBACK file_puts(BIO *h, const char *str);
79static int MS_CALLBACK file_puts(BIO *h,char *str); 79static int MS_CALLBACK file_gets(BIO *h, char *str, int size);
80static int MS_CALLBACK file_gets(BIO *h,char *str,int size); 80static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
81static long MS_CALLBACK file_ctrl(BIO *h,int cmd,long arg1,char *arg2);
82static int MS_CALLBACK file_new(BIO *h); 81static int MS_CALLBACK file_new(BIO *h);
83static int MS_CALLBACK file_free(BIO *data); 82static int MS_CALLBACK file_free(BIO *data);
84#else
85static int MS_CALLBACK file_write();
86static int MS_CALLBACK file_read();
87static int MS_CALLBACK file_puts();
88static int MS_CALLBACK file_gets();
89static long MS_CALLBACK file_ctrl();
90static int MS_CALLBACK file_new();
91static int MS_CALLBACK file_free();
92#endif
93
94static BIO_METHOD methods_filep= 83static BIO_METHOD methods_filep=
95 { 84 {
96 BIO_TYPE_FILE, 85 BIO_TYPE_FILE,
@@ -102,11 +91,10 @@ static BIO_METHOD methods_filep=
102 file_ctrl, 91 file_ctrl,
103 file_new, 92 file_new,
104 file_free, 93 file_free,
94 NULL,
105 }; 95 };
106 96
107BIO *BIO_new_file(filename,mode) 97BIO *BIO_new_file(const char *filename, const char *mode)
108char *filename;
109char *mode;
110 { 98 {
111 BIO *ret; 99 BIO *ret;
112 FILE *file; 100 FILE *file;
@@ -115,7 +103,10 @@ char *mode;
115 { 103 {
116 SYSerr(SYS_F_FOPEN,get_last_sys_error()); 104 SYSerr(SYS_F_FOPEN,get_last_sys_error());
117 ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); 105 ERR_add_error_data(5,"fopen('",filename,"','",mode,"')");
118 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); 106 if (errno == ENOENT)
107 BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE);
108 else
109 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
119 return(NULL); 110 return(NULL);
120 } 111 }
121 if ((ret=BIO_new(BIO_s_file_internal())) == NULL) 112 if ((ret=BIO_new(BIO_s_file_internal())) == NULL)
@@ -125,9 +116,7 @@ char *mode;
125 return(ret); 116 return(ret);
126 } 117 }
127 118
128BIO *BIO_new_fp(stream,close_flag) 119BIO *BIO_new_fp(FILE *stream, int close_flag)
129FILE *stream;
130int close_flag;
131 { 120 {
132 BIO *ret; 121 BIO *ret;
133 122
@@ -138,13 +127,12 @@ int close_flag;
138 return(ret); 127 return(ret);
139 } 128 }
140 129
141BIO_METHOD *BIO_s_file() 130BIO_METHOD *BIO_s_file(void)
142 { 131 {
143 return(&methods_filep); 132 return(&methods_filep);
144 } 133 }
145 134
146static int MS_CALLBACK file_new(bi) 135static int MS_CALLBACK file_new(BIO *bi)
147BIO *bi;
148 { 136 {
149 bi->init=0; 137 bi->init=0;
150 bi->num=0; 138 bi->num=0;
@@ -152,8 +140,7 @@ BIO *bi;
152 return(1); 140 return(1);
153 } 141 }
154 142
155static int MS_CALLBACK file_free(a) 143static int MS_CALLBACK file_free(BIO *a)
156BIO *a;
157 { 144 {
158 if (a == NULL) return(0); 145 if (a == NULL) return(0);
159 if (a->shutdown) 146 if (a->shutdown)
@@ -168,10 +155,7 @@ BIO *a;
168 return(1); 155 return(1);
169 } 156 }
170 157
171static int MS_CALLBACK file_read(b,out,outl) 158static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
172BIO *b;
173char *out;
174int outl;
175 { 159 {
176 int ret=0; 160 int ret=0;
177 161
@@ -182,10 +166,7 @@ int outl;
182 return(ret); 166 return(ret);
183 } 167 }
184 168
185static int MS_CALLBACK file_write(b,in,inl) 169static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
186BIO *b;
187char *in;
188int inl;
189 { 170 {
190 int ret=0; 171 int ret=0;
191 172
@@ -194,18 +175,14 @@ int inl;
194 if (fwrite(in,(int)inl,1,(FILE *)b->ptr)) 175 if (fwrite(in,(int)inl,1,(FILE *)b->ptr))
195 ret=inl; 176 ret=inl;
196 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ 177 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
197 /* acording to Tim Hudson <tjh@cryptsoft.com>, the commented 178 /* according to Tim Hudson <tjh@cryptsoft.com>, the commented
198 * out version above can cause 'inl' write calls under 179 * out version above can cause 'inl' write calls under
199 * some stupid stdio implementations (VMS) */ 180 * some stupid stdio implementations (VMS) */
200 } 181 }
201 return(ret); 182 return(ret);
202 } 183 }
203 184
204static long MS_CALLBACK file_ctrl(b,cmd,num,ptr) 185static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
205BIO *b;
206int cmd;
207long num;
208char *ptr;
209 { 186 {
210 long ret=1; 187 long ret=1;
211 FILE *fp=(FILE *)b->ptr; 188 FILE *fp=(FILE *)b->ptr;
@@ -214,26 +191,33 @@ char *ptr;
214 191
215 switch (cmd) 192 switch (cmd)
216 { 193 {
194 case BIO_C_FILE_SEEK:
217 case BIO_CTRL_RESET: 195 case BIO_CTRL_RESET:
218 ret=(long)fseek(fp,num,0); 196 ret=(long)fseek(fp,num,0);
219 break; 197 break;
220 case BIO_CTRL_EOF: 198 case BIO_CTRL_EOF:
221 ret=(long)feof(fp); 199 ret=(long)feof(fp);
222 break; 200 break;
201 case BIO_C_FILE_TELL:
223 case BIO_CTRL_INFO: 202 case BIO_CTRL_INFO:
224 ret=ftell(fp); 203 ret=ftell(fp);
225 break; 204 break;
226 case BIO_C_SET_FILE_PTR: 205 case BIO_C_SET_FILE_PTR:
227 file_free(b); 206 file_free(b);
228 b->shutdown=(int)num; 207 b->shutdown=(int)num&BIO_CLOSE;
229 b->ptr=(char *)ptr; 208 b->ptr=(char *)ptr;
230 b->init=1; 209 b->init=1;
231#if defined(MSDOS) || defined(WINDOWS) 210#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
232 /* Set correct text/binary mode */ 211 /* Set correct text/binary mode */
233 if (num & BIO_FP_TEXT) 212 if (num & BIO_FP_TEXT)
234 _setmode(fileno((FILE *)ptr),_O_TEXT); 213 _setmode(fileno((FILE *)ptr),_O_TEXT);
235 else 214 else
236 _setmode(fileno((FILE *)ptr),_O_BINARY); 215 _setmode(fileno((FILE *)ptr),_O_BINARY);
216#elif defined(OPENSSL_SYS_OS2)
217 if (num & BIO_FP_TEXT)
218 setmode(fileno((FILE *)ptr), O_TEXT);
219 else
220 setmode(fileno((FILE *)ptr), O_BINARY);
237#endif 221#endif
238 break; 222 break;
239 case BIO_C_SET_FILENAME: 223 case BIO_C_SET_FILENAME:
@@ -257,7 +241,7 @@ char *ptr;
257 ret=0; 241 ret=0;
258 break; 242 break;
259 } 243 }
260#if defined(MSDOS) || defined(WINDOWS) 244#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
261 if (!(num & BIO_FP_TEXT)) 245 if (!(num & BIO_FP_TEXT))
262 strcat(p,"b"); 246 strcat(p,"b");
263 else 247 else
@@ -307,10 +291,7 @@ char *ptr;
307 return(ret); 291 return(ret);
308 } 292 }
309 293
310static int MS_CALLBACK file_gets(bp,buf,size) 294static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
311BIO *bp;
312char *buf;
313int size;
314 { 295 {
315 int ret=0; 296 int ret=0;
316 297
@@ -321,9 +302,7 @@ int size;
321 return(ret); 302 return(ret);
322 } 303 }
323 304
324static int MS_CALLBACK file_puts(bp,str) 305static int MS_CALLBACK file_puts(BIO *bp, const char *str)
325BIO *bp;
326char *str;
327 { 306 {
328 int n,ret; 307 int n,ret;
329 308
@@ -332,7 +311,7 @@ char *str;
332 return(ret); 311 return(ret);
333 } 312 }
334 313
335#endif /* NO_STDIO */ 314#endif /* OPENSSL_NO_STDIO */
336 315
337#endif /* HEADER_BSS_FILE_C */ 316#endif /* HEADER_BSS_FILE_C */
338 317
diff --git a/src/lib/libcrypto/bio/bss_log.c b/src/lib/libcrypto/bio/bss_log.c
index db82e757e7..a39d95297c 100644
--- a/src/lib/libcrypto/bio/bss_log.c
+++ b/src/lib/libcrypto/bio/bss_log.c
@@ -57,8 +57,8 @@
57 Why BIO_s_log? 57 Why BIO_s_log?
58 58
59 BIO_s_log is useful for system daemons (or services under NT). 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 (or event log 60 It is one-way BIO, it sends all stuff to syslogd (on system that
61 under NT). 61 commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
62 62
63*/ 63*/
64 64
@@ -66,27 +66,71 @@
66#include <stdio.h> 66#include <stdio.h>
67#include <errno.h> 67#include <errno.h>
68 68
69#ifndef WIN32 69#include "cryptlib.h"
70#ifdef __ultrix 70
71#include <sys/syslog.h> 71#if defined(OPENSSL_SYS_WIN32)
72#else 72# include <process.h>
73#include <syslog.h> 73#elif defined(OPENSSL_SYS_VMS)
74#endif 74# include <opcdef.h>
75# include <descrip.h>
76# include <lib$routines.h>
77# include <starlet.h>
78#elif defined(__ultrix)
79# include <sys/syslog.h>
80#elif !defined(MSDOS) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG) /* Unix */
81# include <syslog.h>
75#endif 82#endif
76 83
77#include "cryptlib.h"
78#include <openssl/buffer.h> 84#include <openssl/buffer.h>
79#include <openssl/err.h> 85#include <openssl/err.h>
86
80#ifndef NO_SYSLOG 87#ifndef NO_SYSLOG
81 88
89#if defined(OPENSSL_SYS_WIN32)
90#define LOG_EMERG 0
91#define LOG_ALERT 1
92#define LOG_CRIT 2
93#define LOG_ERR 3
94#define LOG_WARNING 4
95#define LOG_NOTICE 5
96#define LOG_INFO 6
97#define LOG_DEBUG 7
82 98
83static int MS_CALLBACK slg_write(BIO *h,char *buf,int num); 99#define LOG_DAEMON (3<<3)
84static int MS_CALLBACK slg_puts(BIO *h,char *str); 100#elif defined(OPENSSL_SYS_VMS)
85static long MS_CALLBACK slg_ctrl(BIO *h,int cmd,long arg1,char *arg2); 101/* On VMS, we don't really care about these, but we need them to compile */
102#define LOG_EMERG 0
103#define LOG_ALERT 1
104#define LOG_CRIT 2
105#define LOG_ERR 3
106#define LOG_WARNING 4
107#define LOG_NOTICE 5
108#define LOG_INFO 6
109#define LOG_DEBUG 7
110
111#define LOG_DAEMON OPC$M_NM_NTWORK
112#endif
113
114static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num);
115static int MS_CALLBACK slg_puts(BIO *h, const char *str);
116static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
86static int MS_CALLBACK slg_new(BIO *h); 117static int MS_CALLBACK slg_new(BIO *h);
87static int MS_CALLBACK slg_free(BIO *data); 118static int MS_CALLBACK slg_free(BIO *data);
88static int xopenlog(BIO* bp, const char* name, int level); 119static void xopenlog(BIO* bp, char* name, int level);
89static int xcloselog(BIO* bp); 120static void xsyslog(BIO* bp, int priority, const char* string);
121static void xcloselog(BIO* bp);
122#ifdef OPENSSL_SYS_WIN32
123LONG (WINAPI *go_for_advapi)() = RegOpenKeyEx;
124HANDLE (WINAPI *register_event_source)() = NULL;
125BOOL (WINAPI *deregister_event_source)() = NULL;
126BOOL (WINAPI *report_event)() = NULL;
127#define DL_PROC(m,f) (GetProcAddress( m, f ))
128#ifdef UNICODE
129#define DL_PROC_X(m,f) DL_PROC( m, f "W" )
130#else
131#define DL_PROC_X(m,f) DL_PROC( m, f "A" )
132#endif
133#endif
90 134
91static BIO_METHOD methods_slg= 135static BIO_METHOD methods_slg=
92 { 136 {
@@ -98,6 +142,7 @@ static BIO_METHOD methods_slg=
98 slg_ctrl, 142 slg_ctrl,
99 slg_new, 143 slg_new,
100 slg_free, 144 slg_free,
145 NULL,
101 }; 146 };
102 147
103BIO_METHOD *BIO_s_log(void) 148BIO_METHOD *BIO_s_log(void)
@@ -110,11 +155,7 @@ static int MS_CALLBACK slg_new(BIO *bi)
110 bi->init=1; 155 bi->init=1;
111 bi->num=0; 156 bi->num=0;
112 bi->ptr=NULL; 157 bi->ptr=NULL;
113#ifndef WIN32
114 xopenlog(bi, "application", LOG_DAEMON); 158 xopenlog(bi, "application", LOG_DAEMON);
115#else
116 xopenlog(bi, "application", 0);
117#endif
118 return(1); 159 return(1);
119 } 160 }
120 161
@@ -125,64 +166,60 @@ static int MS_CALLBACK slg_free(BIO *a)
125 return(1); 166 return(1);
126 } 167 }
127 168
128static int MS_CALLBACK slg_write(BIO *b, char *in, int inl) 169static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl)
129 { 170 {
130 int ret= inl; 171 int ret= inl;
131 char* buf= in; 172 char* buf;
132 char* pp; 173 char* pp;
133#if defined(WIN32) 174 int priority, i;
134 LPTSTR lpszStrings[1]; 175 static struct
135 WORD evtype= EVENTLOG_ERROR_TYPE; 176 {
136#else 177 int strl;
137 int priority; 178 char str[10];
138#endif 179 int log_level;
180 }
181 mapping[] =
182 {
183 { 6, "PANIC ", LOG_EMERG },
184 { 6, "EMERG ", LOG_EMERG },
185 { 4, "EMR ", LOG_EMERG },
186 { 6, "ALERT ", LOG_ALERT },
187 { 4, "ALR ", LOG_ALERT },
188 { 5, "CRIT ", LOG_CRIT },
189 { 4, "CRI ", LOG_CRIT },
190 { 6, "ERROR ", LOG_ERR },
191 { 4, "ERR ", LOG_ERR },
192 { 8, "WARNING ", LOG_WARNING },
193 { 5, "WARN ", LOG_WARNING },
194 { 4, "WAR ", LOG_WARNING },
195 { 7, "NOTICE ", LOG_NOTICE },
196 { 5, "NOTE ", LOG_NOTICE },
197 { 4, "NOT ", LOG_NOTICE },
198 { 5, "INFO ", LOG_INFO },
199 { 4, "INF ", LOG_INFO },
200 { 6, "DEBUG ", LOG_DEBUG },
201 { 4, "DBG ", LOG_DEBUG },
202 { 0, "", LOG_ERR } /* The default */
203 };
139 204
140 if((buf= (char *)Malloc(inl+ 1)) == NULL){ 205 if((buf= (char *)OPENSSL_malloc(inl+ 1)) == NULL){
141 return(0); 206 return(0);
142 } 207 }
143 strncpy(buf, in, inl); 208 strncpy(buf, in, inl);
144 buf[inl]= '\0'; 209 buf[inl]= '\0';
145#if defined(WIN32)
146 if(strncmp(buf, "ERR ", 4) == 0){
147 evtype= EVENTLOG_ERROR_TYPE;
148 pp= buf+ 4;
149 }else if(strncmp(buf, "WAR ", 4) == 0){
150 evtype= EVENTLOG_WARNING_TYPE;
151 pp= buf+ 4;
152 }else if(strncmp(buf, "INF ", 4) == 0){
153 evtype= EVENTLOG_INFORMATION_TYPE;
154 pp= buf+ 4;
155 }else{
156 evtype= EVENTLOG_ERROR_TYPE;
157 pp= buf;
158 }
159 lpszStrings[0]= pp;
160 210
161 if(b->ptr) 211 i = 0;
162 ReportEvent(b->ptr, evtype, 0, 1024, NULL, 1, 0, 212 while(strncmp(buf, mapping[i].str, mapping[i].strl) != 0) i++;
163 lpszStrings, NULL); 213 priority = mapping[i].log_level;
164#else 214 pp = buf + mapping[i].strl;
165 if(strncmp(buf, "ERR ", 4) == 0){
166 priority= LOG_ERR;
167 pp= buf+ 4;
168 }else if(strncmp(buf, "WAR ", 4) == 0){
169 priority= LOG_WARNING;
170 pp= buf+ 4;
171 }else if(strncmp(buf, "INF ", 4) == 0){
172 priority= LOG_INFO;
173 pp= buf+ 4;
174 }else{
175 priority= LOG_ERR;
176 pp= buf;
177 }
178 215
179 syslog(priority, "%s", pp); 216 xsyslog(b, priority, pp);
180#endif 217
181 Free(buf); 218 OPENSSL_free(buf);
182 return(ret); 219 return(ret);
183 } 220 }
184 221
185static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr) 222static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr)
186 { 223 {
187 switch (cmd) 224 switch (cmd)
188 { 225 {
@@ -196,7 +233,7 @@ static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr)
196 return(0); 233 return(0);
197 } 234 }
198 235
199static int MS_CALLBACK slg_puts(BIO *bp, char *str) 236static int MS_CALLBACK slg_puts(BIO *bp, const char *str)
200 { 237 {
201 int n,ret; 238 int n,ret;
202 239
@@ -205,28 +242,154 @@ static int MS_CALLBACK slg_puts(BIO *bp, char *str)
205 return(ret); 242 return(ret);
206 } 243 }
207 244
208static int xopenlog(BIO* bp, const char* name, int level) 245#if defined(OPENSSL_SYS_WIN32)
246
247static void xopenlog(BIO* bp, char* name, int level)
209{ 248{
210#if defined(WIN32) 249 if ( !register_event_source )
211 if((bp->ptr= (char *)RegisterEventSource(NULL, name)) == NULL){ 250 {
212 return(0); 251 HANDLE advapi;
213 } 252 if ( !(advapi = GetModuleHandle("advapi32")) )
214#else 253 return;
215 openlog(name, LOG_PID|LOG_CONS, level); 254 register_event_source = (HANDLE (WINAPI *)())DL_PROC_X(advapi,
216#endif 255 "RegisterEventSource" );
217 return(1); 256 deregister_event_source = (BOOL (WINAPI *)())DL_PROC(advapi,
257 "DeregisterEventSource");
258 report_event = (BOOL (WINAPI *)())DL_PROC_X(advapi,
259 "ReportEvent" );
260 if ( !(register_event_source && deregister_event_source &&
261 report_event) )
262 {
263 register_event_source = NULL;
264 deregister_event_source = NULL;
265 report_event = NULL;
266 return;
267 }
268 }
269 bp->ptr= (char *)register_event_source(NULL, name);
218} 270}
219 271
220static int xcloselog(BIO* bp) 272static void xsyslog(BIO *bp, int priority, const char *string)
273{
274 LPCSTR lpszStrings[2];
275 WORD evtype= EVENTLOG_ERROR_TYPE;
276 int pid = _getpid();
277 char pidbuf[20];
278
279 switch (priority)
280 {
281 case LOG_EMERG:
282 case LOG_ALERT:
283 case LOG_CRIT:
284 case LOG_ERR:
285 evtype = EVENTLOG_ERROR_TYPE;
286 break;
287 case LOG_WARNING:
288 evtype = EVENTLOG_WARNING_TYPE;
289 break;
290 case LOG_NOTICE:
291 case LOG_INFO:
292 case LOG_DEBUG:
293 evtype = EVENTLOG_INFORMATION_TYPE;
294 break;
295 default: /* Should never happen, but set it
296 as error anyway. */
297 evtype = EVENTLOG_ERROR_TYPE;
298 break;
299 }
300
301 sprintf(pidbuf, "[%d] ", pid);
302 lpszStrings[0] = pidbuf;
303 lpszStrings[1] = string;
304
305 if(report_event && bp->ptr)
306 report_event(bp->ptr, evtype, 0, 1024, NULL, 2, 0,
307 lpszStrings, NULL);
308}
309
310static void xcloselog(BIO* bp)
221{ 311{
222#if defined(WIN32) 312 if(deregister_event_source && bp->ptr)
223 if(bp->ptr) 313 deregister_event_source((HANDLE)(bp->ptr));
224 DeregisterEventSource((HANDLE)(bp->ptr));
225 bp->ptr= NULL; 314 bp->ptr= NULL;
226#else 315}
316
317#elif defined(OPENSSL_SYS_VMS)
318
319static int VMS_OPC_target = LOG_DAEMON;
320
321static void xopenlog(BIO* bp, char* name, int level)
322{
323 VMS_OPC_target = level;
324}
325
326static void xsyslog(BIO *bp, int priority, const char *string)
327{
328 struct dsc$descriptor_s opc_dsc;
329 struct opcdef *opcdef_p;
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 = (struct opcdef *) OPENSSL_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 = (char *)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 */
377
378static void xopenlog(BIO* bp, char* name, int level)
379{
380 openlog(name, LOG_PID|LOG_CONS, level);
381}
382
383static void xsyslog(BIO *bp, int priority, const char *string)
384{
385 syslog(priority, "%s", string);
386}
387
388static void xcloselog(BIO* bp)
389{
227 closelog(); 390 closelog();
228#endif
229 return(1);
230} 391}
231 392
232#endif 393#endif /* Unix */
394
395#endif /* NO_SYSLOG */
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c
index 40c4e39f02..28ff7582bf 100644
--- a/src/lib/libcrypto/bio/bss_mem.c
+++ b/src/lib/libcrypto/bio/bss_mem.c
@@ -59,26 +59,15 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <errno.h> 60#include <errno.h>
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include "bio.h" 62#include <openssl/bio.h>
63 63
64#ifndef NOPROTO 64static int mem_write(BIO *h, const char *buf, int num);
65static int mem_write(BIO *h,char *buf,int num); 65static int mem_read(BIO *h, char *buf, int size);
66static int mem_read(BIO *h,char *buf,int size); 66static int mem_puts(BIO *h, const char *str);
67static int mem_puts(BIO *h,char *str); 67static int mem_gets(BIO *h, char *str, int size);
68static int mem_gets(BIO *h,char *str,int size); 68static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static long mem_ctrl(BIO *h,int cmd,long arg1,char *arg2);
70static int mem_new(BIO *h); 69static int mem_new(BIO *h);
71static int mem_free(BIO *data); 70static int mem_free(BIO *data);
72#else
73static int mem_write();
74static int mem_read();
75static int mem_puts();
76static int mem_gets();
77static long mem_ctrl();
78static int mem_new();
79static int mem_free();
80#endif
81
82static BIO_METHOD mem_method= 71static BIO_METHOD mem_method=
83 { 72 {
84 BIO_TYPE_MEM, 73 BIO_TYPE_MEM,
@@ -90,15 +79,38 @@ static BIO_METHOD mem_method=
90 mem_ctrl, 79 mem_ctrl,
91 mem_new, 80 mem_new,
92 mem_free, 81 mem_free,
82 NULL,
93 }; 83 };
94 84
95BIO_METHOD *BIO_s_mem() 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)
96 { 89 {
97 return(&mem_method); 90 return(&mem_method);
98 } 91 }
99 92
100static int mem_new(bi) 93BIO *BIO_new_mem_buf(void *buf, int len)
101BIO *bi; 94{
95 BIO *ret;
96 BUF_MEM *b;
97 if (!buf) {
98 BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER);
99 return NULL;
100 }
101 if(len == -1) len = strlen(buf);
102 if(!(ret = BIO_new(BIO_s_mem())) ) return NULL;
103 b = (BUF_MEM *)ret->ptr;
104 b->data = buf;
105 b->length = len;
106 b->max = len;
107 ret->flags |= BIO_FLAGS_MEM_RDONLY;
108 /* Since this is static data retrying wont help */
109 ret->num = 0;
110 return ret;
111}
112
113static int mem_new(BIO *bi)
102 { 114 {
103 BUF_MEM *b; 115 BUF_MEM *b;
104 116
@@ -106,30 +118,29 @@ BIO *bi;
106 return(0); 118 return(0);
107 bi->shutdown=1; 119 bi->shutdown=1;
108 bi->init=1; 120 bi->init=1;
109 bi->num=0; 121 bi->num= -1;
110 bi->ptr=(char *)b; 122 bi->ptr=(char *)b;
111 return(1); 123 return(1);
112 } 124 }
113 125
114static int mem_free(a) 126static int mem_free(BIO *a)
115BIO *a;
116 { 127 {
117 if (a == NULL) return(0); 128 if (a == NULL) return(0);
118 if (a->shutdown) 129 if (a->shutdown)
119 { 130 {
120 if ((a->init) && (a->ptr != NULL)) 131 if ((a->init) && (a->ptr != NULL))
121 { 132 {
122 BUF_MEM_free((BUF_MEM *)a->ptr); 133 BUF_MEM *b;
134 b = (BUF_MEM *)a->ptr;
135 if(a->flags & BIO_FLAGS_MEM_RDONLY) b->data = NULL;
136 BUF_MEM_free(b);
123 a->ptr=NULL; 137 a->ptr=NULL;
124 } 138 }
125 } 139 }
126 return(1); 140 return(1);
127 } 141 }
128 142
129static int mem_read(b,out,outl) 143static int mem_read(BIO *b, char *out, int outl)
130BIO *b;
131char *out;
132int outl;
133 { 144 {
134 int ret= -1; 145 int ret= -1;
135 BUF_MEM *bm; 146 BUF_MEM *bm;
@@ -139,28 +150,27 @@ int outl;
139 bm=(BUF_MEM *)b->ptr; 150 bm=(BUF_MEM *)b->ptr;
140 BIO_clear_retry_flags(b); 151 BIO_clear_retry_flags(b);
141 ret=(outl > bm->length)?bm->length:outl; 152 ret=(outl > bm->length)?bm->length:outl;
142 if ((out != NULL) && (ret > 0)) 153 if ((out != NULL) && (ret > 0)) {
143 {
144 memcpy(out,bm->data,ret); 154 memcpy(out,bm->data,ret);
145 bm->length-=ret; 155 bm->length-=ret;
146 /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */ 156 /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */
147 from=(char *)&(bm->data[ret]); 157 if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret;
148 to=(char *)&(bm->data[0]); 158 else {
149 for (i=0; i<bm->length; i++) 159 from=(char *)&(bm->data[ret]);
150 to[i]=from[i]; 160 to=(char *)&(bm->data[0]);
161 for (i=0; i<bm->length; i++)
162 to[i]=from[i];
151 } 163 }
152 else if (bm->length == 0) 164 } else if (bm->length == 0)
153 { 165 {
154 BIO_set_retry_read(b); 166 ret = b->num;
155 ret= -1; 167 if (ret != 0)
168 BIO_set_retry_read(b);
156 } 169 }
157 return(ret); 170 return(ret);
158 } 171 }
159 172
160static int mem_write(b,in,inl) 173static int mem_write(BIO *b, const char *in, int inl)
161BIO *b;
162char *in;
163int inl;
164 { 174 {
165 int ret= -1; 175 int ret= -1;
166 int blen; 176 int blen;
@@ -173,6 +183,11 @@ int inl;
173 goto end; 183 goto end;
174 } 184 }
175 185
186 if(b->flags & BIO_FLAGS_MEM_RDONLY) {
187 BIOerr(BIO_F_MEM_WRITE,BIO_R_WRITE_TO_READ_ONLY_BIO);
188 goto end;
189 }
190
176 BIO_clear_retry_flags(b); 191 BIO_clear_retry_flags(b);
177 blen=bm->length; 192 blen=bm->length;
178 if (BUF_MEM_grow(bm,blen+inl) != (blen+inl)) 193 if (BUF_MEM_grow(bm,blen+inl) != (blen+inl))
@@ -183,11 +198,7 @@ end:
183 return(ret); 198 return(ret);
184 } 199 }
185 200
186static long mem_ctrl(b,cmd,num,ptr) 201static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
187BIO *b;
188int cmd;
189long num;
190char *ptr;
191 { 202 {
192 long ret=1; 203 long ret=1;
193 char **pptr; 204 char **pptr;
@@ -198,12 +209,26 @@ char *ptr;
198 { 209 {
199 case BIO_CTRL_RESET: 210 case BIO_CTRL_RESET:
200 if (bm->data != NULL) 211 if (bm->data != NULL)
201 memset(bm->data,0,bm->max); 212 {
202 bm->length=0; 213 /* For read only case reset to the start again */
214 if(b->flags & BIO_FLAGS_MEM_RDONLY)
215 {
216 bm->data -= bm->max - bm->length;
217 bm->length = bm->max;
218 }
219 else
220 {
221 memset(bm->data,0,bm->max);
222 bm->length=0;
223 }
224 }
203 break; 225 break;
204 case BIO_CTRL_EOF: 226 case BIO_CTRL_EOF:
205 ret=(long)(bm->length == 0); 227 ret=(long)(bm->length == 0);
206 break; 228 break;
229 case BIO_C_SET_BUF_MEM_EOF_RETURN:
230 b->num=(int)num;
231 break;
207 case BIO_CTRL_INFO: 232 case BIO_CTRL_INFO:
208 ret=(long)bm->length; 233 ret=(long)bm->length;
209 if (ptr != NULL) 234 if (ptr != NULL)
@@ -250,10 +275,7 @@ char *ptr;
250 return(ret); 275 return(ret);
251 } 276 }
252 277
253static int mem_gets(bp,buf,size) 278static int mem_gets(BIO *bp, char *buf, int size)
254BIO *bp;
255char *buf;
256int size;
257 { 279 {
258 int i,j; 280 int i,j;
259 int ret= -1; 281 int ret= -1;
@@ -283,9 +305,7 @@ int size;
283 return(ret); 305 return(ret);
284 } 306 }
285 307
286static int mem_puts(bp,str) 308static int mem_puts(BIO *bp, const char *str)
287BIO *bp;
288char *str;
289 { 309 {
290 int n,ret; 310 int n,ret;
291 311
diff --git a/src/lib/libcrypto/bio/bss_null.c b/src/lib/libcrypto/bio/bss_null.c
index 0791a2471a..46b73339df 100644
--- a/src/lib/libcrypto/bio/bss_null.c
+++ b/src/lib/libcrypto/bio/bss_null.c
@@ -59,26 +59,15 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <errno.h> 60#include <errno.h>
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include "bio.h" 62#include <openssl/bio.h>
63 63
64#ifndef NOPROTO 64static int null_write(BIO *h, const char *buf, int num);
65static int null_write(BIO *h,char *buf,int num); 65static int null_read(BIO *h, char *buf, int size);
66static int null_read(BIO *h,char *buf,int size); 66static int null_puts(BIO *h, const char *str);
67static int null_puts(BIO *h,char *str); 67static int null_gets(BIO *h, char *str, int size);
68static int null_gets(BIO *h,char *str,int size); 68static long null_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static long null_ctrl(BIO *h,int cmd,long arg1,char *arg2);
70static int null_new(BIO *h); 69static int null_new(BIO *h);
71static int null_free(BIO *data); 70static int null_free(BIO *data);
72#else
73static int null_write();
74static int null_read();
75static int null_puts();
76static int null_gets();
77static long null_ctrl();
78static int null_new();
79static int null_free();
80#endif
81
82static BIO_METHOD null_method= 71static BIO_METHOD null_method=
83 { 72 {
84 BIO_TYPE_NULL, 73 BIO_TYPE_NULL,
@@ -90,15 +79,15 @@ static BIO_METHOD null_method=
90 null_ctrl, 79 null_ctrl,
91 null_new, 80 null_new,
92 null_free, 81 null_free,
82 NULL,
93 }; 83 };
94 84
95BIO_METHOD *BIO_s_null() 85BIO_METHOD *BIO_s_null(void)
96 { 86 {
97 return(&null_method); 87 return(&null_method);
98 } 88 }
99 89
100static int null_new(bi) 90static int null_new(BIO *bi)
101BIO *bi;
102 { 91 {
103 bi->init=1; 92 bi->init=1;
104 bi->num=0; 93 bi->num=0;
@@ -106,34 +95,23 @@ BIO *bi;
106 return(1); 95 return(1);
107 } 96 }
108 97
109static int null_free(a) 98static int null_free(BIO *a)
110BIO *a;
111 { 99 {
112 if (a == NULL) return(0); 100 if (a == NULL) return(0);
113 return(1); 101 return(1);
114 } 102 }
115 103
116static int null_read(b,out,outl) 104static int null_read(BIO *b, char *out, int outl)
117BIO *b;
118char *out;
119int outl;
120 { 105 {
121 return(0); 106 return(0);
122 } 107 }
123 108
124static int null_write(b,in,inl) 109static int null_write(BIO *b, const char *in, int inl)
125BIO *b;
126char *in;
127int inl;
128 { 110 {
129 return(inl); 111 return(inl);
130 } 112 }
131 113
132static long null_ctrl(b,cmd,num,ptr) 114static long null_ctrl(BIO *b, int cmd, long num, void *ptr)
133BIO *b;
134int cmd;
135long num;
136char *ptr;
137 { 115 {
138 long ret=1; 116 long ret=1;
139 117
@@ -159,17 +137,12 @@ char *ptr;
159 return(ret); 137 return(ret);
160 } 138 }
161 139
162static int null_gets(bp,buf,size) 140static int null_gets(BIO *bp, char *buf, int size)
163BIO *bp;
164char *buf;
165int size;
166 { 141 {
167 return(0); 142 return(0);
168 } 143 }
169 144
170static int null_puts(bp,str) 145static int null_puts(BIO *bp, const char *str)
171BIO *bp;
172char *str;
173 { 146 {
174 if (str == NULL) return(0); 147 if (str == NULL) return(0);
175 return(strlen(str)); 148 return(strlen(str));
diff --git a/src/lib/libcrypto/bio/bss_sock.c b/src/lib/libcrypto/bio/bss_sock.c
index d907a2867b..fdabd16d7e 100644
--- a/src/lib/libcrypto/bio/bss_sock.c
+++ b/src/lib/libcrypto/bio/bss_sock.c
@@ -56,55 +56,22 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#if !defined(NO_SOCK) || defined(BIO_FD) 59#ifndef OPENSSL_NO_SOCK
60 60
61#include <stdio.h> 61#include <stdio.h>
62#include <errno.h> 62#include <errno.h>
63#define USE_SOCKETS 63#define USE_SOCKETS
64#include "cryptlib.h" 64#include "cryptlib.h"
65#include "bio.h" 65#include <openssl/bio.h>
66 66
67#ifndef BIO_FD 67static int sock_write(BIO *h, const char *buf, int num);
68#ifndef NOPROTO 68static int sock_read(BIO *h, char *buf, int size);
69static int sock_write(BIO *h,char *buf,int num); 69static int sock_puts(BIO *h, const char *str);
70static int sock_read(BIO *h,char *buf,int size); 70static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
71static int sock_puts(BIO *h,char *str);
72static long sock_ctrl(BIO *h,int cmd,long arg1,char *arg2);
73static int sock_new(BIO *h); 71static int sock_new(BIO *h);
74static int sock_free(BIO *data); 72static int sock_free(BIO *data);
75int BIO_sock_should_retry(int s); 73int BIO_sock_should_retry(int s);
76#else
77static int sock_write();
78static int sock_read();
79static int sock_puts();
80static long sock_ctrl();
81static int sock_new();
82static int sock_free();
83int BIO_sock_should_retry();
84#endif
85
86#else
87
88#ifndef NOPROTO
89static int fd_write(BIO *h,char *buf,int num);
90static int fd_read(BIO *h,char *buf,int size);
91static int fd_puts(BIO *h,char *str);
92static long fd_ctrl(BIO *h,int cmd,long arg1,char *arg2);
93static int fd_new(BIO *h);
94static int fd_free(BIO *data);
95int BIO_fd_should_retry(int s);
96#else
97static int fd_write();
98static int fd_read();
99static int fd_puts();
100static long fd_ctrl();
101static int fd_new();
102static int fd_free();
103int BIO_fd_should_retry();
104#endif
105#endif
106 74
107#ifndef BIO_FD
108static BIO_METHOD methods_sockp= 75static BIO_METHOD methods_sockp=
109 { 76 {
110 BIO_TYPE_SOCKET, 77 BIO_TYPE_SOCKET,
@@ -116,57 +83,25 @@ static BIO_METHOD methods_sockp=
116 sock_ctrl, 83 sock_ctrl,
117 sock_new, 84 sock_new,
118 sock_free, 85 sock_free,
86 NULL,
119 }; 87 };
120 88
121BIO_METHOD *BIO_s_socket() 89BIO_METHOD *BIO_s_socket(void)
122 { 90 {
123 return(&methods_sockp); 91 return(&methods_sockp);
124 } 92 }
125#else
126static BIO_METHOD methods_fdp=
127 {
128 BIO_TYPE_FD,"file descriptor",
129 fd_write,
130 fd_read,
131 fd_puts,
132 NULL, /* fd_gets, */
133 fd_ctrl,
134 fd_new,
135 fd_free,
136 };
137 93
138BIO_METHOD *BIO_s_fd() 94BIO *BIO_new_socket(int fd, int close_flag)
139 {
140 return(&methods_fdp);
141 }
142#endif
143
144#ifndef BIO_FD
145BIO *BIO_new_socket(fd,close_flag)
146#else
147BIO *BIO_new_fd(fd,close_flag)
148#endif
149int fd;
150int close_flag;
151 { 95 {
152 BIO *ret; 96 BIO *ret;
153 97
154#ifndef BIO_FD
155 ret=BIO_new(BIO_s_socket()); 98 ret=BIO_new(BIO_s_socket());
156#else
157 ret=BIO_new(BIO_s_fd());
158#endif
159 if (ret == NULL) return(NULL); 99 if (ret == NULL) return(NULL);
160 BIO_set_fd(ret,fd,close_flag); 100 BIO_set_fd(ret,fd,close_flag);
161 return(ret); 101 return(ret);
162 } 102 }
163 103
164#ifndef BIO_FD 104static int sock_new(BIO *bi)
165static int sock_new(bi)
166#else
167static int fd_new(bi)
168#endif
169BIO *bi;
170 { 105 {
171 bi->init=0; 106 bi->init=0;
172 bi->num=0; 107 bi->num=0;
@@ -175,29 +110,14 @@ BIO *bi;
175 return(1); 110 return(1);
176 } 111 }
177 112
178#ifndef BIO_FD 113static int sock_free(BIO *a)
179static int sock_free(a)
180#else
181static int fd_free(a)
182#endif
183BIO *a;
184 { 114 {
185 if (a == NULL) return(0); 115 if (a == NULL) return(0);
186 if (a->shutdown) 116 if (a->shutdown)
187 { 117 {
188 if (a->init) 118 if (a->init)
189 { 119 {
190#ifndef BIO_FD 120 SHUTDOWN2(a->num);
191 shutdown(a->num,2);
192# ifdef WINDOWS
193 closesocket(a->num);
194# else
195 close(a->num);
196# endif
197#else /* BIO_FD */
198 close(a->num);
199#endif
200
201 } 121 }
202 a->init=0; 122 a->init=0;
203 a->flags=0; 123 a->flags=0;
@@ -205,80 +125,40 @@ BIO *a;
205 return(1); 125 return(1);
206 } 126 }
207 127
208#ifndef BIO_FD 128static int sock_read(BIO *b, char *out, int outl)
209static int sock_read(b,out,outl)
210#else
211static int fd_read(b,out,outl)
212#endif
213BIO *b;
214char *out;
215int outl;
216 { 129 {
217 int ret=0; 130 int ret=0;
218 131
219 if (out != NULL) 132 if (out != NULL)
220 { 133 {
221#if defined(WINDOWS) && !defined(BIO_FD)
222 clear_socket_error(); 134 clear_socket_error();
223 ret=recv(b->num,out,outl,0); 135 ret=readsocket(b->num,out,outl);
224#else
225 clear_sys_error();
226 ret=read(b->num,out,outl);
227#endif
228 BIO_clear_retry_flags(b); 136 BIO_clear_retry_flags(b);
229 if (ret <= 0) 137 if (ret <= 0)
230 { 138 {
231#ifndef BIO_FD
232 if (BIO_sock_should_retry(ret)) 139 if (BIO_sock_should_retry(ret))
233#else
234 if (BIO_fd_should_retry(ret))
235#endif
236 BIO_set_retry_read(b); 140 BIO_set_retry_read(b);
237 } 141 }
238 } 142 }
239 return(ret); 143 return(ret);
240 } 144 }
241 145
242#ifndef BIO_FD 146static int sock_write(BIO *b, const char *in, int inl)
243static int sock_write(b,in,inl)
244#else
245static int fd_write(b,in,inl)
246#endif
247BIO *b;
248char *in;
249int inl;
250 { 147 {
251 int ret; 148 int ret;
252 149
253#if defined(WINDOWS) && !defined(BIO_FD)
254 clear_socket_error(); 150 clear_socket_error();
255 ret=send(b->num,in,inl,0); 151 ret=writesocket(b->num,in,inl);
256#else
257 clear_sys_error();
258 ret=write(b->num,in,inl);
259#endif
260 BIO_clear_retry_flags(b); 152 BIO_clear_retry_flags(b);
261 if (ret <= 0) 153 if (ret <= 0)
262 { 154 {
263#ifndef BIO_FD
264 if (BIO_sock_should_retry(ret)) 155 if (BIO_sock_should_retry(ret))
265#else
266 if (BIO_fd_should_retry(ret))
267#endif
268 BIO_set_retry_write(b); 156 BIO_set_retry_write(b);
269 } 157 }
270 return(ret); 158 return(ret);
271 } 159 }
272 160
273#ifndef BIO_FD 161static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
274static long sock_ctrl(b,cmd,num,ptr)
275#else
276static long fd_ctrl(b,cmd,num,ptr)
277#endif
278BIO *b;
279int cmd;
280long num;
281char *ptr;
282 { 162 {
283 long ret=1; 163 long ret=1;
284 int *ip; 164 int *ip;
@@ -286,21 +166,16 @@ char *ptr;
286 switch (cmd) 166 switch (cmd)
287 { 167 {
288 case BIO_CTRL_RESET: 168 case BIO_CTRL_RESET:
289#ifdef BIO_FD 169 num=0;
290 ret=(long)lseek(b->num,0,0); 170 case BIO_C_FILE_SEEK:
291#else
292 ret=0; 171 ret=0;
293#endif
294 break; 172 break;
173 case BIO_C_FILE_TELL:
295 case BIO_CTRL_INFO: 174 case BIO_CTRL_INFO:
296 ret=0; 175 ret=0;
297 break; 176 break;
298 case BIO_C_SET_FD: 177 case BIO_C_SET_FD:
299#ifndef BIO_FD
300 sock_free(b); 178 sock_free(b);
301#else
302 fd_free(b);
303#endif
304 b->num= *((int *)ptr); 179 b->num= *((int *)ptr);
305 b->shutdown=(int)num; 180 b->shutdown=(int)num;
306 b->init=1; 181 b->init=1;
@@ -329,7 +204,6 @@ char *ptr;
329 case BIO_CTRL_FLUSH: 204 case BIO_CTRL_FLUSH:
330 ret=1; 205 ret=1;
331 break; 206 break;
332 break;
333 default: 207 default:
334 ret=0; 208 ret=0;
335 break; 209 break;
@@ -337,82 +211,46 @@ char *ptr;
337 return(ret); 211 return(ret);
338 } 212 }
339 213
340#ifdef undef 214static int sock_puts(BIO *bp, const char *str)
341static int sock_gets(bp,buf,size)
342BIO *bp;
343char *buf;
344int size;
345 {
346 return(-1);
347 }
348#endif
349
350#ifndef BIO_FD
351static int sock_puts(bp,str)
352#else
353static int fd_puts(bp,str)
354#endif
355BIO *bp;
356char *str;
357 { 215 {
358 int n,ret; 216 int n,ret;
359 217
360 n=strlen(str); 218 n=strlen(str);
361#ifndef BIO_FD
362 ret=sock_write(bp,str,n); 219 ret=sock_write(bp,str,n);
363#else
364 ret=fd_write(bp,str,n);
365#endif
366 return(ret); 220 return(ret);
367 } 221 }
368 222
369#ifndef BIO_FD 223int BIO_sock_should_retry(int i)
370int BIO_sock_should_retry(i)
371#else
372int BIO_fd_should_retry(i)
373#endif
374int i;
375 { 224 {
376 int err; 225 int err;
377 226
378 if ((i == 0) || (i == -1)) 227 if ((i == 0) || (i == -1))
379 { 228 {
380#if !defined(BIO_FD) && defined(WINDOWS)
381 err=get_last_socket_error(); 229 err=get_last_socket_error();
382#else
383 err=get_last_sys_error();
384#endif
385 230
386#if defined(WINDOWS) /* more microsoft stupidity */ 231#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
387 if ((i == -1) && (err == 0)) 232 if ((i == -1) && (err == 0))
388 return(1); 233 return(1);
389#endif 234#endif
390 235
391#ifndef BIO_FD
392 return(BIO_sock_non_fatal_error(err)); 236 return(BIO_sock_non_fatal_error(err));
393#else
394 return(BIO_fd_non_fatal_error(err));
395#endif
396 } 237 }
397 return(0); 238 return(0);
398 } 239 }
399 240
400#ifndef BIO_FD 241int BIO_sock_non_fatal_error(int err)
401int BIO_sock_non_fatal_error(err)
402#else
403int BIO_fd_non_fatal_error(err)
404#endif
405int err;
406 { 242 {
407 switch (err) 243 switch (err)
408 { 244 {
409#if !defined(BIO_FD) && defined(WINDOWS) 245#if defined(OPENSSL_SYS_WINDOWS)
410# if defined(WSAEWOULDBLOCK) 246# if defined(WSAEWOULDBLOCK)
411 case WSAEWOULDBLOCK: 247 case WSAEWOULDBLOCK:
412# endif 248# endif
413 249
414# if defined(WSAENOTCONN) 250# if 0 /* This appears to always be an error */
251# if defined(WSAENOTCONN)
415 case WSAENOTCONN: 252 case WSAENOTCONN:
253# endif
416# endif 254# endif
417#endif 255#endif
418 256
@@ -452,7 +290,7 @@ int err;
452 case EALREADY: 290 case EALREADY:
453#endif 291#endif
454 return(1); 292 return(1);
455 break; 293 /* break; */
456 default: 294 default:
457 break; 295 break;
458 } 296 }