]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssltest.c
typo
[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
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <errno.h>
95d29597 63#include <limits.h>
17e3dd1c 64
7e701817 65#include "openssl/e_os.h"
17e3dd1c 66
ec577822
BM
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>
58964a49 72#ifdef WINDOWS
d02b48c6
RE
73#include "../crypto/bio/bss_file.c"
74#endif
75
79df9d62
UM
76#if defined(NO_RSA) && !defined(NO_SSL2)
77#define NO_SSL2
78#endif
79
7d7d2cbc
UM
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
d02b48c6 87
396f6314 88static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
79df9d62 89#ifndef NO_RSA
df63a389 90static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
79df9d62
UM
91#endif
92#ifndef NO_DH
d02b48c6 93static DH *get_dh512(void);
58964a49 94#endif
396f6314
BM
95static BIO *bio_err=NULL;
96static BIO *bio_stdout=NULL;
d02b48c6
RE
97
98static char *cipher=NULL;
99int verbose=0;
58964a49 100int debug=0;
d58d092b
BM
101#if 0
102/* Not used yet. */
d02b48c6
RE
103#ifdef FIONBIO
104static int s_nbio=0;
105#endif
d58d092b 106#endif
d02b48c6
RE
107
108
95d29597 109int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes);
58964a49 110int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
6b691a5c 111static void sv_usage(void)
d02b48c6
RE
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");
58964a49
RE
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");
48c843c3
BM
122#if !defined NO_DH && !defined NO_DSA
123 fprintf(stderr," -dhe1024 - generate 1024 bit key for DHE\n");
124#endif
77fa04a9
BM
125#if !defined NO_DH
126 fprintf(stderr," -no_dhe - disable DHE\n");
127#endif
d02b48c6
RE
128#ifndef NO_SSL2
129 fprintf(stderr," -ssl2 - use SSLv2\n");
130#endif
131#ifndef NO_SSL3
132 fprintf(stderr," -ssl3 - use SSLv3\n");
58964a49
RE
133#endif
134#ifndef NO_TLS1
135 fprintf(stderr," -tls1 - use TLSv1\n");
d02b48c6
RE
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");
58964a49
RE
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");
95d29597
BM
143 fprintf(stderr," -bio_pair - Use BIO pairs\n");
144 fprintf(stderr," -f - Test even cases that can't work\n");
d02b48c6
RE
145 }
146
6b691a5c 147int main(int argc, char *argv[])
d02b48c6
RE
148 {
149 char *CApath=NULL,*CAfile=NULL;
150 int badop=0;
95d29597
BM
151 int bio_pair=0;
152 int force=0;
58964a49 153 int tls1=0,ssl2=0,ssl3=0,ret=1;
d02b48c6 154 int client_auth=0;
58964a49 155 int server_auth=0,i;
d02b48c6
RE
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;
58964a49
RE
161 SSL *c_ssl,*s_ssl;
162 int number=1,reuse=0;
163 long bytes=1L;
164 SSL_CIPHER *ciph;
77fa04a9 165 int dhe1024 = 0, no_dhe = 0;
58964a49 166#ifndef NO_DH
d02b48c6 167 DH *dh;
58964a49 168#endif
d02b48c6
RE
169
170 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
171 bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
172
777ab7e6 173 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
d02b48c6
RE
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;
58964a49
RE
186 else if (strcmp(*argv,"-d") == 0)
187 debug=1;
188 else if (strcmp(*argv,"-reuse") == 0)
189 reuse=1;
48c843c3
BM
190 else if (strcmp(*argv,"-dhe1024") == 0)
191 dhe1024=1;
77fa04a9
BM
192 else if (strcmp(*argv,"-no_dhe") == 0)
193 no_dhe=1;
d02b48c6
RE
194 else if (strcmp(*argv,"-ssl2") == 0)
195 ssl2=1;
58964a49
RE
196 else if (strcmp(*argv,"-tls1") == 0)
197 tls1=1;
d02b48c6
RE
198 else if (strcmp(*argv,"-ssl3") == 0)
199 ssl3=1;
58964a49
RE
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 }
d02b48c6
RE
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 }
95d29597
BM
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 }
d02b48c6
RE
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 {
264bad:
265 sv_usage();
266 goto end;
267 }
268
95d29597
BM
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
d02b48c6
RE
280/* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
281
413c4f45 282 SSL_library_init();
d02b48c6
RE
283 SSL_load_error_strings();
284
285#if !defined(NO_SSL2) && !defined(NO_SSL3)
286 if (ssl2)
287 meth=SSLv2_method();
288 else
58964a49
RE
289 if (tls1)
290 meth=TLSv1_method();
291 else
d02b48c6
RE
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
77fa04a9 319 if (!no_dhe)
48c843c3 320 {
77fa04a9
BM
321# ifndef NO_DSA
322 if (dhe1024)
48c843c3 323 {
77fa04a9
BM
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");
48c843c3 342 }
77fa04a9 343 else
48c843c3 344# endif
77fa04a9
BM
345 dh=get_dh512();
346 SSL_CTX_set_tmp_dh(s_ctx,dh);
347 DH_free(dh);
348 }
58964a49
RE
349#endif
350
351#ifndef NO_RSA
352 SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
d02b48c6
RE
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 {
58964a49 379 /* fprintf(stderr,"SSL_load_verify_locations\n"); */
d02b48c6 380 ERR_print_errors(bio_err);
58964a49 381 /* goto end; */
d02b48c6
RE
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
58964a49
RE
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);
95d29597
BM
404 if (bio_pair)
405 ret=doit_biopair(s_ssl,c_ssl,bytes);
406 else
407 ret=doit(s_ssl,c_ssl,bytes);
58964a49
RE
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
d02b48c6
RE
424end:
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
dfeab068 430 ERR_free_strings();
d02b48c6
RE
431 ERR_remove_state(0);
432 EVP_cleanup();
433 CRYPTO_mem_leaks(bio_err);
434 EXIT(ret);
435 }
436
95d29597
BM
437int 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).
7eea36bb
BM
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.]
95d29597
BM
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);
29159a42 530 if (r < 0)
95d29597
BM
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);
29159a42 603 if (r < 0)
95d29597
BM
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
c1082a90
BM
656 /* TODO: use non-blocking BIO interface for (say) the client
657 * to illustrate its use and to test it. */
658
95d29597
BM
659#define RELAYBUFSIZ 200
660 static char buf[RELAYBUFSIZ];
661
662 /* RELAYBUF is arbitrary. When writing data over some real
663 * network, use a buffer of the same size as in the BIO_pipe
664 * and make that size large (for reading from the network
665 * small buffers usually won't hurt).
666 * Here sizes differ for testing. */
667
668 size_t r1, r2;
669 size_t num;
670 int r;
671 static int prev_progress = 1;
672 int progress = 0;
673
674 /* client to server */
675 do
676 {
677 r1 = BIO_ctrl_pending(client_io);
678 r2 = BIO_ctrl_get_write_guarantee(server_io);
679
680 num = r1;
681 if (r2 < num)
682 num = r2;
683 if (num)
684 {
685 if (sizeof buf < num)
686 num = sizeof buf;
687 if (INT_MAX < num) /* yeah, right */
688 num = INT_MAX;
689
690 r = BIO_read(client_io, buf, (int)num);
691 if (r != (int)num) /* can't happen */
692 {
693 fprintf(stderr, "ERROR: BIO_read could not read "
694 "BIO_ctrl_pending() bytes");
695 goto err;
696 }
697 r = BIO_write(server_io, buf, (int)num);
698 if (r != (int)num) /* can't happen */
699 {
700 fprintf(stderr, "ERROR: BIO_write could not write "
701 "BIO_ctrl_get_write_guarantee() bytes");
702 goto err;
703 }
704 progress = 1;
705
706 if (debug)
707 printf("C->S relaying: %d bytes\n", (int)num);
708 }
709 }
710 while (r1 && r2);
711
712 /* server to client */
713 do
714 {
715 r1 = BIO_ctrl_pending(server_io);
716 r2 = BIO_ctrl_get_write_guarantee(client_io);
717
718 num = r1;
719 if (r2 < num)
720 num = r2;
721 if (num)
722 {
723 if (sizeof buf < num)
724 num = sizeof buf;
725 if (INT_MAX < num)
726 num = INT_MAX;
727
728 r = BIO_read(server_io, buf, (int)num);
729 if (r != (int)num) /* can't happen */
730 {
731 fprintf(stderr, "ERROR: BIO_read could not read "
732 "BIO_ctrl_pending() bytes");
733 goto err;
734 }
735 r = BIO_write(client_io, buf, (int)num);
736 if (r != (int)num) /* can't happen */
737 {
738 fprintf(stderr, "ERROR: BIO_write could not write "
739 "BIO_ctrl_get_write_guarantee() bytes");
740 goto err;
741 }
742 progress = 1;
743
744 if (debug)
745 printf("S->C relaying: %d bytes\n", (int)num);
746 }
747 }
748 while (r1 && r2);
749
750 if (!progress && !prev_progress)
751 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
95d29597
BM
752 {
753 fprintf(stderr, "ERROR: got stuck\n");
d7fcc7f6
BM
754 if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
755 {
756 fprintf(stderr, "This can happen for SSL2 because "
757 "CLIENT-FINISHED and SERVER-VERIFY are written \n"
758 "concurrently ...");
759 if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
760 && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
761 {
762 fprintf(stderr, " ok.\n");
763 goto end;
764 }
765 }
766 fprintf(stderr, " ERROR.\n");
95d29597
BM
767 goto err;
768 }
769 prev_progress = progress;
770 }
771 }
772 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
773
774 ciph = SSL_get_current_cipher(c_ssl);
775 if (verbose)
776 fprintf(stdout,"DONE via BIO pair, protocol %s, cipher %s, %s\n",
777 SSL_get_version(c_ssl),
778 SSL_CIPHER_get_version(ciph),
779 SSL_CIPHER_get_name(ciph));
d7fcc7f6 780 end:
95d29597
BM
781 ret = 0;
782
783 err:
784 ERR_print_errors(bio_err);
785
786 if (server)
787 BIO_free(server);
788 if (server_io)
789 BIO_free(server_io);
790 if (client)
791 BIO_free(client);
792 if (client_io)
793 BIO_free(client_io);
794 if (s_ssl_bio)
795 BIO_free(s_ssl_bio);
796 if (c_ssl_bio)
797 BIO_free(c_ssl_bio);
798
799 return ret;
800 }
801
802
d02b48c6
RE
803#define W_READ 1
804#define W_WRITE 2
805#define C_DONE 1
806#define S_DONE 2
807
6b691a5c 808int doit(SSL *s_ssl, SSL *c_ssl, long count)
d02b48c6 809 {
58964a49
RE
810 MS_STATIC char cbuf[1024*8],sbuf[1024*8];
811 long cw_num=count,cr_num=count;
812 long sw_num=count,sr_num=count;
d02b48c6 813 int ret=1;
d02b48c6
RE
814 BIO *c_to_s=NULL;
815 BIO *s_to_c=NULL;
816 BIO *c_bio=NULL;
817 BIO *s_bio=NULL;
818 int c_r,c_w,s_r,s_w;
819 int c_want,s_want;
58964a49 820 int i,j;
d02b48c6
RE
821 int done=0;
822 int c_write,s_write;
823 int do_server=0,do_client=0;
824 SSL_CIPHER *ciph;
825
d02b48c6
RE
826 c_to_s=BIO_new(BIO_s_mem());
827 s_to_c=BIO_new(BIO_s_mem());
828 if ((s_to_c == NULL) || (c_to_s == NULL))
829 {
830 ERR_print_errors(bio_err);
831 goto err;
832 }
833
834 c_bio=BIO_new(BIO_f_ssl());
835 s_bio=BIO_new(BIO_f_ssl());
836 if ((c_bio == NULL) || (s_bio == NULL))
837 {
838 ERR_print_errors(bio_err);
839 goto err;
840 }
841
842 SSL_set_connect_state(c_ssl);
843 SSL_set_bio(c_ssl,s_to_c,c_to_s);
58964a49 844 BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
d02b48c6
RE
845
846 SSL_set_accept_state(s_ssl);
847 SSL_set_bio(s_ssl,c_to_s,s_to_c);
58964a49 848 BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
d02b48c6
RE
849
850 c_r=0; s_r=1;
851 c_w=1; s_w=0;
852 c_want=W_WRITE;
853 s_want=0;
854 c_write=1,s_write=0;
855
856 /* We can always do writes */
857 for (;;)
858 {
859 do_server=0;
860 do_client=0;
861
862 i=(int)BIO_pending(s_bio);
863 if ((i && s_r) || s_w) do_server=1;
864
865 i=(int)BIO_pending(c_bio);
866 if ((i && c_r) || c_w) do_client=1;
867
58964a49 868 if (do_server && debug)
d02b48c6
RE
869 {
870 if (SSL_in_init(s_ssl))
871 printf("server waiting in SSL_accept - %s\n",
872 SSL_state_string_long(s_ssl));
58964a49 873/* else if (s_write)
d02b48c6 874 printf("server:SSL_write()\n");
58964a49
RE
875 else
876 printf("server:SSL_read()\n"); */
d02b48c6
RE
877 }
878
58964a49 879 if (do_client && debug)
d02b48c6
RE
880 {
881 if (SSL_in_init(c_ssl))
882 printf("client waiting in SSL_connect - %s\n",
883 SSL_state_string_long(c_ssl));
58964a49 884/* else if (c_write)
d02b48c6
RE
885 printf("client:SSL_write()\n");
886 else
58964a49 887 printf("client:SSL_read()\n"); */
d02b48c6
RE
888 }
889
890 if (!do_client && !do_server)
891 {
892 fprintf(stdout,"ERROR IN STARTUP\n");
893 ERR_print_errors(bio_err);
894 break;
895 }
896 if (do_client && !(done & C_DONE))
897 {
898 if (c_write)
899 {
58964a49
RE
900 j=(cw_num > (long)sizeof(cbuf))
901 ?sizeof(cbuf):(int)cw_num;
902 i=BIO_write(c_bio,cbuf,j);
d02b48c6
RE
903 if (i < 0)
904 {
905 c_r=0;
906 c_w=0;
907 if (BIO_should_retry(c_bio))
908 {
909 if (BIO_should_read(c_bio))
910 c_r=1;
911 if (BIO_should_write(c_bio))
912 c_w=1;
913 }
914 else
915 {
916 fprintf(stderr,"ERROR in CLIENT\n");
917 ERR_print_errors(bio_err);
918 goto err;
919 }
920 }
921 else if (i == 0)
922 {
923 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
924 goto err;
925 }
926 else
927 {
58964a49
RE
928 if (debug)
929 printf("client wrote %d\n",i);
d02b48c6 930 /* ok */
58964a49 931 s_r=1;
d02b48c6 932 c_write=0;
58964a49 933 cw_num-=i;
d02b48c6
RE
934 }
935 }
936 else
937 {
58964a49 938 i=BIO_read(c_bio,cbuf,sizeof(cbuf));
d02b48c6
RE
939 if (i < 0)
940 {
941 c_r=0;
942 c_w=0;
943 if (BIO_should_retry(c_bio))
944 {
945 if (BIO_should_read(c_bio))
946 c_r=1;
947 if (BIO_should_write(c_bio))
948 c_w=1;
949 }
950 else
951 {
952 fprintf(stderr,"ERROR in CLIENT\n");
953 ERR_print_errors(bio_err);
954 goto err;
955 }
956 }
957 else if (i == 0)
958 {
959 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
960 goto err;
961 }
962 else
963 {
58964a49
RE
964 if (debug)
965 printf("client read %d\n",i);
966 cr_num-=i;
967 if (sw_num > 0)
968 {
969 s_write=1;
970 s_w=1;
971 }
972 if (cr_num <= 0)
973 {
974 s_write=1;
975 s_w=1;
976 done=S_DONE|C_DONE;
977 }
d02b48c6
RE
978 }
979 }
980 }
981
982 if (do_server && !(done & S_DONE))
983 {
984 if (!s_write)
985 {
58964a49 986 i=BIO_read(s_bio,sbuf,sizeof(cbuf));
d02b48c6
RE
987 if (i < 0)
988 {
989 s_r=0;
990 s_w=0;
991 if (BIO_should_retry(s_bio))
992 {
993 if (BIO_should_read(s_bio))
994 s_r=1;
995 if (BIO_should_write(s_bio))
996 s_w=1;
997 }
998 else
999 {
1000 fprintf(stderr,"ERROR in SERVER\n");
1001 ERR_print_errors(bio_err);
1002 goto err;
1003 }
1004 }
1005 else if (i == 0)
1006 {
1007 ERR_print_errors(bio_err);
1008 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
1009 goto err;
1010 }
1011 else
1012 {
58964a49
RE
1013 if (debug)
1014 printf("server read %d\n",i);
1015 sr_num-=i;
1016 if (cw_num > 0)
1017 {
1018 c_write=1;
1019 c_w=1;
1020 }
1021 if (sr_num <= 0)
1022 {
1023 s_write=1;
1024 s_w=1;
1025 c_write=0;
1026 }
d02b48c6
RE
1027 }
1028 }
1029 else
1030 {
58964a49
RE
1031 j=(sw_num > (long)sizeof(sbuf))?
1032 sizeof(sbuf):(int)sw_num;
1033 i=BIO_write(s_bio,sbuf,j);
d02b48c6
RE
1034 if (i < 0)
1035 {
1036 s_r=0;
1037 s_w=0;
1038 if (BIO_should_retry(s_bio))
1039 {
1040 if (BIO_should_read(s_bio))
1041 s_r=1;
1042 if (BIO_should_write(s_bio))
1043 s_w=1;
1044 }
1045 else
1046 {
1047 fprintf(stderr,"ERROR in SERVER\n");
1048 ERR_print_errors(bio_err);
1049 goto err;
1050 }
1051 }
1052 else if (i == 0)
1053 {
1054 ERR_print_errors(bio_err);
1055 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
1056 goto err;
1057 }
1058 else
1059 {
58964a49
RE
1060 if (debug)
1061 printf("server wrote %d\n",i);
1062 sw_num-=i;
d02b48c6 1063 s_write=0;
58964a49
RE
1064 c_r=1;
1065 if (sw_num <= 0)
1066 done|=S_DONE;
d02b48c6
RE
1067 }
1068 }
1069 }
1070
1071 if ((done & S_DONE) && (done & C_DONE)) break;
1072 }
1073
1074 ciph=SSL_get_current_cipher(c_ssl);
58964a49
RE
1075 if (verbose)
1076 fprintf(stdout,"DONE, protocol %s, cipher %s, %s\n",
1077 SSL_get_version(c_ssl),
1078 SSL_CIPHER_get_version(ciph),
1079 SSL_CIPHER_get_name(ciph));
d02b48c6
RE
1080 ret=0;
1081err:
1082 /* We have to set the BIO's to NULL otherwise they will be
1083 * Free()ed twice. Once when th s_ssl is SSL_free()ed and
1084 * again when c_ssl is SSL_free()ed.
1085 * This is a hack required because s_ssl and c_ssl are sharing the same
1086 * BIO structure and SSL_set_bio() and SSL_free() automatically
1087 * BIO_free non NULL entries.
1088 * You should not normally do this or be required to do this */
1089 if (s_ssl != NULL)
1090 {
1091 s_ssl->rbio=NULL;
1092 s_ssl->wbio=NULL;
1093 }
1094 if (c_ssl != NULL)
1095 {
1096 c_ssl->rbio=NULL;
1097 c_ssl->wbio=NULL;
1098 }
1099
1100 if (c_to_s != NULL) BIO_free(c_to_s);
1101 if (s_to_c != NULL) BIO_free(s_to_c);
58964a49
RE
1102 if (c_bio != NULL) BIO_free_all(c_bio);
1103 if (s_bio != NULL) BIO_free_all(s_bio);
d02b48c6
RE
1104 return(ret);
1105 }
1106
396f6314 1107static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
d02b48c6
RE
1108 {
1109 char *s,buf[256];
1110
1111 s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,256);
1112 if (s != NULL)
1113 {
1114 if (ok)
1115 fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
1116 else
1117 fprintf(stderr,"depth=%d error=%d %s\n",
1118 ctx->error_depth,ctx->error,buf);
1119 }
1120
1121 if (ok == 0)
1122 {
1123 switch (ctx->error)
1124 {
1125 case X509_V_ERR_CERT_NOT_YET_VALID:
1126 case X509_V_ERR_CERT_HAS_EXPIRED:
1127 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1128 ok=1;
1129 }
1130 }
1131
1132 return(ok);
1133 }
1134
58964a49 1135#ifndef NO_DH
d02b48c6
RE
1136static unsigned char dh512_p[]={
1137 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
1138 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
1139 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
1140 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
1141 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
1142 0x47,0x74,0xE8,0x33,
1143 };
1144static unsigned char dh512_g[]={
1145 0x02,
1146 };
1147
6b691a5c 1148static DH *get_dh512(void)
d02b48c6
RE
1149 {
1150 DH *dh=NULL;
1151
d02b48c6
RE
1152 if ((dh=DH_new()) == NULL) return(NULL);
1153 dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
1154 dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
1155 if ((dh->p == NULL) || (dh->g == NULL))
1156 return(NULL);
d02b48c6
RE
1157 return(dh);
1158 }
58964a49
RE
1159#endif
1160
79df9d62 1161#ifndef NO_RSA
df63a389 1162static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
58964a49
RE
1163 {
1164 static RSA *rsa_tmp=NULL;
1165
1166 if (rsa_tmp == NULL)
1167 {
60e31c3a 1168 BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
d58d092b 1169 (void)BIO_flush(bio_err);
60e31c3a 1170 rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
58964a49 1171 BIO_printf(bio_err,"\n");
d58d092b 1172 (void)BIO_flush(bio_err);
58964a49
RE
1173 }
1174 return(rsa_tmp);
1175 }
79df9d62 1176#endif