2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 * ECDH support in OpenSSL originally developed by
13 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
19 * The linked-list of pointers to engine types. engine_list_head incorporates
20 * an implicit structural reference but engine_list_tail does not - the
21 * latter is a computational niceity and only points to something that is
22 * already pointed to by its predecessor in the list (or engine_list_head
23 * itself). In the same way, the use of the "prev" pointer in each ENGINE is
24 * to save excessive list iteration, it doesn't correspond to an extra
25 * structural reference. Hence, engine_list_head, and each non-null "next"
26 * pointer account for the list itself assuming exactly 1 structural
27 * reference on each list member.
29 static ENGINE *engine_list_head = NULL;
30 static ENGINE *engine_list_tail = NULL;
33 * This cleanup function is only needed internally. If it should be called,
34 * we register it with the "engine_cleanup_int()" stack to be called during
38 static void engine_list_cleanup(void)
40 ENGINE *iterator = engine_list_head;
42 while (iterator != NULL) {
43 ENGINE_remove(iterator);
44 iterator = engine_list_head;
50 * These static functions starting with a lower case "engine_" always take
51 * place when global_engine_lock has been locked up.
53 static int engine_list_add(ENGINE *e)
56 ENGINE *iterator = NULL;
59 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
62 iterator = engine_list_head;
63 while (iterator && !conflict) {
64 conflict = (strcmp(iterator->id, e->id) == 0);
65 iterator = iterator->next;
68 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
71 if (engine_list_head == NULL) {
72 /* We are adding to an empty list. */
73 if (engine_list_tail) {
74 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
80 * The first time the list allocates, we should register the cleanup.
82 engine_cleanup_add_last(engine_list_cleanup);
84 /* We are adding to the tail of an existing list. */
85 if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
86 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
89 engine_list_tail->next = e;
90 e->prev = engine_list_tail;
93 * Having the engine in the list assumes a structural reference.
96 engine_ref_debug(e, 0, 1);
97 /* However it came to be, e is the last item in the list. */
103 static int engine_list_remove(ENGINE *e)
108 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
111 /* We need to check that e is in our linked list! */
112 iterator = engine_list_head;
113 while (iterator && (iterator != e))
114 iterator = iterator->next;
115 if (iterator == NULL) {
116 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
117 ENGINE_R_ENGINE_IS_NOT_IN_LIST);
120 /* un-link e from the chain. */
122 e->next->prev = e->prev;
124 e->prev->next = e->next;
125 /* Correct our head/tail if necessary. */
126 if (engine_list_head == e)
127 engine_list_head = e->next;
128 if (engine_list_tail == e)
129 engine_list_tail = e->prev;
130 engine_free_util(e, 0);
134 /* Get the first/last "ENGINE" type available. */
135 ENGINE *ENGINE_get_first(void)
139 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
140 ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE);
144 CRYPTO_THREAD_write_lock(global_engine_lock);
145 ret = engine_list_head;
148 engine_ref_debug(ret, 0, 1);
150 CRYPTO_THREAD_unlock(global_engine_lock);
154 ENGINE *ENGINE_get_last(void)
158 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
159 ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE);
163 CRYPTO_THREAD_write_lock(global_engine_lock);
164 ret = engine_list_tail;
167 engine_ref_debug(ret, 0, 1);
169 CRYPTO_THREAD_unlock(global_engine_lock);
173 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
174 ENGINE *ENGINE_get_next(ENGINE *e)
178 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
181 CRYPTO_THREAD_write_lock(global_engine_lock);
184 /* Return a valid structural reference to the next ENGINE */
186 engine_ref_debug(ret, 0, 1);
188 CRYPTO_THREAD_unlock(global_engine_lock);
189 /* Release the structural reference to the previous ENGINE */
194 ENGINE *ENGINE_get_prev(ENGINE *e)
198 ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
201 CRYPTO_THREAD_write_lock(global_engine_lock);
204 /* Return a valid structural reference to the next ENGINE */
206 engine_ref_debug(ret, 0, 1);
208 CRYPTO_THREAD_unlock(global_engine_lock);
209 /* Release the structural reference to the previous ENGINE */
214 /* Add another "ENGINE" type into the list. */
215 int ENGINE_add(ENGINE *e)
219 ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
222 if ((e->id == NULL) || (e->name == NULL)) {
223 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
226 CRYPTO_THREAD_write_lock(global_engine_lock);
227 if (!engine_list_add(e)) {
228 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
231 CRYPTO_THREAD_unlock(global_engine_lock);
235 /* Remove an existing "ENGINE" type from the array. */
236 int ENGINE_remove(ENGINE *e)
240 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
243 CRYPTO_THREAD_write_lock(global_engine_lock);
244 if (!engine_list_remove(e)) {
245 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
248 CRYPTO_THREAD_unlock(global_engine_lock);
252 static void engine_cpy(ENGINE *dest, const ENGINE *src)
255 dest->name = src->name;
256 #ifndef OPENSSL_NO_RSA
257 dest->rsa_meth = src->rsa_meth;
259 #ifndef OPENSSL_NO_DSA
260 dest->dsa_meth = src->dsa_meth;
262 #ifndef OPENSSL_NO_DH
263 dest->dh_meth = src->dh_meth;
265 #ifndef OPENSSL_NO_EC
266 dest->ec_meth = src->ec_meth;
268 dest->rand_meth = src->rand_meth;
269 dest->ciphers = src->ciphers;
270 dest->digests = src->digests;
271 dest->pkey_meths = src->pkey_meths;
272 dest->destroy = src->destroy;
273 dest->init = src->init;
274 dest->finish = src->finish;
275 dest->ctrl = src->ctrl;
276 dest->load_privkey = src->load_privkey;
277 dest->load_pubkey = src->load_pubkey;
278 dest->cmd_defns = src->cmd_defns;
279 dest->flags = src->flags;
282 ENGINE *ENGINE_by_id(const char *id)
285 char *load_dir = NULL;
287 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
290 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
291 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE);
295 CRYPTO_THREAD_write_lock(global_engine_lock);
296 iterator = engine_list_head;
297 while (iterator && (strcmp(id, iterator->id) != 0))
298 iterator = iterator->next;
299 if (iterator != NULL) {
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.
305 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
306 ENGINE *cp = ENGINE_new();
310 engine_cpy(cp, iterator);
314 iterator->struct_ref++;
315 engine_ref_debug(iterator, 0, 1);
318 CRYPTO_THREAD_unlock(global_engine_lock);
319 if (iterator != NULL)
322 * Prevent infinite recursion if we're looking for the dynamic engine.
324 if (strcmp(id, "dynamic")) {
325 if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
326 load_dir = ENGINESDIR;
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",
332 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
333 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
338 ENGINE_free(iterator);
339 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
340 ERR_add_error_data(2, "id=", id);
342 /* EEK! Experimental code ends */
345 int ENGINE_up_ref(ENGINE *e)
349 ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
352 CRYPTO_atomic_add(&e->struct_ref, 1, &i, global_engine_lock);