11db85805688d7d641ea2bec319b326481560fd5
[openssl.git] / crypto / engine / eng_list.c
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     int ref;
62
63     if (e == NULL) {
64         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
65         return 0;
66     }
67     iterator = engine_list_head;
68     while (iterator && !conflict) {
69         conflict = (strcmp(iterator->id, e->id) == 0);
70         iterator = iterator->next;
71     }
72     if (conflict) {
73         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CONFLICTING_ENGINE_ID);
74         return 0;
75     }
76
77     /*
78      * Having the engine in the list assumes a structural reference.
79      */
80     if (!CRYPTO_UP_REF(&e->struct_ref, &ref, e->refcnt_lock)) {
81             ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
82             return 0;
83     }
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);
90             return 0;
91         }
92         engine_list_head = e;
93         e->prev = NULL;
94         /*
95          * The first time the list allocates, we should register the cleanup.
96          */
97         engine_cleanup_add_last(engine_list_cleanup);
98     } else {
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);
103             return 0;
104         }
105         engine_list_tail->next = e;
106         e->prev = engine_list_tail;
107     }
108
109     /* However it came to be, e is the last item in the list. */
110     engine_list_tail = e;
111     e->next = NULL;
112     return 1;
113 }
114
115 static int engine_list_remove(ENGINE *e)
116 {
117     ENGINE *iterator;
118
119     if (e == NULL) {
120         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
121         return 0;
122     }
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);
129         return 0;
130     }
131     /* un-link e from the chain. */
132     if (e->next)
133         e->next->prev = e->prev;
134     if (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);
142     return 1;
143 }
144
145 /* Add engine to dynamic engine list. */
146 int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
147                           int not_locked)
148 {
149     int result = 0;
150     ENGINE *iterator = NULL;
151
152     if (e == NULL)
153         return 0;
154
155     if (e->dynamic_id == NULL && dynamic_id == NULL)
156         return 0;
157
158     if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
159         return 0;
160
161     if (dynamic_id != NULL) {
162         iterator = engine_dyn_list_head;
163         while (iterator != NULL) {
164             if (iterator->dynamic_id == dynamic_id)
165                 goto err;
166             iterator = iterator->next;
167         }
168         if (e->dynamic_id != NULL)
169             goto err;
170         e->dynamic_id = dynamic_id;
171     }
172
173     if (engine_dyn_list_head == NULL) {
174         /* We are adding to an empty list. */
175         if (engine_dyn_list_tail != NULL)
176             goto err;
177         engine_dyn_list_head = e;
178         e->prev_dyn = NULL;
179     } else {
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)
183             goto err;
184         engine_dyn_list_tail->next_dyn = e;
185         e->prev_dyn = engine_dyn_list_tail;
186     }
187
188     engine_dyn_list_tail = e;
189     e->next_dyn = NULL;
190     result = 1;
191
192  err:
193     if (not_locked)
194         CRYPTO_THREAD_unlock(global_engine_lock);
195     return result;
196 }
197
198 /* Remove engine from dynamic engine list. */
199 void engine_remove_dynamic_id(ENGINE *e, int not_locked)
200 {
201     if (e == NULL || e->dynamic_id == NULL)
202         return;
203
204     if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
205         return;
206
207     e->dynamic_id = NULL;
208
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;
219
220     if (not_locked)
221         CRYPTO_THREAD_unlock(global_engine_lock);
222 }
223
224 /* Get the first/last "ENGINE" type available. */
225 ENGINE *ENGINE_get_first(void)
226 {
227     ENGINE *ret;
228
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);
232         return NULL;
233     }
234
235     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
236         return NULL;
237     ret = engine_list_head;
238     if (ret) {
239         int ref;
240
241         if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
242             ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
243             return NULL;
244         }
245         ENGINE_REF_PRINT(ret, 0, 1);
246     }
247     CRYPTO_THREAD_unlock(global_engine_lock);
248     return ret;
249 }
250
251 ENGINE *ENGINE_get_last(void)
252 {
253     ENGINE *ret;
254
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);
258         return NULL;
259     }
260
261     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
262         return NULL;
263     ret = engine_list_tail;
264     if (ret) {
265         int ref;
266
267         if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
268             ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
269             return NULL;
270         }
271         ENGINE_REF_PRINT(ret, 0, 1);
272     }
273     CRYPTO_THREAD_unlock(global_engine_lock);
274     return ret;
275 }
276
277 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
278 ENGINE *ENGINE_get_next(ENGINE *e)
279 {
280     ENGINE *ret = NULL;
281     if (e == NULL) {
282         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
283         return NULL;
284     }
285     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
286         return NULL;
287     ret = e->next;
288     if (ret) {
289         int ref;
290
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);
294             return NULL;
295         }
296         ENGINE_REF_PRINT(ret, 0, 1);
297     }
298     CRYPTO_THREAD_unlock(global_engine_lock);
299     /* Release the structural reference to the previous ENGINE */
300     ENGINE_free(e);
301     return ret;
302 }
303
304 ENGINE *ENGINE_get_prev(ENGINE *e)
305 {
306     ENGINE *ret = NULL;
307     if (e == NULL) {
308         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
309         return NULL;
310     }
311     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
312         return NULL;
313     ret = e->prev;
314     if (ret) {
315         int ref;
316
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);
320             return NULL;
321         }
322         ENGINE_REF_PRINT(ret, 0, 1);
323     }
324     CRYPTO_THREAD_unlock(global_engine_lock);
325     /* Release the structural reference to the previous ENGINE */
326     ENGINE_free(e);
327     return ret;
328 }
329
330 /* Add another "ENGINE" type into the list. */
331 int ENGINE_add(ENGINE *e)
332 {
333     int to_return = 1;
334     if (e == NULL) {
335         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
336         return 0;
337     }
338     if ((e->id == NULL) || (e->name == NULL)) {
339         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ID_OR_NAME_MISSING);
340         return 0;
341     }
342     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
343         return 0;
344     if (!engine_list_add(e)) {
345         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
346         to_return = 0;
347     }
348     CRYPTO_THREAD_unlock(global_engine_lock);
349     return to_return;
350 }
351
352 /* Remove an existing "ENGINE" type from the array. */
353 int ENGINE_remove(ENGINE *e)
354 {
355     int to_return = 1;
356     if (e == NULL) {
357         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
358         return 0;
359     }
360     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
361         return 0;
362     if (!engine_list_remove(e)) {
363         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
364         to_return = 0;
365     }
366     CRYPTO_THREAD_unlock(global_engine_lock);
367     return to_return;
368 }
369
370 static void engine_cpy(ENGINE *dest, const ENGINE *src)
371 {
372     dest->id = src->id;
373     dest->name = src->name;
374     dest->rsa_meth = src->rsa_meth;
375 #ifndef OPENSSL_NO_DSA
376     dest->dsa_meth = src->dsa_meth;
377 #endif
378 #ifndef OPENSSL_NO_DH
379     dest->dh_meth = src->dh_meth;
380 #endif
381 #ifndef OPENSSL_NO_EC
382     dest->ec_meth = src->ec_meth;
383 #endif
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);
398 }
399
400 ENGINE *ENGINE_by_id(const char *id)
401 {
402     ENGINE *iterator;
403     char *load_dir = NULL;
404     if (id == NULL) {
405         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
406         return NULL;
407     }
408     ENGINE_load_builtin_engines();
409
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);
413         return NULL;
414     }
415
416     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
417         return NULL;
418     iterator = engine_list_head;
419     while (iterator && (strcmp(id, iterator->id) != 0))
420         iterator = iterator->next;
421     if (iterator != NULL) {
422         /*
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.
426          */
427         if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
428             ENGINE *cp = ENGINE_new();
429             if (cp == NULL)
430                 iterator = NULL;
431             else {
432                 engine_cpy(cp, iterator);
433                 iterator = cp;
434             }
435         } else {
436             int ref;
437
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);
442                 return NULL;
443             }
444             ENGINE_REF_PRINT(iterator, 0, 1);
445         }
446     }
447     CRYPTO_THREAD_unlock(global_engine_lock);
448     if (iterator != NULL)
449         return iterator;
450     /*
451      * Prevent infinite recursion if we're looking for the dynamic engine.
452      */
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",
460                                     load_dir, 0) ||
461             !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
462             !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
463             goto notfound;
464         return iterator;
465     }
466  notfound:
467     ENGINE_free(iterator);
468     ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_NO_SUCH_ENGINE, "id=%s", id);
469     return NULL;
470     /* EEK! Experimental code ends */
471 }
472
473 int ENGINE_up_ref(ENGINE *e)
474 {
475     int i;
476     if (e == NULL) {
477         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
478         return 0;
479     }
480     CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);
481     return 1;
482 }