summaryrefslogtreecommitdiff
path: root/src/lib/libssl/src/apps/s_server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/src/apps/s_server.c')
-rw-r--r--src/lib/libssl/src/apps/s_server.c803
1 files changed, 646 insertions, 157 deletions
diff --git a/src/lib/libssl/src/apps/s_server.c b/src/lib/libssl/src/apps/s_server.c
index 5012ef254d..78d90fad55 100644
--- a/src/lib/libssl/src/apps/s_server.c
+++ b/src/lib/libssl/src/apps/s_server.c
@@ -55,59 +55,128 @@
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58/* ====================================================================
59 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
58 111
112#include <assert.h>
59#include <stdio.h> 113#include <stdio.h>
60#include <stdlib.h> 114#include <stdlib.h>
61#include <string.h> 115#include <string.h>
62#include <sys/types.h> 116#include <sys/types.h>
63#include <sys/stat.h> 117#include <sys/stat.h>
64#ifdef NO_STDIO 118#include <openssl/e_os2.h>
119#ifdef OPENSSL_NO_STDIO
65#define APPS_WIN16 120#define APPS_WIN16
66#endif 121#endif
67#include "lhash.h" 122
68#include "bn.h" 123/* With IPv6, it looks like Digital has mixed up the proper order of
124 recursive header file inclusion, resulting in the compiler complaining
125 that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
126 is needed to have fileno() declared correctly... So let's define u_int */
127#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
128#define __U_INT
129typedef unsigned int u_int;
130#endif
131
132#include <openssl/lhash.h>
133#include <openssl/bn.h>
69#define USE_SOCKETS 134#define USE_SOCKETS
70#include "apps.h" 135#include "apps.h"
71#include "err.h" 136#include <openssl/err.h>
72#include "pem.h" 137#include <openssl/pem.h>
73#include "x509.h" 138#include <openssl/x509.h>
74#include "ssl.h" 139#include <openssl/ssl.h>
140#include <openssl/rand.h>
75#include "s_apps.h" 141#include "s_apps.h"
76 142
77#ifndef NOPROTO 143#ifdef OPENSSL_SYS_WINDOWS
78static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int export); 144#include <conio.h>
79static int sv_body(char *hostname, int s); 145#endif
80static int www_body(char *hostname, int s); 146
147#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
148/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
149#undef FIONBIO
150#endif
151
152#ifndef OPENSSL_NO_RSA
153static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
154#endif
155static int sv_body(char *hostname, int s, unsigned char *context);
156static int www_body(char *hostname, int s, unsigned char *context);
81static void close_accept_socket(void ); 157static void close_accept_socket(void );
82static void sv_usage(void); 158static void sv_usage(void);
83static int init_ssl_connection(SSL *s); 159static int init_ssl_connection(SSL *s);
84static void print_stats(BIO *bp,SSL_CTX *ctx); 160static void print_stats(BIO *bp,SSL_CTX *ctx);
85#ifndef NO_DH 161static int generate_session_id(const SSL *ssl, unsigned char *id,
86static DH *load_dh_param(void ); 162 unsigned int *id_len);
163#ifndef OPENSSL_NO_DH
164static DH *load_dh_param(char *dhfile);
87static DH *get_dh512(void); 165static DH *get_dh512(void);
88#endif 166#endif
89/* static void s_server_init(void);*/ 167#ifdef MONOLITH
90#else 168static void s_server_init(void);
91static RSA MS_CALLBACK *tmp_rsa_cb();
92static int sv_body();
93static int www_body();
94static void close_accept_socket();
95static void sv_usage();
96static int init_ssl_connection();
97static void print_stats();
98#ifndef NO_DH
99static DH *load_dh_param();
100static DH *get_dh512();
101#endif
102/* static void s_server_init(); */
103#endif 169#endif
104 170
105
106#ifndef S_ISDIR 171#ifndef S_ISDIR
107#define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR) 172# if defined(_S_IFMT) && defined(_S_IFDIR)
173# define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
174# else
175# define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
176# endif
108#endif 177#endif
109 178
110#ifndef NO_DH 179#ifndef OPENSSL_NO_DH
111static unsigned char dh512_p[]={ 180static unsigned char dh512_p[]={
112 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75, 181 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
113 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F, 182 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
@@ -120,7 +189,7 @@ static unsigned char dh512_g[]={
120 0x02, 189 0x02,
121 }; 190 };
122 191
123static DH *get_dh512() 192static DH *get_dh512(void)
124 { 193 {
125 DH *dh=NULL; 194 DH *dh=NULL;
126 195
@@ -136,35 +205,42 @@ static DH *get_dh512()
136/* static int load_CA(SSL_CTX *ctx, char *file);*/ 205/* static int load_CA(SSL_CTX *ctx, char *file);*/
137 206
138#undef BUFSIZZ 207#undef BUFSIZZ
139#define BUFSIZZ 8*1024 208#define BUFSIZZ 16*1024
209static int bufsize=BUFSIZZ;
140static int accept_socket= -1; 210static int accept_socket= -1;
141 211
142#define TEST_CERT "server.pem" 212#define TEST_CERT "server.pem"
143#undef PROG 213#undef PROG
144#define PROG s_server_main 214#define PROG s_server_main
145 215
146#define DH_PARAM "server.pem"
147
148extern int verify_depth; 216extern int verify_depth;
149 217
150static char *cipher=NULL; 218static char *cipher=NULL;
151static int s_server_verify=SSL_VERIFY_NONE; 219static int s_server_verify=SSL_VERIFY_NONE;
220static int s_server_session_id_context = 1; /* anything will do */
152static char *s_cert_file=TEST_CERT,*s_key_file=NULL; 221static char *s_cert_file=TEST_CERT,*s_key_file=NULL;
153static char *s_dcert_file=NULL,*s_dkey_file=NULL; 222static char *s_dcert_file=NULL,*s_dkey_file=NULL;
154#ifdef FIONBIO 223#ifdef FIONBIO
155static int s_nbio=0; 224static int s_nbio=0;
156#endif 225#endif
157static int s_nbio_test=0; 226static int s_nbio_test=0;
227int s_crlf=0;
158static SSL_CTX *ctx=NULL; 228static SSL_CTX *ctx=NULL;
159static int www=0; 229static int www=0;
160 230
161static BIO *bio_s_out=NULL; 231static BIO *bio_s_out=NULL;
162static int s_debug=0; 232static int s_debug=0;
233static int s_msg=0;
163static int s_quiet=0; 234static int s_quiet=0;
164 235
165#if 0 236static int hack=0;
166static void s_server_init() 237static char *engine_id=NULL;
238static const char *session_id_prefix=NULL;
239
240#ifdef MONOLITH
241static void s_server_init(void)
167 { 242 {
243 accept_socket=-1;
168 cipher=NULL; 244 cipher=NULL;
169 s_server_verify=SSL_VERIFY_NONE; 245 s_server_verify=SSL_VERIFY_NONE;
170 s_dcert_file=NULL; 246 s_dcert_file=NULL;
@@ -180,31 +256,42 @@ static void s_server_init()
180 256
181 bio_s_out=NULL; 257 bio_s_out=NULL;
182 s_debug=0; 258 s_debug=0;
259 s_msg=0;
183 s_quiet=0; 260 s_quiet=0;
261 hack=0;
262 engine_id=NULL;
184 } 263 }
185#endif 264#endif
186 265
187static void sv_usage() 266static void sv_usage(void)
188 { 267 {
189 BIO_printf(bio_err,"usage: s_server [args ...]\n"); 268 BIO_printf(bio_err,"usage: s_server [args ...]\n");
190 BIO_printf(bio_err,"\n"); 269 BIO_printf(bio_err,"\n");
191 BIO_printf(bio_err," -accept arg - port to accept on (default is %d\n",PORT); 270 BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT);
271 BIO_printf(bio_err," -context arg - set session ID context\n");
192 BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n"); 272 BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
193 BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n"); 273 BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n");
194 BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n"); 274 BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n");
195 BIO_printf(bio_err," (default is %s)\n",TEST_CERT); 275 BIO_printf(bio_err," (default is %s)\n",TEST_CERT);
196 BIO_printf(bio_err," -key arg - RSA file to use, PEM format assumed, in cert file if\n"); 276 BIO_printf(bio_err," -key arg - Private Key file to use, PEM format assumed, in cert file if\n");
197 BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT); 277 BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT);
278 BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n");
279 BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n");
280 BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n");
281 BIO_printf(bio_err," or a default set of parameters is used\n");
198#ifdef FIONBIO 282#ifdef FIONBIO
199 BIO_printf(bio_err," -nbio - Run with non-blocking IO\n"); 283 BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
200#endif 284#endif
201 BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n"); 285 BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n");
286 BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
202 BIO_printf(bio_err," -debug - Print more output\n"); 287 BIO_printf(bio_err," -debug - Print more output\n");
288 BIO_printf(bio_err," -msg - Show protocol messages\n");
203 BIO_printf(bio_err," -state - Print the SSL states\n"); 289 BIO_printf(bio_err," -state - Print the SSL states\n");
204 BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n"); 290 BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
205 BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n"); 291 BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
206 BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n"); 292 BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n");
207 BIO_printf(bio_err," -cipher arg - play with 'ssleay ciphers' to see what goes here\n"); 293 BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n");
294 BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n");
208 BIO_printf(bio_err," -quiet - No server output\n"); 295 BIO_printf(bio_err," -quiet - No server output\n");
209 BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n"); 296 BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n");
210 BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n"); 297 BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n");
@@ -213,36 +300,196 @@ static void sv_usage()
213 BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n"); 300 BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n");
214 BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n"); 301 BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n");
215 BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n"); 302 BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n");
216 BIO_printf(bio_err," -bugs - Turn on SSL bug compatability\n"); 303#ifndef OPENSSL_NO_DH
304 BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n");
305#endif
306 BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n");
217 BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n"); 307 BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n");
218 BIO_printf(bio_err," -WWW - Returns requested page from to a 'GET <path> HTTP/1.0'\n"); 308 BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
309 BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
310 BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n");
311 BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
312 BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
313 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
219 } 314 }
220 315
221static int local_argc=0; 316static int local_argc=0;
222static char **local_argv; 317static char **local_argv;
223static int hack=0;
224 318
225int MAIN(argc, argv) 319#ifdef CHARSET_EBCDIC
226int argc; 320static int ebcdic_new(BIO *bi);
227char *argv[]; 321static int ebcdic_free(BIO *a);
322static int ebcdic_read(BIO *b, char *out, int outl);
323static int ebcdic_write(BIO *b, char *in, int inl);
324static long ebcdic_ctrl(BIO *b, int cmd, long num, char *ptr);
325static int ebcdic_gets(BIO *bp, char *buf, int size);
326static int ebcdic_puts(BIO *bp, char *str);
327
328#define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
329static BIO_METHOD methods_ebcdic=
228 { 330 {
331 BIO_TYPE_EBCDIC_FILTER,
332 "EBCDIC/ASCII filter",
333 ebcdic_write,
334 ebcdic_read,
335 ebcdic_puts,
336 ebcdic_gets,
337 ebcdic_ctrl,
338 ebcdic_new,
339 ebcdic_free,
340 };
341
342typedef struct
343{
344 size_t alloced;
345 char buff[1];
346} EBCDIC_OUTBUFF;
347
348BIO_METHOD *BIO_f_ebcdic_filter()
349{
350 return(&methods_ebcdic);
351}
352
353static int ebcdic_new(BIO *bi)
354{
355 EBCDIC_OUTBUFF *wbuf;
356
357 wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
358 wbuf->alloced = 1024;
359 wbuf->buff[0] = '\0';
360
361 bi->ptr=(char *)wbuf;
362 bi->init=1;
363 bi->flags=0;
364 return(1);
365}
366
367static int ebcdic_free(BIO *a)
368{
369 if (a == NULL) return(0);
370 if (a->ptr != NULL)
371 OPENSSL_free(a->ptr);
372 a->ptr=NULL;
373 a->init=0;
374 a->flags=0;
375 return(1);
376}
377
378static int ebcdic_read(BIO *b, char *out, int outl)
379{
380 int ret=0;
381
382 if (out == NULL || outl == 0) return(0);
383 if (b->next_bio == NULL) return(0);
384
385 ret=BIO_read(b->next_bio,out,outl);
386 if (ret > 0)
387 ascii2ebcdic(out,out,ret);
388 return(ret);
389}
390
391static int ebcdic_write(BIO *b, char *in, int inl)
392{
393 EBCDIC_OUTBUFF *wbuf;
394 int ret=0;
395 int num;
396 unsigned char n;
397
398 if ((in == NULL) || (inl <= 0)) return(0);
399 if (b->next_bio == NULL) return(0);
400
401 wbuf=(EBCDIC_OUTBUFF *)b->ptr;
402
403 if (inl > (num = wbuf->alloced))
404 {
405 num = num + num; /* double the size */
406 if (num < inl)
407 num = inl;
408 OPENSSL_free(wbuf);
409 wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
410
411 wbuf->alloced = num;
412 wbuf->buff[0] = '\0';
413
414 b->ptr=(char *)wbuf;
415 }
416
417 ebcdic2ascii(wbuf->buff, in, inl);
418
419 ret=BIO_write(b->next_bio, wbuf->buff, inl);
420
421 return(ret);
422}
423
424static long ebcdic_ctrl(BIO *b, int cmd, long num, char *ptr)
425{
426 long ret;
427
428 if (b->next_bio == NULL) return(0);
429 switch (cmd)
430 {
431 case BIO_CTRL_DUP:
432 ret=0L;
433 break;
434 default:
435 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
436 break;
437 }
438 return(ret);
439}
440
441static int ebcdic_gets(BIO *bp, char *buf, int size)
442{
443 int i, ret;
444 if (bp->next_bio == NULL) return(0);
445/* return(BIO_gets(bp->next_bio,buf,size));*/
446 for (i=0; i<size-1; ++i)
447 {
448 ret = ebcdic_read(bp,&buf[i],1);
449 if (ret <= 0)
450 break;
451 else if (buf[i] == '\n')
452 {
453 ++i;
454 break;
455 }
456 }
457 if (i < size)
458 buf[i] = '\0';
459 return (ret < 0 && i == 0) ? ret : i;
460}
461
462static int ebcdic_puts(BIO *bp, char *str)
463{
464 if (bp->next_bio == NULL) return(0);
465 return ebcdic_write(bp, str, strlen(str));
466}
467#endif
468
469int MAIN(int, char **);
470
471int MAIN(int argc, char *argv[])
472 {
473 X509_STORE *store = NULL;
474 int vflags = 0;
229 short port=PORT; 475 short port=PORT;
230 char *CApath=NULL,*CAfile=NULL; 476 char *CApath=NULL,*CAfile=NULL;
477 char *context = NULL;
478 char *dhfile = NULL;
231 int badop=0,bugs=0; 479 int badop=0,bugs=0;
232 int ret=1; 480 int ret=1;
233 int off=0; 481 int off=0;
234 int no_tmp_rsa=0,nocert=0; 482 int no_tmp_rsa=0,no_dhe=0,nocert=0;
235 int state=0; 483 int state=0;
236 SSL_METHOD *meth=NULL; 484 SSL_METHOD *meth=NULL;
237#ifndef NO_DH 485 ENGINE *e=NULL;
238 DH *dh=NULL; 486 char *inrand=NULL;
239#endif
240 487
241#if !defined(NO_SSL2) && !defined(NO_SSL3) 488#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
242 meth=SSLv23_server_method(); 489 meth=SSLv23_server_method();
243#elif !defined(NO_SSL3) 490#elif !defined(OPENSSL_NO_SSL3)
244 meth=SSLv3_server_method(); 491 meth=SSLv3_server_method();
245#elif !defined(NO_SSL2) 492#elif !defined(OPENSSL_NO_SSL2)
246 meth=SSLv2_server_method(); 493 meth=SSLv2_server_method();
247#endif 494#endif
248 495
@@ -250,12 +497,16 @@ char *argv[];
250 local_argv=argv; 497 local_argv=argv;
251 498
252 apps_startup(); 499 apps_startup();
253 s_quiet=0; 500#ifdef MONOLITH
254 s_debug=0; 501 s_server_init();
502#endif
255 503
256 if (bio_err == NULL) 504 if (bio_err == NULL)
257 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); 505 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
258 506
507 if (!load_config(bio_err, NULL))
508 goto end;
509
259 verify_depth=0; 510 verify_depth=0;
260#ifdef FIONBIO 511#ifdef FIONBIO
261 s_nbio=0; 512 s_nbio=0;
@@ -289,6 +540,11 @@ char *argv[];
289 verify_depth=atoi(*(++argv)); 540 verify_depth=atoi(*(++argv));
290 BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth); 541 BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
291 } 542 }
543 else if (strcmp(*argv,"-context") == 0)
544 {
545 if (--argc < 1) goto bad;
546 context= *(++argv);
547 }
292 else if (strcmp(*argv,"-cert") == 0) 548 else if (strcmp(*argv,"-cert") == 0)
293 { 549 {
294 if (--argc < 1) goto bad; 550 if (--argc < 1) goto bad;
@@ -299,6 +555,11 @@ char *argv[];
299 if (--argc < 1) goto bad; 555 if (--argc < 1) goto bad;
300 s_key_file= *(++argv); 556 s_key_file= *(++argv);
301 } 557 }
558 else if (strcmp(*argv,"-dhparam") == 0)
559 {
560 if (--argc < 1) goto bad;
561 dhfile = *(++argv);
562 }
302 else if (strcmp(*argv,"-dcert") == 0) 563 else if (strcmp(*argv,"-dcert") == 0)
303 { 564 {
304 if (--argc < 1) goto bad; 565 if (--argc < 1) goto bad;
@@ -318,6 +579,16 @@ char *argv[];
318 if (--argc < 1) goto bad; 579 if (--argc < 1) goto bad;
319 CApath= *(++argv); 580 CApath= *(++argv);
320 } 581 }
582 else if (strcmp(*argv,"-crl_check") == 0)
583 {
584 vflags |= X509_V_FLAG_CRL_CHECK;
585 }
586 else if (strcmp(*argv,"-crl_check") == 0)
587 {
588 vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
589 }
590 else if (strcmp(*argv,"-serverpref") == 0)
591 { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
321 else if (strcmp(*argv,"-cipher") == 0) 592 else if (strcmp(*argv,"-cipher") == 0)
322 { 593 {
323 if (--argc < 1) goto bad; 594 if (--argc < 1) goto bad;
@@ -341,38 +612,61 @@ char *argv[];
341 } 612 }
342 else if (strcmp(*argv,"-debug") == 0) 613 else if (strcmp(*argv,"-debug") == 0)
343 { s_debug=1; } 614 { s_debug=1; }
615 else if (strcmp(*argv,"-msg") == 0)
616 { s_msg=1; }
344 else if (strcmp(*argv,"-hack") == 0) 617 else if (strcmp(*argv,"-hack") == 0)
345 { hack=1; } 618 { hack=1; }
346 else if (strcmp(*argv,"-state") == 0) 619 else if (strcmp(*argv,"-state") == 0)
347 { state=1; } 620 { state=1; }
621 else if (strcmp(*argv,"-crlf") == 0)
622 { s_crlf=1; }
348 else if (strcmp(*argv,"-quiet") == 0) 623 else if (strcmp(*argv,"-quiet") == 0)
349 { s_quiet=1; } 624 { s_quiet=1; }
350 else if (strcmp(*argv,"-bugs") == 0) 625 else if (strcmp(*argv,"-bugs") == 0)
351 { bugs=1; } 626 { bugs=1; }
352 else if (strcmp(*argv,"-no_tmp_rsa") == 0) 627 else if (strcmp(*argv,"-no_tmp_rsa") == 0)
353 { no_tmp_rsa=1; } 628 { no_tmp_rsa=1; }
629 else if (strcmp(*argv,"-no_dhe") == 0)
630 { no_dhe=1; }
354 else if (strcmp(*argv,"-www") == 0) 631 else if (strcmp(*argv,"-www") == 0)
355 { www=1; } 632 { www=1; }
356 else if (strcmp(*argv,"-WWW") == 0) 633 else if (strcmp(*argv,"-WWW") == 0)
357 { www=2; } 634 { www=2; }
635 else if (strcmp(*argv,"-HTTP") == 0)
636 { www=3; }
358 else if (strcmp(*argv,"-no_ssl2") == 0) 637 else if (strcmp(*argv,"-no_ssl2") == 0)
359 { off|=SSL_OP_NO_SSLv2; } 638 { off|=SSL_OP_NO_SSLv2; }
360 else if (strcmp(*argv,"-no_ssl3") == 0) 639 else if (strcmp(*argv,"-no_ssl3") == 0)
361 { off|=SSL_OP_NO_SSLv3; } 640 { off|=SSL_OP_NO_SSLv3; }
362 else if (strcmp(*argv,"-no_tls1") == 0) 641 else if (strcmp(*argv,"-no_tls1") == 0)
363 { off|=SSL_OP_NO_TLSv1; } 642 { off|=SSL_OP_NO_TLSv1; }
364#ifndef NO_SSL2 643#ifndef OPENSSL_NO_SSL2
365 else if (strcmp(*argv,"-ssl2") == 0) 644 else if (strcmp(*argv,"-ssl2") == 0)
366 { meth=SSLv2_server_method(); } 645 { meth=SSLv2_server_method(); }
367#endif 646#endif
368#ifndef NO_SSL3 647#ifndef OPENSSL_NO_SSL3
369 else if (strcmp(*argv,"-ssl3") == 0) 648 else if (strcmp(*argv,"-ssl3") == 0)
370 { meth=SSLv3_server_method(); } 649 { meth=SSLv3_server_method(); }
371#endif 650#endif
372#ifndef NO_TLS1 651#ifndef OPENSSL_NO_TLS1
373 else if (strcmp(*argv,"-tls1") == 0) 652 else if (strcmp(*argv,"-tls1") == 0)
374 { meth=TLSv1_server_method(); } 653 { meth=TLSv1_server_method(); }
375#endif 654#endif
655 else if (strcmp(*argv, "-id_prefix") == 0)
656 {
657 if (--argc < 1) goto bad;
658 session_id_prefix = *(++argv);
659 }
660 else if (strcmp(*argv,"-engine") == 0)
661 {
662 if (--argc < 1) goto bad;
663 engine_id= *(++argv);
664 }
665 else if (strcmp(*argv,"-rand") == 0)
666 {
667 if (--argc < 1) goto bad;
668 inrand= *(++argv);
669 }
376 else 670 else
377 { 671 {
378 BIO_printf(bio_err,"unknown option %s\n",*argv); 672 BIO_printf(bio_err,"unknown option %s\n",*argv);
@@ -389,9 +683,18 @@ bad:
389 goto end; 683 goto end;
390 } 684 }
391 685
686 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
687 && !RAND_status())
688 {
689 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
690 }
691 if (inrand != NULL)
692 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
693 app_RAND_load_files(inrand));
694
392 if (bio_s_out == NULL) 695 if (bio_s_out == NULL)
393 { 696 {
394 if (s_quiet && !s_debug) 697 if (s_quiet && !s_debug && !s_msg)
395 { 698 {
396 bio_s_out=BIO_new(BIO_s_null()); 699 bio_s_out=BIO_new(BIO_s_null());
397 } 700 }
@@ -402,7 +705,7 @@ bad:
402 } 705 }
403 } 706 }
404 707
405#if !defined(NO_RSA) || !defined(NO_DSA) 708#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
406 if (nocert) 709 if (nocert)
407#endif 710#endif
408 { 711 {
@@ -413,7 +716,9 @@ bad:
413 } 716 }
414 717
415 SSL_load_error_strings(); 718 SSL_load_error_strings();
416 SSLeay_add_ssl_algorithms(); 719 OpenSSL_add_ssl_algorithms();
720
721 e = setup_engine(bio_err, engine_id, 1);
417 722
418 ctx=SSL_CTX_new(meth); 723 ctx=SSL_CTX_new(meth);
419 if (ctx == NULL) 724 if (ctx == NULL)
@@ -421,12 +726,26 @@ bad:
421 ERR_print_errors(bio_err); 726 ERR_print_errors(bio_err);
422 goto end; 727 goto end;
423 } 728 }
424 729 if (session_id_prefix)
730 {
731 if(strlen(session_id_prefix) >= 32)
732 BIO_printf(bio_err,
733"warning: id_prefix is too long, only one new session will be possible\n");
734 else if(strlen(session_id_prefix) >= 16)
735 BIO_printf(bio_err,
736"warning: id_prefix is too long if you use SSLv2\n");
737 if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
738 {
739 BIO_printf(bio_err,"error setting 'id_prefix'\n");
740 ERR_print_errors(bio_err);
741 goto end;
742 }
743 BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
744 }
425 SSL_CTX_set_quiet_shutdown(ctx,1); 745 SSL_CTX_set_quiet_shutdown(ctx,1);
426 if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL); 746 if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
427 if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); 747 if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
428 SSL_CTX_set_options(ctx,off); 748 SSL_CTX_set_options(ctx,off);
429 if (hack) SSL_CTX_set_options(ctx,SSL_OP_NON_EXPORT_FIRST);
430 749
431 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); 750 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
432 751
@@ -451,23 +770,33 @@ bad:
451 ERR_print_errors(bio_err); 770 ERR_print_errors(bio_err);
452 /* goto end; */ 771 /* goto end; */
453 } 772 }
773 store = SSL_CTX_get_cert_store(ctx);
774 X509_STORE_set_flags(store, vflags);
454 775
455#ifndef NO_DH 776#ifndef OPENSSL_NO_DH
456 /* EAY EAY EAY evil hack */ 777 if (!no_dhe)
457 dh=load_dh_param();
458 if (dh != NULL)
459 {
460 BIO_printf(bio_s_out,"Setting temp DH parameters\n");
461 }
462 else
463 { 778 {
464 BIO_printf(bio_s_out,"Using default temp DH parameters\n"); 779 DH *dh=NULL;
465 dh=get_dh512(); 780
466 } 781 if (dhfile)
467 BIO_flush(bio_s_out); 782 dh = load_dh_param(dhfile);
783 else if (s_cert_file)
784 dh = load_dh_param(s_cert_file);
468 785
469 SSL_CTX_set_tmp_dh(ctx,dh); 786 if (dh != NULL)
470 DH_free(dh); 787 {
788 BIO_printf(bio_s_out,"Setting temp DH parameters\n");
789 }
790 else
791 {
792 BIO_printf(bio_s_out,"Using default temp DH parameters\n");
793 dh=get_dh512();
794 }
795 (void)BIO_flush(bio_s_out);
796
797 SSL_CTX_set_tmp_dh(ctx,dh);
798 DH_free(dh);
799 }
471#endif 800#endif
472 801
473 if (!set_cert_stuff(ctx,s_cert_file,s_key_file)) 802 if (!set_cert_stuff(ctx,s_cert_file,s_key_file))
@@ -478,8 +807,10 @@ bad:
478 goto end; 807 goto end;
479 } 808 }
480 809
810#ifndef OPENSSL_NO_RSA
481#if 1 811#if 1
482 SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb); 812 if (!no_tmp_rsa)
813 SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
483#else 814#else
484 if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx)) 815 if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
485 { 816 {
@@ -499,18 +830,26 @@ bad:
499 BIO_printf(bio_s_out,"\n"); 830 BIO_printf(bio_s_out,"\n");
500 } 831 }
501#endif 832#endif
833#endif
502 834
503 if (cipher != NULL) 835 if (cipher != NULL)
504 SSL_CTX_set_cipher_list(ctx,cipher); 836 if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
837 BIO_printf(bio_err,"error setting cipher list\n");
838 ERR_print_errors(bio_err);
839 goto end;
840 }
505 SSL_CTX_set_verify(ctx,s_server_verify,verify_callback); 841 SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
842 SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
843 sizeof s_server_session_id_context);
506 844
507 SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(s_cert_file)); 845 if (CAfile != NULL)
846 SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
508 847
509 BIO_printf(bio_s_out,"ACCEPT\n"); 848 BIO_printf(bio_s_out,"ACCEPT\n");
510 if (www) 849 if (www)
511 do_server(port,&accept_socket,www_body); 850 do_server(port,&accept_socket,www_body, context);
512 else 851 else
513 do_server(port,&accept_socket,sv_body); 852 do_server(port,&accept_socket,sv_body, context);
514 print_stats(bio_s_out,ctx); 853 print_stats(bio_s_out,ctx);
515 ret=0; 854 ret=0;
516end: 855end:
@@ -520,12 +859,11 @@ end:
520 BIO_free(bio_s_out); 859 BIO_free(bio_s_out);
521 bio_s_out=NULL; 860 bio_s_out=NULL;
522 } 861 }
862 apps_shutdown();
523 EXIT(ret); 863 EXIT(ret);
524 } 864 }
525 865
526static void print_stats(bio,ssl_ctx) 866static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
527BIO *bio;
528SSL_CTX *ssl_ctx;
529 { 867 {
530 BIO_printf(bio,"%4ld items in the session cache\n", 868 BIO_printf(bio,"%4ld items in the session cache\n",
531 SSL_CTX_sess_number(ssl_ctx)); 869 SSL_CTX_sess_number(ssl_ctx));
@@ -550,9 +888,7 @@ SSL_CTX *ssl_ctx;
550 SSL_CTX_sess_get_cache_size(ssl_ctx)); 888 SSL_CTX_sess_get_cache_size(ssl_ctx));
551 } 889 }
552 890
553static int sv_body(hostname, s) 891static int sv_body(char *hostname, int s, unsigned char *context)
554char *hostname;
555int s;
556 { 892 {
557 char *buf=NULL; 893 char *buf=NULL;
558 fd_set readfds; 894 fd_set readfds;
@@ -561,8 +897,11 @@ int s;
561 unsigned long l; 897 unsigned long l;
562 SSL *con=NULL; 898 SSL *con=NULL;
563 BIO *sbio; 899 BIO *sbio;
900#ifdef OPENSSL_SYS_WINDOWS
901 struct timeval tv;
902#endif
564 903
565 if ((buf=Malloc(BUFSIZZ)) == NULL) 904 if ((buf=OPENSSL_malloc(bufsize)) == NULL)
566 { 905 {
567 BIO_printf(bio_err,"out of memory\n"); 906 BIO_printf(bio_err,"out of memory\n");
568 goto err; 907 goto err;
@@ -579,8 +918,21 @@ int s;
579 } 918 }
580#endif 919#endif
581 920
582 if (con == NULL) 921 if (con == NULL) {
583 con=(SSL *)SSL_new(ctx); 922 con=SSL_new(ctx);
923#ifndef OPENSSL_NO_KRB5
924 if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
925 {
926 kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
927 KRB5SVC);
928 kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
929 KRB5KEYTAB);
930 }
931#endif /* OPENSSL_NO_KRB5 */
932 if(context)
933 SSL_set_session_id_context(con, context,
934 strlen((char *)context));
935 }
584 SSL_clear(con); 936 SSL_clear(con);
585 937
586 sbio=BIO_new_socket(s,BIO_NOCLOSE); 938 sbio=BIO_new_socket(s,BIO_NOCLOSE);
@@ -601,20 +953,81 @@ int s;
601 BIO_set_callback(SSL_get_rbio(con),bio_dump_cb); 953 BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
602 BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out); 954 BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
603 } 955 }
956 if (s_msg)
957 {
958 SSL_set_msg_callback(con, msg_cb);
959 SSL_set_msg_callback_arg(con, bio_s_out);
960 }
604 961
605 width=s+1; 962 width=s+1;
606 for (;;) 963 for (;;)
607 { 964 {
608 FD_ZERO(&readfds); 965 int read_from_terminal;
609#ifndef WINDOWS 966 int read_from_sslcon;
610 FD_SET(fileno(stdin),&readfds); 967
968 read_from_terminal = 0;
969 read_from_sslcon = SSL_pending(con);
970
971 if (!read_from_sslcon)
972 {
973 FD_ZERO(&readfds);
974#ifndef OPENSSL_SYS_WINDOWS
975 FD_SET(fileno(stdin),&readfds);
976#endif
977 FD_SET(s,&readfds);
978 /* Note: under VMS with SOCKETSHR the second parameter is
979 * currently of type (int *) whereas under other systems
980 * it is (void *) if you don't have a cast it will choke
981 * the compiler: if you do have a cast then you can either
982 * go for (int *) or (void *).
983 */
984#ifdef OPENSSL_SYS_WINDOWS
985 /* Under Windows we can't select on stdin: only
986 * on sockets. As a workaround we timeout the select every
987 * second and check for any keypress. In a proper Windows
988 * application we wouldn't do this because it is inefficient.
989 */
990 tv.tv_sec = 1;
991 tv.tv_usec = 0;
992 i=select(width,(void *)&readfds,NULL,NULL,&tv);
993 if((i < 0) || (!i && !_kbhit() ) )continue;
994 if(_kbhit())
995 read_from_terminal = 1;
996#else
997 i=select(width,(void *)&readfds,NULL,NULL,NULL);
998 if (i <= 0) continue;
999 if (FD_ISSET(fileno(stdin),&readfds))
1000 read_from_terminal = 1;
611#endif 1001#endif
612 FD_SET(s,&readfds); 1002 if (FD_ISSET(s,&readfds))
613 i=select(width,&readfds,NULL,NULL,NULL); 1003 read_from_sslcon = 1;
614 if (i <= 0) continue; 1004 }
615 if (FD_ISSET(fileno(stdin),&readfds)) 1005 if (read_from_terminal)
616 { 1006 {
617 i=read(fileno(stdin),buf,128/*BUFSIZZ*/); 1007 if (s_crlf)
1008 {
1009 int j, lf_num;
1010
1011 i=read(fileno(stdin), buf, bufsize/2);
1012 lf_num = 0;
1013 /* both loops are skipped when i <= 0 */
1014 for (j = 0; j < i; j++)
1015 if (buf[j] == '\n')
1016 lf_num++;
1017 for (j = i-1; j >= 0; j--)
1018 {
1019 buf[j+lf_num] = buf[j];
1020 if (buf[j] == '\n')
1021 {
1022 lf_num--;
1023 i++;
1024 buf[j+lf_num] = '\r';
1025 }
1026 }
1027 assert(lf_num == 0);
1028 }
1029 else
1030 i=read(fileno(stdin),buf,bufsize);
618 if (!s_quiet) 1031 if (!s_quiet)
619 { 1032 {
620 if ((i <= 0) || (buf[0] == 'Q')) 1033 if ((i <= 0) || (buf[0] == 'Q'))
@@ -641,10 +1054,10 @@ int s;
641 printf("SSL_do_handshake -> %d\n",i); 1054 printf("SSL_do_handshake -> %d\n",i);
642 i=0; /*13; */ 1055 i=0; /*13; */
643 continue; 1056 continue;
644 strcpy(buf,"server side RE-NEGOTIATE\n"); 1057 /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
645 } 1058 }
646 if ((buf[0] == 'R') && 1059 if ((buf[0] == 'R') &&
647 ((buf[1] == '\0') || (buf[1] == '\r'))) 1060 ((buf[1] == '\n') || (buf[1] == '\r')))
648 { 1061 {
649 SSL_set_verify(con, 1062 SSL_set_verify(con,
650 SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL); 1063 SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
@@ -653,7 +1066,7 @@ int s;
653 printf("SSL_do_handshake -> %d\n",i); 1066 printf("SSL_do_handshake -> %d\n",i);
654 i=0; /* 13; */ 1067 i=0; /* 13; */
655 continue; 1068 continue;
656 strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); 1069 /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
657 } 1070 }
658 if (buf[0] == 'P') 1071 if (buf[0] == 'P')
659 { 1072 {
@@ -665,6 +1078,9 @@ int s;
665 print_stats(bio_s_out,SSL_get_SSL_CTX(con)); 1078 print_stats(bio_s_out,SSL_get_SSL_CTX(con));
666 } 1079 }
667 } 1080 }
1081#ifdef CHARSET_EBCDIC
1082 ebcdic2ascii(buf,buf,i);
1083#endif
668 l=k=0; 1084 l=k=0;
669 for (;;) 1085 for (;;)
670 { 1086 {
@@ -688,7 +1104,7 @@ int s;
688 ERR_print_errors(bio_err); 1104 ERR_print_errors(bio_err);
689 ret=1; 1105 ret=1;
690 goto err; 1106 goto err;
691 break; 1107 /* break; */
692 case SSL_ERROR_ZERO_RETURN: 1108 case SSL_ERROR_ZERO_RETURN:
693 BIO_printf(bio_s_out,"DONE\n"); 1109 BIO_printf(bio_s_out,"DONE\n");
694 ret=1; 1110 ret=1;
@@ -699,7 +1115,7 @@ int s;
699 if (i <= 0) break; 1115 if (i <= 0) break;
700 } 1116 }
701 } 1117 }
702 if (FD_ISSET(s,&readfds)) 1118 if (read_from_sslcon)
703 { 1119 {
704 if (!SSL_is_init_finished(con)) 1120 if (!SSL_is_init_finished(con))
705 { 1121 {
@@ -718,12 +1134,17 @@ int s;
718 } 1134 }
719 else 1135 else
720 { 1136 {
721 i=SSL_read(con,(char *)buf,128 /*BUFSIZZ */); 1137again:
1138 i=SSL_read(con,(char *)buf,bufsize);
722 switch (SSL_get_error(con,i)) 1139 switch (SSL_get_error(con,i))
723 { 1140 {
724 case SSL_ERROR_NONE: 1141 case SSL_ERROR_NONE:
1142#ifdef CHARSET_EBCDIC
1143 ascii2ebcdic(buf,buf,i);
1144#endif
725 write(fileno(stdout),buf, 1145 write(fileno(stdout),buf,
726 (unsigned int)i); 1146 (unsigned int)i);
1147 if (SSL_pending(con)) goto again;
727 break; 1148 break;
728 case SSL_ERROR_WANT_WRITE: 1149 case SSL_ERROR_WANT_WRITE:
729 case SSL_ERROR_WANT_READ: 1150 case SSL_ERROR_WANT_READ:
@@ -755,15 +1176,15 @@ err:
755 BIO_printf(bio_s_out,"CONNECTION CLOSED\n"); 1176 BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
756 if (buf != NULL) 1177 if (buf != NULL)
757 { 1178 {
758 memset(buf,0,BUFSIZZ); 1179 memset(buf,0,bufsize);
759 Free(buf); 1180 OPENSSL_free(buf);
760 } 1181 }
761 if (ret >= 0) 1182 if (ret >= 0)
762 BIO_printf(bio_s_out,"ACCEPT\n"); 1183 BIO_printf(bio_s_out,"ACCEPT\n");
763 return(ret); 1184 return(ret);
764 } 1185 }
765 1186
766static void close_accept_socket() 1187static void close_accept_socket(void)
767 { 1188 {
768 BIO_printf(bio_err,"shutdown accept socket\n"); 1189 BIO_printf(bio_err,"shutdown accept socket\n");
769 if (accept_socket >= 0) 1190 if (accept_socket >= 0)
@@ -772,11 +1193,10 @@ static void close_accept_socket()
772 } 1193 }
773 } 1194 }
774 1195
775static int init_ssl_connection(con) 1196static int init_ssl_connection(SSL *con)
776SSL *con;
777 { 1197 {
778 int i; 1198 int i;
779 char *str; 1199 const char *str;
780 X509 *peer; 1200 X509 *peer;
781 long verify_error; 1201 long verify_error;
782 MS_STATIC char buf[BUFSIZ]; 1202 MS_STATIC char buf[BUFSIZ];
@@ -820,18 +1240,22 @@ SSL *con;
820 str=SSL_CIPHER_get_name(SSL_get_current_cipher(con)); 1240 str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
821 BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)"); 1241 BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
822 if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n"); 1242 if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
1243 if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
1244 TLS1_FLAGS_TLS_PADDING_BUG)
1245 BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
1246
823 return(1); 1247 return(1);
824 } 1248 }
825 1249
826#ifndef NO_DH 1250#ifndef OPENSSL_NO_DH
827static DH *load_dh_param() 1251static DH *load_dh_param(char *dhfile)
828 { 1252 {
829 DH *ret=NULL; 1253 DH *ret=NULL;
830 BIO *bio; 1254 BIO *bio;
831 1255
832 if ((bio=BIO_new_file(DH_PARAM,"r")) == NULL) 1256 if ((bio=BIO_new_file(dhfile,"r")) == NULL)
833 goto err; 1257 goto err;
834 ret=PEM_read_bio_DHparams(bio,NULL,NULL); 1258 ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
835err: 1259err:
836 if (bio != NULL) BIO_free(bio); 1260 if (bio != NULL) BIO_free(bio);
837 return(ret); 1261 return(ret);
@@ -839,9 +1263,7 @@ err:
839#endif 1263#endif
840 1264
841#if 0 1265#if 0
842static int load_CA(ctx,file) 1266static int load_CA(SSL_CTX *ctx, char *file)
843SSL_CTX *ctx;
844char *file;
845 { 1267 {
846 FILE *in; 1268 FILE *in;
847 X509 *x=NULL; 1269 X509 *x=NULL;
@@ -861,11 +1283,9 @@ char *file;
861 } 1283 }
862#endif 1284#endif
863 1285
864static int www_body(hostname, s) 1286static int www_body(char *hostname, int s, unsigned char *context)
865char *hostname;
866int s;
867 { 1287 {
868 char buf[1024]; 1288 char *buf=NULL;
869 int ret=1; 1289 int ret=1;
870 int i,j,k,blank,dot; 1290 int i,j,k,blank,dot;
871 struct stat st_buf; 1291 struct stat st_buf;
@@ -874,6 +1294,8 @@ int s;
874 BIO *io,*ssl_bio,*sbio; 1294 BIO *io,*ssl_bio,*sbio;
875 long total_bytes; 1295 long total_bytes;
876 1296
1297 buf=OPENSSL_malloc(bufsize);
1298 if (buf == NULL) return(0);
877 io=BIO_new(BIO_f_buffer()); 1299 io=BIO_new(BIO_f_buffer());
878 ssl_bio=BIO_new(BIO_f_ssl()); 1300 ssl_bio=BIO_new(BIO_f_ssl());
879 if ((io == NULL) || (ssl_bio == NULL)) goto err; 1301 if ((io == NULL) || (ssl_bio == NULL)) goto err;
@@ -891,9 +1313,18 @@ int s;
891#endif 1313#endif
892 1314
893 /* lets make the output buffer a reasonable size */ 1315 /* lets make the output buffer a reasonable size */
894 if (!BIO_set_write_buffer_size(io,253 /*16*1024*/)) goto err; 1316 if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
895 1317
896 if ((con=(SSL *)SSL_new(ctx)) == NULL) goto err; 1318 if ((con=SSL_new(ctx)) == NULL) goto err;
1319#ifndef OPENSSL_NO_KRB5
1320 if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
1321 {
1322 kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
1323 kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
1324 }
1325#endif /* OPENSSL_NO_KRB5 */
1326 if(context) SSL_set_session_id_context(con, context,
1327 strlen((char *)context));
897 1328
898 sbio=BIO_new_socket(s,BIO_NOCLOSE); 1329 sbio=BIO_new_socket(s,BIO_NOCLOSE);
899 if (s_nbio_test) 1330 if (s_nbio_test)
@@ -909,6 +1340,9 @@ int s;
909 /* SSL_set_fd(con,s); */ 1340 /* SSL_set_fd(con,s); */
910 BIO_set_ssl(ssl_bio,con,BIO_CLOSE); 1341 BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
911 BIO_push(io,ssl_bio); 1342 BIO_push(io,ssl_bio);
1343#ifdef CHARSET_EBCDIC
1344 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
1345#endif
912 1346
913 if (s_debug) 1347 if (s_debug)
914 { 1348 {
@@ -916,6 +1350,11 @@ int s;
916 BIO_set_callback(SSL_get_rbio(con),bio_dump_cb); 1350 BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
917 BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out); 1351 BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
918 } 1352 }
1353 if (s_msg)
1354 {
1355 SSL_set_msg_callback(con, msg_cb);
1356 SSL_set_msg_callback_arg(con, bio_s_out);
1357 }
919 1358
920 blank=0; 1359 blank=0;
921 for (;;) 1360 for (;;)
@@ -937,14 +1376,14 @@ int s;
937 case SSL_ERROR_ZERO_RETURN: 1376 case SSL_ERROR_ZERO_RETURN:
938 ret=1; 1377 ret=1;
939 goto err; 1378 goto err;
940 break; 1379 /* break; */
941 } 1380 }
942 1381
943 SSL_renegotiate(con); 1382 SSL_renegotiate(con);
944 SSL_write(con,NULL,0); 1383 SSL_write(con,NULL,0);
945 } 1384 }
946 1385
947 i=BIO_gets(io,buf,sizeof(buf)-1); 1386 i=BIO_gets(io,buf,bufsize-1);
948 if (i < 0) /* error */ 1387 if (i < 0) /* error */
949 { 1388 {
950 if (!BIO_should_retry(io)) 1389 if (!BIO_should_retry(io))
@@ -956,7 +1395,7 @@ int s;
956 else 1395 else
957 { 1396 {
958 BIO_printf(bio_s_out,"read R BLOCK\n"); 1397 BIO_printf(bio_s_out,"read R BLOCK\n");
959#ifndef MSDOS 1398#ifndef OPENSSL_SYS_MSDOS
960 sleep(1); 1399 sleep(1);
961#endif 1400#endif
962 continue; 1401 continue;
@@ -974,11 +1413,11 @@ int s;
974 { 1413 {
975 char *p; 1414 char *p;
976 X509 *peer; 1415 X509 *peer;
977 STACK *sk; 1416 STACK_OF(SSL_CIPHER) *sk;
978 static char *space=" "; 1417 static char *space=" ";
979 1418
980 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 1419 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
981 BIO_puts(io,"<HTML><BODY BGCOLOR=ffffff>\n"); 1420 BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
982 BIO_puts(io,"<pre>\n"); 1421 BIO_puts(io,"<pre>\n");
983/* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/ 1422/* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
984 BIO_puts(io,"\n"); 1423 BIO_puts(io,"\n");
@@ -993,10 +1432,10 @@ int s;
993 * be done */ 1432 * be done */
994 BIO_printf(io,"Ciphers supported in s_server binary\n"); 1433 BIO_printf(io,"Ciphers supported in s_server binary\n");
995 sk=SSL_get_ciphers(con); 1434 sk=SSL_get_ciphers(con);
996 j=sk_num(sk); 1435 j=sk_SSL_CIPHER_num(sk);
997 for (i=0; i<j; i++) 1436 for (i=0; i<j; i++)
998 { 1437 {
999 c=(SSL_CIPHER *)sk_value(sk,i); 1438 c=sk_SSL_CIPHER_value(sk,i);
1000 BIO_printf(io,"%-11s:%-25s", 1439 BIO_printf(io,"%-11s:%-25s",
1001 SSL_CIPHER_get_version(c), 1440 SSL_CIPHER_get_version(c),
1002 SSL_CIPHER_get_name(c)); 1441 SSL_CIPHER_get_name(c));
@@ -1004,7 +1443,7 @@ int s;
1004 BIO_puts(io,"\n"); 1443 BIO_puts(io,"\n");
1005 } 1444 }
1006 BIO_puts(io,"\n"); 1445 BIO_puts(io,"\n");
1007 p=SSL_get_shared_ciphers(con,buf,sizeof(buf)); 1446 p=SSL_get_shared_ciphers(con,buf,bufsize);
1008 if (p != NULL) 1447 if (p != NULL)
1009 { 1448 {
1010 BIO_printf(io,"---\nCiphers common between both SSL end points:\n"); 1449 BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
@@ -1050,7 +1489,8 @@ int s;
1050 BIO_puts(io,"</BODY></HTML>\r\n\r\n"); 1489 BIO_puts(io,"</BODY></HTML>\r\n\r\n");
1051 break; 1490 break;
1052 } 1491 }
1053 else if ((www == 2) && (strncmp("GET ",buf,4) == 0)) 1492 else if ((www == 2 || www == 3)
1493 && (strncmp("GET /",buf,5) == 0))
1054 { 1494 {
1055 BIO *file; 1495 BIO *file;
1056 char *p,*e; 1496 char *p,*e;
@@ -1058,15 +1498,29 @@ int s;
1058 1498
1059 /* skip the '/' */ 1499 /* skip the '/' */
1060 p= &(buf[5]); 1500 p= &(buf[5]);
1061 dot=0; 1501
1502 dot = 1;
1062 for (e=p; *e != '\0'; e++) 1503 for (e=p; *e != '\0'; e++)
1063 { 1504 {
1064 if (e[0] == ' ') break; 1505 if (e[0] == ' ')
1065 if ( (e[0] == '.') && 1506 break;
1066 (strncmp(&(e[-1]),"/../",4) == 0)) 1507
1067 dot=1; 1508 switch (dot)
1509 {
1510 case 1:
1511 dot = (e[0] == '.') ? 2 : 0;
1512 break;
1513 case 2:
1514 dot = (e[0] == '.') ? 3 : 0;
1515 break;
1516 case 3:
1517 dot = (e[0] == '/') ? -1 : 0;
1518 break;
1519 }
1520 if (dot == 0)
1521 dot = (e[0] == '/') ? 1 : 0;
1068 } 1522 }
1069 1523 dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
1070 1524
1071 if (*e == '\0') 1525 if (*e == '\0')
1072 { 1526 {
@@ -1090,9 +1544,11 @@ int s;
1090 break; 1544 break;
1091 } 1545 }
1092 1546
1547#if 0
1093 /* append if a directory lookup */ 1548 /* append if a directory lookup */
1094 if (e[-1] == '/') 1549 if (e[-1] == '/')
1095 strcat(p,"index.html"); 1550 strcat(p,"index.html");
1551#endif
1096 1552
1097 /* if a directory, do the index thang */ 1553 /* if a directory, do the index thang */
1098 if (stat(p,&st_buf) < 0) 1554 if (stat(p,&st_buf) < 0)
@@ -1104,7 +1560,13 @@ int s;
1104 } 1560 }
1105 if (S_ISDIR(st_buf.st_mode)) 1561 if (S_ISDIR(st_buf.st_mode))
1106 { 1562 {
1563#if 0 /* must check buffer size */
1107 strcat(p,"/index.html"); 1564 strcat(p,"/index.html");
1565#else
1566 BIO_puts(io,text);
1567 BIO_printf(io,"'%s' is a directory\r\n",p);
1568 break;
1569#endif
1108 } 1570 }
1109 1571
1110 if ((file=BIO_new_file(p,"r")) == NULL) 1572 if ((file=BIO_new_file(p,"r")) == NULL)
@@ -1118,20 +1580,24 @@ int s;
1118 if (!s_quiet) 1580 if (!s_quiet)
1119 BIO_printf(bio_err,"FILE:%s\n",p); 1581 BIO_printf(bio_err,"FILE:%s\n",p);
1120 1582
1121 i=strlen(p); 1583 if (www == 2)
1122 if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) || 1584 {
1123 ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) || 1585 i=strlen(p);
1124 ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0))) 1586 if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
1125 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 1587 ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
1126 else 1588 ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
1127 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"); 1589 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1590 else
1591 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
1592 }
1128 /* send the file */ 1593 /* send the file */
1129 total_bytes=0; 1594 total_bytes=0;
1130 for (;;) 1595 for (;;)
1131 { 1596 {
1132 i=BIO_read(file,buf,1024); 1597 i=BIO_read(file,buf,bufsize);
1133 if (i <= 0) break; 1598 if (i <= 0) break;
1134 1599
1600#ifdef RENEG
1135 total_bytes+=i; 1601 total_bytes+=i;
1136 fprintf(stderr,"%d\n",i); 1602 fprintf(stderr,"%d\n",i);
1137 if (total_bytes > 3*1024) 1603 if (total_bytes > 3*1024)
@@ -1140,6 +1606,7 @@ int s;
1140 fprintf(stderr,"RENEGOTIATE\n"); 1606 fprintf(stderr,"RENEGOTIATE\n");
1141 SSL_renegotiate(con); 1607 SSL_renegotiate(con);
1142 } 1608 }
1609#endif
1143 1610
1144 for (j=0; j<i; ) 1611 for (j=0; j<i; )
1145 { 1612 {
@@ -1184,7 +1651,7 @@ end:
1184 /* make sure we re-use sessions */ 1651 /* make sure we re-use sessions */
1185 SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 1652 SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1186#else 1653#else
1187 /* This kills performace */ 1654 /* This kills performance */
1188/* SSL_shutdown(con); A shutdown gets sent in the 1655/* SSL_shutdown(con); A shutdown gets sent in the
1189 * BIO_free_all(io) procession */ 1656 * BIO_free_all(io) procession */
1190#endif 1657#endif
@@ -1194,14 +1661,14 @@ err:
1194 if (ret >= 0) 1661 if (ret >= 0)
1195 BIO_printf(bio_s_out,"ACCEPT\n"); 1662 BIO_printf(bio_s_out,"ACCEPT\n");
1196 1663
1664 if (buf != NULL) OPENSSL_free(buf);
1197 if (io != NULL) BIO_free_all(io); 1665 if (io != NULL) BIO_free_all(io);
1198/* if (ssl_bio != NULL) BIO_free(ssl_bio);*/ 1666/* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
1199 return(ret); 1667 return(ret);
1200 } 1668 }
1201 1669
1202static RSA MS_CALLBACK *tmp_rsa_cb(s,export) 1670#ifndef OPENSSL_NO_RSA
1203SSL *s; 1671static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
1204int export;
1205 { 1672 {
1206 static RSA *rsa_tmp=NULL; 1673 static RSA *rsa_tmp=NULL;
1207 1674
@@ -1209,17 +1676,39 @@ int export;
1209 { 1676 {
1210 if (!s_quiet) 1677 if (!s_quiet)
1211 { 1678 {
1212 BIO_printf(bio_err,"Generating temp (512 bit) RSA key..."); 1679 BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
1213 BIO_flush(bio_err); 1680 (void)BIO_flush(bio_err);
1214 } 1681 }
1215#ifndef NO_RSA 1682 rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
1216 rsa_tmp=RSA_generate_key(512,RSA_F4,NULL,NULL);
1217#endif
1218 if (!s_quiet) 1683 if (!s_quiet)
1219 { 1684 {
1220 BIO_printf(bio_err,"\n"); 1685 BIO_printf(bio_err,"\n");
1221 BIO_flush(bio_err); 1686 (void)BIO_flush(bio_err);
1222 } 1687 }
1223 } 1688 }
1224 return(rsa_tmp); 1689 return(rsa_tmp);
1225 } 1690 }
1691#endif
1692
1693#define MAX_SESSION_ID_ATTEMPTS 10
1694static int generate_session_id(const SSL *ssl, unsigned char *id,
1695 unsigned int *id_len)
1696 {
1697 unsigned int count = 0;
1698 do {
1699 RAND_pseudo_bytes(id, *id_len);
1700 /* Prefix the session_id with the required prefix. NB: If our
1701 * prefix is too long, clip it - but there will be worse effects
1702 * anyway, eg. the server could only possibly create 1 session
1703 * ID (ie. the prefix!) so all future session negotiations will
1704 * fail due to conflicts. */
1705 memcpy(id, session_id_prefix,
1706 (strlen(session_id_prefix) < *id_len) ?
1707 strlen(session_id_prefix) : *id_len);
1708 }
1709 while(SSL_has_matching_session_id(ssl, id, *id_len) &&
1710 (++count < MAX_SESSION_ID_ATTEMPTS));
1711 if(count >= MAX_SESSION_ID_ATTEMPTS)
1712 return 0;
1713 return 1;
1714 }