2a97dbcc3552946cabf369d075e757f2a30ca384
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_plugin.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <openssl/conf.h>
18 #include <openssl/evp.h>
19 #include <openssl/engine.h>
20 #include <openssl/crypto.h>
21
22 #include "openssl_plugin.h"
23
24 #include <library.h>
25 #include <threading/thread.h>
26 #include <threading/mutex.h>
27 #include "openssl_util.h"
28 #include "openssl_crypter.h"
29 #include "openssl_hasher.h"
30 #include "openssl_sha1_prf.h"
31 #include "openssl_diffie_hellman.h"
32 #include "openssl_ec_diffie_hellman.h"
33 #include "openssl_rsa_private_key.h"
34 #include "openssl_rsa_public_key.h"
35 #include "openssl_ec_private_key.h"
36 #include "openssl_ec_public_key.h"
37 #include "openssl_x509.h"
38 #include "openssl_crl.h"
39
40 typedef struct private_openssl_plugin_t private_openssl_plugin_t;
41
42 /**
43 * private data of openssl_plugin
44 */
45 struct private_openssl_plugin_t {
46
47 /**
48 * public functions
49 */
50 openssl_plugin_t public;
51 };
52
53 /**
54 * Array of static mutexs, with CRYPTO_num_locks() mutex
55 */
56 static mutex_t **mutex = NULL;
57
58 /**
59 * Locking callback for static locks
60 */
61 static void locking_function(int mode, int type, const char *file, int line)
62 {
63 if (mutex)
64 {
65 if (mode & CRYPTO_LOCK)
66 {
67 mutex[type]->lock(mutex[type]);
68 }
69 else
70 {
71 mutex[type]->unlock(mutex[type]);
72 }
73 }
74 }
75
76 /**
77 * Implementation of dynlock
78 */
79 struct CRYPTO_dynlock_value {
80 mutex_t *mutex;
81 };
82
83 /**
84 * Callback to create a dynamic lock
85 */
86 static struct CRYPTO_dynlock_value *create_function(const char *file, int line)
87 {
88 struct CRYPTO_dynlock_value *lock;
89
90 lock = malloc_thing(struct CRYPTO_dynlock_value);
91 lock->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
92 return lock;
93 }
94
95 /**
96 * Callback to (un-)lock a dynamic lock
97 */
98 static void lock_function(int mode, struct CRYPTO_dynlock_value *lock,
99 const char *file, int line)
100 {
101 if (mode & CRYPTO_LOCK)
102 {
103 lock->mutex->lock(lock->mutex);
104 }
105 else
106 {
107 lock->mutex->unlock(lock->mutex);
108 }
109 }
110
111 /**
112 * Callback to destroy a dynamic lock
113 */
114 static void destroy_function(struct CRYPTO_dynlock_value *lock,
115 const char *file, int line)
116 {
117 lock->mutex->destroy(lock->mutex);
118 free(lock);
119 }
120
121 /**
122 * Thread-ID callback function
123 */
124 static unsigned long id_function(void)
125 {
126 return (unsigned long)thread_current_id();
127 }
128
129 /**
130 * initialize OpenSSL for multi-threaded use
131 */
132 static void threading_init()
133 {
134 int i, num_locks;
135
136 CRYPTO_set_id_callback(id_function);
137 CRYPTO_set_locking_callback(locking_function);
138
139 CRYPTO_set_dynlock_create_callback(create_function);
140 CRYPTO_set_dynlock_lock_callback(lock_function);
141 CRYPTO_set_dynlock_destroy_callback(destroy_function);
142
143 num_locks = CRYPTO_num_locks();
144 mutex = malloc(sizeof(mutex_t*) * num_locks);
145 for (i = 0; i < num_locks; i++)
146 {
147 mutex[i] = mutex_create(MUTEX_TYPE_DEFAULT);
148 }
149 }
150
151 /**
152 * cleanup OpenSSL threading locks
153 */
154 static void threading_cleanup()
155 {
156 int i, num_locks;
157
158 num_locks = CRYPTO_num_locks();
159 for (i = 0; i < num_locks; i++)
160 {
161 mutex[i]->destroy(mutex[i]);
162 }
163 free(mutex);
164 mutex = NULL;
165 }
166
167 /**
168 * Implementation of openssl_plugin_t.destroy
169 */
170 static void destroy(private_openssl_plugin_t *this)
171 {
172 lib->crypto->remove_crypter(lib->crypto,
173 (crypter_constructor_t)openssl_crypter_create);
174 lib->crypto->remove_hasher(lib->crypto,
175 (hasher_constructor_t)openssl_hasher_create);
176 lib->crypto->remove_prf(lib->crypto,
177 (prf_constructor_t)openssl_sha1_prf_create);
178 lib->crypto->remove_dh(lib->crypto,
179 (dh_constructor_t)openssl_diffie_hellman_create);
180 lib->creds->remove_builder(lib->creds,
181 (builder_function_t)openssl_rsa_private_key_load);
182 lib->creds->remove_builder(lib->creds,
183 (builder_function_t)openssl_rsa_private_key_gen);
184 lib->creds->remove_builder(lib->creds,
185 (builder_function_t)openssl_rsa_private_key_connect);
186 lib->creds->remove_builder(lib->creds,
187 (builder_function_t)openssl_rsa_public_key_load);
188 #ifndef OPENSSL_NO_EC
189 lib->crypto->remove_dh(lib->crypto,
190 (dh_constructor_t)openssl_ec_diffie_hellman_create);
191 lib->creds->remove_builder(lib->creds,
192 (builder_function_t)openssl_ec_private_key_load);
193 lib->creds->remove_builder(lib->creds,
194 (builder_function_t)openssl_ec_private_key_gen);
195 lib->creds->remove_builder(lib->creds,
196 (builder_function_t)openssl_ec_public_key_load);
197 #endif /* OPENSSL_NO_EC */
198 lib->creds->remove_builder(lib->creds,
199 (builder_function_t)openssl_x509_load);
200 lib->creds->remove_builder(lib->creds,
201 (builder_function_t)openssl_crl_load);
202
203 ENGINE_cleanup();
204 EVP_cleanup();
205 CONF_modules_free();
206
207 threading_cleanup();
208
209 free(this);
210 }
211
212 /*
213 * see header file
214 */
215 plugin_t *openssl_plugin_create()
216 {
217 private_openssl_plugin_t *this = malloc_thing(private_openssl_plugin_t);
218
219 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
220
221 threading_init();
222
223 OPENSSL_config(NULL);
224 OpenSSL_add_all_algorithms();
225
226 /* activate support for hardware accelerators */
227 ENGINE_load_builtin_engines();
228 ENGINE_register_all_complete();
229
230 /* crypter */
231 lib->crypto->add_crypter(lib->crypto, ENCR_AES_CBC,
232 (crypter_constructor_t)openssl_crypter_create);
233 lib->crypto->add_crypter(lib->crypto, ENCR_CAMELLIA_CBC,
234 (crypter_constructor_t)openssl_crypter_create);
235 lib->crypto->add_crypter(lib->crypto, ENCR_3DES,
236 (crypter_constructor_t)openssl_crypter_create);
237 lib->crypto->add_crypter(lib->crypto, ENCR_RC5,
238 (crypter_constructor_t)openssl_crypter_create);
239 lib->crypto->add_crypter(lib->crypto, ENCR_IDEA,
240 (crypter_constructor_t)openssl_crypter_create);
241 lib->crypto->add_crypter(lib->crypto, ENCR_CAST,
242 (crypter_constructor_t)openssl_crypter_create);
243 lib->crypto->add_crypter(lib->crypto, ENCR_BLOWFISH,
244 (crypter_constructor_t)openssl_crypter_create);
245 lib->crypto->add_crypter(lib->crypto, ENCR_DES,
246 (crypter_constructor_t)openssl_crypter_create);
247 lib->crypto->add_crypter(lib->crypto, ENCR_DES_ECB,
248 (crypter_constructor_t)openssl_crypter_create);
249 lib->crypto->add_crypter(lib->crypto, ENCR_NULL,
250 (crypter_constructor_t)openssl_crypter_create);
251
252 /* hasher */
253 lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
254 (hasher_constructor_t)openssl_hasher_create);
255 lib->crypto->add_hasher(lib->crypto, HASH_MD2,
256 (hasher_constructor_t)openssl_hasher_create);
257 lib->crypto->add_hasher(lib->crypto, HASH_MD4,
258 (hasher_constructor_t)openssl_hasher_create);
259 lib->crypto->add_hasher(lib->crypto, HASH_MD5,
260 (hasher_constructor_t)openssl_hasher_create);
261 lib->crypto->add_hasher(lib->crypto, HASH_SHA224,
262 (hasher_constructor_t)openssl_hasher_create);
263 lib->crypto->add_hasher(lib->crypto, HASH_SHA256,
264 (hasher_constructor_t)openssl_hasher_create);
265 lib->crypto->add_hasher(lib->crypto, HASH_SHA384,
266 (hasher_constructor_t)openssl_hasher_create);
267 lib->crypto->add_hasher(lib->crypto, HASH_SHA512,
268 (hasher_constructor_t)openssl_hasher_create);
269
270 /* prf */
271 lib->crypto->add_prf(lib->crypto, PRF_KEYED_SHA1,
272 (prf_constructor_t)openssl_sha1_prf_create);
273
274 /* (ec) diffie hellman */
275 lib->crypto->add_dh(lib->crypto, MODP_2048_BIT,
276 (dh_constructor_t)openssl_diffie_hellman_create);
277 lib->crypto->add_dh(lib->crypto, MODP_2048_224,
278 (dh_constructor_t)openssl_diffie_hellman_create);
279 lib->crypto->add_dh(lib->crypto, MODP_2048_256,
280 (dh_constructor_t)openssl_diffie_hellman_create);
281 lib->crypto->add_dh(lib->crypto, MODP_1536_BIT,
282 (dh_constructor_t)openssl_diffie_hellman_create);
283 #ifndef OPENSSL_NO_EC
284 lib->crypto->add_dh(lib->crypto, ECP_256_BIT,
285 (dh_constructor_t)openssl_ec_diffie_hellman_create);
286 lib->crypto->add_dh(lib->crypto, ECP_384_BIT,
287 (dh_constructor_t)openssl_ec_diffie_hellman_create);
288 lib->crypto->add_dh(lib->crypto, ECP_521_BIT,
289 (dh_constructor_t)openssl_ec_diffie_hellman_create);
290 lib->crypto->add_dh(lib->crypto, ECP_224_BIT,
291 (dh_constructor_t)openssl_ec_diffie_hellman_create);
292 lib->crypto->add_dh(lib->crypto, ECP_192_BIT,
293 (dh_constructor_t)openssl_ec_diffie_hellman_create);
294 #endif /* OPENSSL_NO_EC */
295 lib->crypto->add_dh(lib->crypto, MODP_3072_BIT,
296 (dh_constructor_t)openssl_diffie_hellman_create);
297 lib->crypto->add_dh(lib->crypto, MODP_4096_BIT,
298 (dh_constructor_t)openssl_diffie_hellman_create);
299 lib->crypto->add_dh(lib->crypto, MODP_6144_BIT,
300 (dh_constructor_t)openssl_diffie_hellman_create);
301 lib->crypto->add_dh(lib->crypto, MODP_8192_BIT,
302 (dh_constructor_t)openssl_diffie_hellman_create);
303 lib->crypto->add_dh(lib->crypto, MODP_1024_BIT,
304 (dh_constructor_t)openssl_diffie_hellman_create);
305 lib->crypto->add_dh(lib->crypto, MODP_1024_160,
306 (dh_constructor_t)openssl_diffie_hellman_create);
307 lib->crypto->add_dh(lib->crypto, MODP_768_BIT,
308 (dh_constructor_t)openssl_diffie_hellman_create);
309
310 /* rsa */
311 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
312 (builder_function_t)openssl_rsa_private_key_load);
313 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
314 (builder_function_t)openssl_rsa_private_key_gen);
315 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
316 (builder_function_t)openssl_rsa_private_key_connect);
317 lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
318 (builder_function_t)openssl_rsa_public_key_load);
319 lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
320 (builder_function_t)openssl_rsa_public_key_load);
321
322 #ifndef OPENSSL_NO_EC
323 /* ecdsa */
324 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_ECDSA,
325 (builder_function_t)openssl_ec_private_key_load);
326 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_ECDSA,
327 (builder_function_t)openssl_ec_private_key_gen);
328 lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_ECDSA,
329 (builder_function_t)openssl_ec_public_key_load);
330 #endif /* OPENSSL_NO_EC */
331
332 /* X509 certificates */
333 lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_X509,
334 (builder_function_t)openssl_x509_load);
335 lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_X509_CRL,
336 (builder_function_t)openssl_crl_load);
337
338 return &this->public.plugin;
339 }
340