make update
[openssl.git] / crypto / threads / mttest.c
index be395f2bc4adaccd35a5f568894e334aa9c5164d..7142e4edc77cf8086195ee49cd5d8853ae4d1576 100644 (file)
@@ -63,7 +63,7 @@
 #ifdef LINUX
 #include <typedefs.h>
 #endif
-#ifdef WIN32
+#ifdef OPENSSL_SYS_WIN32
 #include <windows.h>
 #endif
 #ifdef SOLARIS
 #include <ulocks.h>
 #include <sys/prctl.h>
 #endif
-#include "lhash.h"
-#include "crypto.h"
-#include "buffer.h"
-#include "../e_os.h"
-#include "x509.h"
-#include "ssl.h"
-#include "err.h"
-
-#ifdef NO_FP_API
+#ifdef PTHREADS
+#include <pthread.h>
+#endif
+#include <openssl/lhash.h>
+#include <openssl/crypto.h>
+#include <openssl/buffer.h>
+#include "../../e_os.h"
+#include <openssl/x509.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+#include <openssl/rand.h>
+
+#ifdef OPENSSL_NO_FP_API
 #define APPS_WIN16
-#include "../crypto/buffer/bss_file.c"
+#include "../buffer/bss_file.c"
 #endif
 
-#define TEST_SERVER_CERT "../apps/server.pem"
-#define TEST_CLIENT_CERT "../apps/client.pem"
+#define TEST_SERVER_CERT "../../apps/server.pem"
+#define TEST_CLIENT_CERT "../../apps/client.pem"
 
 #define MAX_THREAD_NUMBER      100
 
-#ifndef NOPROTO
-int MS_CALLBACK verify_callback(int ok, X509 *xs, X509 *xi, int depth,
-       int error,char *arg);
+int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *xs);
 void thread_setup(void);
 void thread_cleanup(void);
 void do_threads(SSL_CTX *s_ctx,SSL_CTX *c_ctx);
@@ -108,23 +110,6 @@ unsigned long irix_thread_id(void );
 unsigned long solaris_thread_id(void );
 unsigned long pthreads_thread_id(void );
 
-#else
-int MS_CALLBACK verify_callback();
-void thread_setup();
-void thread_cleanup();
-void do_threads();
-
-void irix_locking_callback();
-void solaris_locking_callback();
-void win32_locking_callback();
-void pthreads_locking_callback();
-
-unsigned long irix_thread_id();
-unsigned long solaris_thread_id();
-unsigned long pthreads_thread_id();
-
-#endif
-
 BIO *bio_err=NULL;
 BIO *bio_stdout=NULL;
 
@@ -139,15 +124,10 @@ int number_of_loops=10;
 int reconnect=0;
 int cache_stats=0;
 
-#ifndef  NOPROTO
-int doit(char *ctx[4]);
-#else
-int doit();
-#endif
+static const char rnd_seed[] = "string to make the random number generator think it has entropy";
 
-static void print_stats(fp,ctx)
-FILE *fp;
-SSL_CTX *ctx;
+int doit(char *ctx[4]);
+static void print_stats(FILE *fp, SSL_CTX *ctx)
 {
        fprintf(fp,"%4ld items in the session cache\n",
                SSL_CTX_sess_number(ctx));
@@ -164,7 +144,7 @@ SSL_CTX *ctx;
        fprintf(fp,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ctx));
        }
 
-static void sv_usage()
+static void sv_usage(void)
        {
        fprintf(stderr,"usage: ssltest [args ...]\n");
        fprintf(stderr,"\n");
@@ -182,9 +162,7 @@ static void sv_usage()
        fprintf(stderr," -ssl3         - just SSLv3n\n");
        }
 
