This adds 2 things to the ENGINE code.
[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 /* Weird "ex_data" handling. Some have suggested there's some problems with the
65  * CRYPTO_EX_DATA code (or model), but for now I'm implementing it exactly as
66  * it's done in crypto/rsa/. That way the usage and documentation of that can be
67  * used to assist here, and any changes or fixes made there should similarly map
68  * over here quite straightforwardly. */
69 static int engine_ex_data_num = 0;
70 static STACK_OF(CRYPTO_EX_DATA_FUNCS) *engine_ex_data_stack = NULL;
71
72 /* The linked-list of pointers to engine types. engine_list_head
73  * incorporates an implicit structural reference but engine_list_tail
74  * does not - the latter is a computational niceity and only points
75  * to something that is already pointed to by its predecessor in the
76  * list (or engine_list_head itself). In the same way, the use of the
77  * "prev" pointer in each ENGINE is to save excessive list iteration,
78  * it doesn't correspond to an extra structural reference. Hence,
79  * engine_list_head, and each non-null "next" pointer account for
80  * the list itself assuming exactly 1 structural reference on each
81  * list member. */
82 static ENGINE *engine_list_head = NULL;
83 static ENGINE *engine_list_tail = NULL;
84 /* A boolean switch, used to ensure we only initialise once. This
85  * is needed because the engine list may genuinely become empty during
86  * use (so we can't use engine_list_head as an indicator for example. */
87 static int engine_list_flag = 0;
88
89 /* These static functions starting with a lower case "engine_" always
90  * take place when CRYPTO_LOCK_ENGINE has been locked up. */
91 static int engine_list_add(ENGINE *e)
92         {
93         int conflict = 0;
94         ENGINE *iterator = NULL;
95
96         if(e == NULL)
97                 {
98                 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
99                         ERR_R_PASSED_NULL_PARAMETER);
100                 return 0;
101                 }
102         iterator = engine_list_head;
103         while(iterator && !conflict)
104                 {
105                 conflict = (strcmp(iterator->id, e->id) == 0);
106                 iterator = iterator->next;
107                 }
108         if(conflict)
109                 {
110                 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
111                         ENGINE_R_CONFLICTING_ENGINE_ID);
112                 return 0;
113                 }
114         if(engine_list_head == NULL)
115                 {
116                 /* We are adding to an empty list. */
117                 if(engine_list_tail)
118                         {
119                         ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
120                                 ENGINE_R_INTERNAL_LIST_ERROR);
121                         return 0;
122                         }
123                 engine_list_head = e;
124                 e->prev = NULL;
125                 }
126         else
127                 {
128                 /* We are adding to the tail of an existing list. */
129                 if((engine_list_tail == NULL) ||
130                                 (engine_list_tail->next != NULL))
131                         {
132                         ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
133                                 ENGINE_R_INTERNAL_LIST_ERROR);
134                         return 0;
135                         }
136                 engine_list_tail->next = e;
137                 e->prev = engine_list_tail;
138                 }
139         /* Having the engine in the list assumes a structural
140          * reference. */
141         e->struct_ref++;
142         /* However it came to be, e is the last item in the list. */
143         engine_list_tail = e;
144         e->next = NULL;
145         return 1;
146         }
147
148 static int engine_list_remove(ENGINE *e)
149         {
150         ENGINE *iterator;
151
152         if(e == NULL)
153                 {
154                 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
155                         ERR_R_PASSED_NULL_PARAMETER);
156                 return 0;
157                 }
158         /* We need to check that e is in our linked list! */
159         iterator = engine_list_head;
160         while(iterator && (iterator != e))
161                 iterator = iterator->next;
162         if(iterator == NULL)
163                 {
164                 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
165                         ENGINE_R_ENGINE_IS_NOT_IN_LIST);
166                 return 0;
167                 }
168         /* un-link e from the chain. */
169         if(e->next)
170                 e->next->prev = e->prev;
171         if(e->prev)
172                 e->prev->next = e->next;
173         /* Correct our head/tail if necessary. */
174         if(engine_list_head == e)
175                 engine_list_head = e->next;
176         if(engine_list_tail == e)
177                 engine_list_tail = e->prev;
178         /* remove our structural reference. */
179         e->struct_ref--;
180         return 1;
181         }
182
183 /* This check always takes place with CRYPTO_LOCK_ENGINE locked up
184  * so we're synchronised, but we can't call anything that tries to
185  * lock it again! :-) NB: For convenience (and code-clarity) we
186  * don't output errors for failures of the engine_list_add function
187  * as it will generate errors itself. */
188 static int engine_internal_check(void)
189         {
190         if(engine_list_flag)
191                 return 1;
192         /* This is our first time up, we need to populate the list
193          * with our statically compiled-in engines. */
194         if(!engine_list_add(ENGINE_openssl()))
195                 return 0;
196         engine_list_flag = 1;
197         return 1;
198         }
199
200 /* Get the first/last "ENGINE" type available. */
201 ENGINE *ENGINE_get_first(void)
202         {
203         ENGINE *ret = NULL;
204
205         CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
206         if(engine_internal_check())
207                 {
208                 ret = engine_list_head;
209                 if(ret)
210                         ret->struct_ref++;
211                 }
212         CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
213         return ret;
214         }
215 ENGINE *ENGINE_get_last(void)
216         {
217         ENGINE *ret = NULL;
218
219         CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
220         if(engine_internal_check())
221                 {
222                 ret = engine_list_tail;
223                 if(ret)
224                         ret->struct_ref++;
225                 }
226         CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
227         return ret;
228         }
229
230 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
231 ENGINE *ENGINE_get_next(ENGINE *e)
232         {
233         ENGINE *ret = NULL;
234         if(e == NULL)
235                 {
236                 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
237                         ERR_R_PASSED_NULL_PARAMETER);
238                 return 0;
239                 }
240         CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
241         ret = e->next;
242         if(ret)
243                 /* Return a valid structural refernce to the next ENGINE */
244                 ret->struct_ref++;
245         CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
246         /* Release the structural reference to the previous ENGINE */
247         ENGINE_free(e);
248         return ret;
249         }
250 ENGINE *ENGINE_get_prev(ENGINE *e)
251         {
252         ENGINE *ret = NULL;
253         if(e == NULL)
254                 {
255                 ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
256                         ERR_R_PASSED_NULL_PARAMETER);
257                 return 0;
258                 }
259         CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
260         ret = e->prev;
261         if(ret)
262                 /* Return a valid structural reference to the next ENGINE */
263                 ret->struct_ref++;
264         CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
265         /* Release the structural reference to the previous ENGINE */
266         ENGINE_free(e);
267         return ret;
268         }
269
270 /* Add another "ENGINE" type into the list. */
271 int ENGINE_add(ENGINE *e)
272         {
273         int to_return = 1;
274         if(e == NULL)
275                 {
276                 ENGINEerr(ENGINE_F_ENGINE_ADD,
277                         ERR_R_PASSED_NULL_PARAMETER);
278                 return 0;
279                 }
280         if((e->id == NULL) || (e->name == NULL))
281                 {
282                 ENGINEerr(ENGINE_F_ENGINE_ADD,
283                         ENGINE_R_ID_OR_NAME_MISSING);
284                 }
285         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
286         if(!engine_internal_check() || !engine_list_add(e))
287                 {
288                 ENGINEerr(ENGINE_F_ENGINE_ADD,
289                         ENGINE_R_INTERNAL_LIST_ERROR);
290                 to_return = 0;
291                 }
292         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
293         return to_return;
294         }
295
296 /* Remove an existing "ENGINE" type from the array. */
297 int ENGINE_remove(ENGINE *e)
298         {
299         int to_return = 1;
300         if(e == NULL)
301                 {
302                 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
303                         ERR_R_PASSED_NULL_PARAMETER);
304                 return 0;
305                 }
306         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
307         if(!engine_internal_check() || !engine_list_remove(e))
308                 {
309                 ENGINEerr(ENGINE_F_ENGINE_REMOVE,
310                         ENGINE_R_INTERNAL_LIST_ERROR);
311                 to_return = 0;
312                 }
313         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
314         return to_return;
315         }
316
317 ENGINE *ENGINE_by_id(const char *id)
318         {
319         ENGINE *iterator = NULL, *cp = NULL;
320         if(id == NULL)
321                 {
322                 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
323                         ERR_R_PASSED_NULL_PARAMETER);
324                 return NULL;
325                 }
326         CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
327         if(!engine_internal_check())
328                 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
329                         ENGINE_R_INTERNAL_LIST_ERROR);
330         else
331                 {
332                 iterator = engine_list_head;
333                 while(iterator && (strcmp(id, iterator->id) != 0))
334                         iterator = iterator->next;
335                 if(iterator)
336                         {
337                         /* We need to return a structural reference. If this is
338                          * a "dynamic" ENGINE type, make a duplicate - otherwise
339                          * increment the existing ENGINE's reference count. */
340                         if(iterator->flags & ENGINE_FLAGS_BY_ID_COPY)
341                                 {
342                                 cp = ENGINE_new();
343                                 if(!cp)
344                                         iterator = NULL;
345                                 else
346                                         {
347                                         ENGINE_cpy(cp, iterator);
348                                         iterator = cp;
349                                         }
350                                 }
351                         else
352                                 iterator->struct_ref++;
353                         }
354                 }
355         CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
356         if(iterator == NULL)
357                 ENGINEerr(ENGINE_F_ENGINE_BY_ID,
358                         ENGINE_R_NO_SUCH_ENGINE);
359         return iterator;
360         }
361
362 ENGINE *ENGINE_new(void)
363         {
364         ENGINE *ret;
365
366         ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
367         if(ret == NULL)
368                 {
369                 ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
370                 return NULL;
371                 }
372         memset(ret, 0, sizeof(ENGINE));
373         ret->struct_ref = 1;
374         CRYPTO_new_ex_data(engine_ex_data_stack, ret, &ret->ex_data);
375         return ret;
376         }
377
378 int ENGINE_free(ENGINE *e)
379         {
380         int i;
381
382         if(e == NULL)
383                 {
384                 ENGINEerr(ENGINE_F_ENGINE_FREE,
385                         ERR_R_PASSED_NULL_PARAMETER);
386                 return 0;
387                 }
388         i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
389 #ifdef REF_PRINT
390         REF_PRINT("ENGINE",e);
391 #endif
392         if (i > 0) return 1;
393 #ifdef REF_CHECK
394         if (i < 0)
395                 {
396                 fprintf(stderr,"ENGINE_free, bad reference count\n");
397                 abort();
398                 }
399 #endif
400         CRYPTO_free_ex_data(engine_ex_data_stack, e, &e->ex_data);
401         OPENSSL_free(e);
402         return 1;
403         }
404
405 int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
406                 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
407         {
408         engine_ex_data_num++;
409         return(CRYPTO_get_ex_new_index(engine_ex_data_num - 1,
410                         &engine_ex_data_stack, argl, argp,
411                         new_func, dup_func, free_func));
412         }
413
414 int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
415         {
416         return(CRYPTO_set_ex_data(&e->ex_data, idx, arg));
417         }
418
419 void *ENGINE_get_ex_data(const ENGINE *e, int idx)
420         {
421         return(CRYPTO_get_ex_data(&e->ex_data, idx));
422         }
423
424 void ENGINE_cleanup(void)
425         {
426         ENGINE *iterator = engine_list_head;
427
428         while(iterator != NULL)
429                 {
430                 ENGINE_remove(iterator);
431                 ENGINE_free(iterator);
432                 iterator = engine_list_head;
433                 }
434         engine_list_flag = 0;
435         return;
436         }
437
438 int ENGINE_set_id(ENGINE *e, const char *id)
439         {
440         if(id == NULL)
441                 {
442                 ENGINEerr(ENGINE_F_ENGINE_SET_ID,
443                         ERR_R_PASSED_NULL_PARAMETER);
444                 return 0;
445                 }
446         e->id = id;
447         return 1;
448         }
449
450 int ENGINE_set_name(ENGINE *e, const char *name)
451         {
452         if(name == NULL)
453                 {
454                 ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
455                         ERR_R_PASSED_NULL_PARAMETER);
456                 return 0;
457                 }
458         e->name = name;
459         return 1;
460         }
461
462 int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth)
463         {
464 #ifndef OPENSSL_NO_RSA
465         e->rsa_meth = rsa_meth;
466         return 1;
467 #else
468         return 0;
469 #endif
470         }
471
472 int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth)
473         {
474 #ifndef OPENSSL_NO_DSA
475         e->dsa_meth = dsa_meth;
476         return 1;
477 #else
478         return 0;
479 #endif
480         }
481
482 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth)
483         {
484 #ifndef OPENSSL_NO_DH
485         e->dh_meth = dh_meth;
486         return 1;
487 #else
488         return 0;
489 #endif
490         }
491
492 int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth)
493         {
494         e->rand_meth = rand_meth;
495         return 1;
496         }
497
498 int ENGINE_set_BN_mod_exp(ENGINE *e, BN_MOD_EXP bn_mod_exp)
499         {
500         e->bn_mod_exp = bn_mod_exp;
501         return 1;
502         }
503
504 int ENGINE_set_BN_mod_exp_crt(ENGINE *e, BN_MOD_EXP_CRT bn_mod_exp_crt)
505         {
506         e->bn_mod_exp_crt = bn_mod_exp_crt;
507         return 1;
508         }
509
510 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
511         {
512         e->init = init_f;
513         return 1;
514         }
515
516 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
517         {
518         e->finish = finish_f;
519         return 1;
520         }
521
522 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
523         {
524         e->ctrl = ctrl_f;
525         return 1;
526         }
527
528 int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f)
529         {
530         e->load_privkey = loadpriv_f;
531         return 1;
532         }
533
534 int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
535         {
536         e->load_pubkey = loadpub_f;
537         return 1;
538         }
539
540 int ENGINE_set_flags(ENGINE *e, int flags)
541         {
542         e->flags = flags;
543         return 1;
544         }
545
546 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
547         {
548         e->cmd_defns = defns;
549         return 1;
550         }
551
552 int ENGINE_cpy(ENGINE *dest, const ENGINE *src)
553         {
554         if(ENGINE_set_id(dest, ENGINE_get_id(src)) &&
555                         ENGINE_set_name(dest, ENGINE_get_name(src)) &&
556 #ifndef OPENSSL_NO_RSA
557                         ENGINE_set_RSA(dest, ENGINE_get_RSA(src)) &&
558 #endif
559 #ifndef OPENSSL_NO_RSA
560                         ENGINE_set_DSA(dest, ENGINE_get_DSA(src)) &&
561 #endif
562 #ifndef OPENSSL_NO_RSA
563                         ENGINE_set_DH(dest, ENGINE_get_DH(src)) &&
564 #endif
565                         ENGINE_set_RAND(dest, ENGINE_get_RAND(src)) &&
566                         ENGINE_set_BN_mod_exp(dest,
567                                         ENGINE_get_BN_mod_exp(src)) &&
568                         ENGINE_set_BN_mod_exp_crt(dest,
569                                         ENGINE_get_BN_mod_exp_crt(src)) &&
570                         ENGINE_set_init_function(dest,
571                                         ENGINE_get_init_function(src)) &&
572                         ENGINE_set_finish_function(dest,
573                                         ENGINE_get_finish_function(src)) &&
574                         ENGINE_set_ctrl_function(dest,
575                                         ENGINE_get_ctrl_function(src)) &&
576                         ENGINE_set_load_privkey_function(dest,
577                                         ENGINE_get_load_privkey_function(src)) &&
578                         ENGINE_set_load_pubkey_function(dest,
579                                         ENGINE_get_load_pubkey_function(src)) &&
580                         ENGINE_set_flags(dest, ENGINE_get_flags(src)) &&
581                         ENGINE_set_cmd_defns(dest, ENGINE_get_cmd_defns(src)))
582                 return 1;
583         return 0;
584         }
585
586 const char *ENGINE_get_id(const ENGINE *e)
587         {
588         return e->id;
589         }
590
591 const char *ENGINE_get_name(const ENGINE *e)
592         {
593         return e->name;
594         }
595
596 const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e)
597         {
598         return e->rsa_meth;
599         }
600
601 const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e)
602         {
603         return e->dsa_meth;
604         }
605
606 const DH_METHOD *ENGINE_get_DH(const ENGINE *e)
607         {
608         return e->dh_meth;
609         }
610
611 const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e)
612         {
613         return e->rand_meth;
614         }
615
616 BN_MOD_EXP ENGINE_get_BN_mod_exp(const ENGINE *e)
617         {
618         return e->bn_mod_exp;
619         }
620
621 BN_MOD_EXP_CRT ENGINE_get_BN_mod_exp_crt(const ENGINE *e)
622         {
623         return e->bn_mod_exp_crt;
624         }
625
626 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
627         {
628         return e->init;
629         }
630
631 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
632         {
633         return e->finish;
634         }
635
636 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
637         {
638         return e->ctrl;
639         }
640
641 ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
642         {
643         return e->load_privkey;
644         }
645
646 ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
647         {
648         return e->load_pubkey;
649         }
650
651 int ENGINE_get_flags(const ENGINE *e)
652         {
653         return e->flags;
654         }
655
656 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
657         {
658         return e->cmd_defns;
659         }