os/ossrv/ssl/libssl/src/ssltest.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libssl/src/ssltest.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,2294 @@
     1.4 +/* ssl/ssltest.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/* ====================================================================
    1.62 + * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
    1.63 + *
    1.64 + * Redistribution and use in source and binary forms, with or without
    1.65 + * modification, are permitted provided that the following conditions
    1.66 + * are met:
    1.67 + *
    1.68 + * 1. Redistributions of source code must retain the above copyright
    1.69 + *    notice, this list of conditions and the following disclaimer. 
    1.70 + *
    1.71 + * 2. Redistributions in binary form must reproduce the above copyright
    1.72 + *    notice, this list of conditions and the following disclaimer in
    1.73 + *    the documentation and/or other materials provided with the
    1.74 + *    distribution.
    1.75 + *
    1.76 + * 3. All advertising materials mentioning features or use of this
    1.77 + *    software must display the following acknowledgment:
    1.78 + *    "This product includes software developed by the OpenSSL Project
    1.79 + *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    1.80 + *
    1.81 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.82 + *    endorse or promote products derived from this software without
    1.83 + *    prior written permission. For written permission, please contact
    1.84 + *    openssl-core@openssl.org.
    1.85 + *
    1.86 + * 5. Products derived from this software may not be called "OpenSSL"
    1.87 + *    nor may "OpenSSL" appear in their names without prior written
    1.88 + *    permission of the OpenSSL Project.
    1.89 + *
    1.90 + * 6. Redistributions of any form whatsoever must retain the following
    1.91 + *    acknowledgment:
    1.92 + *    "This product includes software developed by the OpenSSL Project
    1.93 + *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    1.94 + *
    1.95 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.96 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.97 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.98 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.99 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   1.100 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   1.101 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   1.102 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   1.103 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   1.104 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   1.105 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   1.106 + * OF THE POSSIBILITY OF SUCH DAMAGE.
   1.107 + * ====================================================================
   1.108 + *
   1.109 + * This product includes cryptographic software written by Eric Young
   1.110 + * (eay@cryptsoft.com).  This product includes software written by Tim
   1.111 + * Hudson (tjh@cryptsoft.com).
   1.112 + *
   1.113 + */
   1.114 +/* ====================================================================
   1.115 + * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
   1.116 + * ECC cipher suite support in OpenSSL originally developed by 
   1.117 + * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
   1.118 + */
   1.119 +
   1.120 +#define _BSD_SOURCE 1		/* Or gethostname won't be declared properly
   1.121 +				   on Linux and GNU platforms. */
   1.122 +
   1.123 +#include <assert.h>
   1.124 +#include <errno.h>
   1.125 +#include <limits.h>
   1.126 +#include <stdio.h>
   1.127 +#include <stdlib.h>
   1.128 +#include <string.h>
   1.129 +#include <time.h>
   1.130 +
   1.131 +#define USE_SOCKETS
   1.132 +#include "e_os.h"
   1.133 +
   1.134 +#define _XOPEN_SOURCE 500	/* Or isascii won't be declared properly on
   1.135 +				   VMS (at least with DECompHP C).  */
   1.136 +#include <ctype.h>
   1.137 +
   1.138 +#include <openssl/bio.h>
   1.139 +#include <openssl/crypto.h>
   1.140 +#include <openssl/evp.h>
   1.141 +#include <openssl/x509.h>
   1.142 +#include <openssl/x509v3.h>
   1.143 +#include <openssl/ssl.h>
   1.144 +#ifndef OPENSSL_NO_ENGINE
   1.145 +#include <openssl/engine.h>
   1.146 +#endif
   1.147 +#include <openssl/err.h>
   1.148 +#include <openssl/rand.h>
   1.149 +#ifndef OPENSSL_NO_RSA
   1.150 +#include <openssl/rsa.h>
   1.151 +#endif
   1.152 +#ifndef OPENSSL_NO_DSA
   1.153 +#include <openssl/dsa.h>
   1.154 +#endif
   1.155 +#ifndef OPENSSL_NO_DH
   1.156 +#include <openssl/dh.h>
   1.157 +#endif
   1.158 +#include <openssl/bn.h>
   1.159 +
   1.160 +#define _XOPEN_SOURCE_EXTENDED	1 /* Or gethostname won't be declared properly
   1.161 +				     on Compaq platforms (at least with DEC C).
   1.162 +				     Do not try to put it earlier, or IPv6 includes
   1.163 +				     get screwed...
   1.164 +				  */
   1.165 +
   1.166 +#ifdef OPENSSL_SYS_WINDOWS
   1.167 +#include <winsock.h>
   1.168 +#else
   1.169 +#include OPENSSL_UNISTD
   1.170 +#endif
   1.171 +
   1.172 +#ifdef OPENSSL_SYS_VMS
   1.173 +#  define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
   1.174 +#  define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
   1.175 +#elif defined(OPENSSL_SYS_WINCE)
   1.176 +#  define TEST_SERVER_CERT "\\OpenSSL\\server.pem"
   1.177 +#  define TEST_CLIENT_CERT "\\OpenSSL\\client.pem"
   1.178 +#elif defined(OPENSSL_SYS_NETWARE)
   1.179 +#  define TEST_SERVER_CERT "\\openssl\\apps\\server.pem"
   1.180 +#  define TEST_CLIENT_CERT "\\openssl\\apps\\client.pem"
   1.181 +#else
   1.182 +#  define TEST_SERVER_CERT "../apps/server.pem"
   1.183 +#  define TEST_CLIENT_CERT "../apps/client.pem"
   1.184 +#endif
   1.185 +
   1.186 +/* There is really no standard for this, so let's assign some tentative
   1.187 +   numbers.  In any case, these numbers are only for this test */
   1.188 +#define COMP_RLE	255
   1.189 +#define COMP_ZLIB	1
   1.190 +
   1.191 +static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
   1.192 +#ifndef OPENSSL_NO_RSA
   1.193 +static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
   1.194 +static void free_tmp_rsa(void);
   1.195 +#endif
   1.196 +static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg);
   1.197 +#define APP_CALLBACK_STRING "Test Callback Argument"
   1.198 +struct app_verify_arg
   1.199 +	{
   1.200 +	char *string;
   1.201 +	int app_verify;
   1.202 +	int allow_proxy_certs;
   1.203 +	char *proxy_auth;
   1.204 +	char *proxy_cond;
   1.205 +	};
   1.206 +
   1.207 +#ifndef OPENSSL_NO_DH
   1.208 +static DH *get_dh512(void);
   1.209 +static DH *get_dh1024(void);
   1.210 +static DH *get_dh1024dsa(void);
   1.211 +#endif
   1.212 +
   1.213 +static BIO *bio_err=NULL;
   1.214 +static BIO *bio_stdout=NULL;
   1.215 +
   1.216 +static char *cipher=NULL;
   1.217 +static int verbose=0;
   1.218 +static int debug=0;
   1.219 +#if 0
   1.220 +/* Not used yet. */
   1.221 +#ifdef FIONBIO
   1.222 +static int s_nbio=0;
   1.223 +#endif
   1.224 +#endif
   1.225 +
   1.226 +static const char rnd_seed[] = "string to make the random number generator think it has entropy";
   1.227 +
   1.228 +int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
   1.229 +int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
   1.230 +static int do_test_cipherlist(void);
   1.231 +static void sv_usage(void)
   1.232 +	{
   1.233 +	fprintf(stderr,"usage: ssltest [args ...]\n");
   1.234 +	fprintf(stderr,"\n");
   1.235 +	fprintf(stderr," -server_auth  - check server certificate\n");
   1.236 +	fprintf(stderr," -client_auth  - do client authentication\n");
   1.237 +	fprintf(stderr," -proxy        - allow proxy certificates\n");
   1.238 +	fprintf(stderr," -proxy_auth <val> - set proxy policy rights\n");
   1.239 +	fprintf(stderr," -proxy_cond <val> - experssion to test proxy policy rights\n");
   1.240 +	fprintf(stderr," -v            - more output\n");
   1.241 +	fprintf(stderr," -d            - debug output\n");
   1.242 +	fprintf(stderr," -reuse        - use session-id reuse\n");
   1.243 +	fprintf(stderr," -num <val>    - number of connections to perform\n");
   1.244 +	fprintf(stderr," -bytes <val>  - number of bytes to swap between client/server\n");
   1.245 +#ifndef OPENSSL_NO_DH
   1.246 +	fprintf(stderr," -dhe1024      - use 1024 bit key (safe prime) for DHE\n");
   1.247 +	fprintf(stderr," -dhe1024dsa   - use 1024 bit key (with 160-bit subprime) for DHE\n");
   1.248 +	fprintf(stderr," -no_dhe       - disable DHE\n");
   1.249 +#endif
   1.250 +#ifndef OPENSSL_NO_ECDH
   1.251 +	fprintf(stderr," -no_ecdhe     - disable ECDHE\n");
   1.252 +#endif
   1.253 +#ifndef OPENSSL_NO_SSL2
   1.254 +	fprintf(stderr," -ssl2         - use SSLv2\n");
   1.255 +#endif
   1.256 +#ifndef OPENSSL_NO_SSL3
   1.257 +	fprintf(stderr," -ssl3         - use SSLv3\n");
   1.258 +#endif
   1.259 +#ifndef OPENSSL_NO_TLS1
   1.260 +	fprintf(stderr," -tls1         - use TLSv1\n");
   1.261 +#endif
   1.262 +	fprintf(stderr," -CApath arg   - PEM format directory of CA's\n");
   1.263 +	fprintf(stderr," -CAfile arg   - PEM format file of CA's\n");
   1.264 +	fprintf(stderr," -cert arg     - Server certificate file\n");
   1.265 +	fprintf(stderr," -key arg      - Server key file (default: same as -cert)\n");
   1.266 +	fprintf(stderr," -c_cert arg   - Client certificate file\n");
   1.267 +	fprintf(stderr," -c_key arg    - Client key file (default: same as -c_cert)\n");
   1.268 +	fprintf(stderr," -cipher arg   - The cipher list\n");
   1.269 +	fprintf(stderr," -bio_pair     - Use BIO pairs\n");
   1.270 +	fprintf(stderr," -f            - Test even cases that can't work\n");
   1.271 +	fprintf(stderr," -time         - measure processor time used by client and server\n");
   1.272 +	fprintf(stderr," -zlib         - use zlib compression\n");
   1.273 +	fprintf(stderr," -rle          - use rle compression\n");
   1.274 +#ifndef OPENSSL_NO_ECDH
   1.275 +	fprintf(stderr," -named_curve arg  - Elliptic curve name to use for ephemeral ECDH keys.\n" \
   1.276 +	               "                 Use \"openssl ecparam -list_curves\" for all names\n"  \
   1.277 +	               "                 (default is sect163r2).\n");
   1.278 +#endif
   1.279 +	fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n");
   1.280 +	}
   1.281 +
   1.282 +static void print_details(SSL *c_ssl, const char *prefix)
   1.283 +	{
   1.284 +	SSL_CIPHER *ciph;
   1.285 +	X509 *cert;
   1.286 +		
   1.287 +	ciph=SSL_get_current_cipher(c_ssl);
   1.288 +	BIO_printf(bio_stdout,"%s%s, cipher %s %s",
   1.289 +		prefix,
   1.290 +		SSL_get_version(c_ssl),
   1.291 +		SSL_CIPHER_get_version(ciph),
   1.292 +		SSL_CIPHER_get_name(ciph));
   1.293 +	cert=SSL_get_peer_certificate(c_ssl);
   1.294 +	if (cert != NULL)
   1.295 +		{
   1.296 +		EVP_PKEY *pkey = X509_get_pubkey(cert);
   1.297 +		if (pkey != NULL)
   1.298 +			{
   1.299 +			if (0) 
   1.300 +				;
   1.301 +#ifndef OPENSSL_NO_RSA
   1.302 +			else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
   1.303 +				&& pkey->pkey.rsa->n != NULL)
   1.304 +				{
   1.305 +				BIO_printf(bio_stdout, ", %d bit RSA",
   1.306 +					BN_num_bits(pkey->pkey.rsa->n));
   1.307 +				}
   1.308 +#endif
   1.309 +#ifndef OPENSSL_NO_DSA
   1.310 +			else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
   1.311 +				&& pkey->pkey.dsa->p != NULL)
   1.312 +				{
   1.313 +				BIO_printf(bio_stdout, ", %d bit DSA",
   1.314 +					BN_num_bits(pkey->pkey.dsa->p));
   1.315 +				}
   1.316 +#endif
   1.317 +			EVP_PKEY_free(pkey);
   1.318 +			}
   1.319 +		X509_free(cert);
   1.320 +		}
   1.321 +	/* The SSL API does not allow us to look at temporary RSA/DH keys,
   1.322 +	 * otherwise we should print their lengths too */
   1.323 +	BIO_printf(bio_stdout,"\n");
   1.324 +	}
   1.325 +
   1.326 +static void lock_dbg_cb(int mode, int type, const char *file, int line)
   1.327 +	{
   1.328 +	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
   1.329 +	const char *errstr = NULL;
   1.330 +	int rw;
   1.331 +	
   1.332 +	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
   1.333 +	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
   1.334 +		{
   1.335 +		errstr = "invalid mode";
   1.336 +		goto err;
   1.337 +		}
   1.338 +
   1.339 +	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
   1.340 +		{
   1.341 +		errstr = "type out of bounds";
   1.342 +		goto err;
   1.343 +		}
   1.344 +
   1.345 +	if (mode & CRYPTO_LOCK)
   1.346 +		{
   1.347 +		if (modes[type])
   1.348 +			{
   1.349 +			errstr = "already locked";
   1.350 +			/* must not happen in a single-threaded program
   1.351 +			 * (would deadlock) */
   1.352 +			goto err;
   1.353 +			}
   1.354 +
   1.355 +		modes[type] = rw;
   1.356 +		}
   1.357 +	else if (mode & CRYPTO_UNLOCK)
   1.358 +		{
   1.359 +		if (!modes[type])
   1.360 +			{
   1.361 +			errstr = "not locked";
   1.362 +			goto err;
   1.363 +			}
   1.364 +		
   1.365 +		if (modes[type] != rw)
   1.366 +			{
   1.367 +			errstr = (rw == CRYPTO_READ) ?
   1.368 +				"CRYPTO_r_unlock on write lock" :
   1.369 +				"CRYPTO_w_unlock on read lock";
   1.370 +			}
   1.371 +
   1.372 +		modes[type] = 0;
   1.373 +		}
   1.374 +	else
   1.375 +		{
   1.376 +		errstr = "invalid mode";
   1.377 +		goto err;
   1.378 +		}
   1.379 +
   1.380 + err:
   1.381 +	if (errstr)
   1.382 +		{
   1.383 +		/* we cannot use bio_err here */
   1.384 +		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
   1.385 +			errstr, mode, type, file, line);
   1.386 +		}
   1.387 +	}
   1.388 +
   1.389 +
   1.390 +int main(int argc, char *argv[])
   1.391 +	{
   1.392 +	char *CApath=NULL,*CAfile=NULL;
   1.393 +	int badop=0;
   1.394 +	int bio_pair=0;
   1.395 +	int force=0;
   1.396 +	int tls1=0,ssl2=0,ssl3=0,ret=1;
   1.397 +	int client_auth=0;
   1.398 +	int server_auth=0,i;
   1.399 +	struct app_verify_arg app_verify_arg =
   1.400 +		{ APP_CALLBACK_STRING, 0, 0, NULL, NULL };
   1.401 +	char *server_cert=TEST_SERVER_CERT;
   1.402 +	char *server_key=NULL;
   1.403 +	char *client_cert=TEST_CLIENT_CERT;
   1.404 +	char *client_key=NULL;
   1.405 +#ifndef OPENSSL_NO_ECDH
   1.406 +	char *named_curve = NULL;
   1.407 +#endif
   1.408 +	SSL_CTX *s_ctx=NULL;
   1.409 +	SSL_CTX *c_ctx=NULL;
   1.410 +	SSL_METHOD *meth=NULL;
   1.411 +	SSL *c_ssl,*s_ssl;
   1.412 +	int number=1,reuse=0;
   1.413 +	long bytes=256L;
   1.414 +#ifndef OPENSSL_NO_DH
   1.415 +	DH *dh;
   1.416 +	int dhe1024 = 0, dhe1024dsa = 0;
   1.417 +#endif
   1.418 +#ifndef OPENSSL_NO_ECDH
   1.419 +	EC_KEY *ecdh = NULL;
   1.420 +#endif
   1.421 +	int no_dhe = 0;
   1.422 +	int no_ecdhe = 0;
   1.423 +	int print_time = 0;
   1.424 +	clock_t s_time = 0, c_time = 0;
   1.425 +	int comp = 0;
   1.426 +#ifndef OPENSSL_NO_COMP
   1.427 +	COMP_METHOD *cm = NULL;
   1.428 +#endif
   1.429 +	STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
   1.430 +	int test_cipherlist = 0;
   1.431 +
   1.432 +	verbose = 0;
   1.433 +	debug = 0;
   1.434 +	cipher = 0;
   1.435 +
   1.436 +	bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);	
   1.437 +
   1.438 +	CRYPTO_set_locking_callback(lock_dbg_cb);
   1.439 +
   1.440 +	/* enable memory leak checking unless explicitly disabled */
   1.441 +	if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
   1.442 +		{
   1.443 +		CRYPTO_malloc_debug_init();
   1.444 +		CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
   1.445 +		}
   1.446 +	else
   1.447 +		{
   1.448 +		/* OPENSSL_DEBUG_MEMORY=off */
   1.449 +		CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
   1.450 +		}
   1.451 +	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
   1.452 +
   1.453 +	RAND_seed(rnd_seed, sizeof rnd_seed);
   1.454 +
   1.455 +	bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
   1.456 +
   1.457 +	argc--;
   1.458 +	argv++;
   1.459 +
   1.460 +	while (argc >= 1)
   1.461 +		{
   1.462 +		if	(strcmp(*argv,"-server_auth") == 0)
   1.463 +			server_auth=1;
   1.464 +		else if	(strcmp(*argv,"-client_auth") == 0)
   1.465 +			client_auth=1;
   1.466 +		else if (strcmp(*argv,"-proxy_auth") == 0)
   1.467 +			{
   1.468 +			if (--argc < 1) goto bad;
   1.469 +			app_verify_arg.proxy_auth= *(++argv);
   1.470 +			}
   1.471 +		else if (strcmp(*argv,"-proxy_cond") == 0)
   1.472 +			{
   1.473 +			if (--argc < 1) goto bad;
   1.474 +			app_verify_arg.proxy_cond= *(++argv);
   1.475 +			}
   1.476 +		else if	(strcmp(*argv,"-v") == 0)
   1.477 +			verbose=1;
   1.478 +		else if	(strcmp(*argv,"-d") == 0)
   1.479 +			debug=1;
   1.480 +		else if	(strcmp(*argv,"-reuse") == 0)
   1.481 +			reuse=1;
   1.482 +		else if	(strcmp(*argv,"-dhe1024") == 0)
   1.483 +			{
   1.484 +#ifndef OPENSSL_NO_DH
   1.485 +			dhe1024=1;
   1.486 +#else
   1.487 +			fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
   1.488 +#endif
   1.489 +			}
   1.490 +		else if	(strcmp(*argv,"-dhe1024dsa") == 0)
   1.491 +			{
   1.492 +#ifndef OPENSSL_NO_DH
   1.493 +			dhe1024dsa=1;
   1.494 +#else
   1.495 +			fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
   1.496 +#endif
   1.497 +			}
   1.498 +		else if	(strcmp(*argv,"-no_dhe") == 0)
   1.499 +			no_dhe=1;
   1.500 +		else if	(strcmp(*argv,"-no_ecdhe") == 0)
   1.501 +			no_ecdhe=1;
   1.502 +		else if	(strcmp(*argv,"-ssl2") == 0)
   1.503 +			ssl2=1;
   1.504 +		else if	(strcmp(*argv,"-tls1") == 0)
   1.505 +			tls1=1;
   1.506 +		else if	(strcmp(*argv,"-ssl3") == 0)
   1.507 +			ssl3=1;
   1.508 +		else if	(strncmp(*argv,"-num",4) == 0)
   1.509 +			{
   1.510 +			if (--argc < 1) goto bad;
   1.511 +			number= atoi(*(++argv));
   1.512 +			if (number == 0) number=1;
   1.513 +			}
   1.514 +		else if	(strcmp(*argv,"-bytes") == 0)
   1.515 +			{
   1.516 +			if (--argc < 1) goto bad;
   1.517 +			bytes= atol(*(++argv));
   1.518 +			if (bytes == 0L) bytes=1L;
   1.519 +			i=strlen(argv[0]);
   1.520 +			if (argv[0][i-1] == 'k') bytes*=1024L;
   1.521 +			if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
   1.522 +			}
   1.523 +		else if	(strcmp(*argv,"-cert") == 0)
   1.524 +			{
   1.525 +			if (--argc < 1) goto bad;
   1.526 +			server_cert= *(++argv);
   1.527 +			}
   1.528 +		else if	(strcmp(*argv,"-s_cert") == 0)
   1.529 +			{
   1.530 +			if (--argc < 1) goto bad;
   1.531 +			server_cert= *(++argv);
   1.532 +			}
   1.533 +		else if	(strcmp(*argv,"-key") == 0)
   1.534 +			{
   1.535 +			if (--argc < 1) goto bad;
   1.536 +			server_key= *(++argv);
   1.537 +			}
   1.538 +		else if	(strcmp(*argv,"-s_key") == 0)
   1.539 +			{
   1.540 +			if (--argc < 1) goto bad;
   1.541 +			server_key= *(++argv);
   1.542 +			}
   1.543 +		else if	(strcmp(*argv,"-c_cert") == 0)
   1.544 +			{
   1.545 +			if (--argc < 1) goto bad;
   1.546 +			client_cert= *(++argv);
   1.547 +			}
   1.548 +		else if	(strcmp(*argv,"-c_key") == 0)
   1.549 +			{
   1.550 +			if (--argc < 1) goto bad;
   1.551 +			client_key= *(++argv);
   1.552 +			}
   1.553 +		else if	(strcmp(*argv,"-cipher") == 0)
   1.554 +			{
   1.555 +			if (--argc < 1) goto bad;
   1.556 +			cipher= *(++argv);
   1.557 +			}
   1.558 +		else if	(strcmp(*argv,"-CApath") == 0)
   1.559 +			{
   1.560 +			if (--argc < 1) goto bad;
   1.561 +			CApath= *(++argv);
   1.562 +			}
   1.563 +		else if	(strcmp(*argv,"-CAfile") == 0)
   1.564 +			{
   1.565 +			if (--argc < 1) goto bad;
   1.566 +			CAfile= *(++argv);
   1.567 +			}
   1.568 +		else if	(strcmp(*argv,"-bio_pair") == 0)
   1.569 +			{
   1.570 +			bio_pair = 1;
   1.571 +			}
   1.572 +		else if	(strcmp(*argv,"-f") == 0)
   1.573 +			{
   1.574 +			force = 1;
   1.575 +			}
   1.576 +		else if	(strcmp(*argv,"-time") == 0)
   1.577 +			{
   1.578 +			print_time = 1;
   1.579 +			}
   1.580 +		else if	(strcmp(*argv,"-zlib") == 0)
   1.581 +			{
   1.582 +			comp = COMP_ZLIB;
   1.583 +			}
   1.584 +		else if	(strcmp(*argv,"-rle") == 0)
   1.585 +			{
   1.586 +			comp = COMP_RLE;
   1.587 +			}
   1.588 +		else if	(strcmp(*argv,"-named_curve") == 0)
   1.589 +			{
   1.590 +			if (--argc < 1) goto bad;
   1.591 +#ifndef OPENSSL_NO_ECDH		
   1.592 +			named_curve = *(++argv);
   1.593 +#else
   1.594 +			fprintf(stderr,"ignoring -named_curve, since I'm compiled without ECDH\n");
   1.595 +			++argv;
   1.596 +#endif
   1.597 +			}
   1.598 +		else if	(strcmp(*argv,"-app_verify") == 0)
   1.599 +			{
   1.600 +			app_verify_arg.app_verify = 1;
   1.601 +			}
   1.602 +		else if	(strcmp(*argv,"-proxy") == 0)
   1.603 +			{
   1.604 +			app_verify_arg.allow_proxy_certs = 1;
   1.605 +			}
   1.606 +		else if (strcmp(*argv,"-test_cipherlist") == 0)
   1.607 +			{
   1.608 +			test_cipherlist = 1;
   1.609 +			}
   1.610 +		else
   1.611 +			{
   1.612 +			fprintf(stderr,"unknown option %s\n",*argv);
   1.613 +			badop=1;
   1.614 +			break;
   1.615 +			}
   1.616 +		argc--;
   1.617 +		argv++;
   1.618 +		}
   1.619 +	if (badop)
   1.620 +		{
   1.621 +bad:
   1.622 +		sv_usage();
   1.623 +		goto end;
   1.624 +		}
   1.625 +
   1.626 +	if (test_cipherlist == 1)
   1.627 +		{
   1.628 +		/* ensure that the cipher list are correctly sorted and exit */
   1.629 +		if (do_test_cipherlist() == 0)
   1.630 +			EXIT(1);
   1.631 +		ret = 0;
   1.632 +		goto end;
   1.633 +		}
   1.634 +
   1.635 +	if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
   1.636 +		{
   1.637 +		fprintf(stderr, "This case cannot work.  Use -f to perform "
   1.638 +			"the test anyway (and\n-d to see what happens), "
   1.639 +			"or add one of -ssl2, -ssl3, -tls1, -reuse\n"
   1.640 +			"to avoid protocol mismatch.\n");
   1.641 +		EXIT(1);
   1.642 +		}
   1.643 +
   1.644 +	if (print_time)
   1.645 +		{
   1.646 +		if (!bio_pair)
   1.647 +			{
   1.648 +			fprintf(stderr, "Using BIO pair (-bio_pair)\n");
   1.649 +			bio_pair = 1;
   1.650 +			}
   1.651 +		if (number < 50 && !force)
   1.652 +			fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
   1.653 +		}
   1.654 +
   1.655 +/*	if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
   1.656 +
   1.657 +	SSL_library_init();
   1.658 +	SSL_load_error_strings();
   1.659 +
   1.660 +#ifndef OPENSSL_NO_COMP
   1.661 +	if (comp == COMP_ZLIB) cm = COMP_zlib();
   1.662 +	if (comp == COMP_RLE) cm = COMP_rle();
   1.663 +	if (cm != NULL)
   1.664 +		{
   1.665 +		if (cm->type != NID_undef)
   1.666 +			{
   1.667 +			if (SSL_COMP_add_compression_method(comp, cm) != 0)
   1.668 +				{
   1.669 +				fprintf(stderr,
   1.670 +					"Failed to add compression method\n");
   1.671 +				ERR_print_errors_fp(stderr);
   1.672 +				}
   1.673 +			}
   1.674 +		else
   1.675 +			{
   1.676 +			fprintf(stderr,
   1.677 +				"Warning: %s compression not supported\n",
   1.678 +				(comp == COMP_RLE ? "rle" :
   1.679 +					(comp == COMP_ZLIB ? "zlib" :
   1.680 +						"unknown")));
   1.681 +			ERR_print_errors_fp(stderr);
   1.682 +			}
   1.683 +		}
   1.684 +	ssl_comp_methods = SSL_COMP_get_compression_methods();
   1.685 +	fprintf(stderr, "Available compression methods:\n");
   1.686 +	{
   1.687 +	int j, n = sk_SSL_COMP_num(ssl_comp_methods);
   1.688 +	if (n == 0)
   1.689 +		fprintf(stderr, "  NONE\n");
   1.690 +	else
   1.691 +		for (j = 0; j < n; j++)
   1.692 +			{
   1.693 +			SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
   1.694 +			fprintf(stderr, "  %d: %s\n", c->id, c->name);
   1.695 +			}
   1.696 +	}
   1.697 +#endif
   1.698 +
   1.699 +#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
   1.700 +	if (ssl2)
   1.701 +		meth=SSLv2_method();
   1.702 +	else 
   1.703 +	if (tls1)
   1.704 +		meth=TLSv1_method();
   1.705 +	else
   1.706 +	if (ssl3)
   1.707 +		meth=SSLv3_method();
   1.708 +	else
   1.709 +		meth=SSLv23_method();
   1.710 +#else
   1.711 +#ifdef OPENSSL_NO_SSL2
   1.712 +	meth=SSLv3_method();
   1.713 +#else
   1.714 +	meth=SSLv2_method();
   1.715 +#endif
   1.716 +#endif
   1.717 +
   1.718 +	c_ctx=SSL_CTX_new(meth);
   1.719 +	s_ctx=SSL_CTX_new(meth);
   1.720 +	if ((c_ctx == NULL) || (s_ctx == NULL))
   1.721 +		{
   1.722 +		ERR_print_errors(bio_err);
   1.723 +		goto end;
   1.724 +		}
   1.725 +
   1.726 +	if (cipher != NULL)
   1.727 +		{
   1.728 +		SSL_CTX_set_cipher_list(c_ctx,cipher);
   1.729 +		SSL_CTX_set_cipher_list(s_ctx,cipher);
   1.730 +		}
   1.731 +
   1.732 +#ifndef OPENSSL_NO_DH
   1.733 +	if (!no_dhe)
   1.734 +		{
   1.735 +		if (dhe1024dsa)
   1.736 +			{
   1.737 +			/* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
   1.738 +			SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
   1.739 +			dh=get_dh1024dsa();
   1.740 +			}
   1.741 +		else if (dhe1024)
   1.742 +			dh=get_dh1024();
   1.743 +		else
   1.744 +			dh=get_dh512();
   1.745 +		SSL_CTX_set_tmp_dh(s_ctx,dh);
   1.746 +		DH_free(dh);
   1.747 +		}
   1.748 +#else
   1.749 +	(void)no_dhe;
   1.750 +#endif
   1.751 +
   1.752 +#ifndef OPENSSL_NO_ECDH
   1.753 +	if (!no_ecdhe)
   1.754 +		{
   1.755 +		int nid;
   1.756 +
   1.757 +		if (named_curve != NULL)
   1.758 +			{
   1.759 +			nid = OBJ_sn2nid(named_curve);
   1.760 +			if (nid == 0)
   1.761 +			{
   1.762 +				BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);
   1.763 +				goto end;
   1.764 +				}
   1.765 +			}
   1.766 +		else
   1.767 +			nid = NID_sect163r2;
   1.768 +
   1.769 +		ecdh = EC_KEY_new_by_curve_name(nid);
   1.770 +		if (ecdh == NULL)
   1.771 +			{
   1.772 +			BIO_printf(bio_err, "unable to create curve\n");
   1.773 +			goto end;
   1.774 +			}
   1.775 +
   1.776 +		SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);
   1.777 +		SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);
   1.778 +		EC_KEY_free(ecdh);
   1.779 +		}
   1.780 +#else
   1.781 +	(void)no_ecdhe;
   1.782 +#endif
   1.783 +
   1.784 +#ifndef OPENSSL_NO_RSA
   1.785 +	SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
   1.786 +#endif
   1.787 +
   1.788 +	if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
   1.789 +		{
   1.790 +		ERR_print_errors(bio_err);
   1.791 +		}
   1.792 +	else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
   1.793 +		(server_key?server_key:server_cert), SSL_FILETYPE_PEM))
   1.794 +		{
   1.795 +		ERR_print_errors(bio_err);
   1.796 +		goto end;
   1.797 +		}
   1.798 +
   1.799 +	if (client_auth)
   1.800 +		{
   1.801 +		SSL_CTX_use_certificate_file(c_ctx,client_cert,
   1.802 +			SSL_FILETYPE_PEM);
   1.803 +		SSL_CTX_use_PrivateKey_file(c_ctx,
   1.804 +			(client_key?client_key:client_cert),
   1.805 +			SSL_FILETYPE_PEM);
   1.806 +		}
   1.807 +
   1.808 +	if (	(!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
   1.809 +		(!SSL_CTX_set_default_verify_paths(s_ctx)) ||
   1.810 +		(!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
   1.811 +		(!SSL_CTX_set_default_verify_paths(c_ctx)))
   1.812 +		{
   1.813 +		/* fprintf(stderr,"SSL_load_verify_locations\n"); */
   1.814 +		ERR_print_errors(bio_err);
   1.815 +		/* goto end; */
   1.816 +		}
   1.817 +
   1.818 +	if (client_auth)
   1.819 +		{
   1.820 +		BIO_printf(bio_err,"client authentication\n");
   1.821 +		SSL_CTX_set_verify(s_ctx,
   1.822 +			SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
   1.823 +			verify_callback);
   1.824 +		SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, &app_verify_arg);
   1.825 +		}
   1.826 +	if (server_auth)
   1.827 +		{
   1.828 +		BIO_printf(bio_err,"server authentication\n");
   1.829 +		SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
   1.830 +			verify_callback);
   1.831 +		SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback, &app_verify_arg);
   1.832 +		}
   1.833 +	
   1.834 +	{
   1.835 +		int session_id_context = 0;
   1.836 +		SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);
   1.837 +	}
   1.838 +
   1.839 +	c_ssl=SSL_new(c_ctx);
   1.840 +	s_ssl=SSL_new(s_ctx);
   1.841 +
   1.842 +#ifndef OPENSSL_NO_KRB5
   1.843 +	if (c_ssl  &&  c_ssl->kssl_ctx)
   1.844 +                {
   1.845 +                char	localhost[MAXHOSTNAMELEN+2];
   1.846 +
   1.847 +		if (gethostname(localhost, sizeof localhost-1) == 0)
   1.848 +                        {
   1.849 +			localhost[sizeof localhost-1]='\0';
   1.850 +			if(strlen(localhost) == sizeof localhost-1)
   1.851 +				{
   1.852 +				BIO_printf(bio_err,"localhost name too long\n");
   1.853 +				goto end;
   1.854 +				}
   1.855 +			kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,
   1.856 +                                localhost);
   1.857 +			}
   1.858 +		}
   1.859 +#endif    /* OPENSSL_NO_KRB5  */
   1.860 +
   1.861 +	for (i=0; i<number; i++)
   1.862 +		{
   1.863 +		if (!reuse) SSL_set_session(c_ssl,NULL);
   1.864 +		if (bio_pair)
   1.865 +			ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);
   1.866 +		else
   1.867 +			ret=doit(s_ssl,c_ssl,bytes);
   1.868 +		}
   1.869 +
   1.870 +	if (!verbose)
   1.871 +		{
   1.872 +		print_details(c_ssl, "");
   1.873 +		}
   1.874 +	if ((number > 1) || (bytes > 1L))
   1.875 +		BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);
   1.876 +	if (print_time)
   1.877 +		{
   1.878 +#ifdef CLOCKS_PER_SEC
   1.879 +		/* "To determine the time in seconds, the value returned
   1.880 +		 * by the clock function should be divided by the value
   1.881 +		 * of the macro CLOCKS_PER_SEC."
   1.882 +		 *                                       -- ISO/IEC 9899 */
   1.883 +		BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
   1.884 +			"Approximate total client time: %6.2f s\n",
   1.885 +			(double)s_time/CLOCKS_PER_SEC,
   1.886 +			(double)c_time/CLOCKS_PER_SEC);
   1.887 +#else
   1.888 +		/* "`CLOCKS_PER_SEC' undeclared (first use this function)"
   1.889 +		 *                            -- cc on NeXTstep/OpenStep */
   1.890 +		BIO_printf(bio_stdout,
   1.891 +			"Approximate total server time: %6.2f units\n"
   1.892 +			"Approximate total client time: %6.2f units\n",
   1.893 +			(double)s_time,
   1.894 +			(double)c_time);
   1.895 +#endif
   1.896 +		}
   1.897 +
   1.898 +	SSL_free(s_ssl);
   1.899 +	SSL_free(c_ssl);
   1.900 +
   1.901 +end:
   1.902 +	if (s_ctx != NULL) SSL_CTX_free(s_ctx);
   1.903 +	if (c_ctx != NULL) SSL_CTX_free(c_ctx);
   1.904 +
   1.905 +	if (bio_stdout != NULL) BIO_free(bio_stdout);
   1.906 +
   1.907 +#ifndef OPENSSL_NO_RSA
   1.908 +	free_tmp_rsa();
   1.909 +#endif
   1.910 +#ifndef OPENSSL_NO_ENGINE
   1.911 +	ENGINE_cleanup();
   1.912 +#endif
   1.913 +	CRYPTO_cleanup_all_ex_data();
   1.914 +	ERR_free_strings();
   1.915 +	ERR_remove_state(0);
   1.916 +	EVP_cleanup();
   1.917 +	CRYPTO_mem_leaks(bio_err);
   1.918 +	if (bio_err != NULL) BIO_free(bio_err);
   1.919 +	EXIT(ret);
   1.920 +	return ret;
   1.921 +	}
   1.922 +
   1.923 +int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
   1.924 +	clock_t *s_time, clock_t *c_time)
   1.925 +	{
   1.926 +	long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
   1.927 +	BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
   1.928 +	BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
   1.929 +	int ret = 1;
   1.930 +	
   1.931 +	size_t bufsiz = 256; /* small buffer for testing */
   1.932 +
   1.933 +	if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
   1.934 +		goto err;
   1.935 +	if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
   1.936 +		goto err;
   1.937 +	
   1.938 +	s_ssl_bio = BIO_new(BIO_f_ssl());
   1.939 +	if (!s_ssl_bio)
   1.940 +		goto err;
   1.941 +
   1.942 +	c_ssl_bio = BIO_new(BIO_f_ssl());
   1.943 +	if (!c_ssl_bio)
   1.944 +		goto err;
   1.945 +
   1.946 +	SSL_set_connect_state(c_ssl);
   1.947 +	SSL_set_bio(c_ssl, client, client);
   1.948 +	(void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
   1.949 +
   1.950 +	SSL_set_accept_state(s_ssl);
   1.951 +	SSL_set_bio(s_ssl, server, server);
   1.952 +	(void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
   1.953 +
   1.954 +	do
   1.955 +		{
   1.956 +		/* c_ssl_bio:          SSL filter BIO
   1.957 +		 *
   1.958 +		 * client:             pseudo-I/O for SSL library
   1.959 +		 *
   1.960 +		 * client_io:          client's SSL communication; usually to be
   1.961 +		 *                     relayed over some I/O facility, but in this
   1.962 +		 *                     test program, we're the server, too:
   1.963 +		 *
   1.964 +		 * server_io:          server's SSL communication
   1.965 +		 *
   1.966 +		 * server:             pseudo-I/O for SSL library
   1.967 +		 *
   1.968 +		 * s_ssl_bio:          SSL filter BIO
   1.969 +		 *
   1.970 +		 * The client and the server each employ a "BIO pair":
   1.971 +		 * client + client_io, server + server_io.
   1.972 +		 * BIO pairs are symmetric.  A BIO pair behaves similar
   1.973 +		 * to a non-blocking socketpair (but both endpoints must
   1.974 +		 * be handled by the same thread).
   1.975 +		 * [Here we could connect client and server to the ends
   1.976 +		 * of a single BIO pair, but then this code would be less
   1.977 +		 * suitable as an example for BIO pairs in general.]
   1.978 +		 *
   1.979 +		 * Useful functions for querying the state of BIO pair endpoints:
   1.980 +		 *
   1.981 +		 * BIO_ctrl_pending(bio)              number of bytes we can read now
   1.982 +		 * BIO_ctrl_get_read_request(bio)     number of bytes needed to fulfil
   1.983 +		 *                                      other side's read attempt
   1.984 +		 * BIO_ctrl_get_write_guarantee(bio)   number of bytes we can write now
   1.985 +		 *
   1.986 +		 * ..._read_request is never more than ..._write_guarantee;
   1.987 +		 * it depends on the application which one you should use.
   1.988 +		 */
   1.989 +
   1.990 +		/* We have non-blocking behaviour throughout this test program, but
   1.991 +		 * can be sure that there is *some* progress in each iteration; so
   1.992 +		 * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
   1.993 +		 * -- we just try everything in each iteration
   1.994 +		 */
   1.995 +
   1.996 +			{
   1.997 +			/* CLIENT */
   1.998 +		
   1.999 +			MS_STATIC char cbuf[1024*8];
  1.1000 +			int i, r;
  1.1001 +			clock_t c_clock = clock();
  1.1002 +
  1.1003 +			memset(cbuf, 0, sizeof(cbuf));
  1.1004 +
  1.1005 +			if (debug)
  1.1006 +				if (SSL_in_init(c_ssl))
  1.1007 +					printf("client waiting in SSL_connect - %s\n",
  1.1008 +						SSL_state_string_long(c_ssl));
  1.1009 +
  1.1010 +			if (cw_num > 0)
  1.1011 +				{
  1.1012 +				/* Write to server. */
  1.1013 +				
  1.1014 +				if (cw_num > (long)sizeof cbuf)
  1.1015 +					i = sizeof cbuf;
  1.1016 +				else
  1.1017 +					i = (int)cw_num;
  1.1018 +				r = BIO_write(c_ssl_bio, cbuf, i);
  1.1019 +				if (r < 0)
  1.1020 +					{
  1.1021 +					if (!BIO_should_retry(c_ssl_bio))
  1.1022 +						{
  1.1023 +						fprintf(stderr,"ERROR in CLIENT\n");
  1.1024 +						goto err;
  1.1025 +						}
  1.1026 +					/* BIO_should_retry(...) can just be ignored here.
  1.1027 +					 * The library expects us to call BIO_write with
  1.1028 +					 * the same arguments again, and that's what we will
  1.1029 +					 * do in the next iteration. */
  1.1030 +					}
  1.1031 +				else if (r == 0)
  1.1032 +					{
  1.1033 +					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1.1034 +					goto err;
  1.1035 +					}
  1.1036 +				else
  1.1037 +					{
  1.1038 +					if (debug)
  1.1039 +						printf("client wrote %d\n", r);
  1.1040 +					cw_num -= r;				
  1.1041 +					}
  1.1042 +				}
  1.1043 +
  1.1044 +			if (cr_num > 0)
  1.1045 +				{
  1.1046 +				/* Read from server. */
  1.1047 +
  1.1048 +				r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  1.1049 +				if (r < 0)
  1.1050 +					{
  1.1051 +					if (!BIO_should_retry(c_ssl_bio))
  1.1052 +						{
  1.1053 +						fprintf(stderr,"ERROR in CLIENT\n");
  1.1054 +						goto err;
  1.1055 +						}
  1.1056 +					/* Again, "BIO_should_retry" can be ignored. */
  1.1057 +					}
  1.1058 +				else if (r == 0)
  1.1059 +					{
  1.1060 +					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1.1061 +					goto err;
  1.1062 +					}
  1.1063 +				else
  1.1064 +					{
  1.1065 +					if (debug)
  1.1066 +						printf("client read %d\n", r);
  1.1067 +					cr_num -= r;
  1.1068 +					}
  1.1069 +				}
  1.1070 +
  1.1071 +			/* c_time and s_time increments will typically be very small
  1.1072 +			 * (depending on machine speed and clock tick intervals),
  1.1073 +			 * but sampling over a large number of connections should
  1.1074 +			 * result in fairly accurate figures.  We cannot guarantee
  1.1075 +			 * a lot, however -- if each connection lasts for exactly
  1.1076 +			 * one clock tick, it will be counted only for the client
  1.1077 +			 * or only for the server or even not at all.
  1.1078 +			 */
  1.1079 +			*c_time += (clock() - c_clock);
  1.1080 +			}
  1.1081 +
  1.1082 +			{
  1.1083 +			/* SERVER */
  1.1084 +		
  1.1085 +			MS_STATIC char sbuf[1024*8];
  1.1086 +			int i, r;
  1.1087 +			clock_t s_clock = clock();
  1.1088 +
  1.1089 +			memset(sbuf, 0, sizeof(sbuf));
  1.1090 +
  1.1091 +			if (debug)
  1.1092 +				if (SSL_in_init(s_ssl))
  1.1093 +					printf("server waiting in SSL_accept - %s\n",
  1.1094 +						SSL_state_string_long(s_ssl));
  1.1095 +
  1.1096 +			if (sw_num > 0)
  1.1097 +				{
  1.1098 +				/* Write to client. */
  1.1099 +				
  1.1100 +				if (sw_num > (long)sizeof sbuf)
  1.1101 +					i = sizeof sbuf;
  1.1102 +				else
  1.1103 +					i = (int)sw_num;
  1.1104 +				r = BIO_write(s_ssl_bio, sbuf, i);
  1.1105 +				if (r < 0)
  1.1106 +					{
  1.1107 +					if (!BIO_should_retry(s_ssl_bio))
  1.1108 +						{
  1.1109 +						fprintf(stderr,"ERROR in SERVER\n");
  1.1110 +						goto err;
  1.1111 +						}
  1.1112 +					/* Ignore "BIO_should_retry". */
  1.1113 +					}
  1.1114 +				else if (r == 0)
  1.1115 +					{
  1.1116 +					fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  1.1117 +					goto err;
  1.1118 +					}
  1.1119 +				else
  1.1120 +					{
  1.1121 +					if (debug)
  1.1122 +						printf("server wrote %d\n", r);
  1.1123 +					sw_num -= r;				
  1.1124 +					}
  1.1125 +				}
  1.1126 +
  1.1127 +			if (sr_num > 0)
  1.1128 +				{
  1.1129 +				/* Read from client. */
  1.1130 +
  1.1131 +				r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  1.1132 +				if (r < 0)
  1.1133 +					{
  1.1134 +					if (!BIO_should_retry(s_ssl_bio))
  1.1135 +						{
  1.1136 +						fprintf(stderr,"ERROR in SERVER\n");
  1.1137 +						goto err;
  1.1138 +						}
  1.1139 +					/* blah, blah */
  1.1140 +					}
  1.1141 +				else if (r == 0)
  1.1142 +					{
  1.1143 +					fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  1.1144 +					goto err;
  1.1145 +					}
  1.1146 +				else
  1.1147 +					{
  1.1148 +					if (debug)
  1.1149 +						printf("server read %d\n", r);
  1.1150 +					sr_num -= r;
  1.1151 +					}
  1.1152 +				}
  1.1153 +
  1.1154 +			*s_time += (clock() - s_clock);
  1.1155 +			}
  1.1156 +			
  1.1157 +			{
  1.1158 +			/* "I/O" BETWEEN CLIENT AND SERVER. */
  1.1159 +
  1.1160 +			size_t r1, r2;
  1.1161 +			BIO *io1 = server_io, *io2 = client_io;
  1.1162 +			/* we use the non-copying interface for io1
  1.1163 +			 * and the standard BIO_write/BIO_read interface for io2
  1.1164 +			 */
  1.1165 +			
  1.1166 +			static int prev_progress = 1;
  1.1167 +			int progress = 0;
  1.1168 +			
  1.1169 +			/* io1 to io2 */
  1.1170 +			do
  1.1171 +				{
  1.1172 +				size_t num;
  1.1173 +				int r;
  1.1174 +
  1.1175 +				r1 = BIO_ctrl_pending(io1);
  1.1176 +				r2 = BIO_ctrl_get_write_guarantee(io2);
  1.1177 +
  1.1178 +				num = r1;
  1.1179 +				if (r2 < num)
  1.1180 +					num = r2;
  1.1181 +				if (num)
  1.1182 +					{
  1.1183 +					char *dataptr;
  1.1184 +
  1.1185 +					if (INT_MAX < num) /* yeah, right */
  1.1186 +						num = INT_MAX;
  1.1187 +					
  1.1188 +					r = BIO_nread(io1, &dataptr, (int)num);
  1.1189 +					assert(r > 0);
  1.1190 +					assert(r <= (int)num);
  1.1191 +					/* possibly r < num (non-contiguous data) */
  1.1192 +					num = r;
  1.1193 +					r = BIO_write(io2, dataptr, (int)num);
  1.1194 +					if (r != (int)num) /* can't happen */
  1.1195 +						{
  1.1196 +						fprintf(stderr, "ERROR: BIO_write could not write "
  1.1197 +							"BIO_ctrl_get_write_guarantee() bytes");
  1.1198 +						goto err;
  1.1199 +						}
  1.1200 +					progress = 1;
  1.1201 +
  1.1202 +					if (debug)
  1.1203 +						printf((io1 == client_io) ?
  1.1204 +							"C->S relaying: %d bytes\n" :
  1.1205 +							"S->C relaying: %d bytes\n",
  1.1206 +							(int)num);
  1.1207 +					}
  1.1208 +				}
  1.1209 +			while (r1 && r2);
  1.1210 +
  1.1211 +			/* io2 to io1 */
  1.1212 +			{
  1.1213 +				size_t num;
  1.1214 +				int r;
  1.1215 +
  1.1216 +				r1 = BIO_ctrl_pending(io2);
  1.1217 +				r2 = BIO_ctrl_get_read_request(io1);
  1.1218 +				/* here we could use ..._get_write_guarantee instead of
  1.1219 +				 * ..._get_read_request, but by using the latter
  1.1220 +				 * we test restartability of the SSL implementation
  1.1221 +				 * more thoroughly */
  1.1222 +				num = r1;
  1.1223 +				if (r2 < num)
  1.1224 +					num = r2;
  1.1225 +				if (num)
  1.1226 +					{
  1.1227 +					char *dataptr;
  1.1228 +					
  1.1229 +					if (INT_MAX < num)
  1.1230 +						num = INT_MAX;
  1.1231 +
  1.1232 +					if (num > 1)
  1.1233 +						--num; /* test restartability even more thoroughly */
  1.1234 +					
  1.1235 +					r = BIO_nwrite0(io1, &dataptr);
  1.1236 +					assert(r > 0);
  1.1237 +					if (r < (int)num)
  1.1238 +						num = r;
  1.1239 +					r = BIO_read(io2, dataptr, (int)num);
  1.1240 +					if (r != (int)num) /* can't happen */
  1.1241 +						{
  1.1242 +						fprintf(stderr, "ERROR: BIO_read could not read "
  1.1243 +							"BIO_ctrl_pending() bytes");
  1.1244 +						goto err;
  1.1245 +						}
  1.1246 +					progress = 1;
  1.1247 +					r = BIO_nwrite(io1, &dataptr, (int)num);
  1.1248 +					if (r != (int)num) /* can't happen */
  1.1249 +						{
  1.1250 +						fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
  1.1251 +							"BIO_nwrite0() bytes");
  1.1252 +						goto err;
  1.1253 +						}
  1.1254 +					
  1.1255 +					if (debug)
  1.1256 +						printf((io2 == client_io) ?
  1.1257 +							"C->S relaying: %d bytes\n" :
  1.1258 +							"S->C relaying: %d bytes\n",
  1.1259 +							(int)num);
  1.1260 +					}
  1.1261 +			} /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
  1.1262 +
  1.1263 +			if (!progress && !prev_progress)
  1.1264 +				if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
  1.1265 +					{
  1.1266 +					fprintf(stderr, "ERROR: got stuck\n");
  1.1267 +					if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
  1.1268 +						{
  1.1269 +						fprintf(stderr, "This can happen for SSL2 because "
  1.1270 +							"CLIENT-FINISHED and SERVER-VERIFY are written \n"
  1.1271 +							"concurrently ...");
  1.1272 +						if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
  1.1273 +							&& strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
  1.1274 +							{
  1.1275 +							fprintf(stderr, " ok.\n");
  1.1276 +							goto end;
  1.1277 +							}
  1.1278 +						}
  1.1279 +					fprintf(stderr, " ERROR.\n");
  1.1280 +					goto err;
  1.1281 +					}
  1.1282 +			prev_progress = progress;
  1.1283 +			}
  1.1284 +		}
  1.1285 +	while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  1.1286 +
  1.1287 +	if (verbose)
  1.1288 +		print_details(c_ssl, "DONE via BIO pair: ");
  1.1289 +end:
  1.1290 +	ret = 0;
  1.1291 +
  1.1292 + err:
  1.1293 +	ERR_print_errors(bio_err);
  1.1294 +	
  1.1295 +	if (server)
  1.1296 +		BIO_free(server);
  1.1297 +	if (server_io)
  1.1298 +		BIO_free(server_io);
  1.1299 +	if (client)
  1.1300 +		BIO_free(client);
  1.1301 +	if (client_io)
  1.1302 +		BIO_free(client_io);
  1.1303 +	if (s_ssl_bio)
  1.1304 +		BIO_free(s_ssl_bio);
  1.1305 +	if (c_ssl_bio)
  1.1306 +		BIO_free(c_ssl_bio);
  1.1307 +
  1.1308 +	return ret;
  1.1309 +	}
  1.1310 +
  1.1311 +
  1.1312 +#define W_READ	1
  1.1313 +#define W_WRITE	2
  1.1314 +#define C_DONE	1
  1.1315 +#define S_DONE	2
  1.1316 +
  1.1317 +int doit(SSL *s_ssl, SSL *c_ssl, long count)
  1.1318 +	{
  1.1319 +	MS_STATIC char cbuf[1024*8],sbuf[1024*8];
  1.1320 +	long cw_num=count,cr_num=count;
  1.1321 +	long sw_num=count,sr_num=count;
  1.1322 +	int ret=1;
  1.1323 +	BIO *c_to_s=NULL;
  1.1324 +	BIO *s_to_c=NULL;
  1.1325 +	BIO *c_bio=NULL;
  1.1326 +	BIO *s_bio=NULL;
  1.1327 +	int c_r,c_w,s_r,s_w;
  1.1328 +	int c_want,s_want;
  1.1329 +	int i,j;
  1.1330 +	int done=0;
  1.1331 +	int c_write,s_write;
  1.1332 +	int do_server=0,do_client=0;
  1.1333 +
  1.1334 +	memset(cbuf,0,sizeof(cbuf));
  1.1335 +	memset(sbuf,0,sizeof(sbuf));
  1.1336 +
  1.1337 +	c_to_s=BIO_new(BIO_s_mem());
  1.1338 +	s_to_c=BIO_new(BIO_s_mem());
  1.1339 +	if ((s_to_c == NULL) || (c_to_s == NULL))
  1.1340 +		{
  1.1341 +		ERR_print_errors(bio_err);
  1.1342 +		goto err;
  1.1343 +		}
  1.1344 +
  1.1345 +	c_bio=BIO_new(BIO_f_ssl());
  1.1346 +	s_bio=BIO_new(BIO_f_ssl());
  1.1347 +	if ((c_bio == NULL) || (s_bio == NULL))
  1.1348 +		{
  1.1349 +		ERR_print_errors(bio_err);
  1.1350 +		goto err;
  1.1351 +		}
  1.1352 +
  1.1353 +	SSL_set_connect_state(c_ssl);
  1.1354 +	SSL_set_bio(c_ssl,s_to_c,c_to_s);
  1.1355 +	BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
  1.1356 +
  1.1357 +	SSL_set_accept_state(s_ssl);
  1.1358 +	SSL_set_bio(s_ssl,c_to_s,s_to_c);
  1.1359 +	BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
  1.1360 +
  1.1361 +	c_r=0; s_r=1;
  1.1362 +	c_w=1; s_w=0;
  1.1363 +	c_want=W_WRITE;
  1.1364 +	s_want=0;
  1.1365 +	c_write=1,s_write=0;
  1.1366 +
  1.1367 +	/* We can always do writes */
  1.1368 +	for (;;)
  1.1369 +		{
  1.1370 +		do_server=0;
  1.1371 +		do_client=0;
  1.1372 +
  1.1373 +		i=(int)BIO_pending(s_bio);
  1.1374 +		if ((i && s_r) || s_w) do_server=1;
  1.1375 +
  1.1376 +		i=(int)BIO_pending(c_bio);
  1.1377 +		if ((i && c_r) || c_w) do_client=1;
  1.1378 +
  1.1379 +		if (do_server && debug)
  1.1380 +			{
  1.1381 +			if (SSL_in_init(s_ssl))
  1.1382 +				printf("server waiting in SSL_accept - %s\n",
  1.1383 +					SSL_state_string_long(s_ssl));
  1.1384 +/*			else if (s_write)
  1.1385 +				printf("server:SSL_write()\n");
  1.1386 +			else
  1.1387 +				printf("server:SSL_read()\n"); */
  1.1388 +			}
  1.1389 +
  1.1390 +		if (do_client && debug)
  1.1391 +			{
  1.1392 +			if (SSL_in_init(c_ssl))
  1.1393 +				printf("client waiting in SSL_connect - %s\n",
  1.1394 +					SSL_state_string_long(c_ssl));
  1.1395 +/*			else if (c_write)
  1.1396 +				printf("client:SSL_write()\n");
  1.1397 +			else
  1.1398 +				printf("client:SSL_read()\n"); */
  1.1399 +			}
  1.1400 +
  1.1401 +		if (!do_client && !do_server)
  1.1402 +			{
  1.1403 +			fprintf(stdout,"ERROR IN STARTUP\n");
  1.1404 +			ERR_print_errors(bio_err);
  1.1405 +			break;
  1.1406 +			}
  1.1407 +		if (do_client && !(done & C_DONE))
  1.1408 +			{
  1.1409 +			if (c_write)
  1.1410 +				{
  1.1411 +				j = (cw_num > (long)sizeof(cbuf)) ?
  1.1412 +					(int)sizeof(cbuf) : (int)cw_num;
  1.1413 +				i=BIO_write(c_bio,cbuf,j);
  1.1414 +				if (i < 0)
  1.1415 +					{
  1.1416 +					c_r=0;
  1.1417 +					c_w=0;
  1.1418 +					if (BIO_should_retry(c_bio))
  1.1419 +						{
  1.1420 +						if (BIO_should_read(c_bio))
  1.1421 +							c_r=1;
  1.1422 +						if (BIO_should_write(c_bio))
  1.1423 +							c_w=1;
  1.1424 +						}
  1.1425 +					else
  1.1426 +						{
  1.1427 +						fprintf(stderr,"ERROR in CLIENT\n");
  1.1428 +						ERR_print_errors(bio_err);
  1.1429 +						goto err;
  1.1430 +						}
  1.1431 +					}
  1.1432 +				else if (i == 0)
  1.1433 +					{
  1.1434 +					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1.1435 +					goto err;
  1.1436 +					}
  1.1437 +				else
  1.1438 +					{
  1.1439 +					if (debug)
  1.1440 +						printf("client wrote %d\n",i);
  1.1441 +					/* ok */
  1.1442 +					s_r=1;
  1.1443 +					c_write=0;
  1.1444 +					cw_num-=i;
  1.1445 +					}
  1.1446 +				}
  1.1447 +			else
  1.1448 +				{
  1.1449 +				i=BIO_read(c_bio,cbuf,sizeof(cbuf));
  1.1450 +				if (i < 0)
  1.1451 +					{
  1.1452 +					c_r=0;
  1.1453 +					c_w=0;
  1.1454 +					if (BIO_should_retry(c_bio))
  1.1455 +						{
  1.1456 +						if (BIO_should_read(c_bio))
  1.1457 +							c_r=1;
  1.1458 +						if (BIO_should_write(c_bio))
  1.1459 +							c_w=1;
  1.1460 +						}
  1.1461 +					else
  1.1462 +						{
  1.1463 +						fprintf(stderr,"ERROR in CLIENT\n");
  1.1464 +						ERR_print_errors(bio_err);
  1.1465 +						goto err;
  1.1466 +						}
  1.1467 +					}
  1.1468 +				else if (i == 0)
  1.1469 +					{
  1.1470 +					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1.1471 +					goto err;
  1.1472 +					}
  1.1473 +				else
  1.1474 +					{
  1.1475 +					if (debug)
  1.1476 +						printf("client read %d\n",i);
  1.1477 +					cr_num-=i;
  1.1478 +					if (sw_num > 0)
  1.1479 +						{
  1.1480 +						s_write=1;
  1.1481 +						s_w=1;
  1.1482 +						}
  1.1483 +					if (cr_num <= 0)
  1.1484 +						{
  1.1485 +						s_write=1;
  1.1486 +						s_w=1;
  1.1487 +						done=S_DONE|C_DONE;
  1.1488 +						}
  1.1489 +					}
  1.1490 +				}
  1.1491 +			}
  1.1492 +
  1.1493 +		if (do_server && !(done & S_DONE))
  1.1494 +			{
  1.1495 +			if (!s_write)
  1.1496 +				{
  1.1497 +				i=BIO_read(s_bio,sbuf,sizeof(cbuf));
  1.1498 +				if (i < 0)
  1.1499 +					{
  1.1500 +					s_r=0;
  1.1501 +					s_w=0;
  1.1502 +					if (BIO_should_retry(s_bio))
  1.1503 +						{
  1.1504 +						if (BIO_should_read(s_bio))
  1.1505 +							s_r=1;
  1.1506 +						if (BIO_should_write(s_bio))
  1.1507 +							s_w=1;
  1.1508 +						}
  1.1509 +					else
  1.1510 +						{
  1.1511 +						fprintf(stderr,"ERROR in SERVER\n");
  1.1512 +						ERR_print_errors(bio_err);
  1.1513 +						goto err;
  1.1514 +						}
  1.1515 +					}
  1.1516 +				else if (i == 0)
  1.1517 +					{
  1.1518 +					ERR_print_errors(bio_err);
  1.1519 +					fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
  1.1520 +					goto err;
  1.1521 +					}
  1.1522 +				else
  1.1523 +					{
  1.1524 +					if (debug)
  1.1525 +						printf("server read %d\n",i);
  1.1526 +					sr_num-=i;
  1.1527 +					if (cw_num > 0)
  1.1528 +						{
  1.1529 +						c_write=1;
  1.1530 +						c_w=1;
  1.1531 +						}
  1.1532 +					if (sr_num <= 0)
  1.1533 +						{
  1.1534 +						s_write=1;
  1.1535 +						s_w=1;
  1.1536 +						c_write=0;
  1.1537 +						}
  1.1538 +					}
  1.1539 +				}
  1.1540 +			else
  1.1541 +				{
  1.1542 +				j = (sw_num > (long)sizeof(sbuf)) ?
  1.1543 +					(int)sizeof(sbuf) : (int)sw_num;
  1.1544 +				i=BIO_write(s_bio,sbuf,j);
  1.1545 +				if (i < 0)
  1.1546 +					{
  1.1547 +					s_r=0;
  1.1548 +					s_w=0;
  1.1549 +					if (BIO_should_retry(s_bio))
  1.1550 +						{
  1.1551 +						if (BIO_should_read(s_bio))
  1.1552 +							s_r=1;
  1.1553 +						if (BIO_should_write(s_bio))
  1.1554 +							s_w=1;
  1.1555 +						}
  1.1556 +					else
  1.1557 +						{
  1.1558 +						fprintf(stderr,"ERROR in SERVER\n");
  1.1559 +						ERR_print_errors(bio_err);
  1.1560 +						goto err;
  1.1561 +						}
  1.1562 +					}
  1.1563 +				else if (i == 0)
  1.1564 +					{
  1.1565 +					ERR_print_errors(bio_err);
  1.1566 +					fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
  1.1567 +					goto err;
  1.1568 +					}
  1.1569 +				else
  1.1570 +					{
  1.1571 +					if (debug)
  1.1572 +						printf("server wrote %d\n",i);
  1.1573 +					sw_num-=i;
  1.1574 +					s_write=0;
  1.1575 +					c_r=1;
  1.1576 +					if (sw_num <= 0)
  1.1577 +						done|=S_DONE;
  1.1578 +					}
  1.1579 +				}
  1.1580 +			}
  1.1581 +
  1.1582 +		if ((done & S_DONE) && (done & C_DONE)) break;
  1.1583 +		}
  1.1584 +
  1.1585 +	if (verbose)
  1.1586 +		print_details(c_ssl, "DONE: ");
  1.1587 +	ret=0;
  1.1588 +err:
  1.1589 +	/* We have to set the BIO's to NULL otherwise they will be
  1.1590 +	 * OPENSSL_free()ed twice.  Once when th s_ssl is SSL_free()ed and
  1.1591 +	 * again when c_ssl is SSL_free()ed.
  1.1592 +	 * This is a hack required because s_ssl and c_ssl are sharing the same
  1.1593 +	 * BIO structure and SSL_set_bio() and SSL_free() automatically
  1.1594 +	 * BIO_free non NULL entries.
  1.1595 +	 * You should not normally do this or be required to do this */
  1.1596 +	if (s_ssl != NULL)
  1.1597 +		{
  1.1598 +		s_ssl->rbio=NULL;
  1.1599 +		s_ssl->wbio=NULL;
  1.1600 +		}
  1.1601 +	if (c_ssl != NULL)
  1.1602 +		{
  1.1603 +		c_ssl->rbio=NULL;
  1.1604 +		c_ssl->wbio=NULL;
  1.1605 +		}
  1.1606 +
  1.1607 +	if (c_to_s != NULL) BIO_free(c_to_s);
  1.1608 +	if (s_to_c != NULL) BIO_free(s_to_c);
  1.1609 +	if (c_bio != NULL) BIO_free_all(c_bio);
  1.1610 +	if (s_bio != NULL) BIO_free_all(s_bio);
  1.1611 +	return(ret);
  1.1612 +	}
  1.1613 +
  1.1614 +static int get_proxy_auth_ex_data_idx(void)
  1.1615 +	{
  1.1616 +	static volatile int idx = -1;
  1.1617 +	if (idx < 0)
  1.1618 +		{
  1.1619 +		CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  1.1620 +		if (idx < 0)
  1.1621 +			{
  1.1622 +			idx = X509_STORE_CTX_get_ex_new_index(0,
  1.1623 +				"SSLtest for verify callback", NULL,NULL,NULL);
  1.1624 +			}
  1.1625 +		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  1.1626 +		}
  1.1627 +	return idx;
  1.1628 +	}
  1.1629 +
  1.1630 +static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
  1.1631 +	{
  1.1632 +	char *s,buf[256];
  1.1633 +
  1.1634 +	s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,
  1.1635 +			    sizeof buf);
  1.1636 +	if (s != NULL)
  1.1637 +		{
  1.1638 +		if (ok)
  1.1639 +			fprintf(stderr,"depth=%d %s\n",
  1.1640 +				ctx->error_depth,buf);
  1.1641 +		else
  1.1642 +			{
  1.1643 +			fprintf(stderr,"depth=%d error=%d %s\n",
  1.1644 +				ctx->error_depth,ctx->error,buf);
  1.1645 +			}
  1.1646 +		}
  1.1647 +
  1.1648 +	if (ok == 0)
  1.1649 +		{
  1.1650 +		fprintf(stderr,"Error string: %s\n",
  1.1651 +			X509_verify_cert_error_string(ctx->error));
  1.1652 +		switch (ctx->error)
  1.1653 +			{
  1.1654 +		case X509_V_ERR_CERT_NOT_YET_VALID:
  1.1655 +		case X509_V_ERR_CERT_HAS_EXPIRED:
  1.1656 +		case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  1.1657 +			fprintf(stderr,"  ... ignored.\n");
  1.1658 +			ok=1;
  1.1659 +			}
  1.1660 +		}
  1.1661 +
  1.1662 +	if (ok == 1)
  1.1663 +		{
  1.1664 +		X509 *xs = ctx->current_cert;
  1.1665 +#if 0
  1.1666 +		X509 *xi = ctx->current_issuer;
  1.1667 +#endif
  1.1668 +
  1.1669 +		if (xs->ex_flags & EXFLAG_PROXY)
  1.1670 +			{
  1.1671 +			unsigned int *letters =
  1.1672 +				X509_STORE_CTX_get_ex_data(ctx,
  1.1673 +					get_proxy_auth_ex_data_idx());
  1.1674 +
  1.1675 +			if (letters)
  1.1676 +				{
  1.1677 +				int found_any = 0;
  1.1678 +				int i;
  1.1679 +				PROXY_CERT_INFO_EXTENSION *pci =
  1.1680 +					X509_get_ext_d2i(xs, NID_proxyCertInfo,
  1.1681 +						NULL, NULL);
  1.1682 +
  1.1683 +				switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage))
  1.1684 +					{
  1.1685 +				case NID_Independent:
  1.1686 +					/* Completely meaningless in this
  1.1687 +					   program, as there's no way to
  1.1688 +					   grant explicit rights to a
  1.1689 +					   specific PrC.  Basically, using
  1.1690 +					   id-ppl-Independent is the perfect
  1.1691 +					   way to grant no rights at all. */
  1.1692 +					fprintf(stderr, "  Independent proxy certificate");
  1.1693 +					for (i = 0; i < 26; i++)
  1.1694 +						letters[i] = 0;
  1.1695 +					break;
  1.1696 +				case NID_id_ppl_inheritAll:
  1.1697 +					/* This is basically a NOP, we
  1.1698 +					   simply let the current rights
  1.1699 +					   stand as they are. */
  1.1700 +					fprintf(stderr, "  Proxy certificate inherits all");
  1.1701 +					break;
  1.1702 +				default:
  1.1703 +					s = (char *)
  1.1704 +						pci->proxyPolicy->policy->data;
  1.1705 +					i = pci->proxyPolicy->policy->length;
  1.1706 +
  1.1707 +					/* The algorithm works as follows:
  1.1708 +					   it is assumed that previous
  1.1709 +					   iterations or the initial granted
  1.1710 +					   rights has already set some elements
  1.1711 +					   of `letters'.  What we need to do is
  1.1712 +					   to clear those that weren't granted
  1.1713 +					   by the current PrC as well.  The
  1.1714 +					   easiest way to do this is to add 1
  1.1715 +					   to all the elements whose letters
  1.1716 +					   are given with the current policy.
  1.1717 +					   That way, all elements that are set
  1.1718 +					   by the current policy and were
  1.1719 +					   already set by earlier policies and
  1.1720 +					   through the original grant of rights
  1.1721 +					   will get the value 2 or higher.
  1.1722 +					   The last thing to do is to sweep
  1.1723 +					   through `letters' and keep the
  1.1724 +					   elements having the value 2 as set,
  1.1725 +					   and clear all the others. */
  1.1726 +
  1.1727 +					fprintf(stderr, "  Certificate proxy rights = %*.*s", i, i, s);
  1.1728 +					while(i-- > 0)
  1.1729 +						{
  1.1730 +						int c = *s++;
  1.1731 +						if (isascii(c) && isalpha(c))
  1.1732 +							{
  1.1733 +							if (islower(c))
  1.1734 +								c = toupper(c);
  1.1735 +							letters[c - 'A']++;
  1.1736 +							}
  1.1737 +						}
  1.1738 +					for (i = 0; i < 26; i++)
  1.1739 +						if (letters[i] < 2)
  1.1740 +							letters[i] = 0;
  1.1741 +						else
  1.1742 +							letters[i] = 1;
  1.1743 +					}
  1.1744 +
  1.1745 +				found_any = 0;
  1.1746 +				fprintf(stderr,
  1.1747 +					", resulting proxy rights = ");
  1.1748 +				for(i = 0; i < 26; i++)
  1.1749 +					if (letters[i])
  1.1750 +						{
  1.1751 +						fprintf(stderr, "%c", i + 'A');
  1.1752 +						found_any = 1;
  1.1753 +						}
  1.1754 +				if (!found_any)
  1.1755 +					fprintf(stderr, "none");
  1.1756 +				fprintf(stderr, "\n");
  1.1757 +
  1.1758 +				PROXY_CERT_INFO_EXTENSION_free(pci);
  1.1759 +				}
  1.1760 +			}
  1.1761 +		}
  1.1762 +
  1.1763 +	return(ok);
  1.1764 +	}
  1.1765 +
  1.1766 +static void process_proxy_debug(int indent, const char *format, ...)
  1.1767 +	{
  1.1768 +	static const char indentation[] =
  1.1769 +		">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  1.1770 +		">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; /* That's 80 > */
  1.1771 +	char my_format[256];
  1.1772 +	va_list args;
  1.1773 +
  1.1774 +	BIO_snprintf(my_format, sizeof(my_format), "%*.*s %s",
  1.1775 +		indent, indent, indentation, format);
  1.1776 +
  1.1777 +	va_start(args, format);
  1.1778 +	vfprintf(stderr, my_format, args);
  1.1779 +	va_end(args);
  1.1780 +	}
  1.1781 +/* Priority levels:
  1.1782 +   0	[!]var, ()
  1.1783 +   1	& ^
  1.1784 +   2	|
  1.1785 +*/
  1.1786 +static int process_proxy_cond_adders(unsigned int letters[26],
  1.1787 +	const char *cond, const char **cond_end, int *pos, int indent);
  1.1788 +static int process_proxy_cond_val(unsigned int letters[26],
  1.1789 +	const char *cond, const char **cond_end, int *pos, int indent)
  1.1790 +	{
  1.1791 +	int c;
  1.1792 +	int ok = 1;
  1.1793 +	int negate = 0;
  1.1794 +
  1.1795 +	while(isspace((int)*cond))
  1.1796 +		{
  1.1797 +		cond++; (*pos)++;
  1.1798 +		}
  1.1799 +	c = *cond;
  1.1800 +
  1.1801 +	if (debug)
  1.1802 +		process_proxy_debug(indent,
  1.1803 +			"Start process_proxy_cond_val at position %d: %s\n",
  1.1804 +			*pos, cond);
  1.1805 +
  1.1806 +	while(c == '!')
  1.1807 +		{
  1.1808 +		negate = !negate;
  1.1809 +		cond++; (*pos)++;
  1.1810 +		while(isspace((int)*cond))
  1.1811 +			{
  1.1812 +			cond++; (*pos)++;
  1.1813 +			}
  1.1814 +		c = *cond;
  1.1815 +		}
  1.1816 +
  1.1817 +	if (c == '(')
  1.1818 +		{
  1.1819 +		cond++; (*pos)++;
  1.1820 +		ok = process_proxy_cond_adders(letters, cond, cond_end, pos,
  1.1821 +			indent + 1);
  1.1822 +		cond = *cond_end;
  1.1823 +		if (ok < 0)
  1.1824 +			goto end;
  1.1825 +		while(isspace((int)*cond))
  1.1826 +			{
  1.1827 +			cond++; (*pos)++;
  1.1828 +			}
  1.1829 +		c = *cond;
  1.1830 +		if (c != ')')
  1.1831 +			{
  1.1832 +			fprintf(stderr,
  1.1833 +				"Weird condition character in position %d: "
  1.1834 +				"%c\n", *pos, c);
  1.1835 +			ok = -1;
  1.1836 +			goto end;
  1.1837 +			}
  1.1838 +		cond++; (*pos)++;
  1.1839 +		}
  1.1840 +	else if (isascii(c) && isalpha(c))
  1.1841 +		{
  1.1842 +		if (islower(c))
  1.1843 +			c = toupper(c);
  1.1844 +		ok = letters[c - 'A'];
  1.1845 +		cond++; (*pos)++;
  1.1846 +		}
  1.1847 +	else
  1.1848 +		{
  1.1849 +		fprintf(stderr,
  1.1850 +			"Weird condition character in position %d: "
  1.1851 +			"%c\n", *pos, c);
  1.1852 +		ok = -1;
  1.1853 +		goto end;
  1.1854 +		}
  1.1855 + end:
  1.1856 +	*cond_end = cond;
  1.1857 +	if (ok >= 0 && negate)
  1.1858 +		ok = !ok;
  1.1859 +
  1.1860 +	if (debug)
  1.1861 +		process_proxy_debug(indent,
  1.1862 +			"End process_proxy_cond_val at position %d: %s, returning %d\n",
  1.1863 +			*pos, cond, ok);
  1.1864 +
  1.1865 +	return ok;
  1.1866 +	}
  1.1867 +static int process_proxy_cond_multipliers(unsigned int letters[26],
  1.1868 +	const char *cond, const char **cond_end, int *pos, int indent)
  1.1869 +	{
  1.1870 +	int ok;
  1.1871 +	char c;
  1.1872 +
  1.1873 +	if (debug)
  1.1874 +		process_proxy_debug(indent,
  1.1875 +			"Start process_proxy_cond_multipliers at position %d: %s\n",
  1.1876 +			*pos, cond);
  1.1877 +
  1.1878 +	ok = process_proxy_cond_val(letters, cond, cond_end, pos, indent + 1);
  1.1879 +	cond = *cond_end;
  1.1880 +	if (ok < 0)
  1.1881 +		goto end;
  1.1882 +
  1.1883 +	while(ok >= 0)
  1.1884 +		{
  1.1885 +		while(isspace((int)*cond))
  1.1886 +			{
  1.1887 +			cond++; (*pos)++;
  1.1888 +			}
  1.1889 +		c = *cond;
  1.1890 +
  1.1891 +		switch(c)
  1.1892 +			{
  1.1893 +		case '&':
  1.1894 +		case '^':
  1.1895 +			{
  1.1896 +			int save_ok = ok;
  1.1897 +
  1.1898 +			cond++; (*pos)++;
  1.1899 +			ok = process_proxy_cond_val(letters,
  1.1900 +				cond, cond_end, pos, indent + 1);
  1.1901 +			cond = *cond_end;
  1.1902 +			if (ok < 0)
  1.1903 +				break;
  1.1904 +
  1.1905 +			switch(c)
  1.1906 +				{
  1.1907 +			case '&':
  1.1908 +				ok &= save_ok;
  1.1909 +				break;
  1.1910 +			case '^':
  1.1911 +				ok ^= save_ok;
  1.1912 +				break;
  1.1913 +			default:
  1.1914 +				fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  1.1915 +					" STOPPING\n");
  1.1916 +				EXIT(1);
  1.1917 +				}
  1.1918 +			}
  1.1919 +			break;
  1.1920 +		default:
  1.1921 +			goto end;
  1.1922 +			}
  1.1923 +		}
  1.1924 + end:
  1.1925 +	if (debug)
  1.1926 +		process_proxy_debug(indent,
  1.1927 +			"End process_proxy_cond_multipliers at position %d: %s, returning %d\n",
  1.1928 +			*pos, cond, ok);
  1.1929 +
  1.1930 +	*cond_end = cond;
  1.1931 +	return ok;
  1.1932 +	}
  1.1933 +static int process_proxy_cond_adders(unsigned int letters[26],
  1.1934 +	const char *cond, const char **cond_end, int *pos, int indent)
  1.1935 +	{
  1.1936 +	int ok;
  1.1937 +	char c;
  1.1938 +
  1.1939 +	if (debug)
  1.1940 +		process_proxy_debug(indent,
  1.1941 +			"Start process_proxy_cond_adders at position %d: %s\n",
  1.1942 +			*pos, cond);
  1.1943 +
  1.1944 +	ok = process_proxy_cond_multipliers(letters, cond, cond_end, pos,
  1.1945 +		indent + 1);
  1.1946 +	cond = *cond_end;
  1.1947 +	if (ok < 0)
  1.1948 +		goto end;
  1.1949 +
  1.1950 +	while(ok >= 0)
  1.1951 +		{
  1.1952 +		while(isspace((int)*cond))
  1.1953 +			{
  1.1954 +			cond++; (*pos)++;
  1.1955 +			}
  1.1956 +		c = *cond;
  1.1957 +
  1.1958 +		switch(c)
  1.1959 +			{
  1.1960 +		case '|':
  1.1961 +			{
  1.1962 +			int save_ok = ok;
  1.1963 +
  1.1964 +			cond++; (*pos)++;
  1.1965 +			ok = process_proxy_cond_multipliers(letters,
  1.1966 +				cond, cond_end, pos, indent + 1);
  1.1967 +			cond = *cond_end;
  1.1968 +			if (ok < 0)
  1.1969 +				break;
  1.1970 +
  1.1971 +			switch(c)
  1.1972 +				{
  1.1973 +			case '|':
  1.1974 +				ok |= save_ok;
  1.1975 +				break;
  1.1976 +			default:
  1.1977 +				fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  1.1978 +					" STOPPING\n");
  1.1979 +				EXIT(1);
  1.1980 +				}
  1.1981 +			}
  1.1982 +			break;
  1.1983 +		default:
  1.1984 +			goto end;
  1.1985 +			}
  1.1986 +		}
  1.1987 + end:
  1.1988 +	if (debug)
  1.1989 +		process_proxy_debug(indent,
  1.1990 +			"End process_proxy_cond_adders at position %d: %s, returning %d\n",
  1.1991 +			*pos, cond, ok);
  1.1992 +
  1.1993 +	*cond_end = cond;
  1.1994 +	return ok;
  1.1995 +	}
  1.1996 +
  1.1997 +static int process_proxy_cond(unsigned int letters[26],
  1.1998 +	const char *cond, const char **cond_end)
  1.1999 +	{
  1.2000 +	int pos = 1;
  1.2001 +	return process_proxy_cond_adders(letters, cond, cond_end, &pos, 1);
  1.2002 +	}
  1.2003 +
  1.2004 +static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg)
  1.2005 +	{
  1.2006 +	int ok=1;
  1.2007 +	struct app_verify_arg *cb_arg = arg;
  1.2008 +	unsigned int letters[26]; /* only used with proxy_auth */
  1.2009 +
  1.2010 +	if (cb_arg->app_verify)
  1.2011 +		{
  1.2012 +		char *s = NULL,buf[256];
  1.2013 +
  1.2014 +		fprintf(stderr, "In app_verify_callback, allowing cert. ");
  1.2015 +		fprintf(stderr, "Arg is: %s\n", cb_arg->string);
  1.2016 +		fprintf(stderr, "Finished printing do we have a context? 0x%p a cert? 0x%p\n",
  1.2017 +			(void *)ctx, (void *)ctx->cert);
  1.2018 +		if (ctx->cert)
  1.2019 +			s=X509_NAME_oneline(X509_get_subject_name(ctx->cert),buf,256);
  1.2020 +		if (s != NULL)
  1.2021 +			{
  1.2022 +			fprintf(stderr,"cert depth=%d %s\n",ctx->error_depth,buf);
  1.2023 +			}
  1.2024 +		return(1);
  1.2025 +		}
  1.2026 +	if (cb_arg->proxy_auth)
  1.2027 +		{
  1.2028 +		int found_any = 0, i;
  1.2029 +		char *sp;
  1.2030 +
  1.2031 +		for(i = 0; i < 26; i++)
  1.2032 +			letters[i] = 0;
  1.2033 +		for(sp = cb_arg->proxy_auth; *sp; sp++)
  1.2034 +			{
  1.2035 +			int c = *sp;
  1.2036 +			if (isascii(c) && isalpha(c))
  1.2037 +				{
  1.2038 +				if (islower(c))
  1.2039 +					c = toupper(c);
  1.2040 +				letters[c - 'A'] = 1;
  1.2041 +				}
  1.2042 +			}
  1.2043 +
  1.2044 +		fprintf(stderr,
  1.2045 +			"  Initial proxy rights = ");
  1.2046 +		for(i = 0; i < 26; i++)
  1.2047 +			if (letters[i])
  1.2048 +				{
  1.2049 +				fprintf(stderr, "%c", i + 'A');
  1.2050 +				found_any = 1;
  1.2051 +				}
  1.2052 +		if (!found_any)
  1.2053 +			fprintf(stderr, "none");
  1.2054 +		fprintf(stderr, "\n");
  1.2055 +
  1.2056 +		X509_STORE_CTX_set_ex_data(ctx,
  1.2057 +			get_proxy_auth_ex_data_idx(),letters);
  1.2058 +		}
  1.2059 +	if (cb_arg->allow_proxy_certs)
  1.2060 +		{
  1.2061 +		X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
  1.2062 +		}
  1.2063 +
  1.2064 +#ifndef OPENSSL_NO_X509_VERIFY
  1.2065 +# ifdef OPENSSL_FIPS
  1.2066 +	if(s->version == TLS1_VERSION)
  1.2067 +		FIPS_allow_md5(1);
  1.2068 +# endif
  1.2069 +	ok = X509_verify_cert(ctx);
  1.2070 +# ifdef OPENSSL_FIPS
  1.2071 +	if(s->version == TLS1_VERSION)
  1.2072 +		FIPS_allow_md5(0);
  1.2073 +# endif
  1.2074 +#endif
  1.2075 +
  1.2076 +	if (cb_arg->proxy_auth)
  1.2077 +		{
  1.2078 +		if (ok)
  1.2079 +			{
  1.2080 +			const char *cond_end = NULL;
  1.2081 +
  1.2082 +			ok = process_proxy_cond(letters,
  1.2083 +				cb_arg->proxy_cond, &cond_end);
  1.2084 +
  1.2085 +			if (ok < 0)
  1.2086 +				EXIT(3);
  1.2087 +			if (*cond_end)
  1.2088 +				{
  1.2089 +				fprintf(stderr, "Stopped processing condition before it's end.\n");
  1.2090 +				ok = 0;
  1.2091 +				}
  1.2092 +			if (!ok)
  1.2093 +				fprintf(stderr, "Proxy rights check with condition '%s' proved invalid\n",
  1.2094 +					cb_arg->proxy_cond);
  1.2095 +			else
  1.2096 +				fprintf(stderr, "Proxy rights check with condition '%s' proved valid\n",
  1.2097 +					cb_arg->proxy_cond);
  1.2098 +			}
  1.2099 +		}
  1.2100 +	return(ok);
  1.2101 +	}
  1.2102 +
  1.2103 +#ifndef OPENSSL_NO_RSA
  1.2104 +static RSA *rsa_tmp=NULL;
  1.2105 +
  1.2106 +static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  1.2107 +	{
  1.2108 +	BIGNUM *bn = NULL;
  1.2109 +	if (rsa_tmp == NULL)
  1.2110 +		{
  1.2111 +		bn = BN_new();
  1.2112 +		rsa_tmp = RSA_new();
  1.2113 +		if(!bn || !rsa_tmp || !BN_set_word(bn, RSA_F4))
  1.2114 +			{
  1.2115 +			BIO_printf(bio_err, "Memory error...");
  1.2116 +			goto end;
  1.2117 +			}
  1.2118 +		BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  1.2119 +		(void)BIO_flush(bio_err);
  1.2120 +		if(!RSA_generate_key_ex(rsa_tmp,keylength,bn,NULL))
  1.2121 +			{
  1.2122 +			BIO_printf(bio_err, "Error generating key.");
  1.2123 +			RSA_free(rsa_tmp);
  1.2124 +			rsa_tmp = NULL;
  1.2125 +			}
  1.2126 +end:
  1.2127 +		BIO_printf(bio_err,"\n");
  1.2128 +		(void)BIO_flush(bio_err);
  1.2129 +		}
  1.2130 +	if(bn) BN_free(bn);
  1.2131 +	return(rsa_tmp);
  1.2132 +	}
  1.2133 +
  1.2134 +static void free_tmp_rsa(void)
  1.2135 +	{
  1.2136 +	if (rsa_tmp != NULL)
  1.2137 +		{
  1.2138 +		RSA_free(rsa_tmp);
  1.2139 +		rsa_tmp = NULL;
  1.2140 +		}
  1.2141 +	}
  1.2142 +#endif
  1.2143 +
  1.2144 +#ifndef OPENSSL_NO_DH
  1.2145 +/* These DH parameters have been generated as follows:
  1.2146 + *    $ openssl dhparam -C -noout 512
  1.2147 + *    $ openssl dhparam -C -noout 1024
  1.2148 + *    $ openssl dhparam -C -noout -dsaparam 1024
  1.2149 + * (The third function has been renamed to avoid name conflicts.)
  1.2150 + */
  1.2151 +static DH *get_dh512()
  1.2152 +	{
  1.2153 +	static unsigned char dh512_p[]={
  1.2154 +		0xCB,0xC8,0xE1,0x86,0xD0,0x1F,0x94,0x17,0xA6,0x99,0xF0,0xC6,
  1.2155 +		0x1F,0x0D,0xAC,0xB6,0x25,0x3E,0x06,0x39,0xCA,0x72,0x04,0xB0,
  1.2156 +		0x6E,0xDA,0xC0,0x61,0xE6,0x7A,0x77,0x25,0xE8,0x3B,0xB9,0x5F,
  1.2157 +		0x9A,0xB6,0xB5,0xFE,0x99,0x0B,0xA1,0x93,0x4E,0x35,0x33,0xB8,
  1.2158 +		0xE1,0xF1,0x13,0x4F,0x59,0x1A,0xD2,0x57,0xC0,0x26,0x21,0x33,
  1.2159 +		0x02,0xC5,0xAE,0x23,
  1.2160 +		};
  1.2161 +	static unsigned char dh512_g[]={
  1.2162 +		0x02,
  1.2163 +		};
  1.2164 +	DH *dh;
  1.2165 +
  1.2166 +	if ((dh=DH_new()) == NULL) return(NULL);
  1.2167 +	dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  1.2168 +	dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  1.2169 +	if ((dh->p == NULL) || (dh->g == NULL))
  1.2170 +		{ DH_free(dh); return(NULL); }
  1.2171 +	return(dh);
  1.2172 +	}
  1.2173 +
  1.2174 +static DH *get_dh1024()
  1.2175 +	{
  1.2176 +	static unsigned char dh1024_p[]={
  1.2177 +		0xF8,0x81,0x89,0x7D,0x14,0x24,0xC5,0xD1,0xE6,0xF7,0xBF,0x3A,
  1.2178 +		0xE4,0x90,0xF4,0xFC,0x73,0xFB,0x34,0xB5,0xFA,0x4C,0x56,0xA2,
  1.2179 +		0xEA,0xA7,0xE9,0xC0,0xC0,0xCE,0x89,0xE1,0xFA,0x63,0x3F,0xB0,
  1.2180 +		0x6B,0x32,0x66,0xF1,0xD1,0x7B,0xB0,0x00,0x8F,0xCA,0x87,0xC2,
  1.2181 +		0xAE,0x98,0x89,0x26,0x17,0xC2,0x05,0xD2,0xEC,0x08,0xD0,0x8C,
  1.2182 +		0xFF,0x17,0x52,0x8C,0xC5,0x07,0x93,0x03,0xB1,0xF6,0x2F,0xB8,
  1.2183 +		0x1C,0x52,0x47,0x27,0x1B,0xDB,0xD1,0x8D,0x9D,0x69,0x1D,0x52,
  1.2184 +		0x4B,0x32,0x81,0xAA,0x7F,0x00,0xC8,0xDC,0xE6,0xD9,0xCC,0xC1,
  1.2185 +		0x11,0x2D,0x37,0x34,0x6C,0xEA,0x02,0x97,0x4B,0x0E,0xBB,0xB1,
  1.2186 +		0x71,0x33,0x09,0x15,0xFD,0xDD,0x23,0x87,0x07,0x5E,0x89,0xAB,
  1.2187 +		0x6B,0x7C,0x5F,0xEC,0xA6,0x24,0xDC,0x53,
  1.2188 +		};
  1.2189 +	static unsigned char dh1024_g[]={
  1.2190 +		0x02,
  1.2191 +		};
  1.2192 +	DH *dh;
  1.2193 +
  1.2194 +	if ((dh=DH_new()) == NULL) return(NULL);
  1.2195 +	dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  1.2196 +	dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  1.2197 +	if ((dh->p == NULL) || (dh->g == NULL))
  1.2198 +		{ DH_free(dh); return(NULL); }
  1.2199 +	return(dh);
  1.2200 +	}
  1.2201 +
  1.2202 +static DH *get_dh1024dsa()
  1.2203 +	{
  1.2204 +	static unsigned char dh1024_p[]={
  1.2205 +		0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,
  1.2206 +		0x21,0x1B,0xF7,0x31,0xA6,0xA2,0xDA,0x23,0x9A,0xC7,0x87,0x19,
  1.2207 +		0x3B,0x47,0xB6,0x8C,0x04,0x6F,0xFF,0xC6,0x9B,0xB8,0x65,0xD2,
  1.2208 +		0xC2,0x5F,0x31,0x83,0x4A,0xA7,0x5F,0x2F,0x88,0x38,0xB6,0x55,
  1.2209 +		0xCF,0xD9,0x87,0x6D,0x6F,0x9F,0xDA,0xAC,0xA6,0x48,0xAF,0xFC,
  1.2210 +		0x33,0x84,0x37,0x5B,0x82,0x4A,0x31,0x5D,0xE7,0xBD,0x52,0x97,
  1.2211 +		0xA1,0x77,0xBF,0x10,0x9E,0x37,0xEA,0x64,0xFA,0xCA,0x28,0x8D,
  1.2212 +		0x9D,0x3B,0xD2,0x6E,0x09,0x5C,0x68,0xC7,0x45,0x90,0xFD,0xBB,
  1.2213 +		0x70,0xC9,0x3A,0xBB,0xDF,0xD4,0x21,0x0F,0xC4,0x6A,0x3C,0xF6,
  1.2214 +		0x61,0xCF,0x3F,0xD6,0x13,0xF1,0x5F,0xBC,0xCF,0xBC,0x26,0x9E,
  1.2215 +		0xBC,0x0B,0xBD,0xAB,0x5D,0xC9,0x54,0x39,
  1.2216 +		};
  1.2217 +	static unsigned char dh1024_g[]={
  1.2218 +		0x3B,0x40,0x86,0xE7,0xF3,0x6C,0xDE,0x67,0x1C,0xCC,0x80,0x05,
  1.2219 +		0x5A,0xDF,0xFE,0xBD,0x20,0x27,0x74,0x6C,0x24,0xC9,0x03,0xF3,
  1.2220 +		0xE1,0x8D,0xC3,0x7D,0x98,0x27,0x40,0x08,0xB8,0x8C,0x6A,0xE9,
  1.2221 +		0xBB,0x1A,0x3A,0xD6,0x86,0x83,0x5E,0x72,0x41,0xCE,0x85,0x3C,
  1.2222 +		0xD2,0xB3,0xFC,0x13,0xCE,0x37,0x81,0x9E,0x4C,0x1C,0x7B,0x65,
  1.2223 +		0xD3,0xE6,0xA6,0x00,0xF5,0x5A,0x95,0x43,0x5E,0x81,0xCF,0x60,
  1.2224 +		0xA2,0x23,0xFC,0x36,0xA7,0x5D,0x7A,0x4C,0x06,0x91,0x6E,0xF6,
  1.2225 +		0x57,0xEE,0x36,0xCB,0x06,0xEA,0xF5,0x3D,0x95,0x49,0xCB,0xA7,
  1.2226 +		0xDD,0x81,0xDF,0x80,0x09,0x4A,0x97,0x4D,0xA8,0x22,0x72,0xA1,
  1.2227 +		0x7F,0xC4,0x70,0x56,0x70,0xE8,0x20,0x10,0x18,0x8F,0x2E,0x60,
  1.2228 +		0x07,0xE7,0x68,0x1A,0x82,0x5D,0x32,0xA2,
  1.2229 +		};
  1.2230 +	DH *dh;
  1.2231 +
  1.2232 +	if ((dh=DH_new()) == NULL) return(NULL);
  1.2233 +	dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  1.2234 +	dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  1.2235 +	if ((dh->p == NULL) || (dh->g == NULL))
  1.2236 +		{ DH_free(dh); return(NULL); }
  1.2237 +	dh->length = 160;
  1.2238 +	return(dh);
  1.2239 +	}
  1.2240 +#endif
  1.2241 +
  1.2242 +static int do_test_cipherlist(void)
  1.2243 +	{
  1.2244 +	int i = 0;
  1.2245 +	const SSL_METHOD *meth;
  1.2246 +	SSL_CIPHER *ci, *tci = NULL;
  1.2247 +
  1.2248 +#ifndef OPENSSL_NO_SSL2
  1.2249 +	fprintf(stderr, "testing SSLv2 cipher list order: ");
  1.2250 +	meth = SSLv2_method();
  1.2251 +	while ((ci = meth->get_cipher(i++)) != NULL)
  1.2252 +		{
  1.2253 +		if (tci != NULL)
  1.2254 +			if (ci->id >= tci->id)
  1.2255 +				{
  1.2256 +				fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  1.2257 +				return 0;
  1.2258 +				}
  1.2259 +		tci = ci;
  1.2260 +		}
  1.2261 +	fprintf(stderr, "ok\n");
  1.2262 +#endif
  1.2263 +#ifndef OPENSSL_NO_SSL3
  1.2264 +	fprintf(stderr, "testing SSLv3 cipher list order: ");
  1.2265 +	meth = SSLv3_method();
  1.2266 +	tci = NULL;
  1.2267 +	while ((ci = meth->get_cipher(i++)) != NULL)
  1.2268 +		{
  1.2269 +		if (tci != NULL)
  1.2270 +			if (ci->id >= tci->id)
  1.2271 +				{
  1.2272 +				fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  1.2273 +				return 0;
  1.2274 +				}
  1.2275 +		tci = ci;
  1.2276 +		}
  1.2277 +	fprintf(stderr, "ok\n");
  1.2278 +#endif
  1.2279 +#ifndef OPENSSL_NO_TLS1
  1.2280 +	fprintf(stderr, "testing TLSv1 cipher list order: ");
  1.2281 +	meth = TLSv1_method();
  1.2282 +	tci = NULL;
  1.2283 +	while ((ci = meth->get_cipher(i++)) != NULL)
  1.2284 +		{
  1.2285 +		if (tci != NULL)
  1.2286 +			if (ci->id >= tci->id)
  1.2287 +				{
  1.2288 +				fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  1.2289 +				return 0;
  1.2290 +				}
  1.2291 +		tci = ci;
  1.2292 +		}
  1.2293 +	fprintf(stderr, "ok\n");
  1.2294 +#endif
  1.2295 +
  1.2296 +	return 1;
  1.2297 +	}