2 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
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.
67 * The linked-list of pointers to engine types. engine_list_head incorporates
68 * an implicit structural reference but engine_list_tail does not - the
69 * latter is a computational niceity and only points to something that is
70 * already pointed to by its predecessor in the list (or engine_list_head
71 * itself). In the same way, the use of the "prev" pointer in each ENGINE is
72 * to save excessive list iteration, it doesn't correspond to an extra
73 * structural reference. Hence, engine_list_head, and each non-null "next"
74 * pointer account for the list itself assuming exactly 1 structural
75 * reference on each list member.
77 static ENGINE *engine_list_head = NULL;
78 static ENGINE *engine_list_tail = NULL;
81 * This cleanup function is only needed internally. If it should be called,
82 * we register it with the "ENGINE_cleanup()" stack to be called during
86 static void engine_list_cleanup(void)
88 ENGINE *iterator = engine_list_head;
90 while (iterator != NULL) {
91 ENGINE_remove(iterator);
92 iterator = engine_list_head;
98 * These static functions starting with a lower case "engine_" always take
99 * place when CRYPTO_LOCK_ENGINE has been locked up.
101 static int engine_list_add(ENGINE *e)
104 ENGINE *iterator = NULL;
107 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
110 iterator = engine_list_head;
111 while (iterator && !conflict) {
112 conflict = (strcmp(iterator->id, e->id) == 0);
113 iterator = iterator->next;
116 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
119 if (engine_list_head == NULL) {
120 /* We are adding to an empty list. */
121 if (engine_list_tail) {
122 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
125 engine_list_head = e;
128 * The first time the list allocates, we should register the cleanup.
130 engine_cleanup_add_last(engine_list_cleanup);
132 /* We are adding to the tail of an existing list. */
133 if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
134 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
137 engine_list_tail->next = e;
138 e->prev = engine_list_tail;
141 * Having the engine in the list assumes a structural reference.
144 engine_ref_debug(e, 0, 1);
145 /* However it came to be, e is the last item in the list. */
146 engine_list_tail = e;
151 static int engine_list_remove(ENGINE *e)
156 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, 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;
163 if (iterator == NULL) {
164 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
165 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
168 /* un-link e from the chain. */
170 e->next->prev = e->prev;
172 e->prev->next = e->next;
173 /* Correct our head/tail if necessary. */
174 if (engine_list_head == e)
175 engine_list_head = e->next;
176 if (engine_list_tail == e)
177 engine_list_tail = e->prev;
178 engine_free_util(e, 0);
182 /* Get the first/last "ENGINE" type available. */
183 ENGINE *ENGINE_get_first(void)
187 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
188 ret = engine_list_head;
191 engine_ref_debug(ret, 0, 1);
193 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
197 ENGINE *ENGINE_get_last(void)
201 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
202 ret = engine_list_tail;
205 engine_ref_debug(ret, 0, 1);
207 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
211 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
212 ENGINE *ENGINE_get_next(ENGINE *e)
216 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
219 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
222 /* Return a valid structural reference to the next ENGINE */
224 engine_ref_debug(ret, 0, 1);
226 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
227 /* Release the structural reference to the previous ENGINE */
232 ENGINE *ENGINE_get_prev(ENGINE *e)
236 ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
239 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
242 /* Return a valid structural reference to the next ENGINE */
244 engine_ref_debug(ret, 0, 1);
246 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
247 /* Release the structural reference to the previous ENGINE */
252 /* Add another "ENGINE" type into the list. */
253 int ENGINE_add(ENGINE *e)
257 ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
260 if ((e->id == NULL) || (e->name == NULL)) {
261 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
264 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
265 if (!engine_list_add(e)) {
266 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
269 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
273 /* Remove an existing "ENGINE" type from the array. */
274 int ENGINE_remove(ENGINE *e)
278 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
281 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
282 if (!engine_list_remove(e)) {
283 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
286 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
290 static void engine_cpy(ENGINE *dest, const ENGINE *src)
293 dest->name = src->name;
294 #ifndef OPENSSL_NO_RSA
295 dest->rsa_meth = src->rsa_meth;
297 #ifndef OPENSSL_NO_DSA
298 dest->dsa_meth = src->dsa_meth;
300 #ifndef OPENSSL_NO_DH
301 dest->dh_meth = src->dh_meth;
303 #ifndef OPENSSL_NO_EC
304 dest->ec_meth = src->ec_meth;
306 dest->rand_meth = src->rand_meth;
307 dest->ciphers = src->ciphers;
308 dest->digests = src->digests;
309 dest->pkey_meths = src->pkey_meths;
310 dest->destroy = src->destroy;
311 dest->init = src->init;
312 dest->finish = src->finish;
313 dest->ctrl = src->ctrl;
314 dest->load_privkey = src->load_privkey;
315 dest->load_pubkey = src->load_pubkey;
316 dest->cmd_defns = src->cmd_defns;
317 dest->flags = src->flags;
320 ENGINE *ENGINE_by_id(const char *id)
323 char *load_dir = NULL;
325 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
328 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
329 iterator = engine_list_head;
330 while (iterator && (strcmp(id, iterator->id) != 0))
331 iterator = iterator->next;
332 if (iterator != NULL) {
334 * We need to return a structural reference. If this is an ENGINE
335 * type that returns copies, make a duplicate - otherwise increment
336 * the existing ENGINE's reference count.
338 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
339 ENGINE *cp = ENGINE_new();
343 engine_cpy(cp, iterator);
347 iterator->struct_ref++;
348 engine_ref_debug(iterator, 0, 1);
351 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
352 if (iterator != NULL)
355 * Prevent infinite recursion if we're looking for the dynamic engine.
357 if (strcmp(id, "dynamic")) {
358 if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
359 load_dir = ENGINESDIR;
360 iterator = ENGINE_by_id("dynamic");
361 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
362 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
363 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
365 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
366 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
371 ENGINE_free(iterator);
372 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
373 ERR_add_error_data(2, "id=", id);
375 /* EEK! Experimental code ends */
378 int ENGINE_up_ref(ENGINE *e)
381 ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
384 CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);