]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssltest.c
Setting the ex_data index is unsafe in a threaded environment, so
[thirdparty/openssl.git] / ssl / ssltest.c
CommitLineData
d02b48c6 1/* ssl/ssltest.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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 */
3ac82faa
BM
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 */
ea262260
BM
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 */
d02b48c6 116
4fbe40c5 117#define _BSD_SOURCE 1 /* Or gethostname won't be declared properly
37289744 118 on Linux and GNU platforms. */
37289744 119
6f7af152
BM
120#include <assert.h>
121#include <errno.h>
122#include <limits.h>
d02b48c6
RE
123#include <stdio.h>
124#include <stdlib.h>
125#include <string.h>
563f1503 126#include <time.h>
17e3dd1c 127
e7a28569 128#define USE_SOCKETS
41d2a336 129#include "e_os.h"
17e3dd1c 130
ec577822
BM
131#include <openssl/bio.h>
132#include <openssl/crypto.h>
563f1503 133#include <openssl/evp.h>
ec577822
BM
134#include <openssl/x509.h>
135#include <openssl/ssl.h>
0b13e9f0 136#ifndef OPENSSL_NO_ENGINE
b8e2f83a 137#include <openssl/engine.h>
0b13e9f0 138#endif
ec577822 139#include <openssl/err.h>
b9d82f47 140#include <openssl/rand.h>
09867a47
RL
141
142#define _XOPEN_SOURCE_EXTENDED 1 /* Or gethostname won't be declared properly
143 on Compaq platforms (at least with DEC C).
144 Do not try to put it earlier, or IPv6 includes
145 get screwed...
146 */
147
bc36ee62 148#ifdef OPENSSL_SYS_WINDOWS
f9b3bff6 149#include <winsock.h>
37289744
RL
150#else
151#include OPENSSL_UNISTD
d02b48c6
RE
152#endif
153
bc36ee62 154#ifdef OPENSSL_SYS_VMS
7d7d2cbc
UM
155# define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
156# define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
0bf23d9b
RL
157#elif defined(OPENSSL_SYS_WINCE)
158# define TEST_SERVER_CERT "\\OpenSSL\\server.pem"
159# define TEST_CLIENT_CERT "\\OpenSSL\\client.pem"
7d7d2cbc
UM
160#else
161# define TEST_SERVER_CERT "../apps/server.pem"
162# define TEST_CLIENT_CERT "../apps/client.pem"
163#endif
d02b48c6 164
23f80f46
RL
165/* There is really no standard for this, so let's assign some tentative
166 numbers. In any case, these numbers are only for this test */
167#define COMP_RLE 1
168#define COMP_ZLIB 2
169
396f6314 170static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
bc36ee62 171#ifndef OPENSSL_NO_RSA
df63a389 172static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
46b3bd54 173static void free_tmp_rsa(void);
79df9d62 174#endif
023ec151
BM
175static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg);
176#define APP_CALLBACK "Test Callback Argument"
177static char *app_verify_arg = APP_CALLBACK;
178
bc36ee62 179#ifndef OPENSSL_NO_DH
d02b48c6 180static DH *get_dh512(void);
e4589582
BM
181static DH *get_dh1024(void);
182static DH *get_dh1024dsa(void);
53002dc6
BM
183#endif
184
396f6314
BM
185static BIO *bio_err=NULL;
186static BIO *bio_stdout=NULL;
d02b48c6
RE
187
188static char *cipher=NULL;
79875776
BM
189static int verbose=0;
190static int debug=0;
d58d092b
BM
191#if 0
192/* Not used yet. */
d02b48c6
RE
193#ifdef FIONBIO
194static int s_nbio=0;
195#endif
d58d092b 196#endif
d02b48c6 197
b9d82f47 198static const char rnd_seed[] = "string to make the random number generator think it has entropy";
d02b48c6 199
563f1503 200int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
58964a49 201int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
6b691a5c 202static void sv_usage(void)
d02b48c6
RE
203 {
204 fprintf(stderr,"usage: ssltest [args ...]\n");
205 fprintf(stderr,"\n");
206 fprintf(stderr," -server_auth - check server certificate\n");
207 fprintf(stderr," -client_auth - do client authentication\n");
208 fprintf(stderr," -v - more output\n");
58964a49
RE
209 fprintf(stderr," -d - debug output\n");
210 fprintf(stderr," -reuse - use session-id reuse\n");
211 fprintf(stderr," -num <val> - number of connections to perform\n");
212 fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n");
bc36ee62 213#ifndef OPENSSL_NO_DH
e4589582
BM
214 fprintf(stderr," -dhe1024 - use 1024 bit key (safe prime) for DHE\n");
215 fprintf(stderr," -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
77fa04a9
BM
216 fprintf(stderr," -no_dhe - disable DHE\n");
217#endif
ea262260
BM
218#ifndef OPENSSL_NO_ECDH
219 fprintf(stderr," -no_ecdhe - disable ECDHE\n");
220#endif
bc36ee62 221#ifndef OPENSSL_NO_SSL2
d02b48c6
RE
222 fprintf(stderr," -ssl2 - use SSLv2\n");
223#endif
bc36ee62 224#ifndef OPENSSL_NO_SSL3
d02b48c6 225 fprintf(stderr," -ssl3 - use SSLv3\n");
58964a49 226#endif
bc36ee62 227#ifndef OPENSSL_NO_TLS1
58964a49 228 fprintf(stderr," -tls1 - use TLSv1\n");
d02b48c6
RE
229#endif
230 fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
231 fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
7d2509b6
BM
232 fprintf(stderr," -cert arg - Server certificate file\n");
233 fprintf(stderr," -key arg - Server key file (default: same as -cert)\n");
234 fprintf(stderr," -c_cert arg - Client certificate file\n");
235 fprintf(stderr," -c_key arg - Client key file (default: same as -c_cert)\n");
58964a49 236 fprintf(stderr," -cipher arg - The cipher list\n");
95d29597
BM
237 fprintf(stderr," -bio_pair - Use BIO pairs\n");
238 fprintf(stderr," -f - Test even cases that can't work\n");
563f1503 239 fprintf(stderr," -time - measure processor time used by client and server\n");
23f80f46 240 fprintf(stderr," -zlib - use zlib compression\n");
ea262260
BM
241 fprintf(stderr," -rle - use rle compression\n");
242#ifndef OPENSSL_NO_ECDH
243 fprintf(stderr," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
244 " Use \"openssl ecparam -list_curves\" for all names\n" \
245 " (default is sect163r2).\n");
246#endif
563f1503
BM
247 }
248
249static void print_details(SSL *c_ssl, const char *prefix)
250 {
251 SSL_CIPHER *ciph;
252 X509 *cert;
253
254 ciph=SSL_get_current_cipher(c_ssl);
255 BIO_printf(bio_stdout,"%s%s, cipher %s %s",
256 prefix,
257 SSL_get_version(c_ssl),
258 SSL_CIPHER_get_version(ciph),
259 SSL_CIPHER_get_name(ciph));
260 cert=SSL_get_peer_certificate(c_ssl);
261 if (cert != NULL)
262 {
263 EVP_PKEY *pkey = X509_get_pubkey(cert);
264 if (pkey != NULL)
265 {
266 if (0)
267 ;
bc36ee62 268#ifndef OPENSSL_NO_RSA
563f1503
BM
269 else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
270 && pkey->pkey.rsa->n != NULL)
271 {
272 BIO_printf(bio_stdout, ", %d bit RSA",
273 BN_num_bits(pkey->pkey.rsa->n));
274 }
275#endif
bc36ee62 276#ifndef OPENSSL_NO_DSA
563f1503
BM
277 else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
278 && pkey->pkey.dsa->p != NULL)
279 {
280 BIO_printf(bio_stdout, ", %d bit DSA",
281 BN_num_bits(pkey->pkey.dsa->p));
282 }
283#endif
284 EVP_PKEY_free(pkey);
285 }
286 X509_free(cert);
287 }
288 /* The SSL API does not allow us to look at temporary RSA/DH keys,
289 * otherwise we should print their lengths too */
290 BIO_printf(bio_stdout,"\n");
d02b48c6
RE
291 }
292
3ac82faa
BM
293static void 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 {
302 errstr = "invalid mode";
303 goto err;
304 }
305
563c05e2 306 if (type < 0 || type >= CRYPTO_NUM_LOCKS)
3ac82faa
BM
307 {
308 errstr = "type out of bounds";
309 goto err;
310 }
311
312 if (mode & CRYPTO_LOCK)
313 {
314 if (modes[type])
315 {
316 errstr = "already locked";
317 /* must not happen in a single-threaded program
318 * (would deadlock) */
319 goto err;
320 }
321
322 modes[type] = rw;
323 }
324 else if (mode & CRYPTO_UNLOCK)
325 {
326 if (!modes[type])
327 {
328 errstr = "not locked";
329 goto err;
330 }
331
332 if (modes[type] != rw)
333 {
334 errstr = (rw == CRYPTO_READ) ?
335 "CRYPTO_r_unlock on write lock" :
336 "CRYPTO_w_unlock on read lock";
337 }
338
339 modes[type] = 0;
340 }
341 else
342 {
343 errstr = "invalid mode";
344 goto err;
345 }
346
347 err:
348 if (errstr)
349 {
350 /* we cannot use bio_err here */
351 fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
352 errstr, mode, type, file, line);
353 }
354 }
355
6b691a5c 356int main(int argc, char *argv[])
d02b48c6
RE
357 {
358 char *CApath=NULL,*CAfile=NULL;
359 int badop=0;
95d29597
BM
360 int bio_pair=0;
361 int force=0;
58964a49 362 int tls1=0,ssl2=0,ssl3=0,ret=1;
d02b48c6 363 int client_auth=0;
58964a49 364 int server_auth=0,i;
023ec151 365 int app_verify=0;
d02b48c6 366 char *server_cert=TEST_SERVER_CERT;
65b002f3 367 char *server_key=NULL;
d02b48c6 368 char *client_cert=TEST_CLIENT_CERT;
65b002f3 369 char *client_key=NULL;
ea262260 370 char *named_curve = NULL;
d02b48c6
RE
371 SSL_CTX *s_ctx=NULL;
372 SSL_CTX *c_ctx=NULL;
373 SSL_METHOD *meth=NULL;
58964a49
RE
374 SSL *c_ssl,*s_ssl;
375 int number=1,reuse=0;
376 long bytes=1L;
bc36ee62 377#ifndef OPENSSL_NO_DH
d02b48c6 378 DH *dh;
e4589582 379 int dhe1024 = 0, dhe1024dsa = 0;
ea262260
BM
380#endif
381#ifndef OPENSSL_NO_ECDH
382 EC_KEY *ecdh = NULL;
58964a49 383#endif
e4589582 384 int no_dhe = 0;
ea262260 385 int no_ecdhe = 0;
563f1503
BM
386 int print_time = 0;
387 clock_t s_time = 0, c_time = 0;
23f80f46 388 int comp = 0;
8df788c9 389 COMP_METHOD *cm = NULL;
563f1503 390
79875776
BM
391 verbose = 0;
392 debug = 0;
393 cipher = 0;
1e3a9b65
BM
394
395 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
396
3ac82faa
BM
397 CRYPTO_set_locking_callback(lock_dbg_cb);
398
10654d3a
BM
399 /* enable memory leak checking unless explicitly disabled */
400 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
401 {
402 CRYPTO_malloc_debug_init();
384eff87
BM
403 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
404 }
405 else
406 {
407 /* OPENSSL_DEBUG_MEMORY=off */
408 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
10654d3a 409 }
79875776 410 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
d02b48c6 411
b9d82f47
UM
412 RAND_seed(rnd_seed, sizeof rnd_seed);
413
d02b48c6
RE
414 bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
415
d02b48c6
RE
416 argc--;
417 argv++;
418
419 while (argc >= 1)
420 {
421 if (strcmp(*argv,"-server_auth") == 0)
422 server_auth=1;
423 else if (strcmp(*argv,"-client_auth") == 0)
424 client_auth=1;
425 else if (strcmp(*argv,"-v") == 0)
426 verbose=1;
58964a49
RE
427 else if (strcmp(*argv,"-d") == 0)
428 debug=1;
429 else if (strcmp(*argv,"-reuse") == 0)
430 reuse=1;
48c843c3 431 else if (strcmp(*argv,"-dhe1024") == 0)
90f5a2b6
RL
432 {
433#ifndef OPENSSL_NO_DH
48c843c3 434 dhe1024=1;
90f5a2b6 435#else
ba5ba549 436 fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
90f5a2b6
RL
437#endif
438 }
e4589582 439 else if (strcmp(*argv,"-dhe1024dsa") == 0)
90f5a2b6
RL
440 {
441#ifndef OPENSSL_NO_DH
e4589582 442 dhe1024dsa=1;
90f5a2b6 443#else
ba5ba549 444 fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
e4589582 445#endif
90f5a2b6 446 }
77fa04a9
BM
447 else if (strcmp(*argv,"-no_dhe") == 0)
448 no_dhe=1;
ea262260
BM
449 else if (strcmp(*argv,"-no_ecdhe") == 0)
450 no_ecdhe=1;
d02b48c6
RE
451 else if (strcmp(*argv,"-ssl2") == 0)
452 ssl2=1;
58964a49
RE
453 else if (strcmp(*argv,"-tls1") == 0)
454 tls1=1;
d02b48c6
RE
455 else if (strcmp(*argv,"-ssl3") == 0)
456 ssl3=1;
58964a49
RE
457 else if (strncmp(*argv,"-num",4) == 0)
458 {
459 if (--argc < 1) goto bad;
460 number= atoi(*(++argv));
461 if (number == 0) number=1;
462 }
463 else if (strcmp(*argv,"-bytes") == 0)
464 {
465 if (--argc < 1) goto bad;
466 bytes= atol(*(++argv));
467 if (bytes == 0L) bytes=1L;
468 i=strlen(argv[0]);
469 if (argv[0][i-1] == 'k') bytes*=1024L;
470 if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
471 }
d02b48c6
RE
472 else if (strcmp(*argv,"-cert") == 0)
473 {
474 if (--argc < 1) goto bad;
475 server_cert= *(++argv);
476 }
477 else if (strcmp(*argv,"-s_cert") == 0)
478 {
479 if (--argc < 1) goto bad;
480 server_cert= *(++argv);
481 }
65b002f3
BM
482 else if (strcmp(*argv,"-key") == 0)
483 {
484 if (--argc < 1) goto bad;
485 server_key= *(++argv);
486 }
487 else if (strcmp(*argv,"-s_key") == 0)
488 {
489 if (--argc < 1) goto bad;
490 server_key= *(++argv);
491 }
d02b48c6
RE
492 else if (strcmp(*argv,"-c_cert") == 0)
493 {
494 if (--argc < 1) goto bad;
495 client_cert= *(++argv);
496 }
65b002f3
BM
497 else if (strcmp(*argv,"-c_key") == 0)
498 {
499 if (--argc < 1) goto bad;
500 client_key= *(++argv);
501 }
d02b48c6
RE
502 else if (strcmp(*argv,"-cipher") == 0)
503 {
504 if (--argc < 1) goto bad;
505 cipher= *(++argv);
506 }
507 else if (strcmp(*argv,"-CApath") == 0)
508 {
509 if (--argc < 1) goto bad;
510 CApath= *(++argv);
511 }
512 else if (strcmp(*argv,"-CAfile") == 0)
513 {
514 if (--argc < 1) goto bad;
515 CAfile= *(++argv);
516 }
95d29597
BM
517 else if (strcmp(*argv,"-bio_pair") == 0)
518 {
519 bio_pair = 1;
520 }
521 else if (strcmp(*argv,"-f") == 0)
522 {
523 force = 1;
524 }
563f1503
BM
525 else if (strcmp(*argv,"-time") == 0)
526 {
527 print_time = 1;
528 }
23f80f46
RL
529 else if (strcmp(*argv,"-zlib") == 0)
530 {
531 comp = COMP_ZLIB;
532 }
533 else if (strcmp(*argv,"-rle") == 0)
534 {
535 comp = COMP_RLE;
536 }
ea262260
BM
537 else if (strcmp(*argv,"-named_curve") == 0)
538 {
539 if (--argc < 1) goto bad;
90f5a2b6 540#ifndef OPENSSL_NO_ECDH
ea262260 541 named_curve = *(++argv);
90f5a2b6 542#else
5e3247d8 543 fprintf(stderr,"ignoring -named_curve, since I'm compiled without ECDH\n");
90f5a2b6 544 ++argv;
ea262260 545#endif
90f5a2b6 546 }
023ec151
BM
547 else if (strcmp(*argv,"-app_verify") == 0)
548 {
549 app_verify = 1;
550 }
d02b48c6
RE
551 else
552 {
553 fprintf(stderr,"unknown option %s\n",*argv);
554 badop=1;
555 break;
556 }
557 argc--;
558 argv++;
559 }
560 if (badop)
561 {
562bad:
563 sv_usage();
564 goto end;
565 }
566
95d29597
BM
567 if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
568 {
563f1503
BM
569 fprintf(stderr, "This case cannot work. Use -f to perform "
570 "the test anyway (and\n-d to see what happens), "
571 "or add one of -ssl2, -ssl3, -tls1, -reuse\n"
572 "to avoid protocol mismatch.\n");
55f78baf 573 EXIT(1);
95d29597
BM
574 }
575
563f1503
BM
576 if (print_time)
577 {
578 if (!bio_pair)
579 {
580 fprintf(stderr, "Using BIO pair (-bio_pair)\n");
581 bio_pair = 1;
582 }
583 if (number < 50 && !force)
584 fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
585 }
586
d02b48c6
RE
587/* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
588
413c4f45 589 SSL_library_init();
d02b48c6
RE
590 SSL_load_error_strings();
591
bd68b6b1
RL
592 if (comp == COMP_ZLIB) cm = COMP_zlib();
593 if (comp == COMP_RLE) cm = COMP_rle();
594 if (cm != NULL)
23f80f46 595 {
23f80f46 596 if (cm->type != NID_undef)
f82ab534
RL
597 {
598 if (SSL_COMP_add_compression_method(comp, cm) != 0)
599 {
600 fprintf(stderr,
601 "Failed to add compression method\n");
602 ERR_print_errors_fp(stderr);
603 }
604 }
23f80f46 605 else
4751717c 606 {
bd68b6b1
RL
607 fprintf(stderr,
608 "Warning: %s compression not supported\n",
609 (comp == COMP_RLE ? "rle" :
610 (comp == COMP_ZLIB ? "zlib" :
611 "unknown")));
4751717c
RL
612 ERR_print_errors_fp(stderr);
613 }
23f80f46
RL
614 }
615
bc36ee62 616#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
d02b48c6
RE
617 if (ssl2)
618 meth=SSLv2_method();
619 else
58964a49
RE
620 if (tls1)
621 meth=TLSv1_method();
622 else
d02b48c6
RE
623 if (ssl3)
624 meth=SSLv3_method();
625 else
626 meth=SSLv23_method();
627#else
bc36ee62 628#ifdef OPENSSL_NO_SSL2
d02b48c6
RE
629 meth=SSLv3_method();
630#else
631 meth=SSLv2_method();
632#endif
633#endif
634
635 c_ctx=SSL_CTX_new(meth);
636 s_ctx=SSL_CTX_new(meth);
637 if ((c_ctx == NULL) || (s_ctx == NULL))
638 {
639 ERR_print_errors(bio_err);
640 goto end;
641 }
642
643 if (cipher != NULL)
644 {
645 SSL_CTX_set_cipher_list(c_ctx,cipher);
646 SSL_CTX_set_cipher_list(s_ctx,cipher);
647 }
648
bc36ee62 649#ifndef OPENSSL_NO_DH
77fa04a9 650 if (!no_dhe)
48c843c3 651 {
e4589582 652 if (dhe1024dsa)
48c843c3 653 {
e4589582 654 /* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
77fa04a9 655 SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
e4589582 656 dh=get_dh1024dsa();
48c843c3 657 }
e4589582
BM
658 else if (dhe1024)
659 dh=get_dh1024();
77fa04a9 660 else
77fa04a9
BM
661 dh=get_dh512();
662 SSL_CTX_set_tmp_dh(s_ctx,dh);
663 DH_free(dh);
664 }
e4589582
BM
665#else
666 (void)no_dhe;
58964a49
RE
667#endif
668
ea262260
BM
669#ifndef OPENSSL_NO_ECDH
670 if (!no_ecdhe)
671 {
672 ecdh = EC_KEY_new();
673 if (ecdh != NULL)
674 {
675 if (named_curve)
676 {
677 int nid = OBJ_sn2nid(named_curve);
678
679 if (nid == 0)
680 {
681 BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);
682 EC_KEY_free(ecdh);
683 goto end;
684 }
685
686 ecdh->group = EC_GROUP_new_by_nid(nid);
687 if (ecdh->group == NULL)
688 {
689 BIO_printf(bio_err, "unable to create curve (%s)\n", named_curve);
690 EC_KEY_free(ecdh);
691 goto end;
692 }
693 }
694
695 if (ecdh->group == NULL)
696 ecdh->group=EC_GROUP_new_by_nid(NID_sect163r2);
697
698 SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);
699 SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);
700 EC_KEY_free(ecdh);
701 }
702 }
703#else
704 (void)no_ecdhe;
705#endif
706
bc36ee62 707#ifndef OPENSSL_NO_RSA
58964a49 708 SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
d02b48c6
RE
709#endif
710
711 if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
712 {
713 ERR_print_errors(bio_err);
714 }
65b002f3
BM
715 else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
716 (server_key?server_key:server_cert), SSL_FILETYPE_PEM))
d02b48c6
RE
717 {
718 ERR_print_errors(bio_err);
719 goto end;
720 }
721
722 if (client_auth)
723 {
724 SSL_CTX_use_certificate_file(c_ctx,client_cert,
725 SSL_FILETYPE_PEM);
65b002f3
BM
726 SSL_CTX_use_PrivateKey_file(c_ctx,
727 (client_key?client_key:client_cert),
d02b48c6
RE
728 SSL_FILETYPE_PEM);
729 }
730
731 if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
732 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
733 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
734 (!SSL_CTX_set_default_verify_paths(c_ctx)))
735 {
58964a49 736 /* fprintf(stderr,"SSL_load_verify_locations\n"); */
d02b48c6 737 ERR_print_errors(bio_err);
58964a49 738 /* goto end; */
d02b48c6
RE
739 }
740
741 if (client_auth)
742 {
53002dc6 743 BIO_printf(bio_err,"client authentication\n");
d02b48c6
RE
744 SSL_CTX_set_verify(s_ctx,
745 SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
746 verify_callback);
023ec151
BM
747 if (app_verify)
748 {
749 SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg);
750 }
d02b48c6
RE
751 }
752 if (server_auth)
753 {
53002dc6 754 BIO_printf(bio_err,"server authentication\n");
d02b48c6
RE
755 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
756 verify_callback);
023ec151
BM
757 if (app_verify)
758 {
759 SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg);
760 }
d02b48c6 761 }
b1fe6ca1
BM
762
763 {
764 int session_id_context = 0;
765 SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);
766 }
d02b48c6 767
58964a49
RE
768 c_ssl=SSL_new(c_ctx);
769 s_ssl=SSL_new(s_ctx);
770
bc36ee62 771#ifndef OPENSSL_NO_KRB5
f9b3bff6
RL
772 if (c_ssl && c_ssl->kssl_ctx)
773 {
54a656ef 774 char localhost[MAXHOSTNAMELEN+2];
f9b3bff6 775
54a656ef 776 if (gethostname(localhost, sizeof localhost-1) == 0)
f9b3bff6 777 {
54a656ef
BL
778 localhost[sizeof localhost-1]='\0';
779 if(strlen(localhost) == sizeof localhost-1)
780 {
781 BIO_printf(bio_err,"localhost name too long\n");
31be2daa 782 goto end;
54a656ef 783 }
f9b3bff6
RL
784 kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,
785 localhost);
786 }
787 }
bc36ee62 788#endif /* OPENSSL_NO_KRB5 */
f9b3bff6 789
58964a49
RE
790 for (i=0; i<number; i++)
791 {
792 if (!reuse) SSL_set_session(c_ssl,NULL);
95d29597 793 if (bio_pair)
563f1503 794 ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);
95d29597
BM
795 else
796 ret=doit(s_ssl,c_ssl,bytes);
58964a49
RE
797 }
798
799 if (!verbose)
800 {
563f1503 801 print_details(c_ssl, "");
58964a49
RE
802 }
803 if ((number > 1) || (bytes > 1L))
53002dc6 804 BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);
563f1503
BM
805 if (print_time)
806 {
617d71bc
BM
807#ifdef CLOCKS_PER_SEC
808 /* "To determine the time in seconds, the value returned
809 * by the clock function should be divided by the value
810 * of the macro CLOCKS_PER_SEC."
811 * -- ISO/IEC 9899 */
563f1503
BM
812 BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
813 "Approximate total client time: %6.2f s\n",
814 (double)s_time/CLOCKS_PER_SEC,
815 (double)c_time/CLOCKS_PER_SEC);
617d71bc
BM
816#else
817 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
818 * -- cc on NeXTstep/OpenStep */
819 BIO_printf(bio_stdout,
820 "Approximate total server time: %6.2f units\n"
821 "Approximate total client time: %6.2f units\n",
822 (double)s_time,
823 (double)c_time);
824#endif
563f1503 825 }
58964a49
RE
826
827 SSL_free(s_ssl);
828 SSL_free(c_ssl);
829
d02b48c6
RE
830end:
831 if (s_ctx != NULL) SSL_CTX_free(s_ctx);
832 if (c_ctx != NULL) SSL_CTX_free(c_ctx);
833
834 if (bio_stdout != NULL) BIO_free(bio_stdout);
835
bc36ee62 836#ifndef OPENSSL_NO_RSA
46b3bd54
BM
837 free_tmp_rsa();
838#endif
0b13e9f0 839#ifndef OPENSSL_NO_ENGINE
b8e2f83a 840 ENGINE_cleanup();
0b13e9f0 841#endif
79aa04ef 842 CRYPTO_cleanup_all_ex_data();
dfeab068 843 ERR_free_strings();
d02b48c6
RE
844 ERR_remove_state(0);
845 EVP_cleanup();
846 CRYPTO_mem_leaks(bio_err);
79875776 847 if (bio_err != NULL) BIO_free(bio_err);
d02b48c6
RE
848 EXIT(ret);
849 }
850
563f1503
BM
851int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
852 clock_t *s_time, clock_t *c_time)
95d29597
BM
853 {
854 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
855 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
856 BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
95d29597
BM
857 int ret = 1;
858
859 size_t bufsiz = 256; /* small buffer for testing */
860
861 if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
862 goto err;
863 if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
864 goto err;
865
866 s_ssl_bio = BIO_new(BIO_f_ssl());
867 if (!s_ssl_bio)
868 goto err;
869
870 c_ssl_bio = BIO_new(BIO_f_ssl());
871 if (!c_ssl_bio)
872 goto err;
873
874 SSL_set_connect_state(c_ssl);
875 SSL_set_bio(c_ssl, client, client);
876 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
877
878 SSL_set_accept_state(s_ssl);
879 SSL_set_bio(s_ssl, server, server);
880 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
881
882 do
883 {
884 /* c_ssl_bio: SSL filter BIO
885 *
886 * client: pseudo-I/O for SSL library
887 *
888 * client_io: client's SSL communication; usually to be
889 * relayed over some I/O facility, but in this
890 * test program, we're the server, too:
891 *
892 * server_io: server's SSL communication
893 *
894 * server: pseudo-I/O for SSL library
895 *
896 * s_ssl_bio: SSL filter BIO
897 *
898 * The client and the server each employ a "BIO pair":
899 * client + client_io, server + server_io.
900 * BIO pairs are symmetric. A BIO pair behaves similar
901 * to a non-blocking socketpair (but both endpoints must
902 * be handled by the same thread).
7eea36bb
BM
903 * [Here we could connect client and server to the ends
904 * of a single BIO pair, but then this code would be less
905 * suitable as an example for BIO pairs in general.]
95d29597
BM
906 *
907 * Useful functions for querying the state of BIO pair endpoints:
908 *
909 * BIO_ctrl_pending(bio) number of bytes we can read now
f50c0497 910 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil
95d29597 911 * other side's read attempt
657e60fa 912 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
95d29597
BM
913 *
914 * ..._read_request is never more than ..._write_guarantee;
915 * it depends on the application which one you should use.
916 */
917
918 /* We have non-blocking behaviour throughout this test program, but
919 * can be sure that there is *some* progress in each iteration; so
920 * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
921 * -- we just try everything in each iteration
922 */
923
924 {
925 /* CLIENT */
926
927 MS_STATIC char cbuf[1024*8];
928 int i, r;
563f1503 929 clock_t c_clock = clock();
95d29597 930
896e4fef
BM
931 memset(cbuf, 0, sizeof(cbuf));
932
95d29597
BM
933 if (debug)
934 if (SSL_in_init(c_ssl))
935 printf("client waiting in SSL_connect - %s\n",
936 SSL_state_string_long(c_ssl));
937
938 if (cw_num > 0)
939 {
940 /* Write to server. */
941
942 if (cw_num > (long)sizeof cbuf)
943 i = sizeof cbuf;
944 else
945 i = (int)cw_num;
946 r = BIO_write(c_ssl_bio, cbuf, i);
29159a42 947 if (r < 0)
95d29597
BM
948 {
949 if (!BIO_should_retry(c_ssl_bio))
950 {
951 fprintf(stderr,"ERROR in CLIENT\n");
952 goto err;
953 }
954 /* BIO_should_retry(...) can just be ignored here.
955 * The library expects us to call BIO_write with
956 * the same arguments again, and that's what we will
957 * do in the next iteration. */
958 }
959 else if (r == 0)
960 {
961 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
962 goto err;
963 }
964 else
965 {
966 if (debug)
967 printf("client wrote %d\n", r);
968 cw_num -= r;
969 }
970 }
971
972 if (cr_num > 0)
973 {
974 /* Read from server. */
975
976 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
977 if (r < 0)
978 {
979 if (!BIO_should_retry(c_ssl_bio))
980 {
981 fprintf(stderr,"ERROR in CLIENT\n");
982 goto err;
983 }
984 /* Again, "BIO_should_retry" can be ignored. */
985 }
986 else if (r == 0)
987 {
988 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
989 goto err;
990 }
991 else
992 {
993 if (debug)
994 printf("client read %d\n", r);
995 cr_num -= r;
996 }
997 }
563f1503
BM
998
999 /* c_time and s_time increments will typically be very small
1000 * (depending on machine speed and clock tick intervals),
1001 * but sampling over a large number of connections should
1002 * result in fairly accurate figures. We cannot guarantee
1003 * a lot, however -- if each connection lasts for exactly
1004 * one clock tick, it will be counted only for the client
1005 * or only for the server or even not at all.
1006 */
1007 *c_time += (clock() - c_clock);
95d29597
BM
1008 }
1009
1010 {
1011 /* SERVER */
1012
1013 MS_STATIC char sbuf[1024*8];
1014 int i, r;
563f1503 1015 clock_t s_clock = clock();
95d29597 1016
896e4fef
BM
1017 memset(sbuf, 0, sizeof(sbuf));
1018
95d29597
BM
1019 if (debug)
1020 if (SSL_in_init(s_ssl))
1021 printf("server waiting in SSL_accept - %s\n",
1022 SSL_state_string_long(s_ssl));
1023
1024 if (sw_num > 0)
1025 {
1026 /* Write to client. */
1027
1028 if (sw_num > (long)sizeof sbuf)
1029 i = sizeof sbuf;
1030 else
1031 i = (int)sw_num;
1032 r = BIO_write(s_ssl_bio, sbuf, i);
29159a42 1033 if (r < 0)
95d29597
BM
1034 {
1035 if (!BIO_should_retry(s_ssl_bio))
1036 {
1037 fprintf(stderr,"ERROR in SERVER\n");
1038 goto err;
1039 }
1040 /* Ignore "BIO_should_retry". */
1041 }
1042 else if (r == 0)
1043 {
1044 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
1045 goto err;
1046 }
1047 else
1048 {
1049 if (debug)
1050 printf("server wrote %d\n", r);
1051 sw_num -= r;
1052 }
1053 }
1054
1055 if (sr_num > 0)
1056 {
1057 /* Read from client. */
1058
1059 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
1060 if (r < 0)
1061 {
1062 if (!BIO_should_retry(s_ssl_bio))
1063 {
1064 fprintf(stderr,"ERROR in SERVER\n");
1065 goto err;
1066 }
1067 /* blah, blah */
1068 }
1069 else if (r == 0)
1070 {
1071 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
1072 goto err;
1073 }
1074 else
1075 {
1076 if (debug)
1077 printf("server read %d\n", r);
1078 sr_num -= r;
1079 }
1080 }
563f1503
BM
1081
1082 *s_time += (clock() - s_clock);
95d29597
BM
1083 }
1084
1085 {
1086 /* "I/O" BETWEEN CLIENT AND SERVER. */
1087
95d29597 1088 size_t r1, r2;
6f7af152
BM
1089 BIO *io1 = server_io, *io2 = client_io;
1090 /* we use the non-copying interface for io1
1091 * and the standard BIO_write/BIO_read interface for io2
1092 */
1093
95d29597
BM
1094 static int prev_progress = 1;
1095 int progress = 0;
1096
6f7af152 1097 /* io1 to io2 */
95d29597
BM
1098 do
1099 {
6f7af152 1100 size_t num;
b52f3818 1101 int r;
6f7af152
BM
1102
1103 r1 = BIO_ctrl_pending(io1);
1104 r2 = BIO_ctrl_get_write_guarantee(io2);
95d29597
BM
1105
1106 num = r1;
1107 if (r2 < num)
1108 num = r2;
1109 if (num)
1110 {
6f7af152
BM
1111 char *dataptr;
1112
95d29597
BM
1113 if (INT_MAX < num) /* yeah, right */
1114 num = INT_MAX;
1115
6f7af152
BM
1116 r = BIO_nread(io1, &dataptr, (int)num);
1117 assert(r > 0);
1118 assert(r <= (int)num);
1119 /* possibly r < num (non-contiguous data) */
1120 num = r;
1121 r = BIO_write(io2, dataptr, (int)num);
95d29597
BM
1122 if (r != (int)num) /* can't happen */
1123 {
1124 fprintf(stderr, "ERROR: BIO_write could not write "
1125 "BIO_ctrl_get_write_guarantee() bytes");
1126 goto err;
1127 }
1128 progress = 1;
1129
1130 if (debug)
6f7af152
BM
1131 printf((io1 == client_io) ?
1132 "C->S relaying: %d bytes\n" :
1133 "S->C relaying: %d bytes\n",
1134 (int)num);
95d29597
BM
1135 }
1136 }
1137 while (r1 && r2);
1138
6f7af152
BM
1139 /* io2 to io1 */
1140 {
1141 size_t num;
1142 int r;
1143
1144 r1 = BIO_ctrl_pending(io2);
1145 r2 = BIO_ctrl_get_read_request(io1);
1146 /* here we could use ..._get_write_guarantee instead of
1147 * ..._get_read_request, but by using the latter
1148 * we test restartability of the SSL implementation
1149 * more thoroughly */
95d29597
BM
1150 num = r1;
1151 if (r2 < num)
1152 num = r2;
1153 if (num)
1154 {
6f7af152
BM
1155 char *dataptr;
1156
95d29597
BM
1157 if (INT_MAX < num)
1158 num = INT_MAX;
6f7af152
BM
1159
1160 if (num > 1)
cb0369d8 1161 --num; /* test restartability even more thoroughly */
95d29597 1162
234c7376 1163 r = BIO_nwrite0(io1, &dataptr);
6f7af152 1164 assert(r > 0);
cc129755 1165 if (r < (int)num)
234c7376 1166 num = r;
6f7af152 1167 r = BIO_read(io2, dataptr, (int)num);
95d29597
BM
1168 if (r != (int)num) /* can't happen */
1169 {
1170 fprintf(stderr, "ERROR: BIO_read could not read "
1171 "BIO_ctrl_pending() bytes");
1172 goto err;
1173 }
95d29597 1174 progress = 1;
234c7376
BM
1175 r = BIO_nwrite(io1, &dataptr, (int)num);
1176 if (r != (int)num) /* can't happen */
1177 {
1178 fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
1179 "BIO_nwrite0() bytes");
1180 goto err;
1181 }
6f7af152 1182
95d29597 1183 if (debug)
6f7af152
BM
1184 printf((io2 == client_io) ?
1185 "C->S relaying: %d bytes\n" :
1186 "S->C relaying: %d bytes\n",
1187 (int)num);
95d29597 1188 }
6f7af152 1189 } /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
95d29597
BM
1190
1191 if (!progress && !prev_progress)
1192 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
95d29597
BM
1193 {
1194 fprintf(stderr, "ERROR: got stuck\n");
d7fcc7f6
BM
1195 if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
1196 {
1197 fprintf(stderr, "This can happen for SSL2 because "
1198 "CLIENT-FINISHED and SERVER-VERIFY are written \n"
1199 "concurrently ...");
1200 if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
1201 && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
1202 {
1203 fprintf(stderr, " ok.\n");
1204 goto end;
1205 }
1206 }
1207 fprintf(stderr, " ERROR.\n");
95d29597
BM
1208 goto err;
1209 }
1210 prev_progress = progress;
1211 }
1212 }
1213 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
1214
95d29597 1215 if (verbose)
563f1503
BM
1216 print_details(c_ssl, "DONE via BIO pair: ");
1217end:
95d29597
BM
1218 ret = 0;
1219
1220 err:
1221 ERR_print_errors(bio_err);
1222
1223 if (server)
1224 BIO_free(server);
1225 if (server_io)
1226 BIO_free(server_io);
1227 if (client)
1228 BIO_free(client);
1229 if (client_io)
1230 BIO_free(client_io);
1231 if (s_ssl_bio)
1232 BIO_free(s_ssl_bio);
1233 if (c_ssl_bio)
1234 BIO_free(c_ssl_bio);
1235
1236 return ret;
1237 }
1238
1239
d02b48c6
RE
1240#define W_READ 1
1241#define W_WRITE 2
1242#define C_DONE 1
1243#define S_DONE 2
1244
6b691a5c 1245int doit(SSL *s_ssl, SSL *c_ssl, long count)
d02b48c6 1246 {
58964a49
RE
1247 MS_STATIC char cbuf[1024*8],sbuf[1024*8];
1248 long cw_num=count,cr_num=count;
1249 long sw_num=count,sr_num=count;
d02b48c6 1250 int ret=1;
d02b48c6
RE
1251 BIO *c_to_s=NULL;
1252 BIO *s_to_c=NULL;
1253 BIO *c_bio=NULL;
1254 BIO *s_bio=NULL;
1255 int c_r,c_w,s_r,s_w;
1256 int c_want,s_want;
58964a49 1257 int i,j;
d02b48c6
RE
1258 int done=0;
1259 int c_write,s_write;
1260 int do_server=0,do_client=0;
d02b48c6 1261
896e4fef
BM
1262 memset(cbuf,0,sizeof(cbuf));
1263 memset(sbuf,0,sizeof(sbuf));
1264
d02b48c6
RE
1265 c_to_s=BIO_new(BIO_s_mem());
1266 s_to_c=BIO_new(BIO_s_mem());
1267 if ((s_to_c == NULL) || (c_to_s == NULL))
1268 {
1269 ERR_print_errors(bio_err);
1270 goto err;
1271 }
1272
1273 c_bio=BIO_new(BIO_f_ssl());
1274 s_bio=BIO_new(BIO_f_ssl());
1275 if ((c_bio == NULL) || (s_bio == NULL))
1276 {
1277 ERR_print_errors(bio_err);
1278 goto err;
1279 }
1280
1281 SSL_set_connect_state(c_ssl);
1282 SSL_set_bio(c_ssl,s_to_c,c_to_s);
58964a49 1283 BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
d02b48c6
RE
1284
1285 SSL_set_accept_state(s_ssl);
1286 SSL_set_bio(s_ssl,c_to_s,s_to_c);
58964a49 1287 BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
d02b48c6
RE
1288
1289 c_r=0; s_r=1;
1290 c_w=1; s_w=0;
1291 c_want=W_WRITE;
1292 s_want=0;
1293 c_write=1,s_write=0;
1294
1295 /* We can always do writes */
1296 for (;;)
1297 {
1298 do_server=0;
1299 do_client=0;
1300
1301 i=(int)BIO_pending(s_bio);
1302 if ((i && s_r) || s_w) do_server=1;
1303
1304 i=(int)BIO_pending(c_bio);
1305 if ((i && c_r) || c_w) do_client=1;
1306
58964a49 1307 if (do_server && debug)
d02b48c6
RE
1308 {
1309 if (SSL_in_init(s_ssl))
1310 printf("server waiting in SSL_accept - %s\n",
1311 SSL_state_string_long(s_ssl));
58964a49 1312/* else if (s_write)
d02b48c6 1313 printf("server:SSL_write()\n");
58964a49
RE
1314 else
1315 printf("server:SSL_read()\n"); */
d02b48c6
RE
1316 }
1317
58964a49 1318 if (do_client && debug)
d02b48c6
RE
1319 {
1320 if (SSL_in_init(c_ssl))
1321 printf("client waiting in SSL_connect - %s\n",
1322 SSL_state_string_long(c_ssl));
58964a49 1323/* else if (c_write)
d02b48c6
RE
1324 printf("client:SSL_write()\n");
1325 else
58964a49 1326 printf("client:SSL_read()\n"); */
d02b48c6
RE
1327 }
1328
1329 if (!do_client && !do_server)
1330 {
1331 fprintf(stdout,"ERROR IN STARTUP\n");
1332 ERR_print_errors(bio_err);
1333 break;
1334 }
1335 if (do_client && !(done & C_DONE))
1336 {
1337 if (c_write)
1338 {
58964a49
RE
1339 j=(cw_num > (long)sizeof(cbuf))
1340 ?sizeof(cbuf):(int)cw_num;
1341 i=BIO_write(c_bio,cbuf,j);
d02b48c6
RE
1342 if (i < 0)
1343 {
1344 c_r=0;
1345 c_w=0;
1346 if (BIO_should_retry(c_bio))
1347 {
1348 if (BIO_should_read(c_bio))
1349 c_r=1;
1350 if (BIO_should_write(c_bio))
1351 c_w=1;
1352 }
1353 else
1354 {
1355 fprintf(stderr,"ERROR in CLIENT\n");
1356 ERR_print_errors(bio_err);
1357 goto err;
1358 }
1359 }
1360 else if (i == 0)
1361 {
1362 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
1363 goto err;
1364 }
1365 else
1366 {
58964a49
RE
1367 if (debug)
1368 printf("client wrote %d\n",i);
d02b48c6 1369 /* ok */
58964a49 1370 s_r=1;
d02b48c6 1371 c_write=0;
58964a49 1372 cw_num-=i;
d02b48c6
RE
1373 }
1374 }
1375 else
1376 {
58964a49 1377 i=BIO_read(c_bio,cbuf,sizeof(cbuf));
d02b48c6
RE
1378 if (i < 0)
1379 {
1380 c_r=0;
1381 c_w=0;
1382 if (BIO_should_retry(c_bio))
1383 {
1384 if (BIO_should_read(c_bio))
1385 c_r=1;
1386 if (BIO_should_write(c_bio))
1387 c_w=1;
1388 }
1389 else
1390 {
1391 fprintf(stderr,"ERROR in CLIENT\n");
1392 ERR_print_errors(bio_err);
1393 goto err;
1394 }
1395 }
1396 else if (i == 0)
1397 {
1398 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
1399 goto err;
1400 }
1401 else
1402 {
58964a49
RE
1403 if (debug)
1404 printf("client read %d\n",i);
1405 cr_num-=i;
1406 if (sw_num > 0)
1407 {
1408 s_write=1;
1409 s_w=1;
1410 }
1411 if (cr_num <= 0)
1412 {
1413 s_write=1;
1414 s_w=1;
1415 done=S_DONE|C_DONE;
1416 }
d02b48c6
RE
1417 }
1418 }
1419 }
1420
1421 if (do_server && !(done & S_DONE))
1422 {
1423 if (!s_write)
1424 {
58964a49 1425 i=BIO_read(s_bio,sbuf,sizeof(cbuf));
d02b48c6
RE
1426 if (i < 0)
1427 {
1428 s_r=0;
1429 s_w=0;
1430 if (BIO_should_retry(s_bio))
1431 {
1432 if (BIO_should_read(s_bio))
1433 s_r=1;
1434 if (BIO_should_write(s_bio))
1435 s_w=1;
1436 }
1437 else
1438 {
1439 fprintf(stderr,"ERROR in SERVER\n");
1440 ERR_print_errors(bio_err);
1441 goto err;
1442 }
1443 }
1444 else if (i == 0)
1445 {
1446 ERR_print_errors(bio_err);
1447 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
1448 goto err;
1449 }
1450 else
1451 {
58964a49
RE
1452 if (debug)
1453 printf("server read %d\n",i);
1454 sr_num-=i;
1455 if (cw_num > 0)
1456 {
1457 c_write=1;
1458 c_w=1;
1459 }
1460 if (sr_num <= 0)
1461 {
1462 s_write=1;
1463 s_w=1;
1464 c_write=0;
1465 }
d02b48c6
RE
1466 }
1467 }
1468 else
1469 {
58964a49
RE
1470 j=(sw_num > (long)sizeof(sbuf))?
1471 sizeof(sbuf):(int)sw_num;
1472 i=BIO_write(s_bio,sbuf,j);
d02b48c6
RE
1473 if (i < 0)
1474 {
1475 s_r=0;
1476 s_w=0;
1477 if (BIO_should_retry(s_bio))
1478 {
1479 if (BIO_should_read(s_bio))
1480 s_r=1;
1481 if (BIO_should_write(s_bio))
1482 s_w=1;
1483 }
1484 else
1485 {
1486 fprintf(stderr,"ERROR in SERVER\n");
1487 ERR_print_errors(bio_err);
1488 goto err;
1489 }
1490 }
1491 else if (i == 0)
1492 {
1493 ERR_print_errors(bio_err);
1494 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
1495 goto err;
1496 }
1497 else
1498 {
58964a49
RE
1499 if (debug)
1500 printf("server wrote %d\n",i);
1501 sw_num-=i;
d02b48c6 1502 s_write=0;
58964a49
RE
1503 c_r=1;
1504 if (sw_num <= 0)
1505 done|=S_DONE;
d02b48c6
RE
1506 }
1507 }
1508 }
1509
1510 if ((done & S_DONE) && (done & C_DONE)) break;
1511 }
1512
58964a49 1513 if (verbose)
563f1503 1514 print_details(c_ssl, "DONE: ");
d02b48c6
RE
1515 ret=0;
1516err:
1517 /* We have to set the BIO's to NULL otherwise they will be
26a3a48d 1518 * OPENSSL_free()ed twice. Once when th s_ssl is SSL_free()ed and
d02b48c6
RE
1519 * again when c_ssl is SSL_free()ed.
1520 * This is a hack required because s_ssl and c_ssl are sharing the same
1521 * BIO structure and SSL_set_bio() and SSL_free() automatically
1522 * BIO_free non NULL entries.
1523 * You should not normally do this or be required to do this */
1524 if (s_ssl != NULL)
1525 {
1526 s_ssl->rbio=NULL;
1527 s_ssl->wbio=NULL;
1528 }
1529 if (c_ssl != NULL)
1530 {
1531 c_ssl->rbio=NULL;
1532 c_ssl->wbio=NULL;
1533 }
1534
1535 if (c_to_s != NULL) BIO_free(c_to_s);
1536 if (s_to_c != NULL) BIO_free(s_to_c);
58964a49
RE
1537 if (c_bio != NULL) BIO_free_all(c_bio);
1538 if (s_bio != NULL) BIO_free_all(s_bio);
d02b48c6
RE
1539 return(ret);
1540 }
1541
396f6314 1542static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
d02b48c6
RE
1543 {
1544 char *s,buf[256];
1545
54a656ef
BL
1546 s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,
1547 sizeof buf);
d02b48c6
RE
1548 if (s != NULL)
1549 {
1550 if (ok)
1551 fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
1552 else
1553 fprintf(stderr,"depth=%d error=%d %s\n",
1554 ctx->error_depth,ctx->error,buf);
1555 }
1556
1557 if (ok == 0)
1558 {
1559 switch (ctx->error)
1560 {
1561 case X509_V_ERR_CERT_NOT_YET_VALID:
1562 case X509_V_ERR_CERT_HAS_EXPIRED:
1563 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1564 ok=1;
1565 }
1566 }
1567
1568 return(ok);
1569 }
1570
023ec151
BM
1571static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg)
1572 {
1573 char *s = NULL,buf[256];
1574 int ok=1;
1575
1576 fprintf(stderr, "In app_verify_callback, allowing cert. ");
1577 fprintf(stderr, "Arg is: %s\n", (char *)arg);
1578 fprintf(stderr, "Finished printing do we have a context? 0x%x a cert? 0x%x\n",
1579 (unsigned int)ctx, (unsigned int)ctx->cert);
1580 if (ctx->cert)
1581 s=X509_NAME_oneline(X509_get_subject_name(ctx->cert),buf,256);
1582 if (s != NULL)
1583 {
1584 fprintf(stderr,"cert depth=%d %s\n",ctx->error_depth,buf);
1585 }
1586
1587 return(ok);
1588 }
1589
bc36ee62 1590#ifndef OPENSSL_NO_RSA
46b3bd54
BM
1591static RSA *rsa_tmp=NULL;
1592
df63a389 1593static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
58964a49 1594 {
58964a49
RE
1595 if (rsa_tmp == NULL)
1596 {
e9224c71
GT
1597 rsa_tmp = RSA_new();
1598 if(!rsa_tmp)
1599 {
1600 BIO_printf(bio_err, "Memory error...");
1601 goto end;
1602 }
60e31c3a 1603 BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
d58d092b 1604 (void)BIO_flush(bio_err);
e9224c71
GT
1605 if(!RSA_generate_key_ex(rsa_tmp,keylength,RSA_F4,NULL))
1606 {
3ae70939 1607 BIO_printf(bio_err, "Error generating key.");
e9224c71
GT
1608 RSA_free(rsa_tmp);
1609 rsa_tmp = NULL;
1610 }
1611end:
58964a49 1612 BIO_printf(bio_err,"\n");
d58d092b 1613 (void)BIO_flush(bio_err);
58964a49
RE
1614 }
1615 return(rsa_tmp);
1616 }
46b3bd54
BM
1617
1618static void free_tmp_rsa(void)
1619 {
1620 if (rsa_tmp != NULL)
1621 {
1622 RSA_free(rsa_tmp);
1623 rsa_tmp = NULL;
1624 }
1625 }
79df9d62 1626#endif
53002dc6 1627
bc36ee62 1628#ifndef OPENSSL_NO_DH
e4589582
BM
1629/* These DH parameters have been generated as follows:
1630 * $ openssl dhparam -C -noout 512
1631 * $ openssl dhparam -C -noout 1024
1632 * $ openssl dhparam -C -noout -dsaparam 1024
1633 * (The third function has been renamed to avoid name conflicts.)
1634 */
f3f316f1 1635static DH *get_dh512()
e4589582
BM
1636 {
1637 static unsigned char dh512_p[]={
1638 0xCB,0xC8,0xE1,0x86,0xD0,0x1F,0x94,0x17,0xA6,0x99,0xF0,0xC6,
1639 0x1F,0x0D,0xAC,0xB6,0x25,0x3E,0x06,0x39,0xCA,0x72,0x04,0xB0,
1640 0x6E,0xDA,0xC0,0x61,0xE6,0x7A,0x77,0x25,0xE8,0x3B,0xB9,0x5F,
1641 0x9A,0xB6,0xB5,0xFE,0x99,0x0B,0xA1,0x93,0x4E,0x35,0x33,0xB8,
1642 0xE1,0xF1,0x13,0x4F,0x59,0x1A,0xD2,0x57,0xC0,0x26,0x21,0x33,
1643 0x02,0xC5,0xAE,0x23,
1644 };
1645 static unsigned char dh512_g[]={
1646 0x02,
1647 };
1648 DH *dh;
1649
1650 if ((dh=DH_new()) == NULL) return(NULL);
1651 dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
1652 dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
1653 if ((dh->p == NULL) || (dh->g == NULL))
1654 { DH_free(dh); return(NULL); }
1655 return(dh);
1656 }
1657
f3f316f1 1658static DH *get_dh1024()
53002dc6 1659 {
e4589582
BM
1660 static unsigned char dh1024_p[]={
1661 0xF8,0x81,0x89,0x7D,0x14,0x24,0xC5,0xD1,0xE6,0xF7,0xBF,0x3A,
1662 0xE4,0x90,0xF4,0xFC,0x73,0xFB,0x34,0xB5,0xFA,0x4C,0x56,0xA2,
1663 0xEA,0xA7,0xE9,0xC0,0xC0,0xCE,0x89,0xE1,0xFA,0x63,0x3F,0xB0,
1664 0x6B,0x32,0x66,0xF1,0xD1,0x7B,0xB0,0x00,0x8F,0xCA,0x87,0xC2,
1665 0xAE,0x98,0x89,0x26,0x17,0xC2,0x05,0xD2,0xEC,0x08,0xD0,0x8C,
1666 0xFF,0x17,0x52,0x8C,0xC5,0x07,0x93,0x03,0xB1,0xF6,0x2F,0xB8,
1667 0x1C,0x52,0x47,0x27,0x1B,0xDB,0xD1,0x8D,0x9D,0x69,0x1D,0x52,
1668 0x4B,0x32,0x81,0xAA,0x7F,0x00,0xC8,0xDC,0xE6,0xD9,0xCC,0xC1,
1669 0x11,0x2D,0x37,0x34,0x6C,0xEA,0x02,0x97,0x4B,0x0E,0xBB,0xB1,
1670 0x71,0x33,0x09,0x15,0xFD,0xDD,0x23,0x87,0x07,0x5E,0x89,0xAB,
1671 0x6B,0x7C,0x5F,0xEC,0xA6,0x24,0xDC,0x53,
1672 };
1673 static unsigned char dh1024_g[]={
1674 0x02,
1675 };
1676 DH *dh;
1677
1678 if ((dh=DH_new()) == NULL) return(NULL);
1679 dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
1680 dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
1681 if ((dh->p == NULL) || (dh->g == NULL))
1682 { DH_free(dh); return(NULL); }
1683 return(dh);
1684 }
53002dc6 1685
f3f316f1 1686static DH *get_dh1024dsa()
e4589582
BM
1687 {
1688 static unsigned char dh1024_p[]={
1689 0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,
1690 0x21,0x1B,0xF7,0x31,0xA6,0xA2,0xDA,0x23,0x9A,0xC7,0x87,0x19,
1691 0x3B,0x47,0xB6,0x8C,0x04,0x6F,0xFF,0xC6,0x9B,0xB8,0x65,0xD2,
1692 0xC2,0x5F,0x31,0x83,0x4A,0xA7,0x5F,0x2F,0x88,0x38,0xB6,0x55,
1693 0xCF,0xD9,0x87,0x6D,0x6F,0x9F,0xDA,0xAC,0xA6,0x48,0xAF,0xFC,
1694 0x33,0x84,0x37,0x5B,0x82,0x4A,0x31,0x5D,0xE7,0xBD,0x52,0x97,
1695 0xA1,0x77,0xBF,0x10,0x9E,0x37,0xEA,0x64,0xFA,0xCA,0x28,0x8D,
1696 0x9D,0x3B,0xD2,0x6E,0x09,0x5C,0x68,0xC7,0x45,0x90,0xFD,0xBB,
1697 0x70,0xC9,0x3A,0xBB,0xDF,0xD4,0x21,0x0F,0xC4,0x6A,0x3C,0xF6,
1698 0x61,0xCF,0x3F,0xD6,0x13,0xF1,0x5F,0xBC,0xCF,0xBC,0x26,0x9E,
1699 0xBC,0x0B,0xBD,0xAB,0x5D,0xC9,0x54,0x39,
1700 };
1701 static unsigned char dh1024_g[]={
1702 0x3B,0x40,0x86,0xE7,0xF3,0x6C,0xDE,0x67,0x1C,0xCC,0x80,0x05,
1703 0x5A,0xDF,0xFE,0xBD,0x20,0x27,0x74,0x6C,0x24,0xC9,0x03,0xF3,
1704 0xE1,0x8D,0xC3,0x7D,0x98,0x27,0x40,0x08,0xB8,0x8C,0x6A,0xE9,
1705 0xBB,0x1A,0x3A,0xD6,0x86,0x83,0x5E,0x72,0x41,0xCE,0x85,0x3C,
1706 0xD2,0xB3,0xFC,0x13,0xCE,0x37,0x81,0x9E,0x4C,0x1C,0x7B,0x65,
1707 0xD3,0xE6,0xA6,0x00,0xF5,0x5A,0x95,0x43,0x5E,0x81,0xCF,0x60,
1708 0xA2,0x23,0xFC,0x36,0xA7,0x5D,0x7A,0x4C,0x06,0x91,0x6E,0xF6,
1709 0x57,0xEE,0x36,0xCB,0x06,0xEA,0xF5,0x3D,0x95,0x49,0xCB,0xA7,
1710 0xDD,0x81,0xDF,0x80,0x09,0x4A,0x97,0x4D,0xA8,0x22,0x72,0xA1,
1711 0x7F,0xC4,0x70,0x56,0x70,0xE8,0x20,0x10,0x18,0x8F,0x2E,0x60,
1712 0x07,0xE7,0x68,0x1A,0x82,0x5D,0x32,0xA2,
1713 };
1714 DH *dh;
53002dc6 1715
e4589582
BM
1716 if ((dh=DH_new()) == NULL) return(NULL);
1717 dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
1718 dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
1719 if ((dh->p == NULL) || (dh->g == NULL))
1720 { DH_free(dh); return(NULL); }
1721 dh->length = 160;
1722 return(dh);
53002dc6
BM
1723 }
1724#endif