blob: 2c5c8de40918ef688de412b1db478657215363df [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lision907ed2e2014-02-04 10:33:09 -05002 Copyright (C) 2006-2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software; you can redistribute it and/or modify
Alexandre Lision907ed2e2014-02-04 10:33:09 -05005 it under the terms of the GNU Lesser General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05006 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 Lision907ed2e2014-02-04 10:33:09 -050021#include <config.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050022
Alexandre Lision907ed2e2014-02-04 10:33:09 -050023#ifdef _MSWINDOWS_
Alexandre Lision51140e12013-12-02 10:54:09 -050024#include <windows.h>
25#endif
Alexandre Lision51140e12013-12-02 10:54:09 -050026#if defined SOLARIS && !defined HAVE_PTHREAD_H
27#include <synch.h>
28#include <thread.h>
29#endif
Alexandre Lision907ed2e2014-02-04 10:33:09 -050030#if !defined _MSWINDOWS_ && !defined SOLARIS
Alexandre Lision51140e12013-12-02 10:54:09 -050031#include <pthread.h>
32#endif
33
34#ifdef const
35#undef const
36#endif
37
38static void threadLockSetup(void);
39static void threadLockCleanup(void);
40static 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
50static int initialized = 0;
51
52int initializeOpenSSL ()
53{
54
55 if (initialized) {
56 return 1;
57 }
58 initialized = 1;
59 threadLockSetup();
60 return 1;
61}
62
63int finalizeOpenSSL ()
64{
65 if(!initialized)
66 return 1;
67
68 initialized = 0;
69 threadLockCleanup();
70 return 1;
71}
72
73#ifdef _MSWINDOWS_
Alexandre Lision51140e12013-12-02 10:54:09 -050074
75static HANDLE *lock_cs;
76
77static 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
89static 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
99static 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 Lision907ed2e2014-02-04 10:33:09 -0500111#if defined SOLARIS && !defined HAVE_PTHREAD_H
Alexandre Lision51140e12013-12-02 10:54:09 -0500112
113static mutex_t *lock_cs;
114static long *lock_count;
115
116static 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
129static 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
145static 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
169static 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 Lision51140e12013-12-02 10:54:09 -0500178static pthread_mutex_t* lock_cs;
179static long* lock_count;
180
181static 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
195static 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
210static 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/*
228static unsigned long pthreads_thread_id(void)
229{
230 unsigned long ret;
231
232 ret = (unsigned long)pthread_self();
233 return(ret);
234}
235*/
Alexandre Lision907ed2e2014-02-04 10:33:09 -0500236