2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include <openssl/evp.h>
12 #include <openssl/lhash.h>
15 /* The type of the items in the table */
16 struct st_engine_pile {
17 /* The 'nid' of this algorithm/mode */
19 /* ENGINEs that implement this algorithm/mode. */
21 /* The default ENGINE to perform this algorithm/mode. */
24 * Zero if 'sk' is newer than the cached 'funct', non-zero otherwise
29 /* The type exposed in eng_int.h */
30 struct st_engine_table {
31 LHASH_OF(ENGINE_PILE) piles;
34 typedef struct st_engine_pile_doall {
35 engine_table_doall_cb *cb;
39 /* Global flags (ENGINE_TABLE_FLAG_***). */
40 static unsigned int table_flags = 0;
42 /* API function manipulating 'table_flags' */
43 unsigned int ENGINE_get_table_flags(void)
48 void ENGINE_set_table_flags(unsigned int flags)
53 /* Internal functions for the "piles" hash table */
54 static unsigned long engine_pile_hash(const ENGINE_PILE *c)
59 static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
61 return a->nid - b->nid;
64 static int int_table_check(ENGINE_TABLE **t, int create)
66 LHASH_OF(ENGINE_PILE) *lh;
72 if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL)
74 *t = (ENGINE_TABLE *)lh;
79 * Privately exposed (via eng_int.h) functions for adding and/or removing
80 * ENGINEs from the implementation table
82 int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
83 ENGINE *e, const int *nids, int num_nids,
86 int ret = 0, added = 0;
87 ENGINE_PILE tmplate, *fnd;
88 CRYPTO_THREAD_write_lock(global_engine_lock);
91 if (!int_table_check(table, 1))
94 /* The cleanup callback needs to be added */
95 engine_cleanup_add_first(cleanup);
98 fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
100 fnd = OPENSSL_malloc(sizeof(*fnd));
105 fnd->sk = sk_ENGINE_new_null();
111 (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
112 if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) {
113 sk_ENGINE_free(fnd->sk);
118 /* A registration shouldn't add duplicate entries */
119 (void)sk_ENGINE_delete_ptr(fnd->sk, e);
121 * if 'setdefault', this ENGINE goes to the head of the list
123 if (!sk_ENGINE_push(fnd->sk, e))
125 /* "touch" this ENGINE_PILE */
128 if (!engine_unlocked_init(e)) {
129 ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER,
130 ENGINE_R_INIT_FAILED);
134 engine_unlocked_finish(fnd->funct, 0);
142 CRYPTO_THREAD_unlock(global_engine_lock);
146 static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
149 /* Iterate the 'c->sk' stack removing any occurrence of 'e' */
150 while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) {
151 (void)sk_ENGINE_delete(pile->sk, n);
154 if (pile->funct == e) {
155 engine_unlocked_finish(e, 0);
160 IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE);
162 void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
164 CRYPTO_THREAD_write_lock(global_engine_lock);
165 if (int_table_check(table, 0))
166 lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e);
167 CRYPTO_THREAD_unlock(global_engine_lock);
170 static void int_cleanup_cb_doall(ENGINE_PILE *p)
174 sk_ENGINE_free(p->sk);
176 engine_unlocked_finish(p->funct, 0);
180 void engine_table_cleanup(ENGINE_TABLE **table)
182 CRYPTO_THREAD_write_lock(global_engine_lock);
184 lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall);
185 lh_ENGINE_PILE_free(&(*table)->piles);
188 CRYPTO_THREAD_unlock(global_engine_lock);
191 /* return a functional reference for a given 'nid' */
192 #ifndef ENGINE_TABLE_DEBUG
193 ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)
195 ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
200 ENGINE_PILE tmplate, *fnd = NULL;
201 int initres, loop = 0;
204 #ifdef ENGINE_TABLE_DEBUG
205 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "
206 "registered!\n", f, l, nid);
211 CRYPTO_THREAD_write_lock(global_engine_lock);
213 * Check again inside the lock otherwise we could race against cleanup
214 * operations. But don't worry about a fprintf(stderr).
216 if (!int_table_check(table, 0))
219 fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
222 if (fnd->funct && engine_unlocked_init(fnd->funct)) {
223 #ifdef ENGINE_TABLE_DEBUG
224 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
225 "ENGINE '%s' cached\n", f, l, nid, fnd->funct->id);
235 ret = sk_ENGINE_value(fnd->sk, loop++);
237 #ifdef ENGINE_TABLE_DEBUG
238 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "
239 "registered implementations would initialise\n", f, l, nid);
243 /* Try to initialise the ENGINE? */
244 if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
245 initres = engine_unlocked_init(ret);
250 if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
251 /* If there was a previous default we release it. */
253 engine_unlocked_finish(fnd->funct, 0);
255 #ifdef ENGINE_TABLE_DEBUG
256 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "
257 "setting default to '%s'\n", f, l, nid, ret->id);
260 #ifdef ENGINE_TABLE_DEBUG
261 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
262 "newly initialised '%s'\n", f, l, nid, ret->id);
269 * If it failed, it is unlikely to succeed again until some future
270 * registrations have taken place. In all cases, we cache.
274 #ifdef ENGINE_TABLE_DEBUG
276 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
277 "ENGINE '%s'\n", f, l, nid, ret->id);
279 fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
280 "'no matching ENGINE'\n", f, l, nid);
282 CRYPTO_THREAD_unlock(global_engine_lock);
284 * Whatever happened, any failed init()s are not failures in this
285 * context, so clear our error state.
291 /* Table enumeration */
293 static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
295 dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
298 IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL);
300 void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
303 ENGINE_PILE_DOALL dall;
307 lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);