For the operating systems where it matters, it is sometimes good to
[openssl.git] / ssl / ssltest.c
index 5eda38e676d1cdb5e0eef29c561bfba713c9b1f9..4763f2a6d78f3a22d643fcd19dc241fb7705dea6 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 #include "openssl/e_os.h"
 
 #include <openssl/bio.h>
 #include <openssl/crypto.h>
+#include <openssl/evp.h>
 #include <openssl/x509.h>
 #include <openssl/ssl.h>
 #include <openssl/err.h>
@@ -86,6 +88,7 @@
 static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
 #ifndef NO_RSA
 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
+static void free_tmp_rsa(void);
 #endif
 #ifndef NO_DH
 static DH *get_dh512(void);
@@ -108,7 +111,7 @@ static int s_nbio=0;
 
 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
 
-int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes);
+int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
 int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
 static void sv_usage(void)
        {
@@ -137,12 +140,58 @@ static void sv_usage(void)
 #endif
        fprintf(stderr," -CApath arg   - PEM format directory of CA's\n");
        fprintf(stderr," -CAfile arg   - PEM format file of CA's\n");
-       fprintf(stderr," -cert arg     - Certificate file\n");
-       fprintf(stderr," -s_cert arg   - Just the server certificate file\n");
-       fprintf(stderr," -c_cert arg   - Just the client certificate file\n");
+       fprintf(stderr," -cert arg     - Server certificate file\n");
+       fprintf(stderr," -key arg      - Server key file (default: same as -cert)\n");
+       fprintf(stderr," -c_cert arg   - Client certificate file\n");
+       fprintf(stderr," -c_key arg    - Client key file (default: same as -c_cert)\n");
        fprintf(stderr," -cipher arg   - The cipher list\n");
        fprintf(stderr," -bio_pair     - Use BIO pairs\n");
        fprintf(stderr," -f            - Test even cases that can't work\n");
+       fprintf(stderr," -time         - measure processor time used by client and server\n");
+       }
+
+static void print_details(SSL *c_ssl, const char *prefix)
+       {
+       SSL_CIPHER *ciph;
+       X509 *cert;
+               
+       ciph=SSL_get_current_cipher(c_ssl);
+       BIO_printf(bio_stdout,"%s%s, cipher %s %s",
+               prefix,
+               SSL_get_version(c_ssl),
+               SSL_CIPHER_get_version(ciph),
+               SSL_CIPHER_get_name(ciph));
+       cert=SSL_get_peer_certificate(c_ssl);
+       if (cert != NULL)
+               {
+               EVP_PKEY *pkey = X509_get_pubkey(cert);
+               if (pkey != NULL)
+                       {
+                       if (0) 
+                               ;
+#ifndef NO_RSA
+                       else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
+                               && pkey->pkey.rsa->n != NULL)
+                               {
+                               BIO_printf(bio_stdout, ", %d bit RSA",
+                                       BN_num_bits(pkey->pkey.rsa->n));
+                               }
+#endif
+#ifndef NO_DSA
+                       else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
+                               && pkey->pkey.dsa->p != NULL)
+                               {
+                               BIO_printf(bio_stdout, ", %d bit DSA",
+                                       BN_num_bits(pkey->pkey.dsa->p));
+                               }
+#endif
+                       EVP_PKEY_free(pkey);
+                       }
+               X509_free(cert);
+               }
+       /* The SSL API does not allow us to look at temporary RSA/DH keys,
+        * otherwise we should print their lengths too */
+       BIO_printf(bio_stdout,"\n");
        }
 
 int main(int argc, char *argv[])
@@ -155,19 +204,23 @@ int main(int argc, char *argv[])
        int client_auth=0;
        int server_auth=0,i;
        char *server_cert=TEST_SERVER_CERT;
+       char *server_key=NULL;
        char *client_cert=TEST_CLIENT_CERT;
+       char *client_key=NULL;
        SSL_CTX *s_ctx=NULL;
        SSL_CTX *c_ctx=NULL;
        SSL_METHOD *meth=NULL;
        SSL *c_ssl,*s_ssl;
        int number=1,reuse=0;
        long bytes=1L;
-       SSL_CIPHER *ciph;
 #ifndef NO_DH
        DH *dh;
        int dhe1024 = 0, dhe1024dsa = 0;
 #endif
        int no_dhe = 0;
+       int print_time = 0;
+       clock_t s_time = 0, c_time = 0;
+
        verbose = 0;
        debug = 0;
        cipher = 0;
@@ -233,11 +286,26 @@ int main(int argc, char *argv[])
                        if (--argc < 1) goto bad;
                        server_cert= *(++argv);
                        }
