1 /* crypto/engine/eng_list.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2001 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>
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
;
77 /* This cleanup function is only needed internally. If it should be called, we
78 * register it with the "ENGINE_cleanup()" stack to be called during cleanup. */
80 static void engine_list_cleanup(void)
82 ENGINE
*iterator
= engine_list_head
;
84 while(iterator
!= NULL
)
86 ENGINE_remove(iterator
);
87 iterator
= engine_list_head
;
92 /* These static functions starting with a lower case "engine_" always
93 * take place when CRYPTO_LOCK_ENGINE has been locked up. */
94 static int engine_list_add(ENGINE
*e
)
97 ENGINE
*iterator
= NULL
;
101 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
102 ERR_R_PASSED_NULL_PARAMETER
);
105 iterator
= engine_list_head
;
106 while(iterator
&& !conflict
)
108 conflict
= (strcmp(iterator
->id
, e
->id
) == 0);
109 iterator
= iterator
->next
;
113 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
114 ENGINE_R_CONFLICTING_ENGINE_ID
);
117 if(engine_list_head
== NULL
)
119 /* We are adding to an empty list. */
122 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
123 ENGINE_R_INTERNAL_LIST_ERROR
);
126 engine_list_head
= e
;
128 /* The first time the list allocates, we should register the
130 engine_cleanup_add_last(engine_list_cleanup
);
134 /* We are adding to the tail of an existing list. */
135 if((engine_list_tail
== NULL
) ||
136 (engine_list_tail
->next
!= NULL
))
138 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD
,
139 ENGINE_R_INTERNAL_LIST_ERROR
);
142 engine_list_tail
->next
= e
;
143 e
->prev
= engine_list_tail
;
145 /* Having the engine in the list assumes a structural
148 engine_ref_debug(e
, 0, 1)
149 /* However it came to be, e is the last item in the list. */
150 engine_list_tail
= e
;
155 static int engine_list_remove(ENGINE
*e
)
161 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE
,
162 ERR_R_PASSED_NULL_PARAMETER
);
165 /* We need to check that e is in our linked list! */
166 iterator
= engine_list_head
;
167 while(iterator
&& (iterator
!= e
))
168 iterator
= iterator
->next
;
171 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE
,
172 ENGINE_R_ENGINE_IS_NOT_IN_LIST
);
175 /* un-link e from the chain. */
177 e
->next
->prev
= e
->prev
;
179 e
->prev
->next
= e
->next
;
180 /* Correct our head/tail if necessary. */
181 if(engine_list_head
== e
)
182 engine_list_head
= e
->next
;
183 if(engine_list_tail
== e
)
184 engine_list_tail
= e
->prev
;
185 engine_free_util(e
, 0);
189 /* Get the first/last "ENGINE" type available. */
190 ENGINE
*ENGINE_get_first(void)
194 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
195 ret
= engine_list_head
;
199 engine_ref_debug(ret
, 0, 1)
201 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
205 ENGINE
*ENGINE_get_last(void)
209 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
210 ret
= engine_list_tail
;
214 engine_ref_debug(ret
, 0, 1)
216 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
220 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
221 ENGINE
*ENGINE_get_next(ENGINE
*e
)
226 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT
,
227 ERR_R_PASSED_NULL_PARAMETER
);
230 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
234 /* Return a valid structural refernce to the next ENGINE */
236 engine_ref_debug(ret
, 0, 1)
238 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
239 /* Release the structural reference to the previous ENGINE */
244 ENGINE
*ENGINE_get_prev(ENGINE
*e
)
249 ENGINEerr(ENGINE_F_ENGINE_GET_PREV
,
250 ERR_R_PASSED_NULL_PARAMETER
);
253 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
257 /* Return a valid structural reference to the next ENGINE */
259 engine_ref_debug(ret
, 0, 1)
261 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
262 /* Release the structural reference to the previous ENGINE */
267 /* Add another "ENGINE" type into the list. */
268 int ENGINE_add(ENGINE
*e
)
273 ENGINEerr(ENGINE_F_ENGINE_ADD
,
274 ERR_R_PASSED_NULL_PARAMETER
);
277 if((e
->id
== NULL
) || (e
->name
== NULL
))
279 ENGINEerr(ENGINE_F_ENGINE_ADD
,
280 ENGINE_R_ID_OR_NAME_MISSING
);
282 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
283 if(!engine_list_add(e
))
285 ENGINEerr(ENGINE_F_ENGINE_ADD
,
286 ENGINE_R_INTERNAL_LIST_ERROR
);
289 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
293 /* Remove an existing "ENGINE" type from the array. */
294 int ENGINE_remove(ENGINE
*e
)
299 ENGINEerr(ENGINE_F_ENGINE_REMOVE
,
300 ERR_R_PASSED_NULL_PARAMETER
);
303 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
304 if(!engine_list_remove(e
))
306 ENGINEerr(ENGINE_F_ENGINE_REMOVE
,
307 ENGINE_R_INTERNAL_LIST_ERROR
);
310 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
314 static void engine_cpy(ENGINE
*dest
, const ENGINE
*src
)
317 dest
->name
= src
->name
;
318 #ifndef OPENSSL_NO_RSA
319 dest
->rsa_meth
= src
->rsa_meth
;
321 #ifndef OPENSSL_NO_DSA
322 dest
->dsa_meth
= src
->dsa_meth
;
324 #ifndef OPENSSL_NO_DH
325 dest
->dh_meth
= src
->dh_meth
;
327 #ifndef OPENSSL_NO_ECDSA
328 dest
->ecdsa_meth
= src
->ecdsa_meth
;
330 dest
->rand_meth
= src
->rand_meth
;
331 dest
->ciphers
= src
->ciphers
;
332 dest
->digests
= src
->digests
;
333 dest
->destroy
= src
->destroy
;
334 dest
->init
= src
->init
;
335 dest
->finish
= src
->finish
;
336 dest
->ctrl
= src
->ctrl
;
337 dest
->load_privkey
= src
->load_privkey
;
338 dest
->load_pubkey
= src
->load_pubkey
;
339 dest
->cmd_defns
= src
->cmd_defns
;
340 dest
->flags
= src
->flags
;
343 ENGINE
*ENGINE_by_id(const char *id
)
348 ENGINEerr(ENGINE_F_ENGINE_BY_ID
,
349 ERR_R_PASSED_NULL_PARAMETER
);
352 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE
);
353 iterator
= engine_list_head
;
354 while(iterator
&& (strcmp(id
, iterator
->id
) != 0))
355 iterator
= iterator
->next
;
358 /* We need to return a structural reference. If this is an
359 * ENGINE type that returns copies, make a duplicate - otherwise
360 * increment the existing ENGINE's reference count. */
361 if(iterator
->flags
& ENGINE_FLAGS_BY_ID_COPY
)
363 ENGINE
*cp
= ENGINE_new();
368 engine_cpy(cp
, iterator
);
374 iterator
->struct_ref
++;
375 engine_ref_debug(iterator
, 0, 1)
378 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE
);
381 ENGINEerr(ENGINE_F_ENGINE_BY_ID
,
382 ENGINE_R_NO_SUCH_ENGINE
);
383 ERR_add_error_data(2, "id=", id
);