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