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