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
|
/* Copyright (C) 2013 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <openssl/crypto.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <pthread.h>
#include "libknot/common.h"
#include "libknot/dnssec/config.h"
#include "libknot/dnssec/crypto.h"
/*- thread safety -----------------------------------------------------------*/
/*!
* \brief Mutexes to be used by OpenSSL.
*/
static pthread_mutex_t *openssl_mutex = NULL;
static int openssl_mutex_count = 0;
/*!
* \brief Callback for OpenSSL mutex locking and unlocking.
*
* \see CRYPTO_set_locking_callback() in OpenSSL documentation.
*
* \param mode Locking mode.
* \param n Mutex number.
* \param file Source file where locking occurs (for debugging).
* \param line Line number where locking occurs (for debugging).
*/
static void openssl_mutex_cb(int mode, int n, const char *file, int line)
{
UNUSED(file);
UNUSED(line);
assert(openssl_mutex);
assert(n < openssl_mutex_count);
pthread_mutex_t *mutex = &openssl_mutex[n];
if (mode & CRYPTO_LOCK) {
pthread_mutex_lock(mutex);
} else {
pthread_mutex_unlock(mutex);
}
}
/*!
* \brief Initialize mutexes for OpenSSL usage.
*/
static void openssl_mutexes_init(void)
{
assert(openssl_mutex_count == 0);
assert(openssl_mutex == NULL);
openssl_mutex_count = CRYPTO_num_locks();
if (openssl_mutex_count == 0) {
return;
}
openssl_mutex = calloc(openssl_mutex_count, sizeof(pthread_mutex_t));
for (int i = 0; i < openssl_mutex_count; i++) {
pthread_mutex_init(&openssl_mutex[i], NULL);
}
CRYPTO_set_locking_callback(openssl_mutex_cb);
}
/*!
* \brief Destroy mutexes for OpenSSL usage.
*/
static void openssl_mutexes_destroy(void)
{
assert(openssl_mutex);
for (int i = 0; i < openssl_mutex_count; i++) {
pthread_mutex_destroy(&openssl_mutex[i]);
}
free(openssl_mutex);
openssl_mutex_count = 0;
openssl_mutex = NULL;
}
/*!
* \brief Callback for thread identification for purpose of OpenSSL.
*
* \see CRYPTO_THREADID_set_callback() in OpenSSL documentation.
*
* \param openssl_id Thread identifier in OpenSSL.
*/
#if OPENSSL_VERSION_NUMBER >= 0x1000001fL
static void openssl_threadid_cb(CRYPTO_THREADID *openssl_id)
{
pthread_t id = pthread_self();
CRYPTO_THREADID_set_pointer(openssl_id, (void *)id);
}
#else
static unsigned long openssl_threadid_cb(void)
{
unsigned long ret;
ret = (unsigned long)pthread_self();
return(ret);
}
#endif
/*- pluggable engines -------------------------------------------------------*/
#if KNOT_ENABLE_GOST
static ENGINE *gost_engine = NULL;
static void init_gost_engine(void)
{
assert(gost_engine == NULL);
#ifndef OPENSSL_NO_STATIC_ENGINE
ENGINE_load_gost();
#else
ENGINE_load_dynamic();
#endif
gost_engine = ENGINE_by_id("gost");
if (!gost_engine) {
return;
}
ENGINE_init(gost_engine);
ENGINE_register_pkey_asn1_meths(gost_engine);
ENGINE_ctrl_cmd_string(gost_engine, "CRYPT_PARAMS",
"id-Gost28147-89-CryptoPro-A-ParamSet", 0);
}
static void deinit_gost_engine(void)
{
assert(gost_engine);
ENGINE_finish(gost_engine);
ENGINE_free(gost_engine);
gost_engine = NULL;
}
#endif
/*- public API --------------------------------------------------------------*/
void knot_crypto_init(void)
{
OpenSSL_add_all_digests();
}
void knot_crypto_cleanup(void)
{
knot_crypto_unload_engines();
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
ERR_free_strings();
knot_crypto_cleanup_thread();
}
void knot_crypto_cleanup_thread(void)
{
ERR_remove_state(0);
}
void knot_crypto_init_threads(void)
{
// locking
if (!openssl_mutex) {
openssl_mutexes_init();
}
// thread identification
#if OPENSSL_VERSION_NUMBER >= 0x1000001fL
CRYPTO_THREADID_set_callback(openssl_threadid_cb);
#else
CRYPTO_set_id_callback((unsigned long (*)())openssl_threadid_cb);
#endif
}
void knot_crypto_cleanup_threads(void)
{
if (openssl_mutex) {
openssl_mutexes_destroy();
}
}
void knot_crypto_load_engines(void)
{
#if KNOT_ENABLE_GOST
if (!gost_engine) {
init_gost_engine();
}
#endif
}
void knot_crypto_unload_engines(void)
{
#if KNOT_ENABLE_GOST
if (gost_engine) {
deinit_gost_engine();
}
#endif
}
|