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 /* Initialise a engine type for use (or up its functional reference count
290 * if it's already in use). */
291 int ENGINE_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)())
295 ENGINEerr(ENGINE_F_ENGINE_CTRL
,ERR_R_PASSED_NULL_PARAMETER
);
298 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
299 if(e
->struct_ref
== 0)
301 ENGINEerr(ENGINE_F_ENGINE_CTRL
,ENGINE_R_NO_REFERENCE
);
306 ENGINEerr(ENGINE_F_ENGINE_CTRL
,ENGINE_R_NO_CONTROL_FUNCTION
);
309 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
310 return e
->ctrl(cmd
, i
, p
, f
);
313 static ENGINE
*engine_get_default_type(ENGINE_TYPE t
)
317 /* engine_def_check is lean and mean and won't replace any
318 * prior default engines ... so we must ensure that it is always
319 * the first function to get to touch the default values. */
321 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
324 case ENGINE_TYPE_RSA
:
325 ret
= engine_def_rsa
; break;
326 case ENGINE_TYPE_DSA
:
327 ret
= engine_def_dsa
; break;
329 ret
= engine_def_dh
; break;
330 case ENGINE_TYPE_RAND
:
331 ret
= engine_def_rand
; break;
332 case ENGINE_TYPE_BN_MOD_EXP
:
333 ret
= engine_def_bn_mod_exp
; break;
334 case ENGINE_TYPE_BN_MOD_EXP_CRT
:
335 ret
= engine_def_bn_mod_exp_crt
; break;
337 /* Unforunately we can't do this work outside the lock with a
338 * call to ENGINE_init() because that would leave a race
345 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
349 ENGINE
*ENGINE_get_default_RSA(void)
351 return engine_get_default_type(ENGINE_TYPE_RSA
);
354 ENGINE
*ENGINE_get_default_DSA(void)
356 return engine_get_default_type(ENGINE_TYPE_DSA
);
359 ENGINE
*ENGINE_get_default_DH(void)
361 return engine_get_default_type(ENGINE_TYPE_DH
);
364 ENGINE
*ENGINE_get_default_RAND(void)
366 return engine_get_default_type(ENGINE_TYPE_RAND
);
369 ENGINE
*ENGINE_get_default_BN_mod_exp(void)
371 return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP
);
374 ENGINE
*ENGINE_get_default_BN_mod_exp_crt(void)
376 return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT
);
379 static int engine_set_default_type(ENGINE_TYPE t
, ENGINE
*e
)
385 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE
,
386 ERR_R_PASSED_NULL_PARAMETER
);
389 /* engine_def_check is lean and mean and won't replace any
390 * prior default engines ... so we must ensure that it is always
391 * the first function to get to touch the default values. */
393 /* Attempt to get a functional reference (we need one anyway, but
394 * also, 'e' may be just a structural reference being passed in so
395 * this call may actually be the first). */
398 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE
,
399 ENGINE_R_INIT_FAILED
);
402 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
405 case ENGINE_TYPE_RSA
:
406 old
= engine_def_rsa
;
407 engine_def_rsa
= e
; break;
408 case ENGINE_TYPE_DSA
:
409 old
= engine_def_dsa
;
410 engine_def_dsa
= e
; break;
413 engine_def_dh
= e
; break;
414 case ENGINE_TYPE_RAND
:
415 old
= engine_def_rand
;
416 engine_def_rand
= e
; break;
417 case ENGINE_TYPE_BN_MOD_EXP
:
418 old
= engine_def_bn_mod_exp
;
419 engine_def_bn_mod_exp
= e
; break;
420 case ENGINE_TYPE_BN_MOD_EXP_CRT
:
421 old
= engine_def_bn_mod_exp_crt
;
422 engine_def_bn_mod_exp_crt
= e
; break;
424 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
425 /* If we've replaced a previous value, then we need to remove the
426 * functional reference we had. */
427 if(old
&& !ENGINE_finish(old
))
429 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE
,
430 ENGINE_R_FINISH_FAILED
);
436 int ENGINE_set_default_RSA(ENGINE
*e
)
438 return engine_set_default_type(ENGINE_TYPE_RSA
, e
);
441 int ENGINE_set_default_DSA(ENGINE
*e
)
443 return engine_set_default_type(ENGINE_TYPE_DSA
, e
);
446 int ENGINE_set_default_DH(ENGINE
*e
)
448 return engine_set_default_type(ENGINE_TYPE_DH
, e
);
451 int ENGINE_set_default_RAND(ENGINE
*e
)
453 return engine_set_default_type(ENGINE_TYPE_RAND
, e
);
456 int ENGINE_set_default_BN_mod_exp(ENGINE
*e
)
458 return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP
, e
);
461 int ENGINE_set_default_BN_mod_exp_crt(ENGINE
*e
)
463 return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT
, e
);
466 int ENGINE_set_default(ENGINE
*e
, unsigned int flags
)
468 if((flags
& ENGINE_METHOD_RSA
) && e
->rsa_meth
&&
469 !ENGINE_set_default_RSA(e
))
471 if((flags
& ENGINE_METHOD_DSA
) && e
->dsa_meth
&&
472 !ENGINE_set_default_DSA(e
))
474 if((flags
& ENGINE_METHOD_DH
) && e
->dh_meth
&&
475 !ENGINE_set_default_DH(e
))
477 if((flags
& ENGINE_METHOD_RAND
) && e
->rand_meth
&&
478 !ENGINE_set_default_RAND(e
))
480 if((flags
& ENGINE_METHOD_BN_MOD_EXP
) && e
->bn_mod_exp
&&
481 !ENGINE_set_default_BN_mod_exp(e
))
483 if((flags
& ENGINE_METHOD_BN_MOD_EXP_CRT
) && e
->bn_mod_exp_crt
&&
484 !ENGINE_set_default_BN_mod_exp_crt(e
))