compiler warning fixed
[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 #include <pthread.h>
22
23 #include "openssl_plugin.h"
24
25 #include <library.h>
26 #include <utils/mutex.h>
27 #include "openssl_crypter.h"
28 #include "openssl_hasher.h"
29 #include "openssl_diffie_hellman.h"
30 #include "openssl_ec_diffie_hellman.h"
31 #include "openssl_rsa_private_key.h"
32 #include "openssl_rsa_public_key.h"
33 #include "openssl_ec_private_key.h"
34 #include "openssl_ec_public_key.h"
35
36 typedef struct private_openssl_plugin_t private_openssl_plugin_t;
37
38 /**
39 * private data of openssl_plugin
40 */
41 struct private_openssl_plugin_t {
42
43 /**
44 * public functions
45 */
46 openssl_plugin_t public;
47 };
48
49 /**
50 * Array of static mutexs, with CRYPTO_num_locks() mutex
51 */
52 static mutex_t **mutex = NULL;
53
54 /**
55 * Locking callback for static locks
56 */
57 static void locking_function(int mode, int type, const char *file, int line)
58 {
59 if (mutex)
60 {
61 if (mode & CRYPTO_LOCK)
62 {
63 mutex[type]->lock(mutex[type]);
64 }
65 else
66 {
67 mutex[type]->unlock(mutex[type]);
68 }
69 }
70 }
71
72 /**
73 * Implementation of dynlock
74 */
75 struct CRYPTO_dynlock_value {
76 mutex_t *mutex;
77 };
78
79 /**
80 * Callback to create a dynamic lock
81 */
82 static struct CRYPTO_dynlock_value *create_function(const char *file, int line)
83 {
84 struct CRYPTO_dynlock_value *lock;
85
86 lock = malloc_thing(struct CRYPTO_dynlock_value);
87 lock->mutex = mutex_create(MUTEX_DEFAULT);
88 return lock;
89 }
90
91 /**
92 * Callback to (un-)lock a dynamic lock
93 */
94 static void lock_function(int mode, struct CRYPTO_dynlock_value *lock,
95 const char *file, int line)
96 {
97 if (mode & CRYPTO_LOCK)
98 {
99 lock->mutex->lock(lock->mutex);
100 }
101 else
102 {
103 lock->mutex->unlock(lock->mutex);
104 }
105 }
106
107 /**
108 * Callback to destroy a dynamic lock
109 */
110 static void destroy_function(struct CRYPTO_dynlock_value *lock,
111 const char *file, int line)
112 {
113 lock->mutex->destroy(lock->mutex);
114 free(lock);
115 }
116
117 /**
118 * Thread-ID callback function
119 */
120 static unsigned long id_function(void)
121 {
122 return (unsigned long)pthread_self();
123 }
124
125 /**
126 * initialize OpenSSL for multi-threaded use
127 */
128 static void threading_init()
129 {
130 int i, num_locks;
131
132 CRYPTO_set_id_callback(id_function);
133 CRYPTO_set_locking_callback(locking_function);
134
135 CRYPTO_set_dynlock_create_callback(create_function);
136 CRYPTO_set_dynlock_lock_callback(lock_function);
137 CRYPTO_set_dynlock_destroy_callback(destroy_function);
138
139 num_locks = CRYPTO_num_locks();
140 mutex = malloc(sizeof(mutex_t*) * num_locks);
141 for (i = 0; i < num_locks; i++)
142 {
143 mutex[i] = mutex_create(MUTEX_DEFAULT);
144 }
145 }
146
147 /**
148 * cleanup OpenSSL threading locks
149 */
150 static void threading_cleanup()
151 {
152 int i, num_locks;
153
154 num_locks = CRYPTO_num_locks();
155 for (i = 0; i < num_locks; i++)
156 {
157 mutex[i]->destroy(mutex[i]);
158 }
159 free(mutex);
160 mutex = NULL;
161 }
162
163 /**
164 * Implementation of openssl_plugin_t.destroy
165 */
166 static void destroy(private_openssl_plugin_t *this)
167 {
168 lib->crypto->remove_crypter(lib->crypto,
169 (crypter_constructor_t)openssl_crypter_create);
170 lib->crypto->remove_hasher(lib->crypto,
171 (hasher_constructor_t)openssl_hasher_create);
172 lib->crypto->remove_dh(lib->crypto,
173 (dh_constructor_t)openssl_diffie_hellman_create);
174 lib->crypto->remove_dh(lib->crypto,
175 (dh_constructor_t)openssl_ec_diffie_hellman_create);
176 lib->creds->remove_builder(lib->creds,
177 (builder_constructor_t)openssl_rsa_private_key_builder);
178 lib->creds->remove_builder(lib->creds,
179 (builder_constructor_t)openssl_rsa_public_key_builder);
180 lib->creds->remove_builder(lib->creds,
181 (builder_constructor_t)openssl_ec_private_key_builder);
182 lib->creds->remove_builder(lib->creds,
183 (builder_constructor_t)openssl_ec_public_key_builder);
184
185 ENGINE_cleanup();
186 EVP_cleanup();
187 CONF_modules_free();
188
189 threading_cleanup();
190
191 free(this);
192 }
193
194 /*
195 * see header file
196 */
197 plugin_t *plugin_create()
198 {
199 private_openssl_plugin_t *this = malloc_thing(private_openssl_plugin_t);
200
201 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
202
203 threading_init();
204
205 OPENSSL_config(NULL);
206 OpenSSL_add_all_algorithms();
207
208 /* activate support for hardware accelerators */
209 ENGINE_load_builtin_engines();
210 ENGINE_register_all_complete();
211
212 /* crypter */
213 lib->crypto->add_crypter(lib->crypto, ENCR_AES_CBC,
214 (crypter_constructor_t)openssl_crypter_create);
215 lib->crypto->add_crypter(lib->crypto, ENCR_3DES,
216 (crypter_constructor_t)openssl_crypter_create);
217 lib->crypto->add_crypter(lib->crypto, ENCR_RC5,
218 (crypter_constructor_t)openssl_crypter_create);
219 lib->crypto->add_crypter(lib->crypto, ENCR_IDEA,
220 (crypter_constructor_t)openssl_crypter_create);
221 lib->crypto->add_crypter(lib->crypto, ENCR_CAST,
222 (crypter_constructor_t)openssl_crypter_create);
223 lib->crypto->add_crypter(lib->crypto, ENCR_BLOWFISH,
224 (crypter_constructor_t)openssl_crypter_create);
225 lib->crypto->add_crypter(lib->crypto, ENCR_DES,
226 (crypter_constructor_t)openssl_crypter_create);
227 lib->crypto->add_crypter(lib->crypto, ENCR_DES_ECB,
228 (crypter_constructor_t)openssl_crypter_create);
229 lib->crypto->add_crypter(lib->crypto, ENCR_NULL,
230 (crypter_constructor_t)openssl_crypter_create);
231
232 /* hasher */
233 lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
234 (hasher_constructor_t)openssl_hasher_create);
235 lib->crypto->add_hasher(lib->crypto, HASH_MD2,
236 (hasher_constructor_t)openssl_hasher_create);
237 lib->crypto->add_hasher(lib->crypto, HASH_MD4,
238 (hasher_constructor_t)openssl_hasher_create);
239 lib->crypto->add_hasher(lib->crypto, HASH_MD5,
240 (hasher_constructor_t)openssl_hasher_create);
241 lib->crypto->add_hasher(lib->crypto, HASH_SHA256,
242 (hasher_constructor_t)openssl_hasher_create);
243 lib->crypto->add_hasher(lib->crypto, HASH_SHA384,
244 (hasher_constructor_t)openssl_hasher_create);
245 lib->crypto->add_hasher(lib->crypto, HASH_SHA512,
246 (hasher_constructor_t)openssl_hasher_create);
247
248 /* ec diffie hellman */
249 lib->crypto->add_dh(lib->crypto, ECP_192_BIT,
250 (dh_constructor_t)openssl_ec_diffie_hellman_create);
251 lib->crypto->add_dh(lib->crypto, ECP_224_BIT,
252 (dh_constructor_t)openssl_ec_diffie_hellman_create);
253 lib->crypto->add_dh(lib->crypto, ECP_256_BIT,
254 (dh_constructor_t)openssl_ec_diffie_hellman_create);
255 lib->crypto->add_dh(lib->crypto, ECP_384_BIT,
256 (dh_constructor_t)openssl_ec_diffie_hellman_create);
257 lib->crypto->add_dh(lib->crypto, ECP_521_BIT,
258 (dh_constructor_t)openssl_ec_diffie_hellman_create);
259
260 /* diffie hellman */
261 lib->crypto->add_dh(lib->crypto, MODP_2048_BIT,
262 (dh_constructor_t)openssl_diffie_hellman_create);
263 lib->crypto->add_dh(lib->crypto, MODP_1536_BIT,
264 (dh_constructor_t)openssl_diffie_hellman_create);
265 lib->crypto->add_dh(lib->crypto, MODP_3072_BIT,
266 (dh_constructor_t)openssl_diffie_hellman_create);
267 lib->crypto->add_dh(lib->crypto, MODP_4096_BIT,
268 (dh_constructor_t)openssl_diffie_hellman_create);
269 lib->crypto->add_dh(lib->crypto, MODP_6144_BIT,
270 (dh_constructor_t)openssl_diffie_hellman_create);
271 lib->crypto->add_dh(lib->crypto, MODP_8192_BIT,
272 (dh_constructor_t)openssl_diffie_hellman_create);
273 lib->crypto->add_dh(lib->crypto, MODP_1024_BIT,
274 (dh_constructor_t)openssl_diffie_hellman_create);
275 lib->crypto->add_dh(lib->crypto, MODP_768_BIT,
276 (dh_constructor_t)openssl_diffie_hellman_create);
277
278 /* rsa */
279 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
280 (builder_constructor_t)openssl_rsa_private_key_builder);
281 lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
282 (builder_constructor_t)openssl_rsa_public_key_builder);
283
284 /* ec */
285 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_ECDSA,
286 (builder_constructor_t)openssl_ec_private_key_builder);
287 lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_ECDSA,
288 (builder_constructor_t)openssl_ec_public_key_builder);
289
290 return &this->public.plugin;
291 }