Back out safestack.h change for now: seems to break some things.
[openssl.git] / crypto / threads / mttest.c
index 3e4d231f25975bc7da160ea5927aa69c5b000ff2..eba7aa8a6ebcc432533ab522a295ad9afddf64a4 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
+#ifdef PTHREADS
+#include <pthread.h>
+#endif
+#ifdef OPENSSL_SYS_NETWARE
+#if !defined __int64
+#  define __int64 long long
+#endif   
+#include <nwmpk.h>
+#endif
 #include <openssl/lhash.h>
 #include <openssl/crypto.h>
 #include <openssl/buffer.h>
-#include "../e_os.h"
+#include "../../e_os.h"
 #include <openssl/x509.h>
 #include <openssl/ssl.h>
 #include <openssl/err.h>
+#include <openssl/rand.h>
 
-#ifdef NO_FP_API
+#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"
+#ifdef OPENSSL_SYS_NETWARE
+#define TEST_SERVER_CERT "/openssl/apps/server.pem"
+#define TEST_CLIENT_CERT "/openssl/apps/client.pem"
+#else
+#define TEST_SERVER_CERT "../../apps/server.pem"
+#define TEST_CLIENT_CERT "../../apps/client.pem"
+#endif
 
 #define MAX_THREAD_NUMBER      100
 
-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);
@@ -102,10 +116,20 @@ void irix_locking_callback(int mode,int type,char *file,int line);
 void solaris_locking_callback(int mode,int type,char *file,int line);
 void win32_locking_callback(int mode,int type,char *file,int line);
 void pthreads_locking_callback(int mode,int type,char *file,int line);
+void netware_locking_callback(int mode,int type,char *file,int line);
+void beos_locking_callback(int mode,int type,const char *file,int line);
 
 unsigned long irix_thread_id(void );
 unsigned long solaris_thread_id(void );
 unsigned long pthreads_thread_id(void );
+unsigned long netware_thread_id(void );
+unsigned long beos_thread_id(void );
+
+#if defined(OPENSSL_SYS_NETWARE)
+static MPKMutex *lock_cs;
+static MPKSema ThreadSem;
+static long *lock_count;
+#endif
 
 BIO *bio_err=NULL;
 BIO *bio_stdout=NULL;
@@ -121,6 +145,8 @@ int number_of_loops=10;
 int reconnect=0;
 int cache_stats=0;
 
+static const char rnd_seed[] = "string to make the random number generator think it has entropy";
+
 int doit(char *ctx[4]);
 static void print_stats(FILE *fp, SSL_CTX *ctx)
 {
@@ -170,6 +196,8 @@ int main(int argc, 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)
@@ -241,7 +269,8 @@ bad:
                goto end;
                }
 
-       if (cipher == NULL) cipher=getenv("SSL_CIPHER");
+       if (cipher == NULL && OPENSSL_issetugid() == 0)
+               cipher=getenv("SSL_CIPHER");
 
        SSL_load_error_strings();
        OpenSSL_add_ssl_algorithms();
@@ -259,8 +288,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)
                {
@@ -374,6 +410,9 @@ int ndoit(SSL_CTX *ssl_ctx[2])
                SSL_free((SSL *)ctx[2]);
                SSL_free((SSL *)ctx[3]);
                }
+#   ifdef OPENSSL_SYS_NETWARE
+        MPKSemaphoreSignal(ThreadSem);
+#   endif
        return(0);
        }
 
@@ -489,6 +528,7 @@ int doit(char *ctx[4])
                                        else
                                                {
                                                fprintf(stderr,"ERROR in CLIENT\n");
+                                               ERR_print_errors_fp(stderr);
                                                return(1);
                                                }
                                        }
@@ -520,6 +560,7 @@ int doit(char *ctx[4])
                                        else
                                                {
                                                fprintf(stderr,"ERROR in CLIENT\n");
+                                               ERR_print_errors_fp(stderr);
                                                return(1);
                                                }
                                        }
@@ -615,6 +656,9 @@ int doit(char *ctx[4])
                        }
 
                if ((done & S_DONE) && (done & C_DONE)) break;
+#   if defined(OPENSSL_SYS_NETWARE)
+        ThreadSwitchWithDelay();
+#   endif
                }
 
        SSL_set_shutdown(c_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
@@ -652,33 +696,39 @@ err:
        return(0);
        }
 
-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 *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)
        {
        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);
                }
@@ -692,8 +742,9 @@ 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(int mode, int type, char *file, int line)
@@ -759,19 +810,21 @@ void do_threads(SSL_CTX *s_ctx, SSL_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)
        {
        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); */
@@ -787,31 +840,37 @@ 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(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)
@@ -871,7 +930,7 @@ unsigned long solaris_thread_id(void)
 
 
 static usptr_t *arena;
-static usema_t *lock_cs[CRYPTO_NUM_LOCKS];
+static usema_t **lock_cs;
 
 void thread_setup(void)
        {
@@ -888,7 +947,8 @@ void thread_setup(void)
        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);
                }
@@ -902,7 +962,7 @@ 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];
 
@@ -910,6 +970,7 @@ void thread_cleanup(void)
                usdumpsema(lock_cs[i],stdout,buf);
                usfreesema(lock_cs[i],arena);
                }
