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;
64 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
67 iterator = engine_list_head;
68 while (iterator && !conflict) {
69 conflict = (strcmp(iterator->id, e->id) == 0);
70 iterator = iterator->next;
73 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CONFLICTING_ENGINE_ID);
78 * Having the engine in the list assumes a structural reference.
80 if (!CRYPTO_UP_REF(&e->struct_ref, &ref, e->refcnt_lock)) {
81 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
84 ENGINE_REF_PRINT(e, 0, 1);
85 if (engine_list_head == NULL) {
86 /* We are adding to an empty list. */
87 if (engine_list_tail != NULL) {
88 CRYPTO_DOWN_REF(&e->struct_ref, &ref, e->refcnt_lock);
89 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
95 * The first time the list allocates, we should register the cleanup.
97 engine_cleanup_add_last(engine_list_cleanup);
99 /* We are adding to the tail of an existing list. */
100 if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
101 CRYPTO_DOWN_REF(&e->struct_ref, &ref, e->refcnt_lock);
102 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
105 engine_list_tail->next = e;
106 e->prev = engine_list_tail;
109 /* However it came to be, e is the last item in the list. */
110 engine_list_tail = e;
115 static int engine_list_remove(ENGINE *e)
120 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
123 /* We need to check that e is in our linked list! */
124 iterator = engine_list_head;
125 while (iterator && (iterator != e))
126 iterator = iterator->next;
127 if (iterator == NULL) {
128 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ENGINE_IS_NOT_IN_LIST);
131 /* un-link e from the chain. */
133 e->next->prev = e->prev;
135 e->prev->next = e->next;
136 /* Correct our head/tail if necessary. */
137 if (engine_list_head == e)
138 engine_list_head = e->next;
139 if (engine_list_tail == e)
140 engine_list_tail = e->prev;
141 engine_free_util(e, 0);
145 /* Add engine to dynamic engine list. */
146 int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
150 ENGINE *iterator = NULL;
155 if (e->dynamic_id == NULL && dynamic_id == NULL)
158 if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
161 if (dynamic_id != NULL) {
162 iterator = engine_dyn_list_head;
163 while (iterator != NULL) {
164 if (iterator->dynamic_id == dynamic_id)
166 iterator = iterator->next;
168 if (e->dynamic_id != NULL)
170 e->dynamic_id = dynamic_id;
173 if (engine_dyn_list_head == NULL) {
174 /* We are adding to an empty list. */
175 if (engine_dyn_list_tail != NULL)
177 engine_dyn_list_head = e;
180 /* We are adding to the tail of an existing list. */
181 if (engine_dyn_list_tail == NULL
182 || engine_dyn_list_tail->next_dyn != NULL)
184 engine_dyn_list_tail->next_dyn = e;
185 e->prev_dyn = engine_dyn_list_tail;
188 engine_dyn_list_tail = e;
194 CRYPTO_THREAD_unlock(global_engine_lock);
198 /* Remove engine from dynamic engine list. */
199 void engine_remove_dynamic_id(ENGINE *e, int not_locked)
201 if (e == NULL || e->dynamic_id == NULL)
204 if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
207 e->dynamic_id = NULL;
209 /* un-link e from the chain. */
210 if (e->next_dyn != NULL)
211 e->next_dyn->prev_dyn = e->prev_dyn;
212 if (e->prev_dyn != NULL)
213 e->prev_dyn->next_dyn = e->next_dyn;
214 /* Correct our head/tail if necessary. */
215 if (engine_dyn_list_head == e)
216 engine_dyn_list_head = e->next_dyn;
217 if (engine_dyn_list_tail == e)
218 engine_dyn_list_tail = e->prev_dyn;
221 CRYPTO_THREAD_unlock(global_engine_lock);
224 /* Get the first/last "ENGINE" type available. */
225 ENGINE *ENGINE_get_first(void)
229 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
230 /* Maybe this should be raised in do_engine_lock_init() */
231 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
235 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
237 ret = engine_list_head;
241 if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
242 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
245 ENGINE_REF_PRINT(ret, 0, 1);
247 CRYPTO_THREAD_unlock(global_engine_lock);
251 ENGINE *ENGINE_get_last(void)
255 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
256 /* Maybe this should be raised in do_engine_lock_init() */
257 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
261 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
263 ret = engine_list_tail;
267 if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
268 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
271 ENGINE_REF_PRINT(ret, 0, 1);
273 CRYPTO_THREAD_unlock(global_engine_lock);
277 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
278 ENGINE *ENGINE_get_next(ENGINE *e)
282 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
285 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
291 /* Return a valid structural reference to the next ENGINE */
292 if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
293 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
296 ENGINE_REF_PRINT(ret, 0, 1);
298 CRYPTO_THREAD_unlock(global_engine_lock);
299 /* Release the structural reference to the previous ENGINE */
304 ENGINE *ENGINE_get_prev(ENGINE *e)
308 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
311 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
317 /* Return a valid structural reference to the next ENGINE */
318 if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
319 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
322 ENGINE_REF_PRINT(ret, 0, 1);
324 CRYPTO_THREAD_unlock(global_engine_lock);
325 /* Release the structural reference to the previous ENGINE */
330 /* Add another "ENGINE" type into the list. */
331 int ENGINE_add(ENGINE *e)
335 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
338 if ((e->id == NULL) || (e->name == NULL)) {
339 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ID_OR_NAME_MISSING);
342 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
344 if (!engine_list_add(e)) {
345 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
348 CRYPTO_THREAD_unlock(global_engine_lock);
352 /* Remove an existing "ENGINE" type from the array. */
353 int ENGINE_remove(ENGINE *e)
357 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
360 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
362 if (!engine_list_remove(e)) {
363 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
366 CRYPTO_THREAD_unlock(global_engine_lock);
370 static void engine_cpy(ENGINE *dest, const ENGINE *src)
373 dest->name = src->name;
374 dest->rsa_meth = src->rsa_meth;
375 #ifndef OPENSSL_NO_DSA
376 dest->dsa_meth = src->dsa_meth;
378 #ifndef OPENSSL_NO_DH
379 dest->dh_meth = src->dh_meth;
381 #ifndef OPENSSL_NO_EC
382 dest->ec_meth = src->ec_meth;
384 dest->rand_meth = src->rand_meth;
385 dest->ciphers = src->ciphers;
386 dest->digests = src->digests;
387 dest->pkey_meths = src->pkey_meths;
388 dest->destroy = src->destroy;
389 dest->init = src->init;
390 dest->finish = src->finish;
391 dest->ctrl = src->ctrl;
392 dest->load_privkey = src->load_privkey;
393 dest->load_pubkey = src->load_pubkey;
394 dest->cmd_defns = src->cmd_defns;
395 dest->flags = src->flags;
396 dest->dynamic_id = src->dynamic_id;
397 engine_add_dynamic_id(dest, NULL, 0);
400 ENGINE *ENGINE_by_id(const char *id)
403 char *load_dir = NULL;
405 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
408 ENGINE_load_builtin_engines();
410 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
411 /* Maybe this should be raised in do_engine_lock_init() */
412 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
416 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
418 iterator = engine_list_head;
419 while (iterator && (strcmp(id, iterator->id) != 0))
420 iterator = iterator->next;
421 if (iterator != NULL) {
423 * We need to return a structural reference. If this is an ENGINE
424 * type that returns copies, make a duplicate - otherwise increment
425 * the existing ENGINE's reference count.
427 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
428 ENGINE *cp = ENGINE_new();
432 engine_cpy(cp, iterator);
438 if (!CRYPTO_UP_REF(&iterator->struct_ref, &ref,
439 iterator->refcnt_lock)) {
440 CRYPTO_THREAD_unlock(global_engine_lock);
441 ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
444 ENGINE_REF_PRINT(iterator, 0, 1);
447 CRYPTO_THREAD_unlock(global_engine_lock);
448 if (iterator != NULL)
451 * Prevent infinite recursion if we're looking for the dynamic engine.
453 if (strcmp(id, "dynamic")) {
454 if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL)
455 load_dir = ENGINESDIR;
456 iterator = ENGINE_by_id("dynamic");
457 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
458 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
459 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
461 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
462 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
467 ENGINE_free(iterator);
468 ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_NO_SUCH_ENGINE, "id=%s", id);
470 /* EEK! Experimental code ends */
473 int ENGINE_up_ref(ENGINE *e)
477 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
480 CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);