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