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