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 * The linked list of currently loaded dynamic engines.
33 static ENGINE *engine_dyn_list_head = NULL;
34 static ENGINE *engine_dyn_list_tail = NULL;
37 * This cleanup function is only needed internally. If it should be called,
38 * we register it with the "engine_cleanup_int()" stack to be called during
42 static void engine_list_cleanup(void)
44 ENGINE *iterator = engine_list_head;
46 while (iterator != NULL) {
47 ENGINE_remove(iterator);
48 iterator = engine_list_head;
54 * These static functions starting with a lower case "engine_" always take
55 * place when global_engine_lock has been locked up.
57 static int engine_list_add(ENGINE *e)
60 ENGINE *iterator = NULL;
63 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
66 iterator = engine_list_head;
67 while (iterator && !conflict) {
68 conflict = (strcmp(iterator->id, e->id) == 0);
69 iterator = iterator->next;
72 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CONFLICTING_ENGINE_ID);
75 if (engine_list_head == NULL) {
76 /* We are adding to an empty list. */
77 if (engine_list_tail) {
78 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
84 * The first time the list allocates, we should register the cleanup.
86 engine_cleanup_add_last(engine_list_cleanup);
88 /* We are adding to the tail of an existing list. */
89 if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
90 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
93 engine_list_tail->next = e;
94 e->prev = engine_list_tail;
97 * Having the engine in the list assumes a structural reference.
100 ENGINE_REF_PRINT(e, 0, 1);
101 /* However it came to be, e is the last item in the list. */
102 engine_list_tail = e;
107 static int engine_list_remove(ENGINE *e)
112 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
115 /* We need to check that e is in our linked list! */
116 iterator = engine_list_head;
117 while (iterator && (iterator != e))
118 iterator = iterator->next;
119 if (iterator == NULL) {
120 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ENGINE_IS_NOT_IN_LIST);
123 /* un-link e from the chain. */
125 e->next->prev = e->prev;
127 e->prev->next = e->next;
128 /* Correct our head/tail if necessary. */
129 if (engine_list_head == e)
130 engine_list_head = e->next;
131 if (engine_list_tail == e)
132 engine_list_tail = e->prev;
133 engine_free_util(e, 0);
137 /* Add engine to dynamic engine list. */
138 int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
142 ENGINE *iterator = NULL;
147 if (e->dynamic_id == NULL && dynamic_id == NULL)
150 if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
153 if (dynamic_id != NULL) {
154 iterator = engine_dyn_list_head;
155 while (iterator != NULL) {
156 if (iterator->dynamic_id == dynamic_id)
158 iterator = iterator->next;
160 if (e->dynamic_id != NULL)
162 e->dynamic_id = dynamic_id;
165 if (engine_dyn_list_head == NULL) {
166 /* We are adding to an empty list. */
167 if (engine_dyn_list_tail != NULL)
169 engine_dyn_list_head = e;
172 /* We are adding to the tail of an existing list. */
173 if (engine_dyn_list_tail == NULL
174 || engine_dyn_list_tail->next_dyn != NULL)
176 engine_dyn_list_tail->next_dyn = e;
177 e->prev_dyn = engine_dyn_list_tail;
180 engine_dyn_list_tail = e;
186 CRYPTO_THREAD_unlock(global_engine_lock);
190 /* Remove engine from dynamic engine list. */
191 void engine_remove_dynamic_id(ENGINE *e, int not_locked)
193 if (e == NULL || e->dynamic_id == NULL)
196 if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
199 e->dynamic_id = NULL;
201 /* un-link e from the chain. */
202 if (e->next_dyn != NULL)
203 e->next_dyn->prev_dyn = e->prev_dyn;
204 if (e->prev_dyn != NULL)
205 e->prev_dyn->next_dyn = e->next_dyn;
206 /* Correct our head/tail if necessary. */
207 if (engine_dyn_list_head == e)
208 engine_dyn_list_head = e->next_dyn;
209 if (engine_dyn_list_tail == e)
210 engine_dyn_list_tail = e->prev_dyn;
213 CRYPTO_THREAD_unlock(global_engine_lock);
216 /* Get the first/last "ENGINE" type available. */
217 ENGINE *ENGINE_get_first(void)
221 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
222 /* Maybe this should be raised in do_engine_lock_init() */
223 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
227 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
229 ret = engine_list_head;
232 ENGINE_REF_PRINT(ret, 0, 1);
234 CRYPTO_THREAD_unlock(global_engine_lock);
238 ENGINE *ENGINE_get_last(void)
242 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
243 /* Maybe this should be raised in do_engine_lock_init() */
244 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
248 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
250 ret = engine_list_tail;
253 ENGINE_REF_PRINT(ret, 0, 1);
255 CRYPTO_THREAD_unlock(global_engine_lock);
259 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
260 ENGINE *ENGINE_get_next(ENGINE *e)
264 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
267 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
271 /* Return a valid structural reference to the next ENGINE */
273 ENGINE_REF_PRINT(ret, 0, 1);
275 CRYPTO_THREAD_unlock(global_engine_lock);
276 /* Release the structural reference to the previous ENGINE */
281 ENGINE *ENGINE_get_prev(ENGINE *e)
285 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
288 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
292 /* Return a valid structural reference to the next ENGINE */
294 ENGINE_REF_PRINT(ret, 0, 1);
296 CRYPTO_THREAD_unlock(global_engine_lock);
297 /* Release the structural reference to the previous ENGINE */
302 /* Add another "ENGINE" type into the list. */
303 int ENGINE_add(ENGINE *e)
307 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
310 if ((e->id == NULL) || (e->name == NULL)) {
311 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ID_OR_NAME_MISSING);
314 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
316 if (!engine_list_add(e)) {
317 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
320 CRYPTO_THREAD_unlock(global_engine_lock);
324 /* Remove an existing "ENGINE" type from the array. */
325 int ENGINE_remove(ENGINE *e)
329 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
332 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
334 if (!engine_list_remove(e)) {
335 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
338 CRYPTO_THREAD_unlock(global_engine_lock);
342 static void engine_cpy(ENGINE *dest, const ENGINE *src)
345 dest->name = src->name;
346 dest->rsa_meth = src->rsa_meth;
347 #ifndef OPENSSL_NO_DSA
348 dest->dsa_meth = src->dsa_meth;
350 #ifndef OPENSSL_NO_DH
351 dest->dh_meth = src->dh_meth;
353 #ifndef OPENSSL_NO_EC
354 dest->ec_meth = src->ec_meth;
356 dest->rand_meth = src->rand_meth;
357 dest->ciphers = src->ciphers;
358 dest->digests = src->digests;
359 dest->pkey_meths = src->pkey_meths;
360 dest->destroy = src->destroy;
361 dest->init = src->init;
362 dest->finish = src->finish;
363 dest->ctrl = src->ctrl;
364 dest->load_privkey = src->load_privkey;
365 dest->load_pubkey = src->load_pubkey;
366 dest->cmd_defns = src->cmd_defns;
367 dest->flags = src->flags;
368 dest->dynamic_id = src->dynamic_id;
369 engine_add_dynamic_id(dest, NULL, 0);
372 ENGINE *ENGINE_by_id(const char *id)
375 char *load_dir = NULL;
377 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
380 ENGINE_load_builtin_engines();
382 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
383 /* Maybe this should be raised in do_engine_lock_init() */
384 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
388 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
390 iterator = engine_list_head;
391 while (iterator && (strcmp(id, iterator->id) != 0))
392 iterator = iterator->next;
393 if (iterator != NULL) {
395 * We need to return a structural reference. If this is an ENGINE
396 * type that returns copies, make a duplicate - otherwise increment
397 * the existing ENGINE's reference count.
399 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
400 ENGINE *cp = ENGINE_new();
404 engine_cpy(cp, iterator);
408 iterator->struct_ref++;
409 ENGINE_REF_PRINT(iterator, 0, 1);
412 CRYPTO_THREAD_unlock(global_engine_lock);
413 if (iterator != NULL)
416 * Prevent infinite recursion if we're looking for the dynamic engine.
418 if (strcmp(id, "dynamic")) {
419 if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL)
420 load_dir = ENGINESDIR;
421 iterator = ENGINE_by_id("dynamic");
422 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
423 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
424 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
426 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
427 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
432 ENGINE_free(iterator);
433 ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_NO_SUCH_ENGINE, "id=%s", id);
435 /* EEK! Experimental code ends */
438 int ENGINE_up_ref(ENGINE *e)
442 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
445 CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);