1 | /* |
2 | * Copyright 2001-2018 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 */ |
17 | struct 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 */ |
31 | struct st_engine_table { |
32 | LHASH_OF(ENGINE_PILE) piles; |
33 | }; /* ENGINE_TABLE */ |
34 | |
35 | typedef 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_***). */ |
41 | static unsigned int table_flags = 0; |
42 | |
43 | /* API function manipulating 'table_flags' */ |
44 | unsigned int ENGINE_get_table_flags(void) |
45 | { |
46 | return table_flags; |
47 | } |
48 | |
49 | void ENGINE_set_table_flags(unsigned int flags) |
50 | { |
51 | table_flags = flags; |
52 | } |
53 | |
54 | /* Internal functions for the "piles" hash table */ |
55 | static unsigned long engine_pile_hash(const ENGINE_PILE *c) |
56 | { |
57 | return c->nid; |
58 | } |
59 | |
60 | static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b) |
61 | { |
62 | return a->nid - b->nid; |
63 | } |
64 | |
65 | static 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 | */ |
83 | int 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 | CRYPTO_THREAD_write_lock(global_engine_lock); |
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) { |
101 | fnd = OPENSSL_malloc(sizeof(*fnd)); |
102 | if (fnd == NULL) |
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); |
113 | if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) { |
114 | sk_ENGINE_free(fnd->sk); |
115 | OPENSSL_free(fnd); |
116 | goto end; |
117 | } |
118 | } |
119 | /* A registration shouldn't add duplicate entries */ |
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)) { |
130 | ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER, |
131 | ENGINE_R_INIT_FAILED); |
132 | goto end; |
133 | } |
134 | if (fnd->funct) |
135 | engine_unlocked_finish(fnd->funct, 0); |
136 | fnd->funct = e; |
137 | fnd->uptodate = 1; |
138 | } |
139 | nids++; |
140 | } |
141 | ret = 1; |
142 | end: |
143 | CRYPTO_THREAD_unlock(global_engine_lock); |
144 | return ret; |
145 | } |
146 | |
147 | static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e) |
148 | { |
149 | int n; |
150 | /* Iterate the 'c->sk' stack removing any occurrence of 'e' */ |
151 | while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) { |
152 | (void)sk_ENGINE_delete(pile->sk, n); |
153 | pile->uptodate = 0; |
154 | } |
155 | if (pile->funct == e) { |
156 | engine_unlocked_finish(e, 0); |
157 | pile->funct = NULL; |
158 | } |
159 | } |
160 | |
161 | IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE); |
162 | |
163 | void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e) |
164 | { |
165 | CRYPTO_THREAD_write_lock(global_engine_lock); |
166 | if (int_table_check(table, 0)) |
167 | lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e); |
168 | CRYPTO_THREAD_unlock(global_engine_lock); |
169 | } |
170 | |
171 | static void int_cleanup_cb_doall(ENGINE_PILE *p) |
172 | { |
173 | if (p == NULL) |
174 | return; |
175 | sk_ENGINE_free(p->sk); |
176 | if (p->funct) |
177 | engine_unlocked_finish(p->funct, 0); |
178 | OPENSSL_free(p); |
179 | } |
180 | |
181 | void engine_table_cleanup(ENGINE_TABLE **table) |
182 | { |
183 | CRYPTO_THREAD_write_lock(global_engine_lock); |
184 | if (*table) { |
185 | lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall); |
186 | lh_ENGINE_PILE_free(&(*table)->piles); |
187 | *table = NULL; |
188 | } |
189 | CRYPTO_THREAD_unlock(global_engine_lock); |
190 | } |
191 | |
192 | /* return a functional reference for a given 'nid' */ |
193 | ENGINE *engine_table_select_int(ENGINE_TABLE **table, int nid, const char *f, |
194 | int l) |
195 | { |
196 | ENGINE *ret = NULL; |
197 | ENGINE_PILE tmplate, *fnd = NULL; |
198 | int initres, loop = 0; |
199 | |
200 | /* Load the config before trying to check if engines are available */ |
201 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
202 | |
203 | if (!(*table)) { |
204 | OSSL_TRACE3(ENGINE_TABLE, |
205 | "%s:%d, nid=%d, nothing registered!\n" , |
206 | f, l, nid); |
207 | return NULL; |
208 | } |
209 | ERR_set_mark(); |
210 | CRYPTO_THREAD_write_lock(global_engine_lock); |
211 | /* |
212 | * Check again inside the lock otherwise we could race against cleanup |
213 | * operations. But don't worry about a debug printout |
214 | */ |
215 | if (!int_table_check(table, 0)) |
216 | goto end; |
217 | tmplate.nid = nid; |
218 | fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate); |
219 | if (!fnd) |
220 | goto end; |
221 | if (fnd->funct && engine_unlocked_init(fnd->funct)) { |
222 | OSSL_TRACE4(ENGINE_TABLE, |
223 | "%s:%d, nid=%d, using ENGINE '%s' cached\n" , |
224 | f, l, nid, fnd->funct->id); |
225 | ret = fnd->funct; |
226 | goto end; |
227 | } |
228 | if (fnd->uptodate) { |
229 | ret = fnd->funct; |
230 | goto end; |
231 | } |
232 | trynext: |
233 | ret = sk_ENGINE_value(fnd->sk, loop++); |
234 | if (!ret) { |
235 | OSSL_TRACE3(ENGINE_TABLE, |
236 | "%s:%d, nid=%d, " |
237 | "no registered implementations would initialise\n" , |
238 | f, l, nid); |
239 | goto end; |
240 | } |
241 | /* Try to initialise the ENGINE? */ |
242 | if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT)) |
243 | initres = engine_unlocked_init(ret); |
244 | else |
245 | initres = 0; |
246 | if (initres) { |
247 | /* Update 'funct' */ |
248 | if ((fnd->funct != ret) && engine_unlocked_init(ret)) { |
249 | /* If there was a previous default we release it. */ |
250 | if (fnd->funct) |
251 | engine_unlocked_finish(fnd->funct, 0); |
252 | fnd->funct = ret; |
253 | OSSL_TRACE4(ENGINE_TABLE, |
254 | "%s:%d, nid=%d, setting default to '%s'\n" , |
255 | f, l, nid, ret->id); |
256 | } |
257 | OSSL_TRACE4(ENGINE_TABLE, |
258 | "%s:%d, nid=%d, using newly initialised '%s'\n" , |
259 | f, l, nid, ret->id); |
260 | goto end; |
261 | } |
262 | goto trynext; |
263 | end: |
264 | /* |
265 | * If it failed, it is unlikely to succeed again until some future |
266 | * registrations have taken place. In all cases, we cache. |
267 | */ |
268 | if (fnd) |
269 | fnd->uptodate = 1; |
270 | if (ret) |
271 | OSSL_TRACE4(ENGINE_TABLE, |
272 | "%s:%d, nid=%d, caching ENGINE '%s'\n" , |
273 | f, l, nid, ret->id); |
274 | else |
275 | OSSL_TRACE3(ENGINE_TABLE, |
276 | "%s:%d, nid=%d, caching 'no matching ENGINE'\n" , |
277 | f, l, nid); |
278 | CRYPTO_THREAD_unlock(global_engine_lock); |
279 | /* |
280 | * Whatever happened, any failed init()s are not failures in this |
281 | * context, so clear our error state. |
282 | */ |
283 | ERR_pop_to_mark(); |
284 | return ret; |
285 | } |
286 | |
287 | /* Table enumeration */ |
288 | |
289 | static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall) |
290 | { |
291 | dall->cb(pile->nid, pile->sk, pile->funct, dall->arg); |
292 | } |
293 | |
294 | IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL); |
295 | |
296 | void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb, |
297 | void *arg) |
298 | { |
299 | ENGINE_PILE_DOALL dall; |
300 | dall.cb = cb; |
301 | dall.arg = arg; |
302 | if (table) |
303 | lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall); |
304 | } |
305 | |