174664626SKris Kennaway /* apps/s_server.c */ 274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 374664626SKris Kennaway * All rights reserved. 474664626SKris Kennaway * 574664626SKris Kennaway * This package is an SSL implementation written 674664626SKris Kennaway * by Eric Young (eay@cryptsoft.com). 774664626SKris Kennaway * The implementation was written so as to conform with Netscapes SSL. 874664626SKris Kennaway * 974664626SKris Kennaway * This library is free for commercial and non-commercial use as long as 1074664626SKris Kennaway * the following conditions are aheared to. The following conditions 1174664626SKris Kennaway * apply to all code found in this distribution, be it the RC4, RSA, 1274664626SKris Kennaway * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1374664626SKris Kennaway * included with this distribution is covered by the same copyright terms 1474664626SKris Kennaway * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1574664626SKris Kennaway * 1674664626SKris Kennaway * Copyright remains Eric Young's, and as such any Copyright notices in 1774664626SKris Kennaway * the code are not to be removed. 1874664626SKris Kennaway * If this package is used in a product, Eric Young should be given attribution 1974664626SKris Kennaway * as the author of the parts of the library used. 2074664626SKris Kennaway * This can be in the form of a textual message at program startup or 2174664626SKris Kennaway * in documentation (online or textual) provided with the package. 2274664626SKris Kennaway * 2374664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 2474664626SKris Kennaway * modification, are permitted provided that the following conditions 2574664626SKris Kennaway * are met: 2674664626SKris Kennaway * 1. Redistributions of source code must retain the copyright 2774664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 2874664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 2974664626SKris Kennaway * notice, this list of conditions and the following disclaimer in the 3074664626SKris Kennaway * documentation and/or other materials provided with the distribution. 3174664626SKris Kennaway * 3. All advertising materials mentioning features or use of this software 3274664626SKris Kennaway * must display the following acknowledgement: 3374664626SKris Kennaway * "This product includes cryptographic software written by 3474664626SKris Kennaway * Eric Young (eay@cryptsoft.com)" 3574664626SKris Kennaway * The word 'cryptographic' can be left out if the rouines from the library 3674664626SKris Kennaway * being used are not cryptographic related :-). 3774664626SKris Kennaway * 4. If you include any Windows specific code (or a derivative thereof) from 3874664626SKris Kennaway * the apps directory (application code) you must include an acknowledgement: 3974664626SKris Kennaway * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4074664626SKris Kennaway * 4174664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4274664626SKris Kennaway * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4374664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4474664626SKris Kennaway * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4574664626SKris Kennaway * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4674664626SKris Kennaway * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4774664626SKris Kennaway * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4874664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4974664626SKris Kennaway * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5074664626SKris Kennaway * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5174664626SKris Kennaway * SUCH DAMAGE. 5274664626SKris Kennaway * 5374664626SKris Kennaway * The licence and distribution terms for any publically available version or 5474664626SKris Kennaway * derivative of this code cannot be changed. i.e. this code cannot simply be 5574664626SKris Kennaway * copied and put under another distribution licence 5674664626SKris Kennaway * [including the GNU Public Licence.] 5774664626SKris Kennaway */ 585c87c606SMark Murray /* ==================================================================== 595c87c606SMark Murray * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 605c87c606SMark Murray * 615c87c606SMark Murray * Redistribution and use in source and binary forms, with or without 625c87c606SMark Murray * modification, are permitted provided that the following conditions 635c87c606SMark Murray * are met: 645c87c606SMark Murray * 655c87c606SMark Murray * 1. Redistributions of source code must retain the above copyright 665c87c606SMark Murray * notice, this list of conditions and the following disclaimer. 675c87c606SMark Murray * 685c87c606SMark Murray * 2. Redistributions in binary form must reproduce the above copyright 695c87c606SMark Murray * notice, this list of conditions and the following disclaimer in 705c87c606SMark Murray * the documentation and/or other materials provided with the 715c87c606SMark Murray * distribution. 725c87c606SMark Murray * 735c87c606SMark Murray * 3. All advertising materials mentioning features or use of this 745c87c606SMark Murray * software must display the following acknowledgment: 755c87c606SMark Murray * "This product includes software developed by the OpenSSL Project 765c87c606SMark Murray * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 775c87c606SMark Murray * 785c87c606SMark Murray * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 795c87c606SMark Murray * endorse or promote products derived from this software without 805c87c606SMark Murray * prior written permission. For written permission, please contact 815c87c606SMark Murray * openssl-core@openssl.org. 825c87c606SMark Murray * 835c87c606SMark Murray * 5. Products derived from this software may not be called "OpenSSL" 845c87c606SMark Murray * nor may "OpenSSL" appear in their names without prior written 855c87c606SMark Murray * permission of the OpenSSL Project. 865c87c606SMark Murray * 875c87c606SMark Murray * 6. Redistributions of any form whatsoever must retain the following 885c87c606SMark Murray * acknowledgment: 895c87c606SMark Murray * "This product includes software developed by the OpenSSL Project 905c87c606SMark Murray * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 915c87c606SMark Murray * 925c87c606SMark Murray * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 935c87c606SMark Murray * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 945c87c606SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 955c87c606SMark Murray * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 965c87c606SMark Murray * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 975c87c606SMark Murray * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 985c87c606SMark Murray * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 995c87c606SMark Murray * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1005c87c606SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1015c87c606SMark Murray * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1025c87c606SMark Murray * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1035c87c606SMark Murray * OF THE POSSIBILITY OF SUCH DAMAGE. 1045c87c606SMark Murray * ==================================================================== 1055c87c606SMark Murray * 1065c87c606SMark Murray * This product includes cryptographic software written by Eric Young 1075c87c606SMark Murray * (eay@cryptsoft.com). This product includes software written by Tim 1085c87c606SMark Murray * Hudson (tjh@cryptsoft.com). 1095c87c606SMark Murray * 1105c87c606SMark Murray */ 1113b4e3dcbSSimon L. B. Nielsen /* ==================================================================== 1123b4e3dcbSSimon L. B. Nielsen * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 1133b4e3dcbSSimon L. B. Nielsen * ECC cipher suite support in OpenSSL originally developed by 1143b4e3dcbSSimon L. B. Nielsen * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 1153b4e3dcbSSimon L. B. Nielsen */ 1163b4e3dcbSSimon L. B. Nielsen 1173b4e3dcbSSimon L. B. Nielsen /* Until the key-gen callbacks are modified to use newer prototypes, we allow 1183b4e3dcbSSimon L. B. Nielsen * deprecated functions for openssl-internal code */ 1193b4e3dcbSSimon L. B. Nielsen #ifdef OPENSSL_NO_DEPRECATED 1203b4e3dcbSSimon L. B. Nielsen #undef OPENSSL_NO_DEPRECATED 1213b4e3dcbSSimon L. B. Nielsen #endif 12274664626SKris Kennaway 12374664626SKris Kennaway #include <assert.h> 12474664626SKris Kennaway #include <stdio.h> 12574664626SKris Kennaway #include <stdlib.h> 12674664626SKris Kennaway #include <string.h> 1273b4e3dcbSSimon L. B. Nielsen 12874664626SKris Kennaway #include <sys/stat.h> 1295c87c606SMark Murray #include <openssl/e_os2.h> 1305c87c606SMark Murray #ifdef OPENSSL_NO_STDIO 13174664626SKris Kennaway #define APPS_WIN16 13274664626SKris Kennaway #endif 13374664626SKris Kennaway 1343b4e3dcbSSimon L. B. Nielsen #if !defined(OPENSSL_SYS_NETWARE) /* conflicts with winsock2 stuff on netware */ 1353b4e3dcbSSimon L. B. Nielsen #include <sys/types.h> 1363b4e3dcbSSimon L. B. Nielsen #endif 1373b4e3dcbSSimon L. B. Nielsen 13874664626SKris Kennaway /* With IPv6, it looks like Digital has mixed up the proper order of 13974664626SKris Kennaway recursive header file inclusion, resulting in the compiler complaining 14074664626SKris Kennaway that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which 14174664626SKris Kennaway is needed to have fileno() declared correctly... So let's define u_int */ 1425c87c606SMark Murray #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) 14374664626SKris Kennaway #define __U_INT 14474664626SKris Kennaway typedef unsigned int u_int; 14574664626SKris Kennaway #endif 14674664626SKris Kennaway 14774664626SKris Kennaway #include <openssl/lhash.h> 14874664626SKris Kennaway #include <openssl/bn.h> 14974664626SKris Kennaway #define USE_SOCKETS 15074664626SKris Kennaway #include "apps.h" 15174664626SKris Kennaway #include <openssl/err.h> 15274664626SKris Kennaway #include <openssl/pem.h> 15374664626SKris Kennaway #include <openssl/x509.h> 15474664626SKris Kennaway #include <openssl/ssl.h> 1555740a5e3SKris Kennaway #include <openssl/rand.h> 1563b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DH 1573b4e3dcbSSimon L. B. Nielsen #include <openssl/dh.h> 1583b4e3dcbSSimon L. B. Nielsen #endif 1593b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_RSA 1603b4e3dcbSSimon L. B. Nielsen #include <openssl/rsa.h> 1613b4e3dcbSSimon L. B. Nielsen #endif 16274664626SKris Kennaway #include "s_apps.h" 1633b4e3dcbSSimon L. B. Nielsen #include "timeouts.h" 16474664626SKris Kennaway 1655c87c606SMark Murray #ifdef OPENSSL_SYS_WINCE 1665c87c606SMark Murray /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */ 1675c87c606SMark Murray #ifdef fileno 1685c87c606SMark Murray #undef fileno 1695c87c606SMark Murray #endif 1705c87c606SMark Murray #define fileno(a) (int)_fileno(a) 1715c87c606SMark Murray #endif 1725c87c606SMark Murray 1735c87c606SMark Murray #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) 17474664626SKris Kennaway /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ 17574664626SKris Kennaway #undef FIONBIO 17674664626SKris Kennaway #endif 17774664626SKris Kennaway 1785c87c606SMark Murray #ifndef OPENSSL_NO_RSA 17974664626SKris Kennaway static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength); 18074664626SKris Kennaway #endif 18174664626SKris Kennaway static int sv_body(char *hostname, int s, unsigned char *context); 18274664626SKris Kennaway static int www_body(char *hostname, int s, unsigned char *context); 18374664626SKris Kennaway static void close_accept_socket(void ); 18474664626SKris Kennaway static void sv_usage(void); 18574664626SKris Kennaway static int init_ssl_connection(SSL *s); 18674664626SKris Kennaway static void print_stats(BIO *bp,SSL_CTX *ctx); 1875c87c606SMark Murray static int generate_session_id(const SSL *ssl, unsigned char *id, 1885c87c606SMark Murray unsigned int *id_len); 1895c87c606SMark Murray #ifndef OPENSSL_NO_DH 1903b4e3dcbSSimon L. B. Nielsen static DH *load_dh_param(const char *dhfile); 19174664626SKris Kennaway static DH *get_dh512(void); 19274664626SKris Kennaway #endif 1933b4e3dcbSSimon L. B. Nielsen 194f579bf8eSKris Kennaway #ifdef MONOLITH 195f579bf8eSKris Kennaway static void s_server_init(void); 196f579bf8eSKris Kennaway #endif 19774664626SKris Kennaway 19874664626SKris Kennaway #ifndef S_ISDIR 19974664626SKris Kennaway # if defined(_S_IFMT) && defined(_S_IFDIR) 20074664626SKris Kennaway # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR) 20174664626SKris Kennaway # else 20274664626SKris Kennaway # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) 20374664626SKris Kennaway # endif 20474664626SKris Kennaway #endif 20574664626SKris Kennaway 2065c87c606SMark Murray #ifndef OPENSSL_NO_DH 20774664626SKris Kennaway static unsigned char dh512_p[]={ 20874664626SKris Kennaway 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75, 20974664626SKris Kennaway 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F, 21074664626SKris Kennaway 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3, 21174664626SKris Kennaway 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12, 21274664626SKris Kennaway 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C, 21374664626SKris Kennaway 0x47,0x74,0xE8,0x33, 21474664626SKris Kennaway }; 21574664626SKris Kennaway static unsigned char dh512_g[]={ 21674664626SKris Kennaway 0x02, 21774664626SKris Kennaway }; 21874664626SKris Kennaway 21974664626SKris Kennaway static DH *get_dh512(void) 22074664626SKris Kennaway { 22174664626SKris Kennaway DH *dh=NULL; 22274664626SKris Kennaway 22374664626SKris Kennaway if ((dh=DH_new()) == NULL) return(NULL); 22474664626SKris Kennaway dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL); 22574664626SKris Kennaway dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL); 22674664626SKris Kennaway if ((dh->p == NULL) || (dh->g == NULL)) 22774664626SKris Kennaway return(NULL); 22874664626SKris Kennaway return(dh); 22974664626SKris Kennaway } 23074664626SKris Kennaway #endif 23174664626SKris Kennaway 2323b4e3dcbSSimon L. B. Nielsen 23374664626SKris Kennaway /* static int load_CA(SSL_CTX *ctx, char *file);*/ 23474664626SKris Kennaway 23574664626SKris Kennaway #undef BUFSIZZ 23674664626SKris Kennaway #define BUFSIZZ 16*1024 237f579bf8eSKris Kennaway static int bufsize=BUFSIZZ; 23874664626SKris Kennaway static int accept_socket= -1; 23974664626SKris Kennaway 24074664626SKris Kennaway #define TEST_CERT "server.pem" 24174664626SKris Kennaway #undef PROG 24274664626SKris Kennaway #define PROG s_server_main 24374664626SKris Kennaway 24474664626SKris Kennaway extern int verify_depth; 24574664626SKris Kennaway 24674664626SKris Kennaway static char *cipher=NULL; 24774664626SKris Kennaway static int s_server_verify=SSL_VERIFY_NONE; 24874664626SKris Kennaway static int s_server_session_id_context = 1; /* anything will do */ 2493b4e3dcbSSimon L. B. Nielsen static const char *s_cert_file=TEST_CERT,*s_key_file=NULL; 25074664626SKris Kennaway static char *s_dcert_file=NULL,*s_dkey_file=NULL; 25174664626SKris Kennaway #ifdef FIONBIO 25274664626SKris Kennaway static int s_nbio=0; 25374664626SKris Kennaway #endif 25474664626SKris Kennaway static int s_nbio_test=0; 25574664626SKris Kennaway int s_crlf=0; 25674664626SKris Kennaway static SSL_CTX *ctx=NULL; 25774664626SKris Kennaway static int www=0; 25874664626SKris Kennaway 25974664626SKris Kennaway static BIO *bio_s_out=NULL; 26074664626SKris Kennaway static int s_debug=0; 2615c87c606SMark Murray static int s_msg=0; 26274664626SKris Kennaway static int s_quiet=0; 26374664626SKris Kennaway 264f579bf8eSKris Kennaway static int hack=0; 265fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 2665c87c606SMark Murray static char *engine_id=NULL; 267fceca8a3SJacques Vidrine #endif 2685c87c606SMark Murray static const char *session_id_prefix=NULL; 269f579bf8eSKris Kennaway 2703b4e3dcbSSimon L. B. Nielsen static int enable_timeouts = 0; 2713b4e3dcbSSimon L. B. Nielsen #ifdef mtu 2723b4e3dcbSSimon L. B. Nielsen #undef mtu 2733b4e3dcbSSimon L. B. Nielsen #endif 2743b4e3dcbSSimon L. B. Nielsen static long mtu; 2753b4e3dcbSSimon L. B. Nielsen static int cert_chain = 0; 2763b4e3dcbSSimon L. B. Nielsen 2773b4e3dcbSSimon L. B. Nielsen 278f579bf8eSKris Kennaway #ifdef MONOLITH 27974664626SKris Kennaway static void s_server_init(void) 28074664626SKris Kennaway { 281f579bf8eSKris Kennaway accept_socket=-1; 28274664626SKris Kennaway cipher=NULL; 28374664626SKris Kennaway s_server_verify=SSL_VERIFY_NONE; 28474664626SKris Kennaway s_dcert_file=NULL; 28574664626SKris Kennaway s_dkey_file=NULL; 28674664626SKris Kennaway s_cert_file=TEST_CERT; 28774664626SKris Kennaway s_key_file=NULL; 28874664626SKris Kennaway #ifdef FIONBIO 28974664626SKris Kennaway s_nbio=0; 29074664626SKris Kennaway #endif 29174664626SKris Kennaway s_nbio_test=0; 29274664626SKris Kennaway ctx=NULL; 29374664626SKris Kennaway www=0; 29474664626SKris Kennaway 29574664626SKris Kennaway bio_s_out=NULL; 29674664626SKris Kennaway s_debug=0; 2975c87c606SMark Murray s_msg=0; 29874664626SKris Kennaway s_quiet=0; 299f579bf8eSKris Kennaway hack=0; 300fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 3015c87c606SMark Murray engine_id=NULL; 302fceca8a3SJacques Vidrine #endif 30374664626SKris Kennaway } 30474664626SKris Kennaway #endif 30574664626SKris Kennaway 30674664626SKris Kennaway static void sv_usage(void) 30774664626SKris Kennaway { 30874664626SKris Kennaway BIO_printf(bio_err,"usage: s_server [args ...]\n"); 30974664626SKris Kennaway BIO_printf(bio_err,"\n"); 31074664626SKris Kennaway BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT); 31174664626SKris Kennaway BIO_printf(bio_err," -context arg - set session ID context\n"); 31274664626SKris Kennaway BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n"); 31374664626SKris Kennaway BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n"); 3143b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -cert arg - certificate file to use\n"); 31574664626SKris Kennaway BIO_printf(bio_err," (default is %s)\n",TEST_CERT); 3163b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n"); 3173b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -key arg - Private Key file to use, in cert file if\n"); 31874664626SKris Kennaway BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT); 3193b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -keyform arg - key format (PEM, DER or ENGINE) PEM default\n"); 3203b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -pass arg - private key file pass phrase source\n"); 32174664626SKris Kennaway BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n"); 3223b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -dcertform x - second certificate format (PEM or DER) PEM default\n"); 32374664626SKris Kennaway BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n"); 3243b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n"); 3253b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -dpass arg - second private key file pass phrase source\n"); 326f579bf8eSKris Kennaway BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n"); 327f579bf8eSKris Kennaway BIO_printf(bio_err," or a default set of parameters is used\n"); 3283b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 3293b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \ 3303b4e3dcbSSimon L. B. Nielsen " Use \"openssl ecparam -list_curves\" for all names\n" \ 3313b4e3dcbSSimon L. B. Nielsen " (default is sect163r2).\n"); 3323b4e3dcbSSimon L. B. Nielsen #endif 33374664626SKris Kennaway #ifdef FIONBIO 33474664626SKris Kennaway BIO_printf(bio_err," -nbio - Run with non-blocking IO\n"); 33574664626SKris Kennaway #endif 33674664626SKris Kennaway BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n"); 33774664626SKris Kennaway BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n"); 33874664626SKris Kennaway BIO_printf(bio_err," -debug - Print more output\n"); 3395c87c606SMark Murray BIO_printf(bio_err," -msg - Show protocol messages\n"); 34074664626SKris Kennaway BIO_printf(bio_err," -state - Print the SSL states\n"); 34174664626SKris Kennaway BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n"); 34274664626SKris Kennaway BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n"); 34374664626SKris Kennaway BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n"); 34474664626SKris Kennaway BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n"); 3455c87c606SMark Murray BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n"); 34674664626SKris Kennaway BIO_printf(bio_err," -quiet - No server output\n"); 34774664626SKris Kennaway BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n"); 34874664626SKris Kennaway BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n"); 34974664626SKris Kennaway BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n"); 35074664626SKris Kennaway BIO_printf(bio_err," -tls1 - Just talk TLSv1\n"); 3513b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -dtls1 - Just talk DTLSv1\n"); 3523b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -timeout - Enable timeouts\n"); 3533b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -mtu - Set MTU\n"); 3543b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -chain - Read a certificate chain\n"); 35574664626SKris Kennaway BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n"); 35674664626SKris Kennaway BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n"); 35774664626SKris Kennaway BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n"); 3585c87c606SMark Murray #ifndef OPENSSL_NO_DH 35974664626SKris Kennaway BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n"); 36074664626SKris Kennaway #endif 3613b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 3623b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n"); 3633b4e3dcbSSimon L. B. Nielsen #endif 364f579bf8eSKris Kennaway BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n"); 36574664626SKris Kennaway BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n"); 36674664626SKris Kennaway BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n"); 3675c87c606SMark Murray BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n"); 3685c87c606SMark Murray BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n"); 369fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 3705c87c606SMark Murray BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n"); 371fceca8a3SJacques Vidrine #endif 3725c87c606SMark Murray BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n"); 3735740a5e3SKris Kennaway BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); 37474664626SKris Kennaway } 37574664626SKris Kennaway 37674664626SKris Kennaway static int local_argc=0; 37774664626SKris Kennaway static char **local_argv; 37874664626SKris Kennaway 37974664626SKris Kennaway #ifdef CHARSET_EBCDIC 38074664626SKris Kennaway static int ebcdic_new(BIO *bi); 38174664626SKris Kennaway static int ebcdic_free(BIO *a); 38274664626SKris Kennaway static int ebcdic_read(BIO *b, char *out, int outl); 3835c87c606SMark Murray static int ebcdic_write(BIO *b, const char *in, int inl); 3845c87c606SMark Murray static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr); 38574664626SKris Kennaway static int ebcdic_gets(BIO *bp, char *buf, int size); 3865c87c606SMark Murray static int ebcdic_puts(BIO *bp, const char *str); 38774664626SKris Kennaway 38874664626SKris Kennaway #define BIO_TYPE_EBCDIC_FILTER (18|0x0200) 38974664626SKris Kennaway static BIO_METHOD methods_ebcdic= 39074664626SKris Kennaway { 39174664626SKris Kennaway BIO_TYPE_EBCDIC_FILTER, 39274664626SKris Kennaway "EBCDIC/ASCII filter", 39374664626SKris Kennaway ebcdic_write, 39474664626SKris Kennaway ebcdic_read, 39574664626SKris Kennaway ebcdic_puts, 39674664626SKris Kennaway ebcdic_gets, 39774664626SKris Kennaway ebcdic_ctrl, 39874664626SKris Kennaway ebcdic_new, 39974664626SKris Kennaway ebcdic_free, 40074664626SKris Kennaway }; 40174664626SKris Kennaway 40274664626SKris Kennaway typedef struct 40374664626SKris Kennaway { 40474664626SKris Kennaway size_t alloced; 40574664626SKris Kennaway char buff[1]; 40674664626SKris Kennaway } EBCDIC_OUTBUFF; 40774664626SKris Kennaway 40874664626SKris Kennaway BIO_METHOD *BIO_f_ebcdic_filter() 40974664626SKris Kennaway { 41074664626SKris Kennaway return(&methods_ebcdic); 41174664626SKris Kennaway } 41274664626SKris Kennaway 41374664626SKris Kennaway static int ebcdic_new(BIO *bi) 41474664626SKris Kennaway { 41574664626SKris Kennaway EBCDIC_OUTBUFF *wbuf; 41674664626SKris Kennaway 417ddd58736SKris Kennaway wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024); 41874664626SKris Kennaway wbuf->alloced = 1024; 41974664626SKris Kennaway wbuf->buff[0] = '\0'; 42074664626SKris Kennaway 42174664626SKris Kennaway bi->ptr=(char *)wbuf; 42274664626SKris Kennaway bi->init=1; 42374664626SKris Kennaway bi->flags=0; 42474664626SKris Kennaway return(1); 42574664626SKris Kennaway } 42674664626SKris Kennaway 42774664626SKris Kennaway static int ebcdic_free(BIO *a) 42874664626SKris Kennaway { 42974664626SKris Kennaway if (a == NULL) return(0); 43074664626SKris Kennaway if (a->ptr != NULL) 431ddd58736SKris Kennaway OPENSSL_free(a->ptr); 43274664626SKris Kennaway a->ptr=NULL; 43374664626SKris Kennaway a->init=0; 43474664626SKris Kennaway a->flags=0; 43574664626SKris Kennaway return(1); 43674664626SKris Kennaway } 43774664626SKris Kennaway 43874664626SKris Kennaway static int ebcdic_read(BIO *b, char *out, int outl) 43974664626SKris Kennaway { 44074664626SKris Kennaway int ret=0; 44174664626SKris Kennaway 44274664626SKris Kennaway if (out == NULL || outl == 0) return(0); 44374664626SKris Kennaway if (b->next_bio == NULL) return(0); 44474664626SKris Kennaway 44574664626SKris Kennaway ret=BIO_read(b->next_bio,out,outl); 44674664626SKris Kennaway if (ret > 0) 44774664626SKris Kennaway ascii2ebcdic(out,out,ret); 44874664626SKris Kennaway return(ret); 44974664626SKris Kennaway } 45074664626SKris Kennaway 4515c87c606SMark Murray static int ebcdic_write(BIO *b, const char *in, int inl) 45274664626SKris Kennaway { 45374664626SKris Kennaway EBCDIC_OUTBUFF *wbuf; 45474664626SKris Kennaway int ret=0; 45574664626SKris Kennaway int num; 45674664626SKris Kennaway unsigned char n; 45774664626SKris Kennaway 45874664626SKris Kennaway if ((in == NULL) || (inl <= 0)) return(0); 45974664626SKris Kennaway if (b->next_bio == NULL) return(0); 46074664626SKris Kennaway 46174664626SKris Kennaway wbuf=(EBCDIC_OUTBUFF *)b->ptr; 46274664626SKris Kennaway 46374664626SKris Kennaway if (inl > (num = wbuf->alloced)) 46474664626SKris Kennaway { 46574664626SKris Kennaway num = num + num; /* double the size */ 46674664626SKris Kennaway if (num < inl) 46774664626SKris Kennaway num = inl; 468ddd58736SKris Kennaway OPENSSL_free(wbuf); 469ddd58736SKris Kennaway wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num); 47074664626SKris Kennaway 47174664626SKris Kennaway wbuf->alloced = num; 47274664626SKris Kennaway wbuf->buff[0] = '\0'; 47374664626SKris Kennaway 47474664626SKris Kennaway b->ptr=(char *)wbuf; 47574664626SKris Kennaway } 47674664626SKris Kennaway 47774664626SKris Kennaway ebcdic2ascii(wbuf->buff, in, inl); 47874664626SKris Kennaway 47974664626SKris Kennaway ret=BIO_write(b->next_bio, wbuf->buff, inl); 48074664626SKris Kennaway 48174664626SKris Kennaway return(ret); 48274664626SKris Kennaway } 48374664626SKris Kennaway 4845c87c606SMark Murray static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr) 48574664626SKris Kennaway { 48674664626SKris Kennaway long ret; 48774664626SKris Kennaway 48874664626SKris Kennaway if (b->next_bio == NULL) return(0); 48974664626SKris Kennaway switch (cmd) 49074664626SKris Kennaway { 49174664626SKris Kennaway case BIO_CTRL_DUP: 49274664626SKris Kennaway ret=0L; 49374664626SKris Kennaway break; 49474664626SKris Kennaway default: 49574664626SKris Kennaway ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 49674664626SKris Kennaway break; 49774664626SKris Kennaway } 49874664626SKris Kennaway return(ret); 49974664626SKris Kennaway } 50074664626SKris Kennaway 50174664626SKris Kennaway static int ebcdic_gets(BIO *bp, char *buf, int size) 50274664626SKris Kennaway { 5035c87c606SMark Murray int i, ret=0; 50474664626SKris Kennaway if (bp->next_bio == NULL) return(0); 50574664626SKris Kennaway /* return(BIO_gets(bp->next_bio,buf,size));*/ 50674664626SKris Kennaway for (i=0; i<size-1; ++i) 50774664626SKris Kennaway { 50874664626SKris Kennaway ret = ebcdic_read(bp,&buf[i],1); 50974664626SKris Kennaway if (ret <= 0) 51074664626SKris Kennaway break; 51174664626SKris Kennaway else if (buf[i] == '\n') 51274664626SKris Kennaway { 51374664626SKris Kennaway ++i; 51474664626SKris Kennaway break; 51574664626SKris Kennaway } 51674664626SKris Kennaway } 51774664626SKris Kennaway if (i < size) 51874664626SKris Kennaway buf[i] = '\0'; 51974664626SKris Kennaway return (ret < 0 && i == 0) ? ret : i; 52074664626SKris Kennaway } 52174664626SKris Kennaway 5225c87c606SMark Murray static int ebcdic_puts(BIO *bp, const char *str) 52374664626SKris Kennaway { 52474664626SKris Kennaway if (bp->next_bio == NULL) return(0); 52574664626SKris Kennaway return ebcdic_write(bp, str, strlen(str)); 52674664626SKris Kennaway } 52774664626SKris Kennaway #endif 52874664626SKris Kennaway 529f579bf8eSKris Kennaway int MAIN(int, char **); 530f579bf8eSKris Kennaway 53174664626SKris Kennaway int MAIN(int argc, char *argv[]) 53274664626SKris Kennaway { 5335c87c606SMark Murray X509_STORE *store = NULL; 5345c87c606SMark Murray int vflags = 0; 53574664626SKris Kennaway short port=PORT; 53674664626SKris Kennaway char *CApath=NULL,*CAfile=NULL; 5373b4e3dcbSSimon L. B. Nielsen unsigned char *context = NULL; 538f579bf8eSKris Kennaway char *dhfile = NULL; 5393b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 5403b4e3dcbSSimon L. B. Nielsen char *named_curve = NULL; 5413b4e3dcbSSimon L. B. Nielsen #endif 54274664626SKris Kennaway int badop=0,bugs=0; 54374664626SKris Kennaway int ret=1; 54474664626SKris Kennaway int off=0; 5453b4e3dcbSSimon L. B. Nielsen int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0; 54674664626SKris Kennaway int state=0; 54774664626SKris Kennaway SSL_METHOD *meth=NULL; 5483b4e3dcbSSimon L. B. Nielsen #ifdef sock_type 5493b4e3dcbSSimon L. B. Nielsen #undef sock_type 5503b4e3dcbSSimon L. B. Nielsen #endif 5513b4e3dcbSSimon L. B. Nielsen int sock_type=SOCK_STREAM; 552fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 5535c87c606SMark Murray ENGINE *e=NULL; 554fceca8a3SJacques Vidrine #endif 5555740a5e3SKris Kennaway char *inrand=NULL; 5563b4e3dcbSSimon L. B. Nielsen int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM; 5573b4e3dcbSSimon L. B. Nielsen char *passarg = NULL, *pass = NULL; 5583b4e3dcbSSimon L. B. Nielsen char *dpassarg = NULL, *dpass = NULL; 5593b4e3dcbSSimon L. B. Nielsen int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM; 5603b4e3dcbSSimon L. B. Nielsen X509 *s_cert = NULL, *s_dcert = NULL; 5613b4e3dcbSSimon L. B. Nielsen EVP_PKEY *s_key = NULL, *s_dkey = NULL; 56274664626SKris Kennaway 5635c87c606SMark Murray #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3) 56474664626SKris Kennaway meth=SSLv23_server_method(); 5655c87c606SMark Murray #elif !defined(OPENSSL_NO_SSL3) 56674664626SKris Kennaway meth=SSLv3_server_method(); 5675c87c606SMark Murray #elif !defined(OPENSSL_NO_SSL2) 56874664626SKris Kennaway meth=SSLv2_server_method(); 56974664626SKris Kennaway #endif 57074664626SKris Kennaway 57174664626SKris Kennaway local_argc=argc; 57274664626SKris Kennaway local_argv=argv; 57374664626SKris Kennaway 57474664626SKris Kennaway apps_startup(); 575f579bf8eSKris Kennaway #ifdef MONOLITH 576f579bf8eSKris Kennaway s_server_init(); 577f579bf8eSKris Kennaway #endif 57874664626SKris Kennaway 57974664626SKris Kennaway if (bio_err == NULL) 58074664626SKris Kennaway bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); 58174664626SKris Kennaway 5825c87c606SMark Murray if (!load_config(bio_err, NULL)) 5835c87c606SMark Murray goto end; 5845c87c606SMark Murray 58574664626SKris Kennaway verify_depth=0; 58674664626SKris Kennaway #ifdef FIONBIO 58774664626SKris Kennaway s_nbio=0; 58874664626SKris Kennaway #endif 58974664626SKris Kennaway s_nbio_test=0; 59074664626SKris Kennaway 59174664626SKris Kennaway argc--; 59274664626SKris Kennaway argv++; 59374664626SKris Kennaway 59474664626SKris Kennaway while (argc >= 1) 59574664626SKris Kennaway { 59674664626SKris Kennaway if ((strcmp(*argv,"-port") == 0) || 59774664626SKris Kennaway (strcmp(*argv,"-accept") == 0)) 59874664626SKris Kennaway { 59974664626SKris Kennaway if (--argc < 1) goto bad; 60074664626SKris Kennaway if (!extract_port(*(++argv),&port)) 60174664626SKris Kennaway goto bad; 60274664626SKris Kennaway } 60374664626SKris Kennaway else if (strcmp(*argv,"-verify") == 0) 60474664626SKris Kennaway { 60574664626SKris Kennaway s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE; 60674664626SKris Kennaway if (--argc < 1) goto bad; 60774664626SKris Kennaway verify_depth=atoi(*(++argv)); 60874664626SKris Kennaway BIO_printf(bio_err,"verify depth is %d\n",verify_depth); 60974664626SKris Kennaway } 61074664626SKris Kennaway else if (strcmp(*argv,"-Verify") == 0) 61174664626SKris Kennaway { 61274664626SKris Kennaway s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT| 61374664626SKris Kennaway SSL_VERIFY_CLIENT_ONCE; 61474664626SKris Kennaway if (--argc < 1) goto bad; 61574664626SKris Kennaway verify_depth=atoi(*(++argv)); 61674664626SKris Kennaway BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth); 61774664626SKris Kennaway } 61874664626SKris Kennaway else if (strcmp(*argv,"-context") == 0) 61974664626SKris Kennaway { 62074664626SKris Kennaway if (--argc < 1) goto bad; 6213b4e3dcbSSimon L. B. Nielsen context= (unsigned char *)*(++argv); 62274664626SKris Kennaway } 62374664626SKris Kennaway else if (strcmp(*argv,"-cert") == 0) 62474664626SKris Kennaway { 62574664626SKris Kennaway if (--argc < 1) goto bad; 62674664626SKris Kennaway s_cert_file= *(++argv); 62774664626SKris Kennaway } 6283b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-certform") == 0) 6293b4e3dcbSSimon L. B. Nielsen { 6303b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 6313b4e3dcbSSimon L. B. Nielsen s_cert_format = str2fmt(*(++argv)); 6323b4e3dcbSSimon L. B. Nielsen } 63374664626SKris Kennaway else if (strcmp(*argv,"-key") == 0) 63474664626SKris Kennaway { 63574664626SKris Kennaway if (--argc < 1) goto bad; 63674664626SKris Kennaway s_key_file= *(++argv); 63774664626SKris Kennaway } 6383b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-keyform") == 0) 6393b4e3dcbSSimon L. B. Nielsen { 6403b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 6413b4e3dcbSSimon L. B. Nielsen s_key_format = str2fmt(*(++argv)); 6423b4e3dcbSSimon L. B. Nielsen } 6433b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-pass") == 0) 6443b4e3dcbSSimon L. B. Nielsen { 6453b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 6463b4e3dcbSSimon L. B. Nielsen passarg = *(++argv); 6473b4e3dcbSSimon L. B. Nielsen } 648f579bf8eSKris Kennaway else if (strcmp(*argv,"-dhparam") == 0) 649f579bf8eSKris Kennaway { 650f579bf8eSKris Kennaway if (--argc < 1) goto bad; 651f579bf8eSKris Kennaway dhfile = *(++argv); 652f579bf8eSKris Kennaway } 6533b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 6543b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-named_curve") == 0) 6553b4e3dcbSSimon L. B. Nielsen { 6563b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 6573b4e3dcbSSimon L. B. Nielsen named_curve = *(++argv); 6583b4e3dcbSSimon L. B. Nielsen } 6593b4e3dcbSSimon L. B. Nielsen #endif 6603b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-dcertform") == 0) 6613b4e3dcbSSimon L. B. Nielsen { 6623b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 6633b4e3dcbSSimon L. B. Nielsen s_dcert_format = str2fmt(*(++argv)); 6643b4e3dcbSSimon L. B. Nielsen } 66574664626SKris Kennaway else if (strcmp(*argv,"-dcert") == 0) 66674664626SKris Kennaway { 66774664626SKris Kennaway if (--argc < 1) goto bad; 66874664626SKris Kennaway s_dcert_file= *(++argv); 66974664626SKris Kennaway } 6703b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-dkeyform") == 0) 6713b4e3dcbSSimon L. B. Nielsen { 6723b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 6733b4e3dcbSSimon L. B. Nielsen s_dkey_format = str2fmt(*(++argv)); 6743b4e3dcbSSimon L. B. Nielsen } 6753b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-dpass") == 0) 6763b4e3dcbSSimon L. B. Nielsen { 6773b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 6783b4e3dcbSSimon L. B. Nielsen dpassarg = *(++argv); 6793b4e3dcbSSimon L. B. Nielsen } 68074664626SKris Kennaway else if (strcmp(*argv,"-dkey") == 0) 68174664626SKris Kennaway { 68274664626SKris Kennaway if (--argc < 1) goto bad; 68374664626SKris Kennaway s_dkey_file= *(++argv); 68474664626SKris Kennaway } 68574664626SKris Kennaway else if (strcmp(*argv,"-nocert") == 0) 68674664626SKris Kennaway { 68774664626SKris Kennaway nocert=1; 68874664626SKris Kennaway } 68974664626SKris Kennaway else if (strcmp(*argv,"-CApath") == 0) 69074664626SKris Kennaway { 69174664626SKris Kennaway if (--argc < 1) goto bad; 69274664626SKris Kennaway CApath= *(++argv); 69374664626SKris Kennaway } 6945c87c606SMark Murray else if (strcmp(*argv,"-crl_check") == 0) 6955c87c606SMark Murray { 6965c87c606SMark Murray vflags |= X509_V_FLAG_CRL_CHECK; 6975c87c606SMark Murray } 6985c87c606SMark Murray else if (strcmp(*argv,"-crl_check") == 0) 6995c87c606SMark Murray { 7005c87c606SMark Murray vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL; 7015c87c606SMark Murray } 7025c87c606SMark Murray else if (strcmp(*argv,"-serverpref") == 0) 7035c87c606SMark Murray { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; } 70474664626SKris Kennaway else if (strcmp(*argv,"-cipher") == 0) 70574664626SKris Kennaway { 70674664626SKris Kennaway if (--argc < 1) goto bad; 70774664626SKris Kennaway cipher= *(++argv); 70874664626SKris Kennaway } 70974664626SKris Kennaway else if (strcmp(*argv,"-CAfile") == 0) 71074664626SKris Kennaway { 71174664626SKris Kennaway if (--argc < 1) goto bad; 71274664626SKris Kennaway CAfile= *(++argv); 71374664626SKris Kennaway } 71474664626SKris Kennaway #ifdef FIONBIO 71574664626SKris Kennaway else if (strcmp(*argv,"-nbio") == 0) 71674664626SKris Kennaway { s_nbio=1; } 71774664626SKris Kennaway #endif 71874664626SKris Kennaway else if (strcmp(*argv,"-nbio_test") == 0) 71974664626SKris Kennaway { 72074664626SKris Kennaway #ifdef FIONBIO 72174664626SKris Kennaway s_nbio=1; 72274664626SKris Kennaway #endif 72374664626SKris Kennaway s_nbio_test=1; 72474664626SKris Kennaway } 72574664626SKris Kennaway else if (strcmp(*argv,"-debug") == 0) 72674664626SKris Kennaway { s_debug=1; } 7275c87c606SMark Murray else if (strcmp(*argv,"-msg") == 0) 7285c87c606SMark Murray { s_msg=1; } 72974664626SKris Kennaway else if (strcmp(*argv,"-hack") == 0) 73074664626SKris Kennaway { hack=1; } 73174664626SKris Kennaway else if (strcmp(*argv,"-state") == 0) 73274664626SKris Kennaway { state=1; } 73374664626SKris Kennaway else if (strcmp(*argv,"-crlf") == 0) 73474664626SKris Kennaway { s_crlf=1; } 73574664626SKris Kennaway else if (strcmp(*argv,"-quiet") == 0) 73674664626SKris Kennaway { s_quiet=1; } 73774664626SKris Kennaway else if (strcmp(*argv,"-bugs") == 0) 73874664626SKris Kennaway { bugs=1; } 73974664626SKris Kennaway else if (strcmp(*argv,"-no_tmp_rsa") == 0) 74074664626SKris Kennaway { no_tmp_rsa=1; } 74174664626SKris Kennaway else if (strcmp(*argv,"-no_dhe") == 0) 74274664626SKris Kennaway { no_dhe=1; } 7433b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-no_ecdhe") == 0) 7443b4e3dcbSSimon L. B. Nielsen { no_ecdhe=1; } 74574664626SKris Kennaway else if (strcmp(*argv,"-www") == 0) 74674664626SKris Kennaway { www=1; } 74774664626SKris Kennaway else if (strcmp(*argv,"-WWW") == 0) 74874664626SKris Kennaway { www=2; } 7495c87c606SMark Murray else if (strcmp(*argv,"-HTTP") == 0) 7505c87c606SMark Murray { www=3; } 75174664626SKris Kennaway else if (strcmp(*argv,"-no_ssl2") == 0) 75274664626SKris Kennaway { off|=SSL_OP_NO_SSLv2; } 75374664626SKris Kennaway else if (strcmp(*argv,"-no_ssl3") == 0) 75474664626SKris Kennaway { off|=SSL_OP_NO_SSLv3; } 75574664626SKris Kennaway else if (strcmp(*argv,"-no_tls1") == 0) 75674664626SKris Kennaway { off|=SSL_OP_NO_TLSv1; } 7575c87c606SMark Murray #ifndef OPENSSL_NO_SSL2 75874664626SKris Kennaway else if (strcmp(*argv,"-ssl2") == 0) 75974664626SKris Kennaway { meth=SSLv2_server_method(); } 76074664626SKris Kennaway #endif 7615c87c606SMark Murray #ifndef OPENSSL_NO_SSL3 76274664626SKris Kennaway else if (strcmp(*argv,"-ssl3") == 0) 76374664626SKris Kennaway { meth=SSLv3_server_method(); } 76474664626SKris Kennaway #endif 7655c87c606SMark Murray #ifndef OPENSSL_NO_TLS1 76674664626SKris Kennaway else if (strcmp(*argv,"-tls1") == 0) 76774664626SKris Kennaway { meth=TLSv1_server_method(); } 76874664626SKris Kennaway #endif 7693b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DTLS1 7703b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-dtls1") == 0) 7713b4e3dcbSSimon L. B. Nielsen { 7723b4e3dcbSSimon L. B. Nielsen meth=DTLSv1_server_method(); 7733b4e3dcbSSimon L. B. Nielsen sock_type = SOCK_DGRAM; 7743b4e3dcbSSimon L. B. Nielsen } 7753b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-timeout") == 0) 7763b4e3dcbSSimon L. B. Nielsen enable_timeouts = 1; 7773b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv,"-mtu") == 0) 7783b4e3dcbSSimon L. B. Nielsen { 7793b4e3dcbSSimon L. B. Nielsen if (--argc < 1) goto bad; 7803b4e3dcbSSimon L. B. Nielsen mtu = atol(*(++argv)); 7813b4e3dcbSSimon L. B. Nielsen } 7823b4e3dcbSSimon L. B. Nielsen else if (strcmp(*argv, "-chain") == 0) 7833b4e3dcbSSimon L. B. Nielsen cert_chain = 1; 7843b4e3dcbSSimon L. B. Nielsen #endif 7855c87c606SMark Murray else if (strcmp(*argv, "-id_prefix") == 0) 7865c87c606SMark Murray { 7875c87c606SMark Murray if (--argc < 1) goto bad; 7885c87c606SMark Murray session_id_prefix = *(++argv); 7895c87c606SMark Murray } 790fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 7915c87c606SMark Murray else if (strcmp(*argv,"-engine") == 0) 7925c87c606SMark Murray { 7935c87c606SMark Murray if (--argc < 1) goto bad; 7945c87c606SMark Murray engine_id= *(++argv); 7955c87c606SMark Murray } 796fceca8a3SJacques Vidrine #endif 7975740a5e3SKris Kennaway else if (strcmp(*argv,"-rand") == 0) 7985740a5e3SKris Kennaway { 7995740a5e3SKris Kennaway if (--argc < 1) goto bad; 8005740a5e3SKris Kennaway inrand= *(++argv); 8015740a5e3SKris Kennaway } 80274664626SKris Kennaway else 80374664626SKris Kennaway { 80474664626SKris Kennaway BIO_printf(bio_err,"unknown option %s\n",*argv); 80574664626SKris Kennaway badop=1; 80674664626SKris Kennaway break; 80774664626SKris Kennaway } 80874664626SKris Kennaway argc--; 80974664626SKris Kennaway argv++; 81074664626SKris Kennaway } 81174664626SKris Kennaway if (badop) 81274664626SKris Kennaway { 81374664626SKris Kennaway bad: 81474664626SKris Kennaway sv_usage(); 81574664626SKris Kennaway goto end; 81674664626SKris Kennaway } 81774664626SKris Kennaway 8185c87c606SMark Murray SSL_load_error_strings(); 8195c87c606SMark Murray OpenSSL_add_ssl_algorithms(); 8205c87c606SMark Murray 821fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE 8225c87c606SMark Murray e = setup_engine(bio_err, engine_id, 1); 823fceca8a3SJacques Vidrine #endif 8245c87c606SMark Murray 8253b4e3dcbSSimon L. B. Nielsen if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) 8263b4e3dcbSSimon L. B. Nielsen { 8273b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "Error getting password\n"); 8283b4e3dcbSSimon L. B. Nielsen goto end; 8293b4e3dcbSSimon L. B. Nielsen } 8303b4e3dcbSSimon L. B. Nielsen 8313b4e3dcbSSimon L. B. Nielsen 8323b4e3dcbSSimon L. B. Nielsen if (s_key_file == NULL) 8333b4e3dcbSSimon L. B. Nielsen s_key_file = s_cert_file; 8343b4e3dcbSSimon L. B. Nielsen 8353b4e3dcbSSimon L. B. Nielsen if (nocert == 0) 8363b4e3dcbSSimon L. B. Nielsen { 8373b4e3dcbSSimon L. B. Nielsen s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e, 8383b4e3dcbSSimon L. B. Nielsen "server certificate private key file"); 8393b4e3dcbSSimon L. B. Nielsen if (!s_key) 8403b4e3dcbSSimon L. B. Nielsen { 8413b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err); 8423b4e3dcbSSimon L. B. Nielsen goto end; 8433b4e3dcbSSimon L. B. Nielsen } 8443b4e3dcbSSimon L. B. Nielsen 8453b4e3dcbSSimon L. B. Nielsen s_cert = load_cert(bio_err,s_cert_file,s_cert_format, 8463b4e3dcbSSimon L. B. Nielsen NULL, e, "server certificate file"); 8473b4e3dcbSSimon L. B. Nielsen 8483b4e3dcbSSimon L. B. Nielsen if (!s_cert) 8493b4e3dcbSSimon L. B. Nielsen { 8503b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err); 8513b4e3dcbSSimon L. B. Nielsen goto end; 8523b4e3dcbSSimon L. B. Nielsen } 8533b4e3dcbSSimon L. B. Nielsen } 8543b4e3dcbSSimon L. B. Nielsen 8553b4e3dcbSSimon L. B. Nielsen if (s_dcert_file) 8563b4e3dcbSSimon L. B. Nielsen { 8573b4e3dcbSSimon L. B. Nielsen 8583b4e3dcbSSimon L. B. Nielsen if (s_dkey_file == NULL) 8593b4e3dcbSSimon L. B. Nielsen s_dkey_file = s_dcert_file; 8603b4e3dcbSSimon L. B. Nielsen 8613b4e3dcbSSimon L. B. Nielsen s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format, 8623b4e3dcbSSimon L. B. Nielsen 0, dpass, e, 8633b4e3dcbSSimon L. B. Nielsen "second certificate private key file"); 8643b4e3dcbSSimon L. B. Nielsen if (!s_dkey) 8653b4e3dcbSSimon L. B. Nielsen { 8663b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err); 8673b4e3dcbSSimon L. B. Nielsen goto end; 8683b4e3dcbSSimon L. B. Nielsen } 8693b4e3dcbSSimon L. B. Nielsen 8703b4e3dcbSSimon L. B. Nielsen s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format, 8713b4e3dcbSSimon L. B. Nielsen NULL, e, "second server certificate file"); 8723b4e3dcbSSimon L. B. Nielsen 8733b4e3dcbSSimon L. B. Nielsen if (!s_dcert) 8743b4e3dcbSSimon L. B. Nielsen { 8753b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err); 8763b4e3dcbSSimon L. B. Nielsen goto end; 8773b4e3dcbSSimon L. B. Nielsen } 8783b4e3dcbSSimon L. B. Nielsen 8793b4e3dcbSSimon L. B. Nielsen } 8803b4e3dcbSSimon L. B. Nielsen 8815740a5e3SKris Kennaway if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL 8825740a5e3SKris Kennaway && !RAND_status()) 8835740a5e3SKris Kennaway { 8845740a5e3SKris Kennaway BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); 8855740a5e3SKris Kennaway } 8865740a5e3SKris Kennaway if (inrand != NULL) 8875740a5e3SKris Kennaway BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 8885740a5e3SKris Kennaway app_RAND_load_files(inrand)); 889f579bf8eSKris Kennaway 89074664626SKris Kennaway if (bio_s_out == NULL) 89174664626SKris Kennaway { 8925c87c606SMark Murray if (s_quiet && !s_debug && !s_msg) 89374664626SKris Kennaway { 89474664626SKris Kennaway bio_s_out=BIO_new(BIO_s_null()); 89574664626SKris Kennaway } 89674664626SKris Kennaway else 89774664626SKris Kennaway { 89874664626SKris Kennaway if (bio_s_out == NULL) 89974664626SKris Kennaway bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE); 90074664626SKris Kennaway } 90174664626SKris Kennaway } 90274664626SKris Kennaway 9033b4e3dcbSSimon L. B. Nielsen #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA) 90474664626SKris Kennaway if (nocert) 90574664626SKris Kennaway #endif 90674664626SKris Kennaway { 90774664626SKris Kennaway s_cert_file=NULL; 90874664626SKris Kennaway s_key_file=NULL; 90974664626SKris Kennaway s_dcert_file=NULL; 91074664626SKris Kennaway s_dkey_file=NULL; 91174664626SKris Kennaway } 91274664626SKris Kennaway 91374664626SKris Kennaway ctx=SSL_CTX_new(meth); 91474664626SKris Kennaway if (ctx == NULL) 91574664626SKris Kennaway { 91674664626SKris Kennaway ERR_print_errors(bio_err); 91774664626SKris Kennaway goto end; 91874664626SKris Kennaway } 9195c87c606SMark Murray if (session_id_prefix) 9205c87c606SMark Murray { 9215c87c606SMark Murray if(strlen(session_id_prefix) >= 32) 9225c87c606SMark Murray BIO_printf(bio_err, 9235c87c606SMark Murray "warning: id_prefix is too long, only one new session will be possible\n"); 9245c87c606SMark Murray else if(strlen(session_id_prefix) >= 16) 9255c87c606SMark Murray BIO_printf(bio_err, 9265c87c606SMark Murray "warning: id_prefix is too long if you use SSLv2\n"); 9275c87c606SMark Murray if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) 9285c87c606SMark Murray { 9295c87c606SMark Murray BIO_printf(bio_err,"error setting 'id_prefix'\n"); 9305c87c606SMark Murray ERR_print_errors(bio_err); 9315c87c606SMark Murray goto end; 9325c87c606SMark Murray } 9335c87c606SMark Murray BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix); 9345c87c606SMark Murray } 93574664626SKris Kennaway SSL_CTX_set_quiet_shutdown(ctx,1); 93674664626SKris Kennaway if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL); 93774664626SKris Kennaway if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); 93874664626SKris Kennaway SSL_CTX_set_options(ctx,off); 9393b4e3dcbSSimon L. B. Nielsen /* DTLS: partial reads end up discarding unread UDP bytes :-( 9403b4e3dcbSSimon L. B. Nielsen * Setting read ahead solves this problem. 9413b4e3dcbSSimon L. B. Nielsen */ 9423b4e3dcbSSimon L. B. Nielsen if (sock_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1); 94374664626SKris Kennaway 94474664626SKris Kennaway if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); 94574664626SKris Kennaway 94674664626SKris Kennaway SSL_CTX_sess_set_cache_size(ctx,128); 94774664626SKris Kennaway 94874664626SKris Kennaway #if 0 94974664626SKris Kennaway if (cipher == NULL) cipher=getenv("SSL_CIPHER"); 95074664626SKris Kennaway #endif 95174664626SKris Kennaway 95274664626SKris Kennaway #if 0 95374664626SKris Kennaway if (s_cert_file == NULL) 95474664626SKris Kennaway { 95574664626SKris Kennaway BIO_printf(bio_err,"You must specify a certificate file for the server to use\n"); 95674664626SKris Kennaway goto end; 95774664626SKris Kennaway } 95874664626SKris Kennaway #endif 95974664626SKris Kennaway 96074664626SKris Kennaway if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) || 96174664626SKris Kennaway (!SSL_CTX_set_default_verify_paths(ctx))) 96274664626SKris Kennaway { 96374664626SKris Kennaway /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */ 96474664626SKris Kennaway ERR_print_errors(bio_err); 96574664626SKris Kennaway /* goto end; */ 96674664626SKris Kennaway } 9675c87c606SMark Murray store = SSL_CTX_get_cert_store(ctx); 9685c87c606SMark Murray X509_STORE_set_flags(store, vflags); 96974664626SKris Kennaway 9705c87c606SMark Murray #ifndef OPENSSL_NO_DH 97174664626SKris Kennaway if (!no_dhe) 97274664626SKris Kennaway { 9735c87c606SMark Murray DH *dh=NULL; 9745c87c606SMark Murray 9755c87c606SMark Murray if (dhfile) 9765c87c606SMark Murray dh = load_dh_param(dhfile); 9775c87c606SMark Murray else if (s_cert_file) 9785c87c606SMark Murray dh = load_dh_param(s_cert_file); 9795c87c606SMark Murray 98074664626SKris Kennaway if (dh != NULL) 98174664626SKris Kennaway { 98274664626SKris Kennaway BIO_printf(bio_s_out,"Setting temp DH parameters\n"); 98374664626SKris Kennaway } 98474664626SKris Kennaway else 98574664626SKris Kennaway { 98674664626SKris Kennaway BIO_printf(bio_s_out,"Using default temp DH parameters\n"); 98774664626SKris Kennaway dh=get_dh512(); 98874664626SKris Kennaway } 98974664626SKris Kennaway (void)BIO_flush(bio_s_out); 99074664626SKris Kennaway 99174664626SKris Kennaway SSL_CTX_set_tmp_dh(ctx,dh); 99274664626SKris Kennaway DH_free(dh); 99374664626SKris Kennaway } 99474664626SKris Kennaway #endif 99574664626SKris Kennaway 9963b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 9973b4e3dcbSSimon L. B. Nielsen if (!no_ecdhe) 99874664626SKris Kennaway { 9993b4e3dcbSSimon L. B. Nielsen EC_KEY *ecdh=NULL; 10003b4e3dcbSSimon L. B. Nielsen 10013b4e3dcbSSimon L. B. Nielsen if (named_curve) 10023b4e3dcbSSimon L. B. Nielsen { 10033b4e3dcbSSimon L. B. Nielsen int nid = OBJ_sn2nid(named_curve); 10043b4e3dcbSSimon L. B. Nielsen 10053b4e3dcbSSimon L. B. Nielsen if (nid == 0) 10063b4e3dcbSSimon L. B. Nielsen { 10073b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "unknown curve name (%s)\n", 10083b4e3dcbSSimon L. B. Nielsen named_curve); 10093b4e3dcbSSimon L. B. Nielsen goto end; 10103b4e3dcbSSimon L. B. Nielsen } 10113b4e3dcbSSimon L. B. Nielsen ecdh = EC_KEY_new_by_curve_name(nid); 10123b4e3dcbSSimon L. B. Nielsen if (ecdh == NULL) 10133b4e3dcbSSimon L. B. Nielsen { 10143b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "unable to create curve (%s)\n", 10153b4e3dcbSSimon L. B. Nielsen named_curve); 10163b4e3dcbSSimon L. B. Nielsen goto end; 10173b4e3dcbSSimon L. B. Nielsen } 10183b4e3dcbSSimon L. B. Nielsen } 10193b4e3dcbSSimon L. B. Nielsen 10203b4e3dcbSSimon L. B. Nielsen if (ecdh != NULL) 10213b4e3dcbSSimon L. B. Nielsen { 10223b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_s_out,"Setting temp ECDH parameters\n"); 10233b4e3dcbSSimon L. B. Nielsen } 10243b4e3dcbSSimon L. B. Nielsen else 10253b4e3dcbSSimon L. B. Nielsen { 10263b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_s_out,"Using default temp ECDH parameters\n"); 10273b4e3dcbSSimon L. B. Nielsen ecdh = EC_KEY_new_by_curve_name(NID_sect163r2); 10283b4e3dcbSSimon L. B. Nielsen if (ecdh == NULL) 10293b4e3dcbSSimon L. B. Nielsen { 10303b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "unable to create curve (sect163r2)\n"); 10313b4e3dcbSSimon L. B. Nielsen goto end; 10323b4e3dcbSSimon L. B. Nielsen } 10333b4e3dcbSSimon L. B. Nielsen } 10343b4e3dcbSSimon L. B. Nielsen (void)BIO_flush(bio_s_out); 10353b4e3dcbSSimon L. B. Nielsen 10363b4e3dcbSSimon L. B. Nielsen SSL_CTX_set_tmp_ecdh(ctx,ecdh); 10373b4e3dcbSSimon L. B. Nielsen EC_KEY_free(ecdh); 10383b4e3dcbSSimon L. B. Nielsen } 10393b4e3dcbSSimon L. B. Nielsen #endif 10403b4e3dcbSSimon L. B. Nielsen 10413b4e3dcbSSimon L. B. Nielsen if (!set_cert_key_stuff(ctx,s_cert,s_key)) 10423b4e3dcbSSimon L. B. Nielsen goto end; 10433b4e3dcbSSimon L. B. Nielsen if (s_dcert != NULL) 10443b4e3dcbSSimon L. B. Nielsen { 10453b4e3dcbSSimon L. B. Nielsen if (!set_cert_key_stuff(ctx,s_dcert,s_dkey)) 104674664626SKris Kennaway goto end; 104774664626SKris Kennaway } 104874664626SKris Kennaway 10495c87c606SMark Murray #ifndef OPENSSL_NO_RSA 105074664626SKris Kennaway #if 1 10515740a5e3SKris Kennaway if (!no_tmp_rsa) 105274664626SKris Kennaway SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb); 105374664626SKris Kennaway #else 105474664626SKris Kennaway if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx)) 105574664626SKris Kennaway { 105674664626SKris Kennaway RSA *rsa; 105774664626SKris Kennaway 105874664626SKris Kennaway BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key..."); 105974664626SKris Kennaway BIO_flush(bio_s_out); 106074664626SKris Kennaway 106174664626SKris Kennaway rsa=RSA_generate_key(512,RSA_F4,NULL); 106274664626SKris Kennaway 106374664626SKris Kennaway if (!SSL_CTX_set_tmp_rsa(ctx,rsa)) 106474664626SKris Kennaway { 106574664626SKris Kennaway ERR_print_errors(bio_err); 106674664626SKris Kennaway goto end; 106774664626SKris Kennaway } 106874664626SKris Kennaway RSA_free(rsa); 106974664626SKris Kennaway BIO_printf(bio_s_out,"\n"); 107074664626SKris Kennaway } 107174664626SKris Kennaway #endif 107274664626SKris Kennaway #endif 107374664626SKris Kennaway 107474664626SKris Kennaway if (cipher != NULL) 1075f579bf8eSKris Kennaway if(!SSL_CTX_set_cipher_list(ctx,cipher)) { 1076f579bf8eSKris Kennaway BIO_printf(bio_err,"error setting cipher list\n"); 1077f579bf8eSKris Kennaway ERR_print_errors(bio_err); 1078f579bf8eSKris Kennaway goto end; 1079f579bf8eSKris Kennaway } 108074664626SKris Kennaway SSL_CTX_set_verify(ctx,s_server_verify,verify_callback); 108174664626SKris Kennaway SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context, 108274664626SKris Kennaway sizeof s_server_session_id_context); 108374664626SKris Kennaway 1084f579bf8eSKris Kennaway if (CAfile != NULL) 108574664626SKris Kennaway SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile)); 108674664626SKris Kennaway 108774664626SKris Kennaway BIO_printf(bio_s_out,"ACCEPT\n"); 108874664626SKris Kennaway if (www) 10893b4e3dcbSSimon L. B. Nielsen do_server(port,sock_type,&accept_socket,www_body, context); 109074664626SKris Kennaway else 10913b4e3dcbSSimon L. B. Nielsen do_server(port,sock_type,&accept_socket,sv_body, context); 109274664626SKris Kennaway print_stats(bio_s_out,ctx); 109374664626SKris Kennaway ret=0; 109474664626SKris Kennaway end: 109574664626SKris Kennaway if (ctx != NULL) SSL_CTX_free(ctx); 10963b4e3dcbSSimon L. B. Nielsen if (s_cert) 10973b4e3dcbSSimon L. B. Nielsen X509_free(s_cert); 10983b4e3dcbSSimon L. B. Nielsen if (s_dcert) 10993b4e3dcbSSimon L. B. Nielsen X509_free(s_dcert); 11003b4e3dcbSSimon L. B. Nielsen if (s_key) 11013b4e3dcbSSimon L. B. Nielsen EVP_PKEY_free(s_key); 11023b4e3dcbSSimon L. B. Nielsen if (s_dkey) 11033b4e3dcbSSimon L. B. Nielsen EVP_PKEY_free(s_dkey); 11043b4e3dcbSSimon L. B. Nielsen if (pass) 11053b4e3dcbSSimon L. B. Nielsen OPENSSL_free(pass); 11063b4e3dcbSSimon L. B. Nielsen if (dpass) 11073b4e3dcbSSimon L. B. Nielsen OPENSSL_free(dpass); 110874664626SKris Kennaway if (bio_s_out != NULL) 110974664626SKris Kennaway { 111074664626SKris Kennaway BIO_free(bio_s_out); 111174664626SKris Kennaway bio_s_out=NULL; 111274664626SKris Kennaway } 11135c87c606SMark Murray apps_shutdown(); 11145c87c606SMark Murray OPENSSL_EXIT(ret); 111574664626SKris Kennaway } 111674664626SKris Kennaway 111774664626SKris Kennaway static void print_stats(BIO *bio, SSL_CTX *ssl_ctx) 111874664626SKris Kennaway { 111974664626SKris Kennaway BIO_printf(bio,"%4ld items in the session cache\n", 112074664626SKris Kennaway SSL_CTX_sess_number(ssl_ctx)); 11213b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld client connects (SSL_connect())\n", 112274664626SKris Kennaway SSL_CTX_sess_connect(ssl_ctx)); 11233b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld client renegotiates (SSL_connect())\n", 112474664626SKris Kennaway SSL_CTX_sess_connect_renegotiate(ssl_ctx)); 11253b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld client connects that finished\n", 112674664626SKris Kennaway SSL_CTX_sess_connect_good(ssl_ctx)); 11273b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld server accepts (SSL_accept())\n", 112874664626SKris Kennaway SSL_CTX_sess_accept(ssl_ctx)); 11293b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld server renegotiates (SSL_accept())\n", 113074664626SKris Kennaway SSL_CTX_sess_accept_renegotiate(ssl_ctx)); 11313b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld server accepts that finished\n", 113274664626SKris Kennaway SSL_CTX_sess_accept_good(ssl_ctx)); 11333b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld session cache hits\n",SSL_CTX_sess_hits(ssl_ctx)); 11343b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld session cache misses\n",SSL_CTX_sess_misses(ssl_ctx)); 11353b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx)); 11363b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx)); 11373b4e3dcbSSimon L. B. Nielsen BIO_printf(bio,"%4ld cache full overflows (%ld allowed)\n", 113874664626SKris Kennaway SSL_CTX_sess_cache_full(ssl_ctx), 113974664626SKris Kennaway SSL_CTX_sess_get_cache_size(ssl_ctx)); 114074664626SKris Kennaway } 114174664626SKris Kennaway 114274664626SKris Kennaway static int sv_body(char *hostname, int s, unsigned char *context) 114374664626SKris Kennaway { 114474664626SKris Kennaway char *buf=NULL; 114574664626SKris Kennaway fd_set readfds; 114674664626SKris Kennaway int ret=1,width; 114774664626SKris Kennaway int k,i; 114874664626SKris Kennaway unsigned long l; 114974664626SKris Kennaway SSL *con=NULL; 115074664626SKris Kennaway BIO *sbio; 11513b4e3dcbSSimon L. B. Nielsen #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) 1152f579bf8eSKris Kennaway struct timeval tv; 1153f579bf8eSKris Kennaway #endif 115474664626SKris Kennaway 1155ddd58736SKris Kennaway if ((buf=OPENSSL_malloc(bufsize)) == NULL) 115674664626SKris Kennaway { 115774664626SKris Kennaway BIO_printf(bio_err,"out of memory\n"); 115874664626SKris Kennaway goto err; 115974664626SKris Kennaway } 116074664626SKris Kennaway #ifdef FIONBIO 116174664626SKris Kennaway if (s_nbio) 116274664626SKris Kennaway { 116374664626SKris Kennaway unsigned long sl=1; 116474664626SKris Kennaway 116574664626SKris Kennaway if (!s_quiet) 116674664626SKris Kennaway BIO_printf(bio_err,"turning on non blocking io\n"); 116774664626SKris Kennaway if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0) 116874664626SKris Kennaway ERR_print_errors(bio_err); 116974664626SKris Kennaway } 117074664626SKris Kennaway #endif 117174664626SKris Kennaway 117274664626SKris Kennaway if (con == NULL) { 1173f579bf8eSKris Kennaway con=SSL_new(ctx); 11745c87c606SMark Murray #ifndef OPENSSL_NO_KRB5 11755c87c606SMark Murray if ((con->kssl_ctx = kssl_ctx_new()) != NULL) 11765c87c606SMark Murray { 11775c87c606SMark Murray kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, 11785c87c606SMark Murray KRB5SVC); 11795c87c606SMark Murray kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, 11805c87c606SMark Murray KRB5KEYTAB); 11815c87c606SMark Murray } 11825c87c606SMark Murray #endif /* OPENSSL_NO_KRB5 */ 118374664626SKris Kennaway if(context) 118474664626SKris Kennaway SSL_set_session_id_context(con, context, 118574664626SKris Kennaway strlen((char *)context)); 118674664626SKris Kennaway } 118774664626SKris Kennaway SSL_clear(con); 118874664626SKris Kennaway 11893b4e3dcbSSimon L. B. Nielsen if (SSL_version(con) == DTLS1_VERSION) 11903b4e3dcbSSimon L. B. Nielsen { 11913b4e3dcbSSimon L. B. Nielsen struct timeval timeout; 11923b4e3dcbSSimon L. B. Nielsen 11933b4e3dcbSSimon L. B. Nielsen sbio=BIO_new_dgram(s,BIO_NOCLOSE); 11943b4e3dcbSSimon L. B. Nielsen 11953b4e3dcbSSimon L. B. Nielsen if ( enable_timeouts) 11963b4e3dcbSSimon L. B. Nielsen { 11973b4e3dcbSSimon L. B. Nielsen timeout.tv_sec = 0; 11983b4e3dcbSSimon L. B. Nielsen timeout.tv_usec = DGRAM_RCV_TIMEOUT; 11993b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); 12003b4e3dcbSSimon L. B. Nielsen 12013b4e3dcbSSimon L. B. Nielsen timeout.tv_sec = 0; 12023b4e3dcbSSimon L. B. Nielsen timeout.tv_usec = DGRAM_SND_TIMEOUT; 12033b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); 12043b4e3dcbSSimon L. B. Nielsen } 12053b4e3dcbSSimon L. B. Nielsen 12063b4e3dcbSSimon L. B. Nielsen 12073b4e3dcbSSimon L. B. Nielsen if ( mtu > 0) 12083b4e3dcbSSimon L. B. Nielsen { 12093b4e3dcbSSimon L. B. Nielsen SSL_set_options(con, SSL_OP_NO_QUERY_MTU); 12103b4e3dcbSSimon L. B. Nielsen SSL_set_mtu(con, mtu); 12113b4e3dcbSSimon L. B. Nielsen } 12123b4e3dcbSSimon L. B. Nielsen else 12133b4e3dcbSSimon L. B. Nielsen /* want to do MTU discovery */ 12143b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); 12153b4e3dcbSSimon L. B. Nielsen 12163b4e3dcbSSimon L. B. Nielsen /* turn on cookie exchange */ 12173b4e3dcbSSimon L. B. Nielsen SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE); 12183b4e3dcbSSimon L. B. Nielsen } 12193b4e3dcbSSimon L. B. Nielsen else 122074664626SKris Kennaway sbio=BIO_new_socket(s,BIO_NOCLOSE); 12213b4e3dcbSSimon L. B. Nielsen 122274664626SKris Kennaway if (s_nbio_test) 122374664626SKris Kennaway { 122474664626SKris Kennaway BIO *test; 122574664626SKris Kennaway 122674664626SKris Kennaway test=BIO_new(BIO_f_nbio_test()); 122774664626SKris Kennaway sbio=BIO_push(test,sbio); 122874664626SKris Kennaway } 122974664626SKris Kennaway SSL_set_bio(con,sbio,sbio); 123074664626SKris Kennaway SSL_set_accept_state(con); 123174664626SKris Kennaway /* SSL_set_fd(con,s); */ 123274664626SKris Kennaway 123374664626SKris Kennaway if (s_debug) 123474664626SKris Kennaway { 123574664626SKris Kennaway con->debug=1; 12363b4e3dcbSSimon L. B. Nielsen BIO_set_callback(SSL_get_rbio(con),bio_dump_callback); 12375471f83eSSimon L. B. Nielsen BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out); 123874664626SKris Kennaway } 12395c87c606SMark Murray if (s_msg) 12405c87c606SMark Murray { 12415c87c606SMark Murray SSL_set_msg_callback(con, msg_cb); 12425c87c606SMark Murray SSL_set_msg_callback_arg(con, bio_s_out); 12435c87c606SMark Murray } 124474664626SKris Kennaway 124574664626SKris Kennaway width=s+1; 124674664626SKris Kennaway for (;;) 124774664626SKris Kennaway { 1248f579bf8eSKris Kennaway int read_from_terminal; 1249f579bf8eSKris Kennaway int read_from_sslcon; 1250f579bf8eSKris Kennaway 1251f579bf8eSKris Kennaway read_from_terminal = 0; 1252f579bf8eSKris Kennaway read_from_sslcon = SSL_pending(con); 1253f579bf8eSKris Kennaway 1254f579bf8eSKris Kennaway if (!read_from_sslcon) 1255f579bf8eSKris Kennaway { 125674664626SKris Kennaway FD_ZERO(&readfds); 12573b4e3dcbSSimon L. B. Nielsen #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) 125874664626SKris Kennaway FD_SET(fileno(stdin),&readfds); 125974664626SKris Kennaway #endif 126074664626SKris Kennaway FD_SET(s,&readfds); 126174664626SKris Kennaway /* Note: under VMS with SOCKETSHR the second parameter is 126274664626SKris Kennaway * currently of type (int *) whereas under other systems 126374664626SKris Kennaway * it is (void *) if you don't have a cast it will choke 126474664626SKris Kennaway * the compiler: if you do have a cast then you can either 126574664626SKris Kennaway * go for (int *) or (void *). 126674664626SKris Kennaway */ 12673b4e3dcbSSimon L. B. Nielsen #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) 126850ef0093SJacques Vidrine /* Under DOS (non-djgpp) and Windows we can't select on stdin: only 1269f579bf8eSKris Kennaway * on sockets. As a workaround we timeout the select every 1270f579bf8eSKris Kennaway * second and check for any keypress. In a proper Windows 1271f579bf8eSKris Kennaway * application we wouldn't do this because it is inefficient. 1272f579bf8eSKris Kennaway */ 1273f579bf8eSKris Kennaway tv.tv_sec = 1; 1274f579bf8eSKris Kennaway tv.tv_usec = 0; 1275f579bf8eSKris Kennaway i=select(width,(void *)&readfds,NULL,NULL,&tv); 1276f579bf8eSKris Kennaway if((i < 0) || (!i && !_kbhit() ) )continue; 1277f579bf8eSKris Kennaway if(_kbhit()) 1278f579bf8eSKris Kennaway read_from_terminal = 1; 1279f579bf8eSKris Kennaway #else 128074664626SKris Kennaway i=select(width,(void *)&readfds,NULL,NULL,NULL); 128174664626SKris Kennaway if (i <= 0) continue; 128274664626SKris Kennaway if (FD_ISSET(fileno(stdin),&readfds)) 1283f579bf8eSKris Kennaway read_from_terminal = 1; 1284f579bf8eSKris Kennaway #endif 1285f579bf8eSKris Kennaway if (FD_ISSET(s,&readfds)) 1286f579bf8eSKris Kennaway read_from_sslcon = 1; 1287f579bf8eSKris Kennaway } 1288f579bf8eSKris Kennaway if (read_from_terminal) 128974664626SKris Kennaway { 129074664626SKris Kennaway if (s_crlf) 129174664626SKris Kennaway { 129274664626SKris Kennaway int j, lf_num; 129374664626SKris Kennaway 129474664626SKris Kennaway i=read(fileno(stdin), buf, bufsize/2); 129574664626SKris Kennaway lf_num = 0; 129674664626SKris Kennaway /* both loops are skipped when i <= 0 */ 129774664626SKris Kennaway for (j = 0; j < i; j++) 129874664626SKris Kennaway if (buf[j] == '\n') 129974664626SKris Kennaway lf_num++; 130074664626SKris Kennaway for (j = i-1; j >= 0; j--) 130174664626SKris Kennaway { 130274664626SKris Kennaway buf[j+lf_num] = buf[j]; 130374664626SKris Kennaway if (buf[j] == '\n') 130474664626SKris Kennaway { 130574664626SKris Kennaway lf_num--; 130674664626SKris Kennaway i++; 130774664626SKris Kennaway buf[j+lf_num] = '\r'; 130874664626SKris Kennaway } 130974664626SKris Kennaway } 131074664626SKris Kennaway assert(lf_num == 0); 131174664626SKris Kennaway } 131274664626SKris Kennaway else 131374664626SKris Kennaway i=read(fileno(stdin),buf,bufsize); 131474664626SKris Kennaway if (!s_quiet) 131574664626SKris Kennaway { 131674664626SKris Kennaway if ((i <= 0) || (buf[0] == 'Q')) 131774664626SKris Kennaway { 131874664626SKris Kennaway BIO_printf(bio_s_out,"DONE\n"); 131974664626SKris Kennaway SHUTDOWN(s); 132074664626SKris Kennaway close_accept_socket(); 132174664626SKris Kennaway ret= -11; 132274664626SKris Kennaway goto err; 132374664626SKris Kennaway } 132474664626SKris Kennaway if ((i <= 0) || (buf[0] == 'q')) 132574664626SKris Kennaway { 132674664626SKris Kennaway BIO_printf(bio_s_out,"DONE\n"); 13273b4e3dcbSSimon L. B. Nielsen if (SSL_version(con) != DTLS1_VERSION) 132874664626SKris Kennaway SHUTDOWN(s); 132974664626SKris Kennaway /* close_accept_socket(); 133074664626SKris Kennaway ret= -11;*/ 133174664626SKris Kennaway goto err; 133274664626SKris Kennaway } 133374664626SKris Kennaway if ((buf[0] == 'r') && 133474664626SKris Kennaway ((buf[1] == '\n') || (buf[1] == '\r'))) 133574664626SKris Kennaway { 133674664626SKris Kennaway SSL_renegotiate(con); 133774664626SKris Kennaway i=SSL_do_handshake(con); 133874664626SKris Kennaway printf("SSL_do_handshake -> %d\n",i); 133974664626SKris Kennaway i=0; /*13; */ 134074664626SKris Kennaway continue; 134174664626SKris Kennaway /* strcpy(buf,"server side RE-NEGOTIATE\n"); */ 134274664626SKris Kennaway } 134374664626SKris Kennaway if ((buf[0] == 'R') && 134474664626SKris Kennaway ((buf[1] == '\n') || (buf[1] == '\r'))) 134574664626SKris Kennaway { 134674664626SKris Kennaway SSL_set_verify(con, 134774664626SKris Kennaway SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL); 134874664626SKris Kennaway SSL_renegotiate(con); 134974664626SKris Kennaway i=SSL_do_handshake(con); 135074664626SKris Kennaway printf("SSL_do_handshake -> %d\n",i); 135174664626SKris Kennaway i=0; /* 13; */ 135274664626SKris Kennaway continue; 135374664626SKris Kennaway /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */ 135474664626SKris Kennaway } 135574664626SKris Kennaway if (buf[0] == 'P') 135674664626SKris Kennaway { 13573b4e3dcbSSimon L. B. Nielsen static const char *str="Lets print some clear text\n"; 135874664626SKris Kennaway BIO_write(SSL_get_wbio(con),str,strlen(str)); 135974664626SKris Kennaway } 136074664626SKris Kennaway if (buf[0] == 'S') 136174664626SKris Kennaway { 136274664626SKris Kennaway print_stats(bio_s_out,SSL_get_SSL_CTX(con)); 136374664626SKris Kennaway } 136474664626SKris Kennaway } 136574664626SKris Kennaway #ifdef CHARSET_EBCDIC 136674664626SKris Kennaway ebcdic2ascii(buf,buf,i); 136774664626SKris Kennaway #endif 136874664626SKris Kennaway l=k=0; 136974664626SKris Kennaway for (;;) 137074664626SKris Kennaway { 137174664626SKris Kennaway /* should do a select for the write */ 137274664626SKris Kennaway #ifdef RENEG 137374664626SKris Kennaway { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } } 137474664626SKris Kennaway #endif 137574664626SKris Kennaway k=SSL_write(con,&(buf[l]),(unsigned int)i); 137674664626SKris Kennaway switch (SSL_get_error(con,k)) 137774664626SKris Kennaway { 137874664626SKris Kennaway case SSL_ERROR_NONE: 137974664626SKris Kennaway break; 138074664626SKris Kennaway case SSL_ERROR_WANT_WRITE: 138174664626SKris Kennaway case SSL_ERROR_WANT_READ: 138274664626SKris Kennaway case SSL_ERROR_WANT_X509_LOOKUP: 138374664626SKris Kennaway BIO_printf(bio_s_out,"Write BLOCK\n"); 138474664626SKris Kennaway break; 138574664626SKris Kennaway case SSL_ERROR_SYSCALL: 138674664626SKris Kennaway case SSL_ERROR_SSL: 138774664626SKris Kennaway BIO_printf(bio_s_out,"ERROR\n"); 138874664626SKris Kennaway ERR_print_errors(bio_err); 138974664626SKris Kennaway ret=1; 139074664626SKris Kennaway goto err; 139174664626SKris Kennaway /* break; */ 139274664626SKris Kennaway case SSL_ERROR_ZERO_RETURN: 139374664626SKris Kennaway BIO_printf(bio_s_out,"DONE\n"); 139474664626SKris Kennaway ret=1; 139574664626SKris Kennaway goto err; 139674664626SKris Kennaway } 139774664626SKris Kennaway l+=k; 139874664626SKris Kennaway i-=k; 139974664626SKris Kennaway if (i <= 0) break; 140074664626SKris Kennaway } 140174664626SKris Kennaway } 1402f579bf8eSKris Kennaway if (read_from_sslcon) 140374664626SKris Kennaway { 140474664626SKris Kennaway if (!SSL_is_init_finished(con)) 140574664626SKris Kennaway { 140674664626SKris Kennaway i=init_ssl_connection(con); 140774664626SKris Kennaway 140874664626SKris Kennaway if (i < 0) 140974664626SKris Kennaway { 141074664626SKris Kennaway ret=0; 141174664626SKris Kennaway goto err; 141274664626SKris Kennaway } 141374664626SKris Kennaway else if (i == 0) 141474664626SKris Kennaway { 141574664626SKris Kennaway ret=1; 141674664626SKris Kennaway goto err; 141774664626SKris Kennaway } 141874664626SKris Kennaway } 141974664626SKris Kennaway else 142074664626SKris Kennaway { 142174664626SKris Kennaway again: 142274664626SKris Kennaway i=SSL_read(con,(char *)buf,bufsize); 142374664626SKris Kennaway switch (SSL_get_error(con,i)) 142474664626SKris Kennaway { 142574664626SKris Kennaway case SSL_ERROR_NONE: 142674664626SKris Kennaway #ifdef CHARSET_EBCDIC 142774664626SKris Kennaway ascii2ebcdic(buf,buf,i); 142874664626SKris Kennaway #endif 142974664626SKris Kennaway write(fileno(stdout),buf, 143074664626SKris Kennaway (unsigned int)i); 143174664626SKris Kennaway if (SSL_pending(con)) goto again; 143274664626SKris Kennaway break; 143374664626SKris Kennaway case SSL_ERROR_WANT_WRITE: 143474664626SKris Kennaway case SSL_ERROR_WANT_READ: 143574664626SKris Kennaway case SSL_ERROR_WANT_X509_LOOKUP: 143674664626SKris Kennaway BIO_printf(bio_s_out,"Read BLOCK\n"); 143774664626SKris Kennaway break; 143874664626SKris Kennaway case SSL_ERROR_SYSCALL: 143974664626SKris Kennaway case SSL_ERROR_SSL: 144074664626SKris Kennaway BIO_printf(bio_s_out,"ERROR\n"); 144174664626SKris Kennaway ERR_print_errors(bio_err); 144274664626SKris Kennaway ret=1; 144374664626SKris Kennaway goto err; 144474664626SKris Kennaway case SSL_ERROR_ZERO_RETURN: 144574664626SKris Kennaway BIO_printf(bio_s_out,"DONE\n"); 144674664626SKris Kennaway ret=1; 144774664626SKris Kennaway goto err; 144874664626SKris Kennaway } 144974664626SKris Kennaway } 145074664626SKris Kennaway } 145174664626SKris Kennaway } 145274664626SKris Kennaway err: 145374664626SKris Kennaway BIO_printf(bio_s_out,"shutting down SSL\n"); 145474664626SKris Kennaway #if 1 145574664626SKris Kennaway SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 145674664626SKris Kennaway #else 145774664626SKris Kennaway SSL_shutdown(con); 145874664626SKris Kennaway #endif 145974664626SKris Kennaway if (con != NULL) SSL_free(con); 146074664626SKris Kennaway BIO_printf(bio_s_out,"CONNECTION CLOSED\n"); 146174664626SKris Kennaway if (buf != NULL) 146274664626SKris Kennaway { 14635c87c606SMark Murray OPENSSL_cleanse(buf,bufsize); 1464ddd58736SKris Kennaway OPENSSL_free(buf); 146574664626SKris Kennaway } 146674664626SKris Kennaway if (ret >= 0) 146774664626SKris Kennaway BIO_printf(bio_s_out,"ACCEPT\n"); 146874664626SKris Kennaway return(ret); 146974664626SKris Kennaway } 147074664626SKris Kennaway 147174664626SKris Kennaway static void close_accept_socket(void) 147274664626SKris Kennaway { 147374664626SKris Kennaway BIO_printf(bio_err,"shutdown accept socket\n"); 147474664626SKris Kennaway if (accept_socket >= 0) 147574664626SKris Kennaway { 147674664626SKris Kennaway SHUTDOWN2(accept_socket); 147774664626SKris Kennaway } 147874664626SKris Kennaway } 147974664626SKris Kennaway 148074664626SKris Kennaway static int init_ssl_connection(SSL *con) 148174664626SKris Kennaway { 148274664626SKris Kennaway int i; 148374664626SKris Kennaway const char *str; 148474664626SKris Kennaway X509 *peer; 148574664626SKris Kennaway long verify_error; 148674664626SKris Kennaway MS_STATIC char buf[BUFSIZ]; 148774664626SKris Kennaway 148874664626SKris Kennaway if ((i=SSL_accept(con)) <= 0) 148974664626SKris Kennaway { 149074664626SKris Kennaway if (BIO_sock_should_retry(i)) 149174664626SKris Kennaway { 149274664626SKris Kennaway BIO_printf(bio_s_out,"DELAY\n"); 149374664626SKris Kennaway return(1); 149474664626SKris Kennaway } 149574664626SKris Kennaway 149674664626SKris Kennaway BIO_printf(bio_err,"ERROR\n"); 149774664626SKris Kennaway verify_error=SSL_get_verify_result(con); 149874664626SKris Kennaway if (verify_error != X509_V_OK) 149974664626SKris Kennaway { 150074664626SKris Kennaway BIO_printf(bio_err,"verify error:%s\n", 150174664626SKris Kennaway X509_verify_cert_error_string(verify_error)); 150274664626SKris Kennaway } 150374664626SKris Kennaway else 150474664626SKris Kennaway ERR_print_errors(bio_err); 150574664626SKris Kennaway return(0); 150674664626SKris Kennaway } 150774664626SKris Kennaway 150874664626SKris Kennaway PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con)); 150974664626SKris Kennaway 151074664626SKris Kennaway peer=SSL_get_peer_certificate(con); 151174664626SKris Kennaway if (peer != NULL) 151274664626SKris Kennaway { 151374664626SKris Kennaway BIO_printf(bio_s_out,"Client certificate\n"); 151474664626SKris Kennaway PEM_write_bio_X509(bio_s_out,peer); 15155c87c606SMark Murray X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf); 151674664626SKris Kennaway BIO_printf(bio_s_out,"subject=%s\n",buf); 15175c87c606SMark Murray X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf); 151874664626SKris Kennaway BIO_printf(bio_s_out,"issuer=%s\n",buf); 151974664626SKris Kennaway X509_free(peer); 152074664626SKris Kennaway } 152174664626SKris Kennaway 15225c87c606SMark Murray if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL) 152374664626SKris Kennaway BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf); 152474664626SKris Kennaway str=SSL_CIPHER_get_name(SSL_get_current_cipher(con)); 152574664626SKris Kennaway BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)"); 152674664626SKris Kennaway if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n"); 152774664626SKris Kennaway if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) & 152874664626SKris Kennaway TLS1_FLAGS_TLS_PADDING_BUG) 152974664626SKris Kennaway BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n"); 153050ef0093SJacques Vidrine #ifndef OPENSSL_NO_KRB5 153150ef0093SJacques Vidrine if (con->kssl_ctx->client_princ != NULL) 153250ef0093SJacques Vidrine { 153350ef0093SJacques Vidrine BIO_printf(bio_s_out,"Kerberos peer principal is %s\n", 153450ef0093SJacques Vidrine con->kssl_ctx->client_princ); 153550ef0093SJacques Vidrine } 153650ef0093SJacques Vidrine #endif /* OPENSSL_NO_KRB5 */ 153774664626SKris Kennaway return(1); 153874664626SKris Kennaway } 153974664626SKris Kennaway 15405c87c606SMark Murray #ifndef OPENSSL_NO_DH 15413b4e3dcbSSimon L. B. Nielsen static DH *load_dh_param(const char *dhfile) 154274664626SKris Kennaway { 154374664626SKris Kennaway DH *ret=NULL; 154474664626SKris Kennaway BIO *bio; 154574664626SKris Kennaway 1546f579bf8eSKris Kennaway if ((bio=BIO_new_file(dhfile,"r")) == NULL) 154774664626SKris Kennaway goto err; 154874664626SKris Kennaway ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL); 154974664626SKris Kennaway err: 155074664626SKris Kennaway if (bio != NULL) BIO_free(bio); 155174664626SKris Kennaway return(ret); 155274664626SKris Kennaway } 155374664626SKris Kennaway #endif 155474664626SKris Kennaway 155574664626SKris Kennaway #if 0 155674664626SKris Kennaway static int load_CA(SSL_CTX *ctx, char *file) 155774664626SKris Kennaway { 155874664626SKris Kennaway FILE *in; 155974664626SKris Kennaway X509 *x=NULL; 156074664626SKris Kennaway 156174664626SKris Kennaway if ((in=fopen(file,"r")) == NULL) 156274664626SKris Kennaway return(0); 156374664626SKris Kennaway 156474664626SKris Kennaway for (;;) 156574664626SKris Kennaway { 156674664626SKris Kennaway if (PEM_read_X509(in,&x,NULL) == NULL) 156774664626SKris Kennaway break; 156874664626SKris Kennaway SSL_CTX_add_client_CA(ctx,x); 156974664626SKris Kennaway } 157074664626SKris Kennaway if (x != NULL) X509_free(x); 157174664626SKris Kennaway fclose(in); 157274664626SKris Kennaway return(1); 157374664626SKris Kennaway } 157474664626SKris Kennaway #endif 157574664626SKris Kennaway 157674664626SKris Kennaway static int www_body(char *hostname, int s, unsigned char *context) 157774664626SKris Kennaway { 157874664626SKris Kennaway char *buf=NULL; 157974664626SKris Kennaway int ret=1; 158074664626SKris Kennaway int i,j,k,blank,dot; 158174664626SKris Kennaway struct stat st_buf; 158274664626SKris Kennaway SSL *con; 158374664626SKris Kennaway SSL_CIPHER *c; 158474664626SKris Kennaway BIO *io,*ssl_bio,*sbio; 158574664626SKris Kennaway long total_bytes; 158674664626SKris Kennaway 1587ddd58736SKris Kennaway buf=OPENSSL_malloc(bufsize); 158874664626SKris Kennaway if (buf == NULL) return(0); 158974664626SKris Kennaway io=BIO_new(BIO_f_buffer()); 159074664626SKris Kennaway ssl_bio=BIO_new(BIO_f_ssl()); 159174664626SKris Kennaway if ((io == NULL) || (ssl_bio == NULL)) goto err; 159274664626SKris Kennaway 159374664626SKris Kennaway #ifdef FIONBIO 159474664626SKris Kennaway if (s_nbio) 159574664626SKris Kennaway { 159674664626SKris Kennaway unsigned long sl=1; 159774664626SKris Kennaway 159874664626SKris Kennaway if (!s_quiet) 159974664626SKris Kennaway BIO_printf(bio_err,"turning on non blocking io\n"); 160074664626SKris Kennaway if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0) 160174664626SKris Kennaway ERR_print_errors(bio_err); 160274664626SKris Kennaway } 160374664626SKris Kennaway #endif 160474664626SKris Kennaway 160574664626SKris Kennaway /* lets make the output buffer a reasonable size */ 160674664626SKris Kennaway if (!BIO_set_write_buffer_size(io,bufsize)) goto err; 160774664626SKris Kennaway 1608f579bf8eSKris Kennaway if ((con=SSL_new(ctx)) == NULL) goto err; 16095c87c606SMark Murray #ifndef OPENSSL_NO_KRB5 16105c87c606SMark Murray if ((con->kssl_ctx = kssl_ctx_new()) != NULL) 16115c87c606SMark Murray { 16125c87c606SMark Murray kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC); 16135c87c606SMark Murray kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB); 16145c87c606SMark Murray } 16155c87c606SMark Murray #endif /* OPENSSL_NO_KRB5 */ 161674664626SKris Kennaway if(context) SSL_set_session_id_context(con, context, 161774664626SKris Kennaway strlen((char *)context)); 161874664626SKris Kennaway 161974664626SKris Kennaway sbio=BIO_new_socket(s,BIO_NOCLOSE); 162074664626SKris Kennaway if (s_nbio_test) 162174664626SKris Kennaway { 162274664626SKris Kennaway BIO *test; 162374664626SKris Kennaway 162474664626SKris Kennaway test=BIO_new(BIO_f_nbio_test()); 162574664626SKris Kennaway sbio=BIO_push(test,sbio); 162674664626SKris Kennaway } 162774664626SKris Kennaway SSL_set_bio(con,sbio,sbio); 162874664626SKris Kennaway SSL_set_accept_state(con); 162974664626SKris Kennaway 163074664626SKris Kennaway /* SSL_set_fd(con,s); */ 163174664626SKris Kennaway BIO_set_ssl(ssl_bio,con,BIO_CLOSE); 163274664626SKris Kennaway BIO_push(io,ssl_bio); 163374664626SKris Kennaway #ifdef CHARSET_EBCDIC 163474664626SKris Kennaway io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io); 163574664626SKris Kennaway #endif 163674664626SKris Kennaway 163774664626SKris Kennaway if (s_debug) 163874664626SKris Kennaway { 163974664626SKris Kennaway con->debug=1; 16403b4e3dcbSSimon L. B. Nielsen BIO_set_callback(SSL_get_rbio(con),bio_dump_callback); 16415471f83eSSimon L. B. Nielsen BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out); 164274664626SKris Kennaway } 16435c87c606SMark Murray if (s_msg) 16445c87c606SMark Murray { 16455c87c606SMark Murray SSL_set_msg_callback(con, msg_cb); 16465c87c606SMark Murray SSL_set_msg_callback_arg(con, bio_s_out); 16475c87c606SMark Murray } 164874664626SKris Kennaway 164974664626SKris Kennaway blank=0; 165074664626SKris Kennaway for (;;) 165174664626SKris Kennaway { 165274664626SKris Kennaway if (hack) 165374664626SKris Kennaway { 165474664626SKris Kennaway i=SSL_accept(con); 165574664626SKris Kennaway 165674664626SKris Kennaway switch (SSL_get_error(con,i)) 165774664626SKris Kennaway { 165874664626SKris Kennaway case SSL_ERROR_NONE: 165974664626SKris Kennaway break; 166074664626SKris Kennaway case SSL_ERROR_WANT_WRITE: 166174664626SKris Kennaway case SSL_ERROR_WANT_READ: 166274664626SKris Kennaway case SSL_ERROR_WANT_X509_LOOKUP: 166374664626SKris Kennaway continue; 166474664626SKris Kennaway case SSL_ERROR_SYSCALL: 166574664626SKris Kennaway case SSL_ERROR_SSL: 166674664626SKris Kennaway case SSL_ERROR_ZERO_RETURN: 166774664626SKris Kennaway ret=1; 166874664626SKris Kennaway goto err; 166974664626SKris Kennaway /* break; */ 167074664626SKris Kennaway } 167174664626SKris Kennaway 167274664626SKris Kennaway SSL_renegotiate(con); 167374664626SKris Kennaway SSL_write(con,NULL,0); 167474664626SKris Kennaway } 167574664626SKris Kennaway 167674664626SKris Kennaway i=BIO_gets(io,buf,bufsize-1); 167774664626SKris Kennaway if (i < 0) /* error */ 167874664626SKris Kennaway { 167974664626SKris Kennaway if (!BIO_should_retry(io)) 168074664626SKris Kennaway { 168174664626SKris Kennaway if (!s_quiet) 168274664626SKris Kennaway ERR_print_errors(bio_err); 168374664626SKris Kennaway goto err; 168474664626SKris Kennaway } 168574664626SKris Kennaway else 168674664626SKris Kennaway { 168774664626SKris Kennaway BIO_printf(bio_s_out,"read R BLOCK\n"); 16883b4e3dcbSSimon L. B. Nielsen #if defined(OPENSSL_SYS_NETWARE) 16893b4e3dcbSSimon L. B. Nielsen delay(1000); 16903b4e3dcbSSimon L. B. Nielsen #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__) 169174664626SKris Kennaway sleep(1); 169274664626SKris Kennaway #endif 169374664626SKris Kennaway continue; 169474664626SKris Kennaway } 169574664626SKris Kennaway } 169674664626SKris Kennaway else if (i == 0) /* end of input */ 169774664626SKris Kennaway { 169874664626SKris Kennaway ret=1; 169974664626SKris Kennaway goto end; 170074664626SKris Kennaway } 170174664626SKris Kennaway 170274664626SKris Kennaway /* else we have data */ 170374664626SKris Kennaway if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) || 170474664626SKris Kennaway ((www == 2) && (strncmp("GET /stats ",buf,10) == 0))) 170574664626SKris Kennaway { 170674664626SKris Kennaway char *p; 170774664626SKris Kennaway X509 *peer; 170874664626SKris Kennaway STACK_OF(SSL_CIPHER) *sk; 17093b4e3dcbSSimon L. B. Nielsen static const char *space=" "; 171074664626SKris Kennaway 171174664626SKris Kennaway BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 171274664626SKris Kennaway BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n"); 171374664626SKris Kennaway BIO_puts(io,"<pre>\n"); 171474664626SKris Kennaway /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/ 171574664626SKris Kennaway BIO_puts(io,"\n"); 171674664626SKris Kennaway for (i=0; i<local_argc; i++) 171774664626SKris Kennaway { 171874664626SKris Kennaway BIO_puts(io,local_argv[i]); 171974664626SKris Kennaway BIO_write(io," ",1); 172074664626SKris Kennaway } 172174664626SKris Kennaway BIO_puts(io,"\n"); 172274664626SKris Kennaway 172374664626SKris Kennaway /* The following is evil and should not really 172474664626SKris Kennaway * be done */ 172574664626SKris Kennaway BIO_printf(io,"Ciphers supported in s_server binary\n"); 172674664626SKris Kennaway sk=SSL_get_ciphers(con); 172774664626SKris Kennaway j=sk_SSL_CIPHER_num(sk); 172874664626SKris Kennaway for (i=0; i<j; i++) 172974664626SKris Kennaway { 173074664626SKris Kennaway c=sk_SSL_CIPHER_value(sk,i); 173174664626SKris Kennaway BIO_printf(io,"%-11s:%-25s", 173274664626SKris Kennaway SSL_CIPHER_get_version(c), 173374664626SKris Kennaway SSL_CIPHER_get_name(c)); 173474664626SKris Kennaway if ((((i+1)%2) == 0) && (i+1 != j)) 173574664626SKris Kennaway BIO_puts(io,"\n"); 173674664626SKris Kennaway } 173774664626SKris Kennaway BIO_puts(io,"\n"); 173874664626SKris Kennaway p=SSL_get_shared_ciphers(con,buf,bufsize); 173974664626SKris Kennaway if (p != NULL) 174074664626SKris Kennaway { 174174664626SKris Kennaway BIO_printf(io,"---\nCiphers common between both SSL end points:\n"); 174274664626SKris Kennaway j=i=0; 174374664626SKris Kennaway while (*p) 174474664626SKris Kennaway { 174574664626SKris Kennaway if (*p == ':') 174674664626SKris Kennaway { 174774664626SKris Kennaway BIO_write(io,space,26-j); 174874664626SKris Kennaway i++; 174974664626SKris Kennaway j=0; 175074664626SKris Kennaway BIO_write(io,((i%3)?" ":"\n"),1); 175174664626SKris Kennaway } 175274664626SKris Kennaway else 175374664626SKris Kennaway { 175474664626SKris Kennaway BIO_write(io,p,1); 175574664626SKris Kennaway j++; 175674664626SKris Kennaway } 175774664626SKris Kennaway p++; 175874664626SKris Kennaway } 175974664626SKris Kennaway BIO_puts(io,"\n"); 176074664626SKris Kennaway } 176174664626SKris Kennaway BIO_printf(io,((con->hit) 176274664626SKris Kennaway ?"---\nReused, " 176374664626SKris Kennaway :"---\nNew, ")); 176474664626SKris Kennaway c=SSL_get_current_cipher(con); 176574664626SKris Kennaway BIO_printf(io,"%s, Cipher is %s\n", 176674664626SKris Kennaway SSL_CIPHER_get_version(c), 176774664626SKris Kennaway SSL_CIPHER_get_name(c)); 176874664626SKris Kennaway SSL_SESSION_print(io,SSL_get_session(con)); 176974664626SKris Kennaway BIO_printf(io,"---\n"); 177074664626SKris Kennaway print_stats(io,SSL_get_SSL_CTX(con)); 177174664626SKris Kennaway BIO_printf(io,"---\n"); 177274664626SKris Kennaway peer=SSL_get_peer_certificate(con); 177374664626SKris Kennaway if (peer != NULL) 177474664626SKris Kennaway { 177574664626SKris Kennaway BIO_printf(io,"Client certificate\n"); 177674664626SKris Kennaway X509_print(io,peer); 177774664626SKris Kennaway PEM_write_bio_X509(io,peer); 177874664626SKris Kennaway } 177974664626SKris Kennaway else 178074664626SKris Kennaway BIO_puts(io,"no client certificate available\n"); 178174664626SKris Kennaway BIO_puts(io,"</BODY></HTML>\r\n\r\n"); 178274664626SKris Kennaway break; 178374664626SKris Kennaway } 17845c87c606SMark Murray else if ((www == 2 || www == 3) 17855c87c606SMark Murray && (strncmp("GET /",buf,5) == 0)) 178674664626SKris Kennaway { 178774664626SKris Kennaway BIO *file; 178874664626SKris Kennaway char *p,*e; 17893b4e3dcbSSimon L. B. Nielsen static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"; 179074664626SKris Kennaway 179174664626SKris Kennaway /* skip the '/' */ 179274664626SKris Kennaway p= &(buf[5]); 17935740a5e3SKris Kennaway 17945740a5e3SKris Kennaway dot = 1; 179574664626SKris Kennaway for (e=p; *e != '\0'; e++) 179674664626SKris Kennaway { 17975740a5e3SKris Kennaway if (e[0] == ' ') 17985740a5e3SKris Kennaway break; 179974664626SKris Kennaway 18005740a5e3SKris Kennaway switch (dot) 18015740a5e3SKris Kennaway { 18025740a5e3SKris Kennaway case 1: 18035740a5e3SKris Kennaway dot = (e[0] == '.') ? 2 : 0; 18045740a5e3SKris Kennaway break; 18055740a5e3SKris Kennaway case 2: 18065740a5e3SKris Kennaway dot = (e[0] == '.') ? 3 : 0; 18075740a5e3SKris Kennaway break; 18085740a5e3SKris Kennaway case 3: 18095740a5e3SKris Kennaway dot = (e[0] == '/') ? -1 : 0; 18105740a5e3SKris Kennaway break; 18115740a5e3SKris Kennaway } 18125740a5e3SKris Kennaway if (dot == 0) 18135740a5e3SKris Kennaway dot = (e[0] == '/') ? 1 : 0; 18145740a5e3SKris Kennaway } 18155740a5e3SKris Kennaway dot = (dot == 3) || (dot == -1); /* filename contains ".." component */ 181674664626SKris Kennaway 181774664626SKris Kennaway if (*e == '\0') 181874664626SKris Kennaway { 181974664626SKris Kennaway BIO_puts(io,text); 182074664626SKris Kennaway BIO_printf(io,"'%s' is an invalid file name\r\n",p); 182174664626SKris Kennaway break; 182274664626SKris Kennaway } 182374664626SKris Kennaway *e='\0'; 182474664626SKris Kennaway 182574664626SKris Kennaway if (dot) 182674664626SKris Kennaway { 182774664626SKris Kennaway BIO_puts(io,text); 182874664626SKris Kennaway BIO_printf(io,"'%s' contains '..' reference\r\n",p); 182974664626SKris Kennaway break; 183074664626SKris Kennaway } 183174664626SKris Kennaway 183274664626SKris Kennaway if (*p == '/') 183374664626SKris Kennaway { 183474664626SKris Kennaway BIO_puts(io,text); 183574664626SKris Kennaway BIO_printf(io,"'%s' is an invalid path\r\n",p); 183674664626SKris Kennaway break; 183774664626SKris Kennaway } 183874664626SKris Kennaway 18395740a5e3SKris Kennaway #if 0 184074664626SKris Kennaway /* append if a directory lookup */ 184174664626SKris Kennaway if (e[-1] == '/') 184274664626SKris Kennaway strcat(p,"index.html"); 18435740a5e3SKris Kennaway #endif 184474664626SKris Kennaway 184574664626SKris Kennaway /* if a directory, do the index thang */ 184674664626SKris Kennaway if (stat(p,&st_buf) < 0) 184774664626SKris Kennaway { 184874664626SKris Kennaway BIO_puts(io,text); 184974664626SKris Kennaway BIO_printf(io,"Error accessing '%s'\r\n",p); 185074664626SKris Kennaway ERR_print_errors(io); 185174664626SKris Kennaway break; 185274664626SKris Kennaway } 185374664626SKris Kennaway if (S_ISDIR(st_buf.st_mode)) 185474664626SKris Kennaway { 18555740a5e3SKris Kennaway #if 0 /* must check buffer size */ 185674664626SKris Kennaway strcat(p,"/index.html"); 18575740a5e3SKris Kennaway #else 18585740a5e3SKris Kennaway BIO_puts(io,text); 18595740a5e3SKris Kennaway BIO_printf(io,"'%s' is a directory\r\n",p); 18605740a5e3SKris Kennaway break; 18615740a5e3SKris Kennaway #endif 186274664626SKris Kennaway } 186374664626SKris Kennaway 186474664626SKris Kennaway if ((file=BIO_new_file(p,"r")) == NULL) 186574664626SKris Kennaway { 186674664626SKris Kennaway BIO_puts(io,text); 186774664626SKris Kennaway BIO_printf(io,"Error opening '%s'\r\n",p); 186874664626SKris Kennaway ERR_print_errors(io); 186974664626SKris Kennaway break; 187074664626SKris Kennaway } 187174664626SKris Kennaway 187274664626SKris Kennaway if (!s_quiet) 187374664626SKris Kennaway BIO_printf(bio_err,"FILE:%s\n",p); 187474664626SKris Kennaway 18755c87c606SMark Murray if (www == 2) 18765c87c606SMark Murray { 187774664626SKris Kennaway i=strlen(p); 187874664626SKris Kennaway if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) || 187974664626SKris Kennaway ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) || 188074664626SKris Kennaway ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0))) 188174664626SKris Kennaway BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 188274664626SKris Kennaway else 188374664626SKris Kennaway BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"); 18845c87c606SMark Murray } 188574664626SKris Kennaway /* send the file */ 188674664626SKris Kennaway total_bytes=0; 188774664626SKris Kennaway for (;;) 188874664626SKris Kennaway { 188974664626SKris Kennaway i=BIO_read(file,buf,bufsize); 189074664626SKris Kennaway if (i <= 0) break; 189174664626SKris Kennaway 189274664626SKris Kennaway #ifdef RENEG 189374664626SKris Kennaway total_bytes+=i; 189474664626SKris Kennaway fprintf(stderr,"%d\n",i); 189574664626SKris Kennaway if (total_bytes > 3*1024) 189674664626SKris Kennaway { 189774664626SKris Kennaway total_bytes=0; 189874664626SKris Kennaway fprintf(stderr,"RENEGOTIATE\n"); 189974664626SKris Kennaway SSL_renegotiate(con); 190074664626SKris Kennaway } 190174664626SKris Kennaway #endif 190274664626SKris Kennaway 190374664626SKris Kennaway for (j=0; j<i; ) 190474664626SKris Kennaway { 190574664626SKris Kennaway #ifdef RENEG 190674664626SKris Kennaway { static count=0; if (++count == 13) { SSL_renegotiate(con); } } 190774664626SKris Kennaway #endif 190874664626SKris Kennaway k=BIO_write(io,&(buf[j]),i-j); 190974664626SKris Kennaway if (k <= 0) 191074664626SKris Kennaway { 191174664626SKris Kennaway if (!BIO_should_retry(io)) 191274664626SKris Kennaway goto write_error; 191374664626SKris Kennaway else 191474664626SKris Kennaway { 191574664626SKris Kennaway BIO_printf(bio_s_out,"rwrite W BLOCK\n"); 191674664626SKris Kennaway } 191774664626SKris Kennaway } 191874664626SKris Kennaway else 191974664626SKris Kennaway { 192074664626SKris Kennaway j+=k; 192174664626SKris Kennaway } 192274664626SKris Kennaway } 192374664626SKris Kennaway } 192474664626SKris Kennaway write_error: 192574664626SKris Kennaway BIO_free(file); 192674664626SKris Kennaway break; 192774664626SKris Kennaway } 192874664626SKris Kennaway } 192974664626SKris Kennaway 193074664626SKris Kennaway for (;;) 193174664626SKris Kennaway { 193274664626SKris Kennaway i=(int)BIO_flush(io); 193374664626SKris Kennaway if (i <= 0) 193474664626SKris Kennaway { 193574664626SKris Kennaway if (!BIO_should_retry(io)) 193674664626SKris Kennaway break; 193774664626SKris Kennaway } 193874664626SKris Kennaway else 193974664626SKris Kennaway break; 194074664626SKris Kennaway } 194174664626SKris Kennaway end: 194274664626SKris Kennaway #if 1 194374664626SKris Kennaway /* make sure we re-use sessions */ 194474664626SKris Kennaway SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 194574664626SKris Kennaway #else 1946f579bf8eSKris Kennaway /* This kills performance */ 194774664626SKris Kennaway /* SSL_shutdown(con); A shutdown gets sent in the 194874664626SKris Kennaway * BIO_free_all(io) procession */ 194974664626SKris Kennaway #endif 195074664626SKris Kennaway 195174664626SKris Kennaway err: 195274664626SKris Kennaway 195374664626SKris Kennaway if (ret >= 0) 195474664626SKris Kennaway BIO_printf(bio_s_out,"ACCEPT\n"); 195574664626SKris Kennaway 1956ddd58736SKris Kennaway if (buf != NULL) OPENSSL_free(buf); 195774664626SKris Kennaway if (io != NULL) BIO_free_all(io); 195874664626SKris Kennaway /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/ 195974664626SKris Kennaway return(ret); 196074664626SKris Kennaway } 196174664626SKris Kennaway 19625c87c606SMark Murray #ifndef OPENSSL_NO_RSA 196374664626SKris Kennaway static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength) 196474664626SKris Kennaway { 19653b4e3dcbSSimon L. B. Nielsen BIGNUM *bn = NULL; 196674664626SKris Kennaway static RSA *rsa_tmp=NULL; 196774664626SKris Kennaway 19683b4e3dcbSSimon L. B. Nielsen if (!rsa_tmp && ((bn = BN_new()) == NULL)) 19693b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err,"Allocation error in generating RSA key\n"); 19703b4e3dcbSSimon L. B. Nielsen if (!rsa_tmp && bn) 197174664626SKris Kennaway { 197274664626SKris Kennaway if (!s_quiet) 197374664626SKris Kennaway { 197474664626SKris Kennaway BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength); 197574664626SKris Kennaway (void)BIO_flush(bio_err); 197674664626SKris Kennaway } 19773b4e3dcbSSimon L. B. Nielsen if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) || 19783b4e3dcbSSimon L. B. Nielsen !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) 19793b4e3dcbSSimon L. B. Nielsen { 19803b4e3dcbSSimon L. B. Nielsen if(rsa_tmp) RSA_free(rsa_tmp); 19813b4e3dcbSSimon L. B. Nielsen rsa_tmp = NULL; 19823b4e3dcbSSimon L. B. Nielsen } 198374664626SKris Kennaway if (!s_quiet) 198474664626SKris Kennaway { 198574664626SKris Kennaway BIO_printf(bio_err,"\n"); 198674664626SKris Kennaway (void)BIO_flush(bio_err); 198774664626SKris Kennaway } 19883b4e3dcbSSimon L. B. Nielsen BN_free(bn); 198974664626SKris Kennaway } 199074664626SKris Kennaway return(rsa_tmp); 199174664626SKris Kennaway } 199274664626SKris Kennaway #endif 19935c87c606SMark Murray 19945c87c606SMark Murray #define MAX_SESSION_ID_ATTEMPTS 10 19955c87c606SMark Murray static int generate_session_id(const SSL *ssl, unsigned char *id, 19965c87c606SMark Murray unsigned int *id_len) 19975c87c606SMark Murray { 19985c87c606SMark Murray unsigned int count = 0; 19995c87c606SMark Murray do { 20005c87c606SMark Murray RAND_pseudo_bytes(id, *id_len); 20015c87c606SMark Murray /* Prefix the session_id with the required prefix. NB: If our 20025c87c606SMark Murray * prefix is too long, clip it - but there will be worse effects 20035c87c606SMark Murray * anyway, eg. the server could only possibly create 1 session 20045c87c606SMark Murray * ID (ie. the prefix!) so all future session negotiations will 20055c87c606SMark Murray * fail due to conflicts. */ 20065c87c606SMark Murray memcpy(id, session_id_prefix, 20075c87c606SMark Murray (strlen(session_id_prefix) < *id_len) ? 20085c87c606SMark Murray strlen(session_id_prefix) : *id_len); 20095c87c606SMark Murray } 20105c87c606SMark Murray while(SSL_has_matching_session_id(ssl, id, *id_len) && 20115c87c606SMark Murray (++count < MAX_SESSION_ID_ATTEMPTS)); 20125c87c606SMark Murray if(count >= MAX_SESSION_ID_ATTEMPTS) 20135c87c606SMark Murray return 0; 20145c87c606SMark Murray return 1; 20155c87c606SMark Murray } 2016