-int main(argc, argv)
-int argc;
-char *argv[];
+int main(int argc, char *argv[])
        {
        char *CApath=NULL,*CAfile=NULL;
        int badop=0;
@@ -197,6 +175,8 @@ char *argv[];
        char *ccert=TEST_CLIENT_CERT;
        SSL_METHOD *ssl_method=SSLv23_method();
 
+       RAND_seed(rnd_seed, sizeof rnd_seed);
+
        if (bio_err == NULL)
                bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
        if (bio_stdout == NULL)
@@ -271,7 +251,7 @@ bad:
        if (cipher == NULL) cipher=getenv("SSL_CIPHER");
 
        SSL_load_error_strings();
-       SSLeay_add_ssl_algorithms();
+       OpenSSL_add_ssl_algorithms();
 
        c_ctx=SSL_CTX_new(ssl_method);
        s_ctx=SSL_CTX_new(ssl_method);
@@ -286,8 +266,15 @@ bad:
        SSL_CTX_set_session_cache_mode(c_ctx,
                SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
 
-       SSL_CTX_use_certificate_file(s_ctx,scert,SSL_FILETYPE_PEM);
-       SSL_CTX_use_RSAPrivateKey_file(s_ctx,scert,SSL_FILETYPE_PEM);
+       if (!SSL_CTX_use_certificate_file(s_ctx,scert,SSL_FILETYPE_PEM))
+               {
+               ERR_print_errors(bio_err);
+               }
+       else if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx,scert,SSL_FILETYPE_PEM))
+               {
+               ERR_print_errors(bio_err);
+               goto end;
+               }
 
        if (client_auth)
                {
@@ -358,8 +345,7 @@ end:
 #define C_DONE 1
 #define S_DONE 2
 
-int ndoit(ssl_ctx)
-SSL_CTX *ssl_ctx[2];
+int ndoit(SSL_CTX *ssl_ctx[2])
        {
        int i;
        int ret;
@@ -405,8 +391,7 @@ SSL_CTX *ssl_ctx[2];
        return(0);
        }
 
-int doit(ctx)
-char *ctx[4];
+int doit(char *ctx[4])
        {
        SSL_CTX *s_ctx,*c_ctx;
        static char cbuf[200],sbuf[200];
@@ -518,6 +503,7 @@ char *ctx[4];
                                        else
                                                {
                                                fprintf(stderr,"ERROR in CLIENT\n");
+                                               ERR_print_errors_fp(stderr);
                                                return(1);
                                                }
                                        }
@@ -549,6 +535,7 @@ char *ctx[4];
                                        else
                                                {
                                                fprintf(stderr,"ERROR in CLIENT\n");
+                                               ERR_print_errors_fp(stderr);
                                                return(1);
                                                }
                                        }
@@ -681,38 +668,39 @@ err:
        return(0);
        }
 
-int MS_CALLBACK verify_callback(ok, xs, xi, depth, error, arg)
-int ok;
-X509 *xs;
-X509 *xi;
-int depth;
-int error;
-char *arg;
+int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
        {
-       char buf[256];
+       char *s, buf[256];
 
        if (verbose)
                {
-               X509_NAME_oneline(X509_get_subject_name(xs),buf,256);
-               if (ok)
-                       fprintf(stderr,"depth=%d %s\n",depth,buf);
-               else
-                       fprintf(stderr,"depth=%d error=%d %s\n",depth,error,buf);
+               s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
+                                   buf,256);
+               if (s != NULL)
+                       {
+                       if (ok)
+                               fprintf(stderr,"depth=%d %s\n",
+                                       ctx->error_depth,buf);
+                       else
+                               fprintf(stderr,"depth=%d error=%d %s\n",
+                                       ctx->error_depth,ctx->error,buf);
+                       }
                }
        return(ok);
        }
 
 #define THREAD_STACK_SIZE (16*1024)
 
-#ifdef WIN32
+#ifdef OPENSSL_SYS_WIN32
 
-static HANDLE lock_cs[CRYPTO_NUM_LOCKS];
+static HANDLE *lock_cs;
 
-void thread_setup()
+void thread_setup(void)
        {
        int i;
 
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+       lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
+       for (i=0; i<CRYPTO_num_locks(); i++)
                {
                lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
                }
@@ -721,20 +709,17 @@ void thread_setup()
        /* id callback defined */
        }
 
-void thread_cleanup()
+void thread_cleanup(void)
        {
        int i;
 
        CRYPTO_set_locking_callback(NULL);
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+       for (i=0; i<CRYPTO_num_locks(); i++)
                CloseHandle(lock_cs[i]);
+       OPENSSL_free(lock_cs);
        }
 
