-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathopenarc-crypto.c
317 lines (263 loc) · 6.21 KB
/
openarc-crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
** Copyright (c) 2016, 2017, The Trusted Domain Project.
** All rights reserved.
*/
#include <openssl/opensslv.h>
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
#include "build-config.h"
/* system includes */
#include <sys/types.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#endif /* HAVE_STDBOOL_H */
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
/* openssl includes */
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/conf.h>
/* openarc includes */
#include "openarc-crypto.h"
#include "openarc.h"
/* globals */
static _Bool crypto_init_done = FALSE;
static pthread_mutex_t id_lock;
static pthread_key_t id_key;
static unsigned int nmutexes = 0;
static unsigned long threadid = 0L;
static pthread_mutex_t *mutexes = NULL;
/*
** ARCF_CRYPTO_LOCK_CALLBACK -- locking callback for libcrypto
**
** Parameters:
** mode -- lock mode (request from libcrypto)
** idx -- lock index for this request
** file -- file making the request
** line -- line making the request
**
** Return value:
** None.
*/
static void
arcf_crypto_lock_callback(int mode, int idx,
/* UNUSED */ const char *file,
/* UNUSED */ int line)
{
int status;
if ((mode & CRYPTO_LOCK) != 0)
status = pthread_mutex_lock(&mutexes[idx]);
else
status = pthread_mutex_unlock(&mutexes[idx]);
assert(status == 0);
}
/*
** ARCF_CRYPTO_GET_ID -- generate/retrieve thread ID
**
** Parameters:
**
** Return value:
**
*/
static unsigned long
arcf_crypto_get_id(void)
{
unsigned long *id;
id = pthread_getspecific(id_key);
if (id == NULL)
{
id = (unsigned long *) malloc(sizeof *id);
assert(pthread_mutex_lock(&id_lock) == 0);
threadid++;
*id = threadid;
assert(pthread_mutex_unlock(&id_lock) == 0);
assert(pthread_setspecific(id_key, id) == 0);
}
return *id;
}
/*
** ARCF_CRYPTO_FREE_ID -- destroy thread ID
**
** Parameters:
** ptr -- pointer to be destroyed
**
** Return value:
** None.
*/
static void
arcf_crypto_free_id(void *ptr)
{
/*
** Trick arcf_crypto_get_id(); the thread-specific pointer has
** already been cleared at this point, but arcf_crypto_get_id()
** may be called by ERR_remove_state() which will then allocate a
** new thread pointer if the thread-specific pointer is NULL. This
** means a memory leak of thread IDs and, on Solaris, an infinite loop
** because the destructor (indirectly) re-sets the thread-specific
** pointer to something not NULL. See pthread_key_create(3).
*/
if (ptr != NULL)
{
assert(pthread_setspecific(id_key, ptr) == 0);
ERR_remove_state(0);
free(ptr);
/* now we can actually clear it for real */
assert(pthread_setspecific(id_key, NULL) == 0);
}
}
/*
** ARCF_CRYPTO_DYN_CREATE -- dynamically create a mutex
**
** Parameters:
** file -- file making the request
** line -- line making the request
**
** Return value:
** Pointer to the new mutex.
*/
static struct CRYPTO_dynlock_value *
arcf_crypto_dyn_create(/* UNUSED */ const char *file,
/* UNUSED */ int line)
{
int err;
pthread_mutex_t *new;
new = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
if (new == NULL)
return NULL;
err = pthread_mutex_init(new, NULL);
if (err != 0)
{
free(new);
return NULL;
}
return (void *) new;
}
/*
** ARCF_CRYPTO_DYN_DESTROY -- destroy a dynamic mutex
**
** Parameters:
** mutex -- pointer to the mutex to destroy
** file -- file making the request
** line -- line making the request
**
** Return value:
** None.
*/
static void
arcf_crypto_dyn_destroy(struct CRYPTO_dynlock_value *lock,
/* UNUSED */ const char *file,
/* UNUSED */ int line)
{
assert(lock != NULL);
pthread_mutex_destroy((pthread_mutex_t *) lock);
free(lock);
}
/*
** ARCF_CRYPTO_DYN_LOCK -- lock/unlock a dynamic mutex
**
** Parameters:
** mode -- lock mode (request from libcrypto)
** mutex -- pointer to the mutex to lock/unlock
** file -- file making the request
** line -- line making the request
**
** Return value:
** None.
*/
static void
arcf_crypto_dyn_lock(int mode, struct CRYPTO_dynlock_value *lock,
/* UNUSED */ const char *file,
/* UNUSED */ int line)
{
int status;
assert(lock != NULL);
if ((mode & CRYPTO_LOCK) != 0)
status = pthread_mutex_lock((pthread_mutex_t *) lock);
else
status = pthread_mutex_unlock((pthread_mutex_t *) lock);
assert(status == 0);
}
/*
** ARCF_CRYPTO_INIT -- set up openssl dependencies
**
** Parameters:
** None.
**
** Return value:
** 0 -- success
** !0 -- an error code (a la errno)
*/
int
arcf_crypto_init(void)
{
int c;
int n;
int status;
n = CRYPTO_num_locks();
mutexes = (pthread_mutex_t *) malloc(n * sizeof(pthread_mutex_t));
if (mutexes == NULL)
return errno;
for (c = 0; c < n; c++)
{
status = pthread_mutex_init(&mutexes[c], NULL);
if (status != 0)
return status;
}
status = pthread_mutex_init(&id_lock, NULL);
if (status != 0)
return status;
nmutexes = n;
status = pthread_key_create(&id_key, &arcf_crypto_free_id);
if (status != 0)
return status;
SSL_load_error_strings();
SSL_library_init();
ERR_load_crypto_strings();
CRYPTO_set_id_callback(&arcf_crypto_get_id);
CRYPTO_set_locking_callback(&arcf_crypto_lock_callback);
CRYPTO_set_dynlock_create_callback(&arcf_crypto_dyn_create);
CRYPTO_set_dynlock_lock_callback(&arcf_crypto_dyn_lock);
CRYPTO_set_dynlock_destroy_callback(&arcf_crypto_dyn_destroy);
#ifdef USE_OPENSSL_ENGINE
if (!SSL_set_engine(NULL))
return EINVAL;
#endif /* USE_OPENSSL_ENGINE */
crypto_init_done = TRUE;
return 0;
}
/*
** ARCF_CRYPTO_FREE -- tear down openssl dependencies
**
** Parameters:
** None.
**
** Return value:
** None.
*/
void
arcf_crypto_free(void)
{
if (crypto_init_done)
{
CRYPTO_cleanup_all_ex_data();
CONF_modules_free();
EVP_cleanup();
ERR_free_strings();
ERR_remove_state(0);
if (nmutexes > 0)
{
unsigned int c;
for (c = 0; c < nmutexes; c++)
pthread_mutex_destroy(&mutexes[c]);
free(mutexes);
mutexes = NULL;
nmutexes = 0;
}
crypto_init_done = FALSE;
}
}
#endif /* OpenSSL < 1.1.0 */