2 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
11 /* We need to use some engine deprecated APIs */
12 #define OPENSSL_SUPPRESS_DEPRECATED
14 #include "eng_local.h"
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
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
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.
27 static ENGINE *engine_list_head = NULL;
28 static ENGINE *engine_list_tail = NULL;
31 * This cleanup function is only needed internally. If it should be called,
32 * we register it with the "engine_cleanup_int()" stack to be called during
36 static void engine_list_cleanup(void)
38 ENGINE *iterator = engine_list_head;
40 while (iterator != NULL) {
41 ENGINE_remove(iterator);
42 iterator = engine_list_head;
48 * These static functions starting with a lower case "engine_" always take
49 * place when global_engine_lock has been locked up.
51 static int engine_list_add(ENGINE *e)
54 ENGINE *iterator = NULL;
57 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
60 iterator = engine_list_head;
61 while (iterator && !conflict) {
62 conflict = (strcmp(iterator->id, e->id) == 0);
63 iterator = iterator->next;
66 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CONFLICTING_ENGINE_ID);
69 if (engine_list_head == NULL) {
70 /* We are adding to an empty list. */
71 if (engine_list_tail) {
72 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
78 * The first time the list allocates, we should register the cleanup.
80 engine_cleanup_add_last(engine_list_cleanup);
82 /* We are adding to the tail of an existing list. */
83 if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
84 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
87 engine_list_tail->next = e;
88 e->prev = engine_list_tail;
91 * Having the engine in the list assumes a structural reference.
94 engine_ref_debug(e, 0, 1);
95 /* However it came to be, e is the last item in the list. */
101 static int engine_list_remove(ENGINE *e)
106 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
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 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ENGINE_IS_NOT_IN_LIST);
117 /* un-link e from the chain. */
119 e->next->prev = e->prev;
121 e->prev->next = e->next;
122 /* Correct our head/tail if necessary. */
123 if (engine_list_head == e)
124 engine_list_head = e->next;
125 if (engine_list_tail == e)
126 engine_list_tail = e->prev;
127 engine_free_util(e, 0);
131 /* Get the first/last "ENGINE" type available. */
132 ENGINE *ENGINE_get_first(void)
136 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
137 ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
141 CRYPTO_THREAD_write_lock(global_engine_lock);
142 ret = engine_list_head;
145 engine_ref_debug(ret, 0, 1);
147 CRYPTO_THREAD_unlock(global_engine_lock);
151 ENGINE *ENGINE_get_last(void)
155 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
156 ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
160 CRYPTO_THREAD_write_lock(global_engine_lock);
161 ret = engine_list_tail;
164 engine_ref_debug(ret, 0, 1);
166 CRYPTO_THREAD_unlock(global_engine_lock);
170 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
171 ENGINE *ENGINE_get_next(ENGINE *e)
175 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
178 CRYPTO_THREAD_write_lock(global_engine_lock);
181 /* Return a valid structural reference to the next ENGINE */
183 engine_ref_debug(ret, 0, 1);
185 CRYPTO_THREAD_unlock(global_engine_lock);
186 /* Release the structural reference to the previous ENGINE */
191 ENGINE *ENGINE_get_prev(ENGINE *e)
195 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
198 CRYPTO_THREAD_write_lock(global_engine_lock);
201 /* Return a valid structural reference to the next ENGINE */
203 engine_ref_debug(ret, 0, 1);
205 CRYPTO_THREAD_unlock(global_engine_lock);
206 /* Release the structural reference to the previous ENGINE */
211 /* Add another "ENGINE" type into the list. */
212 int ENGINE_add(ENGINE *e)
216 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
219 if ((e->id == NULL) || (e->name == NULL)) {
220 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ID_OR_NAME_MISSING);
223 CRYPTO_THREAD_write_lock(global_engine_lock);
224 if (!engine_list_add(e)) {
225 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
228 CRYPTO_THREAD_unlock(global_engine_lock);
232 /* Remove an existing "ENGINE" type from the array. */
233 int ENGINE_remove(ENGINE *e)
237 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
240 CRYPTO_THREAD_write_lock(global_engine_lock);
241 if (!engine_list_remove(e)) {
242 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
245 CRYPTO_THREAD_unlock(global_engine_lock);
249 static void engine_cpy(ENGINE *dest, const ENGINE *src)
252 dest->name = src->name;
253 #ifndef OPENSSL_NO_RSA
254 dest->rsa_meth = src->rsa_meth;
256 #ifndef OPENSSL_NO_DSA
257 dest->dsa_meth = src->dsa_meth;
259 #ifndef OPENSSL_NO_DH
260 dest->dh_meth = src->dh_meth;
262 #ifndef OPENSSL_NO_EC
263 dest->ec_meth = src->ec_meth;
265 dest->rand_meth = src->rand_meth;
266 dest->ciphers = src->ciphers;
267 dest->digests = src->digests;
268 dest->pkey_meths = src->pkey_meths;
269 dest->destroy = src->destroy;
270 dest->init = src->init;
271 dest->finish = src->finish;
272 dest->ctrl = src->ctrl;
273 dest->load_privkey = src->load_privkey;
274 dest->load_pubkey = src->load_pubkey;
275 dest->cmd_defns = src->cmd_defns;
276 dest->flags = src->flags;
279 ENGINE *ENGINE_by_id(const char *id)
282 char *load_dir = NULL;
284 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
287 ENGINE_load_builtin_engines();
289 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
290 ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
294 CRYPTO_THREAD_write_lock(global_engine_lock);
295 iterator = engine_list_head;
296 while (iterator && (strcmp(id, iterator->id) != 0))
297 iterator = iterator->next;
298 if (iterator != NULL) {
300 * We need to return a structural reference. If this is an ENGINE
301 * type that returns copies, make a duplicate - otherwise increment
302 * the existing ENGINE's reference count.
304 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
305 ENGINE *cp = ENGINE_new();
309 engine_cpy(cp, iterator);
313 iterator->struct_ref++;
314 engine_ref_debug(iterator, 0, 1);
317 CRYPTO_THREAD_unlock(global_engine_lock);
318 if (iterator != NULL)
321 * Prevent infinite recursion if we're looking for the dynamic engine.
323 if (strcmp(id, "dynamic")) {
324 if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL)
325 load_dir = ENGINESDIR;
326 iterator = ENGINE_by_id("dynamic");
327 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
328 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
329 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
331 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
332 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
337 ENGINE_free(iterator);
338 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_SUCH_ENGINE);
339 ERR_add_error_data(2, "id=", id);
341 /* EEK! Experimental code ends */
344 int ENGINE_up_ref(ENGINE *e)
348 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
351 CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);