+       OPENSSL_free(lock_cs);
        }
 
 void irix_locking_callback(int mode, int type, char *file, int line)
@@ -962,14 +1023,16 @@ unsigned long irix_thread_id(void)
 
 #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)
        {
        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);
@@ -985,12 +1048,15 @@ void thread_cleanup(void)
 
        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");
        }
 
@@ -1045,7 +1111,7 @@ void do_threads(SSL_CTX *s_ctx, SSL_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(void)
@@ -1060,3 +1126,185 @@ unsigned long pthreads_thread_id(void)
 
 
 
+#ifdef OPENSSL_SYS_NETWARE
+
+void thread_setup(void)
+{
+   int i;
+
+   lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MPKMutex));
+   lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+   for (i=0; i<CRYPTO_num_locks(); i++)
+   {
+      lock_count[i]=0;
+      lock_cs[i]=MPKMutexAlloc("OpenSSL mutex");
+   }
+
+   ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0 );
+
+   CRYPTO_set_id_callback((unsigned long (*)())netware_thread_id);
+   CRYPTO_set_locking_callback((void (*)())netware_locking_callback);
+}
+
+void thread_cleanup(void)
+{
+   int i;
+
+   CRYPTO_set_locking_callback(NULL);
+
+   fprintf(stdout,"thread_cleanup\n");
+
+   for (i=0; i<CRYPTO_num_locks(); i++)
+   {
+      MPKMutexFree(lock_cs[i]);
+      fprintf(stdout,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
+   }
+   OPENSSL_free(lock_cs);
+   OPENSSL_free(lock_count);
+
+   MPKSemaphoreFree(ThreadSem);
+
+   fprintf(stdout,"done cleanup\n");
+}
+
+void netware_locking_callback(int mode, int type, char *file, int line)
+{
+   if (mode & CRYPTO_LOCK)
+   {
+      MPKMutexLock(lock_cs[type]);
+      lock_count[type]++;
+   }
+   else
+      MPKMutexUnlock(lock_cs[type]);
+}
+
+void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
+{
+   SSL_CTX *ssl_ctx[2];
+   int i;
+   ssl_ctx[0]=s_ctx;
+   ssl_ctx[1]=c_ctx;
+
+   for (i=0; i<thread_number; i++)
+   {
+      BeginThread( (void(*)(void*))ndoit, NULL, THREAD_STACK_SIZE, 
+                   (void*)ssl_ctx);
+      ThreadSwitchWithDelay();
+   }
+
+   printf("reaping\n");
+
+      /* loop until all threads have signaled the semaphore */
+   for (i=0; i<thread_number; i++)
+   {
+      MPKSemaphoreWait(ThreadSem);
+   }
+   printf("netware threads done (%d,%d)\n",
+         s_ctx->references,c_ctx->references);
+}
+
+unsigned long netware_thread_id(void)
+{
+   unsigned long ret;
+
+   ret=(unsigned long)GetThreadID();
+   return(ret);
+}
+#endif /* NETWARE */
+
+#ifdef BEOS_THREADS
+
+#include <Locker.h>
+
+static BLocker** lock_cs;
+static long* lock_count;
+
+void thread_setup(void)
+       {
+       int i;
+
+       lock_cs=(BLocker**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(BLocker*));
+       lock_count=(long*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+       for (i=0; i<CRYPTO_num_locks(); i++)
+               {
+               lock_count[i]=0;
+               lock_cs[i] = new BLocker(CRYPTO_get_lock_name(i));
+               }
+
+       CRYPTO_set_id_callback((unsigned long (*)())beos_thread_id);
+       CRYPTO_set_locking_callback(beos_locking_callback);
+       }
+
+void thread_cleanup(void)
+       {
+       int i;
+
+       CRYPTO_set_locking_callback(NULL);
+       fprintf(stderr,"cleanup\n");
+       for (i=0; i<CRYPTO_num_locks(); i++)
+               {
+               delete 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 beos_locking_callback(int mode, int type, const char *file, int line)
+    {
+#if 0
+       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 (mode & CRYPTO_LOCK)
+               {
+               lock_cs[type]->Lock();
+               lock_count[type]++;
+               }
+       else
+               {
+               lock_cs[type]->Unlock();
+               }
+       }
+
+void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
+       {
+       SSL_CTX *ssl_ctx[2];
+       thread_id thread_ctx[MAX_THREAD_NUMBER];
+       int i;
+
+       ssl_ctx[0]=s_ctx;
+       ssl_ctx[1]=c_ctx;
+
+       for (i=0; i<thread_number; i++)
+               {
+               thread_ctx[i] = spawn_thread((thread_func)ndoit,
+                       NULL, B_NORMAL_PRIORITY, (void *)ssl_ctx);
+               resume_thread(thread_ctx[i]);
+               }
+
+       printf("waiting...\n");
+       for (i=0; i<thread_number; i++)
+               {
+               status_t result;
+               wait_for_thread(thread_ctx[i], &result);
+               }
+
+       printf("beos threads done (%d,%d)\n",
+               s_ctx->references,c_ctx->references);
+       }
+
+unsigned long beos_thread_id(void)
+       {
+       unsigned long ret;
+
+       ret=(unsigned long)find_thread(NULL);
+       return(ret);
+       }
+
+#endif /* BEOS_THREADS */