This commit was manufactured by cvs2svn to create branch
[oweals/openssl.git] / apps / s_server.c
1 /* apps/s_server.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <assert.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #ifdef NO_STDIO
66 #define APPS_WIN16
67 #endif
68
69 /* With IPv6, it looks like Digital has mixed up the proper order of
70    recursive header file inclusion, resulting in the compiler complaining
71    that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
72    is needed to have fileno() declared correctly...  So let's define u_int */
73 #if defined(VMS) && defined(__DECC) && !defined(__U_INT)
74 #define __U_INT
75 typedef unsigned int u_int;
76 #endif
77
78 #include <openssl/lhash.h>
79 #include <openssl/bn.h>
80 #define USE_SOCKETS
81 #include "apps.h"
82 #include <openssl/err.h>
83 #include <openssl/pem.h>
84 #include <openssl/x509.h>
85 #include <openssl/ssl.h>
86 #include "s_apps.h"
87
88 #ifdef WINDOWS
89 #include <conio.h>
90 #endif
91
92 #if (defined(VMS) && __VMS_VER < 70000000)
93 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
94 #undef FIONBIO
95 #endif
96
97 #ifndef NO_RSA
98 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
99 #endif
100 static int sv_body(char *hostname, int s, unsigned char *context);
101 static int www_body(char *hostname, int s, unsigned char *context);
102 static void close_accept_socket(void );
103 static void sv_usage(void);
104 static int init_ssl_connection(SSL *s);
105 static void print_stats(BIO *bp,SSL_CTX *ctx);
106 #ifndef NO_DH
107 static DH *load_dh_param(char *dhfile);
108 static DH *get_dh512(void);
109 #endif
110 #ifdef MONOLITH
111 static void s_server_init(void);
112 #endif
113
114 #ifndef S_ISDIR
115 # if defined(_S_IFMT) && defined(_S_IFDIR)
116 #  define S_ISDIR(a)    (((a) & _S_IFMT) == _S_IFDIR)
117 # else
118 #  define S_ISDIR(a)    (((a) & S_IFMT) == S_IFDIR)
119 # endif
120 #endif
121
122 #ifndef NO_DH
123 static unsigned char dh512_p[]={
124         0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
125         0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
126         0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
127         0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
128         0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
129         0x47,0x74,0xE8,0x33,
130         };
131 static unsigned char dh512_g[]={
132         0x02,
133         };
134
135 static DH *get_dh512(void)
136         {
137         DH *dh=NULL;
138
139         if ((dh=DH_new()) == NULL) return(NULL);
140         dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
141         dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
142         if ((dh->p == NULL) || (dh->g == NULL))
143                 return(NULL);
144         return(dh);
145         }
146 #endif
147
148 /* static int load_CA(SSL_CTX *ctx, char *file);*/
149
150 #undef BUFSIZZ
151 #define BUFSIZZ 16*1024
152 static int bufsize=BUFSIZZ;
153 static int accept_socket= -1;
154
155 #define TEST_CERT       "server.pem"
156 #undef PROG
157 #define PROG            s_server_main
158
159 extern int verify_depth;
160
161 static char *cipher=NULL;
162 static int s_server_verify=SSL_VERIFY_NONE;
163 static int s_server_session_id_context = 1; /* anything will do */
164 static char *s_cert_file=TEST_CERT,*s_key_file=NULL;
165 static char *s_dcert_file=NULL,*s_dkey_file=NULL;
166 #ifdef FIONBIO
167 static int s_nbio=0;
168 #endif
169 static int s_nbio_test=0;
170 int s_crlf=0;
171 static SSL_CTX *ctx=NULL;
172 static int www=0;
173
174 static BIO *bio_s_out=NULL;
175 static int s_debug=0;
176 static int s_quiet=0;
177
178 static int hack=0;
179
180 #ifdef MONOLITH
181 static void s_server_init(void)
182         {
183         accept_socket=-1;
184         cipher=NULL;
185         s_server_verify=SSL_VERIFY_NONE;
186         s_dcert_file=NULL;
187         s_dkey_file=NULL;
188         s_cert_file=TEST_CERT;
189         s_key_file=NULL;
190 #ifdef FIONBIO
191         s_nbio=0;
192 #endif
193         s_nbio_test=0;
194         ctx=NULL;
195         www=0;
196
197         bio_s_out=NULL;
198         s_debug=0;
199         s_quiet=0;
200         hack=0;
201         }
202 #endif
203
204 static void sv_usage(void)
205         {
206         BIO_printf(bio_err,"usage: s_server [args ...]\n");
207         BIO_printf(bio_err,"\n");
208         BIO_printf(bio_err," -accept arg   - port to accept on (default is %d)\n",PORT);
209         BIO_printf(bio_err," -context arg  - set session ID context\n");
210         BIO_printf(bio_err," -verify arg   - turn on peer certificate verification\n");
211         BIO_printf(bio_err," -Verify arg   - turn on peer certificate verification, must have a cert.\n");
212         BIO_printf(bio_err," -cert arg     - certificate file to use, PEM format assumed\n");
213         BIO_printf(bio_err,"                 (default is %s)\n",TEST_CERT);
214         BIO_printf(bio_err," -key arg      - Private Key file to use, PEM format assumed, in cert file if\n");
215         BIO_printf(bio_err,"                 not specified (default is %s)\n",TEST_CERT);
216         BIO_printf(bio_err," -dcert arg    - second certificate file to use (usually for DSA)\n");
217         BIO_printf(bio_err," -dkey arg     - second private key file to use (usually for DSA)\n");
218         BIO_printf(bio_err," -dhparam arg  - DH parameter file to use, in cert file if not specified\n");
219         BIO_printf(bio_err,"                 or a default set of parameters is used\n");
220 #ifdef FIONBIO
221         BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
222 #endif
223         BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
224         BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
225         BIO_printf(bio_err," -debug        - Print more output\n");
226         BIO_printf(bio_err," -state        - Print the SSL states\n");
227         BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
228         BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
229         BIO_printf(bio_err," -nocert       - Don't use any certificates (Anon-DH)\n");
230         BIO_printf(bio_err," -cipher arg   - play with 'openssl ciphers' to see what goes here\n");
231         BIO_printf(bio_err," -quiet        - No server output\n");
232         BIO_printf(bio_err," -no_tmp_rsa   - Do not generate a tmp RSA key\n");
233         BIO_printf(bio_err," -ssl2         - Just talk SSLv2\n");
234         BIO_printf(bio_err," -ssl3         - Just talk SSLv3\n");
235         BIO_printf(bio_err," -tls1         - Just talk TLSv1\n");
236         BIO_printf(bio_err," -no_ssl2      - Just disable SSLv2\n");
237         BIO_printf(bio_err," -no_ssl3      - Just disable SSLv3\n");
238         BIO_printf(bio_err," -no_tls1      - Just disable TLSv1\n");
239 #ifndef NO_DH
240         BIO_printf(bio_err," -no_dhe       - Disable ephemeral DH\n");
241 #endif
242         BIO_printf(bio_err," -bugs         - Turn on SSL bug compatibility\n");
243         BIO_printf(bio_err," -www          - Respond to a 'GET /' with a status page\n");
244         BIO_printf(bio_err," -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
245         BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
246         }
247
248 static int local_argc=0;
249 static char **local_argv;
250
251 #ifdef CHARSET_EBCDIC
252 static int ebcdic_new(BIO *bi);
253 static int ebcdic_free(BIO *a);
254 static int ebcdic_read(BIO *b, char *out, int outl);
255 static int ebcdic_write(BIO *b, char *in, int inl);
256 static long ebcdic_ctrl(BIO *b, int cmd, long num, char *ptr);
257 static int ebcdic_gets(BIO *bp, char *buf, int size);
258 static int ebcdic_puts(BIO *bp, char *str);
259
260 #define BIO_TYPE_EBCDIC_FILTER  (18|0x0200)
261 static BIO_METHOD methods_ebcdic=
262         {
263         BIO_TYPE_EBCDIC_FILTER,
264         "EBCDIC/ASCII filter",
265         ebcdic_write,
266         ebcdic_read,
267         ebcdic_puts,
268         ebcdic_gets,
269         ebcdic_ctrl,
270         ebcdic_new,
271         ebcdic_free,
272         };
273
274 typedef struct
275 {
276         size_t  alloced;
277         char    buff[1];
278 } EBCDIC_OUTBUFF;
279
280 BIO_METHOD *BIO_f_ebcdic_filter()
281 {
282         return(&methods_ebcdic);
283 }
284
285 static int ebcdic_new(BIO *bi)
286 {
287         EBCDIC_OUTBUFF *wbuf;
288
289         wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
290         wbuf->alloced = 1024;
291         wbuf->buff[0] = '\0';
292
293         bi->ptr=(char *)wbuf;
294         bi->init=1;
295         bi->flags=0;
296         return(1);
297 }
298
299 static int ebcdic_free(BIO *a)
300 {
301         if (a == NULL) return(0);
302         if (a->ptr != NULL)
303                 OPENSSL_free(a->ptr);
304         a->ptr=NULL;
305         a->init=0;
306         a->flags=0;
307         return(1);
308 }
309         
310 static int ebcdic_read(BIO *b, char *out, int outl)
311 {
312         int ret=0;
313
314         if (out == NULL || outl == 0) return(0);
315         if (b->next_bio == NULL) return(0);
316
317         ret=BIO_read(b->next_bio,out,outl);
318         if (ret > 0)
319                 ascii2ebcdic(out,out,ret);
320         return(ret);
321 }
322
323 static int ebcdic_write(BIO *b, char *in, int inl)
324 {
325         EBCDIC_OUTBUFF *wbuf;
326         int ret=0;
327         int num;
328         unsigned char n;
329
330         if ((in == NULL) || (inl <= 0)) return(0);
331         if (b->next_bio == NULL) return(0);
332
333         wbuf=(EBCDIC_OUTBUFF *)b->ptr;
334
335         if (inl > (num = wbuf->alloced))
336         {
337                 num = num + num;  /* double the size */
338                 if (num < inl)
339                         num = inl;
340                 OPENSSL_free(wbuf);
341                 wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
342
343                 wbuf->alloced = num;
344                 wbuf->buff[0] = '\0';
345
346                 b->ptr=(char *)wbuf;
347         }
348
349         ebcdic2ascii(wbuf->buff, in, inl);
350
351         ret=BIO_write(b->next_bio, wbuf->buff, inl);
352
353         return(ret);
354 }
355
356 static long ebcdic_ctrl(BIO *b, int cmd, long num, char *ptr)
357 {
358         long ret;
359
360         if (b->next_bio == NULL) return(0);
361         switch (cmd)
362         {
363         case BIO_CTRL_DUP:
364                 ret=0L;
365                 break;
366         default:
367                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
368                 break;
369         }
370         return(ret);
371 }
372
373 static int ebcdic_gets(BIO *bp, char *buf, int size)
374 {
375         int i, ret;
376         if (bp->next_bio == NULL) return(0);
377 /*      return(BIO_gets(bp->next_bio,buf,size));*/
378         for (i=0; i<size-1; ++i)
379         {
380                 ret = ebcdic_read(bp,&buf[i],1);
381                 if (ret <= 0)
382                         break;
383                 else if (buf[i] == '\n')
384                 {
385                         ++i;
386                         break;
387                 }
388         }
389         if (i < size)
390                 buf[i] = '\0';
391         return (ret < 0 && i == 0) ? ret : i;
392 }
393
394 static int ebcdic_puts(BIO *bp, char *str)
395 {
396         if (bp->next_bio == NULL) return(0);
397         return ebcdic_write(bp, str, strlen(str));
398 }
399 #endif
400
401 int MAIN(int, char **);
402
403 int MAIN(int argc, char *argv[])
404         {
405         short port=PORT;
406         char *CApath=NULL,*CAfile=NULL;
407         char *context = NULL;
408         char *dhfile = NULL;
409         int badop=0,bugs=0;
410         int ret=1;
411         int off=0;
412         int no_tmp_rsa=0,no_dhe=0,nocert=0;
413         int state=0;
414         SSL_METHOD *meth=NULL;
415         char *inrand=NULL;
416 #ifndef NO_DH
417         DH *dh=NULL;
418 #endif
419
420 #if !defined(NO_SSL2) && !defined(NO_SSL3)
421         meth=SSLv23_server_method();
422 #elif !defined(NO_SSL3)
423         meth=SSLv3_server_method();
424 #elif !defined(NO_SSL2)
425         meth=SSLv2_server_method();
426 #endif
427
428         local_argc=argc;
429         local_argv=argv;
430
431         apps_startup();
432 #ifdef MONOLITH
433         s_server_init();
434 #endif
435
436         if (bio_err == NULL)
437                 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
438
439         verify_depth=0;
440 #ifdef FIONBIO
441         s_nbio=0;
442 #endif
443         s_nbio_test=0;
444
445         argc--;
446         argv++;
447
448         while (argc >= 1)
449                 {
450                 if      ((strcmp(*argv,"-port") == 0) ||
451                          (strcmp(*argv,"-accept") == 0))
452                         {
453                         if (--argc < 1) goto bad;
454                         if (!extract_port(*(++argv),&port))
455                                 goto bad;
456                         }
457                 else if (strcmp(*argv,"-verify") == 0)
458                         {
459                         s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
460                         if (--argc < 1) goto bad;
461                         verify_depth=atoi(*(++argv));
462                         BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
463                         }
464                 else if (strcmp(*argv,"-Verify") == 0)
465                         {
466                         s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
467                                 SSL_VERIFY_CLIENT_ONCE;
468                         if (--argc < 1) goto bad;
469                         verify_depth=atoi(*(++argv));
470                         BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
471                         }
472                 else if (strcmp(*argv,"-context") == 0)
473                         {
474                         if (--argc < 1) goto bad;
475                         context= *(++argv);
476                         }
477                 else if (strcmp(*argv,"-cert") == 0)
478                         {
479                         if (--argc < 1) goto bad;
480                         s_cert_file= *(++argv);
481                         }
482                 else if (strcmp(*argv,"-key") == 0)
483                         {
484                         if (--argc < 1) goto bad;
485                         s_key_file= *(++argv);
486                         }
487                 else if (strcmp(*argv,"-dhparam") == 0)
488                         {
489                         if (--argc < 1) goto bad;
490                         dhfile = *(++argv);
491                         }
492                 else if (strcmp(*argv,"-dcert") == 0)
493                         {
494                         if (--argc < 1) goto bad;
495                         s_dcert_file= *(++argv);
496                         }
497                 else if (strcmp(*argv,"-dkey") == 0)
498                         {
499                         if (--argc < 1) goto bad;
500                         s_dkey_file= *(++argv);
501                         }
502                 else if (strcmp(*argv,"-nocert") == 0)
503                         {
504                         nocert=1;
505                         }
506                 else if (strcmp(*argv,"-CApath") == 0)
507                         {
508                         if (--argc < 1) goto bad;
509                         CApath= *(++argv);
510                         }
511                 else if (strcmp(*argv,"-cipher") == 0)
512                         {
513                         if (--argc < 1) goto bad;
514                         cipher= *(++argv);
515                         }
516                 else if (strcmp(*argv,"-CAfile") == 0)
517                         {
518                         if (--argc < 1) goto bad;
519                         CAfile= *(++argv);
520                         }
521 #ifdef FIONBIO  
522                 else if (strcmp(*argv,"-nbio") == 0)
523                         { s_nbio=1; }
524 #endif
525                 else if (strcmp(*argv,"-nbio_test") == 0)
526                         {
527 #ifdef FIONBIO  
528                         s_nbio=1;
529 #endif
530                         s_nbio_test=1;
531                         }
532                 else if (strcmp(*argv,"-debug") == 0)
533                         { s_debug=1; }
534                 else if (strcmp(*argv,"-hack") == 0)
535                         { hack=1; }
536                 else if (strcmp(*argv,"-state") == 0)
537                         { state=1; }
538                 else if (strcmp(*argv,"-crlf") == 0)
539                         { s_crlf=1; }
540                 else if (strcmp(*argv,"-quiet") == 0)
541                         { s_quiet=1; }
542                 else if (strcmp(*argv,"-bugs") == 0)
543                         { bugs=1; }
544                 else if (strcmp(*argv,"-no_tmp_rsa") == 0)
545                         { no_tmp_rsa=1; }
546                 else if (strcmp(*argv,"-no_dhe") == 0)
547                         { no_dhe=1; }
548                 else if (strcmp(*argv,"-www") == 0)
549                         { www=1; }
550                 else if (strcmp(*argv,"-WWW") == 0)
551                         { www=2; }
552                 else if (strcmp(*argv,"-no_ssl2") == 0)
553                         { off|=SSL_OP_NO_SSLv2; }
554                 else if (strcmp(*argv,"-no_ssl3") == 0)
555                         { off|=SSL_OP_NO_SSLv3; }
556                 else if (strcmp(*argv,"-no_tls1") == 0)
557                         { off|=SSL_OP_NO_TLSv1; }
558 #ifndef NO_SSL2
559                 else if (strcmp(*argv,"-ssl2") == 0)
560                         { meth=SSLv2_server_method(); }
561 #endif
562 #ifndef NO_SSL3
563                 else if (strcmp(*argv,"-ssl3") == 0)
564                         { meth=SSLv3_server_method(); }
565 #endif
566 #ifndef NO_TLS1
567                 else if (strcmp(*argv,"-tls1") == 0)
568                         { meth=TLSv1_server_method(); }
569 #endif
570                 else if (strcmp(*argv,"-rand") == 0)
571                         {
572                         if (--argc < 1) goto bad;
573                         inrand= *(++argv);
574                         }
575                 else
576                         {
577                         BIO_printf(bio_err,"unknown option %s\n",*argv);
578                         badop=1;
579                         break;
580                         }
581                 argc--;
582                 argv++;
583                 }
584         if (badop)
585                 {
586 bad:
587                 sv_usage();
588                 goto end;
589                 }
590
591         if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
592                 && !RAND_status())
593                 {
594                 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
595                 }
596         if (inrand != NULL)
597                 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
598                         app_RAND_load_files(inrand));
599
600         if (bio_s_out == NULL)
601                 {
602                 if (s_quiet && !s_debug)
603                         {
604                         bio_s_out=BIO_new(BIO_s_null());
605                         }
606                 else
607                         {
608                         if (bio_s_out == NULL)
609                                 bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
610                         }
611                 }
612
613 #if !defined(NO_RSA) || !defined(NO_DSA)
614         if (nocert)
615 #endif
616                 {
617                 s_cert_file=NULL;
618                 s_key_file=NULL;
619                 s_dcert_file=NULL;
620                 s_dkey_file=NULL;
621                 }
622
623         SSL_load_error_strings();
624         OpenSSL_add_ssl_algorithms();
625
626         ctx=SSL_CTX_new(meth);
627         if (ctx == NULL)
628                 {
629                 ERR_print_errors(bio_err);
630                 goto end;
631                 }
632
633         SSL_CTX_set_quiet_shutdown(ctx,1);
634         if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
635         if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
636         SSL_CTX_set_options(ctx,off);
637         if (hack) SSL_CTX_set_options(ctx,SSL_OP_NON_EXPORT_FIRST);
638
639         if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
640
641         SSL_CTX_sess_set_cache_size(ctx,128);
642
643 #if 0
644         if (cipher == NULL) cipher=getenv("SSL_CIPHER");
645 #endif
646
647 #if 0
648         if (s_cert_file == NULL)
649                 {
650                 BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
651                 goto end;
652                 }
653 #endif
654
655         if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
656                 (!SSL_CTX_set_default_verify_paths(ctx)))
657                 {
658                 /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
659                 ERR_print_errors(bio_err);
660                 /* goto end; */
661                 }
662
663 #ifndef NO_DH
664         if (!no_dhe)
665                 {
666                 dh=load_dh_param(dhfile ? dhfile : s_cert_file);
667                 if (dh != NULL)
668                         {
669                         BIO_printf(bio_s_out,"Setting temp DH parameters\n");
670                         }
671                 else
672                         {
673                         BIO_printf(bio_s_out,"Using default temp DH parameters\n");
674                         dh=get_dh512();
675                         }
676                 (void)BIO_flush(bio_s_out);
677
678                 SSL_CTX_set_tmp_dh(ctx,dh);
679                 DH_free(dh);
680                 }
681 #endif
682         
683         if (!set_cert_stuff(ctx,s_cert_file,s_key_file))
684                 goto end;
685         if (s_dcert_file != NULL)
686                 {
687                 if (!set_cert_stuff(ctx,s_dcert_file,s_dkey_file))
688                         goto end;
689                 }
690
691 #ifndef NO_RSA
692 #if 1
693         SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
694 #else
695         if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
696                 {
697                 RSA *rsa;
698
699                 BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
700                 BIO_flush(bio_s_out);
701
702                 rsa=RSA_generate_key(512,RSA_F4,NULL);
703
704                 if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
705                         {
706                         ERR_print_errors(bio_err);
707                         goto end;
708                         }
709                 RSA_free(rsa);
710                 BIO_printf(bio_s_out,"\n");
711                 }
712 #endif
713 #endif
714
715         if (cipher != NULL)
716                 if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
717                 BIO_printf(bio_err,"error setting cipher list\n");
718                 ERR_print_errors(bio_err);
719                 goto end;
720         }
721         SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
722         SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
723                 sizeof s_server_session_id_context);
724
725         if (CAfile != NULL)
726             SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
727
728         BIO_printf(bio_s_out,"ACCEPT\n");
729         if (www)
730                 do_server(port,&accept_socket,www_body, context);
731         else
732                 do_server(port,&accept_socket,sv_body, context);
733         print_stats(bio_s_out,ctx);
734         ret=0;
735 end:
736         if (ctx != NULL) SSL_CTX_free(ctx);
737         if (bio_s_out != NULL)
738                 {
739                 BIO_free(bio_s_out);
740                 bio_s_out=NULL;
741                 }
742         EXIT(ret);
743         }
744
745 static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
746         {
747         BIO_printf(bio,"%4ld items in the session cache\n",
748                 SSL_CTX_sess_number(ssl_ctx));
749         BIO_printf(bio,"%4d client connects (SSL_connect())\n",
750                 SSL_CTX_sess_connect(ssl_ctx));
751         BIO_printf(bio,"%4d client renegotiates (SSL_connect())\n",
752                 SSL_CTX_sess_connect_renegotiate(ssl_ctx));
753         BIO_printf(bio,"%4d client connects that finished\n",
754                 SSL_CTX_sess_connect_good(ssl_ctx));
755         BIO_printf(bio,"%4d server accepts (SSL_accept())\n",
756                 SSL_CTX_sess_accept(ssl_ctx));
757         BIO_printf(bio,"%4d server renegotiates (SSL_accept())\n",
758                 SSL_CTX_sess_accept_renegotiate(ssl_ctx));
759         BIO_printf(bio,"%4d server accepts that finished\n",
760                 SSL_CTX_sess_accept_good(ssl_ctx));
761         BIO_printf(bio,"%4d session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
762         BIO_printf(bio,"%4d session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
763         BIO_printf(bio,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
764         BIO_printf(bio,"%4d callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
765         BIO_printf(bio,"%4d cache full overflows (%d allowed)\n",
766                 SSL_CTX_sess_cache_full(ssl_ctx),
767                 SSL_CTX_sess_get_cache_size(ssl_ctx));
768         }
769
770 static int sv_body(char *hostname, int s, unsigned char *context)
771         {
772         char *buf=NULL;
773         fd_set readfds;
774         int ret=1,width;
775         int k,i;
776         unsigned long l;
777         SSL *con=NULL;
778         BIO *sbio;
779 #ifdef WINDOWS
780         struct timeval tv;
781 #endif
782
783         if ((buf=OPENSSL_malloc(bufsize)) == NULL)
784                 {
785                 BIO_printf(bio_err,"out of memory\n");
786                 goto err;
787                 }
788 #ifdef FIONBIO  
789         if (s_nbio)
790                 {
791                 unsigned long sl=1;
792
793                 if (!s_quiet)
794                         BIO_printf(bio_err,"turning on non blocking io\n");
795                 if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
796                         ERR_print_errors(bio_err);
797                 }
798 #endif
799
800         if (con == NULL) {
801                 con=SSL_new(ctx);
802                 if(context)
803                       SSL_set_session_id_context(con, context,
804                                                  strlen((char *)context));
805         }
806         SSL_clear(con);
807
808         sbio=BIO_new_socket(s,BIO_NOCLOSE);
809         if (s_nbio_test)
810                 {
811                 BIO *test;
812
813                 test=BIO_new(BIO_f_nbio_test());
814                 sbio=BIO_push(test,sbio);
815                 }
816         SSL_set_bio(con,sbio,sbio);
817         SSL_set_accept_state(con);
818         /* SSL_set_fd(con,s); */
819
820         if (s_debug)
821                 {
822                 con->debug=1;
823                 BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
824                 BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
825                 }
826
827         width=s+1;
828         for (;;)
829                 {
830                 int read_from_terminal;
831                 int read_from_sslcon;
832
833                 read_from_terminal = 0;
834                 read_from_sslcon = SSL_pending(con);
835
836                 if (!read_from_sslcon)
837                         {
838                         FD_ZERO(&readfds);
839 #ifndef WINDOWS
840                         FD_SET(fileno(stdin),&readfds);
841 #endif
842                         FD_SET(s,&readfds);
843                         /* Note: under VMS with SOCKETSHR the second parameter is
844                          * currently of type (int *) whereas under other systems
845                          * it is (void *) if you don't have a cast it will choke
846                          * the compiler: if you do have a cast then you can either
847                          * go for (int *) or (void *).
848                          */
849 #ifdef WINDOWS
850                         /* Under Windows we can't select on stdin: only
851                          * on sockets. As a workaround we timeout the select every
852                          * second and check for any keypress. In a proper Windows
853                          * application we wouldn't do this because it is inefficient.
854                          */
855                         tv.tv_sec = 1;
856                         tv.tv_usec = 0;
857                         i=select(width,(void *)&readfds,NULL,NULL,&tv);
858                         if((i < 0) || (!i && !_kbhit() ) )continue;
859                         if(_kbhit())
860                                 read_from_terminal = 1;
861 #else
862                         i=select(width,(void *)&readfds,NULL,NULL,NULL);
863                         if (i <= 0) continue;
864                         if (FD_ISSET(fileno(stdin),&readfds))
865                                 read_from_terminal = 1;
866 #endif
867                         if (FD_ISSET(s,&readfds))
868                                 read_from_sslcon = 1;
869                         }
870                 if (read_from_terminal)
871                         {
872                         if (s_crlf)
873                                 {
874                                 int j, lf_num;
875
876                                 i=read(fileno(stdin), buf, bufsize/2);
877                                 lf_num = 0;
878                                 /* both loops are skipped when i <= 0 */
879                                 for (j = 0; j < i; j++)
880                                         if (buf[j] == '\n')
881                                                 lf_num++;
882                                 for (j = i-1; j >= 0; j--)
883                                         {
884                                         buf[j+lf_num] = buf[j];
885                                         if (buf[j] == '\n')
886                                                 {
887                                                 lf_num--;
888                                                 i++;
889                                                 buf[j+lf_num] = '\r';
890                                                 }
891                                         }
892                                 assert(lf_num == 0);
893                                 }
894                         else
895                                 i=read(fileno(stdin),buf,bufsize);
896                         if (!s_quiet)
897                                 {
898                                 if ((i <= 0) || (buf[0] == 'Q'))
899                                         {
900                                         BIO_printf(bio_s_out,"DONE\n");
901                                         SHUTDOWN(s);
902                                         close_accept_socket();
903                                         ret= -11;
904                                         goto err;
905                                         }
906                                 if ((i <= 0) || (buf[0] == 'q'))
907                                         {
908                                         BIO_printf(bio_s_out,"DONE\n");
909                                         SHUTDOWN(s);
910         /*                              close_accept_socket();
911                                         ret= -11;*/
912                                         goto err;
913                                         }
914                                 if ((buf[0] == 'r') && 
915                                         ((buf[1] == '\n') || (buf[1] == '\r')))
916                                         {
917                                         SSL_renegotiate(con);
918                                         i=SSL_do_handshake(con);
919                                         printf("SSL_do_handshake -> %d\n",i);
920                                         i=0; /*13; */
921                                         continue;
922                                         /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
923                                         }
924                                 if ((buf[0] == 'R') &&
925                                         ((buf[1] == '\n') || (buf[1] == '\r')))
926                                         {
927                                         SSL_set_verify(con,
928                                                 SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
929                                         SSL_renegotiate(con);
930                                         i=SSL_do_handshake(con);
931                                         printf("SSL_do_handshake -> %d\n",i);
932                                         i=0; /* 13; */
933                                         continue;
934                                         /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
935                                         }
936                                 if (buf[0] == 'P')
937                                         {
938                                         static char *str="Lets print some clear text\n";
939                                         BIO_write(SSL_get_wbio(con),str,strlen(str));
940                                         }
941                                 if (buf[0] == 'S')
942                                         {
943                                         print_stats(bio_s_out,SSL_get_SSL_CTX(con));
944                                         }
945                                 }
946 #ifdef CHARSET_EBCDIC
947                         ebcdic2ascii(buf,buf,i);
948 #endif
949                         l=k=0;
950                         for (;;)
951                                 {
952                                 /* should do a select for the write */
953 #ifdef RENEG
954 { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
955 #endif
956                                 k=SSL_write(con,&(buf[l]),(unsigned int)i);
957                                 switch (SSL_get_error(con,k))
958                                         {
959                                 case SSL_ERROR_NONE:
960                                         break;
961                                 case SSL_ERROR_WANT_WRITE:
962                                 case SSL_ERROR_WANT_READ:
963                                 case SSL_ERROR_WANT_X509_LOOKUP:
964                                         BIO_printf(bio_s_out,"Write BLOCK\n");
965                                         break;
966                                 case SSL_ERROR_SYSCALL:
967                                 case SSL_ERROR_SSL:
968                                         BIO_printf(bio_s_out,"ERROR\n");
969                                         ERR_print_errors(bio_err);
970                                         ret=1;
971                                         goto err;
972                                         /* break; */
973                                 case SSL_ERROR_ZERO_RETURN:
974                                         BIO_printf(bio_s_out,"DONE\n");
975                                         ret=1;
976                                         goto err;
977                                         }
978                                 l+=k;
979                                 i-=k;
980                                 if (i <= 0) break;
981                                 }
982                         }
983                 if (read_from_sslcon)
984                         {
985                         if (!SSL_is_init_finished(con))
986                                 {
987                                 i=init_ssl_connection(con);
988                                 
989                                 if (i < 0)
990                                         {
991                                         ret=0;
992                                         goto err;
993                                         }
994                                 else if (i == 0)
995                                         {
996                                         ret=1;
997                                         goto err;
998                                         }
999                                 }
1000                         else
1001                                 {
1002 again:  
1003                                 i=SSL_read(con,(char *)buf,bufsize);
1004                                 switch (SSL_get_error(con,i))
1005                                         {
1006                                 case SSL_ERROR_NONE:
1007 #ifdef CHARSET_EBCDIC
1008                                         ascii2ebcdic(buf,buf,i);
1009 #endif
1010                                         write(fileno(stdout),buf,
1011                                                 (unsigned int)i);
1012                                         if (SSL_pending(con)) goto again;
1013                                         break;
1014                                 case SSL_ERROR_WANT_WRITE:
1015                                 case SSL_ERROR_WANT_READ:
1016                                 case SSL_ERROR_WANT_X509_LOOKUP:
1017                                         BIO_printf(bio_s_out,"Read BLOCK\n");
1018                                         break;
1019                                 case SSL_ERROR_SYSCALL:
1020                                 case SSL_ERROR_SSL:
1021                                         BIO_printf(bio_s_out,"ERROR\n");
1022                                         ERR_print_errors(bio_err);
1023                                         ret=1;
1024                                         goto err;
1025                                 case SSL_ERROR_ZERO_RETURN:
1026                                         BIO_printf(bio_s_out,"DONE\n");
1027                                         ret=1;
1028                                         goto err;
1029                                         }
1030                                 }
1031                         }
1032                 }
1033 err:
1034         BIO_printf(bio_s_out,"shutting down SSL\n");
1035 #if 1
1036         SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1037 #else
1038         SSL_shutdown(con);
1039 #endif
1040         if (con != NULL) SSL_free(con);
1041         BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
1042         if (buf != NULL)
1043                 {
1044                 memset(buf,0,bufsize);
1045                 OPENSSL_free(buf);
1046                 }
1047         if (ret >= 0)
1048                 BIO_printf(bio_s_out,"ACCEPT\n");
1049         return(ret);
1050         }
1051
1052 static void close_accept_socket(void)
1053         {
1054         BIO_printf(bio_err,"shutdown accept socket\n");
1055         if (accept_socket >= 0)
1056                 {
1057                 SHUTDOWN2(accept_socket);
1058                 }
1059         }
1060
1061 static int init_ssl_connection(SSL *con)
1062         {
1063         int i;
1064         const char *str;
1065         X509 *peer;
1066         long verify_error;
1067         MS_STATIC char buf[BUFSIZ];
1068
1069         if ((i=SSL_accept(con)) <= 0)
1070                 {
1071                 if (BIO_sock_should_retry(i))
1072                         {
1073                         BIO_printf(bio_s_out,"DELAY\n");
1074                         return(1);
1075                         }
1076
1077                 BIO_printf(bio_err,"ERROR\n");
1078                 verify_error=SSL_get_verify_result(con);
1079                 if (verify_error != X509_V_OK)
1080                         {
1081                         BIO_printf(bio_err,"verify error:%s\n",
1082                                 X509_verify_cert_error_string(verify_error));
1083                         }
1084                 else
1085                         ERR_print_errors(bio_err);
1086                 return(0);
1087                 }
1088
1089         PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
1090
1091         peer=SSL_get_peer_certificate(con);
1092         if (peer != NULL)
1093                 {
1094                 BIO_printf(bio_s_out,"Client certificate\n");
1095                 PEM_write_bio_X509(bio_s_out,peer);
1096                 X509_NAME_oneline(X509_get_subject_name(peer),buf,BUFSIZ);
1097                 BIO_printf(bio_s_out,"subject=%s\n",buf);
1098                 X509_NAME_oneline(X509_get_issuer_name(peer),buf,BUFSIZ);
1099                 BIO_printf(bio_s_out,"issuer=%s\n",buf);
1100                 X509_free(peer);
1101                 }
1102
1103         if (SSL_get_shared_ciphers(con,buf,BUFSIZ) != NULL)
1104                 BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
1105         str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
1106         BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
1107         if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
1108         if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
1109                 TLS1_FLAGS_TLS_PADDING_BUG)
1110                 BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
1111
1112         return(1);
1113         }
1114
1115 #ifndef NO_DH
1116 static DH *load_dh_param(char *dhfile)
1117         {
1118         DH *ret=NULL;
1119         BIO *bio;
1120
1121         if ((bio=BIO_new_file(dhfile,"r")) == NULL)
1122                 goto err;
1123         ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
1124 err:
1125         if (bio != NULL) BIO_free(bio);
1126         return(ret);
1127         }
1128 #endif
1129
1130 #if 0
1131 static int load_CA(SSL_CTX *ctx, char *file)
1132         {
1133         FILE *in;
1134         X509 *x=NULL;
1135
1136         if ((in=fopen(file,"r")) == NULL)
1137                 return(0);
1138
1139         for (;;)
1140                 {
1141                 if (PEM_read_X509(in,&x,NULL) == NULL)
1142                         break;
1143                 SSL_CTX_add_client_CA(ctx,x);
1144                 }
1145         if (x != NULL) X509_free(x);
1146         fclose(in);
1147         return(1);
1148         }
1149 #endif
1150
1151 static int www_body(char *hostname, int s, unsigned char *context)
1152         {
1153         char *buf=NULL;
1154         int ret=1;
1155         int i,j,k,blank,dot;
1156         struct stat st_buf;
1157         SSL *con;
1158         SSL_CIPHER *c;
1159         BIO *io,*ssl_bio,*sbio;
1160         long total_bytes;
1161
1162         buf=OPENSSL_malloc(bufsize);
1163         if (buf == NULL) return(0);
1164         io=BIO_new(BIO_f_buffer());
1165         ssl_bio=BIO_new(BIO_f_ssl());
1166         if ((io == NULL) || (ssl_bio == NULL)) goto err;
1167
1168 #ifdef FIONBIO  
1169         if (s_nbio)
1170                 {
1171                 unsigned long sl=1;
1172
1173                 if (!s_quiet)
1174                         BIO_printf(bio_err,"turning on non blocking io\n");
1175                 if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
1176                         ERR_print_errors(bio_err);
1177                 }
1178 #endif
1179
1180         /* lets make the output buffer a reasonable size */
1181         if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
1182
1183         if ((con=SSL_new(ctx)) == NULL) goto err;
1184         if(context) SSL_set_session_id_context(con, context,
1185                                                strlen((char *)context));
1186
1187         sbio=BIO_new_socket(s,BIO_NOCLOSE);
1188         if (s_nbio_test)
1189                 {
1190                 BIO *test;
1191
1192                 test=BIO_new(BIO_f_nbio_test());
1193                 sbio=BIO_push(test,sbio);
1194                 }
1195         SSL_set_bio(con,sbio,sbio);
1196         SSL_set_accept_state(con);
1197
1198         /* SSL_set_fd(con,s); */
1199         BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
1200         BIO_push(io,ssl_bio);
1201 #ifdef CHARSET_EBCDIC
1202         io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
1203 #endif
1204
1205         if (s_debug)
1206                 {
1207                 con->debug=1;
1208                 BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
1209                 BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
1210                 }
1211
1212         blank=0;
1213         for (;;)
1214                 {
1215                 if (hack)
1216                         {
1217                         i=SSL_accept(con);
1218
1219                         switch (SSL_get_error(con,i))
1220                                 {
1221                         case SSL_ERROR_NONE:
1222                                 break;
1223                         case SSL_ERROR_WANT_WRITE:
1224                         case SSL_ERROR_WANT_READ:
1225                         case SSL_ERROR_WANT_X509_LOOKUP:
1226                                 continue;
1227                         case SSL_ERROR_SYSCALL:
1228                         case SSL_ERROR_SSL:
1229                         case SSL_ERROR_ZERO_RETURN:
1230                                 ret=1;
1231                                 goto err;
1232                                 /* break; */
1233                                 }
1234
1235                         SSL_renegotiate(con);
1236                         SSL_write(con,NULL,0);
1237                         }
1238
1239                 i=BIO_gets(io,buf,bufsize-1);
1240                 if (i < 0) /* error */
1241                         {
1242                         if (!BIO_should_retry(io))
1243                                 {
1244                                 if (!s_quiet)
1245                                         ERR_print_errors(bio_err);
1246                                 goto err;
1247                                 }
1248                         else
1249                                 {
1250                                 BIO_printf(bio_s_out,"read R BLOCK\n");
1251 #ifndef MSDOS
1252                                 sleep(1);
1253 #endif
1254                                 continue;
1255                                 }
1256                         }
1257                 else if (i == 0) /* end of input */
1258                         {
1259                         ret=1;
1260                         goto end;
1261                         }
1262
1263                 /* else we have data */
1264                 if (    ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
1265                         ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
1266                         {
1267                         char *p;
1268                         X509 *peer;
1269                         STACK_OF(SSL_CIPHER) *sk;
1270                         static char *space="                          ";
1271
1272                         BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1273                         BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
1274                         BIO_puts(io,"<pre>\n");
1275 /*                      BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
1276                         BIO_puts(io,"\n");
1277                         for (i=0; i<local_argc; i++)
1278                                 {
1279                                 BIO_puts(io,local_argv[i]);
1280                                 BIO_write(io," ",1);
1281                                 }
1282                         BIO_puts(io,"\n");
1283
1284                         /* The following is evil and should not really
1285                          * be done */
1286                         BIO_printf(io,"Ciphers supported in s_server binary\n");
1287                         sk=SSL_get_ciphers(con);
1288                         j=sk_SSL_CIPHER_num(sk);
1289                         for (i=0; i<j; i++)
1290                                 {
1291                                 c=sk_SSL_CIPHER_value(sk,i);
1292                                 BIO_printf(io,"%-11s:%-25s",
1293                                         SSL_CIPHER_get_version(c),
1294                                         SSL_CIPHER_get_name(c));
1295                                 if ((((i+1)%2) == 0) && (i+1 != j))
1296                                         BIO_puts(io,"\n");
1297                                 }
1298                         BIO_puts(io,"\n");
1299                         p=SSL_get_shared_ciphers(con,buf,bufsize);
1300                         if (p != NULL)
1301                                 {
1302                                 BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
1303                                 j=i=0;
1304                                 while (*p)
1305                                         {
1306                                         if (*p == ':')
1307                                                 {
1308                                                 BIO_write(io,space,26-j);
1309                                                 i++;
1310                                                 j=0;
1311                                                 BIO_write(io,((i%3)?" ":"\n"),1);
1312                                                 }
1313                                         else
1314                                                 {
1315                                                 BIO_write(io,p,1);
1316                                                 j++;
1317                                                 }
1318                                         p++;
1319                                         }
1320                                 BIO_puts(io,"\n");
1321                                 }
1322                         BIO_printf(io,((con->hit)
1323                                 ?"---\nReused, "
1324                                 :"---\nNew, "));
1325                         c=SSL_get_current_cipher(con);
1326                         BIO_printf(io,"%s, Cipher is %s\n",
1327                                 SSL_CIPHER_get_version(c),
1328                                 SSL_CIPHER_get_name(c));
1329                         SSL_SESSION_print(io,SSL_get_session(con));
1330                         BIO_printf(io,"---\n");
1331                         print_stats(io,SSL_get_SSL_CTX(con));
1332                         BIO_printf(io,"---\n");
1333                         peer=SSL_get_peer_certificate(con);
1334                         if (peer != NULL)
1335                                 {
1336                                 BIO_printf(io,"Client certificate\n");
1337                                 X509_print(io,peer);
1338                                 PEM_write_bio_X509(io,peer);
1339                                 }
1340                         else
1341                                 BIO_puts(io,"no client certificate available\n");
1342                         BIO_puts(io,"</BODY></HTML>\r\n\r\n");
1343                         break;
1344                         }
1345                 else if ((www == 2) && (strncmp("GET /",buf,5) == 0))
1346                         {
1347                         BIO *file;
1348                         char *p,*e;
1349                         static char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
1350
1351                         /* skip the '/' */
1352                         p= &(buf[5]);
1353                         dot=0;
1354                         for (e=p; *e != '\0'; e++)
1355                                 {
1356                                 if (e[0] == ' ') break;
1357                                 if (    (e[0] == '.') &&
1358                                         (strncmp(&(e[-1]),"/../",4) == 0))
1359                                         dot=1;
1360                                 }
1361                         
1362
1363                         if (*e == '\0')
1364                                 {
1365                                 BIO_puts(io,text);
1366                                 BIO_printf(io,"'%s' is an invalid file name\r\n",p);
1367                                 break;
1368                                 }
1369                         *e='\0';
1370
1371                         if (dot)
1372                                 {
1373                                 BIO_puts(io,text);
1374                                 BIO_printf(io,"'%s' contains '..' reference\r\n",p);
1375                                 break;
1376                                 }
1377
1378                         if (*p == '/')
1379                                 {
1380                                 BIO_puts(io,text);
1381                                 BIO_printf(io,"'%s' is an invalid path\r\n",p);
1382                                 break;
1383                                 }
1384
1385                         /* append if a directory lookup */
1386                         if (e[-1] == '/')
1387                                 strcat(p,"index.html");
1388
1389                         /* if a directory, do the index thang */
1390                         if (stat(p,&st_buf) < 0)
1391                                 {
1392                                 BIO_puts(io,text);
1393                                 BIO_printf(io,"Error accessing '%s'\r\n",p);
1394                                 ERR_print_errors(io);
1395                                 break;
1396                                 }
1397                         if (S_ISDIR(st_buf.st_mode))
1398                                 {
1399                                 strcat(p,"/index.html");
1400                                 }
1401
1402                         if ((file=BIO_new_file(p,"r")) == NULL)
1403                                 {
1404                                 BIO_puts(io,text);
1405                                 BIO_printf(io,"Error opening '%s'\r\n",p);
1406                                 ERR_print_errors(io);
1407                                 break;
1408                                 }
1409
1410                         if (!s_quiet)
1411                                 BIO_printf(bio_err,"FILE:%s\n",p);
1412
1413                         i=strlen(p);
1414                         if (    ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
1415                                 ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
1416                                 ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
1417                                 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1418                         else
1419                                 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
1420                         /* send the file */
1421                         total_bytes=0;
1422                         for (;;)
1423                                 {
1424                                 i=BIO_read(file,buf,bufsize);
1425                                 if (i <= 0) break;
1426
1427 #ifdef RENEG
1428                                 total_bytes+=i;
1429                                 fprintf(stderr,"%d\n",i);
1430                                 if (total_bytes > 3*1024)
1431                                         {
1432                                         total_bytes=0;
1433                                         fprintf(stderr,"RENEGOTIATE\n");
1434                                         SSL_renegotiate(con);
1435                                         }
1436 #endif
1437
1438                                 for (j=0; j<i; )
1439                                         {
1440 #ifdef RENEG
1441 { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
1442 #endif
1443                                         k=BIO_write(io,&(buf[j]),i-j);
1444                                         if (k <= 0)
1445                                                 {
1446                                                 if (!BIO_should_retry(io))
1447                                                         goto write_error;
1448                                                 else
1449                                                         {
1450                                                         BIO_printf(bio_s_out,"rwrite W BLOCK\n");
1451                                                         }
1452                                                 }
1453                                         else
1454                                                 {
1455                                                 j+=k;
1456                                                 }
1457                                         }
1458                                 }
1459 write_error:
1460                         BIO_free(file);
1461                         break;
1462                         }
1463                 }
1464
1465         for (;;)
1466                 {
1467                 i=(int)BIO_flush(io);
1468                 if (i <= 0)
1469                         {
1470                         if (!BIO_should_retry(io))
1471                                 break;
1472                         }
1473                 else
1474                         break;
1475                 }
1476 end:
1477 #if 1
1478         /* make sure we re-use sessions */
1479         SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1480 #else
1481         /* This kills performance */
1482 /*      SSL_shutdown(con); A shutdown gets sent in the
1483  *      BIO_free_all(io) procession */
1484 #endif
1485
1486 err:
1487
1488         if (ret >= 0)
1489                 BIO_printf(bio_s_out,"ACCEPT\n");
1490
1491         if (buf != NULL) OPENSSL_free(buf);
1492         if (io != NULL) BIO_free_all(io);
1493 /*      if (ssl_bio != NULL) BIO_free(ssl_bio);*/
1494         return(ret);
1495         }
1496
1497 #ifndef NO_RSA
1498 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
1499         {
1500         static RSA *rsa_tmp=NULL;
1501
1502         if (rsa_tmp == NULL)
1503                 {
1504                 if (!s_quiet)
1505                         {
1506                         BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
1507                         (void)BIO_flush(bio_err);
1508                         }
1509                 rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
1510                 if (!s_quiet)
1511                         {
1512                         BIO_printf(bio_err,"\n");
1513                         (void)BIO_flush(bio_err);
1514                         }
1515                 }
1516         return(rsa_tmp);
1517         }
1518 #endif