+               else if (strcmp(*argv,"-key") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       server_key= *(++argv);
+                       }
+               else if (strcmp(*argv,"-s_key") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       server_key= *(++argv);
+                       }
                else if (strcmp(*argv,"-c_cert") == 0)
                        {
                        if (--argc < 1) goto bad;
                        client_cert= *(++argv);
                        }
+               else if (strcmp(*argv,"-c_key") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       client_key= *(++argv);
+                       }
                else if (strcmp(*argv,"-cipher") == 0)
                        {
                        if (--argc < 1) goto bad;
@@ -261,6 +329,10 @@ int main(int argc, char *argv[])
                        {
                        force = 1;
                        }
+               else if (strcmp(*argv,"-time") == 0)
+                       {
+                       print_time = 1;
+                       }
                else
                        {
                        fprintf(stderr,"unknown option %s\n",*argv);
@@ -279,15 +351,24 @@ bad:
 
        if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
                {
-               fprintf(stderr, "This case cannot work.  Use -f switch to perform "
-                       "the test anyway\n"
-                       "(and -d to see what happens, "
-                       "and -bio_pair to really make it happen :-)\n"
-                       "or add one of -ssl2, -ssl3, -tls1, -reuse to "
-                       "avoid protocol mismatch.\n");
+               fprintf(stderr, "This case cannot work.  Use -f to perform "
+                       "the test anyway (and\n-d to see what happens), "
+                       "or add one of -ssl2, -ssl3, -tls1, -reuse\n"
+                       "to avoid protocol mismatch.\n");
                exit(1);
                }
 
+       if (print_time)
+               {
+               if (!bio_pair)
+                       {
+                       fprintf(stderr, "Using BIO pair (-bio_pair)\n");
+                       bio_pair = 1;
+                       }
+               if (number < 50 && !force)
+                       fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
+               }
+
 /*     if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
 
        SSL_library_init();
@@ -354,8 +435,8 @@ bad:
                {
                ERR_print_errors(bio_err);
                }
-       else if (!SSL_CTX_use_PrivateKey_file(s_ctx,server_cert,
-               SSL_FILETYPE_PEM))
+       else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
+               (server_key?server_key:server_cert), SSL_FILETYPE_PEM))
                {
                ERR_print_errors(bio_err);
                goto end;
@@ -365,7 +446,8 @@ bad:
                {
                SSL_CTX_use_certificate_file(c_ctx,client_cert,
                        SSL_FILETYPE_PEM);
-               SSL_CTX_use_PrivateKey_file(c_ctx,client_cert,
+               SSL_CTX_use_PrivateKey_file(c_ctx,
+                       (client_key?client_key:client_cert),
                        SSL_FILETYPE_PEM);
                }
 
@@ -405,21 +487,38 @@ bad:
                {
                if (!reuse) SSL_set_session(c_ssl,NULL);
                if (bio_pair)
-                       ret=doit_biopair(s_ssl,c_ssl,bytes);
+                       ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);
                else
                        ret=doit(s_ssl,c_ssl,bytes);
                }
 
        if (!verbose)
                {
-               ciph=SSL_get_current_cipher(c_ssl);
-               BIO_printf(bio_stdout,"Protocol %s, cipher %s, %s\n",
-                       SSL_get_version(c_ssl),
-                       SSL_CIPHER_get_version(ciph),
-                       SSL_CIPHER_get_name(ciph));
+               print_details(c_ssl, "");
                }
        if ((number > 1) || (bytes > 1L))
                BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);
+       if (print_time)
+               {
+#ifdef CLOCKS_PER_SEC
+               /* "To determine the time in seconds, the value returned
+                * by the clock function should be divided by the value
+                * of the macro CLOCKS_PER_SEC."
+                *                                       -- ISO/IEC 9899 */
+               BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
+                       "Approximate total client time: %6.2f s\n",
+                       (double)s_time/CLOCKS_PER_SEC,
+                       (double)c_time/CLOCKS_PER_SEC);
+#else
+               /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
+                *                            -- cc on NeXTstep/OpenStep */
+               BIO_printf(bio_stdout,
+                       "Approximate total server time: %6.2f units\n"
+                       "Approximate total client time: %6.2f units\n",
+                       (double)s_time,
+                       (double)c_time);
+#endif
+               }
 
        SSL_free(s_ssl);
        SSL_free(c_ssl);
@@ -430,6 +529,9 @@ end:
 
        if (bio_stdout != NULL) BIO_free(bio_stdout);
 
+#ifndef NO_RSA
+       free_tmp_rsa();
+#endif
        ERR_free_strings();
        ERR_remove_state(0);
        EVP_cleanup();
@@ -438,12 +540,12 @@ end:
        EXIT(ret);
        }
 
-int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count)
+int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
+       clock_t *s_time, clock_t *c_time)
        {
        long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
        BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
        BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
-       SSL_CIPHER *ciph;
        int ret = 1;
        
        size_t bufsiz = 256; /* small buffer for testing */
@@ -516,6 +618,7 @@ int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count)
                
                        MS_STATIC char cbuf[1024*8];
                        int i, r;
