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