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;
88 static int ENGINE_free_nolock(ENGINE *e);
90 /* These static functions starting with a lower case "engine_" always
91 * take place when CRYPTO_LOCK_ENGINE has been locked up. */
92 static int engine_list_add(ENGINE *e)
95 ENGINE *iterator = NULL;
99 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
100 ERR_R_PASSED_NULL_PARAMETER);
103 iterator = engine_list_head;
104 while(iterator && !conflict)
106 conflict = (strcmp(iterator->id, e->id) == 0);
107 iterator = iterator->next;
111 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
112 ENGINE_R_CONFLICTING_ENGINE_ID);
115 if(engine_list_head == NULL)
117 /* We are adding to an empty list. */
120 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
121 ENGINE_R_INTERNAL_LIST_ERROR);
124 engine_list_head = e;
129 /* We are adding to the tail of an existing list. */
130 if((engine_list_tail == NULL) ||
131 (engine_list_tail->next != NULL))
133 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
134 ENGINE_R_INTERNAL_LIST_ERROR);
137 engine_list_tail->next = e;
138 e->prev = engine_list_tail;
140 /* Having the engine in the list assumes a structural
143 engine_ref_debug(e, 0, 1)
144 /* However it came to be, e is the last item in the list. */
145 engine_list_tail = e;
150 static int engine_list_remove(ENGINE *e)
156 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
157 ERR_R_PASSED_NULL_PARAMETER);
160 /* We need to check that e is in our linked list! */
161 iterator = engine_list_head;
162 while(iterator && (iterator != e))
163 iterator = iterator->next;
166 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
167 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
170 /* un-link e from the chain. */
172 e->next->prev = e->prev;
174 e->prev->next = e->next;
175 /* Correct our head/tail if necessary. */
176 if(engine_list_head == e)
177 engine_list_head = e->next;
178 if(engine_list_tail == e)
179 engine_list_tail = e->prev;
180 ENGINE_free_nolock(e);
184 /* This check always takes place with CRYPTO_LOCK_ENGINE locked up
185 * so we're synchronised, but we can't call anything that tries to
186 * lock it again! :-) NB: For convenience (and code-clarity) we
187 * don't output errors for failures of the engine_list_add function
188 * as it will generate errors itself. */
189 static int engine_internal_check(void)
195 /* This is our first time up, we need to populate the list
196 * with our statically compiled-in engines. */
197 def_engine = ENGINE_openssl();
198 if(!engine_list_add(def_engine))
201 engine_list_flag = 1;
202 ENGINE_free_nolock(def_engine);
206 /* Get the first/last "ENGINE" type available. */
207 ENGINE *ENGINE_get_first(void)
211 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
212 if(engine_internal_check())
214 ret = engine_list_head;
218 engine_ref_debug(ret, 0, 1)
221 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
224 ENGINE *ENGINE_get_last(void)
228 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
229 if(engine_internal_check())
231 ret = engine_list_tail;
235 engine_ref_debug(ret, 0, 1)
238 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
242 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
243 ENGINE *ENGINE_get_next(ENGINE *e)
248 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
249 ERR_R_PASSED_NULL_PARAMETER);
252 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
256 /* Return a valid structural refernce to the next ENGINE */
258 engine_ref_debug(ret, 0, 1)
260 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
261 /* Release the structural reference to the previous ENGINE */
265 ENGINE *ENGINE_get_prev(ENGINE *e)
270 ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
271 ERR_R_PASSED_NULL_PARAMETER);
274 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
278 /* Return a valid structural reference to the next ENGINE */
280 engine_ref_debug(ret, 0, 1)
282 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
283 /* Release the structural reference to the previous ENGINE */
288 /* Add another "ENGINE" type into the list. */
289 int ENGINE_add(ENGINE *e)
294 ENGINEerr(ENGINE_F_ENGINE_ADD,
295 ERR_R_PASSED_NULL_PARAMETER);
298 if((e->id == NULL) || (e->name == NULL))
300 ENGINEerr(ENGINE_F_ENGINE_ADD,
301 ENGINE_R_ID_OR_NAME_MISSING);
303 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
304 if(!engine_internal_check() || !engine_list_add(e))
306 ENGINEerr(ENGINE_F_ENGINE_ADD,
307 ENGINE_R_INTERNAL_LIST_ERROR);
310 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
314 /* Remove an existing "ENGINE" type from the array. */
315 int ENGINE_remove(ENGINE *e)
320 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
321 ERR_R_PASSED_NULL_PARAMETER);
324 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
325 if(!engine_internal_check() || !engine_list_remove(e))
327 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
328 ENGINE_R_INTERNAL_LIST_ERROR);
331 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
335 ENGINE *ENGINE_by_id(const char *id)
337 ENGINE *iterator = NULL, *cp = NULL;
340 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
341 ERR_R_PASSED_NULL_PARAMETER);
344 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
345 if(!engine_internal_check())
346 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
347 ENGINE_R_INTERNAL_LIST_ERROR);
350 iterator = engine_list_head;
351 while(iterator && (strcmp(id, iterator->id) != 0))
352 iterator = iterator->next;
355 /* We need to return a structural reference. If this is
356 * a "dynamic" ENGINE type, make a duplicate - otherwise
357 * increment the existing ENGINE's reference count. */
358 if(iterator->flags & ENGINE_FLAGS_BY_ID_COPY)
365 ENGINE_cpy(cp, iterator);
371 iterator->struct_ref++;
372 engine_ref_debug(iterator, 0, 1)
376 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
378 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
379 ENGINE_R_NO_SUCH_ENGINE);
383 ENGINE *ENGINE_new(void)
387 ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
390 ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
393 memset(ret, 0, sizeof(ENGINE));
395 engine_ref_debug(ret, 0, 1)
396 CRYPTO_new_ex_data(engine_ex_data_stack, ret, &ret->ex_data);
400 int ENGINE_free(ENGINE *e)
406 ENGINEerr(ENGINE_F_ENGINE_FREE,
407 ERR_R_PASSED_NULL_PARAMETER);
410 i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
411 engine_ref_debug(e, 0, -1)
416 fprintf(stderr,"ENGINE_free, bad structural reference count\n");
420 CRYPTO_free_ex_data(engine_ex_data_stack, e, &e->ex_data);
425 static int ENGINE_free_nolock(ENGINE *e)
431 ENGINEerr(ENGINE_F_ENGINE_FREE,
432 ERR_R_PASSED_NULL_PARAMETER);
437 engine_ref_debug(e, 0, -1)
442 fprintf(stderr,"ENGINE_free, bad structural reference count\n");
446 CRYPTO_free_ex_data(engine_ex_data_stack, e, &e->ex_data);
451 int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
452 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
454 if(CRYPTO_get_ex_new_index(engine_ex_data_num, &engine_ex_data_stack,
455 argl, argp, new_func, dup_func, free_func) < 0)
457 return (engine_ex_data_num++);
460 int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
462 return(CRYPTO_set_ex_data(&e->ex_data, idx, arg));
465 void *ENGINE_get_ex_data(const ENGINE *e, int idx)
467 return(CRYPTO_get_ex_data(&e->ex_data, idx));
470 void ENGINE_cleanup(void)
472 ENGINE *iterator = engine_list_head;
474 while(iterator != NULL)
476 ENGINE_remove(iterator);
477 iterator = engine_list_head;
479 engine_list_flag = 0;
480 /* Also unset any "default" ENGINEs that may have been set up (a default
481 * constitutes a functional reference on an ENGINE and there's one for
482 * each algorithm). */
483 ENGINE_clear_defaults();
487 int ENGINE_set_id(ENGINE *e, const char *id)
491 ENGINEerr(ENGINE_F_ENGINE_SET_ID,
492 ERR_R_PASSED_NULL_PARAMETER);
499 int ENGINE_set_name(ENGINE *e, const char *name)
503 ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
504 ERR_R_PASSED_NULL_PARAMETER);
511 int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth)
513 #ifndef OPENSSL_NO_RSA
514 e->rsa_meth = rsa_meth;
521 int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth)
523 #ifndef OPENSSL_NO_DSA
524 e->dsa_meth = dsa_meth;
531 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth)
533 #ifndef OPENSSL_NO_DH
534 e->dh_meth = dh_meth;
541 int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth)
543 e->rand_meth = rand_meth;
547 int ENGINE_set_BN_mod_exp(ENGINE *e, BN_MOD_EXP bn_mod_exp)
549 e->bn_mod_exp = bn_mod_exp;
553 int ENGINE_set_BN_mod_exp_crt(ENGINE *e, BN_MOD_EXP_CRT bn_mod_exp_crt)
555 e->bn_mod_exp_crt = bn_mod_exp_crt;
559 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
565 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
567 e->finish = finish_f;
571 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
577 int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f)
579 e->load_privkey = loadpriv_f;
583 int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
585 e->load_pubkey = loadpub_f;
589 int ENGINE_set_flags(ENGINE *e, int flags)
595 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
597 e->cmd_defns = defns;
601 int ENGINE_cpy(ENGINE *dest, const ENGINE *src)
603 if(ENGINE_set_id(dest, ENGINE_get_id(src)) &&
604 ENGINE_set_name(dest, ENGINE_get_name(src)) &&
605 #ifndef OPENSSL_NO_RSA
606 ENGINE_set_RSA(dest, ENGINE_get_RSA(src)) &&
608 #ifndef OPENSSL_NO_RSA
609 ENGINE_set_DSA(dest, ENGINE_get_DSA(src)) &&
611 #ifndef OPENSSL_NO_RSA
612 ENGINE_set_DH(dest, ENGINE_get_DH(src)) &&
614 ENGINE_set_RAND(dest, ENGINE_get_RAND(src)) &&
615 ENGINE_set_BN_mod_exp(dest,
616 ENGINE_get_BN_mod_exp(src)) &&
617 ENGINE_set_BN_mod_exp_crt(dest,
618 ENGINE_get_BN_mod_exp_crt(src)) &&
619 ENGINE_set_init_function(dest,
620 ENGINE_get_init_function(src)) &&
621 ENGINE_set_finish_function(dest,
622 ENGINE_get_finish_function(src)) &&
623 ENGINE_set_ctrl_function(dest,
624 ENGINE_get_ctrl_function(src)) &&
625 ENGINE_set_load_privkey_function(dest,
626 ENGINE_get_load_privkey_function(src)) &&
627 ENGINE_set_load_pubkey_function(dest,
628 ENGINE_get_load_pubkey_function(src)) &&
629 ENGINE_set_flags(dest, ENGINE_get_flags(src)) &&
630 ENGINE_set_cmd_defns(dest, ENGINE_get_cmd_defns(src)))
635 const char *ENGINE_get_id(const ENGINE *e)
640 const char *ENGINE_get_name(const ENGINE *e)
645 const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e)
650 const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e)
655 const DH_METHOD *ENGINE_get_DH(const ENGINE *e)
660 const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e)
665 BN_MOD_EXP ENGINE_get_BN_mod_exp(const ENGINE *e)
667 return e->bn_mod_exp;
670 BN_MOD_EXP_CRT ENGINE_get_BN_mod_exp_crt(const ENGINE *e)
672 return e->bn_mod_exp_crt;
675 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
680 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
685 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
690 ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
692 return e->load_privkey;
695 ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
697 return e->load_pubkey;
700 int ENGINE_get_flags(const ENGINE *e)
705 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)