]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/engine/engine_list.c
Merge the engine branch into the main trunk. All conflicts resolved.
[thirdparty/openssl.git] / crypto / engine / engine_list.c
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. */
74 static ENGINE *engine_list_head = NULL;
75 static 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. */
79 static 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. */
83 static 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
140 static 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. */
180 static 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;
188 #ifndef NO_HW
189 #ifndef NO_HW_CSWIFT
190 if(!engine_list_add(ENGINE_cswift()))
191 return 0;
192 #endif /* !NO_HW_CSWIFT */
193 #ifndef NO_HW_NCIPHER
194 if(!engine_list_add(ENGINE_ncipher()))
195 return 0;
196 #endif /* !NO_HW_NCIPHER */
197 #ifndef NO_HW_ATALLA
198 if(!engine_list_add(ENGINE_atalla()))
199 return 0;
200 #endif /* !NO_HW_ATALLA */
201 #ifndef NO_HW_NURON
202 if(!engine_list_add(ENGINE_nuron()))
203 return 0;
204 #endif /* !NO_HW_NURON */
205 #endif /* !NO_HW */
206 engine_list_flag = 1;
207 return 1;
208 }
209
210 /* Get the first/last "ENGINE" type available. */
211 ENGINE *ENGINE_get_first(void)
212 {
213 ENGINE *ret = NULL;
214
215 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
216 if(engine_internal_check())
217 {
218 ret = engine_list_head;
219 if(ret)
220 ret->struct_ref++;
221 }
222 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
223 return ret;
224 }
225 ENGINE *ENGINE_get_last(void)
226 {
227 ENGINE *ret = NULL;
228
229 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
230 if(engine_internal_check())
231 {
232 ret = engine_list_tail;
233 if(ret)
234 ret->struct_ref++;
235 }
236 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
237 return ret;
238 }
239
240 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
241 ENGINE *ENGINE_get_next(ENGINE *e)
242 {
243 ENGINE *ret = NULL;
244 if(e == NULL)
245 {
246 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
247 ERR_R_PASSED_NULL_PARAMETER);
248 return 0;
249 }
250 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
251 ret = e->next;
252 e->struct_ref--;
253 if(ret)
254 ret->struct_ref++;
255 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
256 return ret;
257 }
258 ENGINE *ENGINE_get_prev(ENGINE *e)
259 {
260 ENGINE *ret = NULL;
261 if(e == NULL)
262 {
263 ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
264 ERR_R_PASSED_NULL_PARAMETER);
265 return 0;
266 }
267 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
268 ret = e->prev;
269 e->struct_ref--;
270 if(ret)
271 ret->struct_ref++;
272 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
273 return ret;
274 }
275
276 /* Add another "ENGINE" type into the list. */
277 int ENGINE_add(ENGINE *e)
278 {
279 int to_return = 1;
280 if(e == NULL)
281 {
282 ENGINEerr(ENGINE_F_ENGINE_ADD,
283 ERR_R_PASSED_NULL_PARAMETER);
284 return 0;
285 }
286 if((e->id == NULL) || (e->name == NULL))
287 {
288 ENGINEerr(ENGINE_F_ENGINE_ADD,
289 ENGINE_R_ID_OR_NAME_MISSING);
290 }
291 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
292 if(!engine_internal_check() || !engine_list_add(e))
293 {
294 ENGINEerr(ENGINE_F_ENGINE_ADD,
295 ENGINE_R_INTERNAL_LIST_ERROR);
296 to_return = 0;
297 }
298 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
299 return to_return;
300 }
301
302 /* Remove an existing "ENGINE" type from the array. */
303 int ENGINE_remove(ENGINE *e)
304 {
305 int to_return = 1;
306 if(e == NULL)
307 {
308 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
309 ERR_R_PASSED_NULL_PARAMETER);
310 return 0;
311 }
312 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
313 if(!engine_internal_check() || !engine_list_remove(e))
314 {
315 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
316 ENGINE_R_INTERNAL_LIST_ERROR);
317 to_return = 0;
318 }
319 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
320 return to_return;
321 }
322
323 ENGINE *ENGINE_by_id(const char *id)
324 {
325 ENGINE *iterator = NULL;
326 if(id == NULL)
327 {
328 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
329 ERR_R_PASSED_NULL_PARAMETER);
330 return NULL;
331 }
332 CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
333 if(!engine_internal_check())
334 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
335 ENGINE_R_INTERNAL_LIST_ERROR);
336 else
337 {
338 iterator = engine_list_head;
339 while(iterator && (strcmp(id, iterator->id) != 0))
340 iterator = iterator->next;
341 if(iterator)
342 /* We need to return a structural reference */
343 iterator->struct_ref++;
344 }
345 CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
346 if(iterator == NULL)
347 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
348 ENGINE_R_NO_SUCH_ENGINE);
349 return iterator;
350 }
351
352 /* As per the comments in engine.h, it is generally better all round
353 * if the ENGINE structure is allocated within this framework. */
354 #if 0
355 int ENGINE_get_struct_size(void)
356 {
357 return sizeof(ENGINE);
358 }
359
360 ENGINE *ENGINE_new(ENGINE *e)
361 {
362 ENGINE *ret;
363
364 if(e == NULL)
365 {
366 ret = (ENGINE *)(OPENSSL_malloc(sizeof(ENGINE));
367 if(ret == NULL)
368 {
369 ENGINEerr(ENGINE_F_ENGINE_NEW,
370 ERR_R_MALLOC_FAILURE);
371 return NULL;
372 }
373 }
374 else
375 ret = e;
376 memset(ret, 0, sizeof(ENGINE));
377 if(e)
378 ret->flags = ENGINE_FLAGS_MALLOCED;
379 ret->struct_ref = 1;
380 return ret;
381 }
382 #else
383 ENGINE *ENGINE_new(void)
384 {
385 ENGINE *ret;
386
387 ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
388 if(ret == NULL)
389 {
390 ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
391 return NULL;
392 }
393 memset(ret, 0, sizeof(ENGINE));
394 ret->flags = ENGINE_FLAGS_MALLOCED;
395 ret->struct_ref = 1;
396 return ret;
397 }
398 #endif
399
400 int ENGINE_free(ENGINE *e)
401 {
402 int i;
403
404 if(e == NULL)
405 {
406 ENGINEerr(ENGINE_F_ENGINE_FREE,
407 ERR_R_PASSED_NULL_PARAMETER);
408 return 0;
409 }
410 i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
411 #ifdef REF_PRINT
412 REF_PRINT("ENGINE",e);
413 #endif
414 if (i > 0) return 1;
415 #ifdef REF_CHECK
416 if (i < 0)
417 {
418 fprintf(stderr,"ENGINE_free, bad reference count\n");
419 abort();
420 }
421 #endif
422 if(e->flags & ENGINE_FLAGS_MALLOCED)
423 OPENSSL_free(e);
424 return 1;
425 }
426
427 int ENGINE_set_id(ENGINE *e, const char *id)
428 {
429 if((e == NULL) || (id == NULL))
430 {
431 ENGINEerr(ENGINE_F_ENGINE_SET_ID,
432 ERR_R_PASSED_NULL_PARAMETER);
433 return 0;
434 }
435 e->id = id;
436 return 1;
437 }
438
439 int ENGINE_set_name(ENGINE *e, const char *name)
440 {
441 if((e == NULL) || (name == NULL))
442 {
443 ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
444 ERR_R_PASSED_NULL_PARAMETER);
445 return 0;
446 }
447 e->name = name;
448 return 1;
449 }
450
451 int ENGINE_set_RSA(ENGINE *e, RSA_METHOD *rsa_meth)
452 {
453 if((e == NULL) || (rsa_meth == NULL))
454 {
455 ENGINEerr(ENGINE_F_ENGINE_SET_RSA,
456 ERR_R_PASSED_NULL_PARAMETER);
457 return 0;
458 }
459 e->rsa_meth = rsa_meth;
460 return 1;
461 }
462
463 int ENGINE_set_DSA(ENGINE *e, DSA_METHOD *dsa_meth)
464 {
465 if((e == NULL) || (dsa_meth == NULL))
466 {
467 ENGINEerr(ENGINE_F_ENGINE_SET_DSA,
468 ERR_R_PASSED_NULL_PARAMETER);
469 return 0;
470 }
471 e->dsa_meth = dsa_meth;
472 return 1;
473 }
474
475 int ENGINE_set_DH(ENGINE *e, DH_METHOD *dh_meth)
476 {
477 if((e == NULL) || (dh_meth == NULL))
478 {
479 ENGINEerr(ENGINE_F_ENGINE_SET_DH,
480 ERR_R_PASSED_NULL_PARAMETER);
481 return 0;
482 }
483 e->dh_meth = dh_meth;
484 return 1;
485 }
486
487 int ENGINE_set_RAND(ENGINE *e, RAND_METHOD *rand_meth)
488 {
489 if((e == NULL) || (rand_meth == NULL))
490 {
491 ENGINEerr(ENGINE_F_ENGINE_SET_RAND,
492 ERR_R_PASSED_NULL_PARAMETER);
493 return 0;
494 }
495 e->rand_meth = rand_meth;
496 return 1;
497 }
498
499 int ENGINE_set_BN_mod_exp(ENGINE *e, BN_MOD_EXP bn_mod_exp)
500 {
501 if((e == NULL) || (bn_mod_exp == NULL))
502 {
503 ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP,
504 ERR_R_PASSED_NULL_PARAMETER);
505 return 0;
506 }
507 e->bn_mod_exp = bn_mod_exp;
508 return 1;
509 }
510
511 int ENGINE_set_BN_mod_exp_crt(ENGINE *e, BN_MOD_EXP_CRT bn_mod_exp_crt)
512 {
513 if((e == NULL) || (bn_mod_exp_crt == NULL))
514 {
515 ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP_CRT,
516 ERR_R_PASSED_NULL_PARAMETER);
517 return 0;
518 }
519 e->bn_mod_exp_crt = bn_mod_exp_crt;
520 return 1;
521 }
522
523 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
524 {
525 if((e == NULL) || (init_f == NULL))
526 {
527 ENGINEerr(ENGINE_F_ENGINE_SET_INIT_FUNCTION,
528 ERR_R_PASSED_NULL_PARAMETER);
529 return 0;
530 }
531 e->init = init_f;
532 return 1;
533 }
534
535 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
536 {
537 if((e == NULL) || (finish_f == NULL))
538 {
539 ENGINEerr(ENGINE_F_ENGINE_SET_FINISH_FUNCTION,
540 ERR_R_PASSED_NULL_PARAMETER);
541 return 0;
542 }
543 e->finish = finish_f;
544 return 1;
545 }
546
547 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
548 {
549 if((e == NULL) || (ctrl_f == NULL))
550 {
551 ENGINEerr(ENGINE_F_ENGINE_SET_CTRL_FUNCTION,
552 ERR_R_PASSED_NULL_PARAMETER);
553 return 0;
554 }
555 e->ctrl = ctrl_f;
556 return 1;
557 }
558
559 const char *ENGINE_get_id(ENGINE *e)
560 {
561 if(e == NULL)
562 {
563 ENGINEerr(ENGINE_F_ENGINE_GET_ID,
564 ERR_R_PASSED_NULL_PARAMETER);
565 return 0;
566 }
567 return e->id;
568 }
569
570 const char *ENGINE_get_name(ENGINE *e)
571 {
572 if(e == NULL)
573 {
574 ENGINEerr(ENGINE_F_ENGINE_GET_NAME,
575 ERR_R_PASSED_NULL_PARAMETER);
576 return 0;
577 }
578 return e->name;
579 }
580
581 RSA_METHOD *ENGINE_get_RSA(ENGINE *e)
582 {
583 if(e == NULL)
584 {
585 ENGINEerr(ENGINE_F_ENGINE_GET_RSA,
586 ERR_R_PASSED_NULL_PARAMETER);
587 return NULL;
588 }
589 return e->rsa_meth;
590 }
591
592 DSA_METHOD *ENGINE_get_DSA(ENGINE *e)
593 {
594 if(e == NULL)
595 {
596 ENGINEerr(ENGINE_F_ENGINE_GET_DSA,
597 ERR_R_PASSED_NULL_PARAMETER);
598 return NULL;
599 }
600 return e->dsa_meth;
601 }
602
603 DH_METHOD *ENGINE_get_DH(ENGINE *e)
604 {
605 if(e == NULL)
606 {
607 ENGINEerr(ENGINE_F_ENGINE_GET_DH,
608 ERR_R_PASSED_NULL_PARAMETER);
609 return NULL;
610 }
611 return e->dh_meth;
612 }
613
614 RAND_METHOD *ENGINE_get_RAND(ENGINE *e)
615 {
616 if(e == NULL)
617 {
618 ENGINEerr(ENGINE_F_ENGINE_GET_RAND,
619 ERR_R_PASSED_NULL_PARAMETER);
620 return NULL;
621 }
622 return e->rand_meth;
623 }
624
625 BN_MOD_EXP ENGINE_get_BN_mod_exp(ENGINE *e)
626 {
627 if(e == NULL)
628 {
629 ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP,
630 ERR_R_PASSED_NULL_PARAMETER);
631 return NULL;
632 }
633 return e->bn_mod_exp;
634 }
635
636 BN_MOD_EXP_CRT ENGINE_get_BN_mod_exp_crt(ENGINE *e)
637 {
638 if(e == NULL)
639 {
640 ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP_CRT,
641 ERR_R_PASSED_NULL_PARAMETER);
642 return NULL;
643 }
644 return e->bn_mod_exp_crt;
645 }
646
647 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(ENGINE *e)
648 {
649 if(e == NULL)
650 {
651 ENGINEerr(ENGINE_F_ENGINE_GET_INIT_FUNCTION,
652 ERR_R_PASSED_NULL_PARAMETER);
653 return NULL;
654 }
655 return e->init;
656 }
657
658 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(ENGINE *e)
659 {
660 if(e == NULL)
661 {
662 ENGINEerr(ENGINE_F_ENGINE_GET_FINISH_FUNCTION,
663 ERR_R_PASSED_NULL_PARAMETER);
664 return NULL;
665 }
666 return e->finish;
667 }
668
669 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(ENGINE *e)
670 {
671 if(e == NULL)
672 {
673 ENGINEerr(ENGINE_F_ENGINE_GET_CTRL_FUNCTION,
674 ERR_R_PASSED_NULL_PARAMETER);
675 return NULL;
676 }
677 return e->ctrl;
678 }
679