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