+                       clock_t c_clock = clock();
 
                        if (debug)
                                if (SSL_in_init(c_ssl))
@@ -582,6 +685,16 @@ int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count)
                                        cr_num -= r;
                                        }
                                }
+
+                       /* c_time and s_time increments will typically be very small
+                        * (depending on machine speed and clock tick intervals),
+                        * but sampling over a large number of connections should
+                        * result in fairly accurate figures.  We cannot guarantee
+                        * a lot, however -- if each connection lasts for exactly
+                        * one clock tick, it will be counted only for the client
+                        * or only for the server or even not at all.
+                        */
+                       *c_time += (clock() - c_clock);
                        }
 
                        {
@@ -589,6 +702,7 @@ int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count)
                
                        MS_STATIC char sbuf[1024*8];
                        int i, r;
+                       clock_t s_clock = clock();
 
                        if (debug)
                                if (SSL_in_init(s_ssl))
@@ -652,6 +766,8 @@ int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count)
                                        sr_num -= r;
                                        }
                                }
+
+                       *s_time += (clock() - s_clock);
                        }
                        
                        {
@@ -777,13 +893,9 @@ int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count)
                }
        while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
 
-       ciph = SSL_get_current_cipher(c_ssl);
        if (verbose)
-               fprintf(stdout,"DONE via BIO pair, protocol %s, cipher %s, %s\n",
-                       SSL_get_version(c_ssl),
-                       SSL_CIPHER_get_version(ciph),
-                       SSL_CIPHER_get_name(ciph));
- end:
+               print_details(c_ssl, "DONE via BIO pair: ");
+end:
        ret = 0;
 
  err:
@@ -827,7 +939,6 @@ int doit(SSL *s_ssl, SSL *c_ssl, long count)
        int done=0;
        int c_write,s_write;
        int do_server=0,do_client=0;
-       SSL_CIPHER *ciph;
 
        c_to_s=BIO_new(BIO_s_mem());
        s_to_c=BIO_new(BIO_s_mem());
@@ -1077,16 +1188,12 @@ int doit(SSL *s_ssl, SSL *c_ssl, long count)
                if ((done & S_DONE) && (done & C_DONE)) break;
                }
 
-       ciph=SSL_get_current_cipher(c_ssl);
        if (verbose)
-               fprintf(stdout,"DONE, protocol %s, cipher %s, %s\n",
-                       SSL_get_version(c_ssl),
-                       SSL_CIPHER_get_version(ciph),
-                       SSL_CIPHER_get_name(ciph));
+               print_details(c_ssl, "DONE: ");
        ret=0;
 err:
        /* We have to set the BIO's to NULL otherwise they will be
-        * Free()ed twice.  Once when th s_ssl is SSL_free()ed and
+        * OPENSSL_free()ed twice.  Once when th s_ssl is SSL_free()ed and
         * again when c_ssl is SSL_free()ed.
         * This is a hack required because s_ssl and c_ssl are sharing the same
         * BIO structure and SSL_set_bio() and SSL_free() automatically
@@ -1139,10 +1246,10 @@ static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
        }
 
 #ifndef NO_RSA
+static RSA *rsa_tmp=NULL;
+
 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
        {
-       static RSA *rsa_tmp=NULL;
-
        if (rsa_tmp == NULL)
                {
                BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
@@ -1153,6 +1260,15 @@ static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
                }
        return(rsa_tmp);
        }
+
+static void free_tmp_rsa(void)
+       {
+       if (rsa_tmp != NULL)
+               {
+               RSA_free(rsa_tmp);
+               rsa_tmp = NULL;
+               }
+       }
 #endif
 
 #ifndef NO_DH
@@ -1162,7 +1278,7 @@ static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  *    $ openssl dhparam -C -noout -dsaparam 1024
  * (The third function has been renamed to avoid name conflicts.)
  */
-DH *get_dh512()
+static DH *get_dh512()
        {
        static unsigned char dh512_p[]={
                0xCB,0xC8,0xE1,0x86,0xD0,0x1F,0x94,0x17,0xA6,0x99,0xF0,0xC6,
@@ -1185,7 +1301,7 @@ DH *get_dh512()
        return(dh);
        }
 
-DH *get_dh1024()
+static DH *get_dh1024()
        {
        static unsigned char dh1024_p[]={
                0xF8,0x81,0x89,0x7D,0x14,0x24,0xC5,0xD1,0xE6,0xF7,0xBF,0x3A,
@@ -1213,7 +1329,7 @@ DH *get_dh1024()
        return(dh);
        }
 
-DH *get_dh1024dsa()
+static DH *get_dh1024dsa()
        {
        static unsigned char dh1024_p[]={
                0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,