2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
19 #include <openssl/conf.h>
20 #include <openssl/evp.h>
21 #include <openssl/engine.h>
22 #include <openssl/crypto.h>
25 #include "openssl_plugin.h"
28 #include <utils/mutex.h>
29 #include "openssl_crypter.h"
30 #include "openssl_hasher.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"
38 typedef struct private_openssl_plugin_t private_openssl_plugin_t
;
41 * private data of openssl_plugin
43 struct private_openssl_plugin_t
{
48 openssl_plugin_t
public;
52 * Array of static mutexs, with CRYPTO_num_locks() mutex
54 static mutex_t
**mutex
= NULL
;
57 * Locking callback for static locks
59 static void locking_function(int mode
, int type
, const char *file
, int line
)
63 if (mode
& CRYPTO_LOCK
)
65 mutex
[type
]->lock(mutex
[type
]);
69 mutex
[type
]->unlock(mutex
[type
]);
75 * Implementation of dynlock
77 struct CRYPTO_dynlock_value
{
82 * Callback to create a dynamic lock
84 static struct CRYPTO_dynlock_value
*create_function(const char *file
, int line
)
86 struct CRYPTO_dynlock_value
*lock
;
88 lock
= malloc_thing(struct CRYPTO_dynlock_value
);
89 lock
->mutex
= mutex_create(MUTEX_DEFAULT
);
94 * Callback to (un-)lock a dynamic lock
96 static void lock_function(int mode
, struct CRYPTO_dynlock_value
*lock
,
97 const char *file
, int line
)
99 if (mode
& CRYPTO_LOCK
)
101 lock
->mutex
->lock(lock
->mutex
);
105 lock
->mutex
->unlock(lock
->mutex
);
110 * Callback to destroy a dynamic lock
112 static void destroy_function(struct CRYPTO_dynlock_value
*lock
,
113 const char *file
, int line
)
115 lock
->mutex
->destroy(lock
->mutex
);
120 * Thread-ID callback function
122 static unsigned long id_function(void)
124 return pthread_self();
128 * initialize OpenSSL for multi-threaded use
130 static void threading_init()
134 CRYPTO_set_id_callback(id_function
);
135 CRYPTO_set_locking_callback(locking_function
);
137 CRYPTO_set_dynlock_create_callback(create_function
);
138 CRYPTO_set_dynlock_lock_callback(lock_function
);
139 CRYPTO_set_dynlock_destroy_callback(destroy_function
);
141 num_locks
= CRYPTO_num_locks();
142 mutex
= malloc(sizeof(mutex_t
*) * num_locks
);
143 for (i
= 0; i
< num_locks
; i
++)
145 mutex
[i
] = mutex_create(MUTEX_DEFAULT
);
150 * cleanup OpenSSL threading locks
152 static void threading_cleanup()
156 num_locks
= CRYPTO_num_locks();
157 for (i
= 0; i
< num_locks
; i
++)
159 mutex
[i
]->destroy(mutex
[i
]);
166 * Implementation of openssl_plugin_t.destroy
168 static void destroy(private_openssl_plugin_t
*this)
170 lib
->crypto
->remove_crypter(lib
->crypto
,
171 (crypter_constructor_t
)openssl_crypter_create
);
172 lib
->crypto
->remove_hasher(lib
->crypto
,
173 (hasher_constructor_t
)openssl_hasher_create
);
174 lib
->crypto
->remove_dh(lib
->crypto
,
175 (dh_constructor_t
)openssl_diffie_hellman_create
);
176 lib
->crypto
->remove_dh(lib
->crypto
,
177 (dh_constructor_t
)openssl_ec_diffie_hellman_create
);
178 lib
->creds
->remove_builder(lib
->creds
,
179 (builder_constructor_t
)openssl_rsa_private_key_builder
);
180 lib
->creds
->remove_builder(lib
->creds
,
181 (builder_constructor_t
)openssl_rsa_public_key_builder
);
182 lib
->creds
->remove_builder(lib
->creds
,
183 (builder_constructor_t
)openssl_ec_private_key_builder
);
184 lib
->creds
->remove_builder(lib
->creds
,
185 (builder_constructor_t
)openssl_ec_public_key_builder
);
199 plugin_t
*plugin_create()
201 private_openssl_plugin_t
*this = malloc_thing(private_openssl_plugin_t
);
203 this->public.plugin
.destroy
= (void(*)(plugin_t
*))destroy
;
207 OPENSSL_config(NULL
);
208 OpenSSL_add_all_algorithms();
210 /* activate support for hardware accelerators */
211 ENGINE_load_builtin_engines();
212 ENGINE_register_all_complete();
215 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_AES_CBC
,
216 (crypter_constructor_t
)openssl_crypter_create
);
217 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_3DES
,
218 (crypter_constructor_t
)openssl_crypter_create
);
219 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_RC5
,
220 (crypter_constructor_t
)openssl_crypter_create
);
221 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_IDEA
,
222 (crypter_constructor_t
)openssl_crypter_create
);
223 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_CAST
,
224 (crypter_constructor_t
)openssl_crypter_create
);
225 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_BLOWFISH
,
226 (crypter_constructor_t
)openssl_crypter_create
);
227 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_DES
,
228 (crypter_constructor_t
)openssl_crypter_create
);
229 lib
->crypto
->add_crypter(lib
->crypto
, ENCR_NULL
,
230 (crypter_constructor_t
)openssl_crypter_create
);
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_MD5
,
238 (hasher_constructor_t
)openssl_hasher_create
);
239 lib
->crypto
->add_hasher(lib
->crypto
, HASH_SHA256
,
240 (hasher_constructor_t
)openssl_hasher_create
);
241 lib
->crypto
->add_hasher(lib
->crypto
, HASH_SHA384
,
242 (hasher_constructor_t
)openssl_hasher_create
);
243 lib
->crypto
->add_hasher(lib
->crypto
, HASH_SHA512
,
244 (hasher_constructor_t
)openssl_hasher_create
);
246 /* ec diffie hellman */
247 lib
->crypto
->add_dh(lib
->crypto
, ECP_192_BIT
,
248 (dh_constructor_t
)openssl_ec_diffie_hellman_create
);
249 lib
->crypto
->add_dh(lib
->crypto
, ECP_224_BIT
,
250 (dh_constructor_t
)openssl_ec_diffie_hellman_create
);
251 lib
->crypto
->add_dh(lib
->crypto
, ECP_256_BIT
,
252 (dh_constructor_t
)openssl_ec_diffie_hellman_create
);
253 lib
->crypto
->add_dh(lib
->crypto
, ECP_384_BIT
,
254 (dh_constructor_t
)openssl_ec_diffie_hellman_create
);
255 lib
->crypto
->add_dh(lib
->crypto
, ECP_521_BIT
,
256 (dh_constructor_t
)openssl_ec_diffie_hellman_create
);
259 lib
->crypto
->add_dh(lib
->crypto
, MODP_2048_BIT
,
260 (dh_constructor_t
)openssl_diffie_hellman_create
);
261 lib
->crypto
->add_dh(lib
->crypto
, MODP_1536_BIT
,
262 (dh_constructor_t
)openssl_diffie_hellman_create
);
263 lib
->crypto
->add_dh(lib
->crypto
, MODP_3072_BIT
,
264 (dh_constructor_t
)openssl_diffie_hellman_create
);
265 lib
->crypto
->add_dh(lib
->crypto
, MODP_4096_BIT
,
266 (dh_constructor_t
)openssl_diffie_hellman_create
);
267 lib
->crypto
->add_dh(lib
->crypto
, MODP_6144_BIT
,
268 (dh_constructor_t
)openssl_diffie_hellman_create
);
269 lib
->crypto
->add_dh(lib
->crypto
, MODP_8192_BIT
,
270 (dh_constructor_t
)openssl_diffie_hellman_create
);
271 lib
->crypto
->add_dh(lib
->crypto
, MODP_1024_BIT
,
272 (dh_constructor_t
)openssl_diffie_hellman_create
);
273 lib
->crypto
->add_dh(lib
->crypto
, MODP_768_BIT
,
274 (dh_constructor_t
)openssl_diffie_hellman_create
);
277 lib
->creds
->add_builder(lib
->creds
, CRED_PRIVATE_KEY
, KEY_RSA
,
278 (builder_constructor_t
)openssl_rsa_private_key_builder
);
279 lib
->creds
->add_builder(lib
->creds
, CRED_PUBLIC_KEY
, KEY_RSA
,
280 (builder_constructor_t
)openssl_rsa_public_key_builder
);
283 lib
->creds
->add_builder(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ECDSA
,
284 (builder_constructor_t
)openssl_ec_private_key_builder
);
285 lib
->creds
->add_builder(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ECDSA
,
286 (builder_constructor_t
)openssl_ec_public_key_builder
);
288 return &this->public.plugin
;