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