]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
605856d7 | 2 | * Copyright 2001-2020 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; | |
40e068d5 | 89 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
90 | if (!(*table)) |
91 | added = 1; | |
92 | if (!int_table_check(table, 1)) | |
93 | goto end; | |
94 | if (added) | |
95 | /* The cleanup callback needs to be added */ | |
96 | engine_cleanup_add_first(cleanup); | |
97 | while (num_nids--) { | |
98 | tmplate.nid = *nids; | |
99 | fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate); | |
100 | if (!fnd) { | |
b4faea50 | 101 | fnd = OPENSSL_malloc(sizeof(*fnd)); |
90945fa3 | 102 | if (fnd == NULL) |
0f113f3e MC |
103 | goto end; |
104 | fnd->uptodate = 1; | |
105 | fnd->nid = *nids; | |
106 | fnd->sk = sk_ENGINE_new_null(); | |
107 | if (!fnd->sk) { | |
108 | OPENSSL_free(fnd); | |
109 | goto end; | |
110 | } | |
111 | fnd->funct = NULL; | |
112 | (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd); | |
55a7f77d BE |
113 | if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) { |
114 | sk_ENGINE_free(fnd->sk); | |
115 | OPENSSL_free(fnd); | |
116 | goto end; | |
117 | } | |
0f113f3e | 118 | } |
0d4fb843 | 119 | /* A registration shouldn't add duplicate entries */ |
0f113f3e MC |
120 | (void)sk_ENGINE_delete_ptr(fnd->sk, e); |
121 | /* | |
122 | * if 'setdefault', this ENGINE goes to the head of the list | |
123 | */ | |
124 | if (!sk_ENGINE_push(fnd->sk, e)) | |
125 | goto end; | |
126 | /* "touch" this ENGINE_PILE */ | |
127 | fnd->uptodate = 0; | |
128 | if (setdefault) { | |
129 | if (!engine_unlocked_init(e)) { | |
9311d0c4 | 130 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INIT_FAILED); |
0f113f3e MC |
131 | goto end; |
132 | } | |
133 | if (fnd->funct) | |
134 | engine_unlocked_finish(fnd->funct, 0); | |
135 | fnd->funct = e; | |
136 | fnd->uptodate = 1; | |
137 | } | |
138 | nids++; | |
139 | } | |
140 | ret = 1; | |
141 | end: | |
40e068d5 | 142 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
143 | return ret; |
144 | } | |
145 | ||
2a056de8 | 146 | static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e) |
0f113f3e MC |
147 | { |
148 | int n; | |
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); | |
152 | pile->uptodate = 0; | |
153 | } | |
154 | if (pile->funct == e) { | |
155 | engine_unlocked_finish(e, 0); | |
156 | pile->funct = NULL; | |
157 | } | |
158 | } | |
159 | ||
2a056de8 | 160 | IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE); |
3c1d6bbc | 161 | |
b6d1e52d | 162 | void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e) |
0f113f3e | 163 | { |
40e068d5 | 164 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e | 165 | if (int_table_check(table, 0)) |
2a056de8 | 166 | lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e); |
40e068d5 | 167 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e | 168 | } |
b6d1e52d | 169 | |
3c1d6bbc | 170 | static void int_cleanup_cb_doall(ENGINE_PILE *p) |
0f113f3e | 171 | { |
12a765a5 | 172 | if (p == NULL) |
efa7dd64 | 173 | return; |
0f113f3e MC |
174 | sk_ENGINE_free(p->sk); |
175 | if (p->funct) | |
176 | engine_unlocked_finish(p->funct, 0); | |
177 | OPENSSL_free(p); | |
178 | } | |
179 | ||
b6d1e52d | 180 | void engine_table_cleanup(ENGINE_TABLE **table) |
0f113f3e | 181 | { |
40e068d5 | 182 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e | 183 | if (*table) { |
63c75cd6 | 184 | lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall); |
0f113f3e MC |
185 | lh_ENGINE_PILE_free(&(*table)->piles); |
186 | *table = NULL; | |
187 | } | |
40e068d5 | 188 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e | 189 | } |
b6d1e52d | 190 | |
340f5856 | 191 | /* return a functional reference for a given 'nid' */ |
f272be67 | 192 | ENGINE *engine_table_select_int(ENGINE_TABLE **table, int nid, const char *f, |
0f113f3e | 193 | int l) |
0f113f3e MC |
194 | { |
195 | ENGINE *ret = NULL; | |
196 | ENGINE_PILE tmplate, *fnd = NULL; | |
197 | int initres, loop = 0; | |
b6d1e52d | 198 | |
29dc6e00 MC |
199 | /* Load the config before trying to check if engines are available */ |
200 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); | |
201 | ||
0f113f3e | 202 | if (!(*table)) { |
f272be67 RL |
203 | OSSL_TRACE3(ENGINE_TABLE, |
204 | "%s:%d, nid=%d, nothing registered!\n", | |
205 | f, l, nid); | |
0f113f3e MC |
206 | return NULL; |
207 | } | |
208 | ERR_set_mark(); | |
40e068d5 | 209 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
210 | /* |
211 | * Check again inside the lock otherwise we could race against cleanup | |
f272be67 | 212 | * operations. But don't worry about a debug printout |
0f113f3e MC |
213 | */ |
214 | if (!int_table_check(table, 0)) | |
215 | goto end; | |
216 | tmplate.nid = nid; | |
217 | fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate); | |
218 | if (!fnd) | |
219 | goto end; | |
220 | if (fnd->funct && engine_unlocked_init(fnd->funct)) { | |
f272be67 RL |
221 | OSSL_TRACE4(ENGINE_TABLE, |
222 | "%s:%d, nid=%d, using ENGINE '%s' cached\n", | |
223 | f, l, nid, fnd->funct->id); | |
0f113f3e MC |
224 | ret = fnd->funct; |
225 | goto end; | |
226 | } | |
227 | if (fnd->uptodate) { | |
228 | ret = fnd->funct; | |
229 | goto end; | |
230 | } | |
231 | trynext: | |
232 | ret = sk_ENGINE_value(fnd->sk, loop++); | |
233 | if (!ret) { | |
f272be67 RL |
234 | OSSL_TRACE3(ENGINE_TABLE, |
235 | "%s:%d, nid=%d, " | |
236 | "no registered implementations would initialise\n", | |
237 | f, l, nid); | |
0f113f3e MC |
238 | goto end; |
239 | } | |
240 | /* Try to initialise the ENGINE? */ | |
241 | if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT)) | |
242 | initres = engine_unlocked_init(ret); | |
243 | else | |
244 | initres = 0; | |
245 | if (initres) { | |
246 | /* Update 'funct' */ | |
247 | if ((fnd->funct != ret) && engine_unlocked_init(ret)) { | |
248 | /* If there was a previous default we release it. */ | |
249 | if (fnd->funct) | |
250 | engine_unlocked_finish(fnd->funct, 0); | |
251 | fnd->funct = ret; | |
f272be67 RL |
252 | OSSL_TRACE4(ENGINE_TABLE, |
253 | "%s:%d, nid=%d, setting default to '%s'\n", | |
254 | f, l, nid, ret->id); | |
0f113f3e | 255 | } |
f272be67 RL |
256 | OSSL_TRACE4(ENGINE_TABLE, |
257 | "%s:%d, nid=%d, using newly initialised '%s'\n", | |
258 | f, l, nid, ret->id); | |
0f113f3e MC |
259 | goto end; |
260 | } | |
261 | goto trynext; | |
262 | end: | |
263 | /* | |
264 | * If it failed, it is unlikely to succeed again until some future | |
265 | * registrations have taken place. In all cases, we cache. | |
266 | */ | |
267 | if (fnd) | |
268 | fnd->uptodate = 1; | |
0f113f3e | 269 | if (ret) |
f272be67 RL |
270 | OSSL_TRACE4(ENGINE_TABLE, |
271 | "%s:%d, nid=%d, caching ENGINE '%s'\n", | |
272 | f, l, nid, ret->id); | |
0f113f3e | 273 | else |
f272be67 RL |
274 | OSSL_TRACE3(ENGINE_TABLE, |
275 | "%s:%d, nid=%d, caching 'no matching ENGINE'\n", | |
276 | f, l, nid); | |
40e068d5 | 277 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
278 | /* |
279 | * Whatever happened, any failed init()s are not failures in this | |
280 | * context, so clear our error state. | |
281 | */ | |
282 | ERR_pop_to_mark(); | |
283 | return ret; | |
284 | } | |
2f0550c4 DSH |
285 | |
286 | /* Table enumeration */ | |
287 | ||
2a056de8 | 288 | static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall) |
0f113f3e MC |
289 | { |
290 | dall->cb(pile->nid, pile->sk, pile->funct, dall->arg); | |
291 | } | |
292 | ||
2a056de8 | 293 | IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL); |
2f0550c4 | 294 | |
2f0550c4 | 295 | void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb, |
0f113f3e MC |
296 | void *arg) |
297 | { | |
298 | ENGINE_PILE_DOALL dall; | |
299 | dall.cb = cb; | |
300 | dall.arg = arg; | |
301 | if (table) | |
2a056de8 | 302 | lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall); |
0f113f3e | 303 | } |