]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/engine/eng_list.c
Continue standardising malloc style for libcrypto
[thirdparty/openssl.git] / crypto / engine / eng_list.c
CommitLineData
2b671586 1/* crypto/engine/eng_list.c */
0f113f3e
MC
2/*
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4 * 2000.
5270e702
RL
5 */
6/* ====================================================================
2b671586 7 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
5270e702
RL
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
5270e702
RL
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
e172d60d
BM
59/* ====================================================================
60 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
0f113f3e 61 * ECDH support in OpenSSL originally developed by
e172d60d
BM
62 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
63 */
5270e702 64
14cfde9c 65#include "eng_int.h"
5270e702 66
0f113f3e
MC
67/*
68 * The linked-list of pointers to engine types. engine_list_head incorporates
69 * an implicit structural reference but engine_list_tail does not - the
70 * latter is a computational niceity and only points to something that is
71 * already pointed to by its predecessor in the list (or engine_list_head
72 * itself). In the same way, the use of the "prev" pointer in each ENGINE is
73 * to save excessive list iteration, it doesn't correspond to an extra
74 * structural reference. Hence, engine_list_head, and each non-null "next"
75 * pointer account for the list itself assuming exactly 1 structural
76 * reference on each list member.
77 */
5270e702
RL
78static ENGINE *engine_list_head = NULL;
79static ENGINE *engine_list_tail = NULL;
b6d1e52d 80
0f113f3e
MC
81/*
82 * This cleanup function is only needed internally. If it should be called,
83 * we register it with the "ENGINE_cleanup()" stack to be called during
84 * cleanup.
85 */
b6d1e52d
GT
86
87static void engine_list_cleanup(void)
0f113f3e
MC
88{
89 ENGINE *iterator = engine_list_head;
b6d1e52d 90
0f113f3e
MC
91 while (iterator != NULL) {
92 ENGINE_remove(iterator);
93 iterator = engine_list_head;
94 }
95 return;
96}
5270e702 97
0f113f3e
MC
98/*
99 * These static functions starting with a lower case "engine_" always take
100 * place when CRYPTO_LOCK_ENGINE has been locked up.
101 */
5270e702 102static int engine_list_add(ENGINE *e)
0f113f3e
MC
103{
104 int conflict = 0;
105 ENGINE *iterator = NULL;
5270e702 106
0f113f3e
MC
107 if (e == NULL) {
108 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
109 return 0;
110 }
111 iterator = engine_list_head;
112 while (iterator && !conflict) {
113 conflict = (strcmp(iterator->id, e->id) == 0);
114 iterator = iterator->next;
115 }
116 if (conflict) {
117 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
118 return 0;
119 }
120 if (engine_list_head == NULL) {
121 /* We are adding to an empty list. */
122 if (engine_list_tail) {
123 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
124 return 0;
125 }
126 engine_list_head = e;
127 e->prev = NULL;
128 /*
129 * The first time the list allocates, we should register the cleanup.
130 */
131 engine_cleanup_add_last(engine_list_cleanup);
132 } else {
133 /* We are adding to the tail of an existing list. */
134 if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
135 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
136 return 0;
137 }
138 engine_list_tail->next = e;
139 e->prev = engine_list_tail;
140 }
141 /*
142 * Having the engine in the list assumes a structural reference.
143 */
144 e->struct_ref++;
145 engine_ref_debug(e, 0, 1)
146 /* However it came to be, e is the last item in the list. */
147 engine_list_tail = e;
148 e->next = NULL;
149 return 1;
150}
5270e702
RL
151
152static int engine_list_remove(ENGINE *e)
0f113f3e
MC
153{
154 ENGINE *iterator;
5270e702 155
0f113f3e
MC
156 if (e == NULL) {
157 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
158 return 0;
159 }
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;
164 if (iterator == NULL) {
165 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
166 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
167 return 0;
168 }
169 /* un-link e from the chain. */
170 if (e->next)
171 e->next->prev = e->prev;
172 if (e->prev)
173 e->prev->next = e->next;
174 /* Correct our head/tail if necessary. */
175 if (engine_list_head == e)
176 engine_list_head = e->next;
177 if (engine_list_tail == e)
178 engine_list_tail = e->prev;
179 engine_free_util(e, 0);
180 return 1;
181}
5270e702
RL
182
183/* Get the first/last "ENGINE" type available. */
184ENGINE *ENGINE_get_first(void)
0f113f3e
MC
185{
186 ENGINE *ret;
5270e702 187
0f113f3e
MC
188 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
189 ret = engine_list_head;
190 if (ret) {
191 ret->struct_ref++;
192 engine_ref_debug(ret, 0, 1)
193 }
194 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
195 return ret;
196}
b6d1e52d 197
5270e702 198ENGINE *ENGINE_get_last(void)
0f113f3e
MC
199{
200 ENGINE *ret;
5270e702 201
0f113f3e
MC
202 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
203 ret = engine_list_tail;
204 if (ret) {
205 ret->struct_ref++;
206 engine_ref_debug(ret, 0, 1)
207 }
208 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
209 return ret;
210}
5270e702
RL
211
212/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
213ENGINE *ENGINE_get_next(ENGINE *e)
0f113f3e
MC
214{
215 ENGINE *ret = NULL;
216 if (e == NULL) {
217 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
218 return 0;
219 }
220 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
221 ret = e->next;
222 if (ret) {
223 /* Return a valid structural refernce to the next ENGINE */
224 ret->struct_ref++;
225 engine_ref_debug(ret, 0, 1)
226 }
227 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
228 /* Release the structural reference to the previous ENGINE */
229 ENGINE_free(e);
230 return ret;
231}
b6d1e52d 232
5270e702 233ENGINE *ENGINE_get_prev(ENGINE *e)
0f113f3e
MC
234{
235 ENGINE *ret = NULL;
236 if (e == NULL) {
237 ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
238 return 0;
239 }
240 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
241 ret = e->prev;
242 if (ret) {
243 /* Return a valid structural reference to the next ENGINE */
244 ret->struct_ref++;
245 engine_ref_debug(ret, 0, 1)
246 }
247 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
248 /* Release the structural reference to the previous ENGINE */
249 ENGINE_free(e);
250 return ret;
251}
5270e702
RL
252
253/* Add another "ENGINE" type into the list. */
254int ENGINE_add(ENGINE *e)
0f113f3e
MC
255{
256 int to_return = 1;
257 if (e == NULL) {
258 ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
259 return 0;
260 }
261 if ((e->id == NULL) || (e->name == NULL)) {
262 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
5850cc75 263 return 0;
0f113f3e
MC
264 }
265 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
266 if (!engine_list_add(e)) {
267 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
268 to_return = 0;
269 }
270 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
271 return to_return;
272}
5270e702
RL
273
274/* Remove an existing "ENGINE" type from the array. */
275int ENGINE_remove(ENGINE *e)
0f113f3e
MC
276{
277 int to_return = 1;
278 if (e == NULL) {
279 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
280 return 0;
281 }
282 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
283 if (!engine_list_remove(e)) {
284 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
285 to_return = 0;
286 }
287 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
288 return to_return;
289}
5270e702 290
b6d1e52d 291static void engine_cpy(ENGINE *dest, const ENGINE *src)
0f113f3e
MC
292{
293 dest->id = src->id;
294 dest->name = src->name;
b6d1e52d 295#ifndef OPENSSL_NO_RSA
0f113f3e 296 dest->rsa_meth = src->rsa_meth;
b6d1e52d
GT
297#endif
298#ifndef OPENSSL_NO_DSA
0f113f3e 299 dest->dsa_meth = src->dsa_meth;
b6d1e52d
GT
300#endif
301#ifndef OPENSSL_NO_DH
0f113f3e 302 dest->dh_meth = src->dh_meth;
4d94ae00 303#endif
10bf4fc2 304#ifndef OPENSSL_NO_EC
0f113f3e 305 dest->ecdh_meth = src->ecdh_meth;
0f113f3e 306 dest->ecdsa_meth = src->ecdsa_meth;
b6d1e52d 307#endif
0f113f3e
MC
308 dest->rand_meth = src->rand_meth;
309 dest->store_meth = src->store_meth;
310 dest->ciphers = src->ciphers;
311 dest->digests = src->digests;
312 dest->pkey_meths = src->pkey_meths;
313 dest->destroy = src->destroy;
314 dest->init = src->init;
315 dest->finish = src->finish;
316 dest->ctrl = src->ctrl;
317 dest->load_privkey = src->load_privkey;
318 dest->load_pubkey = src->load_pubkey;
319 dest->cmd_defns = src->cmd_defns;
320 dest->flags = src->flags;
321}
b6d1e52d 322
5270e702 323ENGINE *ENGINE_by_id(const char *id)
0f113f3e
MC
324{
325 ENGINE *iterator;
326 char *load_dir = NULL;
327 if (id == NULL) {
328 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
329 return NULL;
330 }
331 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
332 iterator = engine_list_head;
333 while (iterator && (strcmp(id, iterator->id) != 0))
334 iterator = iterator->next;
90945fa3 335 if (iterator != NULL) {
0f113f3e
MC
336 /*
337 * We need to return a structural reference. If this is an ENGINE
338 * type that returns copies, make a duplicate - otherwise increment
339 * the existing ENGINE's reference count.
340 */
341 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
342 ENGINE *cp = ENGINE_new();
90945fa3 343 if (cp == NULL)
0f113f3e
MC
344 iterator = NULL;
345 else {
346 engine_cpy(cp, iterator);
347 iterator = cp;
348 }
349 } else {
350 iterator->struct_ref++;
351 engine_ref_debug(iterator, 0, 1)
352 }
353 }
354 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
90945fa3 355 if (iterator != NULL)
0f113f3e
MC
356 return iterator;
357 /*
358 * Prevent infinite recusrion if we're looking for the dynamic engine.
359 */
360 if (strcmp(id, "dynamic")) {
361# ifdef OPENSSL_SYS_VMS
362 if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
363 load_dir = "SSLROOT:[ENGINES]";
364# else
365 if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
366 load_dir = ENGINESDIR;
367# endif
368 iterator = ENGINE_by_id("dynamic");
369 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
370 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
371 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
372 load_dir, 0) ||
373 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
374 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
375 goto notfound;
376 return iterator;
377 }
378 notfound:
379 ENGINE_free(iterator);
380 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
381 ERR_add_error_data(2, "id=", id);
382 return NULL;
383 /* EEK! Experimental code ends */
0f113f3e 384}
314c6670
GT
385
386int ENGINE_up_ref(ENGINE *e)
0f113f3e
MC
387{
388 if (e == NULL) {
389 ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
390 return 0;
391 }
392 CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
393 return 1;
394}