]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssltest.c
Synchronise with the makefiles.
[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 */
58
6f7af152
BM
59#include <assert.h>
60#include <errno.h>
61#include <limits.h>
d02b48c6
RE
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
17e3dd1c 65
7e701817 66#include "openssl/e_os.h"
17e3dd1c 67
ec577822
BM
68#include <openssl/bio.h>
69#include <openssl/crypto.h>
70#include <openssl/x509.h>
71#include <openssl/ssl.h>
72#include <openssl/err.h>
58964a49 73#ifdef WINDOWS
d02b48c6
RE
74#include "../crypto/bio/bss_file.c"
75#endif
76
79df9d62
UM
77#if defined(NO_RSA) && !defined(NO_SSL2)
78#define NO_SSL2
79#endif
80
7d7d2cbc
UM
81#ifdef VMS
82# define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
83# define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
84#else
85# define TEST_SERVER_CERT "../apps/server.pem"
86# define TEST_CLIENT_CERT "../apps/client.pem"
87#endif
d02b48c6 88
396f6314 89static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
79df9d62 90#ifndef NO_RSA
df63a389 91static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
79df9d62
UM
92#endif
93#ifndef NO_DH
d02b48c6 94static DH *get_dh512(void);
58964a49 95#endif
396f6314
BM
96static BIO *bio_err=NULL;
97static BIO *bio_stdout=NULL;
d02b48c6
RE
98
99static char *cipher=NULL;
79875776
BM
100static int verbose=0;
101static int debug=0;
d58d092b
BM
102#if 0
103/* Not used yet. */
d02b48c6
RE
104#ifdef FIONBIO
105static int s_nbio=0;
106#endif
d58d092b 107#endif
d02b48c6
RE
108
109
95d29597 110int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes);
58964a49 111int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
6b691a5c 112static void sv_usage(void)
d02b48c6
RE
113 {
114 fprintf(stderr,"usage: ssltest [args ...]\n");
115 fprintf(stderr,"\n");
116 fprintf(stderr," -server_auth - check server certificate\n");
117 fprintf(stderr," -client_auth - do client authentication\n");
118 fprintf(stderr," -v - more output\n");
58964a49
RE
119 fprintf(stderr," -d - debug output\n");
120 fprintf(stderr," -reuse - use session-id reuse\n");
121 fprintf(stderr," -num <val> - number of connections to perform\n");
122 fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n");
48c843c3
BM
123#if !defined NO_DH && !defined NO_DSA
124 fprintf(stderr," -dhe1024 - generate 1024 bit key for DHE\n");
125#endif
77fa04a9
BM
126#if !defined NO_DH
127 fprintf(stderr," -no_dhe - disable DHE\n");
128#endif
d02b48c6
RE
129#ifndef NO_SSL2
130 fprintf(stderr," -ssl2 - use SSLv2\n");
131#endif
132#ifndef NO_SSL3
133 fprintf(stderr," -ssl3 - use SSLv3\n");
58964a49
RE
134#endif
135#ifndef NO_TLS1
136 fprintf(stderr," -tls1 - use TLSv1\n");
d02b48c6
RE
137#endif
138 fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
139 fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
58964a49
RE
140 fprintf(stderr," -cert arg - Certificate file\n");
141 fprintf(stderr," -s_cert arg - Just the server certificate file\n");
142 fprintf(stderr," -c_cert arg - Just the client certificate file\n");
143 fprintf(stderr," -cipher arg - The cipher list\n");
95d29597
BM
144 fprintf(stderr," -bio_pair - Use BIO pairs\n");
145 fprintf(stderr," -f - Test even cases that can't work\n");
d02b48c6
RE
146 }
147
6b691a5c 148int main(int argc, char *argv[])
d02b48c6
RE
149 {
150 char *CApath=NULL,*CAfile=NULL;
151 int badop=0;
95d29597
BM
152 int bio_pair=0;
153 int force=0;
58964a49 154 int tls1=0,ssl2=0,ssl3=0,ret=1;
d02b48c6 155 int client_auth=0;
58964a49 156 int server_auth=0,i;
d02b48c6
RE
157 char *server_cert=TEST_SERVER_CERT;
158 char *client_cert=TEST_CLIENT_CERT;
159 SSL_CTX *s_ctx=NULL;
160 SSL_CTX *c_ctx=NULL;
161 SSL_METHOD *meth=NULL;
58964a49
RE
162 SSL *c_ssl,*s_ssl;
163 int number=1,reuse=0;
164 long bytes=1L;
165 SSL_CIPHER *ciph;
77fa04a9 166 int dhe1024 = 0, no_dhe = 0;
58964a49 167#ifndef NO_DH
d02b48c6 168 DH *dh;
58964a49 169#endif
79875776
BM
170 verbose = 0;
171 debug = 0;
172 cipher = 0;
173
174 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
d02b48c6
RE
175
176 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
177 bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
178
d02b48c6
RE
179 argc--;
180 argv++;
181
182 while (argc >= 1)
183 {
184 if (strcmp(*argv,"-server_auth") == 0)
185 server_auth=1;
186 else if (strcmp(*argv,"-client_auth") == 0)
187 client_auth=1;
188 else if (strcmp(*argv,"-v") == 0)
189 verbose=1;
58964a49
RE
190 else if (strcmp(*argv,"-d") == 0)
191 debug=1;
192 else if (strcmp(*argv,"-reuse") == 0)
193 reuse=1;
48c843c3
BM
194 else if (strcmp(*argv,"-dhe1024") == 0)
195 dhe1024=1;
77fa04a9
BM
196 else if (strcmp(*argv,"-no_dhe") == 0)
197 no_dhe=1;
d02b48c6
RE
198 else if (strcmp(*argv,"-ssl2") == 0)
199 ssl2=1;
58964a49
RE
200 else if (strcmp(*argv,"-tls1") == 0)
201 tls1=1;
d02b48c6
RE
202 else if (strcmp(*argv,"-ssl3") == 0)
203 ssl3=1;
58964a49
RE
204 else if (strncmp(*argv,"-num",4) == 0)
205 {
206 if (--argc < 1) goto bad;
207 number= atoi(*(++argv));
208 if (number == 0) number=1;
209 }
210 else if (strcmp(*argv,"-bytes") == 0)
211 {
212 if (--argc < 1) goto bad;
213 bytes= atol(*(++argv));
214 if (bytes == 0L) bytes=1L;
215 i=strlen(argv[0]);
216 if (argv[0][i-1] == 'k') bytes*=1024L;
217 if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
218 }
d02b48c6
RE
219 else if (strcmp(*argv,"-cert") == 0)
220 {
221 if (--argc < 1) goto bad;
222 server_cert= *(++argv);
223 }
224 else if (strcmp(*argv,"-s_cert") == 0)
225 {
226 if (--argc < 1) goto bad;
227 server_cert= *(++argv);
228 }
229 else if (strcmp(*argv,"-c_cert") == 0)
230 {
231 if (--argc < 1) goto bad;
232 client_cert= *(++argv);
233 }
234 else if (strcmp(*argv,"-cipher") == 0)
235 {
236 if (--argc < 1) goto bad;
237 cipher= *(++argv);
238 }
239 else if (strcmp(*argv,"-CApath") == 0)
240 {
241 if (--argc < 1) goto bad;
242 CApath= *(++argv);
243 }
244 else if (strcmp(*argv,"-CAfile") == 0)
245 {
246 if (--argc < 1) goto bad;
247 CAfile= *(++argv);
248 }
95d29597
BM
249 else if (strcmp(*argv,"-bio_pair") == 0)
250 {
251 bio_pair = 1;
252 }
253 else if (strcmp(*argv,"-f") == 0)
254 {
255 force = 1;
256 }
d02b48c6
RE
257 else
258 {
259 fprintf(stderr,"unknown option %s\n",*argv);
260 badop=1;
261 break;
262 }
263 argc--;
264 argv++;
265 }
266 if (badop)
267 {
268bad:
269 sv_usage();
270 goto end;
271 }
272
95d29597
BM
273 if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
274 {
275 fprintf(stderr, "This case cannot work. Use -f switch to perform "
276 "the test anyway\n"
277 "(and -d to see what happens, "
278 "and -bio_pair to really make it happen :-)\n"
279 "or add one of -ssl2, -ssl3, -tls1, -reuse to "
280 "avoid protocol mismatch.\n");
281 exit(1);
282 }
283
d02b48c6
RE
284/* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
285
413c4f45 286 SSL_library_init();
d02b48c6
RE
287 SSL_load_error_strings();
288
289#if !defined(NO_SSL2) && !defined(NO_SSL3)
290 if (ssl2)
291 meth=SSLv2_method();
292 else
58964a49
RE
293 if (tls1)
294 meth=TLSv1_method();
295 else
d02b48c6
RE
296 if (ssl3)
297 meth=SSLv3_method();
298 else
299 meth=SSLv23_method();
300#else
301#ifdef NO_SSL2
302 meth=SSLv3_method();
303#else
304 meth=SSLv2_method();
305#endif
306#endif
307
308 c_ctx=SSL_CTX_new(meth);
309 s_ctx=SSL_CTX_new(meth);
310 if ((c_ctx == NULL) || (s_ctx == NULL))
311 {
312 ERR_print_errors(bio_err);
313 goto end;
314 }
315
316 if (cipher != NULL)
317 {
318 SSL_CTX_set_cipher_list(c_ctx,cipher);
319 SSL_CTX_set_cipher_list(s_ctx,cipher);
320 }
321
322#ifndef NO_DH
77fa04a9 323 if (!no_dhe)
48c843c3 324 {
77fa04a9
BM
325# ifndef NO_DSA
326 if (dhe1024)
48c843c3 327 {
77fa04a9
BM
328 DSA *dsa;
329 unsigned char seed[20];
330
331 if (verbose)
332 {
333 fprintf(stdout, "Creating 1024 bit DHE parameters ...");
334 fflush(stdout);
335 }
336
337 memcpy(seed, "Random String no. 12", 20);
338 dsa = DSA_generate_parameters(1024, seed, 20, NULL, NULL, 0, NULL);
339 dh = DSA_dup_DH(dsa);
340 DSA_free(dsa);
341 /* important: SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
342 SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
343
344 if (verbose)
345 fprintf(stdout, " done\n");
48c843c3 346 }
77fa04a9 347 else
48c843c3 348# endif
77fa04a9
BM
349 dh=get_dh512();
350 SSL_CTX_set_tmp_dh(s_ctx,dh);
351 DH_free(dh);
352 }
58964a49
RE
353#endif
354
355#ifndef NO_RSA
356 SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
d02b48c6
RE
357#endif
358
359 if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
360 {
361 ERR_print_errors(bio_err);
362 }
363 else if (!SSL_CTX_use_PrivateKey_file(s_ctx,server_cert,
364 SSL_FILETYPE_PEM))
365 {
366 ERR_print_errors(bio_err);
367 goto end;
368 }
369
370 if (client_auth)
371 {
372 SSL_CTX_use_certificate_file(c_ctx,client_cert,
373 SSL_FILETYPE_PEM);
374 SSL_CTX_use_PrivateKey_file(c_ctx,client_cert,
375 SSL_FILETYPE_PEM);
376 }
377
378 if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
379 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
380 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
381 (!SSL_CTX_set_default_verify_paths(c_ctx)))
382 {
58964a49 383 /* fprintf(stderr,"SSL_load_verify_locations\n"); */
d02b48c6 384 ERR_print_errors(bio_err);
58964a49 385 /* goto end; */
d02b48c6
RE
386 }
387
388 if (client_auth)
389 {
390 fprintf(stderr,"client authentication\n");
391 SSL_CTX_set_verify(s_ctx,
392 SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
393 verify_callback);
394 }
395 if (server_auth)
396 {
397 fprintf(stderr,"server authentication\n");
398 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
399 verify_callback);
400 }
b1fe6ca1
BM
401
402 {
403 int session_id_context = 0;
404 SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);
405 }
d02b48c6 406
58964a49
RE
407 c_ssl=SSL_new(c_ctx);
408 s_ssl=SSL_new(s_ctx);
409
410 for (i=0; i<number; i++)
411 {
412 if (!reuse) SSL_set_session(c_ssl,NULL);
95d29597
BM
413 if (bio_pair)
414 ret=doit_biopair(s_ssl,c_ssl,bytes);
415 else
416 ret=doit(s_ssl,c_ssl,bytes);
58964a49
RE
417 }
418
419 if (!verbose)
420 {
421 ciph=SSL_get_current_cipher(c_ssl);
422 fprintf(stdout,"Protocol %s, cipher %s, %s\n",
423 SSL_get_version(c_ssl),
424 SSL_CIPHER_get_version(ciph),
425 SSL_CIPHER_get_name(ciph));
426 }
427 if ((number > 1) || (bytes > 1L))
428 printf("%d handshakes of %ld bytes done\n",number,bytes);
429
430 SSL_free(s_ssl);
431 SSL_free(c_ssl);
432
d02b48c6
RE
433end:
434 if (s_ctx != NULL) SSL_CTX_free(s_ctx);
435 if (c_ctx != NULL) SSL_CTX_free(c_ctx);
436
437 if (bio_stdout != NULL) BIO_free(bio_stdout);
438
dfeab068 439 ERR_free_strings();
d02b48c6
RE
440 ERR_remove_state(0);
441 EVP_cleanup();
442 CRYPTO_mem_leaks(bio_err);
79875776 443 if (bio_err != NULL) BIO_free(bio_err);
d02b48c6
RE
444 EXIT(ret);
445 }
446
95d29597
BM
447int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count)
448 {
449 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
450 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
451 BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
452 SSL_CIPHER *ciph;
453 int ret = 1;
454
455 size_t bufsiz = 256; /* small buffer for testing */
456
457 if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
458 goto err;
459 if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
460 goto err;
461
462 s_ssl_bio = BIO_new(BIO_f_ssl());
463 if (!s_ssl_bio)
464 goto err;
465
466 c_ssl_bio = BIO_new(BIO_f_ssl());
467 if (!c_ssl_bio)
468 goto err;
469
470 SSL_set_connect_state(c_ssl);
471 SSL_set_bio(c_ssl, client, client);
472 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
473
474 SSL_set_accept_state(s_ssl);
475 SSL_set_bio(s_ssl, server, server);
476 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
477
478 do
479 {
480 /* c_ssl_bio: SSL filter BIO
481 *
482 * client: pseudo-I/O for SSL library
483 *
484 * client_io: client's SSL communication; usually to be
485 * relayed over some I/O facility, but in this
486 * test program, we're the server, too:
487 *
488 * server_io: server's SSL communication
489 *
490 * server: pseudo-I/O for SSL library
491 *
492 * s_ssl_bio: SSL filter BIO
493 *
494 * The client and the server each employ a "BIO pair":
495 * client + client_io, server + server_io.
496 * BIO pairs are symmetric. A BIO pair behaves similar
497 * to a non-blocking socketpair (but both endpoints must
498 * be handled by the same thread).
7eea36bb
BM
499 * [Here we could connect client and server to the ends
500 * of a single BIO pair, but then this code would be less
501 * suitable as an example for BIO pairs in general.]
95d29597
BM
502 *
503 * Useful functions for querying the state of BIO pair endpoints:
504 *
505 * BIO_ctrl_pending(bio) number of bytes we can read now
506 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil
507 * other side's read attempt
508 * BIO_ctrl_get_write_gurantee(bio) number of bytes we can write now
509 *
510 * ..._read_request is never more than ..._write_guarantee;
511 * it depends on the application which one you should use.
512 */
513
514 /* We have non-blocking behaviour throughout this test program, but
515 * can be sure that there is *some* progress in each iteration; so
516 * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
517 * -- we just try everything in each iteration
518 */
519
520 {
521 /* CLIENT */
522
523 MS_STATIC char cbuf[1024*8];
524 int i, r;
525
526 if (debug)
527 if (SSL_in_init(c_ssl))
528 printf("client waiting in SSL_connect - %s\n",
529 SSL_state_string_long(c_ssl));
530
531 if (cw_num > 0)
532 {
533 /* Write to server. */
534
535 if (cw_num > (long)sizeof cbuf)
536 i = sizeof cbuf;
537 else
538 i = (int)cw_num;
539 r = BIO_write(c_ssl_bio, cbuf, i);
29159a42 540 if (r < 0)
95d29597
BM
541 {
542 if (!BIO_should_retry(c_ssl_bio))
543 {
544 fprintf(stderr,"ERROR in CLIENT\n");
545 goto err;
546 }
547 /* BIO_should_retry(...) can just be ignored here.
548 * The library expects us to call BIO_write with
549 * the same arguments again, and that's what we will
550 * do in the next iteration. */
551 }
552 else if (r == 0)
553 {
554 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
555 goto err;
556 }
557 else
558 {
559 if (debug)
560 printf("client wrote %d\n", r);
561 cw_num -= r;
562 }
563 }
564
565 if (cr_num > 0)
566 {
567 /* Read from server. */
568
569 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
570 if (r < 0)
571 {
572 if (!BIO_should_retry(c_ssl_bio))
573 {
574 fprintf(stderr,"ERROR in CLIENT\n");
575 goto err;
576 }
577 /* Again, "BIO_should_retry" can be ignored. */
578 }
579 else if (r == 0)
580 {
581 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
582 goto err;
583 }
584 else
585 {
586 if (debug)
587 printf("client read %d\n", r);
588 cr_num -= r;
589 }
590 }
591 }
592
593 {
594 /* SERVER */
595
596 MS_STATIC char sbuf[1024*8];
597 int i, r;
598
599 if (debug)
600 if (SSL_in_init(s_ssl))
601 printf("server waiting in SSL_accept - %s\n",
602 SSL_state_string_long(s_ssl));
603
604 if (sw_num > 0)
605 {
606 /* Write to client. */
607
608 if (sw_num > (long)sizeof sbuf)
609 i = sizeof sbuf;
610 else
611 i = (int)sw_num;
612 r = BIO_write(s_ssl_bio, sbuf, i);
29159a42 613 if (r < 0)
95d29597
BM
614 {
615 if (!BIO_should_retry(s_ssl_bio))
616 {
617 fprintf(stderr,"ERROR in SERVER\n");
618 goto err;
619 }
620 /* Ignore "BIO_should_retry". */
621 }
622 else if (r == 0)
623 {
624 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
625 goto err;
626 }
627 else
628 {
629 if (debug)
630 printf("server wrote %d\n", r);
631 sw_num -= r;
632 }
633 }
634
635 if (sr_num > 0)
636 {
637 /* Read from client. */
638
639 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
640 if (r < 0)
641 {
642 if (!BIO_should_retry(s_ssl_bio))
643 {
644 fprintf(stderr,"ERROR in SERVER\n");
645 goto err;
646 }
647 /* blah, blah */
648 }
649 else if (r == 0)
650 {
651 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
652 goto err;
653 }
654 else
655 {
656 if (debug)
657 printf("server read %d\n", r);
658 sr_num -= r;
659 }
660 }
661 }
662
663 {
664 /* "I/O" BETWEEN CLIENT AND SERVER. */
665
95d29597 666 size_t r1, r2;
6f7af152
BM
667 BIO *io1 = server_io, *io2 = client_io;
668 /* we use the non-copying interface for io1
669 * and the standard BIO_write/BIO_read interface for io2
670 */
671
95d29597
BM
672 static int prev_progress = 1;
673 int progress = 0;
674
6f7af152 675 /* io1 to io2 */
95d29597
BM
676 do
677 {
6f7af152
BM
678 size_t num;
679 int r;
680
681 r1 = BIO_ctrl_pending(io1);
682 r2 = BIO_ctrl_get_write_guarantee(io2);
95d29597
BM
683
684 num = r1;
685 if (r2 < num)
686 num = r2;
687 if (num)
688 {
6f7af152
BM
689 char *dataptr;
690
95d29597
BM
691 if (INT_MAX < num) /* yeah, right */
692 num = INT_MAX;
693
6f7af152
BM
694 r = BIO_nread(io1, &dataptr, (int)num);
695 assert(r > 0);
696 assert(r <= (int)num);
697 /* possibly r < num (non-contiguous data) */
698 num = r;
699 r = BIO_write(io2, dataptr, (int)num);
95d29597
BM
700 if (r != (int)num) /* can't happen */
701 {
702 fprintf(stderr, "ERROR: BIO_write could not write "
703 "BIO_ctrl_get_write_guarantee() bytes");
704 goto err;
705 }
706 progress = 1;
707
708 if (debug)
6f7af152
BM
709 printf((io1 == client_io) ?
710 "C->S relaying: %d bytes\n" :
711 "S->C relaying: %d bytes\n",
712 (int)num);
95d29597
BM
713 }
714 }
715 while (r1 && r2);
716
6f7af152
BM
717 /* io2 to io1 */
718 {
719 size_t num;
720 int r;
721
722 r1 = BIO_ctrl_pending(io2);
723 r2 = BIO_ctrl_get_read_request(io1);
724 /* here we could use ..._get_write_guarantee instead of
725 * ..._get_read_request, but by using the latter
726 * we test restartability of the SSL implementation
727 * more thoroughly */
95d29597
BM
728 num = r1;
729 if (r2 < num)
730 num = r2;
731 if (num)
732 {
6f7af152
BM
733 char *dataptr;
734
95d29597
BM
735 if (INT_MAX < num)
736 num = INT_MAX;
6f7af152
BM
737
738 if (num > 1)
cb0369d8 739 --num; /* test restartability even more thoroughly */
95d29597 740
6f7af152
BM
741 r = BIO_nwrite(io1, &dataptr, (int)num);
742 assert(r > 0);
743 assert(r <= (int)num);
744 num = r;
745 r = BIO_read(io2, dataptr, (int)num);
95d29597
BM
746 if (r != (int)num) /* can't happen */
747 {
748 fprintf(stderr, "ERROR: BIO_read could not read "
749 "BIO_ctrl_pending() bytes");
750 goto err;
751 }
95d29597 752 progress = 1;
6f7af152 753
95d29597 754 if (debug)
6f7af152
BM
755 printf((io2 == client_io) ?
756 "C->S relaying: %d bytes\n" :
757 "S->C relaying: %d bytes\n",
758 (int)num);
95d29597 759 }
6f7af152 760 } /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
95d29597
BM
761
762 if (!progress && !prev_progress)
763 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
95d29597
BM
764 {
765 fprintf(stderr, "ERROR: got stuck\n");
d7fcc7f6
BM
766 if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
767 {
768 fprintf(stderr, "This can happen for SSL2 because "
769 "CLIENT-FINISHED and SERVER-VERIFY are written \n"
770 "concurrently ...");
771 if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
772 && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
773 {
774 fprintf(stderr, " ok.\n");
775 goto end;
776 }
777 }
778 fprintf(stderr, " ERROR.\n");
95d29597
BM
779 goto err;
780 }
781 prev_progress = progress;
782 }
783 }
784 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
785
786 ciph = SSL_get_current_cipher(c_ssl);
787 if (verbose)
788 fprintf(stdout,"DONE via BIO pair, protocol %s, cipher %s, %s\n",
789 SSL_get_version(c_ssl),
790 SSL_CIPHER_get_version(ciph),
791 SSL_CIPHER_get_name(ciph));
d7fcc7f6 792 end:
95d29597
BM
793 ret = 0;
794
795 err:
796 ERR_print_errors(bio_err);
797
798 if (server)
799 BIO_free(server);
800 if (server_io)
801 BIO_free(server_io);
802 if (client)
803 BIO_free(client);
804 if (client_io)
805 BIO_free(client_io);
806 if (s_ssl_bio)
807 BIO_free(s_ssl_bio);
808 if (c_ssl_bio)
809 BIO_free(c_ssl_bio);
810
811 return ret;
812 }
813
814
d02b48c6
RE
815#define W_READ 1
816#define W_WRITE 2
817#define C_DONE 1
818#define S_DONE 2
819
6b691a5c 820int doit(SSL *s_ssl, SSL *c_ssl, long count)
d02b48c6 821 {
58964a49
RE
822 MS_STATIC char cbuf[1024*8],sbuf[1024*8];
823 long cw_num=count,cr_num=count;
824 long sw_num=count,sr_num=count;
d02b48c6 825 int ret=1;
d02b48c6
RE
826 BIO *c_to_s=NULL;
827 BIO *s_to_c=NULL;
828 BIO *c_bio=NULL;
829 BIO *s_bio=NULL;
830 int c_r,c_w,s_r,s_w;
831 int c_want,s_want;
58964a49 832 int i,j;
d02b48c6
RE
833 int done=0;
834 int c_write,s_write;
835 int do_server=0,do_client=0;
836 SSL_CIPHER *ciph;
837
d02b48c6
RE
838 c_to_s=BIO_new(BIO_s_mem());
839 s_to_c=BIO_new(BIO_s_mem());
840 if ((s_to_c == NULL) || (c_to_s == NULL))
841 {
842 ERR_print_errors(bio_err);
843 goto err;
844 }
845
846 c_bio=BIO_new(BIO_f_ssl());
847 s_bio=BIO_new(BIO_f_ssl());
848 if ((c_bio == NULL) || (s_bio == NULL))
849 {
850 ERR_print_errors(bio_err);
851 goto err;
852 }
853
854 SSL_set_connect_state(c_ssl);
855 SSL_set_bio(c_ssl,s_to_c,c_to_s);
58964a49 856 BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
d02b48c6
RE
857
858 SSL_set_accept_state(s_ssl);
859 SSL_set_bio(s_ssl,c_to_s,s_to_c);
58964a49 860 BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
d02b48c6
RE
861
862 c_r=0; s_r=1;
863 c_w=1; s_w=0;
864 c_want=W_WRITE;
865 s_want=0;
866 c_write=1,s_write=0;
867
868 /* We can always do writes */
869 for (;;)
870 {
871 do_server=0;
872 do_client=0;
873
874 i=(int)BIO_pending(s_bio);
875 if ((i && s_r) || s_w) do_server=1;
876
877 i=(int)BIO_pending(c_bio);
878 if ((i && c_r) || c_w) do_client=1;
879
58964a49 880 if (do_server && debug)
d02b48c6
RE
881 {
882 if (SSL_in_init(s_ssl))
883 printf("server waiting in SSL_accept - %s\n",
884 SSL_state_string_long(s_ssl));
58964a49 885/* else if (s_write)
d02b48c6 886 printf("server:SSL_write()\n");
58964a49
RE
887 else
888 printf("server:SSL_read()\n"); */
d02b48c6
RE
889 }
890
58964a49 891 if (do_client && debug)
d02b48c6
RE
892 {
893 if (SSL_in_init(c_ssl))
894 printf("client waiting in SSL_connect - %s\n",
895 SSL_state_string_long(c_ssl));
58964a49 896/* else if (c_write)
d02b48c6
RE
897 printf("client:SSL_write()\n");
898 else
58964a49 899 printf("client:SSL_read()\n"); */
d02b48c6
RE
900 }
901
902 if (!do_client && !do_server)
903 {
904 fprintf(stdout,"ERROR IN STARTUP\n");
905 ERR_print_errors(bio_err);
906 break;
907 }
908 if (do_client && !(done & C_DONE))
909 {
910 if (c_write)
911 {
58964a49
RE
912 j=(cw_num > (long)sizeof(cbuf))
913 ?sizeof(cbuf):(int)cw_num;
914 i=BIO_write(c_bio,cbuf,j);
d02b48c6
RE
915 if (i < 0)
916 {
917 c_r=0;
918 c_w=0;
919 if (BIO_should_retry(c_bio))
920 {
921 if (BIO_should_read(c_bio))
922 c_r=1;
923 if (BIO_should_write(c_bio))
924 c_w=1;
925 }
926 else
927 {
928 fprintf(stderr,"ERROR in CLIENT\n");
929 ERR_print_errors(bio_err);
930 goto err;
931 }
932 }
933 else if (i == 0)
934 {
935 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
936 goto err;
937 }
938 else
939 {
58964a49
RE
940 if (debug)
941 printf("client wrote %d\n",i);
d02b48c6 942 /* ok */
58964a49 943 s_r=1;
d02b48c6 944 c_write=0;
58964a49 945 cw_num-=i;
d02b48c6
RE
946 }
947 }
948 else
949 {
58964a49 950 i=BIO_read(c_bio,cbuf,sizeof(cbuf));
d02b48c6
RE
951 if (i < 0)
952 {
953 c_r=0;
954 c_w=0;
955 if (BIO_should_retry(c_bio))
956 {
957 if (BIO_should_read(c_bio))
958 c_r=1;
959 if (BIO_should_write(c_bio))
960 c_w=1;
961 }
962 else
963 {
964 fprintf(stderr,"ERROR in CLIENT\n");
965 ERR_print_errors(bio_err);
966 goto err;
967 }
968 }
969 else if (i == 0)
970 {
971 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
972 goto err;
973 }
974 else
975 {
58964a49
RE
976 if (debug)
977 printf("client read %d\n",i);
978 cr_num-=i;
979 if (sw_num > 0)
980 {
981 s_write=1;
982 s_w=1;
983 }
984 if (cr_num <= 0)
985 {
986 s_write=1;
987 s_w=1;
988 done=S_DONE|C_DONE;
989 }
d02b48c6
RE
990 }
991 }
992 }
993
994 if (do_server && !(done & S_DONE))
995 {
996 if (!s_write)
997 {
58964a49 998 i=BIO_read(s_bio,sbuf,sizeof(cbuf));
d02b48c6
RE
999 if (i < 0)
1000 {
1001 s_r=0;
1002 s_w=0;
1003 if (BIO_should_retry(s_bio))
1004 {
1005 if (BIO_should_read(s_bio))
1006 s_r=1;
1007 if (BIO_should_write(s_bio))
1008 s_w=1;
1009 }
1010 else
1011 {
1012 fprintf(stderr,"ERROR in SERVER\n");
1013 ERR_print_errors(bio_err);
1014 goto err;
1015 }
1016 }
1017 else if (i == 0)
1018 {
1019 ERR_print_errors(bio_err);
1020 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
1021 goto err;
1022 }
1023 else
1024 {
58964a49
RE
1025 if (debug)
1026 printf("server read %d\n",i);
1027 sr_num-=i;
1028 if (cw_num > 0)
1029 {
1030 c_write=1;
1031 c_w=1;
1032 }
1033 if (sr_num <= 0)
1034 {
1035 s_write=1;
1036 s_w=1;
1037 c_write=0;
1038 }
d02b48c6
RE
1039 }
1040 }
1041 else
1042 {
58964a49
RE
1043 j=(sw_num > (long)sizeof(sbuf))?
1044 sizeof(sbuf):(int)sw_num;
1045 i=BIO_write(s_bio,sbuf,j);
d02b48c6
RE
1046 if (i < 0)
1047 {
1048 s_r=0;
1049 s_w=0;
1050 if (BIO_should_retry(s_bio))
1051 {
1052 if (BIO_should_read(s_bio))
1053 s_r=1;
1054 if (BIO_should_write(s_bio))
1055 s_w=1;
1056 }
1057 else
1058 {
1059 fprintf(stderr,"ERROR in SERVER\n");
1060 ERR_print_errors(bio_err);
1061 goto err;
1062 }
1063 }
1064 else if (i == 0)
1065 {
1066 ERR_print_errors(bio_err);
1067 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
1068 goto err;
1069 }
1070 else
1071 {
58964a49
RE
1072 if (debug)
1073 printf("server wrote %d\n",i);
1074 sw_num-=i;
d02b48c6 1075 s_write=0;
58964a49
RE
1076 c_r=1;
1077 if (sw_num <= 0)
1078 done|=S_DONE;
d02b48c6
RE
1079 }
1080 }
1081 }
1082
1083 if ((done & S_DONE) && (done & C_DONE)) break;
1084 }
1085
1086 ciph=SSL_get_current_cipher(c_ssl);
58964a49
RE
1087 if (verbose)
1088 fprintf(stdout,"DONE, protocol %s, cipher %s, %s\n",
1089 SSL_get_version(c_ssl),
1090 SSL_CIPHER_get_version(ciph),
1091 SSL_CIPHER_get_name(ciph));
d02b48c6
RE
1092 ret=0;
1093err:
1094 /* We have to set the BIO's to NULL otherwise they will be
1095 * Free()ed twice. Once when th s_ssl is SSL_free()ed and
1096 * again when c_ssl is SSL_free()ed.
1097 * This is a hack required because s_ssl and c_ssl are sharing the same
1098 * BIO structure and SSL_set_bio() and SSL_free() automatically
1099 * BIO_free non NULL entries.
1100 * You should not normally do this or be required to do this */
1101 if (s_ssl != NULL)
1102 {
1103 s_ssl->rbio=NULL;
1104 s_ssl->wbio=NULL;
1105 }
1106 if (c_ssl != NULL)
1107 {
1108 c_ssl->rbio=NULL;
1109 c_ssl->wbio=NULL;
1110 }
1111
1112 if (c_to_s != NULL) BIO_free(c_to_s);
1113 if (s_to_c != NULL) BIO_free(s_to_c);
58964a49
RE
1114 if (c_bio != NULL) BIO_free_all(c_bio);
1115 if (s_bio != NULL) BIO_free_all(s_bio);
d02b48c6
RE
1116 return(ret);
1117 }
1118
396f6314 1119static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
d02b48c6
RE
1120 {
1121 char *s,buf[256];
1122
1123 s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,256);
1124 if (s != NULL)
1125 {
1126 if (ok)
1127 fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
1128 else
1129 fprintf(stderr,"depth=%d error=%d %s\n",
1130 ctx->error_depth,ctx->error,buf);
1131 }
1132
1133 if (ok == 0)
1134 {
1135 switch (ctx->error)
1136 {
1137 case X509_V_ERR_CERT_NOT_YET_VALID:
1138 case X509_V_ERR_CERT_HAS_EXPIRED:
1139 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1140 ok=1;
1141 }
1142 }
1143
1144 return(ok);
1145 }
1146
58964a49 1147#ifndef NO_DH
d02b48c6
RE
1148static unsigned char dh512_p[]={
1149 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
1150 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
1151 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
1152 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
1153 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
1154 0x47,0x74,0xE8,0x33,
1155 };
1156static unsigned char dh512_g[]={
1157 0x02,
1158 };
1159
6b691a5c 1160static DH *get_dh512(void)
d02b48c6
RE
1161 {
1162 DH *dh=NULL;
1163
d02b48c6
RE
1164 if ((dh=DH_new()) == NULL) return(NULL);
1165 dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
1166 dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
1167 if ((dh->p == NULL) || (dh->g == NULL))
1168 return(NULL);
d02b48c6
RE
1169 return(dh);
1170 }
58964a49
RE
1171#endif
1172
79df9d62 1173#ifndef NO_RSA
df63a389 1174static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
58964a49
RE
1175 {
1176 static RSA *rsa_tmp=NULL;
1177
1178 if (rsa_tmp == NULL)
1179 {
60e31c3a 1180 BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
d58d092b 1181 (void)BIO_flush(bio_err);
60e31c3a 1182 rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
58964a49 1183 BIO_printf(bio_err,"\n");
d58d092b 1184 (void)BIO_flush(bio_err);
58964a49
RE
1185 }
1186 return(rsa_tmp);
1187 }
79df9d62 1188#endif