VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/crypto/engine/eng_table.c@ 101852

Last change on this file since 101852 was 101211, checked in by vboxsync, 17 months ago

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

File size: 8.9 KB
Line 
1/*
2 * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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"
15
16/* The type of the items in the table */
17struct st_engine_pile {
18 /* The 'nid' of this algorithm/mode */
19 int nid;
20 /* ENGINEs that implement this algorithm/mode. */
21 STACK_OF(ENGINE) *sk;
22 /* The default ENGINE to perform this algorithm/mode. */
23 ENGINE *funct;
24 /*
25 * Zero if 'sk' is newer than the cached 'funct', non-zero otherwise
26 */
27 int uptodate;
28};
29
30/* The type exposed in eng_local.h */
31struct st_engine_table {
32 LHASH_OF(ENGINE_PILE) piles;
33}; /* ENGINE_TABLE */
34
35typedef struct st_engine_pile_doall {
36 engine_table_doall_cb *cb;
37 void *arg;
38} ENGINE_PILE_DOALL;
39
40/* Global flags (ENGINE_TABLE_FLAG_***). */
41static unsigned int table_flags = 0;
42
43/* API function manipulating 'table_flags' */
44unsigned int ENGINE_get_table_flags(void)
45{
46 return table_flags;
47}
48
49void ENGINE_set_table_flags(unsigned int flags)
50{
51 table_flags = flags;
52}
53
54/* Internal functions for the "piles" hash table */
55static unsigned long engine_pile_hash(const ENGINE_PILE *c)
56{
57 return c->nid;
58}
59
60static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
61{
62 return a->nid - b->nid;
63}
64
65static int int_table_check(ENGINE_TABLE **t, int create)
66{
67 LHASH_OF(ENGINE_PILE) *lh;
68
69 if (*t)
70 return 1;
71 if (!create)
72 return 0;
73 if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL)
74 return 0;
75 *t = (ENGINE_TABLE *)lh;
76 return 1;
77}
78
79/*
80 * Privately exposed (via eng_local.h) functions for adding and/or removing
81 * ENGINEs from the implementation table
82 */
83int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
84 ENGINE *e, const int *nids, int num_nids,
85 int setdefault)
86{
87 int ret = 0, added = 0;
88 ENGINE_PILE tmplate, *fnd;
89
90 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
91 return 0;
92 if (!(*table))
93 added = 1;
94 if (!int_table_check(table, 1))
95 goto end;
96 /* The cleanup callback needs to be added */
97 if (added && !engine_cleanup_add_first(cleanup)) {
98 lh_ENGINE_PILE_free(&(*table)->piles);
99 *table = NULL;
100 }
101 while (num_nids--) {
102 tmplate.nid = *nids;
103 fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
104 if (!fnd) {
105 fnd = OPENSSL_malloc(sizeof(*fnd));
106 if (fnd == NULL)
107 goto end;
108 fnd->uptodate = 1;
109 fnd->nid = *nids;
110 fnd->sk = sk_ENGINE_new_null();
111 if (!fnd->sk) {
112 OPENSSL_free(fnd);
113 goto end;
114 }
115 fnd->funct = NULL;
116 (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
117 if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) {
118 sk_ENGINE_free(fnd->sk);
119 OPENSSL_free(fnd);
120 goto end;
121 }
122 }
123 /* A registration shouldn't add duplicate entries */
124 (void)sk_ENGINE_delete_ptr(fnd->sk, e);
125 /*
126 * if 'setdefault', this ENGINE goes to the head of the list
127 */
128 if (!sk_ENGINE_push(fnd->sk, e))
129 goto end;
130 /* "touch" this ENGINE_PILE */
131 fnd->uptodate = 0;
132 if (setdefault) {
133 if (!engine_unlocked_init(e)) {
134 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INIT_FAILED);
135 goto end;
136 }
137 if (fnd->funct)
138 engine_unlocked_finish(fnd->funct, 0);
139 fnd->funct = e;
140 fnd->uptodate = 1;
141 }
142 nids++;
143 }
144 ret = 1;
145 end:
146 CRYPTO_THREAD_unlock(global_engine_lock);
147 return ret;
148}
149
150static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
151{
152 int n;
153 /* Iterate the 'c->sk' stack removing any occurrence of 'e' */
154 while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) {
155 (void)sk_ENGINE_delete(pile->sk, n);
156 pile->uptodate = 0;
157 }
158 if (pile->funct == e) {
159 engine_unlocked_finish(e, 0);
160 pile->funct = NULL;
161 }
162}
163
164IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE);
165
166void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
167{
168 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
169 /* Can't return a value. :( */
170 return;
171 if (int_table_check(table, 0))
172 lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e);
173 CRYPTO_THREAD_unlock(global_engine_lock);
174}
175
176static void int_cleanup_cb_doall(ENGINE_PILE *p)
177{
178 if (p == NULL)
179 return;
180 sk_ENGINE_free(p->sk);
181 if (p->funct)
182 engine_unlocked_finish(p->funct, 0);
183 OPENSSL_free(p);
184}
185
186void engine_table_cleanup(ENGINE_TABLE **table)
187{
188 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
189 return;
190 if (*table) {
191 lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall);
192 lh_ENGINE_PILE_free(&(*table)->piles);
193 *table = NULL;
194 }
195 CRYPTO_THREAD_unlock(global_engine_lock);
196}
197
198/* return a functional reference for a given 'nid' */
199ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
200 const char *f, int l)
201{
202 ENGINE *ret = NULL;
203 ENGINE_PILE tmplate, *fnd = NULL;
204 int initres, loop = 0;
205
206#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
207 /* Load the config before trying to check if engines are available */
208 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
209#endif
210
211 if (!(*table)) {
212 OSSL_TRACE3(ENGINE_TABLE,
213 "%s:%d, nid=%d, nothing registered!\n",
214 f, l, nid);
215 return NULL;
216 }
217 ERR_set_mark();
218 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
219 goto end;
220 /*
221 * Check again inside the lock otherwise we could race against cleanup
222 * operations. But don't worry about a debug printout
223 */
224 if (!int_table_check(table, 0))
225 goto end;
226 tmplate.nid = nid;
227 fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
228 if (!fnd)
229 goto end;
230 if (fnd->funct && engine_unlocked_init(fnd->funct)) {
231 OSSL_TRACE4(ENGINE_TABLE,
232 "%s:%d, nid=%d, using ENGINE '%s' cached\n",
233 f, l, nid, fnd->funct->id);
234 ret = fnd->funct;
235 goto end;
236 }
237 if (fnd->uptodate) {
238 ret = fnd->funct;
239 goto end;
240 }
241 trynext:
242 ret = sk_ENGINE_value(fnd->sk, loop++);
243 if (!ret) {
244 OSSL_TRACE3(ENGINE_TABLE,
245 "%s:%d, nid=%d, "
246 "no registered implementations would initialise\n",
247 f, l, nid);
248 goto end;
249 }
250 /* Try to initialise the ENGINE? */
251 if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
252 initres = engine_unlocked_init(ret);
253 else
254 initres = 0;
255 if (initres) {
256 /* Update 'funct' */
257 if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
258 /* If there was a previous default we release it. */
259 if (fnd->funct)
260 engine_unlocked_finish(fnd->funct, 0);
261 fnd->funct = ret;
262 OSSL_TRACE4(ENGINE_TABLE,
263 "%s:%d, nid=%d, setting default to '%s'\n",
264 f, l, nid, ret->id);
265 }
266 OSSL_TRACE4(ENGINE_TABLE,
267 "%s:%d, nid=%d, using newly initialised '%s'\n",
268 f, l, nid, ret->id);
269 goto end;
270 }
271 goto trynext;
272 end:
273 /*
274 * If it failed, it is unlikely to succeed again until some future
275 * registrations have taken place. In all cases, we cache.
276 */
277 if (fnd)
278 fnd->uptodate = 1;
279 if (ret)
280 OSSL_TRACE4(ENGINE_TABLE,
281 "%s:%d, nid=%d, caching ENGINE '%s'\n",
282 f, l, nid, ret->id);
283 else
284 OSSL_TRACE3(ENGINE_TABLE,
285 "%s:%d, nid=%d, caching 'no matching ENGINE'\n",
286 f, l, nid);
287 CRYPTO_THREAD_unlock(global_engine_lock);
288 /*
289 * Whatever happened, any failed init()s are not failures in this
290 * context, so clear our error state.
291 */
292 ERR_pop_to_mark();
293 return ret;
294}
295
296/* Table enumeration */
297
298static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
299{
300 dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
301}
302
303IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL);
304
305void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
306 void *arg)
307{
308 ENGINE_PILE_DOALL dall;
309 dall.cb = cb;
310 dall.arg = arg;
311 if (table)
312 lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);
313}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette