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 /* The linked-list of pointers to engine types. engine_list_head
65 * incorporates an implicit structural reference but engine_list_tail
66 * does not - the latter is a computational niceity and only points
67 * to something that is already pointed to by its predecessor in the
68 * list (or engine_list_head itself). In the same way, the use of the
69 * "prev" pointer in each ENGINE is to save excessive list iteration,
70 * it doesn't correspond to an extra structural reference. Hence,
71 * engine_list_head, and each non-null "next" pointer account for
72 * the list itself assuming exactly 1 structural reference on each
74 static ENGINE
*engine_list_head
= NULL
;
75 static ENGINE
*engine_list_tail
= NULL
;
76 /* A boolean switch, used to ensure we only initialise once. This
77 * is needed because the engine list may genuinely become empty during
78 * use (so we can't use engine_list_head as an indicator for example. */
79 static int engine_list_flag
= 0;
81 /* These static functions starting with a lower case "engine_" always
82 * take place when CRYPTO_LOCK_ENGINE has been locked up. */
83 static int engine_list_add(ENGINE
*e
)
86 ENGINE
*iterator
= NULL
;
90 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
91 ERR_R_PASSED_NULL_PARAMETER
);
94 iterator
= engine_list_head
;
95 while(iterator
&& !conflict
)
97 conflict
= (strcmp(iterator
->id
, e
->id
) == 0);
98 iterator
= iterator
->next
;
102 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
103 ENGINE_R_CONFLICTING_ENGINE_ID
);
106 if(engine_list_head
== NULL
)
108 /* We are adding to an empty list. */
111 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
112 ENGINE_R_INTERNAL_LIST_ERROR
);
115 engine_list_head
= e
;
120 /* We are adding to the tail of an existing list. */
121 if((engine_list_tail
== NULL
) ||
122 (engine_list_tail
->next
!= NULL
))
124 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
125 ENGINE_R_INTERNAL_LIST_ERROR
);
128 engine_list_tail
->next
= e
;
129 e
->prev
= engine_list_tail
;
131 /* Having the engine in the list assumes a structural
134 /* However it came to be, e is the last item in the list. */
135 engine_list_tail
= e
;
140 static int engine_list_remove(ENGINE
*e
)
146 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE
,
147 ERR_R_PASSED_NULL_PARAMETER
);
150 /* We need to check that e is in our linked list! */
151 iterator
= engine_list_head
;
152 while(iterator
&& (iterator
!= e
))
153 iterator
= iterator
->next
;
156 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE
,
157 ENGINE_R_ENGINE_IS_NOT_IN_LIST
);
160 /* un-link e from the chain. */
162 e
->next
->prev
= e
->prev
;
164 e
->prev
->next
= e
->next
;
165 /* Correct our head/tail if necessary. */
166 if(engine_list_head
== e
)
167 engine_list_head
= e
->next
;
168 if(engine_list_tail
== e
)
169 engine_list_tail
= e
->prev
;
170 /* remove our structural reference. */
175 /* This check always takes place with CRYPTO_LOCK_ENGINE locked up
176 * so we're synchronised, but we can't call anything that tries to
177 * lock it again! :-) NB: For convenience (and code-clarity) we
178 * don't output errors for failures of the engine_list_add function
179 * as it will generate errors itself. */
180 static int engine_internal_check(void)
184 /* This is our first time up, we need to populate the list
185 * with our statically compiled-in engines. */
186 if(!engine_list_add(ENGINE_openssl()))
190 if(!engine_list_add(ENGINE_cswift()))
192 #endif /* !NO_HW_CSWIFT */
193 #ifndef NO_HW_NCIPHER
194 if(!engine_list_add(ENGINE_ncipher()))
196 #endif /* !NO_HW_NCIPHER */
198 if(!engine_list_add(ENGINE_atalla()))
200 #endif /* !NO_HW_ATALLA */
202 if(!engine_list_add(ENGINE_nuron()))
204 #endif /* !NO_HW_NURON */
206 engine_list_flag
= 1;
210 /* Get the first/last "ENGINE" type available. */
211 ENGINE
*ENGINE_get_first(void)
215 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
216 if(engine_internal_check())
218 ret
= engine_list_head
;
222 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
225 ENGINE
*ENGINE_get_last(void)
229 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
230 if(engine_internal_check())
232 ret
= engine_list_tail
;
236 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
240 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
241 ENGINE
*ENGINE_get_next(ENGINE
*e
)
246 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT
,
247 ERR_R_PASSED_NULL_PARAMETER
);
250 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
255 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
258 ENGINE
*ENGINE_get_prev(ENGINE
*e
)
263 ENGINEerr(ENGINE_F_ENGINE_GET_PREV
,
264 ERR_R_PASSED_NULL_PARAMETER
);
267 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
272 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
276 /* Add another "ENGINE" type into the list. */
277 int ENGINE_add(ENGINE
*e
)
282 ENGINEerr(ENGINE_F_ENGINE_ADD
,
283 ERR_R_PASSED_NULL_PARAMETER
);
286 if((e
->id
== NULL
) || (e
->name
== NULL
))
288 ENGINEerr(ENGINE_F_ENGINE_ADD
,
289 ENGINE_R_ID_OR_NAME_MISSING
);
291 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
292 if(!engine_internal_check() || !engine_list_add(e
))
294 ENGINEerr(ENGINE_F_ENGINE_ADD
,
295 ENGINE_R_INTERNAL_LIST_ERROR
);
298 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
302 /* Remove an existing "ENGINE" type from the array. */
303 int ENGINE_remove(ENGINE
*e
)
308 ENGINEerr(ENGINE_F_ENGINE_REMOVE
,
309 ERR_R_PASSED_NULL_PARAMETER
);
312 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
313 if(!engine_internal_check() || !engine_list_remove(e
))
315 ENGINEerr(ENGINE_F_ENGINE_REMOVE
,
316 ENGINE_R_INTERNAL_LIST_ERROR
);
319 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
323 ENGINE
*ENGINE_by_id(const char *id
)
325 ENGINE
*iterator
= NULL
;
328 ENGINEerr(ENGINE_F_ENGINE_BY_ID
,
329 ERR_R_PASSED_NULL_PARAMETER
);
332 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
333 if(!engine_internal_check())
334 ENGINEerr(ENGINE_F_ENGINE_BY_ID
,
335 ENGINE_R_INTERNAL_LIST_ERROR
);
338 iterator
= engine_list_head
;
339 while(iterator
&& (strcmp(id
, iterator
->id
) != 0))
340 iterator
= iterator
->next
;
342 /* We need to return a structural reference */
343 iterator
->struct_ref
++;
345 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
347 ENGINEerr(ENGINE_F_ENGINE_BY_ID
,
348 ENGINE_R_NO_SUCH_ENGINE
);
352 /* As per the comments in engine.h, it is generally better all round
353 * if the ENGINE structure is allocated within this framework. */
355 int ENGINE_get_struct_size(void)
357 return sizeof(ENGINE
);
360 ENGINE
*ENGINE_new(ENGINE
*e
)
366 ret
= (ENGINE
*)(OPENSSL_malloc(sizeof(ENGINE
));
369 ENGINEerr(ENGINE_F_ENGINE_NEW
,
370 ERR_R_MALLOC_FAILURE
);
376 memset(ret
, 0, sizeof(ENGINE
));
378 ret
->flags
= ENGINE_FLAGS_MALLOCED
;
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
));
394 ret
->flags
= ENGINE_FLAGS_MALLOCED
;
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
);
412 REF_PRINT("ENGINE",e
);
418 fprintf(stderr
,"ENGINE_free, bad reference count\n");
422 if(e
->flags
& ENGINE_FLAGS_MALLOCED
)
427 int ENGINE_set_id(ENGINE
*e
, const char *id
)
429 if((e
== NULL
) || (id
== NULL
))
431 ENGINEerr(ENGINE_F_ENGINE_SET_ID
,
432 ERR_R_PASSED_NULL_PARAMETER
);
439 int ENGINE_set_name(ENGINE
*e
, const char *name
)
441 if((e
== NULL
) || (name
== NULL
))
443 ENGINEerr(ENGINE_F_ENGINE_SET_NAME
,
444 ERR_R_PASSED_NULL_PARAMETER
);
451 int ENGINE_set_RSA(ENGINE
*e
, RSA_METHOD
*rsa_meth
)
453 if((e
== NULL
) || (rsa_meth
== NULL
))
455 ENGINEerr(ENGINE_F_ENGINE_SET_RSA
,
456 ERR_R_PASSED_NULL_PARAMETER
);
459 e
->rsa_meth
= rsa_meth
;
463 int ENGINE_set_DSA(ENGINE
*e
, DSA_METHOD
*dsa_meth
)
465 if((e
== NULL
) || (dsa_meth
== NULL
))
467 ENGINEerr(ENGINE_F_ENGINE_SET_DSA
,
468 ERR_R_PASSED_NULL_PARAMETER
);
471 e
->dsa_meth
= dsa_meth
;
475 int ENGINE_set_DH(ENGINE
*e
, DH_METHOD
*dh_meth
)
477 if((e
== NULL
) || (dh_meth
== NULL
))
479 ENGINEerr(ENGINE_F_ENGINE_SET_DH
,
480 ERR_R_PASSED_NULL_PARAMETER
);
483 e
->dh_meth
= dh_meth
;
487 int ENGINE_set_RAND(ENGINE
*e
, RAND_METHOD
*rand_meth
)
489 if((e
== NULL
) || (rand_meth
== NULL
))
491 ENGINEerr(ENGINE_F_ENGINE_SET_RAND
,
492 ERR_R_PASSED_NULL_PARAMETER
);
495 e
->rand_meth
= rand_meth
;
499 int ENGINE_set_BN_mod_exp(ENGINE
*e
, BN_MOD_EXP bn_mod_exp
)
501 if((e
== NULL
) || (bn_mod_exp
== NULL
))
503 ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP
,
504 ERR_R_PASSED_NULL_PARAMETER
);
507 e
->bn_mod_exp
= bn_mod_exp
;
511 int ENGINE_set_BN_mod_exp_crt(ENGINE
*e
, BN_MOD_EXP_CRT bn_mod_exp_crt
)
513 if((e
== NULL
) || (bn_mod_exp_crt
== NULL
))
515 ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP_CRT
,
516 ERR_R_PASSED_NULL_PARAMETER
);
519 e
->bn_mod_exp_crt
= bn_mod_exp_crt
;
523 int ENGINE_set_init_function(ENGINE
*e
, ENGINE_GEN_INT_FUNC_PTR init_f
)
525 if((e
== NULL
) || (init_f
== NULL
))
527 ENGINEerr(ENGINE_F_ENGINE_SET_INIT_FUNCTION
,
528 ERR_R_PASSED_NULL_PARAMETER
);
535 int ENGINE_set_finish_function(ENGINE
*e
, ENGINE_GEN_INT_FUNC_PTR finish_f
)
537 if((e
== NULL
) || (finish_f
== NULL
))
539 ENGINEerr(ENGINE_F_ENGINE_SET_FINISH_FUNCTION
,
540 ERR_R_PASSED_NULL_PARAMETER
);
543 e
->finish
= finish_f
;
547 int ENGINE_set_ctrl_function(ENGINE
*e
, ENGINE_CTRL_FUNC_PTR ctrl_f
)
549 if((e
== NULL
) || (ctrl_f
== NULL
))
551 ENGINEerr(ENGINE_F_ENGINE_SET_CTRL_FUNCTION
,
552 ERR_R_PASSED_NULL_PARAMETER
);
559 const char *ENGINE_get_id(ENGINE
*e
)
563 ENGINEerr(ENGINE_F_ENGINE_GET_ID
,
564 ERR_R_PASSED_NULL_PARAMETER
);
570 const char *ENGINE_get_name(ENGINE
*e
)
574 ENGINEerr(ENGINE_F_ENGINE_GET_NAME
,
575 ERR_R_PASSED_NULL_PARAMETER
);
581 RSA_METHOD
*ENGINE_get_RSA(ENGINE
*e
)
585 ENGINEerr(ENGINE_F_ENGINE_GET_RSA
,
586 ERR_R_PASSED_NULL_PARAMETER
);
592 DSA_METHOD
*ENGINE_get_DSA(ENGINE
*e
)
596 ENGINEerr(ENGINE_F_ENGINE_GET_DSA
,
597 ERR_R_PASSED_NULL_PARAMETER
);
603 DH_METHOD
*ENGINE_get_DH(ENGINE
*e
)
607 ENGINEerr(ENGINE_F_ENGINE_GET_DH
,
608 ERR_R_PASSED_NULL_PARAMETER
);
614 RAND_METHOD
*ENGINE_get_RAND(ENGINE
*e
)
618 ENGINEerr(ENGINE_F_ENGINE_GET_RAND
,
619 ERR_R_PASSED_NULL_PARAMETER
);
625 BN_MOD_EXP
ENGINE_get_BN_mod_exp(ENGINE
*e
)
629 ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP
,
630 ERR_R_PASSED_NULL_PARAMETER
);
633 return e
->bn_mod_exp
;
636 BN_MOD_EXP_CRT
ENGINE_get_BN_mod_exp_crt(ENGINE
*e
)
640 ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP_CRT
,
641 ERR_R_PASSED_NULL_PARAMETER
);
644 return e
->bn_mod_exp_crt
;
647 ENGINE_GEN_INT_FUNC_PTR
ENGINE_get_init_function(ENGINE
*e
)
651 ENGINEerr(ENGINE_F_ENGINE_GET_INIT_FUNCTION
,
652 ERR_R_PASSED_NULL_PARAMETER
);
658 ENGINE_GEN_INT_FUNC_PTR
ENGINE_get_finish_function(ENGINE
*e
)
662 ENGINEerr(ENGINE_F_ENGINE_GET_FINISH_FUNCTION
,
663 ERR_R_PASSED_NULL_PARAMETER
);
669 ENGINE_CTRL_FUNC_PTR
ENGINE_get_ctrl_function(ENGINE
*e
)
673 ENGINEerr(ENGINE_F_ENGINE_GET_CTRL_FUNCTION
,
674 ERR_R_PASSED_NULL_PARAMETER
);