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