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