-void win32_locking_callback(mode,type,file,line)
-int mode;
-int type;
-char *file;
-int line;
+void win32_locking_callback(int mode, int type, char *file, int line)
        {
        if (mode & CRYPTO_LOCK)
                {
@@ -746,8 +731,7 @@ int line;
                }
        }
 
-void do_threads(s_ctx,c_ctx)
-SSL_CTX *s_ctx,*c_ctx;
+void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
        {
        double ret;
        SSL_CTX *ssl_ctx[2];
@@ -798,19 +782,21 @@ SSL_CTX *s_ctx,*c_ctx;
        printf("win32 threads done - %.3f seconds\n",ret);
        }
 
-#endif /* WIN32 */
+#endif /* OPENSSL_SYS_WIN32 */
 
 #ifdef SOLARIS
 
-static mutex_t lock_cs[CRYPTO_NUM_LOCKS];
-/*static rwlock_t lock_cs[CRYPTO_NUM_LOCKS]; */
-static long lock_count[CRYPTO_NUM_LOCKS];
+static mutex_t *lock_cs;
+/*static rwlock_t *lock_cs; */
+static long *lock_count;
 
-void thread_setup()
+void thread_setup(void)
        {
        int i;
 
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+       lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
+       lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+       for (i=0; i<CRYPTO_num_locks(); i++)
                {
                lock_count[i]=0;
                /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
@@ -821,40 +807,42 @@ void thread_setup()
        CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
        }
 
-void thread_cleanup()
+void thread_cleanup(void)
        {
        int i;
 
        CRYPTO_set_locking_callback(NULL);
-fprintf(stderr,"cleanup\n");
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+
+       fprintf(stderr,"cleanup\n");
+
+       for (i=0; i<CRYPTO_num_locks(); i++)
                {
                /* rwlock_destroy(&(lock_cs[i])); */
                mutex_destroy(&(lock_cs[i]));
                fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
                }
-fprintf(stderr,"done cleanup\n");
+       OPENSSL_free(lock_cs);
+       OPENSSL_free(lock_count);
+
+       fprintf(stderr,"done cleanup\n");
+
        }
 
-void solaris_locking_callback(mode,type,file,line)
-int mode;
-int type;
-char *file;
-int line;
+void solaris_locking_callback(int mode, int type, char *file, int line)
        {
 #ifdef undef
-fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
-       CRYPTO_thread_id(),
-       (mode&CRYPTO_LOCK)?"l":"u",
-       (type&CRYPTO_READ)?"r":"w",file,line);
+       fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
+               CRYPTO_thread_id(),
+               (mode&CRYPTO_LOCK)?"l":"u",
+               (type&CRYPTO_READ)?"r":"w",file,line);
 #endif
 
