eng_table.c revision 160814
1220080Snwhitehorn/* ====================================================================
2220080Snwhitehorn * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
3220080Snwhitehorn *
4220080Snwhitehorn * Redistribution and use in source and binary forms, with or without
5220080Snwhitehorn * modification, are permitted provided that the following conditions
6220080Snwhitehorn * are met:
7220080Snwhitehorn *
8220080Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9220080Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10220080Snwhitehorn *
11220080Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
12220080Snwhitehorn *    notice, this list of conditions and the following disclaimer in
13220080Snwhitehorn *    the documentation and/or other materials provided with the
14220080Snwhitehorn *    distribution.
15220080Snwhitehorn *
16220080Snwhitehorn * 3. All advertising materials mentioning features or use of this
17220080Snwhitehorn *    software must display the following acknowledgment:
18220080Snwhitehorn *    "This product includes software developed by the OpenSSL Project
19220080Snwhitehorn *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20220080Snwhitehorn *
21220080Snwhitehorn * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22220080Snwhitehorn *    endorse or promote products derived from this software without
23220080Snwhitehorn *    prior written permission. For written permission, please contact
24220080Snwhitehorn *    licensing@OpenSSL.org.
25220080Snwhitehorn *
26220080Snwhitehorn * 5. Products derived from this software may not be called "OpenSSL"
27220080Snwhitehorn *    nor may "OpenSSL" appear in their names without prior written
28220080Snwhitehorn *    permission of the OpenSSL Project.
29220080Snwhitehorn *
30220080Snwhitehorn * 6. Redistributions of any form whatsoever must retain the following
31220080Snwhitehorn *    acknowledgment:
32220080Snwhitehorn *    "This product includes software developed by the OpenSSL Project
33220080Snwhitehorn *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34220080Snwhitehorn *
35220080Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36220080Snwhitehorn * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37220080Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38220080Snwhitehorn * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39220080Snwhitehorn * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40220080Snwhitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41220080Snwhitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42220080Snwhitehorn * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43220080Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44220080Snwhitehorn * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45220080Snwhitehorn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46220080Snwhitehorn * OF THE POSSIBILITY OF SUCH DAMAGE.
47225467Sbrueffer * ====================================================================
48220080Snwhitehorn *
49220080Snwhitehorn * This product includes cryptographic software written by Eric Young
50220080Snwhitehorn * (eay@cryptsoft.com).  This product includes software written by Tim
51220080Snwhitehorn * Hudson (tjh@cryptsoft.com).
52220080Snwhitehorn *
53220080Snwhitehorn */
54220080Snwhitehorn
55220080Snwhitehorn#include "cryptlib.h"
56220080Snwhitehorn#include <openssl/evp.h>
57220080Snwhitehorn#include <openssl/lhash.h>
58220080Snwhitehorn#include "eng_int.h"
59220080Snwhitehorn
60220080Snwhitehorn/* The type of the items in the table */
61220080Snwhitehorntypedef struct st_engine_pile
62220080Snwhitehorn	{
63220080Snwhitehorn	/* The 'nid' of this algorithm/mode */
64220080Snwhitehorn	int nid;
65220080Snwhitehorn	/* ENGINEs that implement this algorithm/mode. */
66220080Snwhitehorn	STACK_OF(ENGINE) *sk;
67220080Snwhitehorn	/* The default ENGINE to perform this algorithm/mode. */
68220080Snwhitehorn	ENGINE *funct;
69220080Snwhitehorn	/* Zero if 'sk' is newer than the cached 'funct', non-zero otherwise */
70220080Snwhitehorn	int uptodate;
71220080Snwhitehorn	} ENGINE_PILE;
72220080Snwhitehorn
73220080Snwhitehorn/* The type exposed in eng_int.h */
74220080Snwhitehornstruct st_engine_table
75220080Snwhitehorn	{
76220080Snwhitehorn	LHASH piles;
77220080Snwhitehorn	}; /* ENGINE_TABLE */
78220080Snwhitehorn
79220080Snwhitehorn/* Global flags (ENGINE_TABLE_FLAG_***). */
80220080Snwhitehornstatic unsigned int table_flags = 0;
81220080Snwhitehorn
82220080Snwhitehorn/* API function manipulating 'table_flags' */
83220080Snwhitehornunsigned int ENGINE_get_table_flags(void)
84220080Snwhitehorn	{
85220080Snwhitehorn	return table_flags;
86220080Snwhitehorn	}
87224656Srobertovoid ENGINE_set_table_flags(unsigned int flags)
88220080Snwhitehorn	{
89235228Sroberto	table_flags = flags;
90220080Snwhitehorn	}
91220080Snwhitehorn
92224656Sroberto/* Internal functions for the "piles" hash table */
93220080Snwhitehornstatic unsigned long engine_pile_hash(const ENGINE_PILE *c)
94220080Snwhitehorn	{
95220080Snwhitehorn	return c->nid;
96220080Snwhitehorn	}
97220080Snwhitehornstatic int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
98220080Snwhitehorn	{
99220080Snwhitehorn	return a->nid - b->nid;
100220080Snwhitehorn	}
101220080Snwhitehornstatic IMPLEMENT_LHASH_HASH_FN(engine_pile_hash, const ENGINE_PILE *)
102220080Snwhitehornstatic IMPLEMENT_LHASH_COMP_FN(engine_pile_cmp, const ENGINE_PILE *)
103220080Snwhitehornstatic int int_table_check(ENGINE_TABLE **t, int create)
104220080Snwhitehorn	{
105220080Snwhitehorn	LHASH *lh;
106220080Snwhitehorn	if(*t) return 1;
107220080Snwhitehorn	if(!create) return 0;
108220080Snwhitehorn	if((lh = lh_new(LHASH_HASH_FN(engine_pile_hash),
109220080Snwhitehorn			LHASH_COMP_FN(engine_pile_cmp))) == NULL)
110220080Snwhitehorn		return 0;
111220080Snwhitehorn	*t = (ENGINE_TABLE *)lh;
112220080Snwhitehorn	return 1;
113220080Snwhitehorn	}
114220080Snwhitehorn
115220080Snwhitehorn/* Privately exposed (via eng_int.h) functions for adding and/or removing
116220080Snwhitehorn * ENGINEs from the implementation table */
117220080Snwhitehornint engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
118220080Snwhitehorn		ENGINE *e, const int *nids, int num_nids, int setdefault)
119220080Snwhitehorn	{
120220080Snwhitehorn	int ret = 0, added = 0;
121220080Snwhitehorn	ENGINE_PILE tmplate, *fnd;
122220080Snwhitehorn	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
123220080Snwhitehorn	if(!(*table))
124220080Snwhitehorn		added = 1;
125220080Snwhitehorn	if(!int_table_check(table, 1))
126220080Snwhitehorn		goto end;
127220080Snwhitehorn	if(added)
128220080Snwhitehorn		/* The cleanup callback needs to be added */
129220080Snwhitehorn		engine_cleanup_add_first(cleanup);
130220080Snwhitehorn	while(num_nids--)
131220080Snwhitehorn		{
132220080Snwhitehorn		tmplate.nid = *nids;
133220080Snwhitehorn		fnd = lh_retrieve(&(*table)->piles, &tmplate);
134220080Snwhitehorn		if(!fnd)
135220080Snwhitehorn			{
136220080Snwhitehorn			fnd = OPENSSL_malloc(sizeof(ENGINE_PILE));
137220080Snwhitehorn			if(!fnd) goto end;
138220080Snwhitehorn			fnd->uptodate = 0;
139220080Snwhitehorn			fnd->nid = *nids;
140220080Snwhitehorn			fnd->sk = sk_ENGINE_new_null();
141220080Snwhitehorn			if(!fnd->sk)
142220080Snwhitehorn				{
143220080Snwhitehorn				OPENSSL_free(fnd);
144220080Snwhitehorn				goto end;
145220080Snwhitehorn				}
146220080Snwhitehorn			fnd->funct = NULL;
147220080Snwhitehorn			lh_insert(&(*table)->piles, fnd);
148220080Snwhitehorn			}
149220080Snwhitehorn		/* A registration shouldn't add duplciate entries */
150220080Snwhitehorn		sk_ENGINE_delete_ptr(fnd->sk, e);
151220080Snwhitehorn		/* if 'setdefault', this ENGINE goes to the head of the list */
152220080Snwhitehorn		if(!sk_ENGINE_push(fnd->sk, e))
153243832Sjoel			goto end;
154220080Snwhitehorn		/* "touch" this ENGINE_PILE */
155220080Snwhitehorn		fnd->uptodate = 1;
156220080Snwhitehorn		if(setdefault)
157220080Snwhitehorn			{
158220080Snwhitehorn			if(!engine_unlocked_init(e))
159220080Snwhitehorn				{
160220080Snwhitehorn				ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER,
161220080Snwhitehorn						ENGINE_R_INIT_FAILED);
162220080Snwhitehorn				goto end;
163220080Snwhitehorn				}
164220080Snwhitehorn			if(fnd->funct)
165220080Snwhitehorn				engine_unlocked_finish(fnd->funct, 0);
166220080Snwhitehorn			fnd->funct = e;
167220080Snwhitehorn			}
168220080Snwhitehorn		nids++;
169220080Snwhitehorn		}
170220080Snwhitehorn	ret = 1;
171220080Snwhitehornend:
172220080Snwhitehorn	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
173220080Snwhitehorn	return ret;
174220080Snwhitehorn	}
175220080Snwhitehornstatic void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
176220080Snwhitehorn	{
177220080Snwhitehorn	int n;
178220080Snwhitehorn	/* Iterate the 'c->sk' stack removing any occurance of 'e' */
179220080Snwhitehorn	while((n = sk_ENGINE_find(pile->sk, e)) >= 0)
180220080Snwhitehorn		{
181220080Snwhitehorn		sk_ENGINE_delete(pile->sk, n);
182220080Snwhitehorn		/* "touch" this ENGINE_CIPHER */
183220080Snwhitehorn		pile->uptodate = 1;
184220080Snwhitehorn		}
185220080Snwhitehorn	if(pile->funct == e)
186220080Snwhitehorn		{
187220080Snwhitehorn		engine_unlocked_finish(e, 0);
188220080Snwhitehorn		pile->funct = NULL;
189220080Snwhitehorn		}
190220080Snwhitehorn	}
191220080Snwhitehornstatic IMPLEMENT_LHASH_DOALL_ARG_FN(int_unregister_cb,ENGINE_PILE *,ENGINE *)
192220080Snwhitehornvoid engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
193220080Snwhitehorn	{
194220080Snwhitehorn	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
195225270Snwhitehorn	if(int_table_check(table, 0))
196220080Snwhitehorn		lh_doall_arg(&(*table)->piles,
197220080Snwhitehorn			LHASH_DOALL_ARG_FN(int_unregister_cb), e);
198220080Snwhitehorn	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
199220080Snwhitehorn	}
200220080Snwhitehorn
201220080Snwhitehornstatic void int_cleanup_cb(ENGINE_PILE *p)
202220080Snwhitehorn	{
203220080Snwhitehorn	sk_ENGINE_free(p->sk);
204220080Snwhitehorn	if(p->funct)
205220080Snwhitehorn		engine_unlocked_finish(p->funct, 0);
206220080Snwhitehorn	OPENSSL_free(p);
207220080Snwhitehorn	}
208220080Snwhitehornstatic IMPLEMENT_LHASH_DOALL_FN(int_cleanup_cb,ENGINE_PILE *)
209220080Snwhitehornvoid engine_table_cleanup(ENGINE_TABLE **table)
210220080Snwhitehorn	{
211220080Snwhitehorn	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
212220080Snwhitehorn	if(*table)
213220080Snwhitehorn		{
214220080Snwhitehorn		lh_doall(&(*table)->piles, LHASH_DOALL_FN(int_cleanup_cb));
215220080Snwhitehorn		lh_free(&(*table)->piles);
216220080Snwhitehorn		*table = NULL;
217		}
218	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
219	}
220
221/* return a functional reference for a given 'nid' */
222#ifndef ENGINE_TABLE_DEBUG
223ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)
224#else
225ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, int l)
226#endif
227	{
228	ENGINE *ret = NULL;
229	ENGINE_PILE tmplate, *fnd=NULL;
230	int initres, loop = 0;
231
232	if(!(*table))
233		{
234#ifdef ENGINE_TABLE_DEBUG
235		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "
236			"registered!\n", f, l, nid);
237#endif
238		return NULL;
239		}
240	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
241	/* Check again inside the lock otherwise we could race against cleanup
242	 * operations. But don't worry about a fprintf(stderr). */
243	if(!int_table_check(table, 0)) goto end;
244	tmplate.nid = nid;
245	fnd = lh_retrieve(&(*table)->piles, &tmplate);
246	if(!fnd) goto end;
247	if(fnd->funct && engine_unlocked_init(fnd->funct))
248		{
249#ifdef ENGINE_TABLE_DEBUG
250		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
251			"ENGINE '%s' cached\n", f, l, nid, fnd->funct->id);
252#endif
253		ret = fnd->funct;
254		goto end;
255		}
256	if(fnd->uptodate)
257		{
258		ret = fnd->funct;
259		goto end;
260		}
261trynext:
262	ret = sk_ENGINE_value(fnd->sk, loop++);
263	if(!ret)
264		{
265#ifdef ENGINE_TABLE_DEBUG
266		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "
267				"registered implementations would initialise\n",
268				f, l, nid);
269#endif
270		goto end;
271		}
272	/* Try to initialise the ENGINE? */
273	if((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
274		initres = engine_unlocked_init(ret);
275	else
276		initres = 0;
277	if(initres)
278		{
279		/* Update 'funct' */
280		if((fnd->funct != ret) && engine_unlocked_init(ret))
281			{
282			/* If there was a previous default we release it. */
283			if(fnd->funct)
284				engine_unlocked_finish(fnd->funct, 0);
285			fnd->funct = ret;
286#ifdef ENGINE_TABLE_DEBUG
287			fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "
288				"setting default to '%s'\n", f, l, nid, ret->id);
289#endif
290			}
291#ifdef ENGINE_TABLE_DEBUG
292		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
293				"newly initialised '%s'\n", f, l, nid, ret->id);
294#endif
295		goto end;
296		}
297	goto trynext;
298end:
299	/* If it failed, it is unlikely to succeed again until some future
300	 * registrations have taken place. In all cases, we cache. */
301	if(fnd) fnd->uptodate = 1;
302#ifdef ENGINE_TABLE_DEBUG
303	if(ret)
304		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
305				"ENGINE '%s'\n", f, l, nid, ret->id);
306	else
307		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
308				"'no matching ENGINE'\n", f, l, nid);
309#endif
310	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
311	/* Whatever happened, any failed init()s are not failures in this
312	 * context, so clear our error state. */
313	ERR_clear_error();
314	return ret;
315	}
316