Copyright consolidation 09/10
[openssl.git] / crypto / engine / eng_list.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* ====================================================================
11  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12  * ECDH support in OpenSSL originally developed by
13  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
14  */
15
16 #include "eng_int.h"
17
18 /*
19  * The linked-list of pointers to engine types. engine_list_head incorporates
20  * an implicit structural reference but engine_list_tail does not - the
21  * latter is a computational niceity and only points to something that is
22  * already pointed to by its predecessor in the list (or engine_list_head
23  * itself). In the same way, the use of the "prev" pointer in each ENGINE is
24  * to save excessive list iteration, it doesn't correspond to an extra
25  * structural reference. Hence, engine_list_head, and each non-null "next"
26  * pointer account for the list itself assuming exactly 1 structural
27  * reference on each list member.
28  */
29 static ENGINE *engine_list_head = NULL;
30 static ENGINE *engine_list_tail = NULL;
31
32 /*
33  * This cleanup function is only needed internally. If it should be called,
34  * we register it with the "engine_cleanup_int()" stack to be called during
35  * cleanup.
36  */
37
38 static void engine_list_cleanup(void)
39 {
40     ENGINE *iterator = engine_list_head;
41
42     while (iterator != NULL) {
43         ENGINE_remove(iterator);
44         iterator = engine_list_head;
45     }
46     return;
47 }
48
49 /*
50  * These static functions starting with a lower case "engine_" always take
51  * place when global_engine_lock has been locked up.
52  */
53 static int engine_list_add(ENGINE *e)
54 {
55     int conflict = 0;
56     ENGINE *iterator = NULL;
57
58     if (e == NULL) {
59         ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
60         return 0;
61     }
62     iterator = engine_list_head;
63     while (iterator && !conflict) {
64         conflict = (strcmp(iterator->id, e->id) == 0);
65         iterator = iterator->next;
66     }
67     if (conflict) {
68         ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
69         return 0;
70     }
71     if (engine_list_head == NULL) {
72         /* We are adding to an empty list. */
73         if (engine_list_tail) {
74             ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
75             return 0;
76         }
77         engine_list_head = e;
78         e->prev = NULL;
79         /*
80          * The first time the list allocates, we should register the cleanup.
81          */
82         engine_cleanup_add_last(engine_list_cleanup);
83     } else {
84         /* We are adding to the tail of an existing list. */
85         if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
86             ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
87             return 0;
88         }
89         engine_list_tail->next = e;
90         e->prev = engine_list_tail;
91     }
92     /*
93      * Having the engine in the list assumes a structural reference.
94      */
95     e->struct_ref++;
96     engine_ref_debug(e, 0, 1);
97     /* However it came to be, e is the last item in the list. */
98     engine_list_tail = e;
99     e->next = NULL;
100     return 1;
101 }
102
103 static int engine_list_remove(ENGINE *e)
104 {
105     ENGINE *iterator;
106
107     if (e == NULL) {
108         ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
109         return 0;
110     }
111     /* We need to check that e is in our linked list! */
112     iterator = engine_list_head;
113     while (iterator && (iterator != e))
114         iterator = iterator->next;
115     if (iterator == NULL) {
116         ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
117                   ENGINE_R_ENGINE_IS_NOT_IN_LIST);
118         return 0;
119     }
120     /* un-link e from the chain. */
121     if (e->next)
122         e->next->prev = e->prev;
123     if (e->prev)
124         e->prev->next = e->next;
125     /* Correct our head/tail if necessary. */
126     if (engine_list_head == e)
127         engine_list_head = e->next;
128     if (engine_list_tail == e)
129         engine_list_tail = e->prev;
130     engine_free_util(e, 0);
131     return 1;
132 }
133
134 /* Get the first/last "ENGINE" type available. */
135 ENGINE *ENGINE_get_first(void)
136 {
137     ENGINE *ret;
138
139     CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
140     CRYPTO_THREAD_write_lock(global_engine_lock);
141     ret = engine_list_head;
142     if (ret) {
143         ret->struct_ref++;
144         engine_ref_debug(ret, 0, 1);
145     }
146     CRYPTO_THREAD_unlock(global_engine_lock);
147     return ret;
148 }
149
150 ENGINE *ENGINE_get_last(void)
151 {
152     ENGINE *ret;
153
154     CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
155     CRYPTO_THREAD_write_lock(global_engine_lock);
156     ret = engine_list_tail;
157     if (ret) {
158         ret->struct_ref++;
159         engine_ref_debug(ret, 0, 1);
160     }
161     CRYPTO_THREAD_unlock(global_engine_lock);
162     return ret;
163 }
164
165 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
166 ENGINE *ENGINE_get_next(ENGINE *e)
167 {
168     ENGINE *ret = NULL;
169     if (e == NULL) {
170         ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
171         return 0;
172     }
173     CRYPTO_THREAD_write_lock(global_engine_lock);
174     ret = e->next;
175     if (ret) {
176         /* Return a valid structural reference to the next ENGINE */
177         ret->struct_ref++;
178         engine_ref_debug(ret, 0, 1);
179     }
180     CRYPTO_THREAD_unlock(global_engine_lock);
181     /* Release the structural reference to the previous ENGINE */
182     ENGINE_free(e);
183     return ret;
184 }
185
186 ENGINE *ENGINE_get_prev(ENGINE *e)
187 {
188     ENGINE *ret = NULL;
189     if (e == NULL) {
190         ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
191         return 0;
192     }
193     CRYPTO_THREAD_write_lock(global_engine_lock);
194     ret = e->prev;
195     if (ret) {
196         /* Return a valid structural reference to the next ENGINE */
197         ret->struct_ref++;
198         engine_ref_debug(ret, 0, 1);
199     }
200     CRYPTO_THREAD_unlock(global_engine_lock);
201     /* Release the structural reference to the previous ENGINE */
202     ENGINE_free(e);
203     return ret;
204 }
205
206 /* Add another "ENGINE" type into the list. */
207 int ENGINE_add(ENGINE *e)
208 {
209     int to_return = 1;
210     if (e == NULL) {
211         ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
212         return 0;
213     }
214     if ((e->id == NULL) || (e->name == NULL)) {
215         ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
216         return 0;
217     }
218     CRYPTO_THREAD_write_lock(global_engine_lock);
219     if (!engine_list_add(e)) {
220         ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
221         to_return = 0;
222     }
223     CRYPTO_THREAD_unlock(global_engine_lock);
224     return to_return;
225 }
226
227 /* Remove an existing "ENGINE" type from the array. */
228 int ENGINE_remove(ENGINE *e)
229 {
230     int to_return = 1;
231     if (e == NULL) {
232         ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
233         return 0;
234     }
235     CRYPTO_THREAD_write_lock(global_engine_lock);
236     if (!engine_list_remove(e)) {
237         ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
238         to_return = 0;
239     }
240     CRYPTO_THREAD_unlock(global_engine_lock);
241     return to_return;
242 }
243
244 static void engine_cpy(ENGINE *dest, const ENGINE *src)
245 {
246     dest->id = src->id;
247     dest->name = src->name;
248 #ifndef OPENSSL_NO_RSA
249     dest->rsa_meth = src->rsa_meth;
250 #endif
251 #ifndef OPENSSL_NO_DSA
252     dest->dsa_meth = src->dsa_meth;
253 #endif
254 #ifndef OPENSSL_NO_DH
255     dest->dh_meth = src->dh_meth;
256 #endif
257 #ifndef OPENSSL_NO_EC
258     dest->ec_meth = src->ec_meth;
259 #endif
260     dest->rand_meth = src->rand_meth;
261     dest->ciphers = src->ciphers;
262     dest->digests = src->digests;
263     dest->pkey_meths = src->pkey_meths;
264     dest->destroy = src->destroy;
265     dest->init = src->init;
266     dest->finish = src->finish;
267     dest->ctrl = src->ctrl;
268     dest->load_privkey = src->load_privkey;
269     dest->load_pubkey = src->load_pubkey;
270     dest->cmd_defns = src->cmd_defns;
271     dest->flags = src->flags;
272 }
273
274 ENGINE *ENGINE_by_id(const char *id)
275 {
276     ENGINE *iterator;
277     char *load_dir = NULL;
278     if (id == NULL) {
279         ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
280         return NULL;
281     }
282     CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
283     CRYPTO_THREAD_write_lock(global_engine_lock);
284     iterator = engine_list_head;
285     while (iterator && (strcmp(id, iterator->id) != 0))
286         iterator = iterator->next;
287     if (iterator != NULL) {
288         /*
289          * We need to return a structural reference. If this is an ENGINE
290          * type that returns copies, make a duplicate - otherwise increment
291          * the existing ENGINE's reference count.
292          */
293         if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
294             ENGINE *cp = ENGINE_new();
295             if (cp == NULL)
296                 iterator = NULL;
297             else {
298                 engine_cpy(cp, iterator);
299                 iterator = cp;
300             }
301         } else {
302             iterator->struct_ref++;
303             engine_ref_debug(iterator, 0, 1);
304         }
305     }
306     CRYPTO_THREAD_unlock(global_engine_lock);
307     if (iterator != NULL)
308         return iterator;
309     /*
310      * Prevent infinite recursion if we're looking for the dynamic engine.
311      */
312     if (strcmp(id, "dynamic")) {
313         if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
314             load_dir = ENGINESDIR;
315         iterator = ENGINE_by_id("dynamic");
316         if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
317             !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
318             !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
319                                     load_dir, 0) ||
320             !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
321             !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
322             goto notfound;
323         return iterator;
324     }
325  notfound:
326     ENGINE_free(iterator);
327     ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
328     ERR_add_error_data(2, "id=", id);
329     return NULL;
330     /* EEK! Experimental code ends */
331 }
332
333 int ENGINE_up_ref(ENGINE *e)
334 {
335     int i;
336     if (e == NULL) {
337         ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
338         return 0;
339     }
340     CRYPTO_atomic_add(&e->struct_ref, 1, &i, global_engine_lock);
341     return 1;
342 }