]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
5270e702 | 4 | * |
3c120f91 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
5270e702 | 9 | */ |
b1322259 | 10 | |
e4468e6d P |
11 | /* We need to use some engine deprecated APIs */ |
12 | #define OPENSSL_SUPPRESS_DEPRECATED | |
13 | ||
706457b7 | 14 | #include "eng_local.h" |
5270e702 | 15 | |
0f113f3e MC |
16 | /* |
17 | * The linked-list of pointers to engine types. engine_list_head incorporates | |
18 | * an implicit structural reference but engine_list_tail does not - the | |
6bcb4175 JS |
19 | * latter is a computational optimization and only points to something that |
20 | * is already pointed to by its predecessor in the list (or engine_list_head | |
0f113f3e MC |
21 | * itself). In the same way, the use of the "prev" pointer in each ENGINE is |
22 | * to save excessive list iteration, it doesn't correspond to an extra | |
23 | * structural reference. Hence, engine_list_head, and each non-null "next" | |
24 | * pointer account for the list itself assuming exactly 1 structural | |
25 | * reference on each list member. | |
26 | */ | |
5270e702 RL |
27 | static ENGINE *engine_list_head = NULL; |
28 | static ENGINE *engine_list_tail = NULL; | |
b6d1e52d | 29 | |
0f113f3e MC |
30 | /* |
31 | * This cleanup function is only needed internally. If it should be called, | |
b3599dbb | 32 | * we register it with the "engine_cleanup_int()" stack to be called during |
0f113f3e MC |
33 | * cleanup. |
34 | */ | |
b6d1e52d GT |
35 | |
36 | static void engine_list_cleanup(void) | |
0f113f3e MC |
37 | { |
38 | ENGINE *iterator = engine_list_head; | |
b6d1e52d | 39 | |
0f113f3e MC |
40 | while (iterator != NULL) { |
41 | ENGINE_remove(iterator); | |
42 | iterator = engine_list_head; | |
43 | } | |
44 | return; | |
45 | } | |
5270e702 | 46 | |
0f113f3e MC |
47 | /* |
48 | * These static functions starting with a lower case "engine_" always take | |
40e068d5 | 49 | * place when global_engine_lock has been locked up. |
0f113f3e | 50 | */ |
5270e702 | 51 | static int engine_list_add(ENGINE *e) |
0f113f3e MC |
52 | { |
53 | int conflict = 0; | |
54 | ENGINE *iterator = NULL; | |
5270e702 | 55 | |
0f113f3e MC |
56 | if (e == NULL) { |
57 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER); | |
58 | return 0; | |
59 | } | |
60 | iterator = engine_list_head; | |
61 | while (iterator && !conflict) { | |
62 | conflict = (strcmp(iterator->id, e->id) == 0); | |
63 | iterator = iterator->next; | |
64 | } | |
65 | if (conflict) { | |
66 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID); | |
67 | return 0; | |
68 | } | |
69 | if (engine_list_head == NULL) { | |
70 | /* We are adding to an empty list. */ | |
71 | if (engine_list_tail) { | |
72 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR); | |
73 | return 0; | |
74 | } | |
75 | engine_list_head = e; | |
76 | e->prev = NULL; | |
77 | /* | |
78 | * The first time the list allocates, we should register the cleanup. | |
79 | */ | |
80 | engine_cleanup_add_last(engine_list_cleanup); | |
81 | } else { | |
82 | /* We are adding to the tail of an existing list. */ | |
83 | if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) { | |
84 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR); | |
85 | return 0; | |
86 | } | |
87 | engine_list_tail->next = e; | |
88 | e->prev = engine_list_tail; | |
89 | } | |
90 | /* | |
91 | * Having the engine in the list assumes a structural reference. | |
92 | */ | |
93 | e->struct_ref++; | |
43d6702d F |
94 | engine_ref_debug(e, 0, 1); |
95 | /* However it came to be, e is the last item in the list. */ | |
96 | engine_list_tail = e; | |
0f113f3e MC |
97 | e->next = NULL; |
98 | return 1; | |
99 | } | |
5270e702 RL |
100 | |
101 | static int engine_list_remove(ENGINE *e) | |
0f113f3e MC |
102 | { |
103 | ENGINE *iterator; | |
5270e702 | 104 | |
0f113f3e MC |
105 | if (e == NULL) { |
106 | ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER); | |
107 | return 0; | |
108 | } | |
109 | /* We need to check that e is in our linked list! */ | |
110 | iterator = engine_list_head; | |
111 | while (iterator && (iterator != e)) | |
112 | iterator = iterator->next; | |
113 | if (iterator == NULL) { | |
114 | ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, | |
115 | ENGINE_R_ENGINE_IS_NOT_IN_LIST); | |
116 | return 0; | |
117 | } | |
118 | /* un-link e from the chain. */ | |
119 | if (e->next) | |
120 | e->next->prev = e->prev; | |
121 | if (e->prev) | |
122 | e->prev->next = e->next; | |
123 | /* Correct our head/tail if necessary. */ | |
124 | if (engine_list_head == e) | |
125 | engine_list_head = e->next; | |
126 | if (engine_list_tail == e) | |
127 | engine_list_tail = e->prev; | |
128 | engine_free_util(e, 0); | |
129 | return 1; | |
130 | } | |
5270e702 RL |
131 | |
132 | /* Get the first/last "ENGINE" type available. */ | |
133 | ENGINE *ENGINE_get_first(void) | |
0f113f3e MC |
134 | { |
135 | ENGINE *ret; | |
5270e702 | 136 | |
c2e4e5d2 RL |
137 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { |
138 | ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE); | |
139 | return NULL; | |
140 | } | |
141 | ||
40e068d5 | 142 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
143 | ret = engine_list_head; |
144 | if (ret) { | |
145 | ret->struct_ref++; | |
43d6702d | 146 | engine_ref_debug(ret, 0, 1); |
0f113f3e | 147 | } |
40e068d5 | 148 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
149 | return ret; |
150 | } | |
b6d1e52d | 151 | |
5270e702 | 152 | ENGINE *ENGINE_get_last(void) |
0f113f3e MC |
153 | { |
154 | ENGINE *ret; | |
5270e702 | 155 | |
c2e4e5d2 RL |
156 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { |
157 | ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE); | |
158 | return NULL; | |
159 | } | |
160 | ||
40e068d5 | 161 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
162 | ret = engine_list_tail; |
163 | if (ret) { | |
164 | ret->struct_ref++; | |
43d6702d | 165 | engine_ref_debug(ret, 0, 1); |
0f113f3e | 166 | } |
40e068d5 | 167 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
168 | return ret; |
169 | } | |
5270e702 RL |
170 | |
171 | /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */ | |
172 | ENGINE *ENGINE_get_next(ENGINE *e) | |
0f113f3e MC |
173 | { |
174 | ENGINE *ret = NULL; | |
175 | if (e == NULL) { | |
176 | ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER); | |
177 | return 0; | |
178 | } | |
40e068d5 | 179 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
180 | ret = e->next; |
181 | if (ret) { | |
0d4fb843 | 182 | /* Return a valid structural reference to the next ENGINE */ |
0f113f3e | 183 | ret->struct_ref++; |
43d6702d | 184 | engine_ref_debug(ret, 0, 1); |
0f113f3e | 185 | } |
40e068d5 | 186 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
187 | /* Release the structural reference to the previous ENGINE */ |
188 | ENGINE_free(e); | |
189 | return ret; | |
190 | } | |
b6d1e52d | 191 | |
5270e702 | 192 | ENGINE *ENGINE_get_prev(ENGINE *e) |
0f113f3e MC |
193 | { |
194 | ENGINE *ret = NULL; | |
195 | if (e == NULL) { | |
196 | ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER); | |
197 | return 0; | |
198 | } | |
40e068d5 | 199 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
200 | ret = e->prev; |
201 | if (ret) { | |
202 | /* Return a valid structural reference to the next ENGINE */ | |
203 | ret->struct_ref++; | |
43d6702d | 204 | engine_ref_debug(ret, 0, 1); |
0f113f3e | 205 | } |
40e068d5 | 206 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
207 | /* Release the structural reference to the previous ENGINE */ |
208 | ENGINE_free(e); | |
209 | return ret; | |
210 | } | |
5270e702 RL |
211 | |
212 | /* Add another "ENGINE" type into the list. */ | |
213 | int ENGINE_add(ENGINE *e) | |
0f113f3e MC |
214 | { |
215 | int to_return = 1; | |
216 | if (e == NULL) { | |
217 | ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER); | |
218 | return 0; | |
219 | } | |
220 | if ((e->id == NULL) || (e->name == NULL)) { | |
221 | ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING); | |
5850cc75 | 222 | return 0; |
0f113f3e | 223 | } |
40e068d5 | 224 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
225 | if (!engine_list_add(e)) { |
226 | ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR); | |
227 | to_return = 0; | |
228 | } | |
40e068d5 | 229 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
230 | return to_return; |
231 | } | |
5270e702 RL |
232 | |
233 | /* Remove an existing "ENGINE" type from the array. */ | |
234 | int ENGINE_remove(ENGINE *e) | |
0f113f3e MC |
235 | { |
236 | int to_return = 1; | |
237 | if (e == NULL) { | |
238 | ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER); | |
239 | return 0; | |
240 | } | |
40e068d5 | 241 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
242 | if (!engine_list_remove(e)) { |
243 | ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR); | |
244 | to_return = 0; | |
245 | } | |
40e068d5 | 246 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
247 | return to_return; |
248 | } | |
5270e702 | 249 | |
b6d1e52d | 250 | static void engine_cpy(ENGINE *dest, const ENGINE *src) |
0f113f3e MC |
251 | { |
252 | dest->id = src->id; | |
253 | dest->name = src->name; | |
b6d1e52d | 254 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 255 | dest->rsa_meth = src->rsa_meth; |
b6d1e52d GT |
256 | #endif |
257 | #ifndef OPENSSL_NO_DSA | |
0f113f3e | 258 | dest->dsa_meth = src->dsa_meth; |
b6d1e52d GT |
259 | #endif |
260 | #ifndef OPENSSL_NO_DH | |
0f113f3e | 261 | dest->dh_meth = src->dh_meth; |
4d94ae00 | 262 | #endif |
10bf4fc2 | 263 | #ifndef OPENSSL_NO_EC |
7d711cbc | 264 | dest->ec_meth = src->ec_meth; |
b6d1e52d | 265 | #endif |
0f113f3e | 266 | dest->rand_meth = src->rand_meth; |
0f113f3e MC |
267 | dest->ciphers = src->ciphers; |
268 | dest->digests = src->digests; | |
269 | dest->pkey_meths = src->pkey_meths; | |
270 | dest->destroy = src->destroy; | |
271 | dest->init = src->init; | |
272 | dest->finish = src->finish; | |
273 | dest->ctrl = src->ctrl; | |
274 | dest->load_privkey = src->load_privkey; | |
275 | dest->load_pubkey = src->load_pubkey; | |
276 | dest->cmd_defns = src->cmd_defns; | |
277 | dest->flags = src->flags; | |
278 | } | |
b6d1e52d | 279 | |
5270e702 | 280 | ENGINE *ENGINE_by_id(const char *id) |
0f113f3e MC |
281 | { |
282 | ENGINE *iterator; | |
283 | char *load_dir = NULL; | |
284 | if (id == NULL) { | |
285 | ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER); | |
286 | return NULL; | |
287 | } | |
a5c864ce SL |
288 | ENGINE_load_builtin_engines(); |
289 | ||
c2e4e5d2 RL |
290 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { |
291 | ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE); | |
292 | return NULL; | |
293 | } | |
294 | ||
40e068d5 | 295 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
296 | iterator = engine_list_head; |
297 | while (iterator && (strcmp(id, iterator->id) != 0)) | |
298 | iterator = iterator->next; | |
90945fa3 | 299 | if (iterator != NULL) { |
0f113f3e MC |
300 | /* |
301 | * We need to return a structural reference. If this is an ENGINE | |
302 | * type that returns copies, make a duplicate - otherwise increment | |
303 | * the existing ENGINE's reference count. | |
304 | */ | |
305 | if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) { | |
306 | ENGINE *cp = ENGINE_new(); | |
90945fa3 | 307 | if (cp == NULL) |
0f113f3e MC |
308 | iterator = NULL; |
309 | else { | |
310 | engine_cpy(cp, iterator); | |
311 | iterator = cp; | |
312 | } | |
313 | } else { | |
314 | iterator->struct_ref++; | |
43d6702d | 315 | engine_ref_debug(iterator, 0, 1); |
0f113f3e MC |
316 | } |
317 | } | |
40e068d5 | 318 | CRYPTO_THREAD_unlock(global_engine_lock); |
90945fa3 | 319 | if (iterator != NULL) |
0f113f3e MC |
320 | return iterator; |
321 | /* | |
0d4fb843 | 322 | * Prevent infinite recursion if we're looking for the dynamic engine. |
0f113f3e MC |
323 | */ |
324 | if (strcmp(id, "dynamic")) { | |
5c39a55d | 325 | if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL) |
0f113f3e | 326 | load_dir = ENGINESDIR; |
0f113f3e MC |
327 | iterator = ENGINE_by_id("dynamic"); |
328 | if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) || | |
329 | !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) || | |
330 | !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD", | |
331 | load_dir, 0) || | |
332 | !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) || | |
333 | !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0)) | |
334 | goto notfound; | |
335 | return iterator; | |
336 | } | |
337 | notfound: | |
338 | ENGINE_free(iterator); | |
339 | ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE); | |
340 | ERR_add_error_data(2, "id=", id); | |
341 | return NULL; | |
342 | /* EEK! Experimental code ends */ | |
0f113f3e | 343 | } |
314c6670 GT |
344 | |
345 | int ENGINE_up_ref(ENGINE *e) | |
0f113f3e | 346 | { |
40e068d5 | 347 | int i; |
0f113f3e MC |
348 | if (e == NULL) { |
349 | ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER); | |
350 | return 0; | |
351 | } | |
2f545ae4 | 352 | CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock); |
0f113f3e MC |
353 | return 1; |
354 | } |