]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/engine/eng_list.c
Parse version numbers prefixed with text (egcs does that, even with
[thirdparty/openssl.git] / crypto / engine / eng_list.c
CommitLineData
2b671586 1/* crypto/engine/eng_list.c */
5270e702
RL
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
2b671586 6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
5270e702
RL
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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/)"
24 *
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.
29 *
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.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
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 * ====================================================================
52 *
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).
56 *
57 */
58
59#include <openssl/crypto.h>
60#include "cryptlib.h"
14cfde9c 61#include "eng_int.h"
5270e702
RL
62#include <openssl/engine.h>
63
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
73 * list member. */
74static ENGINE *engine_list_head = NULL;
75static ENGINE *engine_list_tail = NULL;
b6d1e52d
GT
76
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. */
79
80static void engine_list_cleanup(void)
81 {
82 ENGINE *iterator = engine_list_head;
83
84 while(iterator != NULL)
85 {
86 ENGINE_remove(iterator);
87 iterator = engine_list_head;
88 }
89 return;
90 }
5270e702
RL
91
92/* These static functions starting with a lower case "engine_" always
93 * take place when CRYPTO_LOCK_ENGINE has been locked up. */
94static int engine_list_add(ENGINE *e)
95 {
96 int conflict = 0;
97 ENGINE *iterator = NULL;
98
99 if(e == NULL)
100 {
101 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
102 ERR_R_PASSED_NULL_PARAMETER);
103 return 0;
104 }
105 iterator = engine_list_head;
106 while(iterator && !conflict)
107 {
108 conflict = (strcmp(iterator->id, e->id) == 0);
109 iterator = iterator->next;
110 }
111 if(conflict)
112 {
113 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
114 ENGINE_R_CONFLICTING_ENGINE_ID);
115 return 0;
116 }
117 if(engine_list_head == NULL)
118 {
119 /* We are adding to an empty list. */
120 if(engine_list_tail)
121 {
122 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
123 ENGINE_R_INTERNAL_LIST_ERROR);
124 return 0;
125 }
126 engine_list_head = e;
127 e->prev = NULL;
b6d1e52d
GT
128 /* The first time the list allocates, we should register the
129 * cleanup. */
130 engine_cleanup_add_last(engine_list_cleanup);
5270e702
RL
131 }
132 else
133 {
134 /* We are adding to the tail of an existing list. */
135 if((engine_list_tail == NULL) ||
136 (engine_list_tail->next != NULL))
137 {
138 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
139 ENGINE_R_INTERNAL_LIST_ERROR);
140 return 0;
141 }
142 engine_list_tail->next = e;
143 e->prev = engine_list_tail;
144 }
145 /* Having the engine in the list assumes a structural
146 * reference. */
147 e->struct_ref++;
b41f836e 148 engine_ref_debug(e, 0, 1)
5270e702
RL
149 /* However it came to be, e is the last item in the list. */
150 engine_list_tail = e;
151 e->next = NULL;
152 return 1;
153 }
154
155static int engine_list_remove(ENGINE *e)
156 {
157 ENGINE *iterator;
158
159 if(e == NULL)
160 {
161 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
162 ERR_R_PASSED_NULL_PARAMETER);
163 return 0;
164 }
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;
169 if(iterator == NULL)
170 {
171 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
172 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
173 return 0;
174 }
175 /* un-link e from the chain. */
176 if(e->next)
177 e->next->prev = e->prev;
178 if(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;
b6d1e52d 185 engine_free_util(e, 0);
5270e702
RL
186 return 1;
187 }
188
189/* Get the first/last "ENGINE" type available. */
190ENGINE *ENGINE_get_first(void)
191 {
b6d1e52d 192 ENGINE *ret;
5270e702
RL
193
194 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
b6d1e52d
GT
195 ret = engine_list_head;
196 if(ret)
5270e702 197 {
b6d1e52d
GT
198 ret->struct_ref++;
199 engine_ref_debug(ret, 0, 1)
5270e702
RL
200 }
201 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
202 return ret;
203 }
b6d1e52d 204
5270e702
RL
205ENGINE *ENGINE_get_last(void)
206 {
b6d1e52d 207 ENGINE *ret;
5270e702
RL
208
209 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
5270e702 210 ret = engine_list_tail;
b6d1e52d
GT
211 if(ret)
212 {
213 ret->struct_ref++;
214 engine_ref_debug(ret, 0, 1)
5270e702
RL
215 }
216 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
217 return ret;
218 }
219
220/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
221ENGINE *ENGINE_get_next(ENGINE *e)
222 {
223 ENGINE *ret = NULL;
224 if(e == NULL)
225 {
226 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
227 ERR_R_PASSED_NULL_PARAMETER);
228 return 0;
229 }
230 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
231 ret = e->next;
5270e702 232 if(ret)
b41f836e 233 {
ea3a429e 234 /* Return a valid structural refernce to the next ENGINE */
5270e702 235 ret->struct_ref++;
b41f836e
GT
236 engine_ref_debug(ret, 0, 1)
237 }
5270e702 238 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
ea3a429e
GT
239 /* Release the structural reference to the previous ENGINE */
240 ENGINE_free(e);
5270e702
RL
241 return ret;
242 }
b6d1e52d 243
5270e702
RL
244ENGINE *ENGINE_get_prev(ENGINE *e)
245 {
246 ENGINE *ret = NULL;
247 if(e == NULL)
248 {
249 ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
250 ERR_R_PASSED_NULL_PARAMETER);
251 return 0;
252 }
253 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
254 ret = e->prev;
5270e702 255 if(ret)
b41f836e 256 {
ea3a429e 257 /* Return a valid structural reference to the next ENGINE */
5270e702 258 ret->struct_ref++;
b41f836e
GT
259 engine_ref_debug(ret, 0, 1)
260 }
5270e702 261 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
ea3a429e
GT
262 /* Release the structural reference to the previous ENGINE */
263 ENGINE_free(e);
5270e702
RL
264 return ret;
265 }
266
267/* Add another "ENGINE" type into the list. */
268int ENGINE_add(ENGINE *e)
269 {
270 int to_return = 1;
271 if(e == NULL)
272 {
273 ENGINEerr(ENGINE_F_ENGINE_ADD,
274 ERR_R_PASSED_NULL_PARAMETER);
275 return 0;
276 }
277 if((e->id == NULL) || (e->name == NULL))
278 {
279 ENGINEerr(ENGINE_F_ENGINE_ADD,
280 ENGINE_R_ID_OR_NAME_MISSING);
281 }
282 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
b6d1e52d 283 if(!engine_list_add(e))
5270e702
RL
284 {
285 ENGINEerr(ENGINE_F_ENGINE_ADD,
286 ENGINE_R_INTERNAL_LIST_ERROR);
287 to_return = 0;
288 }
289 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
290 return to_return;
291 }
292
293/* Remove an existing "ENGINE" type from the array. */
294int ENGINE_remove(ENGINE *e)
295 {
296 int to_return = 1;
297 if(e == NULL)
298 {
299 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
300 ERR_R_PASSED_NULL_PARAMETER);
301 return 0;
302 }
303 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
b6d1e52d 304 if(!engine_list_remove(e))
5270e702
RL
305 {
306 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
307 ENGINE_R_INTERNAL_LIST_ERROR);
308 to_return = 0;
309 }
310 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
311 return to_return;
312 }
313
b6d1e52d
GT
314static void engine_cpy(ENGINE *dest, const ENGINE *src)
315 {
316 dest->id = src->id;
317 dest->name = src->name;
318#ifndef OPENSSL_NO_RSA
319 dest->rsa_meth = src->rsa_meth;
320#endif
321#ifndef OPENSSL_NO_DSA
322 dest->dsa_meth = src->dsa_meth;
323#endif
324#ifndef OPENSSL_NO_DH
325 dest->dh_meth = src->dh_meth;
4d94ae00
BM
326#endif
327#ifndef OPENSSL_NO_ECDSA
328 dest->ecdsa_meth = src->ecdsa_meth;
b6d1e52d
GT
329#endif
330 dest->rand_meth = src->rand_meth;
b370230b
GT
331 dest->ciphers = src->ciphers;
332 dest->digests = src->digests;
b6d1e52d
GT
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;
341 }
342
5270e702
RL
343ENGINE *ENGINE_by_id(const char *id)
344 {
b6d1e52d 345 ENGINE *iterator;
5270e702
RL
346 if(id == NULL)
347 {
348 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
349 ERR_R_PASSED_NULL_PARAMETER);
350 return NULL;
351 }
352 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
b6d1e52d
GT
353 iterator = engine_list_head;
354 while(iterator && (strcmp(id, iterator->id) != 0))
355 iterator = iterator->next;
356 if(iterator)
5270e702 357 {
b6d1e52d
GT
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)
0ce5f3e4 362 {
b6d1e52d
GT
363 ENGINE *cp = ENGINE_new();
364 if(!cp)
365 iterator = NULL;
0ce5f3e4 366 else
b41f836e 367 {
b6d1e52d
GT
368 engine_cpy(cp, iterator);
369 iterator = cp;
b41f836e 370 }
0ce5f3e4 371 }
b6d1e52d
GT
372 else
373 {
374 iterator->struct_ref++;
375 engine_ref_debug(iterator, 0, 1)
376 }
5270e702
RL
377 }
378 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
379 if(iterator == NULL)
c9501c22 380 {
5270e702
RL
381 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
382 ENGINE_R_NO_SUCH_ENGINE);
c9501c22
DSH
383 ERR_add_error_data(2, "id=", id);
384 }
5270e702
RL
385 return iterator;
386 }