xref: /openbsd-src/regress/lib/libssl/ssl/ssltest.c (revision 1b38a69b98b7eec3e81c7dc99baf431b9182d265)
1 /* ssl/ssltest.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 /* ====================================================================
112  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113  * ECC cipher suite support in OpenSSL originally developed by
114  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115  */
116 /* ====================================================================
117  * Copyright 2005 Nokia. All rights reserved.
118  *
119  * The portions of the attached software ("Contribution") is developed by
120  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121  * license.
122  *
123  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125  * support (see RFC 4279) to OpenSSL.
126  *
127  * No patent licenses or other rights except those expressly stated in
128  * the OpenSSL open source license shall be deemed granted or received
129  * expressly, by implication, estoppel, or otherwise.
130  *
131  * No assurances are provided by Nokia that the Contribution does not
132  * infringe the patent or other intellectual property rights of any third
133  * party or that the license provides you with all the necessary rights
134  * to make use of the Contribution.
135  *
136  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140  * OTHERWISE.
141  */
142 
143 #define _BSD_SOURCE 1		/* Or gethostname won't be declared properly
144 				   on Linux and GNU platforms. */
145 #include <sys/types.h>
146 #include <sys/param.h>
147 #include <sys/socket.h>
148 
149 #include <netinet/in.h>
150 
151 #include <assert.h>
152 #include <errno.h>
153 #include <limits.h>
154 #include <netdb.h>
155 #include <stdio.h>
156 #include <stdlib.h>
157 #include <string.h>
158 #include <time.h>
159 #include <unistd.h>
160 
161 #include <ctype.h>
162 
163 #include <openssl/opensslconf.h>
164 #include <openssl/e_os2.h>
165 #include <openssl/bio.h>
166 #include <openssl/crypto.h>
167 #include <openssl/evp.h>
168 #include <openssl/x509.h>
169 #include <openssl/x509v3.h>
170 #include <openssl/ssl.h>
171 #ifndef OPENSSL_NO_ENGINE
172 #include <openssl/engine.h>
173 #endif
174 #include <openssl/err.h>
175 #include <openssl/rand.h>
176 #include <openssl/rsa.h>
177 #include <openssl/dsa.h>
178 #include <openssl/dh.h>
179 #include <openssl/bn.h>
180 
181 #define _XOPEN_SOURCE_EXTENDED	1
182 /* Or gethostname won't be declared properly
183    on Compaq platforms (at least with DEC C).
184    Do not try to put it earlier, or IPv6 includes
185    get screwed... */
186 
187 
188 #  define TEST_SERVER_CERT "../apps/server.pem"
189 #  define TEST_CLIENT_CERT "../apps/client.pem"
190 
191 static int verify_callback(int ok, X509_STORE_CTX *ctx);
192 static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength);
193 static void free_tmp_rsa(void);
194 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
195 #define APP_CALLBACK_STRING "Test Callback Argument"
196 struct app_verify_arg {
197 	char *string;
198 	int app_verify;
199 	int allow_proxy_certs;
200 	char *proxy_auth;
201 	char *proxy_cond;
202 };
203 
204 static DH *get_dh512(void);
205 static DH *get_dh1024(void);
206 static DH *get_dh1024dsa(void);
207 
208 static BIO *bio_err = NULL;
209 static BIO *bio_stdout = NULL;
210 
211 static char *cipher = NULL;
212 static int verbose = 0;
213 static int debug = 0;
214 
215 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
216     clock_t *c_time);
217 int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
218 static int do_test_cipherlist(void);
219 
220 static void
221 sv_usage(void)
222 {
223 	fprintf(stderr, "usage: ssltest [args ...]\n");
224 	fprintf(stderr, "\n");
225 	fprintf(stderr, " -server_auth  - check server certificate\n");
226 	fprintf(stderr, " -client_auth  - do client authentication\n");
227 	fprintf(stderr, " -proxy        - allow proxy certificates\n");
228 	fprintf(stderr, " -proxy_auth <val> - set proxy policy rights\n");
229 	fprintf(stderr, " -proxy_cond <val> - experssion to test proxy policy rights\n");
230 	fprintf(stderr, " -v            - more output\n");
231 	fprintf(stderr, " -d            - debug output\n");
232 	fprintf(stderr, " -reuse        - use session-id reuse\n");
233 	fprintf(stderr, " -num <val>    - number of connections to perform\n");
234 	fprintf(stderr, " -bytes <val>  - number of bytes to swap between client/server\n");
235 	fprintf(stderr, " -dhe1024      - use 1024 bit key (safe prime) for DHE\n");
236 	fprintf(stderr, " -dhe1024dsa   - use 1024 bit key (with 160-bit subprime) for DHE\n");
237 	fprintf(stderr, " -no_dhe       - disable DHE\n");
238 	fprintf(stderr, " -no_ecdhe     - disable ECDHE\n");
239 	fprintf(stderr, " -dtls1        - use DTLSv1\n");
240 	fprintf(stderr, " -ssl3         - use SSLv3\n");
241 	fprintf(stderr, " -tls1         - use TLSv1\n");
242 	fprintf(stderr, " -CApath arg   - PEM format directory of CA's\n");
243 	fprintf(stderr, " -CAfile arg   - PEM format file of CA's\n");
244 	fprintf(stderr, " -cert arg     - Server certificate file\n");
245 	fprintf(stderr, " -key arg      - Server key file (default: same as -cert)\n");
246 	fprintf(stderr, " -c_cert arg   - Client certificate file\n");
247 	fprintf(stderr, " -c_key arg    - Client key file (default: same as -c_cert)\n");
248 	fprintf(stderr, " -cipher arg   - The cipher list\n");
249 	fprintf(stderr, " -bio_pair     - Use BIO pairs\n");
250 	fprintf(stderr, " -f            - Test even cases that can't work\n");
251 	fprintf(stderr, " -time         - measure processor time used by client and server\n");
252 	fprintf(stderr, " -named_curve arg  - Elliptic curve name to use for ephemeral ECDH keys.\n" \
253 	               "                 Use \"openssl ecparam -list_curves\" for all names\n"  \
254 	               "                 (default is sect163r2).\n");
255 	fprintf(stderr, " -test_cipherlist - verifies the order of the ssl cipher lists\n");
256 }
257 
258 static void
259 print_details(SSL *c_ssl, const char *prefix)
260 {
261 	const SSL_CIPHER *ciph;
262 	X509 *cert;
263 
264 	ciph = SSL_get_current_cipher(c_ssl);
265 	BIO_printf(bio_stdout, "%s%s, cipher %s %s",
266 	    prefix, SSL_get_version(c_ssl), SSL_CIPHER_get_version(ciph),
267 	    SSL_CIPHER_get_name(ciph));
268 	cert = SSL_get_peer_certificate(c_ssl);
269 	if (cert != NULL) {
270 		EVP_PKEY *pkey = X509_get_pubkey(cert);
271 		if (pkey != NULL) {
272 			if (pkey->type == EVP_PKEY_RSA &&
273 			    pkey->pkey.rsa != NULL &&
274 			    pkey->pkey.rsa->n != NULL) {
275 				BIO_printf(bio_stdout, ", %d bit RSA",
276 				    BN_num_bits(pkey->pkey.rsa->n));
277 			} else if (pkey->type == EVP_PKEY_DSA &&
278 			    pkey->pkey.dsa != NULL &&
279 			    pkey->pkey.dsa->p != NULL) {
280 				BIO_printf(bio_stdout, ", %d bit DSA",
281 				    BN_num_bits(pkey->pkey.dsa->p));
282 			}
283 			EVP_PKEY_free(pkey);
284 		}
285 		X509_free(cert);
286 	}
287 	/* The SSL API does not allow us to look at temporary RSA/DH keys,
288 	 * otherwise we should print their lengths too */
289 	BIO_printf(bio_stdout, "\n");
290 }
291 
292 static void
293 lock_dbg_cb(int mode, int type, const char *file, int line)
294 {
295 	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
296 	const char *errstr = NULL;
297 	int rw;
298 
299 	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
300 	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
301 		errstr = "invalid mode";
302 		goto err;
303 	}
304 
305 	if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
306 		errstr = "type out of bounds";
307 		goto err;
308 	}
309 
310 	if (mode & CRYPTO_LOCK) {
311 		if (modes[type]) {
312 			errstr = "already locked";
313 			/* must not happen in a single-threaded program
314 			 * (would deadlock) */
315 			goto err;
316 		}
317 
318 		modes[type] = rw;
319 	} else if (mode & CRYPTO_UNLOCK) {
320 		if (!modes[type]) {
321 			errstr = "not locked";
322 			goto err;
323 		}
324 
325 		if (modes[type] != rw) {
326 			errstr = (rw == CRYPTO_READ) ?
327 			    "CRYPTO_r_unlock on write lock" :
328 			    "CRYPTO_w_unlock on read lock";
329 		}
330 
331 		modes[type] = 0;
332 	} else {
333 		errstr = "invalid mode";
334 		goto err;
335 	}
336 
337 err:
338 	if (errstr) {
339 		/* we cannot use bio_err here */
340 		fprintf(stderr,
341 		    "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
342 		    errstr, mode, type, file, line);
343 	}
344 }
345 
346 int
347 main(int argc, char *argv[])
348 {
349 	char *CApath = NULL, *CAfile = NULL;
350 	int badop = 0;
351 	int bio_pair = 0;
352 	int force = 0;
353 	int tls1 = 0, ssl2 = 0, ssl3 = 0, dtls1 = 0, ret = 1;
354 	int client_auth = 0;
355 	int server_auth = 0, i;
356 	struct app_verify_arg app_verify_arg =
357 	    { APP_CALLBACK_STRING, 0, 0, NULL, NULL };
358 	char *server_cert = TEST_SERVER_CERT;
359 	char *server_key = NULL;
360 	char *client_cert = TEST_CLIENT_CERT;
361 	char *client_key = NULL;
362 	char *named_curve = NULL;
363 	SSL_CTX *s_ctx = NULL;
364 	SSL_CTX *c_ctx = NULL;
365 	const SSL_METHOD *meth = NULL;
366 	SSL *c_ssl, *s_ssl;
367 	int number = 1, reuse = 0;
368 	long bytes = 256L;
369 	DH *dh;
370 	int dhe1024 = 0, dhe1024dsa = 0;
371 	EC_KEY *ecdh = NULL;
372 	int no_dhe = 0;
373 	int no_ecdhe = 0;
374 	int print_time = 0;
375 	clock_t s_time = 0, c_time = 0;
376 	int test_cipherlist = 0;
377 
378 	verbose = 0;
379 	debug = 0;
380 	cipher = 0;
381 
382 	bio_err = BIO_new_fp(stderr, BIO_NOCLOSE|BIO_FP_TEXT);
383 
384 	CRYPTO_set_locking_callback(lock_dbg_cb);
385 
386 	bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE|BIO_FP_TEXT);
387 
388 	argc--;
389 	argv++;
390 
391 	while (argc >= 1) {
392 		if (!strcmp(*argv, "-F")) {
393 			fprintf(stderr, "not compiled with FIPS support, so exitting without running.\n");
394 			exit(0);
395 		} else if (strcmp(*argv, "-server_auth") == 0)
396 			server_auth = 1;
397 		else if (strcmp(*argv, "-client_auth") == 0)
398 			client_auth = 1;
399 		else if (strcmp(*argv, "-proxy_auth") == 0) {
400 			if (--argc < 1)
401 				goto bad;
402 			app_verify_arg.proxy_auth= *(++argv);
403 		} else if (strcmp(*argv, "-proxy_cond") == 0) {
404 			if (--argc < 1)
405 				goto bad;
406 			app_verify_arg.proxy_cond= *(++argv);
407 		} else if (strcmp(*argv, "-v") == 0)
408 			verbose = 1;
409 		else if (strcmp(*argv, "-d") == 0)
410 			debug = 1;
411 		else if (strcmp(*argv, "-reuse") == 0)
412 			reuse = 1;
413 		else if (strcmp(*argv, "-dhe1024") == 0) {
414 			dhe1024 = 1;
415 		} else if (strcmp(*argv, "-dhe1024dsa") == 0) {
416 			dhe1024dsa = 1;
417 		} else if (strcmp(*argv, "-no_dhe") == 0)
418 			no_dhe = 1;
419 		else if (strcmp(*argv, "-no_ecdhe") == 0)
420 			no_ecdhe = 1;
421 		else if (strcmp(*argv, "-dtls1") == 0)
422 			dtls1 = 1;
423 		else if (strcmp(*argv, "-ssl2") == 0)
424 			ssl2 = 1;
425 		else if (strcmp(*argv, "-ssl3") == 0)
426 			ssl3 = 1;
427 		else if (strcmp(*argv, "-tls1") == 0)
428 			tls1 = 1;
429 		else if (strncmp(*argv, "-num", 4) == 0) {
430 			if (--argc < 1)
431 				goto bad;
432 			number = atoi(*(++argv));
433 			if (number == 0)
434 				number = 1;
435 		} else if (strcmp(*argv, "-bytes") == 0) {
436 			if (--argc < 1)
437 				goto bad;
438 			bytes = atol(*(++argv));
439 			if (bytes == 0L)
440 				bytes = 1L;
441 			i = strlen(argv[0]);
442 			if (argv[0][i - 1] == 'k')
443 				bytes*=1024L;
444 			if (argv[0][i - 1] == 'm')
445 				bytes*=1024L*1024L;
446 		} else if (strcmp(*argv, "-cert") == 0) {
447 			if (--argc < 1)
448 				goto bad;
449 			server_cert= *(++argv);
450 		} else if (strcmp(*argv, "-s_cert") == 0) {
451 			if (--argc < 1)
452 				goto bad;
453 			server_cert= *(++argv);
454 		} else if (strcmp(*argv, "-key") == 0) {
455 			if (--argc < 1)
456 				goto bad;
457 			server_key= *(++argv);
458 		} else if (strcmp(*argv, "-s_key") == 0) {
459 			if (--argc < 1)
460 				goto bad;
461 			server_key= *(++argv);
462 		} else if (strcmp(*argv, "-c_cert") == 0) {
463 			if (--argc < 1)
464 				goto bad;
465 			client_cert= *(++argv);
466 		} else if (strcmp(*argv, "-c_key") == 0) {
467 			if (--argc < 1)
468 				goto bad;
469 			client_key= *(++argv);
470 		} else if (strcmp(*argv, "-cipher") == 0) {
471 			if (--argc < 1)
472 				goto bad;
473 			cipher= *(++argv);
474 		} else if (strcmp(*argv, "-CApath") == 0) {
475 			if (--argc < 1)
476 				goto bad;
477 			CApath= *(++argv);
478 		} else if (strcmp(*argv, "-CAfile") == 0) {
479 			if (--argc < 1)
480 				goto bad;
481 			CAfile= *(++argv);
482 		} else if (strcmp(*argv, "-bio_pair") == 0) {
483 			bio_pair = 1;
484 		} else if (strcmp(*argv, "-f") == 0) {
485 			force = 1;
486 		} else if (strcmp(*argv, "-time") == 0) {
487 			print_time = 1;
488 		} else if (strcmp(*argv, "-named_curve") == 0) {
489 			if (--argc < 1)
490 				goto bad;
491 			named_curve = *(++argv);
492 		} else if (strcmp(*argv, "-app_verify") == 0) {
493 			app_verify_arg.app_verify = 1;
494 		} else if (strcmp(*argv, "-proxy") == 0) {
495 			app_verify_arg.allow_proxy_certs = 1;
496 		} else if (strcmp(*argv, "-test_cipherlist") == 0) {
497 			test_cipherlist = 1;
498 		} else {
499 			fprintf(stderr, "unknown option %s\n", *argv);
500 			badop = 1;
501 			break;
502 		}
503 		argc--;
504 		argv++;
505 	}
506 	if (badop) {
507 bad:
508 		sv_usage();
509 		goto end;
510 	}
511 
512 	if (test_cipherlist == 1) {
513 		/* ensure that the cipher list are correctly sorted and exit */
514 		if (do_test_cipherlist() == 0)
515 			exit(1);
516 		ret = 0;
517 		goto end;
518 	}
519 
520 	if (!dtls1 && !ssl2 && !ssl3 && !tls1 &&
521 	    number > 1 && !reuse && !force) {
522 		fprintf(stderr,
523 		    "This case cannot work.  Use -f to perform "
524 		    "the test anyway (and\n-d to see what happens), "
525 		    "or add one of -dtls1, -ssl2, -ssl3, -tls1, -reuse\n"
526 		    "to avoid protocol mismatch.\n");
527 		exit(1);
528 	}
529 
530 	if (print_time) {
531 		if (!bio_pair) {
532 			fprintf(stderr, "Using BIO pair (-bio_pair)\n");
533 			bio_pair = 1;
534 		}
535 		if (number < 50 && !force)
536 			fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
537 	}
538 
539 /*	if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
540 
541 	SSL_library_init();
542 	SSL_load_error_strings();
543 
544 
545 	if (dtls1)
546 		meth = DTLSv1_method();
547 	else if (tls1)
548 		meth = TLSv1_method();
549 	else if (ssl3)
550 		meth = SSLv3_method();
551 	else
552 		meth = SSLv23_method();
553 
554 	c_ctx = SSL_CTX_new(meth);
555 	s_ctx = SSL_CTX_new(meth);
556 	if ((c_ctx == NULL) || (s_ctx == NULL)) {
557 		ERR_print_errors(bio_err);
558 		goto end;
559 	}
560 
561 	if (cipher != NULL) {
562 		SSL_CTX_set_cipher_list(c_ctx, cipher);
563 		SSL_CTX_set_cipher_list(s_ctx, cipher);
564 	}
565 
566 	if (!no_dhe) {
567 		if (dhe1024dsa) {
568 			/* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
569 			SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
570 			dh = get_dh1024dsa();
571 		} else if (dhe1024)
572 			dh = get_dh1024();
573 		else
574 			dh = get_dh512();
575 		SSL_CTX_set_tmp_dh(s_ctx, dh);
576 		DH_free(dh);
577 	}
578 
579 	if (!no_ecdhe) {
580 		int nid;
581 
582 		if (named_curve != NULL) {
583 			nid = OBJ_sn2nid(named_curve);
584 			if (nid == 0) {
585 				BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);
586 				goto end;
587 			}
588 		} else
589 #ifdef OPENSSL_NO_EC2M
590 			nid = NID_X9_62_prime256v1;
591 #else
592 		nid = NID_sect163r2;
593 #endif
594 
595 		ecdh = EC_KEY_new_by_curve_name(nid);
596 		if (ecdh == NULL) {
597 			BIO_printf(bio_err, "unable to create curve\n");
598 			goto end;
599 		}
600 
601 		SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);
602 		SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);
603 		EC_KEY_free(ecdh);
604 	}
605 
606 	SSL_CTX_set_tmp_rsa_callback(s_ctx, tmp_rsa_cb);
607 
608 	if (!SSL_CTX_use_certificate_file(s_ctx, server_cert,
609 	    SSL_FILETYPE_PEM)) {
610 		ERR_print_errors(bio_err);
611 	} else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
612 	    (server_key ? server_key : server_cert), SSL_FILETYPE_PEM)) {
613 		ERR_print_errors(bio_err);
614 		goto end;
615 	}
616 
617 	if (client_auth) {
618 		SSL_CTX_use_certificate_file(c_ctx, client_cert,
619 		    SSL_FILETYPE_PEM);
620 		SSL_CTX_use_PrivateKey_file(c_ctx,
621 		    (client_key ? client_key : client_cert),
622 		    SSL_FILETYPE_PEM);
623 	}
624 
625 	if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) ||
626 	    (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
627 	    (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) ||
628 	    (!SSL_CTX_set_default_verify_paths(c_ctx))) {
629 		/* fprintf(stderr,"SSL_load_verify_locations\n"); */
630 		ERR_print_errors(bio_err);
631 		/* goto end; */
632 	}
633 
634 	if (client_auth) {
635 		BIO_printf(bio_err, "client authentication\n");
636 		SSL_CTX_set_verify(s_ctx,
637 		    SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
638 		    verify_callback);
639 		SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
640 		    &app_verify_arg);
641 	}
642 	if (server_auth) {
643 		BIO_printf(bio_err, "server authentication\n");
644 		SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER,
645 		    verify_callback);
646 		SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
647 		    &app_verify_arg);
648 	}
649 
650 	{
651 		int session_id_context = 0;
652 		SSL_CTX_set_session_id_context(s_ctx,
653 		    (void *)&session_id_context, sizeof(session_id_context));
654 	}
655 
656 	c_ssl = SSL_new(c_ctx);
657 	s_ssl = SSL_new(s_ctx);
658 
659 	for (i = 0; i < number; i++) {
660 		if (!reuse)
661 			SSL_set_session(c_ssl, NULL);
662 		if (bio_pair)
663 			ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time,
664 			    &c_time);
665 		else
666 			ret = doit(s_ssl, c_ssl, bytes);
667 	}
668 
669 	if (!verbose) {
670 		print_details(c_ssl, "");
671 	}
672 	if ((number > 1) || (bytes > 1L))
673 		BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",
674 		    number, bytes);
675 	if (print_time) {
676 #ifdef CLOCKS_PER_SEC
677 		/* "To determine the time in seconds, the value returned
678 		 * by the clock function should be divided by the value
679 		 * of the macro CLOCKS_PER_SEC."
680 		 *                                       -- ISO/IEC 9899 */
681 		BIO_printf(bio_stdout,
682 		    "Approximate total server time: %6.2f s\n"
683 		    "Approximate total client time: %6.2f s\n",
684 		    (double)s_time/CLOCKS_PER_SEC,
685 		    (double)c_time/CLOCKS_PER_SEC);
686 #else
687 		/* "`CLOCKS_PER_SEC' undeclared (first use this function)"
688 		 *                            -- cc on NeXTstep/OpenStep */
689 		BIO_printf(bio_stdout,
690 		    "Approximate total server time: %6.2f units\n"
691 		    "Approximate total client time: %6.2f units\n",
692 		    (double)s_time,
693 		    (double)c_time);
694 #endif
695 	}
696 
697 	SSL_free(s_ssl);
698 	SSL_free(c_ssl);
699 
700 end:
701 	if (s_ctx != NULL)
702 		SSL_CTX_free(s_ctx);
703 	if (c_ctx != NULL)
704 		SSL_CTX_free(c_ctx);
705 
706 	if (bio_stdout != NULL)
707 		BIO_free(bio_stdout);
708 
709 	free_tmp_rsa();
710 #ifndef OPENSSL_NO_ENGINE
711 	ENGINE_cleanup();
712 #endif
713 	CRYPTO_cleanup_all_ex_data();
714 	ERR_free_strings();
715 	ERR_remove_thread_state(NULL);
716 	EVP_cleanup();
717 	CRYPTO_mem_leaks(bio_err);
718 	if (bio_err != NULL)
719 		BIO_free(bio_err);
720 	exit(ret);
721 	return ret;
722 }
723 
724 int
725 doit_biopair(SSL *s_ssl, SSL *c_ssl, long count, clock_t *s_time,
726     clock_t *c_time)
727 {
728 	long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
729 	BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
730 	BIO *server = NULL, *server_io = NULL;
731 	BIO *client = NULL, *client_io = NULL;
732 	int ret = 1;
733 
734 	size_t bufsiz = 256; /* small buffer for testing */
735 
736 	if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
737 		goto err;
738 	if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
739 		goto err;
740 
741 	s_ssl_bio = BIO_new(BIO_f_ssl());
742 	if (!s_ssl_bio)
743 		goto err;
744 
745 	c_ssl_bio = BIO_new(BIO_f_ssl());
746 	if (!c_ssl_bio)
747 		goto err;
748 
749 	SSL_set_connect_state(c_ssl);
750 	SSL_set_bio(c_ssl, client, client);
751 	(void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
752 
753 	SSL_set_accept_state(s_ssl);
754 	SSL_set_bio(s_ssl, server, server);
755 	(void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
756 
757 	do {
758 		/* c_ssl_bio:          SSL filter BIO
759 		 *
760 		 * client:             pseudo-I/O for SSL library
761 		 *
762 		 * client_io:          client's SSL communication; usually to be
763 		 *                     relayed over some I/O facility, but in this
764 		 *                     test program, we're the server, too:
765 		 *
766 		 * server_io:          server's SSL communication
767 		 *
768 		 * server:             pseudo-I/O for SSL library
769 		 *
770 		 * s_ssl_bio:          SSL filter BIO
771 		 *
772 		 * The client and the server each employ a "BIO pair":
773 		 * client + client_io, server + server_io.
774 		 * BIO pairs are symmetric.  A BIO pair behaves similar
775 		 * to a non-blocking socketpair (but both endpoints must
776 		 * be handled by the same thread).
777 		 * [Here we could connect client and server to the ends
778 		 * of a single BIO pair, but then this code would be less
779 		 * suitable as an example for BIO pairs in general.]
780 		 *
781 		 * Useful functions for querying the state of BIO pair endpoints:
782 		 *
783 		 * BIO_ctrl_pending(bio)              number of bytes we can read now
784 		 * BIO_ctrl_get_read_request(bio)     number of bytes needed to fulfil
785 		 *                                      other side's read attempt
786 		 * BIO_ctrl_get_write_guarantee(bio)   number of bytes we can write now
787 		 *
788 		 * ..._read_request is never more than ..._write_guarantee;
789 		 * it depends on the application which one you should use.
790 		 */
791 
792 		/* We have non-blocking behaviour throughout this test program, but
793 		 * can be sure that there is *some* progress in each iteration; so
794 		 * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
795 		 * -- we just try everything in each iteration
796 		 */
797 
798 		{
799 			/* CLIENT */
800 
801 			char cbuf[1024*8];
802 			int i, r;
803 			clock_t c_clock = clock();
804 
805 			memset(cbuf, 0, sizeof(cbuf));
806 
807 			if (debug)
808 				if (SSL_in_init(c_ssl))
809 					printf("client waiting in SSL_connect - %s\n",
810 					    SSL_state_string_long(c_ssl));
811 
812 			if (cw_num > 0) {
813 				/* Write to server. */
814 
815 				if (cw_num > (long)sizeof cbuf)
816 					i = sizeof cbuf;
817 				else
818 					i = (int)cw_num;
819 				r = BIO_write(c_ssl_bio, cbuf, i);
820 				if (r < 0) {
821 					if (!BIO_should_retry(c_ssl_bio)) {
822 						fprintf(stderr, "ERROR in CLIENT\n");
823 						goto err;
824 					}
825 					/* BIO_should_retry(...) can just be ignored here.
826 					 * The library expects us to call BIO_write with
827 					 * the same arguments again, and that's what we will
828 					 * do in the next iteration. */
829 				} else if (r == 0) {
830 					fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
831 					goto err;
832 				} else {
833 					if (debug)
834 						printf("client wrote %d\n", r);
835 					cw_num -= r;
836 
837 				}
838 			}
839 
840 			if (cr_num > 0) {
841 				/* Read from server. */
842 
843 				r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
844 				if (r < 0) {
845 					if (!BIO_should_retry(c_ssl_bio)) {
846 						fprintf(stderr, "ERROR in CLIENT\n");
847 						goto err;
848 					}
849 					/* Again, "BIO_should_retry" can be ignored. */
850 				} else if (r == 0) {
851 					fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
852 					goto err;
853 				} else {
854 					if (debug)
855 						printf("client read %d\n", r);
856 					cr_num -= r;
857 				}
858 			}
859 
860 			/* c_time and s_time increments will typically be very small
861 			 * (depending on machine speed and clock tick intervals),
862 			 * but sampling over a large number of connections should
863 			 * result in fairly accurate figures.  We cannot guarantee
864 			 * a lot, however -- if each connection lasts for exactly
865 			 * one clock tick, it will be counted only for the client
866 			 * or only for the server or even not at all.
867 			 */
868 			*c_time += (clock() - c_clock);
869 		}
870 
871 		{
872 			/* SERVER */
873 
874 			char sbuf[1024*8];
875 			int i, r;
876 			clock_t s_clock = clock();
877 
878 			memset(sbuf, 0, sizeof(sbuf));
879 
880 			if (debug)
881 				if (SSL_in_init(s_ssl))
882 					printf("server waiting in SSL_accept - %s\n",
883 					    SSL_state_string_long(s_ssl));
884 
885 			if (sw_num > 0) {
886 				/* Write to client. */
887 
888 				if (sw_num > (long)sizeof sbuf)
889 					i = sizeof sbuf;
890 				else
891 					i = (int)sw_num;
892 				r = BIO_write(s_ssl_bio, sbuf, i);
893 				if (r < 0) {
894 					if (!BIO_should_retry(s_ssl_bio)) {
895 						fprintf(stderr, "ERROR in SERVER\n");
896 						goto err;
897 					}
898 					/* Ignore "BIO_should_retry". */
899 				} else if (r == 0) {
900 					fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
901 					goto err;
902 				} else {
903 					if (debug)
904 						printf("server wrote %d\n", r);
905 					sw_num -= r;
906 
907 				}
908 			}
909 
910 			if (sr_num > 0) {
911 				/* Read from client. */
912 
913 				r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
914 				if (r < 0) {
915 					if (!BIO_should_retry(s_ssl_bio)) {
916 						fprintf(stderr, "ERROR in SERVER\n");
917 						goto err;
918 					}
919 					/* blah, blah */
920 				} else if (r == 0) {
921 					fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
922 					goto err;
923 				} else {
924 					if (debug)
925 						printf("server read %d\n", r);
926 					sr_num -= r;
927 				}
928 			}
929 
930 			*s_time += (clock() - s_clock);
931 		}
932 
933 		{
934 			/* "I/O" BETWEEN CLIENT AND SERVER. */
935 
936 			size_t r1, r2;
937 			BIO *io1 = server_io, *io2 = client_io;
938 			/* we use the non-copying interface for io1
939 			 * and the standard BIO_write/BIO_read interface for io2
940 			 */
941 
942 			static int prev_progress = 1;
943 			int progress = 0;
944 
945 			/* io1 to io2 */
946 			do {
947 				size_t num;
948 				int r;
949 
950 				r1 = BIO_ctrl_pending(io1);
951 				r2 = BIO_ctrl_get_write_guarantee(io2);
952 
953 				num = r1;
954 				if (r2 < num)
955 					num = r2;
956 				if (num) {
957 					char *dataptr;
958 
959 					if (INT_MAX < num) /* yeah, right */
960 						num = INT_MAX;
961 
962 					r = BIO_nread(io1, &dataptr, (int)num);
963 					assert(r > 0);
964 					assert(r <= (int)num);
965 					/* possibly r < num (non-contiguous data) */
966 					num = r;
967 					r = BIO_write(io2, dataptr, (int)num);
968 					if (r != (int)num) /* can't happen */
969 					{
970 						fprintf(stderr, "ERROR: BIO_write could not write "
971 						    "BIO_ctrl_get_write_guarantee() bytes");
972 						goto err;
973 					}
974 					progress = 1;
975 
976 					if (debug)
977 						printf((io1 == client_io) ?
978 						    "C->S relaying: %d bytes\n" :
979 						    "S->C relaying: %d bytes\n",
980 						    (int)num);
981 				}
982 			} while (r1 && r2);
983 
984 			/* io2 to io1 */
985 			{
986 				size_t num;
987 				int r;
988 
989 				r1 = BIO_ctrl_pending(io2);
990 				r2 = BIO_ctrl_get_read_request(io1);
991 				/* here we could use ..._get_write_guarantee instead of
992 				 * ..._get_read_request, but by using the latter
993 				 * we test restartability of the SSL implementation
994 				 * more thoroughly */
995 				num = r1;
996 				if (r2 < num)
997 					num = r2;
998 				if (num) {
999 					char *dataptr;
1000 
1001 					if (INT_MAX < num)
1002 						num = INT_MAX;
1003 
1004 					if (num > 1)
1005 						--num; /* test restartability even more thoroughly */
1006 
1007 					r = BIO_nwrite0(io1, &dataptr);
1008 					assert(r > 0);
1009 					if (r < (int)num)
1010 						num = r;
1011 					r = BIO_read(io2, dataptr, (int)num);
1012 					if (r != (int)num) /* can't happen */
1013 					{
1014 						fprintf(stderr, "ERROR: BIO_read could not read "
1015 						    "BIO_ctrl_pending() bytes");
1016 						goto err;
1017 					}
1018 					progress = 1;
1019 					r = BIO_nwrite(io1, &dataptr, (int)num);
1020 					if (r != (int)num) /* can't happen */
1021 					{
1022 						fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
1023 						    "BIO_nwrite0() bytes");
1024 						goto err;
1025 					}
1026 
1027 					if (debug)
1028 						printf((io2 == client_io) ?
1029 						    "C->S relaying: %d bytes\n" :
1030 						    "S->C relaying: %d bytes\n",
1031 						    (int)num);
1032 				}
1033 			} /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
1034 
1035 			if (!progress && !prev_progress)
1036 				if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
1037 				fprintf(stderr, "ERROR: got stuck\n");
1038 				if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0) {
1039 					fprintf(stderr, "This can happen for SSL2 because "
1040 					    "CLIENT-FINISHED and SERVER-VERIFY are written \n"
1041 					    "concurrently ...");
1042 					if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0 &&
1043 						    strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0) {
1044 						fprintf(stderr, " ok.\n");
1045 						goto end;
1046 					}
1047 				}
1048 				fprintf(stderr, " ERROR.\n");
1049 				goto err;
1050 			}
1051 			prev_progress = progress;
1052 		}
1053 	} while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
1054 
1055 	if (verbose)
1056 		print_details(c_ssl, "DONE via BIO pair: ");
1057 end:
1058 	ret = 0;
1059 
1060 err:
1061 	ERR_print_errors(bio_err);
1062 
1063 	if (server)
1064 		BIO_free(server);
1065 	if (server_io)
1066 		BIO_free(server_io);
1067 	if (client)
1068 		BIO_free(client);
1069 	if (client_io)
1070 		BIO_free(client_io);
1071 	if (s_ssl_bio)
1072 		BIO_free(s_ssl_bio);
1073 	if (c_ssl_bio)
1074 		BIO_free(c_ssl_bio);
1075 
1076 	return ret;
1077 }
1078 
1079 
1080 #define W_READ	1
1081 #define W_WRITE	2
1082 #define C_DONE	1
1083 #define S_DONE	2
1084 
1085 int
1086 doit(SSL *s_ssl, SSL *c_ssl, long count)
1087 {
1088 	char cbuf[1024*8], sbuf[1024*8];
1089 	long cw_num = count, cr_num = count;
1090 	long sw_num = count, sr_num = count;
1091 	int ret = 1;
1092 	BIO *c_to_s = NULL;
1093 	BIO *s_to_c = NULL;
1094 	BIO *c_bio = NULL;
1095 	BIO *s_bio = NULL;
1096 	int c_r, c_w, s_r, s_w;
1097 	int i, j;
1098 	int done = 0;
1099 	int c_write, s_write;
1100 	int do_server = 0, do_client = 0;
1101 
1102 	memset(cbuf, 0, sizeof(cbuf));
1103 	memset(sbuf, 0, sizeof(sbuf));
1104 
1105 	c_to_s = BIO_new(BIO_s_mem());
1106 	s_to_c = BIO_new(BIO_s_mem());
1107 	if ((s_to_c == NULL) || (c_to_s == NULL)) {
1108 		ERR_print_errors(bio_err);
1109 		goto err;
1110 	}
1111 
1112 	c_bio = BIO_new(BIO_f_ssl());
1113 	s_bio = BIO_new(BIO_f_ssl());
1114 	if ((c_bio == NULL) || (s_bio == NULL)) {
1115 		ERR_print_errors(bio_err);
1116 		goto err;
1117 	}
1118 
1119 	SSL_set_connect_state(c_ssl);
1120 	SSL_set_bio(c_ssl, s_to_c, c_to_s);
1121 	BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
1122 
1123 	SSL_set_accept_state(s_ssl);
1124 	SSL_set_bio(s_ssl, c_to_s, s_to_c);
1125 	BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
1126 
1127 	c_r = 0;
1128 	s_r = 1;
1129 	c_w = 1;
1130 	s_w = 0;
1131 	c_write = 1, s_write = 0;
1132 
1133 	/* We can always do writes */
1134 	for (;;) {
1135 		do_server = 0;
1136 		do_client = 0;
1137 
1138 		i = (int)BIO_pending(s_bio);
1139 		if ((i && s_r) || s_w)
1140 			do_server = 1;
1141 
1142 		i = (int)BIO_pending(c_bio);
1143 		if ((i && c_r) || c_w)
1144 			do_client = 1;
1145 
1146 		if (do_server && debug) {
1147 			if (SSL_in_init(s_ssl))
1148 				printf("server waiting in SSL_accept - %s\n",
1149 				    SSL_state_string_long(s_ssl));
1150 /*			else if (s_write)
1151 				printf("server:SSL_write()\n");
1152 			else
1153 				printf("server:SSL_read()\n"); */
1154 			}
1155 
1156 			if (do_client && debug) {
1157 			if (SSL_in_init(c_ssl))
1158 				printf("client waiting in SSL_connect - %s\n",
1159 				    SSL_state_string_long(c_ssl));
1160 /*			else if (c_write)
1161 				printf("client:SSL_write()\n");
1162 			else
1163 				printf("client:SSL_read()\n"); */
1164 			}
1165 
1166 			if (!do_client && !do_server) {
1167 			fprintf(stdout, "ERROR IN STARTUP\n");
1168 			ERR_print_errors(bio_err);
1169 			break;
1170 		}
1171 		if (do_client && !(done & C_DONE)) {
1172 			if (c_write) {
1173 				j = (cw_num > (long)sizeof(cbuf)) ?
1174 				    (int)sizeof(cbuf) : (int)cw_num;
1175 				i = BIO_write(c_bio, cbuf, j);
1176 				if (i < 0) {
1177 					c_r = 0;
1178 					c_w = 0;
1179 					if (BIO_should_retry(c_bio)) {
1180 						if (BIO_should_read(c_bio))
1181 							c_r = 1;
1182 						if (BIO_should_write(c_bio))
1183 							c_w = 1;
1184 					} else {
1185 						fprintf(stderr, "ERROR in CLIENT\n");
1186 						ERR_print_errors(bio_err);
1187 						goto err;
1188 					}
1189 				} else if (i == 0) {
1190 					fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
1191 					goto err;
1192 				} else {
1193 					if (debug)
1194 						printf("client wrote %d\n", i);
1195 					/* ok */
1196 					s_r = 1;
1197 					c_write = 0;
1198 					cw_num -= i;
1199 				}
1200 			} else {
1201 				i = BIO_read(c_bio, cbuf, sizeof(cbuf));
1202 				if (i < 0) {
1203 					c_r = 0;
1204 					c_w = 0;
1205 					if (BIO_should_retry(c_bio)) {
1206 						if (BIO_should_read(c_bio))
1207 							c_r = 1;
1208 						if (BIO_should_write(c_bio))
1209 							c_w = 1;
1210 					} else {
1211 						fprintf(stderr, "ERROR in CLIENT\n");
1212 						ERR_print_errors(bio_err);
1213 						goto err;
1214 					}
1215 				} else if (i == 0) {
1216 					fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
1217 					goto err;
1218 				} else {
1219 					if (debug)
1220 						printf("client read %d\n", i);
1221 					cr_num -= i;
1222 					if (sw_num > 0) {
1223 						s_write = 1;
1224 						s_w = 1;
1225 					}
1226 					if (cr_num <= 0) {
1227 						s_write = 1;
1228 						s_w = 1;
1229 						done = S_DONE|C_DONE;
1230 					}
1231 				}
1232 			}
1233 		}
1234 
1235 		if (do_server && !(done & S_DONE)) {
1236 			if (!s_write) {
1237 				i = BIO_read(s_bio, sbuf, sizeof(cbuf));
1238 				if (i < 0) {
1239 					s_r = 0;
1240 					s_w = 0;
1241 					if (BIO_should_retry(s_bio)) {
1242 						if (BIO_should_read(s_bio))
1243 							s_r = 1;
1244 						if (BIO_should_write(s_bio))
1245 							s_w = 1;
1246 					} else {
1247 						fprintf(stderr, "ERROR in SERVER\n");
1248 						ERR_print_errors(bio_err);
1249 						goto err;
1250 					}
1251 				} else if (i == 0) {
1252 					ERR_print_errors(bio_err);
1253 					fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_read\n");
1254 					goto err;
1255 				} else {
1256 					if (debug)
1257 						printf("server read %d\n", i);
1258 					sr_num -= i;
1259 					if (cw_num > 0) {
1260 						c_write = 1;
1261 						c_w = 1;
1262 					}
1263 					if (sr_num <= 0) {
1264 						s_write = 1;
1265 						s_w = 1;
1266 						c_write = 0;
1267 					}
1268 				}
1269 			} else {
1270 				j = (sw_num > (long)sizeof(sbuf)) ?
1271 				    (int)sizeof(sbuf) : (int)sw_num;
1272 				i = BIO_write(s_bio, sbuf, j);
1273 				if (i < 0) {
1274 					s_r = 0;
1275 					s_w = 0;
1276 					if (BIO_should_retry(s_bio)) {
1277 						if (BIO_should_read(s_bio))
1278 							s_r = 1;
1279 						if (BIO_should_write(s_bio))
1280 							s_w = 1;
1281 					} else {
1282 						fprintf(stderr, "ERROR in SERVER\n");
1283 						ERR_print_errors(bio_err);
1284 						goto err;
1285 					}
1286 				} else if (i == 0) {
1287 					ERR_print_errors(bio_err);
1288 					fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_write\n");
1289 					goto err;
1290 				} else {
1291 					if (debug)
1292 						printf("server wrote %d\n", i);
1293 					sw_num -= i;
1294 					s_write = 0;
1295 					c_r = 1;
1296 					if (sw_num <= 0)
1297 						done |= S_DONE;
1298 				}
1299 			}
1300 		}
1301 
1302 		if ((done & S_DONE) && (done & C_DONE))
1303 			break;
1304 	}
1305 
1306 	if (verbose)
1307 		print_details(c_ssl, "DONE: ");
1308 	ret = 0;
1309 err:
1310 	/* We have to set the BIO's to NULL otherwise they will be
1311 	 * free()ed twice.  Once when th s_ssl is SSL_free()ed and
1312 	 * again when c_ssl is SSL_free()ed.
1313 	 * This is a hack required because s_ssl and c_ssl are sharing the same
1314 	 * BIO structure and SSL_set_bio() and SSL_free() automatically
1315 	 * BIO_free non NULL entries.
1316 	 * You should not normally do this or be required to do this */
1317 	if (s_ssl != NULL) {
1318 		s_ssl->rbio = NULL;
1319 		s_ssl->wbio = NULL;
1320 	}
1321 	if (c_ssl != NULL) {
1322 		c_ssl->rbio = NULL;
1323 		c_ssl->wbio = NULL;
1324 	}
1325 
1326 	if (c_to_s != NULL)
1327 		BIO_free(c_to_s);
1328 	if (s_to_c != NULL)
1329 		BIO_free(s_to_c);
1330 	if (c_bio != NULL)
1331 		BIO_free_all(c_bio);
1332 	if (s_bio != NULL)
1333 		BIO_free_all(s_bio);
1334 	return (ret);
1335 }
1336 
1337 static int
1338 get_proxy_auth_ex_data_idx(void)
1339 {
1340 	static volatile int idx = -1;
1341 	if (idx < 0) {
1342 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
1343 		if (idx < 0) {
1344 			idx = X509_STORE_CTX_get_ex_new_index(0,
1345 			    "SSLtest for verify callback", NULL, NULL, NULL);
1346 		}
1347 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
1348 	}
1349 	return idx;
1350 }
1351 
1352 static int
1353 verify_callback(int ok, X509_STORE_CTX *ctx)
1354 {
1355 	char *s, buf[256];
1356 
1357 	s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), buf,
1358 	    sizeof buf);
1359 	if (s != NULL) {
1360 		if (ok)
1361 			fprintf(stderr, "depth=%d %s\n",
1362 			    ctx->error_depth, buf);
1363 		else {
1364 			fprintf(stderr, "depth=%d error=%d %s\n",
1365 			    ctx->error_depth, ctx->error, buf);
1366 		}
1367 	}
1368 
1369 	if (ok == 0) {
1370 		fprintf(stderr, "Error string: %s\n",
1371 		    X509_verify_cert_error_string(ctx->error));
1372 		switch (ctx->error) {
1373 		case X509_V_ERR_CERT_NOT_YET_VALID:
1374 		case X509_V_ERR_CERT_HAS_EXPIRED:
1375 		case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1376 			fprintf(stderr, "  ... ignored.\n");
1377 			ok = 1;
1378 		}
1379 	}
1380 
1381 	if (ok == 1) {
1382 		X509 *xs = ctx->current_cert;
1383 #if 0
1384 		X509 *xi = ctx->current_issuer;
1385 #endif
1386 
1387 		if (xs->ex_flags & EXFLAG_PROXY) {
1388 			unsigned int *letters =
1389 			    X509_STORE_CTX_get_ex_data(ctx,
1390 			    get_proxy_auth_ex_data_idx());
1391 
1392 			if (letters) {
1393 				int found_any = 0;
1394 				int i;
1395 				PROXY_CERT_INFO_EXTENSION *pci =
1396 				    X509_get_ext_d2i(xs, NID_proxyCertInfo,
1397 				    NULL, NULL);
1398 
1399 				switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage)) {
1400 				case NID_Independent:
1401 					/* Completely meaningless in this
1402 					   program, as there's no way to
1403 					   grant explicit rights to a
1404 					   specific PrC.  Basically, using
1405 					   id-ppl-Independent is the perfect
1406 					   way to grant no rights at all. */
1407 					fprintf(stderr, "  Independent proxy certificate");
1408 					for (i = 0; i < 26; i++)
1409 						letters[i] = 0;
1410 					break;
1411 				case NID_id_ppl_inheritAll:
1412 					/* This is basically a NOP, we
1413 					   simply let the current rights
1414 					   stand as they are. */
1415 					fprintf(stderr, "  Proxy certificate inherits all");
1416 					break;
1417 				default:
1418 					s = (char *)
1419 					pci->proxyPolicy->policy->data;
1420 					i = pci->proxyPolicy->policy->length;
1421 
1422 					/* The algorithm works as follows:
1423 					   it is assumed that previous
1424 					   iterations or the initial granted
1425 					   rights has already set some elements
1426 					   of `letters'.  What we need to do is
1427 					   to clear those that weren't granted
1428 					   by the current PrC as well.  The
1429 					   easiest way to do this is to add 1
1430 					   to all the elements whose letters
1431 					   are given with the current policy.
1432 					   That way, all elements that are set
1433 					   by the current policy and were
1434 					   already set by earlier policies and
1435 					   through the original grant of rights
1436 					   will get the value 2 or higher.
1437 					   The last thing to do is to sweep
1438 					   through `letters' and keep the
1439 					   elements having the value 2 as set,
1440 					   and clear all the others. */
1441 
1442 					fprintf(stderr, "  Certificate proxy rights = %*.*s", i, i, s);
1443 					while (i-- > 0) {
1444 						int c = *s++;
1445 						if (isascii(c) && isalpha(c)) {
1446 							if (islower(c))
1447 								c = toupper(c);
1448 							letters[c - 'A']++;
1449 						}
1450 					}
1451 					for (i = 0; i < 26; i++)
1452 						if (letters[i] < 2)
1453 							letters[i] = 0;
1454 					else
1455 						letters[i] = 1;
1456 				}
1457 
1458 				found_any = 0;
1459 				fprintf(stderr, ", resulting proxy rights = ");
1460 				for (i = 0; i < 26; i++)
1461 					if (letters[i]) {
1462 					fprintf(stderr, "%c", i + 'A');
1463 					found_any = 1;
1464 				}
1465 				if (!found_any)
1466 					fprintf(stderr, "none");
1467 				fprintf(stderr, "\n");
1468 
1469 				PROXY_CERT_INFO_EXTENSION_free(pci);
1470 			}
1471 		}
1472 	}
1473 
1474 	return (ok);
1475 }
1476 
1477 static void
1478 process_proxy_debug(int indent, const char *format, ...)
1479 {
1480 	static const char indentation[] =
1481 	    ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
1482 	    ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; /* That's 80 > */
1483 	char my_format[256];
1484 	va_list args;
1485 
1486 	(void) snprintf(my_format, sizeof(my_format), "%*.*s %s",
1487 	    indent, indent, indentation, format);
1488 
1489 	va_start(args, format);
1490 	vfprintf(stderr, my_format, args);
1491 	va_end(args);
1492 }
1493 /* Priority levels:
1494    0	[!]var, ()
1495    1	& ^
1496    2	|
1497 */
1498 static int process_proxy_cond_adders(unsigned int letters[26],
1499     const char *cond, const char **cond_end, int *pos, int indent);
1500 
1501 static int
1502 process_proxy_cond_val(unsigned int letters[26], const char *cond,
1503     const char **cond_end, int *pos, int indent)
1504 {
1505 	int c;
1506 	int ok = 1;
1507 	int negate = 0;
1508 
1509 	while (isspace((int)*cond)) {
1510 		cond++;
1511 		(*pos)++;
1512 	}
1513 	c = *cond;
1514 
1515 	if (debug)
1516 		process_proxy_debug(indent,
1517 		    "Start process_proxy_cond_val at position %d: %s\n",
1518 		    *pos, cond);
1519 
1520 	while (c == '!') {
1521 		negate = !negate;
1522 		cond++;
1523 		(*pos)++;
1524 		while (isspace((int)*cond)) {
1525 			cond++;
1526 			(*pos)++;
1527 		}
1528 		c = *cond;
1529 	}
1530 
1531 	if (c == '(') {
1532 		cond++;
1533 		(*pos)++;
1534 		ok = process_proxy_cond_adders(letters, cond, cond_end, pos,
1535 		    indent + 1);
1536 		cond = *cond_end;
1537 		if (ok < 0)
1538 			goto end;
1539 		while (isspace((int)*cond)) {
1540 			cond++;
1541 			(*pos)++;
1542 		}
1543 		c = *cond;
1544 		if (c != ')') {
1545 			fprintf(stderr,
1546 			    "Weird condition character in position %d: "
1547 			    "%c\n", *pos, c);
1548 			ok = -1;
1549 			goto end;
1550 		}
1551 		cond++;
1552 		(*pos)++;
1553 	} else if (isascii(c) && isalpha(c)) {
1554 		if (islower(c))
1555 			c = toupper(c);
1556 		ok = letters[c - 'A'];
1557 		cond++;
1558 		(*pos)++;
1559 	} else {
1560 		fprintf(stderr,
1561 		    "Weird condition character in position %d: "
1562 		    "%c\n", *pos, c);
1563 		ok = -1;
1564 		goto end;
1565 	}
1566 end:
1567 	*cond_end = cond;
1568 	if (ok >= 0 && negate)
1569 		ok = !ok;
1570 
1571 	if (debug)
1572 		process_proxy_debug(indent,
1573 		    "End process_proxy_cond_val at position %d: %s, returning %d\n",
1574 		    *pos, cond, ok);
1575 
1576 	return ok;
1577 }
1578 
1579 static int
1580 process_proxy_cond_multipliers(unsigned int letters[26], const char *cond,
1581     const char **cond_end, int *pos, int indent)
1582 {
1583 	int ok;
1584 	char c;
1585 
1586 	if (debug)
1587 		process_proxy_debug(indent,
1588 		    "Start process_proxy_cond_multipliers at position %d: %s\n",
1589 		    *pos, cond);
1590 
1591 	ok = process_proxy_cond_val(letters, cond, cond_end, pos, indent + 1);
1592 	cond = *cond_end;
1593 	if (ok < 0)
1594 		goto end;
1595 
1596 	while (ok >= 0) {
1597 		while (isspace((int)*cond)) {
1598 			cond++;
1599 			(*pos)++;
1600 		}
1601 		c = *cond;
1602 
1603 		switch (c) {
1604 		case '&':
1605 		case '^':
1606 			{
1607 				int save_ok = ok;
1608 
1609 				cond++;
1610 				(*pos)++;
1611 				ok = process_proxy_cond_val(letters,
1612 				    cond, cond_end, pos, indent + 1);
1613 				cond = *cond_end;
1614 				if (ok < 0)
1615 					break;
1616 
1617 				switch (c) {
1618 				case '&':
1619 					ok &= save_ok;
1620 					break;
1621 				case '^':
1622 					ok ^= save_ok;
1623 					break;
1624 				default:
1625 					fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
1626 					    " STOPPING\n");
1627 					exit(1);
1628 				}
1629 			}
1630 			break;
1631 		default:
1632 			goto end;
1633 		}
1634 	}
1635 end:
1636 	if (debug)
1637 		process_proxy_debug(indent,
1638 		    "End process_proxy_cond_multipliers at position %d: %s, "
1639 		    "returning %d\n",
1640 		    *pos, cond, ok);
1641 
1642 	*cond_end = cond;
1643 	return ok;
1644 }
1645 
1646 static int
1647 process_proxy_cond_adders(unsigned int letters[26], const char *cond,
1648     const char **cond_end, int *pos, int indent)
1649 {
1650 	int ok;
1651 	char c;
1652 
1653 	if (debug)
1654 		process_proxy_debug(indent,
1655 		    "Start process_proxy_cond_adders at position %d: %s\n",
1656 		    *pos, cond);
1657 
1658 	ok = process_proxy_cond_multipliers(letters, cond, cond_end, pos,
1659 	    indent + 1);
1660 	cond = *cond_end;
1661 	if (ok < 0)
1662 		goto end;
1663 
1664 	while (ok >= 0) {
1665 		while (isspace((int)*cond)) {
1666 			cond++;
1667 			(*pos)++;
1668 		}
1669 		c = *cond;
1670 
1671 		switch (c) {
1672 		case '|':
1673 			{
1674 				int save_ok = ok;
1675 
1676 				cond++;
1677 				(*pos)++;
1678 				ok = process_proxy_cond_multipliers(letters,
1679 				    cond, cond_end, pos, indent + 1);
1680 				cond = *cond_end;
1681 				if (ok < 0)
1682 					break;
1683 
1684 				switch (c) {
1685 				case '|':
1686 					ok |= save_ok;
1687 					break;
1688 				default:
1689 					fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
1690 					    " STOPPING\n");
1691 					exit(1);
1692 				}
1693 			}
1694 			break;
1695 		default:
1696 			goto end;
1697 		}
1698 	}
1699 end:
1700 	if (debug)
1701 		process_proxy_debug(indent,
1702 		    "End process_proxy_cond_adders at position %d: %s, returning %d\n",
1703 		    *pos, cond, ok);
1704 
1705 	*cond_end = cond;
1706 	return ok;
1707 }
1708 
1709 static int
1710 process_proxy_cond(unsigned int letters[26], const char *cond,
1711     const char **cond_end)
1712 {
1713 	int pos = 1;
1714 	return process_proxy_cond_adders(letters, cond, cond_end, &pos, 1);
1715 }
1716 
1717 static int
1718 app_verify_callback(X509_STORE_CTX *ctx, void *arg)
1719 {
1720 	int ok = 1;
1721 	struct app_verify_arg *cb_arg = arg;
1722 	unsigned int letters[26]; /* only used with proxy_auth */
1723 
1724 	if (cb_arg->app_verify) {
1725 		char *s = NULL, buf[256];
1726 
1727 		fprintf(stderr, "In app_verify_callback, allowing cert. ");
1728 		fprintf(stderr, "Arg is: %s\n", cb_arg->string);
1729 		fprintf(stderr, "Finished printing do we have a context? 0x%p a cert? 0x%p\n",
1730 		    (void *)ctx, (void *)ctx->cert);
1731 		if (ctx->cert)
1732 			s = X509_NAME_oneline(X509_get_subject_name(ctx->cert), buf, 256);
1733 		if (s != NULL) {
1734 			fprintf(stderr, "cert depth=%d %s\n", ctx->error_depth, buf);
1735 		}
1736 		return (1);
1737 	}
1738 	if (cb_arg->proxy_auth) {
1739 		int found_any = 0, i;
1740 		char *sp;
1741 
1742 		for (i = 0; i < 26; i++)
1743 			letters[i] = 0;
1744 		for (sp = cb_arg->proxy_auth; *sp; sp++) {
1745 			int c = *sp;
1746 			if (isascii(c) && isalpha(c)) {
1747 				if (islower(c))
1748 					c = toupper(c);
1749 				letters[c - 'A'] = 1;
1750 			}
1751 		}
1752 
1753 		fprintf(stderr, "  Initial proxy rights = ");
1754 		for (i = 0; i < 26; i++)
1755 			if (letters[i]) {
1756 			fprintf(stderr, "%c", i + 'A');
1757 			found_any = 1;
1758 		}
1759 		if (!found_any)
1760 			fprintf(stderr, "none");
1761 		fprintf(stderr, "\n");
1762 
1763 		X509_STORE_CTX_set_ex_data(ctx,
1764 		    get_proxy_auth_ex_data_idx(), letters);
1765 	}
1766 	if (cb_arg->allow_proxy_certs) {
1767 		X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
1768 	}
1769 
1770 	ok = X509_verify_cert(ctx);
1771 
1772 	if (cb_arg->proxy_auth) {
1773 		if (ok > 0) {
1774 			const char *cond_end = NULL;
1775 
1776 			ok = process_proxy_cond(letters,
1777 			    cb_arg->proxy_cond, &cond_end);
1778 
1779 			if (ok < 0)
1780 				exit(3);
1781 			if (*cond_end) {
1782 				fprintf(stderr, "Stopped processing condition before it's end.\n");
1783 				ok = 0;
1784 			}
1785 			if (!ok)
1786 				fprintf(stderr, "Proxy rights check with condition '%s' proved invalid\n",
1787 				    cb_arg->proxy_cond);
1788 			else
1789 				fprintf(stderr, "Proxy rights check with condition '%s' proved valid\n",
1790 				    cb_arg->proxy_cond);
1791 		}
1792 	}
1793 	return (ok);
1794 }
1795 
1796 static RSA *rsa_tmp = NULL;
1797 
1798 static RSA *
1799 tmp_rsa_cb(SSL *s, int is_export, int keylength)
1800 {
1801 	BIGNUM *bn = NULL;
1802 	if (rsa_tmp == NULL) {
1803 		bn = BN_new();
1804 		rsa_tmp = RSA_new();
1805 		if (!bn || !rsa_tmp || !BN_set_word(bn, RSA_F4)) {
1806 			BIO_printf(bio_err, "Memory error...");
1807 			goto end;
1808 		}
1809 		BIO_printf(bio_err, "Generating temp (%d bit) RSA key...", keylength);
1810 		(void)BIO_flush(bio_err);
1811 		if (!RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) {
1812 			BIO_printf(bio_err, "Error generating key.");
1813 			RSA_free(rsa_tmp);
1814 			rsa_tmp = NULL;
1815 		}
1816 end:
1817 		BIO_printf(bio_err, "\n");
1818 		(void)BIO_flush(bio_err);
1819 	}
1820 	if (bn)
1821 		BN_free(bn);
1822 	return (rsa_tmp);
1823 }
1824 
1825 static void
1826 free_tmp_rsa(void)
1827 {
1828 	if (rsa_tmp != NULL) {
1829 		RSA_free(rsa_tmp);
1830 		rsa_tmp = NULL;
1831 	}
1832 }
1833 
1834 /* These DH parameters have been generated as follows:
1835  *    $ openssl dhparam -C -noout 512
1836  *    $ openssl dhparam -C -noout 1024
1837  *    $ openssl dhparam -C -noout -dsaparam 1024
1838  * (The third function has been renamed to avoid name conflicts.)
1839  */
1840 static DH *
1841 get_dh512()
1842 {
1843 	static unsigned char dh512_p[] = {
1844 		0xCB, 0xC8, 0xE1, 0x86, 0xD0, 0x1F, 0x94, 0x17, 0xA6, 0x99, 0xF0, 0xC6,
1845 		0x1F, 0x0D, 0xAC, 0xB6, 0x25, 0x3E, 0x06, 0x39, 0xCA, 0x72, 0x04, 0xB0,
1846 		0x6E, 0xDA, 0xC0, 0x61, 0xE6, 0x7A, 0x77, 0x25, 0xE8, 0x3B, 0xB9, 0x5F,
1847 		0x9A, 0xB6, 0xB5, 0xFE, 0x99, 0x0B, 0xA1, 0x93, 0x4E, 0x35, 0x33, 0xB8,
1848 		0xE1, 0xF1, 0x13, 0x4F, 0x59, 0x1A, 0xD2, 0x57, 0xC0, 0x26, 0x21, 0x33,
1849 		0x02, 0xC5, 0xAE, 0x23,
1850 	};
1851 	static unsigned char dh512_g[] = {
1852 		0x02,
1853 	};
1854 	DH *dh;
1855 
1856 	if ((dh = DH_new()) == NULL)
1857 		return (NULL);
1858 	dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
1859 	dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
1860 	if ((dh->p == NULL) || (dh->g == NULL)) {
1861 		DH_free(dh);
1862 		return (NULL);
1863 	}
1864 	return (dh);
1865 }
1866 
1867 static DH *
1868 get_dh1024()
1869 {
1870 	static unsigned char dh1024_p[] = {
1871 		0xF8, 0x81, 0x89, 0x7D, 0x14, 0x24, 0xC5, 0xD1, 0xE6, 0xF7, 0xBF, 0x3A,
1872 		0xE4, 0x90, 0xF4, 0xFC, 0x73, 0xFB, 0x34, 0xB5, 0xFA, 0x4C, 0x56, 0xA2,
1873 		0xEA, 0xA7, 0xE9, 0xC0, 0xC0, 0xCE, 0x89, 0xE1, 0xFA, 0x63, 0x3F, 0xB0,
1874 		0x6B, 0x32, 0x66, 0xF1, 0xD1, 0x7B, 0xB0, 0x00, 0x8F, 0xCA, 0x87, 0xC2,
1875 		0xAE, 0x98, 0x89, 0x26, 0x17, 0xC2, 0x05, 0xD2, 0xEC, 0x08, 0xD0, 0x8C,
1876 		0xFF, 0x17, 0x52, 0x8C, 0xC5, 0x07, 0x93, 0x03, 0xB1, 0xF6, 0x2F, 0xB8,
1877 		0x1C, 0x52, 0x47, 0x27, 0x1B, 0xDB, 0xD1, 0x8D, 0x9D, 0x69, 0x1D, 0x52,
1878 		0x4B, 0x32, 0x81, 0xAA, 0x7F, 0x00, 0xC8, 0xDC, 0xE6, 0xD9, 0xCC, 0xC1,
1879 		0x11, 0x2D, 0x37, 0x34, 0x6C, 0xEA, 0x02, 0x97, 0x4B, 0x0E, 0xBB, 0xB1,
1880 		0x71, 0x33, 0x09, 0x15, 0xFD, 0xDD, 0x23, 0x87, 0x07, 0x5E, 0x89, 0xAB,
1881 		0x6B, 0x7C, 0x5F, 0xEC, 0xA6, 0x24, 0xDC, 0x53,
1882 	};
1883 	static unsigned char dh1024_g[] = {
1884 		0x02,
1885 	};
1886 	DH *dh;
1887 
1888 	if ((dh = DH_new()) == NULL)
1889 		return (NULL);
1890 	dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
1891 	dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
1892 	if ((dh->p == NULL) || (dh->g == NULL)) {
1893 		DH_free(dh);
1894 		return (NULL);
1895 	}
1896 	return (dh);
1897 }
1898 
1899 static DH *
1900 get_dh1024dsa()
1901 {
1902 	static unsigned char dh1024_p[] = {
1903 		0xC8, 0x00, 0xF7, 0x08, 0x07, 0x89, 0x4D, 0x90, 0x53, 0xF3, 0xD5, 0x00,
1904 		0x21, 0x1B, 0xF7, 0x31, 0xA6, 0xA2, 0xDA, 0x23, 0x9A, 0xC7, 0x87, 0x19,
1905 		0x3B, 0x47, 0xB6, 0x8C, 0x04, 0x6F, 0xFF, 0xC6, 0x9B, 0xB8, 0x65, 0xD2,
1906 		0xC2, 0x5F, 0x31, 0x83, 0x4A, 0xA7, 0x5F, 0x2F, 0x88, 0x38, 0xB6, 0x55,
1907 		0xCF, 0xD9, 0x87, 0x6D, 0x6F, 0x9F, 0xDA, 0xAC, 0xA6, 0x48, 0xAF, 0xFC,
1908 		0x33, 0x84, 0x37, 0x5B, 0x82, 0x4A, 0x31, 0x5D, 0xE7, 0xBD, 0x52, 0x97,
1909 		0xA1, 0x77, 0xBF, 0x10, 0x9E, 0x37, 0xEA, 0x64, 0xFA, 0xCA, 0x28, 0x8D,
1910 		0x9D, 0x3B, 0xD2, 0x6E, 0x09, 0x5C, 0x68, 0xC7, 0x45, 0x90, 0xFD, 0xBB,
1911 		0x70, 0xC9, 0x3A, 0xBB, 0xDF, 0xD4, 0x21, 0x0F, 0xC4, 0x6A, 0x3C, 0xF6,
1912 		0x61, 0xCF, 0x3F, 0xD6, 0x13, 0xF1, 0x5F, 0xBC, 0xCF, 0xBC, 0x26, 0x9E,
1913 		0xBC, 0x0B, 0xBD, 0xAB, 0x5D, 0xC9, 0x54, 0x39,
1914 	};
1915 	static unsigned char dh1024_g[] = {
1916 		0x3B, 0x40, 0x86, 0xE7, 0xF3, 0x6C, 0xDE, 0x67, 0x1C, 0xCC, 0x80, 0x05,
1917 		0x5A, 0xDF, 0xFE, 0xBD, 0x20, 0x27, 0x74, 0x6C, 0x24, 0xC9, 0x03, 0xF3,
1918 		0xE1, 0x8D, 0xC3, 0x7D, 0x98, 0x27, 0x40, 0x08, 0xB8, 0x8C, 0x6A, 0xE9,
1919 		0xBB, 0x1A, 0x3A, 0xD6, 0x86, 0x83, 0x5E, 0x72, 0x41, 0xCE, 0x85, 0x3C,
1920 		0xD2, 0xB3, 0xFC, 0x13, 0xCE, 0x37, 0x81, 0x9E, 0x4C, 0x1C, 0x7B, 0x65,
1921 		0xD3, 0xE6, 0xA6, 0x00, 0xF5, 0x5A, 0x95, 0x43, 0x5E, 0x81, 0xCF, 0x60,
1922 		0xA2, 0x23, 0xFC, 0x36, 0xA7, 0x5D, 0x7A, 0x4C, 0x06, 0x91, 0x6E, 0xF6,
1923 		0x57, 0xEE, 0x36, 0xCB, 0x06, 0xEA, 0xF5, 0x3D, 0x95, 0x49, 0xCB, 0xA7,
1924 		0xDD, 0x81, 0xDF, 0x80, 0x09, 0x4A, 0x97, 0x4D, 0xA8, 0x22, 0x72, 0xA1,
1925 		0x7F, 0xC4, 0x70, 0x56, 0x70, 0xE8, 0x20, 0x10, 0x18, 0x8F, 0x2E, 0x60,
1926 		0x07, 0xE7, 0x68, 0x1A, 0x82, 0x5D, 0x32, 0xA2,
1927 	};
1928 	DH *dh;
1929 
1930 	if ((dh = DH_new()) == NULL)
1931 		return (NULL);
1932 	dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
1933 	dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
1934 	if ((dh->p == NULL) || (dh->g == NULL)) {
1935 		DH_free(dh);
1936 		return (NULL);
1937 	}
1938 	dh->length = 160;
1939 	return (dh);
1940 }
1941 
1942 static int
1943 do_test_cipherlist(void)
1944 {
1945 	int i = 0;
1946 	const SSL_METHOD *meth;
1947 	const SSL_CIPHER *ci, *tci = NULL;
1948 
1949 	fprintf(stderr, "testing SSLv3 cipher list order: ");
1950 	meth = SSLv3_method();
1951 	tci = NULL;
1952 	while ((ci = meth->get_cipher(i++)) != NULL) {
1953 		if (tci != NULL) {
1954 			if (ci->id >= tci->id) {
1955 				fprintf(stderr,
1956 				    "failed %lx vs. %lx\n", ci->id, tci->id);
1957 				return 0;
1958 			}
1959 		}
1960 		tci = ci;
1961 	}
1962 	fprintf(stderr, "ok\n");
1963 	fprintf(stderr, "testing TLSv1 cipher list order: ");
1964 	meth = TLSv1_method();
1965 	tci = NULL;
1966 	while ((ci = meth->get_cipher(i++)) != NULL) {
1967 		if (tci != NULL) {
1968 			if (ci->id >= tci->id) {
1969 				fprintf(stderr,
1970 				    "failed %lx vs. %lx\n", ci->id, tci->id);
1971 				return 0;
1972 			}
1973 		}
1974 		tci = ci;
1975 	}
1976 	fprintf(stderr, "ok\n");
1977 
1978 	return 1;
1979 }
1980