This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / ssl / ssltest.c
1 /* ssl/ssltest.c */
2 /* Copyright (C) 1995-1997 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 #ifdef WIN16
64 #define APPS_WIN16
65 #endif
66 #include "bio.h"
67 #include "crypto.h"
68 #include "../e_os.h"
69 #include "x509.h"
70 #include "ssl.h"
71 #include "err.h"
72
73 #ifdef WIN16
74 #define APPS_WIN16
75 #include "../crypto/bio/bss_file.c"
76 #endif
77
78 #define TEST_SERVER_CERT "../apps/server.pem"
79 #define TEST_CLIENT_CERT "../apps/client.pem"
80
81 #ifndef NOPROTO
82 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
83 static DH *get_dh512(void);
84 #else
85 int MS_CALLBACK verify_callback();
86 static DH *get_dh512();
87 #endif
88
89 BIO *bio_err=NULL;
90 BIO *bio_stdout=NULL;
91
92 static char *cipher=NULL;
93 int verbose=0;
94 #ifdef FIONBIO
95 static int s_nbio=0;
96 #endif
97
98
99 #ifndef  NOPROTO
100 int doit(SSL_CTX *s_ctx,SSL_CTX *c_ctx);
101 #else
102 int doit();
103 #endif
104
105 static void sv_usage()
106         {
107         fprintf(stderr,"usage: ssltest [args ...]\n");
108         fprintf(stderr,"\n");
109         fprintf(stderr," -server_auth  - check server certificate\n");
110         fprintf(stderr," -client_auth  - do client authentication\n");
111         fprintf(stderr," -v            - more output\n");
112 #ifndef NO_SSL2
113         fprintf(stderr," -ssl2         - use SSLv2\n");
114 #endif
115 #ifndef NO_SSL3
116         fprintf(stderr," -ssl3         - use SSLv3\n");
117 #endif
118         fprintf(stderr," -CApath arg   - PEM format directory of CA's\n");
119         fprintf(stderr," -CAfile arg   - PEM format file of CA's\n");
120         }
121
122 int main(argc, argv)
123 int argc;
124 char *argv[];
125         {
126         char *CApath=NULL,*CAfile=NULL;
127         int badop=0;
128         int ssl2=0,ssl3=0,ret=1;
129         int client_auth=0;
130         int server_auth=0;
131         char *server_cert=TEST_SERVER_CERT;
132         char *client_cert=TEST_CLIENT_CERT;
133         SSL_CTX *s_ctx=NULL;
134         SSL_CTX *c_ctx=NULL;
135         SSL_METHOD *meth=NULL;
136         DH *dh;
137
138         bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
139         bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
140
141         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
142
143         argc--;
144         argv++;
145
146         while (argc >= 1)
147                 {
148                 if      (strcmp(*argv,"-server_auth") == 0)
149                         server_auth=1;
150                 else if (strcmp(*argv,"-client_auth") == 0)
151                         client_auth=1;
152                 else if (strcmp(*argv,"-v") == 0)
153                         verbose=1;
154                 else if (strcmp(*argv,"-ssl2") == 0)
155                         ssl2=1;
156                 else if (strcmp(*argv,"-ssl3") == 0)
157                         ssl3=1;
158                 else if (strcmp(*argv,"-cert") == 0)
159                         {
160                         if (--argc < 1) goto bad;
161                         server_cert= *(++argv);
162                         }
163                 else if (strcmp(*argv,"-s_cert") == 0)
164                         {
165                         if (--argc < 1) goto bad;
166                         server_cert= *(++argv);
167                         }
168                 else if (strcmp(*argv,"-c_cert") == 0)
169                         {
170                         if (--argc < 1) goto bad;
171                         client_cert= *(++argv);
172                         }
173                 else if (strcmp(*argv,"-cipher") == 0)
174                         {
175                         if (--argc < 1) goto bad;
176                         cipher= *(++argv);
177                         }
178                 else if (strcmp(*argv,"-CApath") == 0)
179                         {
180                         if (--argc < 1) goto bad;
181                         CApath= *(++argv);
182                         }
183                 else if (strcmp(*argv,"-CAfile") == 0)
184                         {
185                         if (--argc < 1) goto bad;
186                         CAfile= *(++argv);
187                         }
188                 else
189                         {
190                         fprintf(stderr,"unknown option %s\n",*argv);
191                         badop=1;
192                         break;
193                         }
194                 argc--;
195                 argv++;
196                 }
197         if (badop)
198                 {
199 bad:
200                 sv_usage();
201                 goto end;
202                 }
203
204 /*      if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
205
206         SSLeay_add_ssl_algorithms();
207         SSL_load_error_strings();
208
209 #if !defined(NO_SSL2) && !defined(NO_SSL3)
210         if (ssl2)
211                 meth=SSLv2_method();
212         else 
213         if (ssl3)
214                 meth=SSLv3_method();
215         else
216                 meth=SSLv23_method();
217 #else
218 #ifdef NO_SSL2
219         meth=SSLv3_method();
220 #else
221         meth=SSLv2_method();
222 #endif
223 #endif
224
225         c_ctx=SSL_CTX_new(meth);
226         s_ctx=SSL_CTX_new(meth);
227         if ((c_ctx == NULL) || (s_ctx == NULL))
228                 {
229                 ERR_print_errors(bio_err);
230                 goto end;
231                 }
232
233         if (cipher != NULL)
234                 {
235                 SSL_CTX_set_cipher_list(c_ctx,cipher);
236                 SSL_CTX_set_cipher_list(s_ctx,cipher);
237                 }
238
239 #ifndef NO_DH
240         dh=get_dh512();
241         SSL_CTX_set_tmp_dh(s_ctx,dh);
242         DH_free(dh);
243 #endif
244
245         if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
246                 {
247                 ERR_print_errors(bio_err);
248                 }
249         else if (!SSL_CTX_use_PrivateKey_file(s_ctx,server_cert,
250                 SSL_FILETYPE_PEM))
251                 {
252                 ERR_print_errors(bio_err);
253                 goto end;
254                 }
255
256         if (client_auth)
257                 {
258                 SSL_CTX_use_certificate_file(c_ctx,client_cert,
259                         SSL_FILETYPE_PEM);
260                 SSL_CTX_use_PrivateKey_file(c_ctx,client_cert,
261                         SSL_FILETYPE_PEM);
262                 }
263
264         if (    (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
265                 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
266                 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
267                 (!SSL_CTX_set_default_verify_paths(c_ctx)))
268                 {
269                 fprintf(stderr,"SSL_load_verify_locations\n");
270                 ERR_print_errors(bio_err);
271                 goto end;
272                 }
273
274         if (client_auth)
275                 {
276                 fprintf(stderr,"client authentication\n");
277                 SSL_CTX_set_verify(s_ctx,
278                         SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
279                         verify_callback);
280                 }
281         if (server_auth)
282                 {
283                 fprintf(stderr,"server authentication\n");
284                 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
285                         verify_callback);
286                 }
287
288         ret=doit(s_ctx,c_ctx);
289 end:
290         if (s_ctx != NULL) SSL_CTX_free(s_ctx);
291         if (c_ctx != NULL) SSL_CTX_free(c_ctx);
292
293         if (bio_stdout != NULL) BIO_free(bio_stdout);
294
295         ERR_remove_state(0);
296         EVP_cleanup();
297         CRYPTO_mem_leaks(bio_err);
298         EXIT(ret);
299         }
300
301 #define W_READ  1
302 #define W_WRITE 2
303 #define C_DONE  1
304 #define S_DONE  2
305
306 int doit(s_ctx,c_ctx)
307 SSL_CTX *s_ctx,*c_ctx;
308         {
309         static char cbuf[200],sbuf[200];
310         int ret=1;
311         SSL *c_ssl=NULL;
312         SSL *s_ssl=NULL;
313         BIO *c_to_s=NULL;
314         BIO *s_to_c=NULL;
315         BIO *c_bio=NULL;
316         BIO *s_bio=NULL;
317         int c_r,c_w,s_r,s_w;
318         int c_want,s_want;
319         int i;
320         int done=0;
321         int c_write,s_write;
322         int do_server=0,do_client=0;
323         SSL_CIPHER *ciph;
324
325         c_ssl=SSL_new(c_ctx);
326         s_ssl=SSL_new(s_ctx);
327         if ((s_ssl == NULL) || (c_ssl == NULL))
328                 {
329                 ERR_print_errors(bio_err);
330                 goto err;
331                 }
332
333         c_to_s=BIO_new(BIO_s_mem());
334         s_to_c=BIO_new(BIO_s_mem());
335         if ((s_to_c == NULL) || (c_to_s == NULL))
336                 {
337                 ERR_print_errors(bio_err);
338                 goto err;
339                 }
340
341         c_bio=BIO_new(BIO_f_ssl());
342         s_bio=BIO_new(BIO_f_ssl());
343         if ((c_bio == NULL) || (s_bio == NULL))
344                 {
345                 ERR_print_errors(bio_err);
346                 goto err;
347                 }
348
349         SSL_set_connect_state(c_ssl);
350         SSL_set_bio(c_ssl,s_to_c,c_to_s);
351         BIO_set_ssl(c_bio,c_ssl,BIO_CLOSE);
352
353         SSL_set_accept_state(s_ssl);
354         SSL_set_bio(s_ssl,c_to_s,s_to_c);
355         BIO_set_ssl(s_bio,s_ssl,BIO_CLOSE);
356
357         c_r=0; s_r=1;
358         c_w=1; s_w=0;
359         c_want=W_WRITE;
360         s_want=0;
361         c_write=1,s_write=0;
362
363         /* We can always do writes */
364         for (;;)
365                 {
366                 do_server=0;
367                 do_client=0;
368
369                 i=(int)BIO_pending(s_bio);
370                 if ((i && s_r) || s_w) do_server=1;
371
372                 i=(int)BIO_pending(c_bio);
373                 if ((i && c_r) || c_w) do_client=1;
374
375                 if (do_server && verbose)
376                         {
377                         if (SSL_in_init(s_ssl))
378                                 printf("server waiting in SSL_accept - %s\n",
379                                         SSL_state_string_long(s_ssl));
380                         else if (s_write)
381                                 printf("server:SSL_write()\n");
382                         else 
383                                 printf("server:SSL_read()\n");
384                         }
385
386                 if (do_client && verbose)
387                         {
388                         if (SSL_in_init(c_ssl))
389                                 printf("client waiting in SSL_connect - %s\n",
390                                         SSL_state_string_long(c_ssl));
391                         else if (c_write)
392                                 printf("client:SSL_write()\n");
393                         else
394                                 printf("client:SSL_read()\n");
395                         }
396
397                 if (!do_client && !do_server)
398                         {
399                         fprintf(stdout,"ERROR IN STARTUP\n");
400                         ERR_print_errors(bio_err);
401                         break;
402                         }
403                 if (do_client && !(done & C_DONE))
404                         {
405                         if (c_write)
406                                 {
407                                 i=BIO_write(c_bio,"hello from client\n",18);
408                                 if (i < 0)
409                                         {
410                                         c_r=0;
411                                         c_w=0;
412                                         if (BIO_should_retry(c_bio))
413                                                 {
414                                                 if (BIO_should_read(c_bio))
415                                                         c_r=1;
416                                                 if (BIO_should_write(c_bio))
417                                                         c_w=1;
418                                                 }
419                                         else
420                                                 {
421                                                 fprintf(stderr,"ERROR in CLIENT\n");
422                                                 ERR_print_errors(bio_err);
423                                                 goto err;
424                                                 }
425                                         }
426                                 else if (i == 0)
427                                         {
428                                         fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
429                                         goto err;
430                                         }
431                                 else
432                                         {
433                                         /* ok */
434                                         c_write=0;
435                                         }
436                                 }
437                         else
438                                 {
439                                 i=BIO_read(c_bio,cbuf,100);
440                                 if (i < 0)
441                                         {
442                                         c_r=0;
443                                         c_w=0;
444                                         if (BIO_should_retry(c_bio))
445                                                 {
446                                                 if (BIO_should_read(c_bio))
447                                                         c_r=1;
448                                                 if (BIO_should_write(c_bio))
449                                                         c_w=1;
450                                                 }
451                                         else
452                                                 {
453                                                 fprintf(stderr,"ERROR in CLIENT\n");
454                                                 ERR_print_errors(bio_err);
455                                                 goto err;
456                                                 }
457                                         }
458                                 else if (i == 0)
459                                         {
460                                         fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
461                                         goto err;
462                                         }
463                                 else
464                                         {
465                                         done|=C_DONE;
466                                         fprintf(stdout,"CLIENT:from server:");
467                                         fwrite(cbuf,1,i,stdout);
468                                         fflush(stdout);
469                                         }
470                                 }
471                         }
472
473                 if (do_server && !(done & S_DONE))
474                         {
475                         if (!s_write)
476                                 {
477                                 i=BIO_read(s_bio,sbuf,100);
478                                 if (i < 0)
479                                         {
480                                         s_r=0;
481                                         s_w=0;
482                                         if (BIO_should_retry(s_bio))
483                                                 {
484                                                 if (BIO_should_read(s_bio))
485                                                         s_r=1;
486                                                 if (BIO_should_write(s_bio))
487                                                         s_w=1;
488                                                 }
489                                         else
490                                                 {
491                                                 fprintf(stderr,"ERROR in SERVER\n");
492                                                 ERR_print_errors(bio_err);
493                                                 goto err;
494                                                 }
495                                         }
496                                 else if (i == 0)
497                                         {
498                                         ERR_print_errors(bio_err);
499                                         fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
500                                         goto err;
501                                         }
502                                 else
503                                         {
504                                         s_write=1;
505                                         s_w=1;
506                                         fprintf(stdout,"SERVER:from client:");
507                                         fwrite(sbuf,1,i,stdout);
508                                         fflush(stdout);
509                                         }
510                                 }
511                         else
512                                 {
513                                 i=BIO_write(s_bio,"hello from server\n",18);
514                                 if (i < 0)
515                                         {
516                                         s_r=0;
517                                         s_w=0;
518                                         if (BIO_should_retry(s_bio))
519                                                 {
520                                                 if (BIO_should_read(s_bio))
521                                                         s_r=1;
522                                                 if (BIO_should_write(s_bio))
523                                                         s_w=1;
524                                                 }
525                                         else
526                                                 {
527                                                 fprintf(stderr,"ERROR in SERVER\n");
528                                                 ERR_print_errors(bio_err);
529                                                 goto err;
530                                                 }
531                                         }
532                                 else if (i == 0)
533                                         {
534                                         ERR_print_errors(bio_err);
535                                         fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
536                                         goto err;
537                                         }
538                                 else
539                                         {
540                                         s_write=0;
541                                         s_r=1;
542                                         done|=S_DONE;
543                                         }
544                                 }
545                         }
546
547                 if ((done & S_DONE) && (done & C_DONE)) break;
548                 }
549
550         ciph=SSL_get_current_cipher(c_ssl);
551         fprintf(stdout,"DONE, used %s, %s\n",SSL_CIPHER_get_version(ciph),
552                 SSL_CIPHER_get_name(ciph));
553         ret=0;
554 err:
555         /* We have to set the BIO's to NULL otherwise they will be
556          * Free()ed twice.  Once when th s_ssl is SSL_free()ed and
557          * again when c_ssl is SSL_free()ed.
558          * This is a hack required because s_ssl and c_ssl are sharing the same
559          * BIO structure and SSL_set_bio() and SSL_free() automatically
560          * BIO_free non NULL entries.
561          * You should not normally do this or be required to do this */
562         if (s_ssl != NULL)
563                 {
564                 s_ssl->rbio=NULL;
565                 s_ssl->wbio=NULL;
566                 }
567         if (c_ssl != NULL)
568                 {
569                 c_ssl->rbio=NULL;
570                 c_ssl->wbio=NULL;
571                 }
572
573         if (c_to_s != NULL) BIO_free(c_to_s);
574         if (s_to_c != NULL) BIO_free(s_to_c);
575         if (c_bio != NULL) BIO_free(c_bio);
576         if (s_bio != NULL) BIO_free(s_bio);
577         return(ret);
578         }
579
580 int MS_CALLBACK verify_callback(ok, ctx)
581 int ok;
582 X509_STORE_CTX *ctx;
583         {
584         char *s,buf[256];
585
586         s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,256);
587         if (s != NULL)
588                 {
589                 if (ok)
590                         fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
591                 else
592                         fprintf(stderr,"depth=%d error=%d %s\n",
593                                 ctx->error_depth,ctx->error,buf);
594                 }
595
596         if (ok == 0)
597                 {
598                 switch (ctx->error)
599                         {
600                 case X509_V_ERR_CERT_NOT_YET_VALID:
601                 case X509_V_ERR_CERT_HAS_EXPIRED:
602                 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
603                         ok=1;
604                         }
605                 }
606
607         return(ok);
608         }
609
610 static unsigned char dh512_p[]={
611         0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
612         0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
613         0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
614         0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
615         0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
616         0x47,0x74,0xE8,0x33,
617         };
618 static unsigned char dh512_g[]={
619         0x02,
620         };
621
622 static DH *get_dh512()
623         {
624         DH *dh=NULL;
625
626 #ifndef NO_DH
627         if ((dh=DH_new()) == NULL) return(NULL);
628         dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
629         dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
630         if ((dh->p == NULL) || (dh->g == NULL))
631                 return(NULL);
632 #endif
633         return(dh);
634         }
635