2 * Copyright 2001-2021 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_PRINT(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 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
143 ret = engine_list_head;
146 ENGINE_REF_PRINT(ret, 0, 1);
148 CRYPTO_THREAD_unlock(global_engine_lock);
152 ENGINE *ENGINE_get_last(void)
156 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
157 ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
161 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
163 ret = engine_list_tail;
166 ENGINE_REF_PRINT(ret, 0, 1);
168 CRYPTO_THREAD_unlock(global_engine_lock);
172 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
173 ENGINE *ENGINE_get_next(ENGINE *e)
177 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
180 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
184 /* Return a valid structural reference to the next ENGINE */
186 ENGINE_REF_PRINT(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 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
201 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
205 /* Return a valid structural reference to the next ENGINE */
207 ENGINE_REF_PRINT(ret, 0, 1);
209 CRYPTO_THREAD_unlock(global_engine_lock);
210 /* Release the structural reference to the previous ENGINE */
215 /* Add another "ENGINE" type into the list. */
216 int ENGINE_add(ENGINE *e)
220 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
223 if ((e->id == NULL) || (e->name == NULL)) {
224 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ID_OR_NAME_MISSING);
227 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
229 if (!engine_list_add(e)) {
230 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
233 CRYPTO_THREAD_unlock(global_engine_lock);
237 /* Remove an existing "ENGINE" type from the array. */
238 int ENGINE_remove(ENGINE *e)
242 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
245 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
247 if (!engine_list_remove(e)) {
248 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
251 CRYPTO_THREAD_unlock(global_engine_lock);
255 static void engine_cpy(ENGINE *dest, const ENGINE *src)
258 dest->name = src->name;
259 dest->rsa_meth = src->rsa_meth;
260 #ifndef OPENSSL_NO_DSA
261 dest->dsa_meth = src->dsa_meth;
263 #ifndef OPENSSL_NO_DH
264 dest->dh_meth = src->dh_meth;
266 #ifndef OPENSSL_NO_EC
267 dest->ec_meth = src->ec_meth;
269 dest->rand_meth = src->rand_meth;
270 dest->ciphers = src->ciphers;
271 dest->digests = src->digests;
272 dest->pkey_meths = src->pkey_meths;
273 dest->destroy = src->destroy;
274 dest->init = src->init;
275 dest->finish = src->finish;
276 dest->ctrl = src->ctrl;
277 dest->load_privkey = src->load_privkey;
278 dest->load_pubkey = src->load_pubkey;
279 dest->cmd_defns = src->cmd_defns;
280 dest->flags = src->flags;
283 ENGINE *ENGINE_by_id(const char *id)
286 char *load_dir = NULL;
288 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
291 ENGINE_load_builtin_engines();
293 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
294 ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
298 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
300 iterator = engine_list_head;
301 while (iterator && (strcmp(id, iterator->id) != 0))
302 iterator = iterator->next;
303 if (iterator != NULL) {
305 * We need to return a structural reference. If this is an ENGINE
306 * type that returns copies, make a duplicate - otherwise increment
307 * the existing ENGINE's reference count.
309 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
310 ENGINE *cp = ENGINE_new();
314 engine_cpy(cp, iterator);
318 iterator->struct_ref++;
319 ENGINE_REF_PRINT(iterator, 0, 1);
322 CRYPTO_THREAD_unlock(global_engine_lock);
323 if (iterator != NULL)
326 * Prevent infinite recursion if we're looking for the dynamic engine.
328 if (strcmp(id, "dynamic")) {
329 if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL)
330 load_dir = ENGINESDIR;
331 iterator = ENGINE_by_id("dynamic");
332 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
333 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
334 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
336 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
337 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
342 ENGINE_free(iterator);
343 ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_NO_SUCH_ENGINE, "id=%s", id);
345 /* EEK! Experimental code ends */
348 int ENGINE_up_ref(ENGINE *e)
352 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
355 CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);