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).
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * ECDH support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
64 #include <openssl/crypto.h>
67 #include <openssl/engine.h>
69 /* The linked-list of pointers to engine types. engine_list_head
70 * incorporates an implicit structural reference but engine_list_tail
71 * does not - the latter is a computational niceity and only points
72 * to something that is already pointed to by its predecessor in the
73 * list (or engine_list_head itself). In the same way, the use of the
74 * "prev" pointer in each ENGINE is to save excessive list iteration,
75 * it doesn't correspond to an extra structural reference. Hence,
76 * engine_list_head, and each non-null "next" pointer account for
77 * the list itself assuming exactly 1 structural reference on each
79 static ENGINE *engine_list_head = NULL;
80 static ENGINE *engine_list_tail = NULL;
82 /* This cleanup function is only needed internally. If it should be called, we
83 * register it with the "ENGINE_cleanup()" stack to be called during cleanup. */
85 static void engine_list_cleanup(void)
87 ENGINE *iterator = engine_list_head;
89 while(iterator != NULL)
91 ENGINE_remove(iterator);
92 iterator = engine_list_head;
97 /* These static functions starting with a lower case "engine_" always
98 * take place when CRYPTO_LOCK_ENGINE has been locked up. */
99 static int engine_list_add(ENGINE *e)
102 ENGINE *iterator = NULL;
106 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
107 ERR_R_PASSED_NULL_PARAMETER);
110 iterator = engine_list_head;
111 while(iterator && !conflict)
113 conflict = (strcmp(iterator->id, e->id) == 0);
114 iterator = iterator->next;
118 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
119 ENGINE_R_CONFLICTING_ENGINE_ID);
122 if(engine_list_head == NULL)
124 /* We are adding to an empty list. */
127 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
128 ENGINE_R_INTERNAL_LIST_ERROR);
131 engine_list_head = e;
133 /* The first time the list allocates, we should register the
135 engine_cleanup_add_last(engine_list_cleanup);
139 /* We are adding to the tail of an existing list. */
140 if((engine_list_tail == NULL) ||
141 (engine_list_tail->next != NULL))
143 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
144 ENGINE_R_INTERNAL_LIST_ERROR);
147 engine_list_tail->next = e;
148 e->prev = engine_list_tail;
150 /* Having the engine in the list assumes a structural
153 engine_ref_debug(e, 0, 1)
154 /* However it came to be, e is the last item in the list. */
155 engine_list_tail = e;
160 static int engine_list_remove(ENGINE *e)
166 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
167 ERR_R_PASSED_NULL_PARAMETER);
170 /* We need to check that e is in our linked list! */
171 iterator = engine_list_head;
172 while(iterator && (iterator != e))
173 iterator = iterator->next;
176 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
177 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
180 /* un-link e from the chain. */
182 e->next->prev = e->prev;
184 e->prev->next = e->next;
185 /* Correct our head/tail if necessary. */
186 if(engine_list_head == e)
187 engine_list_head = e->next;
188 if(engine_list_tail == e)
189 engine_list_tail = e->prev;
190 engine_free_util(e, 0);
194 /* Get the first/last "ENGINE" type available. */
195 ENGINE *ENGINE_get_first(void)
199 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
200 ret = engine_list_head;
204 engine_ref_debug(ret, 0, 1)
206 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
210 ENGINE *ENGINE_get_last(void)
214 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
215 ret = engine_list_tail;
219 engine_ref_debug(ret, 0, 1)
221 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
225 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
226 ENGINE *ENGINE_get_next(ENGINE *e)
231 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
232 ERR_R_PASSED_NULL_PARAMETER);
235 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
239 /* Return a valid structural refernce to the next ENGINE */
241 engine_ref_debug(ret, 0, 1)
243 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
244 /* Release the structural reference to the previous ENGINE */
249 ENGINE *ENGINE_get_prev(ENGINE *e)
254 ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
255 ERR_R_PASSED_NULL_PARAMETER);
258 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
262 /* Return a valid structural reference to the next ENGINE */
264 engine_ref_debug(ret, 0, 1)
266 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
267 /* Release the structural reference to the previous ENGINE */
272 /* Add another "ENGINE" type into the list. */
273 int ENGINE_add(ENGINE *e)
278 ENGINEerr(ENGINE_F_ENGINE_ADD,
279 ERR_R_PASSED_NULL_PARAMETER);
282 if((e->id == NULL) || (e->name == NULL))
284 ENGINEerr(ENGINE_F_ENGINE_ADD,
285 ENGINE_R_ID_OR_NAME_MISSING);
287 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
288 if(!engine_list_add(e))
290 ENGINEerr(ENGINE_F_ENGINE_ADD,
291 ENGINE_R_INTERNAL_LIST_ERROR);
294 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
298 /* Remove an existing "ENGINE" type from the array. */
299 int ENGINE_remove(ENGINE *e)
304 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
305 ERR_R_PASSED_NULL_PARAMETER);
308 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
309 if(!engine_list_remove(e))
311 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
312 ENGINE_R_INTERNAL_LIST_ERROR);
315 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
319 static void engine_cpy(ENGINE *dest, const ENGINE *src)
322 dest->name = src->name;
323 #ifndef OPENSSL_NO_RSA
324 dest->rsa_meth = src->rsa_meth;
326 #ifndef OPENSSL_NO_DSA
327 dest->dsa_meth = src->dsa_meth;
329 #ifndef OPENSSL_NO_DH
330 dest->dh_meth = src->dh_meth;
332 #ifndef OPENSSL_NO_ECDH
333 dest->ecdh_meth = src->ecdh_meth;
335 #ifndef OPENSSL_NO_ECDSA
336 dest->ecdsa_meth = src->ecdsa_meth;
338 dest->rand_meth = src->rand_meth;
339 dest->ciphers = src->ciphers;
340 dest->digests = src->digests;
341 dest->destroy = src->destroy;
342 dest->init = src->init;
343 dest->finish = src->finish;
344 dest->ctrl = src->ctrl;
345 dest->load_privkey = src->load_privkey;
346 dest->load_pubkey = src->load_pubkey;
347 dest->cmd_defns = src->cmd_defns;
348 dest->flags = src->flags;
351 ENGINE *ENGINE_by_id(const char *id)
354 char *load_dir = NULL;
357 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
358 ERR_R_PASSED_NULL_PARAMETER);
361 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
362 iterator = engine_list_head;
363 while(iterator && (strcmp(id, iterator->id) != 0))
364 iterator = iterator->next;
367 /* We need to return a structural reference. If this is an
368 * ENGINE type that returns copies, make a duplicate - otherwise
369 * increment the existing ENGINE's reference count. */
370 if(iterator->flags & ENGINE_FLAGS_BY_ID_COPY)
372 ENGINE *cp = ENGINE_new();
377 engine_cpy(cp, iterator);
383 iterator->struct_ref++;
384 engine_ref_debug(iterator, 0, 1)
387 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
391 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
392 ENGINE_R_NO_SUCH_ENGINE);
393 ERR_add_error_data(2, "id=", id);
397 /* EEK! Experimental code starts */
398 if(iterator) return iterator;
399 #ifdef OPENSSL_SYS_VMS
400 if((load_dir = getenv("OPENSSL_ENGINES")) == 0) load_dir = "SSLROOT:[ENGINES]";
402 if((load_dir = getenv("OPENSSL_ENGINES")) == 0) load_dir = OPENSSLDIR "/engines";
404 iterator = ENGINE_by_id("dynamic");
405 if(!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
406 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
407 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
409 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
413 ENGINEerr(ENGINE_F_ENGINE_BY_ID,ENGINE_R_NO_SUCH_ENGINE);
414 ERR_add_error_data(2, "id=", id);
416 /* EEK! Experimental code ends */
420 int ENGINE_up_ref(ENGINE *e)
424 ENGINEerr(ENGINE_F_ENGINE_UP_REF,ERR_R_PASSED_NULL_PARAMETER);
427 CRYPTO_add(&e->struct_ref,1,CRYPTO_LOCK_ENGINE);