1 /* crypto/engine/engine_lib.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/crypto.h>
61 #include "engine_int.h"
62 #include <openssl/engine.h>
64 /* These pointers each have their own "functional reference" when they
65 * are non-NULL. Similarly, when they are retrieved by a call to
66 * ENGINE_get_default_[RSA|DSA|...] the returned pointer is also a
67 * reference and the caller is responsible for freeing that when they
68 * are finished with it (with a call to ENGINE_finish() *NOT* just
69 * ENGINE_free()!!!!!!). */
70 static ENGINE *engine_def_rsa = NULL;
71 static ENGINE *engine_def_dsa = NULL;
72 static ENGINE *engine_def_dh = NULL;
73 static ENGINE *engine_def_rand = NULL;
74 static ENGINE *engine_def_bn_mod_exp = NULL;
75 static ENGINE *engine_def_bn_mod_exp_crt = NULL;
76 /* A static "once-only" flag used to control if/when the above were
77 * initialised to suitable start-up defaults. */
78 static int engine_def_flag = 0;
80 /* This is used in certain static utility functions to save code
81 * repetition for per-algorithm functions. */
87 ENGINE_TYPE_BN_MOD_EXP,
88 ENGINE_TYPE_BN_MOD_EXP_CRT
91 static void engine_def_check_util(ENGINE **def, ENGINE *val)
98 /* In a slight break with convention - this static function must be
99 * called *outside* any locking of CRYPTO_LOCK_ENGINE. */
100 static void engine_def_check(void)
105 e = ENGINE_get_first();
107 /* The list is empty ... not much we can do! */
109 /* We have a structural reference, see if getting a functional
110 * reference is possible. This is done to cope with init errors
111 * in the engine - the following locked code does a bunch of
112 * manual "ENGINE_init"s which do *not* allow such an init
113 * error so this is worth doing. */
116 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
117 /* Doing another check here prevents an obvious race
118 * condition because the whole function itself cannot
121 goto skip_set_defaults;
122 /* OK, we got a functional reference, so we get one each
123 * for the defaults too. */
124 engine_def_check_util(&engine_def_rsa, e);
125 engine_def_check_util(&engine_def_dsa, e);
126 engine_def_check_util(&engine_def_dh, e);
127 engine_def_check_util(&engine_def_rand, e);
128 engine_def_check_util(&engine_def_bn_mod_exp, e);
129 engine_def_check_util(&engine_def_bn_mod_exp_crt, e);
132 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
133 /* The "if" needs to be balanced out. */
136 /* We need to balance out the fact we obtained a structural
137 * reference to begin with from ENGINE_get_first(). */
141 /* Initialise a engine type for use (or up its functional reference count
142 * if it's already in use). */
143 int ENGINE_init(ENGINE *e)
149 ENGINEerr(ENGINE_F_ENGINE_INIT,ERR_R_PASSED_NULL_PARAMETER);
152 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
153 if((e->funct_ref == 0) && e->init)
154 /* This is the first functional reference and the engine
155 * requires initialisation so we do it now. */
156 to_return = e->init();
159 /* OK, we return a functional reference which is also a
160 * structural reference. */
164 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
168 /* Free a functional reference to a engine type */
169 int ENGINE_finish(ENGINE *e)
175 ENGINEerr(ENGINE_F_ENGINE_FINISH,ERR_R_PASSED_NULL_PARAMETER);
178 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
179 if((e->funct_ref == 1) && e->finish)
181 /* This is the last functional reference and the engine
182 * requires cleanup so we do it now. */
183 to_return = e->finish();
186 /* Cleanup the functional reference which is also a
187 * structural reference. */
191 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
193 /* I'm going to deliberately do a convoluted version of this
194 * piece of code because we don't want "finish" functions
195 * being called inside a locked block of code, if at all
196 * possible. I'd rather have this call take an extra couple
197 * of ticks than have throughput serialised on a externally-
198 * provided callback function that may conceivably never come
201 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
202 /* CODE ALERT: This *IS* supposed to be "=" and NOT "==" :-) */
203 if((to_return = e->finish()))
205 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
206 /* Cleanup the functional reference which is also a
207 * structural reference. */
210 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
214 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
219 EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
220 const char *passphrase)
226 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
227 ERR_R_PASSED_NULL_PARAMETER);
230 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
231 if(e->funct_ref == 0)
233 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
234 ENGINE_R_NOT_INITIALISED);
237 if (!e->load_privkey)
239 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
240 ENGINE_R_NO_LOAD_FUNCTION);
243 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
244 pkey = e->load_privkey(key_id, passphrase);
247 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
248 ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
254 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
255 const char *passphrase)
261 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
262 ERR_R_PASSED_NULL_PARAMETER);
265 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
266 if(e->funct_ref == 0)
268 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
269 ENGINE_R_NOT_INITIALISED);
274 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
275 ENGINE_R_NO_LOAD_FUNCTION);
278 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
279 pkey = e->load_pubkey(key_id, passphrase);
282 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
283 ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
289 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
293 ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
296 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
297 if(e->struct_ref == 0)
299 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
304 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
307 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
308 return e->ctrl(cmd, i, p, f);
311 static ENGINE *engine_get_default_type(ENGINE_TYPE t)
315 /* engine_def_check is lean and mean and won't replace any
316 * prior default engines ... so we must ensure that it is always
317 * the first function to get to touch the default values. */
319 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
322 case ENGINE_TYPE_RSA:
323 ret = engine_def_rsa; break;
324 case ENGINE_TYPE_DSA:
325 ret = engine_def_dsa; break;
327 ret = engine_def_dh; break;
328 case ENGINE_TYPE_RAND:
329 ret = engine_def_rand; break;
330 case ENGINE_TYPE_BN_MOD_EXP:
331 ret = engine_def_bn_mod_exp; break;
332 case ENGINE_TYPE_BN_MOD_EXP_CRT:
333 ret = engine_def_bn_mod_exp_crt; break;
335 /* Unforunately we can't do this work outside the lock with a
336 * call to ENGINE_init() because that would leave a race
343 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
347 ENGINE *ENGINE_get_default_RSA(void)
349 return engine_get_default_type(ENGINE_TYPE_RSA);
352 ENGINE *ENGINE_get_default_DSA(void)
354 return engine_get_default_type(ENGINE_TYPE_DSA);
357 ENGINE *ENGINE_get_default_DH(void)
359 return engine_get_default_type(ENGINE_TYPE_DH);
362 ENGINE *ENGINE_get_default_RAND(void)
364 return engine_get_default_type(ENGINE_TYPE_RAND);
367 ENGINE *ENGINE_get_default_BN_mod_exp(void)
369 return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP);
372 ENGINE *ENGINE_get_default_BN_mod_exp_crt(void)
374 return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT);
377 static int engine_set_default_type(ENGINE_TYPE t, ENGINE *e)
383 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
384 ERR_R_PASSED_NULL_PARAMETER);
387 /* engine_def_check is lean and mean and won't replace any
388 * prior default engines ... so we must ensure that it is always
389 * the first function to get to touch the default values. */
391 /* Attempt to get a functional reference (we need one anyway, but
392 * also, 'e' may be just a structural reference being passed in so
393 * this call may actually be the first). */
396 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
397 ENGINE_R_INIT_FAILED);
400 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
403 case ENGINE_TYPE_RSA:
404 old = engine_def_rsa;
405 engine_def_rsa = e; break;
406 case ENGINE_TYPE_DSA:
407 old = engine_def_dsa;
408 engine_def_dsa = e; break;
411 engine_def_dh = e; break;
412 case ENGINE_TYPE_RAND:
413 old = engine_def_rand;
414 engine_def_rand = e; break;
415 case ENGINE_TYPE_BN_MOD_EXP:
416 old = engine_def_bn_mod_exp;
417 engine_def_bn_mod_exp = e; break;
418 case ENGINE_TYPE_BN_MOD_EXP_CRT:
419 old = engine_def_bn_mod_exp_crt;
420 engine_def_bn_mod_exp_crt = e; break;
422 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
423 /* If we've replaced a previous value, then we need to remove the
424 * functional reference we had. */
425 if(old && !ENGINE_finish(old))
427 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
428 ENGINE_R_FINISH_FAILED);
434 int ENGINE_set_default_RSA(ENGINE *e)
436 return engine_set_default_type(ENGINE_TYPE_RSA, e);
439 int ENGINE_set_default_DSA(ENGINE *e)
441 return engine_set_default_type(ENGINE_TYPE_DSA, e);
444 int ENGINE_set_default_DH(ENGINE *e)
446 return engine_set_default_type(ENGINE_TYPE_DH, e);
449 int ENGINE_set_default_RAND(ENGINE *e)
451 return engine_set_default_type(ENGINE_TYPE_RAND, e);
454 int ENGINE_set_default_BN_mod_exp(ENGINE *e)
456 return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP, e);
459 int ENGINE_set_default_BN_mod_exp_crt(ENGINE *e)
461 return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT, e);
464 int ENGINE_set_default(ENGINE *e, unsigned int flags)
466 if((flags & ENGINE_METHOD_RSA) && e->rsa_meth &&
467 !ENGINE_set_default_RSA(e))
469 if((flags & ENGINE_METHOD_DSA) && e->dsa_meth &&
470 !ENGINE_set_default_DSA(e))
472 if((flags & ENGINE_METHOD_DH) && e->dh_meth &&
473 !ENGINE_set_default_DH(e))
475 if((flags & ENGINE_METHOD_RAND) && e->rand_meth &&
476 !ENGINE_set_default_RAND(e))
478 if((flags & ENGINE_METHOD_BN_MOD_EXP) && e->bn_mod_exp &&
479 !ENGINE_set_default_BN_mod_exp(e))
481 if((flags & ENGINE_METHOD_BN_MOD_EXP_CRT) && e->bn_mod_exp_crt &&
482 !ENGINE_set_default_BN_mod_exp_crt(e))