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 CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
140 CRYPTO_THREAD_write_lock(global_engine_lock);
141 ret = engine_list_head;
144 engine_ref_debug(ret, 0, 1);
146 CRYPTO_THREAD_unlock(global_engine_lock);
150 ENGINE *ENGINE_get_last(void)
154 CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
155 CRYPTO_THREAD_write_lock(global_engine_lock);
156 ret = engine_list_tail;
159 engine_ref_debug(ret, 0, 1);
161 CRYPTO_THREAD_unlock(global_engine_lock);
165 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
166 ENGINE *ENGINE_get_next(ENGINE *e)
170 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
173 CRYPTO_THREAD_write_lock(global_engine_lock);
176 /* Return a valid structural reference to the next ENGINE */
178 engine_ref_debug(ret, 0, 1);
180 CRYPTO_THREAD_unlock(global_engine_lock);
181 /* Release the structural reference to the previous ENGINE */
186 ENGINE *ENGINE_get_prev(ENGINE *e)
190 ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
193 CRYPTO_THREAD_write_lock(global_engine_lock);
196 /* Return a valid structural reference to the next ENGINE */
198 engine_ref_debug(ret, 0, 1);
200 CRYPTO_THREAD_unlock(global_engine_lock);
201 /* Release the structural reference to the previous ENGINE */
206 /* Add another "ENGINE" type into the list. */
207 int ENGINE_add(ENGINE *e)
211 ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
214 if ((e->id == NULL) || (e->name == NULL)) {
215 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
218 CRYPTO_THREAD_write_lock(global_engine_lock);
219 if (!engine_list_add(e)) {
220 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
223 CRYPTO_THREAD_unlock(global_engine_lock);
227 /* Remove an existing "ENGINE" type from the array. */
228 int ENGINE_remove(ENGINE *e)
232 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
235 CRYPTO_THREAD_write_lock(global_engine_lock);
236 if (!engine_list_remove(e)) {
237 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
240 CRYPTO_THREAD_unlock(global_engine_lock);
244 static void engine_cpy(ENGINE *dest, const ENGINE *src)
247 dest->name = src->name;
248 #ifndef OPENSSL_NO_RSA
249 dest->rsa_meth = src->rsa_meth;
251 #ifndef OPENSSL_NO_DSA
252 dest->dsa_meth = src->dsa_meth;
254 #ifndef OPENSSL_NO_DH
255 dest->dh_meth = src->dh_meth;
257 #ifndef OPENSSL_NO_EC
258 dest->ec_meth = src->ec_meth;
260 dest->rand_meth = src->rand_meth;
261 dest->ciphers = src->ciphers;
262 dest->digests = src->digests;
263 dest->pkey_meths = src->pkey_meths;
264 dest->destroy = src->destroy;
265 dest->init = src->init;
266 dest->finish = src->finish;
267 dest->ctrl = src->ctrl;
268 dest->load_privkey = src->load_privkey;
269 dest->load_pubkey = src->load_pubkey;
270 dest->cmd_defns = src->cmd_defns;
271 dest->flags = src->flags;
274 ENGINE *ENGINE_by_id(const char *id)
277 char *load_dir = NULL;
279 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
282 CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
283 CRYPTO_THREAD_write_lock(global_engine_lock);
284 iterator = engine_list_head;
285 while (iterator && (strcmp(id, iterator->id) != 0))
286 iterator = iterator->next;
287 if (iterator != NULL) {
289 * We need to return a structural reference. If this is an ENGINE
290 * type that returns copies, make a duplicate - otherwise increment
291 * the existing ENGINE's reference count.
293 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
294 ENGINE *cp = ENGINE_new();
298 engine_cpy(cp, iterator);
302 iterator->struct_ref++;
303 engine_ref_debug(iterator, 0, 1);
306 CRYPTO_THREAD_unlock(global_engine_lock);
307 if (iterator != NULL)
310 * Prevent infinite recursion if we're looking for the dynamic engine.
312 if (strcmp(id, "dynamic")) {
313 if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
314 load_dir = ENGINESDIR;
315 iterator = ENGINE_by_id("dynamic");
316 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
317 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
318 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
320 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
321 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
326 ENGINE_free(iterator);
327 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
328 ERR_add_error_data(2, "id=", id);
330 /* EEK! Experimental code ends */
333 int ENGINE_up_ref(ENGINE *e)
337 ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
340 CRYPTO_atomic_add(&e->struct_ref, 1, &i, global_engine_lock);