Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame^] | 2 | Copyright (C) 2006 Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame^] | 5 | it under the terms of the GNU General Public License as published by |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 6 | the Free Software Foundation; either version 2, or (at your option) |
| 7 | any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place, Boston, MA 02111. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <openssl/evp.h> |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 21 | #include <config.h> |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 22 | |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 23 | #ifdef _MSWINDOWS_ |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 24 | #include <windows.h> |
| 25 | #endif |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 26 | #if defined SOLARIS && !defined HAVE_PTHREAD_H |
| 27 | #include <synch.h> |
| 28 | #include <thread.h> |
| 29 | #endif |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 30 | #if !defined _MSWINDOWS_ && !defined SOLARIS |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 31 | #include <pthread.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifdef const |
| 35 | #undef const |
| 36 | #endif |
| 37 | |
| 38 | static void threadLockSetup(void); |
| 39 | static void threadLockCleanup(void); |
| 40 | static void myLockingCallback(int, int, const char *, int); |
| 41 | |
| 42 | /** |
| 43 | * Implement the locking callback functions for openSSL. |
| 44 | * |
| 45 | * Unfortunatly we can't use the Commonc++ Mutex here because the |
| 46 | * Mutex may use (for some cases) the Commonc++ Thread class. OpenSSL |
| 47 | * does not use this Thread class. |
| 48 | */ |
| 49 | |
| 50 | static int initialized = 0; |
| 51 | |
| 52 | int initializeOpenSSL () |
| 53 | { |
| 54 | |
| 55 | if (initialized) { |
| 56 | return 1; |
| 57 | } |
| 58 | initialized = 1; |
| 59 | threadLockSetup(); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | int finalizeOpenSSL () |
| 64 | { |
| 65 | if(!initialized) |
| 66 | return 1; |
| 67 | |
| 68 | initialized = 0; |
| 69 | threadLockCleanup(); |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | #ifdef _MSWINDOWS_ |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 74 | |
| 75 | static HANDLE *lock_cs; |
| 76 | |
| 77 | static void threadLockSetup(void) { |
| 78 | int i; |
| 79 | |
| 80 | lock_cs=(HANDLE*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); |
| 81 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
| 82 | lock_cs[i] = CreateMutex(NULL,FALSE,NULL); |
| 83 | } |
| 84 | |
| 85 | CRYPTO_set_locking_callback((void (*)(int,int,const char *,int))myLockingCallback); |
| 86 | /* id callback defined */ |
| 87 | } |
| 88 | |
| 89 | static void threadLockCleanup(void) { |
| 90 | int i; |
| 91 | |
| 92 | CRYPTO_set_locking_callback(NULL); |
| 93 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
| 94 | CloseHandle(lock_cs[i]); |
| 95 | } |
| 96 | OPENSSL_free(lock_cs); |
| 97 | } |
| 98 | |
| 99 | static void myLockingCallback(int mode, int type, const char *file, int line) { |
| 100 | if (mode & CRYPTO_LOCK) { |
| 101 | WaitForSingleObject(lock_cs[type], INFINITE); |
| 102 | } |
| 103 | else { |
| 104 | ReleaseMutex(lock_cs[type]); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #endif /* OPENSSL_SYS_WIN32 */ |
| 109 | |
| 110 | |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 111 | #if defined SOLARIS && !defined HAVE_PTHREAD_H |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 112 | |
| 113 | static mutex_t *lock_cs; |
| 114 | static long *lock_count; |
| 115 | |
| 116 | static void threadLockSetup(void) { |
| 117 | int i; |
| 118 | |
| 119 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t)); |
| 120 | lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); |
| 121 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
| 122 | lock_count[i] = 0; |
| 123 | /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */ |
| 124 | mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL); |
| 125 | } |
| 126 | CRYPTO_set_locking_callback((void (*)(int, int ,const char *, int))myLockingCallback); |
| 127 | } |
| 128 | |
| 129 | static void threadLockCleanup(void) { |
| 130 | int i; |
| 131 | |
| 132 | CRYPTO_set_locking_callback(NULL); |
| 133 | |
| 134 | fprintf(stderr,"cleanup\n"); |
| 135 | |
| 136 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
| 137 | /* rwlock_destroy(&(lock_cs[i])); */ |
| 138 | mutex_destroy(&(lock_cs[i])); |
| 139 | fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i)); |
| 140 | } |
| 141 | OPENSSL_free(lock_cs); |
| 142 | OPENSSL_free(lock_count); |
| 143 | } |
| 144 | |
| 145 | static void myLockingCallback(int mode, int type, const char *file, int line) |
| 146 | { |
| 147 | #ifdef undef |
| 148 | fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", |
| 149 | CRYPTO_thread_id(), |
| 150 | (mode&CRYPTO_LOCK)?"l":"u", |
| 151 | (type&CRYPTO_READ)?"r":"w",file,line); |
| 152 | #endif |
| 153 | |
| 154 | /* |
| 155 | if (CRYPTO_LOCK_SSL_CERT == type) |
| 156 | fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n", |
| 157 | CRYPTO_thread_id(), |
| 158 | mode,file,line); |
| 159 | */ |
| 160 | if (mode & CRYPTO_LOCK) { |
| 161 | mutex_lock(&(lock_cs[type])); |
| 162 | lock_count[type]++; |
| 163 | } |
| 164 | else { |
| 165 | mutex_unlock(&(lock_cs[type])); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static unsigned long solaris_thread_id(void) { |
| 170 | unsigned long ret; |
| 171 | |
| 172 | ret=(unsigned long)thr_self(); |
| 173 | return(ret); |
| 174 | } |
| 175 | #endif /* SOLARIS */ |
| 176 | |
| 177 | |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 178 | static pthread_mutex_t* lock_cs; |
| 179 | static long* lock_count; |
| 180 | |
| 181 | static void threadLockSetup(void) { |
| 182 | int i; |
| 183 | |
| 184 | lock_cs = (pthread_mutex_t*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); |
| 185 | lock_count = (long*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); |
| 186 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
| 187 | lock_count[i] = 0; |
| 188 | pthread_mutex_init(&(lock_cs[i]),NULL); |
| 189 | } |
| 190 | |
| 191 | // CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id); |
| 192 | CRYPTO_set_locking_callback((void (*)(int,int,const char *, int))myLockingCallback); |
| 193 | } |
| 194 | |
| 195 | static void threadLockCleanup(void) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | CRYPTO_set_locking_callback(NULL); |
| 200 | fprintf(stderr,"cleanup\n"); |
| 201 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
| 202 | pthread_mutex_destroy(&(lock_cs[i])); |
| 203 | fprintf(stderr,"%8ld:%s\n",lock_count[i], |
| 204 | CRYPTO_get_lock_name(i)); |
| 205 | } |
| 206 | OPENSSL_free(lock_cs); |
| 207 | OPENSSL_free(lock_count); |
| 208 | } |
| 209 | |
| 210 | static void myLockingCallback(int mode, int type, const char *file, |
| 211 | int line) { |
| 212 | #ifdef undef |
| 213 | fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", |
| 214 | CRYPTO_thread_id(), |
| 215 | (mode&CRYPTO_LOCK)?"l":"u", |
| 216 | (type&CRYPTO_READ)?"r":"w",file,line); |
| 217 | #endif |
| 218 | if (mode & CRYPTO_LOCK) { |
| 219 | pthread_mutex_lock(&(lock_cs[type])); |
| 220 | lock_count[type]++; |
| 221 | } |
| 222 | else { |
| 223 | pthread_mutex_unlock(&(lock_cs[type])); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | static unsigned long pthreads_thread_id(void) |
| 229 | { |
| 230 | unsigned long ret; |
| 231 | |
| 232 | ret = (unsigned long)pthread_self(); |
| 233 | return(ret); |
| 234 | } |
| 235 | */ |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 236 | |