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