-/*
-if (CRYPTO_LOCK_SSL_CERT == type)
+       /*
+       if (CRYPTO_LOCK_SSL_CERT == type)
        fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
                CRYPTO_thread_id(),
                mode,file,line);
-*/
+       */
        if (mode & CRYPTO_LOCK)
                {
        /*      if (mode & CRYPTO_READ)
@@ -872,8 +860,7 @@ if (CRYPTO_LOCK_SSL_CERT == type)
                }
        }
 
-void do_threads(s_ctx,c_ctx)
-SSL_CTX *s_ctx,*c_ctx;
+void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
        {
        SSL_CTX *ssl_ctx[2];
        thread_t thread_ctx[MAX_THREAD_NUMBER];
@@ -902,7 +889,7 @@ SSL_CTX *s_ctx,*c_ctx;
                s_ctx->references,c_ctx->references);
        }
 
-unsigned long solaris_thread_id()
+unsigned long solaris_thread_id(void)
        {
        unsigned long ret;
 
@@ -915,9 +902,9 @@ unsigned long solaris_thread_id()
 
 
 static usptr_t *arena;
-static usema_t *lock_cs[CRYPTO_NUM_LOCKS];
+static usema_t **lock_cs;
 
-void thread_setup()
+void thread_setup(void)
        {
        int i;
        char filename[20];
@@ -932,7 +919,8 @@ void thread_setup()
        arena=usinit(filename);
        unlink(filename);
 
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+       lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
+       for (i=0; i<CRYPTO_num_locks(); i++)
                {
                lock_cs[i]=usnewsema(arena,1);
                }
@@ -941,12 +929,12 @@ void thread_setup()
        CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
        }
 
-void thread_cleanup()
+void thread_cleanup(void)
        {
        int i;
 
        CRYPTO_set_locking_callback(NULL);
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+       for (i=0; i<CRYPTO_num_locks(); i++)
                {
                char buf[10];
 
@@ -954,13 +942,10 @@ void thread_cleanup()
                usdumpsema(lock_cs[i],stdout,buf);
                usfreesema(lock_cs[i],arena);
                }
+       OPENSSL_free(lock_cs);
        }
 
-void irix_locking_callback(mode,type,file,line)
-int mode;
-int type;
-char *file;
-int line;
+void irix_locking_callback(int mode, int type, char *file, int line)
        {
        if (mode & CRYPTO_LOCK)
                {
@@ -974,8 +959,7 @@ int line;
                }
        }
 
-void do_threads(s_ctx,c_ctx)
-SSL_CTX *s_ctx,*c_ctx;
+void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
        {
        SSL_CTX *ssl_ctx[2];
        int thread_ctx[MAX_THREAD_NUMBER];
@@ -1000,7 +984,7 @@ SSL_CTX *s_ctx,*c_ctx;
                s_ctx->references,c_ctx->references);
        }
 
-unsigned long irix_thread_id()
+unsigned long irix_thread_id(void)
        {
        unsigned long ret;
 
@@ -1011,14 +995,16 @@ unsigned long irix_thread_id()
 
 #ifdef PTHREADS
 
-static pthread_mutex_t lock_cs[CRYPTO_NUM_LOCKS];
-static long lock_count[CRYPTO_NUM_LOCKS];
+static pthread_mutex_t *lock_cs;
+static long *lock_count;
 
-void thread_setup()
+void thread_setup(void)
        {
        int i;
 
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+       lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
+       lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+       for (i=0; i<CRYPTO_num_locks(); i++)
                {
                lock_count[i]=0;
                pthread_mutex_init(&(lock_cs[i]),NULL);
@@ -1028,26 +1014,26 @@ void thread_setup()
        CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
        }
 
-void thread_cleanup()
+void thread_cleanup(void)
        {
        int i;
 
        CRYPTO_set_locking_callback(NULL);
        fprintf(stderr,"cleanup\n");
-       for (i=0; i<CRYPTO_NUM_LOCKS; i++)
+       for (i=0; i<CRYPTO_num_locks(); i++)
                {
                pthread_mutex_destroy(&(lock_cs[i]));
                fprintf(stderr,"%8ld:%s\n",lock_count[i],
                        CRYPTO_get_lock_name(i));
                }
+       OPENSSL_free(lock_cs);
+       OPENSSL_free(lock_count);
+
        fprintf(stderr,"done cleanup\n");
        }
 
-void pthreads_locking_callback(mode,type,file,line)
-int mode;
-int type;
-char *file;
-int line;
+void pthreads_locking_callback(int mode, int type, char *file,
+            int line)
       {
 #ifdef undef
        fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
@@ -1072,8 +1058,7 @@ int line;
                }
        }
 
-void do_threads(s_ctx,c_ctx)
-SSL_CTX *s_ctx,*c_ctx;
+void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
        {
        SSL_CTX *ssl_ctx[2];
        pthread_t thread_ctx[MAX_THREAD_NUMBER];
@@ -1098,10 +1083,10 @@ SSL_CTX *s_ctx,*c_ctx;
                }
 
        printf("pthreads threads done (%d,%d)\n",
-       s_ctx->references,c_ctx->references);
+               s_ctx->references,c_ctx->references);
        }
 
-unsigned long pthreads_thread_id()
+unsigned long pthreads_thread_id(void)
        {
        unsigned long ret;