]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/engine/engine_list.c
Structural references should never be decremented directly - so leave that
[thirdparty/openssl.git] / crypto / engine / engine_list.c
CommitLineData
5270e702
RL
1/* crypto/engine/engine_list.c */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
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"
61#include "engine_int.h"
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;
76/* A boolean switch, used to ensure we only initialise once. This
77 * is needed because the engine list may genuinely become empty during
78 * use (so we can't use engine_list_head as an indicator for example. */
79static int engine_list_flag = 0;
80
81/* These static functions starting with a lower case "engine_" always
82 * take place when CRYPTO_LOCK_ENGINE has been locked up. */
83static int engine_list_add(ENGINE *e)
84 {
85 int conflict = 0;
86 ENGINE *iterator = NULL;
87
88 if(e == NULL)
89 {
90 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
91 ERR_R_PASSED_NULL_PARAMETER);
92 return 0;
93 }
94 iterator = engine_list_head;
95 while(iterator && !conflict)
96 {
97 conflict = (strcmp(iterator->id, e->id) == 0);
98 iterator = iterator->next;
99 }
100 if(conflict)
101 {
102 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
103 ENGINE_R_CONFLICTING_ENGINE_ID);
104 return 0;
105 }
106 if(engine_list_head == NULL)
107 {
108 /* We are adding to an empty list. */
109 if(engine_list_tail)
110 {
111 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
112 ENGINE_R_INTERNAL_LIST_ERROR);
113 return 0;
114 }
115 engine_list_head = e;
116 e->prev = NULL;
117 }
118 else
119 {
120 /* We are adding to the tail of an existing list. */
121 if((engine_list_tail == NULL) ||
122 (engine_list_tail->next != NULL))
123 {
124 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
125 ENGINE_R_INTERNAL_LIST_ERROR);
126 return 0;
127 }
128 engine_list_tail->next = e;
129 e->prev = engine_list_tail;
130 }
131 /* Having the engine in the list assumes a structural
132 * reference. */
133 e->struct_ref++;
134 /* However it came to be, e is the last item in the list. */
135 engine_list_tail = e;
136 e->next = NULL;
137 return 1;
138 }
139
140static int engine_list_remove(ENGINE *e)
141 {
142 ENGINE *iterator;
143
144 if(e == NULL)
145 {
146 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
147 ERR_R_PASSED_NULL_PARAMETER);
148 return 0;
149 }
150 /* We need to check that e is in our linked list! */
151 iterator = engine_list_head;
152 while(iterator && (iterator != e))
153 iterator = iterator->next;
154 if(iterator == NULL)
155 {
156 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
157 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
158 return 0;
159 }
160 /* un-link e from the chain. */
161 if(e->next)
162 e->next->prev = e->prev;
163 if(e->prev)
164 e->prev->next = e->next;
165 /* Correct our head/tail if necessary. */
166 if(engine_list_head == e)
167 engine_list_head = e->next;
168 if(engine_list_tail == e)
169 engine_list_tail = e->prev;
170 /* remove our structural reference. */
171 e->struct_ref--;
172 return 1;
173 }
174
175/* This check always takes place with CRYPTO_LOCK_ENGINE locked up
176 * so we're synchronised, but we can't call anything that tries to
177 * lock it again! :-) NB: For convenience (and code-clarity) we
178 * don't output errors for failures of the engine_list_add function
179 * as it will generate errors itself. */
180static int engine_internal_check(void)
181 {
182 if(engine_list_flag)
183 return 1;
184 /* This is our first time up, we need to populate the list
185 * with our statically compiled-in engines. */
186 if(!engine_list_add(ENGINE_openssl()))
187 return 0;
5270e702
RL
188 engine_list_flag = 1;
189 return 1;
190 }
191
192/* Get the first/last "ENGINE" type available. */
193ENGINE *ENGINE_get_first(void)
194 {
195 ENGINE *ret = NULL;
196
197 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
198 if(engine_internal_check())
199 {
200 ret = engine_list_head;
201 if(ret)
202 ret->struct_ref++;
203 }
204 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
205 return ret;
206 }
207ENGINE *ENGINE_get_last(void)
208 {
209 ENGINE *ret = NULL;
210
211 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
212 if(engine_internal_check())
213 {
214 ret = engine_list_tail;
215 if(ret)
216 ret->struct_ref++;
217 }
218 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
219 return ret;
220 }
221
222/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
223ENGINE *ENGINE_get_next(ENGINE *e)
224 {
225 ENGINE *ret = NULL;
226 if(e == NULL)
227 {
228 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
229 ERR_R_PASSED_NULL_PARAMETER);
230 return 0;
231 }
232 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
233 ret = e->next;
5270e702 234 if(ret)
ea3a429e 235 /* Return a valid structural refernce to the next ENGINE */
5270e702
RL
236 ret->struct_ref++;
237 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
ea3a429e
GT
238 /* Release the structural reference to the previous ENGINE */
239 ENGINE_free(e);
5270e702
RL
240 return ret;
241 }
242ENGINE *ENGINE_get_prev(ENGINE *e)
243 {
244 ENGINE *ret = NULL;
245 if(e == NULL)
246 {
247 ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
248 ERR_R_PASSED_NULL_PARAMETER);
249 return 0;
250 }
251 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
252 ret = e->prev;
5270e702 253 if(ret)
ea3a429e 254 /* Return a valid structural reference to the next ENGINE */
5270e702
RL
255 ret->struct_ref++;
256 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
ea3a429e
GT
257 /* Release the structural reference to the previous ENGINE */
258 ENGINE_free(e);
5270e702
RL
259 return ret;
260 }
261
262/* Add another "ENGINE" type into the list. */
263int ENGINE_add(ENGINE *e)
264 {
265 int to_return = 1;
266 if(e == NULL)
267 {
268 ENGINEerr(ENGINE_F_ENGINE_ADD,
269 ERR_R_PASSED_NULL_PARAMETER);
270 return 0;
271 }
272 if((e->id == NULL) || (e->name == NULL))
273 {
274 ENGINEerr(ENGINE_F_ENGINE_ADD,
275 ENGINE_R_ID_OR_NAME_MISSING);
276 }
277 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
278 if(!engine_internal_check() || !engine_list_add(e))
279 {
280 ENGINEerr(ENGINE_F_ENGINE_ADD,
281 ENGINE_R_INTERNAL_LIST_ERROR);
282 to_return = 0;
283 }
284 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
285 return to_return;
286 }
287
288/* Remove an existing "ENGINE" type from the array. */
289int ENGINE_remove(ENGINE *e)
290 {
291 int to_return = 1;
292 if(e == NULL)
293 {
294 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
295 ERR_R_PASSED_NULL_PARAMETER);
296 return 0;
297 }
298 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
299 if(!engine_internal_check() || !engine_list_remove(e))
300 {
301 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
302 ENGINE_R_INTERNAL_LIST_ERROR);
303 to_return = 0;
304 }
305 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
306 return to_return;
307 }
308
309ENGINE *ENGINE_by_id(const char *id)
310 {
311 ENGINE *iterator = NULL;
312 if(id == NULL)
313 {
314 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
315 ERR_R_PASSED_NULL_PARAMETER);
316 return NULL;
317 }
318 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
319 if(!engine_internal_check())
320 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
321 ENGINE_R_INTERNAL_LIST_ERROR);
322 else
323 {
324 iterator = engine_list_head;
325 while(iterator && (strcmp(id, iterator->id) != 0))
326 iterator = iterator->next;
327 if(iterator)
328 /* We need to return a structural reference */
329 iterator->struct_ref++;
330 }
331 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
332 if(iterator == NULL)
333 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
334 ENGINE_R_NO_SUCH_ENGINE);
335 return iterator;
336 }
337
5270e702
RL
338ENGINE *ENGINE_new(void)
339 {
340 ENGINE *ret;
341
342 ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
343 if(ret == NULL)
344 {
345 ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
346 return NULL;
347 }
348 memset(ret, 0, sizeof(ENGINE));
349 ret->flags = ENGINE_FLAGS_MALLOCED;
350 ret->struct_ref = 1;
351 return ret;
352 }
5270e702
RL
353
354int ENGINE_free(ENGINE *e)
355 {
356 int i;
357
358 if(e == NULL)
359 {
360 ENGINEerr(ENGINE_F_ENGINE_FREE,
361 ERR_R_PASSED_NULL_PARAMETER);
362 return 0;
363 }
364 i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
365#ifdef REF_PRINT
366 REF_PRINT("ENGINE",e);
367#endif
368 if (i > 0) return 1;
369#ifdef REF_CHECK
370 if (i < 0)
371 {
372 fprintf(stderr,"ENGINE_free, bad reference count\n");
373 abort();
374 }
375#endif
376 if(e->flags & ENGINE_FLAGS_MALLOCED)
377 OPENSSL_free(e);
378 return 1;
379 }
380
381int ENGINE_set_id(ENGINE *e, const char *id)
382 {
383 if((e == NULL) || (id == NULL))
384 {
385 ENGINEerr(ENGINE_F_ENGINE_SET_ID,
386 ERR_R_PASSED_NULL_PARAMETER);
387 return 0;
388 }
389 e->id = id;
390 return 1;
391 }
392
393int ENGINE_set_name(ENGINE *e, const char *name)
394 {
395 if((e == NULL) || (name == NULL))
396 {
397 ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
398 ERR_R_PASSED_NULL_PARAMETER);
399 return 0;
400 }
401 e->name = name;
402 return 1;
403 }
404
10e473e9 405int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth)
5270e702
RL
406 {
407 if((e == NULL) || (rsa_meth == NULL))
408 {
409 ENGINEerr(ENGINE_F_ENGINE_SET_RSA,
410 ERR_R_PASSED_NULL_PARAMETER);
411 return 0;
412 }
413 e->rsa_meth = rsa_meth;
414 return 1;
415 }
416
a4aba800 417int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth)
5270e702
RL
418 {
419 if((e == NULL) || (dsa_meth == NULL))
420 {
421 ENGINEerr(ENGINE_F_ENGINE_SET_DSA,
422 ERR_R_PASSED_NULL_PARAMETER);
423 return 0;
424 }
425 e->dsa_meth = dsa_meth;
426 return 1;
427 }
428
f971ccb2 429int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth)
5270e702
RL
430 {
431 if((e == NULL) || (dh_meth == NULL))
432 {
433 ENGINEerr(ENGINE_F_ENGINE_SET_DH,
434 ERR_R_PASSED_NULL_PARAMETER);
435 return 0;
436 }
437 e->dh_meth = dh_meth;
438 return 1;
439 }
440
441int ENGINE_set_RAND(ENGINE *e, RAND_METHOD *rand_meth)
442 {
443 if((e == NULL) || (rand_meth == NULL))
444 {
445 ENGINEerr(ENGINE_F_ENGINE_SET_RAND,
446 ERR_R_PASSED_NULL_PARAMETER);
447 return 0;
448 }
449 e->rand_meth = rand_meth;
450 return 1;
451 }
452
453int ENGINE_set_BN_mod_exp(ENGINE *e, BN_MOD_EXP bn_mod_exp)
454 {
455 if((e == NULL) || (bn_mod_exp == NULL))
456 {
457 ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP,
458 ERR_R_PASSED_NULL_PARAMETER);
459 return 0;
460 }
461 e->bn_mod_exp = bn_mod_exp;
462 return 1;
463 }
464
465int ENGINE_set_BN_mod_exp_crt(ENGINE *e, BN_MOD_EXP_CRT bn_mod_exp_crt)
466 {
467 if((e == NULL) || (bn_mod_exp_crt == NULL))
468 {
469 ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP_CRT,
470 ERR_R_PASSED_NULL_PARAMETER);
471 return 0;
472 }
473 e->bn_mod_exp_crt = bn_mod_exp_crt;
474 return 1;
475 }
476
477int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
478 {
479 if((e == NULL) || (init_f == NULL))
480 {
481 ENGINEerr(ENGINE_F_ENGINE_SET_INIT_FUNCTION,
482 ERR_R_PASSED_NULL_PARAMETER);
483 return 0;
484 }
485 e->init = init_f;
486 return 1;
487 }
488
489int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
490 {
491 if((e == NULL) || (finish_f == NULL))
492 {
493 ENGINEerr(ENGINE_F_ENGINE_SET_FINISH_FUNCTION,
494 ERR_R_PASSED_NULL_PARAMETER);
495 return 0;
496 }
497 e->finish = finish_f;
498 return 1;
499 }
500
501int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
502 {
503 if((e == NULL) || (ctrl_f == NULL))
504 {
505 ENGINEerr(ENGINE_F_ENGINE_SET_CTRL_FUNCTION,
506 ERR_R_PASSED_NULL_PARAMETER);
507 return 0;
508 }
509 e->ctrl = ctrl_f;
510 return 1;
511 }
512
513const char *ENGINE_get_id(ENGINE *e)
514 {
515 if(e == NULL)
516 {
517 ENGINEerr(ENGINE_F_ENGINE_GET_ID,
518 ERR_R_PASSED_NULL_PARAMETER);
519 return 0;
520 }
521 return e->id;
522 }
523
524const char *ENGINE_get_name(ENGINE *e)
525 {
526 if(e == NULL)
527 {
528 ENGINEerr(ENGINE_F_ENGINE_GET_NAME,
529 ERR_R_PASSED_NULL_PARAMETER);
530 return 0;
531 }
532 return e->name;
533 }
534
10e473e9 535const RSA_METHOD *ENGINE_get_RSA(ENGINE *e)
5270e702
RL
536 {
537 if(e == NULL)
538 {
539 ENGINEerr(ENGINE_F_ENGINE_GET_RSA,
540 ERR_R_PASSED_NULL_PARAMETER);
541 return NULL;
542 }
543 return e->rsa_meth;
544 }
545
a4aba800 546const DSA_METHOD *ENGINE_get_DSA(ENGINE *e)
5270e702
RL
547 {
548 if(e == NULL)
549 {
550 ENGINEerr(ENGINE_F_ENGINE_GET_DSA,
551 ERR_R_PASSED_NULL_PARAMETER);
552 return NULL;
553 }
554 return e->dsa_meth;
555 }
556
f971ccb2 557const DH_METHOD *ENGINE_get_DH(ENGINE *e)
5270e702
RL
558 {
559 if(e == NULL)
560 {
561 ENGINEerr(ENGINE_F_ENGINE_GET_DH,
562 ERR_R_PASSED_NULL_PARAMETER);
563 return NULL;
564 }
565 return e->dh_meth;
566 }
567
568RAND_METHOD *ENGINE_get_RAND(ENGINE *e)
569 {
570 if(e == NULL)
571 {
572 ENGINEerr(ENGINE_F_ENGINE_GET_RAND,
573 ERR_R_PASSED_NULL_PARAMETER);
574 return NULL;
575 }
576 return e->rand_meth;
577 }
578
579BN_MOD_EXP ENGINE_get_BN_mod_exp(ENGINE *e)
580 {
581 if(e == NULL)
582 {
583 ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP,
584 ERR_R_PASSED_NULL_PARAMETER);
585 return NULL;
586 }
587 return e->bn_mod_exp;
588 }
589
590BN_MOD_EXP_CRT ENGINE_get_BN_mod_exp_crt(ENGINE *e)
591 {
592 if(e == NULL)
593 {
594 ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP_CRT,
595 ERR_R_PASSED_NULL_PARAMETER);
596 return NULL;
597 }
598 return e->bn_mod_exp_crt;
599 }
600
601ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(ENGINE *e)
602 {
603 if(e == NULL)
604 {
605 ENGINEerr(ENGINE_F_ENGINE_GET_INIT_FUNCTION,
606 ERR_R_PASSED_NULL_PARAMETER);
607 return NULL;
608 }
609 return e->init;
610 }
611
612ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(ENGINE *e)
613 {
614 if(e == NULL)
615 {
616 ENGINEerr(ENGINE_F_ENGINE_GET_FINISH_FUNCTION,
617 ERR_R_PASSED_NULL_PARAMETER);
618 return NULL;
619 }
620 return e->finish;
621 }
622
623ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(ENGINE *e)
624 {
625 if(e == NULL)
626 {
627 ENGINEerr(ENGINE_F_ENGINE_GET_CTRL_FUNCTION,
628 ERR_R_PASSED_NULL_PARAMETER);
629 return NULL;
630 }
631 return e->ctrl;
632 }
633