s_server.c revision 284295
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 * Copyright (c) 1998-2006 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 */ 111/* ==================================================================== 112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 113 * ECC cipher suite support in OpenSSL originally developed by 114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 115 */ 116/* ==================================================================== 117 * Copyright 2005 Nokia. All rights reserved. 118 * 119 * The portions of the attached software ("Contribution") is developed by 120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source 121 * license. 122 * 123 * The Contribution, originally written by Mika Kousa and Pasi Eronen of 124 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 125 * support (see RFC 4279) to OpenSSL. 126 * 127 * No patent licenses or other rights except those expressly stated in 128 * the OpenSSL open source license shall be deemed granted or received 129 * expressly, by implication, estoppel, or otherwise. 130 * 131 * No assurances are provided by Nokia that the Contribution does not 132 * infringe the patent or other intellectual property rights of any third 133 * party or that the license provides you with all the necessary rights 134 * to make use of the Contribution. 135 * 136 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 137 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 138 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 139 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 140 * OTHERWISE. 141 */ 142 143/* Until the key-gen callbacks are modified to use newer prototypes, we allow 144 * deprecated functions for openssl-internal code */ 145#ifdef OPENSSL_NO_DEPRECATED 146#undef OPENSSL_NO_DEPRECATED 147#endif 148 149#include <assert.h> 150#include <ctype.h> 151#include <stdio.h> 152#include <stdlib.h> 153#include <string.h> 154 155#include <openssl/e_os2.h> 156#ifdef OPENSSL_NO_STDIO 157#define APPS_WIN16 158#endif 159 160#if !defined(OPENSSL_SYS_NETWARE) /* conflicts with winsock2 stuff on netware */ 161#include <sys/types.h> 162#endif 163 164/* With IPv6, it looks like Digital has mixed up the proper order of 165 recursive header file inclusion, resulting in the compiler complaining 166 that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which 167 is needed to have fileno() declared correctly... So let's define u_int */ 168#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) 169#define __U_INT 170typedef unsigned int u_int; 171#endif 172 173#include <openssl/lhash.h> 174#include <openssl/bn.h> 175#define USE_SOCKETS 176#include "apps.h" 177#include <openssl/err.h> 178#include <openssl/pem.h> 179#include <openssl/x509.h> 180#include <openssl/ssl.h> 181#include <openssl/rand.h> 182#include <openssl/ocsp.h> 183#ifndef OPENSSL_NO_DH 184#include <openssl/dh.h> 185#endif 186#ifndef OPENSSL_NO_RSA 187#include <openssl/rsa.h> 188#endif 189#ifndef OPENSSL_NO_SRP 190#include <openssl/srp.h> 191#endif 192#include "s_apps.h" 193#include "timeouts.h" 194 195#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) 196/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ 197#undef FIONBIO 198#endif 199 200#if defined(OPENSSL_SYS_BEOS_R5) 201#include <fcntl.h> 202#endif 203 204#ifndef OPENSSL_NO_RSA 205static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength); 206#endif 207static int sv_body(char *hostname, int s, unsigned char *context); 208static int www_body(char *hostname, int s, unsigned char *context); 209static void close_accept_socket(void ); 210static void sv_usage(void); 211static int init_ssl_connection(SSL *s); 212static void print_stats(BIO *bp,SSL_CTX *ctx); 213static int generate_session_id(const SSL *ssl, unsigned char *id, 214 unsigned int *id_len); 215#ifndef OPENSSL_NO_DH 216static DH *load_dh_param(const char *dhfile); 217static DH *get_dh2048(void); 218#endif 219 220#ifdef MONOLITH 221static void s_server_init(void); 222#endif 223 224#ifndef OPENSSL_NO_DH 225static unsigned char dh2048_p[] = { 226 0xF6,0x42,0x57,0xB7,0x08,0x7F,0x08,0x17,0x72,0xA2,0xBA,0xD6, 227 0xA9,0x42,0xF3,0x05,0xE8,0xF9,0x53,0x11,0x39,0x4F,0xB6,0xF1, 228 0x6E,0xB9,0x4B,0x38,0x20,0xDA,0x01,0xA7,0x56,0xA3,0x14,0xE9, 229 0x8F,0x40,0x55,0xF3,0xD0,0x07,0xC6,0xCB,0x43,0xA9,0x94,0xAD, 230 0xF7,0x4C,0x64,0x86,0x49,0xF8,0x0C,0x83,0xBD,0x65,0xE9,0x17, 231 0xD4,0xA1,0xD3,0x50,0xF8,0xF5,0x59,0x5F,0xDC,0x76,0x52,0x4F, 232 0x3D,0x3D,0x8D,0xDB,0xCE,0x99,0xE1,0x57,0x92,0x59,0xCD,0xFD, 233 0xB8,0xAE,0x74,0x4F,0xC5,0xFC,0x76,0xBC,0x83,0xC5,0x47,0x30, 234 0x61,0xCE,0x7C,0xC9,0x66,0xFF,0x15,0xF9,0xBB,0xFD,0x91,0x5E, 235 0xC7,0x01,0xAA,0xD3,0x5B,0x9E,0x8D,0xA0,0xA5,0x72,0x3A,0xD4, 236 0x1A,0xF0,0xBF,0x46,0x00,0x58,0x2B,0xE5,0xF4,0x88,0xFD,0x58, 237 0x4E,0x49,0xDB,0xCD,0x20,0xB4,0x9D,0xE4,0x91,0x07,0x36,0x6B, 238 0x33,0x6C,0x38,0x0D,0x45,0x1D,0x0F,0x7C,0x88,0xB3,0x1C,0x7C, 239 0x5B,0x2D,0x8E,0xF6,0xF3,0xC9,0x23,0xC0,0x43,0xF0,0xA5,0x5B, 240 0x18,0x8D,0x8E,0xBB,0x55,0x8C,0xB8,0x5D,0x38,0xD3,0x34,0xFD, 241 0x7C,0x17,0x57,0x43,0xA3,0x1D,0x18,0x6C,0xDE,0x33,0x21,0x2C, 242 0xB5,0x2A,0xFF,0x3C,0xE1,0xB1,0x29,0x40,0x18,0x11,0x8D,0x7C, 243 0x84,0xA7,0x0A,0x72,0xD6,0x86,0xC4,0x03,0x19,0xC8,0x07,0x29, 244 0x7A,0xCA,0x95,0x0C,0xD9,0x96,0x9F,0xAB,0xD0,0x0A,0x50,0x9B, 245 0x02,0x46,0xD3,0x08,0x3D,0x66,0xA4,0x5D,0x41,0x9F,0x9C,0x7C, 246 0xBD,0x89,0x4B,0x22,0x19,0x26,0xBA,0xAB,0xA2,0x5E,0xC3,0x55, 247 0xE9,0x32,0x0B,0x3B, 248}; 249 250static unsigned char dh2048_g[] = { 251 0x02, 252}; 253 254DH *get_dh2048() 255{ 256 DH *dh; 257 258 if ((dh = DH_new()) == NULL) 259 return NULL; 260 dh->p=BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL); 261 dh->g=BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL); 262 if (dh->p == NULL || dh->g == NULL) { 263 DH_free(dh); 264 return NULL; 265 } 266 return dh; 267} 268#endif 269 270 271/* static int load_CA(SSL_CTX *ctx, char *file);*/ 272 273#undef BUFSIZZ 274#define BUFSIZZ 16*1024 275static int bufsize=BUFSIZZ; 276static int accept_socket= -1; 277 278#define TEST_CERT "server.pem" 279#ifndef OPENSSL_NO_TLSEXT 280#define TEST_CERT2 "server2.pem" 281#endif 282#undef PROG 283#define PROG s_server_main 284 285extern int verify_depth, verify_return_error; 286 287static char *cipher=NULL; 288static int s_server_verify=SSL_VERIFY_NONE; 289static int s_server_session_id_context = 1; /* anything will do */ 290static const char *s_cert_file=TEST_CERT,*s_key_file=NULL; 291#ifndef OPENSSL_NO_TLSEXT 292static const char *s_cert_file2=TEST_CERT2,*s_key_file2=NULL; 293#endif 294static char *s_dcert_file=NULL,*s_dkey_file=NULL; 295#ifdef FIONBIO 296static int s_nbio=0; 297#endif 298static int s_nbio_test=0; 299int s_crlf=0; 300static SSL_CTX *ctx=NULL; 301#ifndef OPENSSL_NO_TLSEXT 302static SSL_CTX *ctx2=NULL; 303#endif 304static int www=0; 305 306static BIO *bio_s_out=NULL; 307static int s_debug=0; 308#ifndef OPENSSL_NO_TLSEXT 309static int s_tlsextdebug=0; 310static int s_tlsextstatus=0; 311static int cert_status_cb(SSL *s, void *arg); 312#endif 313static int s_msg=0; 314static int s_quiet=0; 315 316static char *keymatexportlabel=NULL; 317static int keymatexportlen=20; 318 319static int hack=0; 320#ifndef OPENSSL_NO_ENGINE 321static char *engine_id=NULL; 322#endif 323static const char *session_id_prefix=NULL; 324 325static int enable_timeouts = 0; 326static long socket_mtu; 327#ifndef OPENSSL_NO_DTLS1 328static int cert_chain = 0; 329#endif 330 331 332#ifndef OPENSSL_NO_PSK 333static char *psk_identity="Client_identity"; 334char *psk_key=NULL; /* by default PSK is not used */ 335 336static unsigned int psk_server_cb(SSL *ssl, const char *identity, 337 unsigned char *psk, unsigned int max_psk_len) 338 { 339 unsigned int psk_len = 0; 340 int ret; 341 BIGNUM *bn = NULL; 342 343 if (s_debug) 344 BIO_printf(bio_s_out,"psk_server_cb\n"); 345 if (!identity) 346 { 347 BIO_printf(bio_err,"Error: client did not send PSK identity\n"); 348 goto out_err; 349 } 350 if (s_debug) 351 BIO_printf(bio_s_out,"identity_len=%d identity=%s\n", 352 identity ? (int)strlen(identity) : 0, identity); 353 354 /* here we could lookup the given identity e.g. from a database */ 355 if (strcmp(identity, psk_identity) != 0) 356 { 357 BIO_printf(bio_s_out, "PSK error: client identity not found" 358 " (got '%s' expected '%s')\n", identity, 359 psk_identity); 360 goto out_err; 361 } 362 if (s_debug) 363 BIO_printf(bio_s_out, "PSK client identity found\n"); 364 365 /* convert the PSK key to binary */ 366 ret = BN_hex2bn(&bn, psk_key); 367 if (!ret) 368 { 369 BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key); 370 if (bn) 371 BN_free(bn); 372 return 0; 373 } 374 if (BN_num_bytes(bn) > (int)max_psk_len) 375 { 376 BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n", 377 max_psk_len, BN_num_bytes(bn)); 378 BN_free(bn); 379 return 0; 380 } 381 382 ret = BN_bn2bin(bn, psk); 383 BN_free(bn); 384 385 if (ret < 0) 386 goto out_err; 387 psk_len = (unsigned int)ret; 388 389 if (s_debug) 390 BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len); 391 return psk_len; 392 out_err: 393 if (s_debug) 394 BIO_printf(bio_err, "Error in PSK server callback\n"); 395 return 0; 396 } 397#endif 398 399#ifndef OPENSSL_NO_SRP 400/* This is a context that we pass to callbacks */ 401typedef struct srpsrvparm_st 402 { 403 char *login; 404 SRP_VBASE *vb; 405 SRP_user_pwd *user; 406 } srpsrvparm; 407 408/* This callback pretends to require some asynchronous logic in order to obtain 409 a verifier. When the callback is called for a new connection we return 410 with a negative value. This will provoke the accept etc to return with 411 an LOOKUP_X509. The main logic of the reinvokes the suspended call 412 (which would normally occur after a worker has finished) and we 413 set the user parameters. 414*/ 415static int MS_CALLBACK ssl_srp_server_param_cb(SSL *s, int *ad, void *arg) 416 { 417 srpsrvparm *p = (srpsrvparm *)arg; 418 if (p->login == NULL && p->user == NULL ) 419 { 420 p->login = SSL_get_srp_username(s); 421 BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login); 422 return (-1) ; 423 } 424 425 if (p->user == NULL) 426 { 427 BIO_printf(bio_err, "User %s doesn't exist\n", p->login); 428 return SSL3_AL_FATAL; 429 } 430 if (SSL_set_srp_server_param(s, p->user->N, p->user->g, p->user->s, p->user->v, 431 p->user->info) < 0) 432 { 433 *ad = SSL_AD_INTERNAL_ERROR; 434 return SSL3_AL_FATAL; 435 } 436 BIO_printf(bio_err, "SRP parameters set: username = \"%s\" info=\"%s\" \n", p->login,p->user->info); 437 /* need to check whether there are memory leaks */ 438 p->user = NULL; 439 p->login = NULL; 440 return SSL_ERROR_NONE; 441 } 442 443#endif 444 445#ifdef MONOLITH 446static void s_server_init(void) 447 { 448 accept_socket=-1; 449 cipher=NULL; 450 s_server_verify=SSL_VERIFY_NONE; 451 s_dcert_file=NULL; 452 s_dkey_file=NULL; 453 s_cert_file=TEST_CERT; 454 s_key_file=NULL; 455#ifndef OPENSSL_NO_TLSEXT 456 s_cert_file2=TEST_CERT2; 457 s_key_file2=NULL; 458 ctx2=NULL; 459#endif 460#ifdef FIONBIO 461 s_nbio=0; 462#endif 463 s_nbio_test=0; 464 ctx=NULL; 465 www=0; 466 467 bio_s_out=NULL; 468 s_debug=0; 469 s_msg=0; 470 s_quiet=0; 471 hack=0; 472#ifndef OPENSSL_NO_ENGINE 473 engine_id=NULL; 474#endif 475 } 476#endif 477 478static void sv_usage(void) 479 { 480 BIO_printf(bio_err,"usage: s_server [args ...]\n"); 481 BIO_printf(bio_err,"\n"); 482 BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT); 483 BIO_printf(bio_err," -context arg - set session ID context\n"); 484 BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n"); 485 BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n"); 486 BIO_printf(bio_err," -verify_return_error - return verification errors\n"); 487 BIO_printf(bio_err," -cert arg - certificate file to use\n"); 488 BIO_printf(bio_err," (default is %s)\n",TEST_CERT); 489 BIO_printf(bio_err," -crl_check - check the peer certificate has not been revoked by its CA.\n" \ 490 " The CRL(s) are appended to the certificate file\n"); 491 BIO_printf(bio_err," -crl_check_all - check the peer certificate has not been revoked by its CA\n" \ 492 " or any other CRL in the CA chain. CRL(s) are appened to the\n" \ 493 " the certificate file.\n"); 494 BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n"); 495 BIO_printf(bio_err," -key arg - Private Key file to use, in cert file if\n"); 496 BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT); 497 BIO_printf(bio_err," -keyform arg - key format (PEM, DER or ENGINE) PEM default\n"); 498 BIO_printf(bio_err," -pass arg - private key file pass phrase source\n"); 499 BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n"); 500 BIO_printf(bio_err," -dcertform x - second certificate format (PEM or DER) PEM default\n"); 501 BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n"); 502 BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n"); 503 BIO_printf(bio_err," -dpass arg - second private key file pass phrase source\n"); 504 BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n"); 505 BIO_printf(bio_err," or a default set of parameters is used\n"); 506#ifndef OPENSSL_NO_ECDH 507 BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \ 508 " Use \"openssl ecparam -list_curves\" for all names\n" \ 509 " (default is nistp256).\n"); 510#endif 511#ifdef FIONBIO 512 BIO_printf(bio_err," -nbio - Run with non-blocking IO\n"); 513#endif 514 BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n"); 515 BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n"); 516 BIO_printf(bio_err," -debug - Print more output\n"); 517 BIO_printf(bio_err," -msg - Show protocol messages\n"); 518 BIO_printf(bio_err," -state - Print the SSL states\n"); 519 BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n"); 520 BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n"); 521 BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n"); 522 BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n"); 523 BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n"); 524 BIO_printf(bio_err," -quiet - No server output\n"); 525 BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n"); 526#ifndef OPENSSL_NO_PSK 527 BIO_printf(bio_err," -psk_hint arg - PSK identity hint to use\n"); 528 BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n"); 529# ifndef OPENSSL_NO_JPAKE 530 BIO_printf(bio_err," -jpake arg - JPAKE secret to use\n"); 531# endif 532#endif 533#ifndef OPENSSL_NO_SRP 534 BIO_printf(bio_err," -srpvfile file - The verifier file for SRP\n"); 535 BIO_printf(bio_err," -srpuserseed string - A seed string for a default user salt.\n"); 536#endif 537 BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n"); 538#ifndef OPENSSL_NO_SSL3_METHOD 539 BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n"); 540#endif 541 BIO_printf(bio_err," -tls1_2 - Just talk TLSv1.2\n"); 542 BIO_printf(bio_err," -tls1_1 - Just talk TLSv1.1\n"); 543 BIO_printf(bio_err," -tls1 - Just talk TLSv1\n"); 544 BIO_printf(bio_err," -dtls1 - Just talk DTLSv1\n"); 545 BIO_printf(bio_err," -timeout - Enable timeouts\n"); 546 BIO_printf(bio_err," -mtu - Set link layer MTU\n"); 547 BIO_printf(bio_err," -chain - Read a certificate chain\n"); 548 BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n"); 549 BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n"); 550 BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n"); 551 BIO_printf(bio_err," -no_tls1_1 - Just disable TLSv1.1\n"); 552 BIO_printf(bio_err," -no_tls1_2 - Just disable TLSv1.2\n"); 553#ifndef OPENSSL_NO_DH 554 BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n"); 555#endif 556#ifndef OPENSSL_NO_ECDH 557 BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n"); 558#endif 559 BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n"); 560 BIO_printf(bio_err," -hack - workaround for early Netscape code\n"); 561 BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n"); 562 BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n"); 563 BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n"); 564 BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n"); 565#ifndef OPENSSL_NO_ENGINE 566 BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n"); 567#endif 568 BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n"); 569 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); 570#ifndef OPENSSL_NO_TLSEXT 571 BIO_printf(bio_err," -servername host - servername for HostName TLS extension\n"); 572 BIO_printf(bio_err," -servername_fatal - on mismatch send fatal alert (default warning alert)\n"); 573 BIO_printf(bio_err," -cert2 arg - certificate file to use for servername\n"); 574 BIO_printf(bio_err," (default is %s)\n",TEST_CERT2); 575 BIO_printf(bio_err," -key2 arg - Private Key file to use for servername, in cert file if\n"); 576 BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT2); 577 BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n"); 578 BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n"); 579 BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n"); 580# ifndef OPENSSL_NO_NEXTPROTONEG 581 BIO_printf(bio_err," -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n"); 582# endif 583# ifndef OPENSSL_NO_SRTP 584 BIO_printf(bio_err," -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n"); 585# endif 586#endif 587 BIO_printf(bio_err," -keymatexport label - Export keying material using label\n"); 588 BIO_printf(bio_err," -keymatexportlen len - Export len bytes of keying material (default 20)\n"); 589 BIO_printf(bio_err," -status - respond to certificate status requests\n"); 590 BIO_printf(bio_err," -status_verbose - enable status request verbose printout\n"); 591 BIO_printf(bio_err," -status_timeout n - status request responder timeout\n"); 592 BIO_printf(bio_err," -status_url URL - status request fallback URL\n"); 593 } 594 595static int local_argc=0; 596static char **local_argv; 597 598#ifdef CHARSET_EBCDIC 599static int ebcdic_new(BIO *bi); 600static int ebcdic_free(BIO *a); 601static int ebcdic_read(BIO *b, char *out, int outl); 602static int ebcdic_write(BIO *b, const char *in, int inl); 603static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr); 604static int ebcdic_gets(BIO *bp, char *buf, int size); 605static int ebcdic_puts(BIO *bp, const char *str); 606 607#define BIO_TYPE_EBCDIC_FILTER (18|0x0200) 608static BIO_METHOD methods_ebcdic= 609 { 610 BIO_TYPE_EBCDIC_FILTER, 611 "EBCDIC/ASCII filter", 612 ebcdic_write, 613 ebcdic_read, 614 ebcdic_puts, 615 ebcdic_gets, 616 ebcdic_ctrl, 617 ebcdic_new, 618 ebcdic_free, 619 }; 620 621typedef struct 622{ 623 size_t alloced; 624 char buff[1]; 625} EBCDIC_OUTBUFF; 626 627BIO_METHOD *BIO_f_ebcdic_filter() 628{ 629 return(&methods_ebcdic); 630} 631 632static int ebcdic_new(BIO *bi) 633{ 634 EBCDIC_OUTBUFF *wbuf; 635 636 wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024); 637 wbuf->alloced = 1024; 638 wbuf->buff[0] = '\0'; 639 640 bi->ptr=(char *)wbuf; 641 bi->init=1; 642 bi->flags=0; 643 return(1); 644} 645 646static int ebcdic_free(BIO *a) 647{ 648 if (a == NULL) return(0); 649 if (a->ptr != NULL) 650 OPENSSL_free(a->ptr); 651 a->ptr=NULL; 652 a->init=0; 653 a->flags=0; 654 return(1); 655} 656 657static int ebcdic_read(BIO *b, char *out, int outl) 658{ 659 int ret=0; 660 661 if (out == NULL || outl == 0) return(0); 662 if (b->next_bio == NULL) return(0); 663 664 ret=BIO_read(b->next_bio,out,outl); 665 if (ret > 0) 666 ascii2ebcdic(out,out,ret); 667 return(ret); 668} 669 670static int ebcdic_write(BIO *b, const char *in, int inl) 671{ 672 EBCDIC_OUTBUFF *wbuf; 673 int ret=0; 674 int num; 675 unsigned char n; 676 677 if ((in == NULL) || (inl <= 0)) return(0); 678 if (b->next_bio == NULL) return(0); 679 680 wbuf=(EBCDIC_OUTBUFF *)b->ptr; 681 682 if (inl > (num = wbuf->alloced)) 683 { 684 num = num + num; /* double the size */ 685 if (num < inl) 686 num = inl; 687 OPENSSL_free(wbuf); 688 wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num); 689 690 wbuf->alloced = num; 691 wbuf->buff[0] = '\0'; 692 693 b->ptr=(char *)wbuf; 694 } 695 696 ebcdic2ascii(wbuf->buff, in, inl); 697 698 ret=BIO_write(b->next_bio, wbuf->buff, inl); 699 700 return(ret); 701} 702 703static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr) 704{ 705 long ret; 706 707 if (b->next_bio == NULL) return(0); 708 switch (cmd) 709 { 710 case BIO_CTRL_DUP: 711 ret=0L; 712 break; 713 default: 714 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 715 break; 716 } 717 return(ret); 718} 719 720static int ebcdic_gets(BIO *bp, char *buf, int size) 721{ 722 int i, ret=0; 723 if (bp->next_bio == NULL) return(0); 724/* return(BIO_gets(bp->next_bio,buf,size));*/ 725 for (i=0; i<size-1; ++i) 726 { 727 ret = ebcdic_read(bp,&buf[i],1); 728 if (ret <= 0) 729 break; 730 else if (buf[i] == '\n') 731 { 732 ++i; 733 break; 734 } 735 } 736 if (i < size) 737 buf[i] = '\0'; 738 return (ret < 0 && i == 0) ? ret : i; 739} 740 741static int ebcdic_puts(BIO *bp, const char *str) 742{ 743 if (bp->next_bio == NULL) return(0); 744 return ebcdic_write(bp, str, strlen(str)); 745} 746#endif 747 748#ifndef OPENSSL_NO_TLSEXT 749 750/* This is a context that we pass to callbacks */ 751typedef struct tlsextctx_st { 752 char * servername; 753 BIO * biodebug; 754 int extension_error; 755} tlsextctx; 756 757 758static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg) 759 { 760 tlsextctx * p = (tlsextctx *) arg; 761 const char * servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); 762 if (servername && p->biodebug) 763 BIO_printf(p->biodebug,"Hostname in TLS extension: \"%s\"\n",servername); 764 765 if (!p->servername) 766 return SSL_TLSEXT_ERR_NOACK; 767 768 if (servername) 769 { 770 if (strcasecmp(servername,p->servername)) 771 return p->extension_error; 772 if (ctx2) 773 { 774 BIO_printf(p->biodebug,"Switching server context.\n"); 775 SSL_set_SSL_CTX(s,ctx2); 776 } 777 } 778 return SSL_TLSEXT_ERR_OK; 779} 780 781/* Structure passed to cert status callback */ 782 783typedef struct tlsextstatusctx_st { 784 /* Default responder to use */ 785 char *host, *path, *port; 786 int use_ssl; 787 int timeout; 788 BIO *err; 789 int verbose; 790} tlsextstatusctx; 791 792static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0}; 793 794/* Certificate Status callback. This is called when a client includes a 795 * certificate status request extension. 796 * 797 * This is a simplified version. It examines certificates each time and 798 * makes one OCSP responder query for each request. 799 * 800 * A full version would store details such as the OCSP certificate IDs and 801 * minimise the number of OCSP responses by caching them until they were 802 * considered "expired". 803 */ 804 805static int cert_status_cb(SSL *s, void *arg) 806 { 807 tlsextstatusctx *srctx = arg; 808 BIO *err = srctx->err; 809 char *host, *port, *path; 810 int use_ssl; 811 unsigned char *rspder = NULL; 812 int rspderlen; 813 STACK_OF(OPENSSL_STRING) *aia = NULL; 814 X509 *x = NULL; 815 X509_STORE_CTX inctx; 816 X509_OBJECT obj; 817 OCSP_REQUEST *req = NULL; 818 OCSP_RESPONSE *resp = NULL; 819 OCSP_CERTID *id = NULL; 820 STACK_OF(X509_EXTENSION) *exts; 821 int ret = SSL_TLSEXT_ERR_NOACK; 822 int i; 823#if 0 824STACK_OF(OCSP_RESPID) *ids; 825SSL_get_tlsext_status_ids(s, &ids); 826BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids)); 827#endif 828 if (srctx->verbose) 829 BIO_puts(err, "cert_status: callback called\n"); 830 /* Build up OCSP query from server certificate */ 831 x = SSL_get_certificate(s); 832 aia = X509_get1_ocsp(x); 833 if (aia) 834 { 835 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0), 836 &host, &port, &path, &use_ssl)) 837 { 838 BIO_puts(err, "cert_status: can't parse AIA URL\n"); 839 goto err; 840 } 841 if (srctx->verbose) 842 BIO_printf(err, "cert_status: AIA URL: %s\n", 843 sk_OPENSSL_STRING_value(aia, 0)); 844 } 845 else 846 { 847 if (!srctx->host) 848 { 849 BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n"); 850 goto done; 851 } 852 host = srctx->host; 853 path = srctx->path; 854 port = srctx->port; 855 use_ssl = srctx->use_ssl; 856 } 857 858 if (!X509_STORE_CTX_init(&inctx, 859 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)), 860 NULL, NULL)) 861 goto err; 862 if (X509_STORE_get_by_subject(&inctx,X509_LU_X509, 863 X509_get_issuer_name(x),&obj) <= 0) 864 { 865 BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n"); 866 X509_STORE_CTX_cleanup(&inctx); 867 goto done; 868 } 869 req = OCSP_REQUEST_new(); 870 if (!req) 871 goto err; 872 id = OCSP_cert_to_id(NULL, x, obj.data.x509); 873 X509_free(obj.data.x509); 874 X509_STORE_CTX_cleanup(&inctx); 875 if (!id) 876 goto err; 877 if (!OCSP_request_add0_id(req, id)) 878 goto err; 879 id = NULL; 880 /* Add any extensions to the request */ 881 SSL_get_tlsext_status_exts(s, &exts); 882 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) 883 { 884 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); 885 if (!OCSP_REQUEST_add_ext(req, ext, -1)) 886 goto err; 887 } 888 resp = process_responder(err, req, host, path, port, use_ssl, NULL, 889 srctx->timeout); 890 if (!resp) 891 { 892 BIO_puts(err, "cert_status: error querying responder\n"); 893 goto done; 894 } 895 rspderlen = i2d_OCSP_RESPONSE(resp, &rspder); 896 if (rspderlen <= 0) 897 goto err; 898 SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen); 899 if (srctx->verbose) 900 { 901 BIO_puts(err, "cert_status: ocsp response sent:\n"); 902 OCSP_RESPONSE_print(err, resp, 2); 903 } 904 ret = SSL_TLSEXT_ERR_OK; 905 done: 906 if (ret != SSL_TLSEXT_ERR_OK) 907 ERR_print_errors(err); 908 if (aia) 909 { 910 OPENSSL_free(host); 911 OPENSSL_free(path); 912 OPENSSL_free(port); 913 X509_email_free(aia); 914 } 915 if (id) 916 OCSP_CERTID_free(id); 917 if (req) 918 OCSP_REQUEST_free(req); 919 if (resp) 920 OCSP_RESPONSE_free(resp); 921 return ret; 922 err: 923 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 924 goto done; 925 } 926 927# ifndef OPENSSL_NO_NEXTPROTONEG 928/* This is the context that we pass to next_proto_cb */ 929typedef struct tlsextnextprotoctx_st { 930 unsigned char *data; 931 unsigned int len; 932} tlsextnextprotoctx; 933 934static int next_proto_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg) 935 { 936 tlsextnextprotoctx *next_proto = arg; 937 938 *data = next_proto->data; 939 *len = next_proto->len; 940 941 return SSL_TLSEXT_ERR_OK; 942 } 943# endif /* ndef OPENSSL_NO_NEXTPROTONEG */ 944 945 946#endif 947 948int MAIN(int, char **); 949 950#ifndef OPENSSL_NO_JPAKE 951static char *jpake_secret = NULL; 952#endif 953#ifndef OPENSSL_NO_SRP 954 static srpsrvparm srp_callback_parm; 955#endif 956#ifndef OPENSSL_NO_SRTP 957static char *srtp_profiles = NULL; 958#endif 959 960int MAIN(int argc, char *argv[]) 961 { 962 X509_VERIFY_PARAM *vpm = NULL; 963 int badarg = 0; 964 short port=PORT; 965 char *CApath=NULL,*CAfile=NULL; 966 unsigned char *context = NULL; 967 char *dhfile = NULL; 968#ifndef OPENSSL_NO_ECDH 969 char *named_curve = NULL; 970#endif 971 int badop=0,bugs=0; 972 int ret=1; 973 int off=0; 974 int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0; 975 int state=0; 976 const SSL_METHOD *meth=NULL; 977 int socket_type=SOCK_STREAM; 978 ENGINE *e=NULL; 979 char *inrand=NULL; 980 int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM; 981 char *passarg = NULL, *pass = NULL; 982 char *dpassarg = NULL, *dpass = NULL; 983 int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM; 984 X509 *s_cert = NULL, *s_dcert = NULL; 985 EVP_PKEY *s_key = NULL, *s_dkey = NULL; 986 int no_cache = 0; 987#ifndef OPENSSL_NO_TLSEXT 988 EVP_PKEY *s_key2 = NULL; 989 X509 *s_cert2 = NULL; 990 tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING}; 991# ifndef OPENSSL_NO_NEXTPROTONEG 992 const char *next_proto_neg_in = NULL; 993 tlsextnextprotoctx next_proto; 994# endif 995#endif 996#ifndef OPENSSL_NO_PSK 997 /* by default do not send a PSK identity hint */ 998 static char *psk_identity_hint=NULL; 999#endif 1000#ifndef OPENSSL_NO_SRP 1001 char *srpuserseed = NULL; 1002 char *srp_verifier_file = NULL; 1003#endif 1004 meth=SSLv23_server_method(); 1005 1006 local_argc=argc; 1007 local_argv=argv; 1008 1009 apps_startup(); 1010#ifdef MONOLITH 1011 s_server_init(); 1012#endif 1013 1014 if (bio_err == NULL) 1015 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); 1016 1017 if (!load_config(bio_err, NULL)) 1018 goto end; 1019 1020 verify_depth=0; 1021#ifdef FIONBIO 1022 s_nbio=0; 1023#endif 1024 s_nbio_test=0; 1025 1026 argc--; 1027 argv++; 1028 1029 while (argc >= 1) 1030 { 1031 if ((strcmp(*argv,"-port") == 0) || 1032 (strcmp(*argv,"-accept") == 0)) 1033 { 1034 if (--argc < 1) goto bad; 1035 if (!extract_port(*(++argv),&port)) 1036 goto bad; 1037 } 1038 else if (strcmp(*argv,"-verify") == 0) 1039 { 1040 s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE; 1041 if (--argc < 1) goto bad; 1042 verify_depth=atoi(*(++argv)); 1043 BIO_printf(bio_err,"verify depth is %d\n",verify_depth); 1044 } 1045 else if (strcmp(*argv,"-Verify") == 0) 1046 { 1047 s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT| 1048 SSL_VERIFY_CLIENT_ONCE; 1049 if (--argc < 1) goto bad; 1050 verify_depth=atoi(*(++argv)); 1051 BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth); 1052 } 1053 else if (strcmp(*argv,"-context") == 0) 1054 { 1055 if (--argc < 1) goto bad; 1056 context= (unsigned char *)*(++argv); 1057 } 1058 else if (strcmp(*argv,"-cert") == 0) 1059 { 1060 if (--argc < 1) goto bad; 1061 s_cert_file= *(++argv); 1062 } 1063 else if (strcmp(*argv,"-certform") == 0) 1064 { 1065 if (--argc < 1) goto bad; 1066 s_cert_format = str2fmt(*(++argv)); 1067 } 1068 else if (strcmp(*argv,"-key") == 0) 1069 { 1070 if (--argc < 1) goto bad; 1071 s_key_file= *(++argv); 1072 } 1073 else if (strcmp(*argv,"-keyform") == 0) 1074 { 1075 if (--argc < 1) goto bad; 1076 s_key_format = str2fmt(*(++argv)); 1077 } 1078 else if (strcmp(*argv,"-pass") == 0) 1079 { 1080 if (--argc < 1) goto bad; 1081 passarg = *(++argv); 1082 } 1083 else if (strcmp(*argv,"-dhparam") == 0) 1084 { 1085 if (--argc < 1) goto bad; 1086 dhfile = *(++argv); 1087 } 1088#ifndef OPENSSL_NO_ECDH 1089 else if (strcmp(*argv,"-named_curve") == 0) 1090 { 1091 if (--argc < 1) goto bad; 1092 named_curve = *(++argv); 1093 } 1094#endif 1095 else if (strcmp(*argv,"-dcertform") == 0) 1096 { 1097 if (--argc < 1) goto bad; 1098 s_dcert_format = str2fmt(*(++argv)); 1099 } 1100 else if (strcmp(*argv,"-dcert") == 0) 1101 { 1102 if (--argc < 1) goto bad; 1103 s_dcert_file= *(++argv); 1104 } 1105 else if (strcmp(*argv,"-dkeyform") == 0) 1106 { 1107 if (--argc < 1) goto bad; 1108 s_dkey_format = str2fmt(*(++argv)); 1109 } 1110 else if (strcmp(*argv,"-dpass") == 0) 1111 { 1112 if (--argc < 1) goto bad; 1113 dpassarg = *(++argv); 1114 } 1115 else if (strcmp(*argv,"-dkey") == 0) 1116 { 1117 if (--argc < 1) goto bad; 1118 s_dkey_file= *(++argv); 1119 } 1120 else if (strcmp(*argv,"-nocert") == 0) 1121 { 1122 nocert=1; 1123 } 1124 else if (strcmp(*argv,"-CApath") == 0) 1125 { 1126 if (--argc < 1) goto bad; 1127 CApath= *(++argv); 1128 } 1129 else if (strcmp(*argv,"-no_cache") == 0) 1130 no_cache = 1; 1131 else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) 1132 { 1133 if (badarg) 1134 goto bad; 1135 continue; 1136 } 1137 else if (strcmp(*argv,"-verify_return_error") == 0) 1138 verify_return_error = 1; 1139 else if (strcmp(*argv,"-serverpref") == 0) 1140 { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; } 1141 else if (strcmp(*argv,"-legacy_renegotiation") == 0) 1142 off|=SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION; 1143 else if (strcmp(*argv,"-cipher") == 0) 1144 { 1145 if (--argc < 1) goto bad; 1146 cipher= *(++argv); 1147 } 1148 else if (strcmp(*argv,"-CAfile") == 0) 1149 { 1150 if (--argc < 1) goto bad; 1151 CAfile= *(++argv); 1152 } 1153#ifdef FIONBIO 1154 else if (strcmp(*argv,"-nbio") == 0) 1155 { s_nbio=1; } 1156#endif 1157 else if (strcmp(*argv,"-nbio_test") == 0) 1158 { 1159#ifdef FIONBIO 1160 s_nbio=1; 1161#endif 1162 s_nbio_test=1; 1163 } 1164 else if (strcmp(*argv,"-debug") == 0) 1165 { s_debug=1; } 1166#ifndef OPENSSL_NO_TLSEXT 1167 else if (strcmp(*argv,"-tlsextdebug") == 0) 1168 s_tlsextdebug=1; 1169 else if (strcmp(*argv,"-status") == 0) 1170 s_tlsextstatus=1; 1171 else if (strcmp(*argv,"-status_verbose") == 0) 1172 { 1173 s_tlsextstatus=1; 1174 tlscstatp.verbose = 1; 1175 } 1176 else if (!strcmp(*argv, "-status_timeout")) 1177 { 1178 s_tlsextstatus=1; 1179 if (--argc < 1) goto bad; 1180 tlscstatp.timeout = atoi(*(++argv)); 1181 } 1182 else if (!strcmp(*argv, "-status_url")) 1183 { 1184 s_tlsextstatus=1; 1185 if (--argc < 1) goto bad; 1186 if (!OCSP_parse_url(*(++argv), 1187 &tlscstatp.host, 1188 &tlscstatp.port, 1189 &tlscstatp.path, 1190 &tlscstatp.use_ssl)) 1191 { 1192 BIO_printf(bio_err, "Error parsing URL\n"); 1193 goto bad; 1194 } 1195 } 1196#endif 1197 else if (strcmp(*argv,"-msg") == 0) 1198 { s_msg=1; } 1199 else if (strcmp(*argv,"-hack") == 0) 1200 { hack=1; } 1201 else if (strcmp(*argv,"-state") == 0) 1202 { state=1; } 1203 else if (strcmp(*argv,"-crlf") == 0) 1204 { s_crlf=1; } 1205 else if (strcmp(*argv,"-quiet") == 0) 1206 { s_quiet=1; } 1207 else if (strcmp(*argv,"-bugs") == 0) 1208 { bugs=1; } 1209 else if (strcmp(*argv,"-no_tmp_rsa") == 0) 1210 { no_tmp_rsa=1; } 1211 else if (strcmp(*argv,"-no_dhe") == 0) 1212 { no_dhe=1; } 1213 else if (strcmp(*argv,"-no_ecdhe") == 0) 1214 { no_ecdhe=1; } 1215#ifndef OPENSSL_NO_PSK 1216 else if (strcmp(*argv,"-psk_hint") == 0) 1217 { 1218 if (--argc < 1) goto bad; 1219 psk_identity_hint= *(++argv); 1220 } 1221 else if (strcmp(*argv,"-psk") == 0) 1222 { 1223 size_t i; 1224 1225 if (--argc < 1) goto bad; 1226 psk_key=*(++argv); 1227 for (i=0; i<strlen(psk_key); i++) 1228 { 1229 if (isxdigit((unsigned char)psk_key[i])) 1230 continue; 1231 BIO_printf(bio_err,"Not a hex number '%s'\n",*argv); 1232 goto bad; 1233 } 1234 } 1235#endif 1236#ifndef OPENSSL_NO_SRP 1237 else if (strcmp(*argv, "-srpvfile") == 0) 1238 { 1239 if (--argc < 1) goto bad; 1240 srp_verifier_file = *(++argv); 1241 meth = TLSv1_server_method(); 1242 } 1243 else if (strcmp(*argv, "-srpuserseed") == 0) 1244 { 1245 if (--argc < 1) goto bad; 1246 srpuserseed = *(++argv); 1247 meth = TLSv1_server_method(); 1248 } 1249#endif 1250 else if (strcmp(*argv,"-www") == 0) 1251 { www=1; } 1252 else if (strcmp(*argv,"-WWW") == 0) 1253 { www=2; } 1254 else if (strcmp(*argv,"-HTTP") == 0) 1255 { www=3; } 1256 else if (strcmp(*argv,"-no_ssl2") == 0) 1257 { off|=SSL_OP_NO_SSLv2; } 1258 else if (strcmp(*argv,"-no_ssl3") == 0) 1259 { off|=SSL_OP_NO_SSLv3; } 1260 else if (strcmp(*argv,"-no_tls1") == 0) 1261 { off|=SSL_OP_NO_TLSv1; } 1262 else if (strcmp(*argv,"-no_tls1_1") == 0) 1263 { off|=SSL_OP_NO_TLSv1_1; } 1264 else if (strcmp(*argv,"-no_tls1_2") == 0) 1265 { off|=SSL_OP_NO_TLSv1_2; } 1266 else if (strcmp(*argv,"-no_comp") == 0) 1267 { off|=SSL_OP_NO_COMPRESSION; } 1268#ifndef OPENSSL_NO_TLSEXT 1269 else if (strcmp(*argv,"-no_ticket") == 0) 1270 { off|=SSL_OP_NO_TICKET; } 1271#endif 1272#ifndef OPENSSL_NO_SSL2 1273 else if (strcmp(*argv,"-ssl2") == 0) 1274 { meth=SSLv2_server_method(); } 1275#endif 1276#ifndef OPENSSL_NO_SSL3_METHOD 1277 else if (strcmp(*argv,"-ssl3") == 0) 1278 { meth=SSLv3_server_method(); } 1279#endif 1280#ifndef OPENSSL_NO_TLS1 1281 else if (strcmp(*argv,"-tls1") == 0) 1282 { meth=TLSv1_server_method(); } 1283 else if (strcmp(*argv,"-tls1_1") == 0) 1284 { meth=TLSv1_1_server_method(); } 1285 else if (strcmp(*argv,"-tls1_2") == 0) 1286 { meth=TLSv1_2_server_method(); } 1287#endif 1288#ifndef OPENSSL_NO_DTLS1 1289 else if (strcmp(*argv,"-dtls1") == 0) 1290 { 1291 meth=DTLSv1_server_method(); 1292 socket_type = SOCK_DGRAM; 1293 } 1294 else if (strcmp(*argv,"-timeout") == 0) 1295 enable_timeouts = 1; 1296 else if (strcmp(*argv,"-mtu") == 0) 1297 { 1298 if (--argc < 1) goto bad; 1299 socket_mtu = atol(*(++argv)); 1300 } 1301 else if (strcmp(*argv, "-chain") == 0) 1302 cert_chain = 1; 1303#endif 1304 else if (strcmp(*argv, "-id_prefix") == 0) 1305 { 1306 if (--argc < 1) goto bad; 1307 session_id_prefix = *(++argv); 1308 } 1309#ifndef OPENSSL_NO_ENGINE 1310 else if (strcmp(*argv,"-engine") == 0) 1311 { 1312 if (--argc < 1) goto bad; 1313 engine_id= *(++argv); 1314 } 1315#endif 1316 else if (strcmp(*argv,"-rand") == 0) 1317 { 1318 if (--argc < 1) goto bad; 1319 inrand= *(++argv); 1320 } 1321#ifndef OPENSSL_NO_TLSEXT 1322 else if (strcmp(*argv,"-servername") == 0) 1323 { 1324 if (--argc < 1) goto bad; 1325 tlsextcbp.servername= *(++argv); 1326 } 1327 else if (strcmp(*argv,"-servername_fatal") == 0) 1328 { tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL; } 1329 else if (strcmp(*argv,"-cert2") == 0) 1330 { 1331 if (--argc < 1) goto bad; 1332 s_cert_file2= *(++argv); 1333 } 1334 else if (strcmp(*argv,"-key2") == 0) 1335 { 1336 if (--argc < 1) goto bad; 1337 s_key_file2= *(++argv); 1338 } 1339# ifndef OPENSSL_NO_NEXTPROTONEG 1340 else if (strcmp(*argv,"-nextprotoneg") == 0) 1341 { 1342 if (--argc < 1) goto bad; 1343 next_proto_neg_in = *(++argv); 1344 } 1345# endif 1346#endif 1347#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK) 1348 else if (strcmp(*argv,"-jpake") == 0) 1349 { 1350 if (--argc < 1) goto bad; 1351 jpake_secret = *(++argv); 1352 } 1353#endif 1354#ifndef OPENSSL_NO_SRTP 1355 else if (strcmp(*argv,"-use_srtp") == 0) 1356 { 1357 if (--argc < 1) goto bad; 1358 srtp_profiles = *(++argv); 1359 } 1360#endif 1361 else if (strcmp(*argv,"-keymatexport") == 0) 1362 { 1363 if (--argc < 1) goto bad; 1364 keymatexportlabel= *(++argv); 1365 } 1366 else if (strcmp(*argv,"-keymatexportlen") == 0) 1367 { 1368 if (--argc < 1) goto bad; 1369 keymatexportlen=atoi(*(++argv)); 1370 if (keymatexportlen == 0) goto bad; 1371 } 1372 else 1373 { 1374 BIO_printf(bio_err,"unknown option %s\n",*argv); 1375 badop=1; 1376 break; 1377 } 1378 argc--; 1379 argv++; 1380 } 1381 if (badop) 1382 { 1383bad: 1384 sv_usage(); 1385 goto end; 1386 } 1387#ifndef OPENSSL_NO_DTLS1 1388 if (www && socket_type == SOCK_DGRAM) 1389 { 1390 BIO_printf(bio_err, 1391 "Can't use -HTTP, -www or -WWW with DTLS\n"); 1392 goto end; 1393 } 1394#endif 1395 1396#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK) 1397 if (jpake_secret) 1398 { 1399 if (psk_key) 1400 { 1401 BIO_printf(bio_err, 1402 "Can't use JPAKE and PSK together\n"); 1403 goto end; 1404 } 1405 psk_identity = "JPAKE"; 1406 if (cipher) 1407 { 1408 BIO_printf(bio_err, "JPAKE sets cipher to PSK\n"); 1409 goto end; 1410 } 1411 cipher = "PSK"; 1412 } 1413 1414#endif 1415 1416 SSL_load_error_strings(); 1417 OpenSSL_add_ssl_algorithms(); 1418 1419#ifndef OPENSSL_NO_ENGINE 1420 e = setup_engine(bio_err, engine_id, 1); 1421#endif 1422 1423 if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) 1424 { 1425 BIO_printf(bio_err, "Error getting password\n"); 1426 goto end; 1427 } 1428 1429 1430 if (s_key_file == NULL) 1431 s_key_file = s_cert_file; 1432#ifndef OPENSSL_NO_TLSEXT 1433 if (s_key_file2 == NULL) 1434 s_key_file2 = s_cert_file2; 1435#endif 1436 1437 if (nocert == 0) 1438 { 1439 s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e, 1440 "server certificate private key file"); 1441 if (!s_key) 1442 { 1443 ERR_print_errors(bio_err); 1444 goto end; 1445 } 1446 1447 s_cert = load_cert(bio_err,s_cert_file,s_cert_format, 1448 NULL, e, "server certificate file"); 1449 1450 if (!s_cert) 1451 { 1452 ERR_print_errors(bio_err); 1453 goto end; 1454 } 1455 1456#ifndef OPENSSL_NO_TLSEXT 1457 if (tlsextcbp.servername) 1458 { 1459 s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e, 1460 "second server certificate private key file"); 1461 if (!s_key2) 1462 { 1463 ERR_print_errors(bio_err); 1464 goto end; 1465 } 1466 1467 s_cert2 = load_cert(bio_err,s_cert_file2,s_cert_format, 1468 NULL, e, "second server certificate file"); 1469 1470 if (!s_cert2) 1471 { 1472 ERR_print_errors(bio_err); 1473 goto end; 1474 } 1475 } 1476#endif 1477 } 1478 1479#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 1480 if (next_proto_neg_in) 1481 { 1482 unsigned short len; 1483 next_proto.data = next_protos_parse(&len, next_proto_neg_in); 1484 if (next_proto.data == NULL) 1485 goto end; 1486 next_proto.len = len; 1487 } 1488 else 1489 { 1490 next_proto.data = NULL; 1491 } 1492#endif 1493 1494 1495 if (s_dcert_file) 1496 { 1497 1498 if (s_dkey_file == NULL) 1499 s_dkey_file = s_dcert_file; 1500 1501 s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format, 1502 0, dpass, e, 1503 "second certificate private key file"); 1504 if (!s_dkey) 1505 { 1506 ERR_print_errors(bio_err); 1507 goto end; 1508 } 1509 1510 s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format, 1511 NULL, e, "second server certificate file"); 1512 1513 if (!s_dcert) 1514 { 1515 ERR_print_errors(bio_err); 1516 goto end; 1517 } 1518 1519 } 1520 1521 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL 1522 && !RAND_status()) 1523 { 1524 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); 1525 } 1526 if (inrand != NULL) 1527 BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 1528 app_RAND_load_files(inrand)); 1529 1530 if (bio_s_out == NULL) 1531 { 1532 if (s_quiet && !s_debug && !s_msg) 1533 { 1534 bio_s_out=BIO_new(BIO_s_null()); 1535 } 1536 else 1537 { 1538 if (bio_s_out == NULL) 1539 bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE); 1540 } 1541 } 1542 1543#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA) 1544 if (nocert) 1545#endif 1546 { 1547 s_cert_file=NULL; 1548 s_key_file=NULL; 1549 s_dcert_file=NULL; 1550 s_dkey_file=NULL; 1551#ifndef OPENSSL_NO_TLSEXT 1552 s_cert_file2=NULL; 1553 s_key_file2=NULL; 1554#endif 1555 } 1556 1557 ctx=SSL_CTX_new(meth); 1558 if (ctx == NULL) 1559 { 1560 ERR_print_errors(bio_err); 1561 goto end; 1562 } 1563 if (session_id_prefix) 1564 { 1565 if(strlen(session_id_prefix) >= 32) 1566 BIO_printf(bio_err, 1567"warning: id_prefix is too long, only one new session will be possible\n"); 1568 else if(strlen(session_id_prefix) >= 16) 1569 BIO_printf(bio_err, 1570"warning: id_prefix is too long if you use SSLv2\n"); 1571 if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) 1572 { 1573 BIO_printf(bio_err,"error setting 'id_prefix'\n"); 1574 ERR_print_errors(bio_err); 1575 goto end; 1576 } 1577 BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix); 1578 } 1579 SSL_CTX_set_quiet_shutdown(ctx,1); 1580 if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL); 1581 if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); 1582 SSL_CTX_set_options(ctx,off); 1583 /* DTLS: partial reads end up discarding unread UDP bytes :-( 1584 * Setting read ahead solves this problem. 1585 */ 1586 if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1); 1587 1588 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); 1589 if (no_cache) 1590 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); 1591 else 1592 SSL_CTX_sess_set_cache_size(ctx,128); 1593 1594#ifndef OPENSSL_NO_SRTP 1595 if (srtp_profiles != NULL) 1596 SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles); 1597#endif 1598 1599#if 0 1600 if (cipher == NULL) cipher=getenv("SSL_CIPHER"); 1601#endif 1602 1603#if 0 1604 if (s_cert_file == NULL) 1605 { 1606 BIO_printf(bio_err,"You must specify a certificate file for the server to use\n"); 1607 goto end; 1608 } 1609#endif 1610 1611 if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) || 1612 (!SSL_CTX_set_default_verify_paths(ctx))) 1613 { 1614 /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */ 1615 ERR_print_errors(bio_err); 1616 /* goto end; */ 1617 } 1618 if (vpm) 1619 SSL_CTX_set1_param(ctx, vpm); 1620 1621#ifndef OPENSSL_NO_TLSEXT 1622 if (s_cert2) 1623 { 1624 ctx2=SSL_CTX_new(meth); 1625 if (ctx2 == NULL) 1626 { 1627 ERR_print_errors(bio_err); 1628 goto end; 1629 } 1630 } 1631 1632 if (ctx2) 1633 { 1634 BIO_printf(bio_s_out,"Setting secondary ctx parameters\n"); 1635 1636 if (session_id_prefix) 1637 { 1638 if(strlen(session_id_prefix) >= 32) 1639 BIO_printf(bio_err, 1640 "warning: id_prefix is too long, only one new session will be possible\n"); 1641 else if(strlen(session_id_prefix) >= 16) 1642 BIO_printf(bio_err, 1643 "warning: id_prefix is too long if you use SSLv2\n"); 1644 if(!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) 1645 { 1646 BIO_printf(bio_err,"error setting 'id_prefix'\n"); 1647 ERR_print_errors(bio_err); 1648 goto end; 1649 } 1650 BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix); 1651 } 1652 SSL_CTX_set_quiet_shutdown(ctx2,1); 1653 if (bugs) SSL_CTX_set_options(ctx2,SSL_OP_ALL); 1654 if (hack) SSL_CTX_set_options(ctx2,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); 1655 SSL_CTX_set_options(ctx2,off); 1656 /* DTLS: partial reads end up discarding unread UDP bytes :-( 1657 * Setting read ahead solves this problem. 1658 */ 1659 if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx2, 1); 1660 1661 if (state) SSL_CTX_set_info_callback(ctx2,apps_ssl_info_callback); 1662 1663 if (no_cache) 1664 SSL_CTX_set_session_cache_mode(ctx2,SSL_SESS_CACHE_OFF); 1665 else 1666 SSL_CTX_sess_set_cache_size(ctx2,128); 1667 1668 if ((!SSL_CTX_load_verify_locations(ctx2,CAfile,CApath)) || 1669 (!SSL_CTX_set_default_verify_paths(ctx2))) 1670 { 1671 ERR_print_errors(bio_err); 1672 } 1673 if (vpm) 1674 SSL_CTX_set1_param(ctx2, vpm); 1675 } 1676 1677# ifndef OPENSSL_NO_NEXTPROTONEG 1678 if (next_proto.data) 1679 SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, &next_proto); 1680# endif 1681#endif 1682 1683#ifndef OPENSSL_NO_DH 1684 if (!no_dhe) { 1685 DH *dh = NULL; 1686 1687 if (dhfile) 1688 dh = load_dh_param(dhfile); 1689 else if (s_cert_file) 1690 dh = load_dh_param(s_cert_file); 1691 1692 if (dh != NULL) { 1693 BIO_printf(bio_s_out, "Setting temp DH parameters\n"); 1694 } else { 1695 BIO_printf(bio_s_out, "Using default temp DH parameters\n"); 1696 dh = get_dh2048(); 1697 if (dh == NULL) { 1698 ERR_print_errors(bio_err); 1699 goto end; 1700 } 1701 } 1702 (void)BIO_flush(bio_s_out); 1703 1704 SSL_CTX_set_tmp_dh(ctx, dh); 1705# ifndef OPENSSL_NO_TLSEXT 1706 if (ctx2) { 1707 if (!dhfile) { 1708 DH *dh2 = load_dh_param(s_cert_file2); 1709 if (dh2 != NULL) { 1710 BIO_printf(bio_s_out, "Setting temp DH parameters\n"); 1711 (void)BIO_flush(bio_s_out); 1712 1713 DH_free(dh); 1714 dh = dh2; 1715 } 1716 } 1717 SSL_CTX_set_tmp_dh(ctx2, dh); 1718 } 1719# endif 1720 DH_free(dh); 1721 } 1722#endif 1723 1724#ifndef OPENSSL_NO_ECDH 1725 if (!no_ecdhe) 1726 { 1727 EC_KEY *ecdh=NULL; 1728 1729 if (named_curve) 1730 { 1731 int nid = OBJ_sn2nid(named_curve); 1732 1733 if (nid == 0) 1734 { 1735 BIO_printf(bio_err, "unknown curve name (%s)\n", 1736 named_curve); 1737 goto end; 1738 } 1739 ecdh = EC_KEY_new_by_curve_name(nid); 1740 if (ecdh == NULL) 1741 { 1742 BIO_printf(bio_err, "unable to create curve (%s)\n", 1743 named_curve); 1744 goto end; 1745 } 1746 } 1747 1748 if (ecdh != NULL) 1749 { 1750 BIO_printf(bio_s_out,"Setting temp ECDH parameters\n"); 1751 } 1752 else 1753 { 1754 BIO_printf(bio_s_out,"Using default temp ECDH parameters\n"); 1755 ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); 1756 if (ecdh == NULL) 1757 { 1758 BIO_printf(bio_err, "unable to create curve (nistp256)\n"); 1759 goto end; 1760 } 1761 } 1762 (void)BIO_flush(bio_s_out); 1763 1764 SSL_CTX_set_tmp_ecdh(ctx,ecdh); 1765#ifndef OPENSSL_NO_TLSEXT 1766 if (ctx2) 1767 SSL_CTX_set_tmp_ecdh(ctx2,ecdh); 1768#endif 1769 EC_KEY_free(ecdh); 1770 } 1771#endif 1772 1773 if (!set_cert_key_stuff(ctx, s_cert, s_key)) 1774 goto end; 1775#ifndef OPENSSL_NO_TLSEXT 1776 if (ctx2 && !set_cert_key_stuff(ctx2,s_cert2,s_key2)) 1777 goto end; 1778#endif 1779 if (s_dcert != NULL) 1780 { 1781 if (!set_cert_key_stuff(ctx, s_dcert, s_dkey)) 1782 goto end; 1783 } 1784 1785#ifndef OPENSSL_NO_RSA 1786#if 1 1787 if (!no_tmp_rsa) 1788 { 1789 SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb); 1790#ifndef OPENSSL_NO_TLSEXT 1791 if (ctx2) 1792 SSL_CTX_set_tmp_rsa_callback(ctx2,tmp_rsa_cb); 1793#endif 1794 } 1795#else 1796 if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx)) 1797 { 1798 RSA *rsa; 1799 1800 BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key..."); 1801 BIO_flush(bio_s_out); 1802 1803 rsa=RSA_generate_key(512,RSA_F4,NULL); 1804 1805 if (!SSL_CTX_set_tmp_rsa(ctx,rsa)) 1806 { 1807 ERR_print_errors(bio_err); 1808 goto end; 1809 } 1810#ifndef OPENSSL_NO_TLSEXT 1811 if (ctx2) 1812 { 1813 if (!SSL_CTX_set_tmp_rsa(ctx2,rsa)) 1814 { 1815 ERR_print_errors(bio_err); 1816 goto end; 1817 } 1818 } 1819#endif 1820 RSA_free(rsa); 1821 BIO_printf(bio_s_out,"\n"); 1822 } 1823#endif 1824#endif 1825 1826#ifndef OPENSSL_NO_PSK 1827#ifdef OPENSSL_NO_JPAKE 1828 if (psk_key != NULL) 1829#else 1830 if (psk_key != NULL || jpake_secret) 1831#endif 1832 { 1833 if (s_debug) 1834 BIO_printf(bio_s_out, "PSK key given or JPAKE in use, setting server callback\n"); 1835 SSL_CTX_set_psk_server_callback(ctx, psk_server_cb); 1836 } 1837 1838 if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) 1839 { 1840 BIO_printf(bio_err,"error setting PSK identity hint to context\n"); 1841 ERR_print_errors(bio_err); 1842 goto end; 1843 } 1844#endif 1845 1846 if (cipher != NULL) 1847 { 1848 if(!SSL_CTX_set_cipher_list(ctx,cipher)) 1849 { 1850 BIO_printf(bio_err,"error setting cipher list\n"); 1851 ERR_print_errors(bio_err); 1852 goto end; 1853 } 1854#ifndef OPENSSL_NO_TLSEXT 1855 if (ctx2 && !SSL_CTX_set_cipher_list(ctx2,cipher)) 1856 { 1857 BIO_printf(bio_err,"error setting cipher list\n"); 1858 ERR_print_errors(bio_err); 1859 goto end; 1860 } 1861#endif 1862 } 1863 SSL_CTX_set_verify(ctx,s_server_verify,verify_callback); 1864 SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context, 1865 sizeof s_server_session_id_context); 1866 1867 /* Set DTLS cookie generation and verification callbacks */ 1868 SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback); 1869 SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback); 1870 1871#ifndef OPENSSL_NO_TLSEXT 1872 if (ctx2) 1873 { 1874 SSL_CTX_set_verify(ctx2,s_server_verify,verify_callback); 1875 SSL_CTX_set_session_id_context(ctx2,(void*)&s_server_session_id_context, 1876 sizeof s_server_session_id_context); 1877 1878 tlsextcbp.biodebug = bio_s_out; 1879 SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb); 1880 SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp); 1881 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb); 1882 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp); 1883 } 1884#endif 1885 1886#ifndef OPENSSL_NO_SRP 1887 if (srp_verifier_file != NULL) 1888 { 1889 srp_callback_parm.vb = SRP_VBASE_new(srpuserseed); 1890 srp_callback_parm.user = NULL; 1891 srp_callback_parm.login = NULL; 1892 if ((ret = SRP_VBASE_init(srp_callback_parm.vb, srp_verifier_file)) != SRP_NO_ERROR) 1893 { 1894 BIO_printf(bio_err, 1895 "Cannot initialize SRP verifier file \"%s\":ret=%d\n", 1896 srp_verifier_file, ret); 1897 goto end; 1898 } 1899 SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE,verify_callback); 1900 SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm); 1901 SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb); 1902 } 1903 else 1904#endif 1905 if (CAfile != NULL) 1906 { 1907 SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile)); 1908#ifndef OPENSSL_NO_TLSEXT 1909 if (ctx2) 1910 SSL_CTX_set_client_CA_list(ctx2,SSL_load_client_CA_file(CAfile)); 1911#endif 1912 } 1913 1914 BIO_printf(bio_s_out,"ACCEPT\n"); 1915 (void)BIO_flush(bio_s_out); 1916 if (www) 1917 do_server(port,socket_type,&accept_socket,www_body, context); 1918 else 1919 do_server(port,socket_type,&accept_socket,sv_body, context); 1920 print_stats(bio_s_out,ctx); 1921 ret=0; 1922end: 1923 if (ctx != NULL) SSL_CTX_free(ctx); 1924 if (s_cert) 1925 X509_free(s_cert); 1926 if (s_dcert) 1927 X509_free(s_dcert); 1928 if (s_key) 1929 EVP_PKEY_free(s_key); 1930 if (s_dkey) 1931 EVP_PKEY_free(s_dkey); 1932 if (pass) 1933 OPENSSL_free(pass); 1934 if (dpass) 1935 OPENSSL_free(dpass); 1936 if (vpm) 1937 X509_VERIFY_PARAM_free(vpm); 1938#ifndef OPENSSL_NO_TLSEXT 1939 if (tlscstatp.host) 1940 OPENSSL_free(tlscstatp.host); 1941 if (tlscstatp.port) 1942 OPENSSL_free(tlscstatp.port); 1943 if (tlscstatp.path) 1944 OPENSSL_free(tlscstatp.path); 1945 if (ctx2 != NULL) SSL_CTX_free(ctx2); 1946 if (s_cert2) 1947 X509_free(s_cert2); 1948 if (s_key2) 1949 EVP_PKEY_free(s_key2); 1950#endif 1951 if (bio_s_out != NULL) 1952 { 1953 BIO_free(bio_s_out); 1954 bio_s_out=NULL; 1955 } 1956 apps_shutdown(); 1957 OPENSSL_EXIT(ret); 1958 } 1959 1960static void print_stats(BIO *bio, SSL_CTX *ssl_ctx) 1961 { 1962 BIO_printf(bio,"%4ld items in the session cache\n", 1963 SSL_CTX_sess_number(ssl_ctx)); 1964 BIO_printf(bio,"%4ld client connects (SSL_connect())\n", 1965 SSL_CTX_sess_connect(ssl_ctx)); 1966 BIO_printf(bio,"%4ld client renegotiates (SSL_connect())\n", 1967 SSL_CTX_sess_connect_renegotiate(ssl_ctx)); 1968 BIO_printf(bio,"%4ld client connects that finished\n", 1969 SSL_CTX_sess_connect_good(ssl_ctx)); 1970 BIO_printf(bio,"%4ld server accepts (SSL_accept())\n", 1971 SSL_CTX_sess_accept(ssl_ctx)); 1972 BIO_printf(bio,"%4ld server renegotiates (SSL_accept())\n", 1973 SSL_CTX_sess_accept_renegotiate(ssl_ctx)); 1974 BIO_printf(bio,"%4ld server accepts that finished\n", 1975 SSL_CTX_sess_accept_good(ssl_ctx)); 1976 BIO_printf(bio,"%4ld session cache hits\n",SSL_CTX_sess_hits(ssl_ctx)); 1977 BIO_printf(bio,"%4ld session cache misses\n",SSL_CTX_sess_misses(ssl_ctx)); 1978 BIO_printf(bio,"%4ld session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx)); 1979 BIO_printf(bio,"%4ld callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx)); 1980 BIO_printf(bio,"%4ld cache full overflows (%ld allowed)\n", 1981 SSL_CTX_sess_cache_full(ssl_ctx), 1982 SSL_CTX_sess_get_cache_size(ssl_ctx)); 1983 } 1984 1985static int sv_body(char *hostname, int s, unsigned char *context) 1986 { 1987 char *buf=NULL; 1988 fd_set readfds; 1989 int ret=1,width; 1990 int k,i; 1991 unsigned long l; 1992 SSL *con=NULL; 1993 BIO *sbio; 1994#ifndef OPENSSL_NO_KRB5 1995 KSSL_CTX *kctx; 1996#endif 1997 struct timeval timeout; 1998#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5) 1999 struct timeval tv; 2000#else 2001 struct timeval *timeoutp; 2002#endif 2003 2004 if ((buf=OPENSSL_malloc(bufsize)) == NULL) 2005 { 2006 BIO_printf(bio_err,"out of memory\n"); 2007 goto err; 2008 } 2009#ifdef FIONBIO 2010 if (s_nbio) 2011 { 2012 unsigned long sl=1; 2013 2014 if (!s_quiet) 2015 BIO_printf(bio_err,"turning on non blocking io\n"); 2016 if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0) 2017 ERR_print_errors(bio_err); 2018 } 2019#endif 2020 2021 if (con == NULL) { 2022 con=SSL_new(ctx); 2023#ifndef OPENSSL_NO_TLSEXT 2024 if (s_tlsextdebug) 2025 { 2026 SSL_set_tlsext_debug_callback(con, tlsext_cb); 2027 SSL_set_tlsext_debug_arg(con, bio_s_out); 2028 } 2029 if (s_tlsextstatus) 2030 { 2031 SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb); 2032 tlscstatp.err = bio_err; 2033 SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp); 2034 } 2035#endif 2036#ifndef OPENSSL_NO_KRB5 2037 if ((kctx = kssl_ctx_new()) != NULL) 2038 { 2039 SSL_set0_kssl_ctx(con, kctx); 2040 kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC); 2041 kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB); 2042 } 2043#endif /* OPENSSL_NO_KRB5 */ 2044 if(context) 2045 SSL_set_session_id_context(con, context, 2046 strlen((char *)context)); 2047 } 2048 SSL_clear(con); 2049#if 0 2050#ifdef TLSEXT_TYPE_opaque_prf_input 2051 SSL_set_tlsext_opaque_prf_input(con, "Test server", 11); 2052#endif 2053#endif 2054 2055 if (SSL_version(con) == DTLS1_VERSION) 2056 { 2057 2058 sbio=BIO_new_dgram(s,BIO_NOCLOSE); 2059 2060 if (enable_timeouts) 2061 { 2062 timeout.tv_sec = 0; 2063 timeout.tv_usec = DGRAM_RCV_TIMEOUT; 2064 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); 2065 2066 timeout.tv_sec = 0; 2067 timeout.tv_usec = DGRAM_SND_TIMEOUT; 2068 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); 2069 } 2070 2071 if (socket_mtu) 2072 { 2073 if(socket_mtu < DTLS_get_link_min_mtu(con)) 2074 { 2075 BIO_printf(bio_err,"MTU too small. Must be at least %ld\n", 2076 DTLS_get_link_min_mtu(con)); 2077 ret = -1; 2078 BIO_free(sbio); 2079 goto err; 2080 } 2081 SSL_set_options(con, SSL_OP_NO_QUERY_MTU); 2082 if(!DTLS_set_link_mtu(con, socket_mtu)) 2083 { 2084 BIO_printf(bio_err, "Failed to set MTU\n"); 2085 ret = -1; 2086 BIO_free(sbio); 2087 goto err; 2088 } 2089 } 2090 else 2091 /* want to do MTU discovery */ 2092 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); 2093 2094 /* turn on cookie exchange */ 2095 SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE); 2096 } 2097 else 2098 sbio=BIO_new_socket(s,BIO_NOCLOSE); 2099 2100 if (s_nbio_test) 2101 { 2102 BIO *test; 2103 2104 test=BIO_new(BIO_f_nbio_test()); 2105 sbio=BIO_push(test,sbio); 2106 } 2107#ifndef OPENSSL_NO_JPAKE 2108 if(jpake_secret) 2109 jpake_server_auth(bio_s_out, sbio, jpake_secret); 2110#endif 2111 2112 SSL_set_bio(con,sbio,sbio); 2113 SSL_set_accept_state(con); 2114 /* SSL_set_fd(con,s); */ 2115 2116 if (s_debug) 2117 { 2118 SSL_set_debug(con, 1); 2119 BIO_set_callback(SSL_get_rbio(con),bio_dump_callback); 2120 BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out); 2121 } 2122 if (s_msg) 2123 { 2124 SSL_set_msg_callback(con, msg_cb); 2125 SSL_set_msg_callback_arg(con, bio_s_out); 2126 } 2127#ifndef OPENSSL_NO_TLSEXT 2128 if (s_tlsextdebug) 2129 { 2130 SSL_set_tlsext_debug_callback(con, tlsext_cb); 2131 SSL_set_tlsext_debug_arg(con, bio_s_out); 2132 } 2133#endif 2134 2135 width=s+1; 2136 for (;;) 2137 { 2138 int read_from_terminal; 2139 int read_from_sslcon; 2140 2141 read_from_terminal = 0; 2142 read_from_sslcon = SSL_pending(con); 2143 2144 if (!read_from_sslcon) 2145 { 2146 FD_ZERO(&readfds); 2147#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5) 2148 openssl_fdset(fileno(stdin),&readfds); 2149#endif 2150 openssl_fdset(s,&readfds); 2151 /* Note: under VMS with SOCKETSHR the second parameter is 2152 * currently of type (int *) whereas under other systems 2153 * it is (void *) if you don't have a cast it will choke 2154 * the compiler: if you do have a cast then you can either 2155 * go for (int *) or (void *). 2156 */ 2157#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) 2158 /* Under DOS (non-djgpp) and Windows we can't select on stdin: only 2159 * on sockets. As a workaround we timeout the select every 2160 * second and check for any keypress. In a proper Windows 2161 * application we wouldn't do this because it is inefficient. 2162 */ 2163 tv.tv_sec = 1; 2164 tv.tv_usec = 0; 2165 i=select(width,(void *)&readfds,NULL,NULL,&tv); 2166 if((i < 0) || (!i && !_kbhit() ) )continue; 2167 if(_kbhit()) 2168 read_from_terminal = 1; 2169#elif defined(OPENSSL_SYS_BEOS_R5) 2170 /* Under BeOS-R5 the situation is similar to DOS */ 2171 tv.tv_sec = 1; 2172 tv.tv_usec = 0; 2173 (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); 2174 i=select(width,(void *)&readfds,NULL,NULL,&tv); 2175 if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0)) 2176 continue; 2177 if (read(fileno(stdin), buf, 0) >= 0) 2178 read_from_terminal = 1; 2179 (void)fcntl(fileno(stdin), F_SETFL, 0); 2180#else 2181 if ((SSL_version(con) == DTLS1_VERSION) && 2182 DTLSv1_get_timeout(con, &timeout)) 2183 timeoutp = &timeout; 2184 else 2185 timeoutp = NULL; 2186 2187 i=select(width,(void *)&readfds,NULL,NULL,timeoutp); 2188 2189 if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) 2190 { 2191 BIO_printf(bio_err,"TIMEOUT occured\n"); 2192 } 2193 2194 if (i <= 0) continue; 2195 if (FD_ISSET(fileno(stdin),&readfds)) 2196 read_from_terminal = 1; 2197#endif 2198 if (FD_ISSET(s,&readfds)) 2199 read_from_sslcon = 1; 2200 } 2201 if (read_from_terminal) 2202 { 2203 if (s_crlf) 2204 { 2205 int j, lf_num; 2206 2207 i=raw_read_stdin(buf, bufsize/2); 2208 lf_num = 0; 2209 /* both loops are skipped when i <= 0 */ 2210 for (j = 0; j < i; j++) 2211 if (buf[j] == '\n') 2212 lf_num++; 2213 for (j = i-1; j >= 0; j--) 2214 { 2215 buf[j+lf_num] = buf[j]; 2216 if (buf[j] == '\n') 2217 { 2218 lf_num--; 2219 i++; 2220 buf[j+lf_num] = '\r'; 2221 } 2222 } 2223 assert(lf_num == 0); 2224 } 2225 else 2226 i=raw_read_stdin(buf,bufsize); 2227 if (!s_quiet) 2228 { 2229 if ((i <= 0) || (buf[0] == 'Q')) 2230 { 2231 BIO_printf(bio_s_out,"DONE\n"); 2232 SHUTDOWN(s); 2233 close_accept_socket(); 2234 ret= -11; 2235 goto err; 2236 } 2237 if ((i <= 0) || (buf[0] == 'q')) 2238 { 2239 BIO_printf(bio_s_out,"DONE\n"); 2240 if (SSL_version(con) != DTLS1_VERSION) 2241 SHUTDOWN(s); 2242 /* close_accept_socket(); 2243 ret= -11;*/ 2244 goto err; 2245 } 2246 2247#ifndef OPENSSL_NO_HEARTBEATS 2248 if ((buf[0] == 'B') && 2249 ((buf[1] == '\n') || (buf[1] == '\r'))) 2250 { 2251 BIO_printf(bio_err,"HEARTBEATING\n"); 2252 SSL_heartbeat(con); 2253 i=0; 2254 continue; 2255 } 2256#endif 2257 if ((buf[0] == 'r') && 2258 ((buf[1] == '\n') || (buf[1] == '\r'))) 2259 { 2260 SSL_renegotiate(con); 2261 i=SSL_do_handshake(con); 2262 printf("SSL_do_handshake -> %d\n",i); 2263 i=0; /*13; */ 2264 continue; 2265 /* strcpy(buf,"server side RE-NEGOTIATE\n"); */ 2266 } 2267 if ((buf[0] == 'R') && 2268 ((buf[1] == '\n') || (buf[1] == '\r'))) 2269 { 2270 SSL_set_verify(con, 2271 SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL); 2272 SSL_renegotiate(con); 2273 i=SSL_do_handshake(con); 2274 printf("SSL_do_handshake -> %d\n",i); 2275 i=0; /* 13; */ 2276 continue; 2277 /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */ 2278 } 2279 if (buf[0] == 'P') 2280 { 2281 static const char *str="Lets print some clear text\n"; 2282 BIO_write(SSL_get_wbio(con),str,strlen(str)); 2283 } 2284 if (buf[0] == 'S') 2285 { 2286 print_stats(bio_s_out,SSL_get_SSL_CTX(con)); 2287 } 2288 } 2289#ifdef CHARSET_EBCDIC 2290 ebcdic2ascii(buf,buf,i); 2291#endif 2292 l=k=0; 2293 for (;;) 2294 { 2295 /* should do a select for the write */ 2296#ifdef RENEG 2297{ static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } } 2298#endif 2299 k=SSL_write(con,&(buf[l]),(unsigned int)i); 2300#ifndef OPENSSL_NO_SRP 2301 while (SSL_get_error(con,k) == SSL_ERROR_WANT_X509_LOOKUP) 2302 { 2303 BIO_printf(bio_s_out,"LOOKUP renego during write\n"); 2304 srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login); 2305 if (srp_callback_parm.user) 2306 BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info); 2307 else 2308 BIO_printf(bio_s_out,"LOOKUP not successful\n"); 2309 k=SSL_write(con,&(buf[l]),(unsigned int)i); 2310 } 2311#endif 2312 switch (SSL_get_error(con,k)) 2313 { 2314 case SSL_ERROR_NONE: 2315 break; 2316 case SSL_ERROR_WANT_WRITE: 2317 case SSL_ERROR_WANT_READ: 2318 case SSL_ERROR_WANT_X509_LOOKUP: 2319 BIO_printf(bio_s_out,"Write BLOCK\n"); 2320 break; 2321 case SSL_ERROR_SYSCALL: 2322 case SSL_ERROR_SSL: 2323 BIO_printf(bio_s_out,"ERROR\n"); 2324 ERR_print_errors(bio_err); 2325 ret=1; 2326 goto err; 2327 /* break; */ 2328 case SSL_ERROR_ZERO_RETURN: 2329 BIO_printf(bio_s_out,"DONE\n"); 2330 ret=1; 2331 goto err; 2332 } 2333 l+=k; 2334 i-=k; 2335 if (i <= 0) break; 2336 } 2337 } 2338 if (read_from_sslcon) 2339 { 2340 if (!SSL_is_init_finished(con)) 2341 { 2342 i=init_ssl_connection(con); 2343 2344 if (i < 0) 2345 { 2346 ret=0; 2347 goto err; 2348 } 2349 else if (i == 0) 2350 { 2351 ret=1; 2352 goto err; 2353 } 2354 } 2355 else 2356 { 2357again: 2358 i=SSL_read(con,(char *)buf,bufsize); 2359#ifndef OPENSSL_NO_SRP 2360 while (SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP) 2361 { 2362 BIO_printf(bio_s_out,"LOOKUP renego during read\n"); 2363 srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login); 2364 if (srp_callback_parm.user) 2365 BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info); 2366 else 2367 BIO_printf(bio_s_out,"LOOKUP not successful\n"); 2368 i=SSL_read(con,(char *)buf,bufsize); 2369 } 2370#endif 2371 switch (SSL_get_error(con,i)) 2372 { 2373 case SSL_ERROR_NONE: 2374#ifdef CHARSET_EBCDIC 2375 ascii2ebcdic(buf,buf,i); 2376#endif 2377 raw_write_stdout(buf, 2378 (unsigned int)i); 2379 if (SSL_pending(con)) goto again; 2380 break; 2381 case SSL_ERROR_WANT_WRITE: 2382 case SSL_ERROR_WANT_READ: 2383 BIO_printf(bio_s_out,"Read BLOCK\n"); 2384 break; 2385 case SSL_ERROR_SYSCALL: 2386 case SSL_ERROR_SSL: 2387 BIO_printf(bio_s_out,"ERROR\n"); 2388 ERR_print_errors(bio_err); 2389 ret=1; 2390 goto err; 2391 case SSL_ERROR_ZERO_RETURN: 2392 BIO_printf(bio_s_out,"DONE\n"); 2393 ret=1; 2394 goto err; 2395 } 2396 } 2397 } 2398 } 2399err: 2400 if (con != NULL) 2401 { 2402 BIO_printf(bio_s_out,"shutting down SSL\n"); 2403#if 1 2404 SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 2405#else 2406 SSL_shutdown(con); 2407#endif 2408 SSL_free(con); 2409 } 2410 BIO_printf(bio_s_out,"CONNECTION CLOSED\n"); 2411 if (buf != NULL) 2412 { 2413 OPENSSL_cleanse(buf,bufsize); 2414 OPENSSL_free(buf); 2415 } 2416 if (ret >= 0) 2417 BIO_printf(bio_s_out,"ACCEPT\n"); 2418 return(ret); 2419 } 2420 2421static void close_accept_socket(void) 2422 { 2423 BIO_printf(bio_err,"shutdown accept socket\n"); 2424 if (accept_socket >= 0) 2425 { 2426 SHUTDOWN2(accept_socket); 2427 } 2428 } 2429 2430static int init_ssl_connection(SSL *con) 2431 { 2432 int i; 2433 const char *str; 2434 X509 *peer; 2435 long verify_error; 2436 MS_STATIC char buf[BUFSIZ]; 2437#ifndef OPENSSL_NO_KRB5 2438 char *client_princ; 2439#endif 2440#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 2441 const unsigned char *next_proto_neg; 2442 unsigned next_proto_neg_len; 2443#endif 2444 unsigned char *exportedkeymat; 2445 2446 2447 i=SSL_accept(con); 2448#ifndef OPENSSL_NO_SRP 2449 while (i <= 0 && SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP) 2450 { 2451 BIO_printf(bio_s_out,"LOOKUP during accept %s\n",srp_callback_parm.login); 2452 srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login); 2453 if (srp_callback_parm.user) 2454 BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info); 2455 else 2456 BIO_printf(bio_s_out,"LOOKUP not successful\n"); 2457 i=SSL_accept(con); 2458 } 2459#endif 2460 if (i <= 0) 2461 { 2462 if (BIO_sock_should_retry(i)) 2463 { 2464 BIO_printf(bio_s_out,"DELAY\n"); 2465 return(1); 2466 } 2467 2468 BIO_printf(bio_err,"ERROR\n"); 2469 verify_error=SSL_get_verify_result(con); 2470 if (verify_error != X509_V_OK) 2471 { 2472 BIO_printf(bio_err,"verify error:%s\n", 2473 X509_verify_cert_error_string(verify_error)); 2474 } 2475 else 2476 ERR_print_errors(bio_err); 2477 return(0); 2478 } 2479 2480 PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con)); 2481 2482 peer=SSL_get_peer_certificate(con); 2483 if (peer != NULL) 2484 { 2485 BIO_printf(bio_s_out,"Client certificate\n"); 2486 PEM_write_bio_X509(bio_s_out,peer); 2487 X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf); 2488 BIO_printf(bio_s_out,"subject=%s\n",buf); 2489 X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf); 2490 BIO_printf(bio_s_out,"issuer=%s\n",buf); 2491 X509_free(peer); 2492 } 2493 2494 if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL) 2495 BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf); 2496 str=SSL_CIPHER_get_name(SSL_get_current_cipher(con)); 2497 BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)"); 2498 2499#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 2500 SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len); 2501 if (next_proto_neg) 2502 { 2503 BIO_printf(bio_s_out,"NEXTPROTO is "); 2504 BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len); 2505 BIO_printf(bio_s_out, "\n"); 2506 } 2507#endif 2508#ifndef OPENSSL_NO_SRTP 2509 { 2510 SRTP_PROTECTION_PROFILE *srtp_profile 2511 = SSL_get_selected_srtp_profile(con); 2512 2513 if(srtp_profile) 2514 BIO_printf(bio_s_out,"SRTP Extension negotiated, profile=%s\n", 2515 srtp_profile->name); 2516 } 2517#endif 2518 if (SSL_cache_hit(con)) BIO_printf(bio_s_out,"Reused session-id\n"); 2519 if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) & 2520 TLS1_FLAGS_TLS_PADDING_BUG) 2521 BIO_printf(bio_s_out, 2522 "Peer has incorrect TLSv1 block padding\n"); 2523#ifndef OPENSSL_NO_KRB5 2524 client_princ = kssl_ctx_get0_client_princ(SSL_get0_kssl_ctx(con)); 2525 if (client_princ != NULL) 2526 { 2527 BIO_printf(bio_s_out,"Kerberos peer principal is %s\n", 2528 client_princ); 2529 } 2530#endif /* OPENSSL_NO_KRB5 */ 2531 BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n", 2532 SSL_get_secure_renegotiation_support(con) ? "" : " NOT"); 2533 if (keymatexportlabel != NULL) 2534 { 2535 BIO_printf(bio_s_out, "Keying material exporter:\n"); 2536 BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel); 2537 BIO_printf(bio_s_out, " Length: %i bytes\n", 2538 keymatexportlen); 2539 exportedkeymat = OPENSSL_malloc(keymatexportlen); 2540 if (exportedkeymat != NULL) 2541 { 2542 if (!SSL_export_keying_material(con, exportedkeymat, 2543 keymatexportlen, 2544 keymatexportlabel, 2545 strlen(keymatexportlabel), 2546 NULL, 0, 0)) 2547 { 2548 BIO_printf(bio_s_out, " Error\n"); 2549 } 2550 else 2551 { 2552 BIO_printf(bio_s_out, " Keying material: "); 2553 for (i=0; i<keymatexportlen; i++) 2554 BIO_printf(bio_s_out, "%02X", 2555 exportedkeymat[i]); 2556 BIO_printf(bio_s_out, "\n"); 2557 } 2558 OPENSSL_free(exportedkeymat); 2559 } 2560 } 2561 2562 return(1); 2563 } 2564 2565#ifndef OPENSSL_NO_DH 2566static DH *load_dh_param(const char *dhfile) 2567 { 2568 DH *ret=NULL; 2569 BIO *bio; 2570 2571 if ((bio=BIO_new_file(dhfile,"r")) == NULL) 2572 goto err; 2573 ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL); 2574err: 2575 if (bio != NULL) BIO_free(bio); 2576 return(ret); 2577 } 2578#endif 2579#ifndef OPENSSL_NO_KRB5 2580 char *client_princ; 2581#endif 2582 2583#if 0 2584static int load_CA(SSL_CTX *ctx, char *file) 2585 { 2586 FILE *in; 2587 X509 *x=NULL; 2588 2589 if ((in=fopen(file,"r")) == NULL) 2590 return(0); 2591 2592 for (;;) 2593 { 2594 if (PEM_read_X509(in,&x,NULL) == NULL) 2595 break; 2596 SSL_CTX_add_client_CA(ctx,x); 2597 } 2598 if (x != NULL) X509_free(x); 2599 fclose(in); 2600 return(1); 2601 } 2602#endif 2603 2604static int www_body(char *hostname, int s, unsigned char *context) 2605 { 2606 char *buf=NULL; 2607 int ret=1; 2608 int i,j,k,dot; 2609 SSL *con; 2610 const SSL_CIPHER *c; 2611 BIO *io,*ssl_bio,*sbio; 2612#ifndef OPENSSL_NO_KRB5 2613 KSSL_CTX *kctx; 2614#endif 2615 2616 buf=OPENSSL_malloc(bufsize); 2617 if (buf == NULL) return(0); 2618 io=BIO_new(BIO_f_buffer()); 2619 ssl_bio=BIO_new(BIO_f_ssl()); 2620 if ((io == NULL) || (ssl_bio == NULL)) goto err; 2621 2622#ifdef FIONBIO 2623 if (s_nbio) 2624 { 2625 unsigned long sl=1; 2626 2627 if (!s_quiet) 2628 BIO_printf(bio_err,"turning on non blocking io\n"); 2629 if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0) 2630 ERR_print_errors(bio_err); 2631 } 2632#endif 2633 2634 /* lets make the output buffer a reasonable size */ 2635 if (!BIO_set_write_buffer_size(io,bufsize)) goto err; 2636 2637 if ((con=SSL_new(ctx)) == NULL) goto err; 2638#ifndef OPENSSL_NO_TLSEXT 2639 if (s_tlsextdebug) 2640 { 2641 SSL_set_tlsext_debug_callback(con, tlsext_cb); 2642 SSL_set_tlsext_debug_arg(con, bio_s_out); 2643 } 2644#endif 2645#ifndef OPENSSL_NO_KRB5 2646 if ((kctx = kssl_ctx_new()) != NULL) 2647 { 2648 kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC); 2649 kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB); 2650 } 2651#endif /* OPENSSL_NO_KRB5 */ 2652 if(context) SSL_set_session_id_context(con, context, 2653 strlen((char *)context)); 2654 2655 sbio=BIO_new_socket(s,BIO_NOCLOSE); 2656 if (s_nbio_test) 2657 { 2658 BIO *test; 2659 2660 test=BIO_new(BIO_f_nbio_test()); 2661 sbio=BIO_push(test,sbio); 2662 } 2663 SSL_set_bio(con,sbio,sbio); 2664 SSL_set_accept_state(con); 2665 2666 /* SSL_set_fd(con,s); */ 2667 BIO_set_ssl(ssl_bio,con,BIO_CLOSE); 2668 BIO_push(io,ssl_bio); 2669#ifdef CHARSET_EBCDIC 2670 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io); 2671#endif 2672 2673 if (s_debug) 2674 { 2675 SSL_set_debug(con, 1); 2676 BIO_set_callback(SSL_get_rbio(con),bio_dump_callback); 2677 BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out); 2678 } 2679 if (s_msg) 2680 { 2681 SSL_set_msg_callback(con, msg_cb); 2682 SSL_set_msg_callback_arg(con, bio_s_out); 2683 } 2684 2685 for (;;) 2686 { 2687 if (hack) 2688 { 2689 i=SSL_accept(con); 2690#ifndef OPENSSL_NO_SRP 2691 while (i <= 0 && SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP) 2692 { 2693 BIO_printf(bio_s_out,"LOOKUP during accept %s\n",srp_callback_parm.login); 2694 srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login); 2695 if (srp_callback_parm.user) 2696 BIO_printf(bio_s_out,"LOOKUP done %s\n",srp_callback_parm.user->info); 2697 else 2698 BIO_printf(bio_s_out,"LOOKUP not successful\n"); 2699 i=SSL_accept(con); 2700 } 2701#endif 2702 switch (SSL_get_error(con,i)) 2703 { 2704 case SSL_ERROR_NONE: 2705 break; 2706 case SSL_ERROR_WANT_WRITE: 2707 case SSL_ERROR_WANT_READ: 2708 case SSL_ERROR_WANT_X509_LOOKUP: 2709 continue; 2710 case SSL_ERROR_SYSCALL: 2711 case SSL_ERROR_SSL: 2712 case SSL_ERROR_ZERO_RETURN: 2713 ret=1; 2714 goto err; 2715 /* break; */ 2716 } 2717 2718 SSL_renegotiate(con); 2719 SSL_write(con,NULL,0); 2720 } 2721 2722 i=BIO_gets(io,buf,bufsize-1); 2723 if (i < 0) /* error */ 2724 { 2725 if (!BIO_should_retry(io)) 2726 { 2727 if (!s_quiet) 2728 ERR_print_errors(bio_err); 2729 goto err; 2730 } 2731 else 2732 { 2733 BIO_printf(bio_s_out,"read R BLOCK\n"); 2734#if defined(OPENSSL_SYS_NETWARE) 2735 delay(1000); 2736#elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__) 2737 sleep(1); 2738#endif 2739 continue; 2740 } 2741 } 2742 else if (i == 0) /* end of input */ 2743 { 2744 ret=1; 2745 goto end; 2746 } 2747 2748 /* else we have data */ 2749 if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) || 2750 ((www == 2) && (strncmp("GET /stats ",buf,10) == 0))) 2751 { 2752 char *p; 2753 X509 *peer; 2754 STACK_OF(SSL_CIPHER) *sk; 2755 static const char *space=" "; 2756 2757 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 2758 BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n"); 2759 BIO_puts(io,"<pre>\n"); 2760/* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/ 2761 BIO_puts(io,"\n"); 2762 for (i=0; i<local_argc; i++) 2763 { 2764 BIO_puts(io,local_argv[i]); 2765 BIO_write(io," ",1); 2766 } 2767 BIO_puts(io,"\n"); 2768 2769 BIO_printf(io, 2770 "Secure Renegotiation IS%s supported\n", 2771 SSL_get_secure_renegotiation_support(con) ? 2772 "" : " NOT"); 2773 2774 /* The following is evil and should not really 2775 * be done */ 2776 BIO_printf(io,"Ciphers supported in s_server binary\n"); 2777 sk=SSL_get_ciphers(con); 2778 j=sk_SSL_CIPHER_num(sk); 2779 for (i=0; i<j; i++) 2780 { 2781 c=sk_SSL_CIPHER_value(sk,i); 2782 BIO_printf(io,"%-11s:%-25s", 2783 SSL_CIPHER_get_version(c), 2784 SSL_CIPHER_get_name(c)); 2785 if ((((i+1)%2) == 0) && (i+1 != j)) 2786 BIO_puts(io,"\n"); 2787 } 2788 BIO_puts(io,"\n"); 2789 p=SSL_get_shared_ciphers(con,buf,bufsize); 2790 if (p != NULL) 2791 { 2792 BIO_printf(io,"---\nCiphers common between both SSL end points:\n"); 2793 j=i=0; 2794 while (*p) 2795 { 2796 if (*p == ':') 2797 { 2798 BIO_write(io,space,26-j); 2799 i++; 2800 j=0; 2801 BIO_write(io,((i%3)?" ":"\n"),1); 2802 } 2803 else 2804 { 2805 BIO_write(io,p,1); 2806 j++; 2807 } 2808 p++; 2809 } 2810 BIO_puts(io,"\n"); 2811 } 2812 BIO_printf(io,(SSL_cache_hit(con) 2813 ?"---\nReused, " 2814 :"---\nNew, ")); 2815 c=SSL_get_current_cipher(con); 2816 BIO_printf(io,"%s, Cipher is %s\n", 2817 SSL_CIPHER_get_version(c), 2818 SSL_CIPHER_get_name(c)); 2819 SSL_SESSION_print(io,SSL_get_session(con)); 2820 BIO_printf(io,"---\n"); 2821 print_stats(io,SSL_get_SSL_CTX(con)); 2822 BIO_printf(io,"---\n"); 2823 peer=SSL_get_peer_certificate(con); 2824 if (peer != NULL) 2825 { 2826 BIO_printf(io,"Client certificate\n"); 2827 X509_print(io,peer); 2828 PEM_write_bio_X509(io,peer); 2829 } 2830 else 2831 BIO_puts(io,"no client certificate available\n"); 2832 BIO_puts(io,"</BODY></HTML>\r\n\r\n"); 2833 break; 2834 } 2835 else if ((www == 2 || www == 3) 2836 && (strncmp("GET /",buf,5) == 0)) 2837 { 2838 BIO *file; 2839 char *p,*e; 2840 static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"; 2841 2842 /* skip the '/' */ 2843 p= &(buf[5]); 2844 2845 dot = 1; 2846 for (e=p; *e != '\0'; e++) 2847 { 2848 if (e[0] == ' ') 2849 break; 2850 2851 switch (dot) 2852 { 2853 case 1: 2854 dot = (e[0] == '.') ? 2 : 0; 2855 break; 2856 case 2: 2857 dot = (e[0] == '.') ? 3 : 0; 2858 break; 2859 case 3: 2860 dot = (e[0] == '/') ? -1 : 0; 2861 break; 2862 } 2863 if (dot == 0) 2864 dot = (e[0] == '/') ? 1 : 0; 2865 } 2866 dot = (dot == 3) || (dot == -1); /* filename contains ".." component */ 2867 2868 if (*e == '\0') 2869 { 2870 BIO_puts(io,text); 2871 BIO_printf(io,"'%s' is an invalid file name\r\n",p); 2872 break; 2873 } 2874 *e='\0'; 2875 2876 if (dot) 2877 { 2878 BIO_puts(io,text); 2879 BIO_printf(io,"'%s' contains '..' reference\r\n",p); 2880 break; 2881 } 2882 2883 if (*p == '/') 2884 { 2885 BIO_puts(io,text); 2886 BIO_printf(io,"'%s' is an invalid path\r\n",p); 2887 break; 2888 } 2889 2890#if 0 2891 /* append if a directory lookup */ 2892 if (e[-1] == '/') 2893 strcat(p,"index.html"); 2894#endif 2895 2896 /* if a directory, do the index thang */ 2897 if (app_isdir(p)>0) 2898 { 2899#if 0 /* must check buffer size */ 2900 strcat(p,"/index.html"); 2901#else 2902 BIO_puts(io,text); 2903 BIO_printf(io,"'%s' is a directory\r\n",p); 2904 break; 2905#endif 2906 } 2907 2908 if ((file=BIO_new_file(p,"r")) == NULL) 2909 { 2910 BIO_puts(io,text); 2911 BIO_printf(io,"Error opening '%s'\r\n",p); 2912 ERR_print_errors(io); 2913 break; 2914 } 2915 2916 if (!s_quiet) 2917 BIO_printf(bio_err,"FILE:%s\n",p); 2918 2919 if (www == 2) 2920 { 2921 i=strlen(p); 2922 if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) || 2923 ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) || 2924 ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0))) 2925 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 2926 else 2927 BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"); 2928 } 2929 /* send the file */ 2930 for (;;) 2931 { 2932 i=BIO_read(file,buf,bufsize); 2933 if (i <= 0) break; 2934 2935#ifdef RENEG 2936 total_bytes+=i; 2937 fprintf(stderr,"%d\n",i); 2938 if (total_bytes > 3*1024) 2939 { 2940 total_bytes=0; 2941 fprintf(stderr,"RENEGOTIATE\n"); 2942 SSL_renegotiate(con); 2943 } 2944#endif 2945 2946 for (j=0; j<i; ) 2947 { 2948#ifdef RENEG 2949{ static count=0; if (++count == 13) { SSL_renegotiate(con); } } 2950#endif 2951 k=BIO_write(io,&(buf[j]),i-j); 2952 if (k <= 0) 2953 { 2954 if (!BIO_should_retry(io)) 2955 goto write_error; 2956 else 2957 { 2958 BIO_printf(bio_s_out,"rwrite W BLOCK\n"); 2959 } 2960 } 2961 else 2962 { 2963 j+=k; 2964 } 2965 } 2966 } 2967write_error: 2968 BIO_free(file); 2969 break; 2970 } 2971 } 2972 2973 for (;;) 2974 { 2975 i=(int)BIO_flush(io); 2976 if (i <= 0) 2977 { 2978 if (!BIO_should_retry(io)) 2979 break; 2980 } 2981 else 2982 break; 2983 } 2984end: 2985#if 1 2986 /* make sure we re-use sessions */ 2987 SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 2988#else 2989 /* This kills performance */ 2990/* SSL_shutdown(con); A shutdown gets sent in the 2991 * BIO_free_all(io) procession */ 2992#endif 2993 2994err: 2995 2996 if (ret >= 0) 2997 BIO_printf(bio_s_out,"ACCEPT\n"); 2998 2999 if (buf != NULL) OPENSSL_free(buf); 3000 if (io != NULL) BIO_free_all(io); 3001/* if (ssl_bio != NULL) BIO_free(ssl_bio);*/ 3002 return(ret); 3003 } 3004 3005#ifndef OPENSSL_NO_RSA 3006static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength) 3007 { 3008 BIGNUM *bn = NULL; 3009 static RSA *rsa_tmp=NULL; 3010 3011 if (!rsa_tmp && ((bn = BN_new()) == NULL)) 3012 BIO_printf(bio_err,"Allocation error in generating RSA key\n"); 3013 if (!rsa_tmp && bn) 3014 { 3015 if (!s_quiet) 3016 { 3017 BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength); 3018 (void)BIO_flush(bio_err); 3019 } 3020 if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) || 3021 !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) 3022 { 3023 if(rsa_tmp) RSA_free(rsa_tmp); 3024 rsa_tmp = NULL; 3025 } 3026 if (!s_quiet) 3027 { 3028 BIO_printf(bio_err,"\n"); 3029 (void)BIO_flush(bio_err); 3030 } 3031 BN_free(bn); 3032 } 3033 return(rsa_tmp); 3034 } 3035#endif 3036 3037#define MAX_SESSION_ID_ATTEMPTS 10 3038static int generate_session_id(const SSL *ssl, unsigned char *id, 3039 unsigned int *id_len) 3040 { 3041 unsigned int count = 0; 3042 do { 3043 RAND_pseudo_bytes(id, *id_len); 3044 /* Prefix the session_id with the required prefix. NB: If our 3045 * prefix is too long, clip it - but there will be worse effects 3046 * anyway, eg. the server could only possibly create 1 session 3047 * ID (ie. the prefix!) so all future session negotiations will 3048 * fail due to conflicts. */ 3049 memcpy(id, session_id_prefix, 3050 (strlen(session_id_prefix) < *id_len) ? 3051 strlen(session_id_prefix) : *id_len); 3052 } 3053 while(SSL_has_matching_session_id(ssl, id, *id_len) && 3054 (++count < MAX_SESSION_ID_ATTEMPTS)); 3055 if(count >= MAX_SESSION_ID_ATTEMPTS) 3056 return 0; 3057 return 1; 3058 } 3059