1 /* crypto/engine/engine_list.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 /* Weird "ex_data" handling. Some have suggested there's some problems with the
65 * CRYPTO_EX_DATA code (or model), but for now I'm implementing it exactly as
66 * it's done in crypto/rsa/. That way the usage and documentation of that can be
67 * used to assist here, and any changes or fixes made there should similarly map
68 * over here quite straightforwardly. */
69 static int engine_ex_data_num = 0;
70 static STACK_OF(CRYPTO_EX_DATA_FUNCS) *engine_ex_data_stack = NULL;
72 /* The linked-list of pointers to engine types. engine_list_head
73 * incorporates an implicit structural reference but engine_list_tail
74 * does not - the latter is a computational niceity and only points
75 * to something that is already pointed to by its predecessor in the
76 * list (or engine_list_head itself). In the same way, the use of the
77 * "prev" pointer in each ENGINE is to save excessive list iteration,
78 * it doesn't correspond to an extra structural reference. Hence,
79 * engine_list_head, and each non-null "next" pointer account for
80 * the list itself assuming exactly 1 structural reference on each
82 static ENGINE *engine_list_head = NULL;
83 static ENGINE *engine_list_tail = NULL;
84 /* A boolean switch, used to ensure we only initialise once. This
85 * is needed because the engine list may genuinely become empty during
86 * use (so we can't use engine_list_head as an indicator for example. */
87 static int engine_list_flag = 0;
89 /* These static functions starting with a lower case "engine_" always
90 * take place when CRYPTO_LOCK_ENGINE has been locked up. */
91 static int engine_list_add(ENGINE *e)
94 ENGINE *iterator = NULL;
98 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
99 ERR_R_PASSED_NULL_PARAMETER);
102 iterator = engine_list_head;
103 while(iterator && !conflict)
105 conflict = (strcmp(iterator->id, e->id) == 0);
106 iterator = iterator->next;
110 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
111 ENGINE_R_CONFLICTING_ENGINE_ID);
114 if(engine_list_head == NULL)
116 /* We are adding to an empty list. */
119 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
120 ENGINE_R_INTERNAL_LIST_ERROR);
123 engine_list_head = e;
128 /* We are adding to the tail of an existing list. */
129 if((engine_list_tail == NULL) ||
130 (engine_list_tail->next != NULL))
132 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
133 ENGINE_R_INTERNAL_LIST_ERROR);
136 engine_list_tail->next = e;
137 e->prev = engine_list_tail;
139 /* Having the engine in the list assumes a structural
142 engine_ref_debug(e, 0, 1)
143 /* However it came to be, e is the last item in the list. */
144 engine_list_tail = e;
149 static int engine_list_remove(ENGINE *e)
155 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
156 ERR_R_PASSED_NULL_PARAMETER);
159 /* We need to check that e is in our linked list! */
160 iterator = engine_list_head;
161 while(iterator && (iterator != e))
162 iterator = iterator->next;
165 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
166 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
169 /* un-link e from the chain. */
171 e->next->prev = e->prev;
173 e->prev->next = e->next;
174 /* Correct our head/tail if necessary. */
175 if(engine_list_head == e)
176 engine_list_head = e->next;
177 if(engine_list_tail == e)
178 engine_list_tail = e->prev;
179 /* remove our structural reference. */
181 engine_ref_debug(e, 0, -1)
185 /* This check always takes place with CRYPTO_LOCK_ENGINE locked up
186 * so we're synchronised, but we can't call anything that tries to
187 * lock it again! :-) NB: For convenience (and code-clarity) we
188 * don't output errors for failures of the engine_list_add function
189 * as it will generate errors itself. */
190 static int engine_internal_check(void)
196 /* This is our first time up, we need to populate the list
197 * with our statically compiled-in engines. */
198 def_engine = ENGINE_openssl();
199 if(!engine_list_add(def_engine))
202 engine_list_flag = 1;
204 ENGINE_free(def_engine);
206 /* We can't ENGINE_free() because the lock's already held */
207 def_engine->struct_ref--;
208 engine_ref_debug(def_engine, 0, -1)
213 /* Get the first/last "ENGINE" type available. */
214 ENGINE *ENGINE_get_first(void)
218 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
219 if(engine_internal_check())
221 ret = engine_list_head;
225 engine_ref_debug(ret, 0, 1)
228 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
231 ENGINE *ENGINE_get_last(void)
235 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
236 if(engine_internal_check())
238 ret = engine_list_tail;
242 engine_ref_debug(ret, 0, 1)
245 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
249 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
250 ENGINE *ENGINE_get_next(ENGINE *e)
255 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
256 ERR_R_PASSED_NULL_PARAMETER);
259 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
263 /* Return a valid structural refernce to the next ENGINE */
265 engine_ref_debug(ret, 0, 1)
267 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
268 /* Release the structural reference to the previous ENGINE */
272 ENGINE *ENGINE_get_prev(ENGINE *e)
277 ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
278 ERR_R_PASSED_NULL_PARAMETER);
281 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
285 /* Return a valid structural reference to the next ENGINE */
287 engine_ref_debug(ret, 0, 1)
289 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
290 /* Release the structural reference to the previous ENGINE */
295 /* Add another "ENGINE" type into the list. */
296 int ENGINE_add(ENGINE *e)
301 ENGINEerr(ENGINE_F_ENGINE_ADD,
302 ERR_R_PASSED_NULL_PARAMETER);
305 if((e->id == NULL) || (e->name == NULL))
307 ENGINEerr(ENGINE_F_ENGINE_ADD,
308 ENGINE_R_ID_OR_NAME_MISSING);
310 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
311 if(!engine_internal_check() || !engine_list_add(e))
313 ENGINEerr(ENGINE_F_ENGINE_ADD,
314 ENGINE_R_INTERNAL_LIST_ERROR);
317 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
321 /* Remove an existing "ENGINE" type from the array. */
322 int ENGINE_remove(ENGINE *e)
327 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
328 ERR_R_PASSED_NULL_PARAMETER);
331 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
332 if(!engine_internal_check() || !engine_list_remove(e))
334 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
335 ENGINE_R_INTERNAL_LIST_ERROR);
338 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
342 ENGINE *ENGINE_by_id(const char *id)
344 ENGINE *iterator = NULL, *cp = NULL;
347 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
348 ERR_R_PASSED_NULL_PARAMETER);
351 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
352 if(!engine_internal_check())
353 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
354 ENGINE_R_INTERNAL_LIST_ERROR);
357 iterator = engine_list_head;
358 while(iterator && (strcmp(id, iterator->id) != 0))
359 iterator = iterator->next;
362 /* We need to return a structural reference. If this is
363 * a "dynamic" ENGINE type, make a duplicate - otherwise
364 * increment the existing ENGINE's reference count. */
365 if(iterator->flags & ENGINE_FLAGS_BY_ID_COPY)
372 ENGINE_cpy(cp, iterator);
378 iterator->struct_ref++;
379 engine_ref_debug(iterator, 0, 1)
383 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
385 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
386 ENGINE_R_NO_SUCH_ENGINE);
390 ENGINE *ENGINE_new(void)
394 ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
397 ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
400 memset(ret, 0, sizeof(ENGINE));
402 engine_ref_debug(ret, 0, 1)
403 CRYPTO_new_ex_data(engine_ex_data_stack, ret, &ret->ex_data);
407 int ENGINE_free(ENGINE *e)
413 ENGINEerr(ENGINE_F_ENGINE_FREE,
414 ERR_R_PASSED_NULL_PARAMETER);
417 i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
418 engine_ref_debug(e, 0, -1)
423 fprintf(stderr,"ENGINE_free, bad structural reference count\n");
427 CRYPTO_free_ex_data(engine_ex_data_stack, e, &e->ex_data);
432 int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
433 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
435 engine_ex_data_num++;
436 return(CRYPTO_get_ex_new_index(engine_ex_data_num - 1,
437 &engine_ex_data_stack, argl, argp,
438 new_func, dup_func, free_func));
441 int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
443 return(CRYPTO_set_ex_data(&e->ex_data, idx, arg));
446 void *ENGINE_get_ex_data(const ENGINE *e, int idx)
448 return(CRYPTO_get_ex_data(&e->ex_data, idx));
451 void ENGINE_cleanup(void)
453 ENGINE *iterator = engine_list_head;
455 while(iterator != NULL)
457 ENGINE_remove(iterator);
458 iterator = engine_list_head;
460 engine_list_flag = 0;
461 /* Also unset any "default" ENGINEs that may have been set up (a default
462 * constitutes a functional reference on an ENGINE and there's one for
463 * each algorithm). */
464 ENGINE_clear_defaults();
468 int ENGINE_set_id(ENGINE *e, const char *id)
472 ENGINEerr(ENGINE_F_ENGINE_SET_ID,
473 ERR_R_PASSED_NULL_PARAMETER);
480 int ENGINE_set_name(ENGINE *e, const char *name)
484 ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
485 ERR_R_PASSED_NULL_PARAMETER);
492 int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth)
494 #ifndef OPENSSL_NO_RSA
495 e->rsa_meth = rsa_meth;
502 int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth)
504 #ifndef OPENSSL_NO_DSA
505 e->dsa_meth = dsa_meth;
512 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth)
514 #ifndef OPENSSL_NO_DH
515 e->dh_meth = dh_meth;
522 int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth)
524 e->rand_meth = rand_meth;
528 int ENGINE_set_BN_mod_exp(ENGINE *e, BN_MOD_EXP bn_mod_exp)
530 e->bn_mod_exp = bn_mod_exp;
534 int ENGINE_set_BN_mod_exp_crt(ENGINE *e, BN_MOD_EXP_CRT bn_mod_exp_crt)
536 e->bn_mod_exp_crt = bn_mod_exp_crt;
540 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
546 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
548 e->finish = finish_f;
552 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
558 int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f)
560 e->load_privkey = loadpriv_f;
564 int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
566 e->load_pubkey = loadpub_f;
570 int ENGINE_set_flags(ENGINE *e, int flags)
576 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
578 e->cmd_defns = defns;
582 int ENGINE_cpy(ENGINE *dest, const ENGINE *src)
584 if(ENGINE_set_id(dest, ENGINE_get_id(src)) &&
585 ENGINE_set_name(dest, ENGINE_get_name(src)) &&
586 #ifndef OPENSSL_NO_RSA
587 ENGINE_set_RSA(dest, ENGINE_get_RSA(src)) &&
589 #ifndef OPENSSL_NO_RSA
590 ENGINE_set_DSA(dest, ENGINE_get_DSA(src)) &&
592 #ifndef OPENSSL_NO_RSA
593 ENGINE_set_DH(dest, ENGINE_get_DH(src)) &&
595 ENGINE_set_RAND(dest, ENGINE_get_RAND(src)) &&
596 ENGINE_set_BN_mod_exp(dest,
597 ENGINE_get_BN_mod_exp(src)) &&
598 ENGINE_set_BN_mod_exp_crt(dest,
599 ENGINE_get_BN_mod_exp_crt(src)) &&
600 ENGINE_set_init_function(dest,
601 ENGINE_get_init_function(src)) &&
602 ENGINE_set_finish_function(dest,
603 ENGINE_get_finish_function(src)) &&
604 ENGINE_set_ctrl_function(dest,
605 ENGINE_get_ctrl_function(src)) &&
606 ENGINE_set_load_privkey_function(dest,
607 ENGINE_get_load_privkey_function(src)) &&
608 ENGINE_set_load_pubkey_function(dest,
609 ENGINE_get_load_pubkey_function(src)) &&
610 ENGINE_set_flags(dest, ENGINE_get_flags(src)) &&
611 ENGINE_set_cmd_defns(dest, ENGINE_get_cmd_defns(src)))
616 const char *ENGINE_get_id(const ENGINE *e)
621 const char *ENGINE_get_name(const ENGINE *e)
626 const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e)
631 const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e)
636 const DH_METHOD *ENGINE_get_DH(const ENGINE *e)
641 const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e)
646 BN_MOD_EXP ENGINE_get_BN_mod_exp(const ENGINE *e)
648 return e->bn_mod_exp;
651 BN_MOD_EXP_CRT ENGINE_get_BN_mod_exp_crt(const ENGINE *e)
653 return e->bn_mod_exp_crt;
656 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
661 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
666 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
671 ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
673 return e->load_privkey;
676 ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
678 return e->load_pubkey;
681 int ENGINE_get_flags(const ENGINE *e)
686 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)