xref: /freebsd/crypto/openssl/crypto/engine/eng_list.c (revision 350b7c3570aa6c87c537e54f706f1866f93a4142)
1 /*
2  * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
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
9  */
10 
11 /* We need to use some engine deprecated APIs */
12 #define OPENSSL_SUPPRESS_DEPRECATED
13 
14 #include "eng_local.h"
15 
16 /*
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.
26  */
27 static ENGINE *engine_list_head = NULL;
28 static ENGINE *engine_list_tail = NULL;
29 
30 /*
31  * The linked list of currently loaded dynamic engines.
32  */
33 static ENGINE *engine_dyn_list_head = NULL;
34 static ENGINE *engine_dyn_list_tail = NULL;
35 
36 /*
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
39  * cleanup.
40  */
41 
42 static void engine_list_cleanup(void)
43 {
44     ENGINE *iterator = engine_list_head;
45 
46     while (iterator != NULL) {
47         ENGINE_remove(iterator);
48         iterator = engine_list_head;
49     }
50     return;
51 }
52 
53 /*
54  * These static functions starting with a lower case "engine_" always take
55  * place when global_engine_lock has been locked up.
56  */
57 static int engine_list_add(ENGINE *e)
58 {
59     int conflict = 0;
60     ENGINE *iterator = NULL;
61 
62     if (e == NULL) {
63         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
64         return 0;
65     }
66     iterator = engine_list_head;
67     while (iterator && !conflict) {
68         conflict = (strcmp(iterator->id, e->id) == 0);
69         iterator = iterator->next;
70     }
71     if (conflict) {
72         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CONFLICTING_ENGINE_ID);
73         return 0;
74     }
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);
79             return 0;
80         }
81         engine_list_head = e;
82         e->prev = NULL;
83         /*
84          * The first time the list allocates, we should register the cleanup.
85          */
86         engine_cleanup_add_last(engine_list_cleanup);
87     } else {
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);
91             return 0;
92         }
93         engine_list_tail->next = e;
94         e->prev = engine_list_tail;
95     }
96     /*
97      * Having the engine in the list assumes a structural reference.
98      */
99     e->struct_ref++;
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;
103     e->next = NULL;
104     return 1;
105 }
106 
107 static int engine_list_remove(ENGINE *e)
108 {
109     ENGINE *iterator;
110 
111     if (e == NULL) {
112         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
113         return 0;
114     }
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);
121         return 0;
122     }
123     /* un-link e from the chain. */
124     if (e->next)
125         e->next->prev = e->prev;
126     if (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);
134     return 1;
135 }
136 
137 /* Add engine to dynamic engine list. */
138 int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
139                           int not_locked)
140 {
141     int result = 0;
142     ENGINE *iterator = NULL;
143 
144     if (e == NULL)
145         return 0;
146 
147     if (e->dynamic_id == NULL && dynamic_id == NULL)
148         return 0;
149 
150     if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
151         return 0;
152 
153     if (dynamic_id != NULL) {
154         iterator = engine_dyn_list_head;
155         while (iterator != NULL) {
156             if (iterator->dynamic_id == dynamic_id)
157                 goto err;
158             iterator = iterator->next;
159         }
160         if (e->dynamic_id != NULL)
161             goto err;
162         e->dynamic_id = dynamic_id;
163     }
164 
165     if (engine_dyn_list_head == NULL) {
166         /* We are adding to an empty list. */
167         if (engine_dyn_list_tail != NULL)
168             goto err;
169         engine_dyn_list_head = e;
170         e->prev_dyn = NULL;
171     } else {
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)
175             goto err;
176         engine_dyn_list_tail->next_dyn = e;
177         e->prev_dyn = engine_dyn_list_tail;
178     }
179 
180     engine_dyn_list_tail = e;
181     e->next_dyn = NULL;
182     result = 1;
183 
184  err:
185     if (not_locked)
186         CRYPTO_THREAD_unlock(global_engine_lock);
187     return result;
188 }
189 
190 /* Remove engine from dynamic engine list. */
191 void engine_remove_dynamic_id(ENGINE *e, int not_locked)
192 {
193     if (e == NULL || e->dynamic_id == NULL)
194         return;
195 
196     if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
197         return;
198 
199     e->dynamic_id = NULL;
200 
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;
211 
212     if (not_locked)
213         CRYPTO_THREAD_unlock(global_engine_lock);
214 }
215 
216 /* Get the first/last "ENGINE" type available. */
217 ENGINE *ENGINE_get_first(void)
218 {
219     ENGINE *ret;
220 
221     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
222         ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
223         return NULL;
224     }
225 
226     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
227         return NULL;
228     ret = engine_list_head;
229     if (ret) {
230         ret->struct_ref++;
231         ENGINE_REF_PRINT(ret, 0, 1);
232     }
233     CRYPTO_THREAD_unlock(global_engine_lock);
234     return ret;
235 }
236 
237 ENGINE *ENGINE_get_last(void)
238 {
239     ENGINE *ret;
240 
241     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
242         ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
243         return NULL;
244     }
245 
246     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
247         return NULL;
248     ret = engine_list_tail;
249     if (ret) {
250         ret->struct_ref++;
251         ENGINE_REF_PRINT(ret, 0, 1);
252     }
253     CRYPTO_THREAD_unlock(global_engine_lock);
254     return ret;
255 }
256 
257 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
258 ENGINE *ENGINE_get_next(ENGINE *e)
259 {
260     ENGINE *ret = NULL;
261     if (e == NULL) {
262         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
263         return NULL;
264     }
265     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
266         return NULL;
267     ret = e->next;
268     if (ret) {
269         /* Return a valid structural reference to the next ENGINE */
270         ret->struct_ref++;
271         ENGINE_REF_PRINT(ret, 0, 1);
272     }
273     CRYPTO_THREAD_unlock(global_engine_lock);
274     /* Release the structural reference to the previous ENGINE */
275     ENGINE_free(e);
276     return ret;
277 }
278 
279 ENGINE *ENGINE_get_prev(ENGINE *e)
280 {
281     ENGINE *ret = NULL;
282     if (e == NULL) {
283         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
284         return NULL;
285     }
286     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
287         return NULL;
288     ret = e->prev;
289     if (ret) {
290         /* Return a valid structural reference to the next ENGINE */
291         ret->struct_ref++;
292         ENGINE_REF_PRINT(ret, 0, 1);
293     }
294     CRYPTO_THREAD_unlock(global_engine_lock);
295     /* Release the structural reference to the previous ENGINE */
296     ENGINE_free(e);
297     return ret;
298 }
299 
300 /* Add another "ENGINE" type into the list. */
301 int ENGINE_add(ENGINE *e)
302 {
303     int to_return = 1;
304     if (e == NULL) {
305         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
306         return 0;
307     }
308     if ((e->id == NULL) || (e->name == NULL)) {
309         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ID_OR_NAME_MISSING);
310         return 0;
311     }
312     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
313         return 0;
314     if (!engine_list_add(e)) {
315         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
316         to_return = 0;
317     }
318     CRYPTO_THREAD_unlock(global_engine_lock);
319     return to_return;
320 }
321 
322 /* Remove an existing "ENGINE" type from the array. */
323 int ENGINE_remove(ENGINE *e)
324 {
325     int to_return = 1;
326     if (e == NULL) {
327         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
328         return 0;
329     }
330     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
331         return 0;
332     if (!engine_list_remove(e)) {
333         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
334         to_return = 0;
335     }
336     CRYPTO_THREAD_unlock(global_engine_lock);
337     return to_return;
338 }
339 
340 static void engine_cpy(ENGINE *dest, const ENGINE *src)
341 {
342     dest->id = src->id;
343     dest->name = src->name;
344     dest->rsa_meth = src->rsa_meth;
345 #ifndef OPENSSL_NO_DSA
346     dest->dsa_meth = src->dsa_meth;
347 #endif
348 #ifndef OPENSSL_NO_DH
349     dest->dh_meth = src->dh_meth;
350 #endif
351 #ifndef OPENSSL_NO_EC
352     dest->ec_meth = src->ec_meth;
353 #endif
354     dest->rand_meth = src->rand_meth;
355     dest->ciphers = src->ciphers;
356     dest->digests = src->digests;
357     dest->pkey_meths = src->pkey_meths;
358     dest->destroy = src->destroy;
359     dest->init = src->init;
360     dest->finish = src->finish;
361     dest->ctrl = src->ctrl;
362     dest->load_privkey = src->load_privkey;
363     dest->load_pubkey = src->load_pubkey;
364     dest->cmd_defns = src->cmd_defns;
365     dest->flags = src->flags;
366     dest->dynamic_id = src->dynamic_id;
367     engine_add_dynamic_id(dest, NULL, 0);
368 }
369 
370 ENGINE *ENGINE_by_id(const char *id)
371 {
372     ENGINE *iterator;
373     char *load_dir = NULL;
374     if (id == NULL) {
375         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
376         return NULL;
377     }
378     ENGINE_load_builtin_engines();
379 
380     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
381         ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
382         return NULL;
383     }
384 
385     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
386         return NULL;
387     iterator = engine_list_head;
388     while (iterator && (strcmp(id, iterator->id) != 0))
389         iterator = iterator->next;
390     if (iterator != NULL) {
391         /*
392          * We need to return a structural reference. If this is an ENGINE
393          * type that returns copies, make a duplicate - otherwise increment
394          * the existing ENGINE's reference count.
395          */
396         if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
397             ENGINE *cp = ENGINE_new();
398             if (cp == NULL)
399                 iterator = NULL;
400             else {
401                 engine_cpy(cp, iterator);
402                 iterator = cp;
403             }
404         } else {
405             iterator->struct_ref++;
406             ENGINE_REF_PRINT(iterator, 0, 1);
407         }
408     }
409     CRYPTO_THREAD_unlock(global_engine_lock);
410     if (iterator != NULL)
411         return iterator;
412     /*
413      * Prevent infinite recursion if we're looking for the dynamic engine.
414      */
415     if (strcmp(id, "dynamic")) {
416         if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL)
417             load_dir = ENGINESDIR;
418         iterator = ENGINE_by_id("dynamic");
419         if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
420             !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
421             !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
422                                     load_dir, 0) ||
423             !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
424             !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
425             goto notfound;
426         return iterator;
427     }
428  notfound:
429     ENGINE_free(iterator);
430     ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_NO_SUCH_ENGINE, "id=%s", id);
431     return NULL;
432     /* EEK! Experimental code ends */
433 }
434 
435 int ENGINE_up_ref(ENGINE *e)
436 {
437     int i;
438     if (e == NULL) {
439         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
440         return 0;
441     }
442     CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);
443     return 1;
444 }
445