2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
13 #include <openssl/trace.h>
14 #include "eng_local.h"
16 /* The type of the items in the table */
17 struct st_engine_pile {
18 /* The 'nid' of this algorithm/mode */
20 /* ENGINEs that implement this algorithm/mode. */
22 /* The default ENGINE to perform this algorithm/mode. */
25 * Zero if 'sk' is newer than the cached 'funct', non-zero otherwise
30 /* The type exposed in eng_local.h */
31 struct st_engine_table {
32 LHASH_OF(ENGINE_PILE) piles;
35 typedef struct st_engine_pile_doall {
36 engine_table_doall_cb *cb;
40 /* Global flags (ENGINE_TABLE_FLAG_***). */
41 static unsigned int table_flags = 0;
43 /* API function manipulating 'table_flags' */
44 unsigned int ENGINE_get_table_flags(void)
49 void ENGINE_set_table_flags(unsigned int flags)
54 /* Internal functions for the "piles" hash table */
55 static unsigned long engine_pile_hash(const ENGINE_PILE *c)
60 static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
62 return a->nid - b->nid;
65 static int int_table_check(ENGINE_TABLE **t, int create)
67 LHASH_OF(ENGINE_PILE) *lh;
73 if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL)
75 *t = (ENGINE_TABLE *)lh;
80 * Privately exposed (via eng_local.h) functions for adding and/or removing
81 * ENGINEs from the implementation table
83 int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
84 ENGINE *e, const int *nids, int num_nids,
87 int ret = 0, added = 0;
88 ENGINE_PILE tmplate, *fnd;
90 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
94 if (!int_table_check(table, 1))
97 /* The cleanup callback needs to be added */
98 engine_cleanup_add_first(cleanup);
101 fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
103 fnd = OPENSSL_malloc(sizeof(*fnd));
108 fnd->sk = sk_ENGINE_new_null();
114 (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
115 if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) {
116 sk_ENGINE_free(fnd->sk);
121 /* A registration shouldn't add duplicate entries */
122 (void)sk_ENGINE_delete_ptr(fnd->sk, e);
124 * if 'setdefault', this ENGINE goes to the head of the list
126 if (!sk_ENGINE_push(fnd->sk, e))
128 /* "touch" this ENGINE_PILE */
131 if (!engine_unlocked_init(e)) {
132 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INIT_FAILED);
136 engine_unlocked_finish(fnd->funct, 0);
144 CRYPTO_THREAD_unlock(global_engine_lock);
148 static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
151 /* Iterate the 'c->sk' stack removing any occurrence of 'e' */
152 while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) {
153 (void)sk_ENGINE_delete(pile->sk, n);
156 if (pile->funct == e) {
157 engine_unlocked_finish(e, 0);
162 IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE);
164 void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
166 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
167 /* Can't return a value. :( */
169 if (int_table_check(table, 0))
170 lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e);
171 CRYPTO_THREAD_unlock(global_engine_lock);
174 static void int_cleanup_cb_doall(ENGINE_PILE *p)
178 sk_ENGINE_free(p->sk);
180 engine_unlocked_finish(p->funct, 0);
184 void engine_table_cleanup(ENGINE_TABLE **table)
186 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
189 lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall);
190 lh_ENGINE_PILE_free(&(*table)->piles);
193 CRYPTO_THREAD_unlock(global_engine_lock);
196 /* return a functional reference for a given 'nid' */
197 ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
198 const char *f, int l)
201 ENGINE_PILE tmplate, *fnd = NULL;
202 int initres, loop = 0;
204 /* Load the config before trying to check if engines are available */
205 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
208 OSSL_TRACE3(ENGINE_TABLE,
209 "%s:%d, nid=%d, nothing registered!\n",
214 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
217 * Check again inside the lock otherwise we could race against cleanup
218 * operations. But don't worry about a debug printout
220 if (!int_table_check(table, 0))
223 fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
226 if (fnd->funct && engine_unlocked_init(fnd->funct)) {
227 OSSL_TRACE4(ENGINE_TABLE,
228 "%s:%d, nid=%d, using ENGINE '%s' cached\n",
229 f, l, nid, fnd->funct->id);
238 ret = sk_ENGINE_value(fnd->sk, loop++);
240 OSSL_TRACE3(ENGINE_TABLE,
242 "no registered implementations would initialise\n",
246 /* Try to initialise the ENGINE? */
247 if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
248 initres = engine_unlocked_init(ret);
253 if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
254 /* If there was a previous default we release it. */
256 engine_unlocked_finish(fnd->funct, 0);
258 OSSL_TRACE4(ENGINE_TABLE,
259 "%s:%d, nid=%d, setting default to '%s'\n",
262 OSSL_TRACE4(ENGINE_TABLE,
263 "%s:%d, nid=%d, using newly initialised '%s'\n",
270 * If it failed, it is unlikely to succeed again until some future
271 * registrations have taken place. In all cases, we cache.
276 OSSL_TRACE4(ENGINE_TABLE,
277 "%s:%d, nid=%d, caching ENGINE '%s'\n",
280 OSSL_TRACE3(ENGINE_TABLE,
281 "%s:%d, nid=%d, caching 'no matching ENGINE'\n",
283 CRYPTO_THREAD_unlock(global_engine_lock);
285 * Whatever happened, any failed init()s are not failures in this
286 * context, so clear our error state.
292 /* Table enumeration */
294 static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
296 dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
299 IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL);
301 void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
304 ENGINE_PILE_DOALL dall;
308 lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);