eng_list.c revision 340704
1/* crypto/engine/eng_list.c */
2/*
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4 * 2000.
5 */
6/* ====================================================================
7 * Copyright (c) 1999-2018 The OpenSSL Project.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in
18 *    the documentation and/or other materials provided with the
19 *    distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 *    software must display the following acknowledgment:
23 *    "This product includes software developed by the OpenSSL Project
24 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 *    endorse or promote products derived from this software without
28 *    prior written permission. For written permission, please contact
29 *    licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 *    nor may "OpenSSL" appear in their names without prior written
33 *    permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 *    acknowledgment:
37 *    "This product includes software developed by the OpenSSL Project
38 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com).  This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59/* ====================================================================
60 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61 * ECDH support in OpenSSL originally developed by
62 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
63 */
64
65#include "cryptlib.h"
66#include "eng_int.h"
67
68/*
69 * The linked-list of pointers to engine types. engine_list_head incorporates
70 * an implicit structural reference but engine_list_tail does not - the
71 * latter is a computational niceity and only points to something that is
72 * already pointed to by its predecessor in the list (or engine_list_head
73 * itself). In the same way, the use of the "prev" pointer in each ENGINE is
74 * to save excessive list iteration, it doesn't correspond to an extra
75 * structural reference. Hence, engine_list_head, and each non-null "next"
76 * pointer account for the list itself assuming exactly 1 structural
77 * reference on each list member.
78 */
79static ENGINE *engine_list_head = NULL;
80static ENGINE *engine_list_tail = NULL;
81
82/*
83 * This cleanup function is only needed internally. If it should be called,
84 * we register it with the "ENGINE_cleanup()" stack to be called during
85 * cleanup.
86 */
87
88static void engine_list_cleanup(void)
89{
90    ENGINE *iterator = engine_list_head;
91
92    while (iterator != NULL) {
93        ENGINE_remove(iterator);
94        iterator = engine_list_head;
95    }
96    return;
97}
98
99/*
100 * These static functions starting with a lower case "engine_" always take
101 * place when CRYPTO_LOCK_ENGINE has been locked up.
102 */
103static int engine_list_add(ENGINE *e)
104{
105    int conflict = 0;
106    ENGINE *iterator = NULL;
107
108    if (e == NULL) {
109        ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
110        return 0;
111    }
112    iterator = engine_list_head;
113    while (iterator && !conflict) {
114        conflict = (strcmp(iterator->id, e->id) == 0);
115        iterator = iterator->next;
116    }
117    if (conflict) {
118        ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
119        return 0;
120    }
121    if (engine_list_head == NULL) {
122        /* We are adding to an empty list. */
123        if (engine_list_tail) {
124            ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
125            return 0;
126        }
127        engine_list_head = e;
128        e->prev = NULL;
129        /*
130         * The first time the list allocates, we should register the cleanup.
131         */
132        engine_cleanup_add_last(engine_list_cleanup);
133    } else {
134        /* We are adding to the tail of an existing list. */
135        if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
136            ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
137            return 0;
138        }
139        engine_list_tail->next = e;
140        e->prev = engine_list_tail;
141    }
142    /*
143     * Having the engine in the list assumes a structural reference.
144     */
145    e->struct_ref++;
146    engine_ref_debug(e, 0, 1)
147        /* However it came to be, e is the last item in the list. */
148        engine_list_tail = e;
149    e->next = NULL;
150    return 1;
151}
152
153static int engine_list_remove(ENGINE *e)
154{
155    ENGINE *iterator;
156
157    if (e == NULL) {
158        ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
159        return 0;
160    }
161    /* We need to check that e is in our linked list! */
162    iterator = engine_list_head;
163    while (iterator && (iterator != e))
164        iterator = iterator->next;
165    if (iterator == NULL) {
166        ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
167                  ENGINE_R_ENGINE_IS_NOT_IN_LIST);
168        return 0;
169    }
170    /* un-link e from the chain. */
171    if (e->next)
172        e->next->prev = e->prev;
173    if (e->prev)
174        e->prev->next = e->next;
175    /* Correct our head/tail if necessary. */
176    if (engine_list_head == e)
177        engine_list_head = e->next;
178    if (engine_list_tail == e)
179        engine_list_tail = e->prev;
180    engine_free_util(e, 0);
181    return 1;
182}
183
184/* Get the first/last "ENGINE" type available. */
185ENGINE *ENGINE_get_first(void)
186{
187    ENGINE *ret;
188
189    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
190    ret = engine_list_head;
191    if (ret) {
192        ret->struct_ref++;
193        engine_ref_debug(ret, 0, 1)
194    }
195    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
196    return ret;
197}
198
199ENGINE *ENGINE_get_last(void)
200{
201    ENGINE *ret;
202
203    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
204    ret = engine_list_tail;
205    if (ret) {
206        ret->struct_ref++;
207        engine_ref_debug(ret, 0, 1)
208    }
209    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
210    return ret;
211}
212
213/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
214ENGINE *ENGINE_get_next(ENGINE *e)
215{
216    ENGINE *ret = NULL;
217    if (e == NULL) {
218        ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
219        return 0;
220    }
221    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
222    ret = e->next;
223    if (ret) {
224        /* Return a valid structural refernce to the next ENGINE */
225        ret->struct_ref++;
226        engine_ref_debug(ret, 0, 1)
227    }
228    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
229    /* Release the structural reference to the previous ENGINE */
230    ENGINE_free(e);
231    return ret;
232}
233
234ENGINE *ENGINE_get_prev(ENGINE *e)
235{
236    ENGINE *ret = NULL;
237    if (e == NULL) {
238        ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
239        return 0;
240    }
241    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
242    ret = e->prev;
243    if (ret) {
244        /* Return a valid structural reference to the next ENGINE */
245        ret->struct_ref++;
246        engine_ref_debug(ret, 0, 1)
247    }
248    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
249    /* Release the structural reference to the previous ENGINE */
250    ENGINE_free(e);
251    return ret;
252}
253
254/* Add another "ENGINE" type into the list. */
255int ENGINE_add(ENGINE *e)
256{
257    int to_return = 1;
258    if (e == NULL) {
259        ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
260        return 0;
261    }
262    if ((e->id == NULL) || (e->name == NULL)) {
263        ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
264        return 0;
265    }
266    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
267    if (!engine_list_add(e)) {
268        ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
269        to_return = 0;
270    }
271    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
272    return to_return;
273}
274
275/* Remove an existing "ENGINE" type from the array. */
276int ENGINE_remove(ENGINE *e)
277{
278    int to_return = 1;
279    if (e == NULL) {
280        ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
281        return 0;
282    }
283    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
284    if (!engine_list_remove(e)) {
285        ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
286        to_return = 0;
287    }
288    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
289    return to_return;
290}
291
292static void engine_cpy(ENGINE *dest, const ENGINE *src)
293{
294    dest->id = src->id;
295    dest->name = src->name;
296#ifndef OPENSSL_NO_RSA
297    dest->rsa_meth = src->rsa_meth;
298#endif
299#ifndef OPENSSL_NO_DSA
300    dest->dsa_meth = src->dsa_meth;
301#endif
302#ifndef OPENSSL_NO_DH
303    dest->dh_meth = src->dh_meth;
304#endif
305#ifndef OPENSSL_NO_ECDH
306    dest->ecdh_meth = src->ecdh_meth;
307#endif
308#ifndef OPENSSL_NO_ECDSA
309    dest->ecdsa_meth = src->ecdsa_meth;
310#endif
311    dest->rand_meth = src->rand_meth;
312    dest->store_meth = src->store_meth;
313    dest->ciphers = src->ciphers;
314    dest->digests = src->digests;
315    dest->pkey_meths = src->pkey_meths;
316    dest->destroy = src->destroy;
317    dest->init = src->init;
318    dest->finish = src->finish;
319    dest->ctrl = src->ctrl;
320    dest->load_privkey = src->load_privkey;
321    dest->load_pubkey = src->load_pubkey;
322    dest->cmd_defns = src->cmd_defns;
323    dest->flags = src->flags;
324}
325
326ENGINE *ENGINE_by_id(const char *id)
327{
328    ENGINE *iterator;
329    char *load_dir = NULL;
330    if (id == NULL) {
331        ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
332        return NULL;
333    }
334    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
335    iterator = engine_list_head;
336    while (iterator && (strcmp(id, iterator->id) != 0))
337        iterator = iterator->next;
338    if (iterator) {
339        /*
340         * We need to return a structural reference. If this is an ENGINE
341         * type that returns copies, make a duplicate - otherwise increment
342         * the existing ENGINE's reference count.
343         */
344        if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
345            ENGINE *cp = ENGINE_new();
346            if (!cp)
347                iterator = NULL;
348            else {
349                engine_cpy(cp, iterator);
350                iterator = cp;
351            }
352        } else {
353            iterator->struct_ref++;
354            engine_ref_debug(iterator, 0, 1)
355        }
356    }
357    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
358#if 0
359    if (iterator == NULL) {
360        ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
361        ERR_add_error_data(2, "id=", id);
362    }
363    return iterator;
364#else
365    /* EEK! Experimental code starts */
366    if (iterator)
367        return iterator;
368    /*
369     * Prevent infinite recusrion if we're looking for the dynamic engine.
370     */
371    if (strcmp(id, "dynamic")) {
372# ifdef OPENSSL_SYS_VMS
373        if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == 0)
374            load_dir = "SSLROOT:[ENGINES]";
375# else
376        if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == 0)
377            load_dir = ENGINESDIR;
378# endif
379        iterator = ENGINE_by_id("dynamic");
380        if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
381            !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
382            !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
383                                    load_dir, 0) ||
384            !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
385            !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
386            goto notfound;
387        return iterator;
388    }
389 notfound:
390    ENGINE_free(iterator);
391    ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
392    ERR_add_error_data(2, "id=", id);
393    return NULL;
394    /* EEK! Experimental code ends */
395#endif
396}
397
398int ENGINE_up_ref(ENGINE *e)
399{
400    if (e == NULL) {
401        ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
402        return 0;
403    }
404    CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
405    return 1;
406}
407