Lookup public key ASN1 methods by string by iterating through all
[openssl.git] / crypto / engine / eng_table.c
1 /* ====================================================================
2  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    licensing@OpenSSL.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This product includes cryptographic software written by Eric Young
50  * (eay@cryptsoft.com).  This product includes software written by Tim
51  * Hudson (tjh@cryptsoft.com).
52  *
53  */
54
55 #include "cryptlib.h"
56 #include <openssl/evp.h>
57 #include <openssl/lhash.h>
58 #include "eng_int.h"
59
60 /* The type of the items in the table */
61 typedef struct st_engine_pile
62         {
63         /* The 'nid' of this algorithm/mode */
64         int nid;
65         /* ENGINEs that implement this algorithm/mode. */
66         STACK_OF(ENGINE) *sk;
67         /* The default ENGINE to perform this algorithm/mode. */
68         ENGINE *funct;
69         /* Zero if 'sk' is newer than the cached 'funct', non-zero otherwise */
70         int uptodate;
71         } ENGINE_PILE;
72
73 /* The type exposed in eng_int.h */
74 struct st_engine_table
75         {
76         LHASH piles;
77         }; /* ENGINE_TABLE */
78
79
80 typedef struct st_engine_pile_doall
81         {
82         engine_table_doall_cb *cb;
83         void *arg;
84         } ENGINE_PILE_DOALL;
85         
86
87 /* Global flags (ENGINE_TABLE_FLAG_***). */
88 static unsigned int table_flags = 0;
89
90 /* API function manipulating 'table_flags' */
91 unsigned int ENGINE_get_table_flags(void)
92         {
93         return table_flags;
94         }
95 void ENGINE_set_table_flags(unsigned int flags)
96         {
97         table_flags = flags;
98         }
99
100 /* Internal functions for the "piles" hash table */
101 static unsigned long engine_pile_hash(const ENGINE_PILE *c)
102         {
103         return c->nid;
104         }
105 static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
106         {
107         return a->nid - b->nid;
108         }
109 static IMPLEMENT_LHASH_HASH_FN(engine_pile_hash, const ENGINE_PILE *)
110 static IMPLEMENT_LHASH_COMP_FN(engine_pile_cmp, const ENGINE_PILE *)
111 static int int_table_check(ENGINE_TABLE **t, int create)
112         {
113         LHASH *lh;
114         if(*t) return 1;
115         if(!create) return 0;
116         if((lh = lh_new(LHASH_HASH_FN(engine_pile_hash),
117                         LHASH_COMP_FN(engine_pile_cmp))) == NULL)
118                 return 0;
119         *t = (ENGINE_TABLE *)lh;
120         return 1;
121         }
122
123 /* Privately exposed (via eng_int.h) functions for adding and/or removing
124  * ENGINEs from the implementation table */
125 int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
126                 ENGINE *e, const int *nids, int num_nids, int setdefault)
127         {
128         int ret = 0, added = 0;
129         ENGINE_PILE tmplate, *fnd;
130         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
131         if(!(*table))
132                 added = 1;
133         if(!int_table_check(table, 1))
134                 goto end;
135         if(added)
136                 /* The cleanup callback needs to be added */
137                 engine_cleanup_add_first(cleanup);
138         while(num_nids--)
139                 {
140                 tmplate.nid = *nids;
141                 fnd = lh_retrieve(&(*table)->piles, &tmplate);
142                 if(!fnd)
143                         {
144                         fnd = OPENSSL_malloc(sizeof(ENGINE_PILE));
145                         if(!fnd) goto end;
146                         fnd->uptodate = 0;
147                         fnd->nid = *nids;
148                         fnd->sk = sk_ENGINE_new_null();
149                         if(!fnd->sk)
150                                 {
151                                 OPENSSL_free(fnd);
152                                 goto end;
153                                 }
154                         fnd->funct = NULL;
155                         lh_insert(&(*table)->piles, fnd);
156                         }
157                 /* A registration shouldn't add duplciate entries */
158                 (void)sk_ENGINE_delete_ptr(fnd->sk, e);
159                 /* if 'setdefault', this ENGINE goes to the head of the list */
160                 if(!sk_ENGINE_push(fnd->sk, e))
161                         goto end;
162                 /* "touch" this ENGINE_PILE */
163                 fnd->uptodate = 1;
164                 if(setdefault)
165                         {
166                         if(!engine_unlocked_init(e))
167                                 {
168                                 ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER,
169                                                 ENGINE_R_INIT_FAILED);
170                                 goto end;
171                                 }
172                         if(fnd->funct)
173                                 engine_unlocked_finish(fnd->funct, 0);
174                         fnd->funct = e;
175                         }
176                 nids++;
177                 }
178         ret = 1;
179 end:
180         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
181         return ret;
182         }
183 static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
184         {
185         int n;
186         /* Iterate the 'c->sk' stack removing any occurance of 'e' */
187         while((n = sk_ENGINE_find(pile->sk, e)) >= 0)
188                 {
189                 (void)sk_ENGINE_delete(pile->sk, n);
190                 /* "touch" this ENGINE_CIPHER */
191                 pile->uptodate = 1;
192                 }
193         if(pile->funct == e)
194                 {
195                 engine_unlocked_finish(e, 0);
196                 pile->funct = NULL;
197                 }
198         }
199 static IMPLEMENT_LHASH_DOALL_ARG_FN(int_unregister_cb,ENGINE_PILE *,ENGINE *)
200 void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
201         {
202         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
203         if(int_table_check(table, 0))
204                 lh_doall_arg(&(*table)->piles,
205                         LHASH_DOALL_ARG_FN(int_unregister_cb), e);
206         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
207         }
208
209 static void int_cleanup_cb(ENGINE_PILE *p)
210         {
211         sk_ENGINE_free(p->sk);
212         if(p->funct)
213                 engine_unlocked_finish(p->funct, 0);
214         OPENSSL_free(p);
215         }
216 static IMPLEMENT_LHASH_DOALL_FN(int_cleanup_cb,ENGINE_PILE *)
217 void engine_table_cleanup(ENGINE_TABLE **table)
218         {
219         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
220         if(*table)
221                 {
222                 lh_doall(&(*table)->piles, LHASH_DOALL_FN(int_cleanup_cb));
223                 lh_free(&(*table)->piles);
224                 *table = NULL;
225                 }
226         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
227         }
228
229 /* return a functional reference for a given 'nid' */
230 #ifndef ENGINE_TABLE_DEBUG
231 ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)
232 #else
233 ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, int l)
234 #endif
235         {
236         ENGINE *ret = NULL;
237         ENGINE_PILE tmplate, *fnd=NULL;
238         int initres, loop = 0;
239
240         if(!(*table))
241                 {
242 #ifdef ENGINE_TABLE_DEBUG
243                 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "
244                         "registered!\n", f, l, nid);
245 #endif
246                 return NULL;
247                 }
248         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
249         /* Check again inside the lock otherwise we could race against cleanup
250          * operations. But don't worry about a fprintf(stderr). */
251         if(!int_table_check(table, 0)) goto end;
252         tmplate.nid = nid;
253         fnd = lh_retrieve(&(*table)->piles, &tmplate);
254         if(!fnd) goto end;
255         if(fnd->funct && engine_unlocked_init(fnd->funct))
256                 {
257 #ifdef ENGINE_TABLE_DEBUG
258                 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
259                         "ENGINE '%s' cached\n", f, l, nid, fnd->funct->id);
260 #endif
261                 ret = fnd->funct;
262                 goto end;
263                 }
264         if(fnd->uptodate)
265                 {
266                 ret = fnd->funct;
267                 goto end;
268                 }
269 trynext:
270         ret = sk_ENGINE_value(fnd->sk, loop++);
271         if(!ret)
272                 {
273 #ifdef ENGINE_TABLE_DEBUG
274                 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "
275                                 "registered implementations would initialise\n",
276                                 f, l, nid);
277 #endif
278                 goto end;
279                 }
280         /* Try to initialise the ENGINE? */
281         if((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
282                 initres = engine_unlocked_init(ret);
283         else
284                 initres = 0;
285         if(initres)
286                 {
287                 /* Update 'funct' */
288                 if((fnd->funct != ret) && engine_unlocked_init(ret))
289                         {
290                         /* If there was a previous default we release it. */
291                         if(fnd->funct)
292                                 engine_unlocked_finish(fnd->funct, 0);
293                         fnd->funct = ret;
294 #ifdef ENGINE_TABLE_DEBUG
295                         fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "
296                                 "setting default to '%s'\n", f, l, nid, ret->id);
297 #endif
298                         }
299 #ifdef ENGINE_TABLE_DEBUG
300                 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
301                                 "newly initialised '%s'\n", f, l, nid, ret->id);
302 #endif
303                 goto end;
304                 }
305         goto trynext;
306 end:
307         /* If it failed, it is unlikely to succeed again until some future
308          * registrations have taken place. In all cases, we cache. */
309         if(fnd) fnd->uptodate = 1;
310 #ifdef ENGINE_TABLE_DEBUG
311         if(ret)
312                 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
313                                 "ENGINE '%s'\n", f, l, nid, ret->id);
314         else
315                 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
316                                 "'no matching ENGINE'\n", f, l, nid);
317 #endif
318         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
319         /* Whatever happened, any failed init()s are not failures in this
320          * context, so clear our error state. */
321         ERR_clear_error();
322         return ret;
323         }
324
325 /* Table enumeration */
326
327 static void int_doall_cb(ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
328         {
329         dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
330         }
331
332 static IMPLEMENT_LHASH_DOALL_ARG_FN(int_doall_cb,ENGINE_PILE *,ENGINE_PILE_DOALL *)
333 void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
334                                                                 void *arg)
335         {
336         ENGINE_PILE_DOALL dall;
337         dall.cb = cb;
338         dall.arg = arg;
339         lh_doall_arg(&table->piles,
340                         LHASH_DOALL_ARG_FN(int_doall_cb), &dall);
341         }