2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
70 #include "../crypto/bio/bss_file.c"
73 #define TEST_SERVER_CERT "../apps/server.pem"
74 #define TEST_CLIENT_CERT "../apps/client.pem"
77 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
78 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int export,int keylength);
80 static DH *get_dh512(void);
83 int MS_CALLBACK verify_callback();
84 static RSA MS_CALLBACK *tmp_rsa_cb();
86 static DH *get_dh512();
93 static char *cipher=NULL;
102 int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
107 static void sv_usage()
109 fprintf(stderr,"usage: ssltest [args ...]\n");
110 fprintf(stderr,"\n");
111 fprintf(stderr," -server_auth - check server certificate\n");
112 fprintf(stderr," -client_auth - do client authentication\n");
113 fprintf(stderr," -v - more output\n");
114 fprintf(stderr," -d - debug output\n");
115 fprintf(stderr," -reuse - use session-id reuse\n");
116 fprintf(stderr," -num <val> - number of connections to perform\n");
117 fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n");
119 fprintf(stderr," -ssl2 - use SSLv2\n");
122 fprintf(stderr," -ssl3 - use SSLv3\n");
125 fprintf(stderr," -tls1 - use TLSv1\n");
127 fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
128 fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
129 fprintf(stderr," -cert arg - Certificate file\n");
130 fprintf(stderr," -s_cert arg - Just the server certificate file\n");
131 fprintf(stderr," -c_cert arg - Just the client certificate file\n");
132 fprintf(stderr," -cipher arg - The cipher list\n");
139 char *CApath=NULL,*CAfile=NULL;
141 int tls1=0,ssl2=0,ssl3=0,ret=1;
144 char *server_cert=TEST_SERVER_CERT;
145 char *client_cert=TEST_CLIENT_CERT;
148 SSL_METHOD *meth=NULL;
150 int number=1,reuse=0;
157 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
158 bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
160 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
167 if (strcmp(*argv,"-server_auth") == 0)
169 else if (strcmp(*argv,"-client_auth") == 0)
171 else if (strcmp(*argv,"-v") == 0)
173 else if (strcmp(*argv,"-d") == 0)
175 else if (strcmp(*argv,"-reuse") == 0)
177 else if (strcmp(*argv,"-ssl2") == 0)
179 else if (strcmp(*argv,"-tls1") == 0)
181 else if (strcmp(*argv,"-ssl3") == 0)
183 else if (strncmp(*argv,"-num",4) == 0)
185 if (--argc < 1) goto bad;
186 number= atoi(*(++argv));
187 if (number == 0) number=1;
189 else if (strcmp(*argv,"-bytes") == 0)
191 if (--argc < 1) goto bad;
192 bytes= atol(*(++argv));
193 if (bytes == 0L) bytes=1L;
195 if (argv[0][i-1] == 'k') bytes*=1024L;
196 if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
198 else if (strcmp(*argv,"-cert") == 0)
200 if (--argc < 1) goto bad;
201 server_cert= *(++argv);
203 else if (strcmp(*argv,"-s_cert") == 0)
205 if (--argc < 1) goto bad;
206 server_cert= *(++argv);
208 else if (strcmp(*argv,"-c_cert") == 0)
210 if (--argc < 1) goto bad;
211 client_cert= *(++argv);
213 else if (strcmp(*argv,"-cipher") == 0)
215 if (--argc < 1) goto bad;
218 else if (strcmp(*argv,"-CApath") == 0)
220 if (--argc < 1) goto bad;
223 else if (strcmp(*argv,"-CAfile") == 0)
225 if (--argc < 1) goto bad;
230 fprintf(stderr,"unknown option %s\n",*argv);
244 /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
247 SSL_load_error_strings();
249 #if !defined(NO_SSL2) && !defined(NO_SSL3)
259 meth=SSLv23_method();
268 c_ctx=SSL_CTX_new(meth);
269 s_ctx=SSL_CTX_new(meth);
270 if ((c_ctx == NULL) || (s_ctx == NULL))
272 ERR_print_errors(bio_err);
278 SSL_CTX_set_cipher_list(c_ctx,cipher);
279 SSL_CTX_set_cipher_list(s_ctx,cipher);
284 SSL_CTX_set_tmp_dh(s_ctx,dh);
289 SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
292 if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
294 ERR_print_errors(bio_err);
296 else if (!SSL_CTX_use_PrivateKey_file(s_ctx,server_cert,
299 ERR_print_errors(bio_err);
305 SSL_CTX_use_certificate_file(c_ctx,client_cert,
307 SSL_CTX_use_PrivateKey_file(c_ctx,client_cert,
311 if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
312 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
313 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
314 (!SSL_CTX_set_default_verify_paths(c_ctx)))
316 /* fprintf(stderr,"SSL_load_verify_locations\n"); */
317 ERR_print_errors(bio_err);
323 fprintf(stderr,"client authentication\n");
324 SSL_CTX_set_verify(s_ctx,
325 SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
330 fprintf(stderr,"server authentication\n");
331 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
335 c_ssl=SSL_new(c_ctx);
336 s_ssl=SSL_new(s_ctx);
338 for (i=0; i<number; i++)
340 if (!reuse) SSL_set_session(c_ssl,NULL);
341 ret=doit(s_ssl,c_ssl,bytes);
346 ciph=SSL_get_current_cipher(c_ssl);
347 fprintf(stdout,"Protocol %s, cipher %s, %s\n",
348 SSL_get_version(c_ssl),
349 SSL_CIPHER_get_version(ciph),
350 SSL_CIPHER_get_name(ciph));
352 if ((number > 1) || (bytes > 1L))
353 printf("%d handshakes of %ld bytes done\n",number,bytes);
359 if (s_ctx != NULL) SSL_CTX_free(s_ctx);
360 if (c_ctx != NULL) SSL_CTX_free(c_ctx);
362 if (bio_stdout != NULL) BIO_free(bio_stdout);
367 CRYPTO_mem_leaks(bio_err);
376 int doit(s_ssl,c_ssl,count)
380 MS_STATIC char cbuf[1024*8],sbuf[1024*8];
381 long cw_num=count,cr_num=count;
382 long sw_num=count,sr_num=count;
393 int do_server=0,do_client=0;
396 c_to_s=BIO_new(BIO_s_mem());
397 s_to_c=BIO_new(BIO_s_mem());
398 if ((s_to_c == NULL) || (c_to_s == NULL))
400 ERR_print_errors(bio_err);
404 c_bio=BIO_new(BIO_f_ssl());
405 s_bio=BIO_new(BIO_f_ssl());
406 if ((c_bio == NULL) || (s_bio == NULL))
408 ERR_print_errors(bio_err);
412 SSL_set_connect_state(c_ssl);
413 SSL_set_bio(c_ssl,s_to_c,c_to_s);
414 BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
416 SSL_set_accept_state(s_ssl);
417 SSL_set_bio(s_ssl,c_to_s,s_to_c);
418 BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
426 /* We can always do writes */
432 i=(int)BIO_pending(s_bio);
433 if ((i && s_r) || s_w) do_server=1;
435 i=(int)BIO_pending(c_bio);
436 if ((i && c_r) || c_w) do_client=1;
438 if (do_server && debug)
440 if (SSL_in_init(s_ssl))
441 printf("server waiting in SSL_accept - %s\n",
442 SSL_state_string_long(s_ssl));
444 printf("server:SSL_write()\n");
446 printf("server:SSL_read()\n"); */
449 if (do_client && debug)
451 if (SSL_in_init(c_ssl))
452 printf("client waiting in SSL_connect - %s\n",
453 SSL_state_string_long(c_ssl));
455 printf("client:SSL_write()\n");
457 printf("client:SSL_read()\n"); */
460 if (!do_client && !do_server)
462 fprintf(stdout,"ERROR IN STARTUP\n");
463 ERR_print_errors(bio_err);
466 if (do_client && !(done & C_DONE))
470 j=(cw_num > (long)sizeof(cbuf))
471 ?sizeof(cbuf):(int)cw_num;
472 i=BIO_write(c_bio,cbuf,j);
477 if (BIO_should_retry(c_bio))
479 if (BIO_should_read(c_bio))
481 if (BIO_should_write(c_bio))
486 fprintf(stderr,"ERROR in CLIENT\n");
487 ERR_print_errors(bio_err);
493 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
499 printf("client wrote %d\n",i);
508 i=BIO_read(c_bio,cbuf,sizeof(cbuf));
513 if (BIO_should_retry(c_bio))
515 if (BIO_should_read(c_bio))
517 if (BIO_should_write(c_bio))
522 fprintf(stderr,"ERROR in CLIENT\n");
523 ERR_print_errors(bio_err);
529 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
535 printf("client read %d\n",i);
552 if (do_server && !(done & S_DONE))
556 i=BIO_read(s_bio,sbuf,sizeof(cbuf));
561 if (BIO_should_retry(s_bio))
563 if (BIO_should_read(s_bio))
565 if (BIO_should_write(s_bio))
570 fprintf(stderr,"ERROR in SERVER\n");
571 ERR_print_errors(bio_err);
577 ERR_print_errors(bio_err);
578 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
584 printf("server read %d\n",i);
601 j=(sw_num > (long)sizeof(sbuf))?
602 sizeof(sbuf):(int)sw_num;
603 i=BIO_write(s_bio,sbuf,j);
608 if (BIO_should_retry(s_bio))
610 if (BIO_should_read(s_bio))
612 if (BIO_should_write(s_bio))
617 fprintf(stderr,"ERROR in SERVER\n");
618 ERR_print_errors(bio_err);
624 ERR_print_errors(bio_err);
625 fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
631 printf("server wrote %d\n",i);
641 if ((done & S_DONE) && (done & C_DONE)) break;
644 ciph=SSL_get_current_cipher(c_ssl);
646 fprintf(stdout,"DONE, protocol %s, cipher %s, %s\n",
647 SSL_get_version(c_ssl),
648 SSL_CIPHER_get_version(ciph),
649 SSL_CIPHER_get_name(ciph));
652 /* We have to set the BIO's to NULL otherwise they will be
653 * Free()ed twice. Once when th s_ssl is SSL_free()ed and
654 * again when c_ssl is SSL_free()ed.
655 * This is a hack required because s_ssl and c_ssl are sharing the same
656 * BIO structure and SSL_set_bio() and SSL_free() automatically
657 * BIO_free non NULL entries.
658 * You should not normally do this or be required to do this */
670 if (c_to_s != NULL) BIO_free(c_to_s);
671 if (s_to_c != NULL) BIO_free(s_to_c);
672 if (c_bio != NULL) BIO_free_all(c_bio);
673 if (s_bio != NULL) BIO_free_all(s_bio);
677 int MS_CALLBACK verify_callback(ok, ctx)
683 s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,256);
687 fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
689 fprintf(stderr,"depth=%d error=%d %s\n",
690 ctx->error_depth,ctx->error,buf);
697 case X509_V_ERR_CERT_NOT_YET_VALID:
698 case X509_V_ERR_CERT_HAS_EXPIRED:
699 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
708 static unsigned char dh512_p[]={
709 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
710 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
711 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
712 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
713 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
716 static unsigned char dh512_g[]={
720 static DH *get_dh512()
724 if ((dh=DH_new()) == NULL) return(NULL);
725 dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
726 dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
727 if ((dh->p == NULL) || (dh->g == NULL))
733 static RSA MS_CALLBACK *tmp_rsa_cb(s,export,keylength)
738 static RSA *rsa_tmp=NULL;
742 BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
745 rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
747 BIO_printf(bio_err,"\n");