Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/engine/eng_list.c
Line
Count
Source (jump to first uncovered line)
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-2001 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 "eng_int.h"
66
67
/*
68
 * The linked-list of pointers to engine types. engine_list_head incorporates
69
 * an implicit structural reference but engine_list_tail does not - the
70
 * latter is a computational niceity and only points to something that is
71
 * already pointed to by its predecessor in the list (or engine_list_head
72
 * itself). In the same way, the use of the "prev" pointer in each ENGINE is
73
 * to save excessive list iteration, it doesn't correspond to an extra
74
 * structural reference. Hence, engine_list_head, and each non-null "next"
75
 * pointer account for the list itself assuming exactly 1 structural
76
 * reference on each list member.
77
 */
78
static ENGINE *engine_list_head = NULL;
79
static ENGINE *engine_list_tail = NULL;
80
81
/*
82
 * This cleanup function is only needed internally. If it should be called,
83
 * we register it with the "ENGINE_cleanup()" stack to be called during
84
 * cleanup.
85
 */
86
87
static void engine_list_cleanup(void)
88
0
{
89
0
    ENGINE *iterator = engine_list_head;
90
91
0
    while (iterator != NULL) {
92
0
        ENGINE_remove(iterator);
93
0
        iterator = engine_list_head;
94
0
    }
95
0
    return;
96
0
}
97
98
/*
99
 * These static functions starting with a lower case "engine_" always take
100
 * place when CRYPTO_LOCK_ENGINE has been locked up.
101
 */
102
static int engine_list_add(ENGINE *e)
103
209
{
104
209
    int conflict = 0;
105
209
    ENGINE *iterator = NULL;
106
107
209
    if (e == NULL) {
108
0
        ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
109
0
        return 0;
110
0
    }
111
209
    iterator = engine_list_head;
112
1.25k
    while (iterator && !conflict) {
113
1.04k
        conflict = (strcmp(iterator->id, e->id) == 0);
114
1.04k
        iterator = iterator->next;
115
1.04k
    }
116
209
    if (conflict) {
117
0
        ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
118
0
        return 0;
119
0
    }
120
209
    if (engine_list_head == NULL) {
121
        /* We are adding to an empty list. */
122
19
        if (engine_list_tail) {
123
0
            ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
124
0
            return 0;
125
0
        }
126
19
        engine_list_head = e;
127
19
        e->prev = NULL;
128
        /*
129
         * The first time the list allocates, we should register the cleanup.
130
         */
131
19
        engine_cleanup_add_last(engine_list_cleanup);
132
190
    } else {
133
        /* We are adding to the tail of an existing list. */
134
190
        if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
135
0
            ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
136
0
            return 0;
137
0
        }
138
190
        engine_list_tail->next = e;
139
190
        e->prev = engine_list_tail;
140
190
    }
141
    /*
142
     * Having the engine in the list assumes a structural reference.
143
     */
144
209
    e->struct_ref++;
145
209
    engine_ref_debug(e, 0, 1)
146
        /* However it came to be, e is the last item in the list. */
147
209
        engine_list_tail = e;
148
209
    e->next = NULL;
149
209
    return 1;
150
209
}
151
152
static int engine_list_remove(ENGINE *e)
153
0
{
154
0
    ENGINE *iterator;
155
156
0
    if (e == NULL) {
157
0
        ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
158
0
        return 0;
159
0
    }
160
    /* We need to check that e is in our linked list! */
161
0
    iterator = engine_list_head;
162
0
    while (iterator && (iterator != e))
163
0
        iterator = iterator->next;
164
0
    if (iterator == NULL) {
165
0
        ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
166
0
                  ENGINE_R_ENGINE_IS_NOT_IN_LIST);
167
0
        return 0;
168
0
    }
169
    /* un-link e from the chain. */
170
0
    if (e->next)
171
0
        e->next->prev = e->prev;
172
0
    if (e->prev)
173
0
        e->prev->next = e->next;
174
    /* Correct our head/tail if necessary. */
175
0
    if (engine_list_head == e)
176
0
        engine_list_head = e->next;
177
0
    if (engine_list_tail == e)
178
0
        engine_list_tail = e->prev;
179
0
    engine_free_util(e, 0);
180
0
    return 1;
181
0
}
182
183
/* Get the first/last "ENGINE" type available. */
184
ENGINE *ENGINE_get_first(void)
185
19
{
186
19
    ENGINE *ret;
187
188
19
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
189
19
    ret = engine_list_head;
190
19
    if (ret) {
191
19
        ret->struct_ref++;
192
19
        engine_ref_debug(ret, 0, 1)
193
19
    }
194
19
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
195
19
    return ret;
196
19
}
197
198
ENGINE *ENGINE_get_last(void)
199
0
{
200
0
    ENGINE *ret;
201
202
0
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
203
0
    ret = engine_list_tail;
204
0
    if (ret) {
205
0
        ret->struct_ref++;
206
0
        engine_ref_debug(ret, 0, 1)
207
0
    }
208
0
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
209
0
    return ret;
210
0
}
211
212
/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
213
ENGINE *ENGINE_get_next(ENGINE *e)
214
209
{
215
209
    ENGINE *ret = NULL;
216
209
    if (e == NULL) {
217
0
        ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
218
0
        return 0;
219
0
    }
220
209
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
221
209
    ret = e->next;
222
209
    if (ret) {
223
        /* Return a valid structural refernce to the next ENGINE */
224
190
        ret->struct_ref++;
225
190
        engine_ref_debug(ret, 0, 1)
226
190
    }
227
209
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
228
    /* Release the structural reference to the previous ENGINE */
229
209
    ENGINE_free(e);
230
209
    return ret;
231
209
}
232
233
ENGINE *ENGINE_get_prev(ENGINE *e)
234
0
{
235
0
    ENGINE *ret = NULL;
236
0
    if (e == NULL) {
237
0
        ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
238
0
        return 0;
239
0
    }
240
0
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
241
0
    ret = e->prev;
242
0
    if (ret) {
243
        /* Return a valid structural reference to the next ENGINE */
244
0
        ret->struct_ref++;
245
0
        engine_ref_debug(ret, 0, 1)
246
0
    }
247
0
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
248
    /* Release the structural reference to the previous ENGINE */
249
0
    ENGINE_free(e);
250
0
    return ret;
251
0
}
252
253
/* Add another "ENGINE" type into the list. */
254
int ENGINE_add(ENGINE *e)
255
209
{
256
209
    int to_return = 1;
257
209
    if (e == NULL) {
258
0
        ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
259
0
        return 0;
260
0
    }
261
209
    if ((e->id == NULL) || (e->name == NULL)) {
262
0
        ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
263
0
        return 0;
264
0
    }
265
209
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
266
209
    if (!engine_list_add(e)) {
267
0
        ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
268
0
        to_return = 0;
269
0
    }
270
209
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
271
209
    return to_return;
272
209
}
273
274
/* Remove an existing "ENGINE" type from the array. */
275
int ENGINE_remove(ENGINE *e)
276
0
{
277
0
    int to_return = 1;
278
0
    if (e == NULL) {
279
0
        ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
280
0
        return 0;
281
0
    }
282
0
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
283
0
    if (!engine_list_remove(e)) {
284
0
        ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
285
0
        to_return = 0;
286
0
    }
287
0
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
288
0
    return to_return;
289
0
}
290
291
static void engine_cpy(ENGINE *dest, const ENGINE *src)
292
0
{
293
0
    dest->id = src->id;
294
0
    dest->name = src->name;
295
0
#ifndef OPENSSL_NO_RSA
296
0
    dest->rsa_meth = src->rsa_meth;
297
0
#endif
298
0
#ifndef OPENSSL_NO_DSA
299
0
    dest->dsa_meth = src->dsa_meth;
300
0
#endif
301
0
#ifndef OPENSSL_NO_DH
302
0
    dest->dh_meth = src->dh_meth;
303
0
#endif
304
0
#ifndef OPENSSL_NO_ECDH
305
0
    dest->ecdh_meth = src->ecdh_meth;
306
0
#endif
307
0
#ifndef OPENSSL_NO_ECDSA
308
0
    dest->ecdsa_meth = src->ecdsa_meth;
309
0
#endif
310
0
    dest->rand_meth = src->rand_meth;
311
0
    dest->store_meth = src->store_meth;
312
0
    dest->ciphers = src->ciphers;
313
0
    dest->digests = src->digests;
314
0
    dest->pkey_meths = src->pkey_meths;
315
0
    dest->destroy = src->destroy;
316
0
    dest->init = src->init;
317
0
    dest->finish = src->finish;
318
0
    dest->ctrl = src->ctrl;
319
0
    dest->load_privkey = src->load_privkey;
320
0
    dest->load_pubkey = src->load_pubkey;
321
0
    dest->cmd_defns = src->cmd_defns;
322
0
    dest->flags = src->flags;
323
0
}
324
325
ENGINE *ENGINE_by_id(const char *id)
326
0
{
327
0
    ENGINE *iterator;
328
0
    char *load_dir = NULL;
329
0
    if (id == NULL) {
330
0
        ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
331
0
        return NULL;
332
0
    }
333
0
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
334
0
    iterator = engine_list_head;
335
0
    while (iterator && (strcmp(id, iterator->id) != 0))
336
0
        iterator = iterator->next;
337
0
    if (iterator) {
338
        /*
339
         * We need to return a structural reference. If this is an ENGINE
340
         * type that returns copies, make a duplicate - otherwise increment
341
         * the existing ENGINE's reference count.
342
         */
343
0
        if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
344
0
            ENGINE *cp = ENGINE_new();
345
0
            if (!cp)
346
0
                iterator = NULL;
347
0
            else {
348
0
                engine_cpy(cp, iterator);
349
0
                iterator = cp;
350
0
            }
351
0
        } else {
352
0
            iterator->struct_ref++;
353
0
            engine_ref_debug(iterator, 0, 1)
354
0
        }
355
0
    }
356
0
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
357
#if 0
358
    if (iterator == NULL) {
359
        ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
360
        ERR_add_error_data(2, "id=", id);
361
    }
362
    return iterator;
363
#else
364
    /* EEK! Experimental code starts */
365
0
    if (iterator)
366
0
        return iterator;
367
    /*
368
     * Prevent infinite recusrion if we're looking for the dynamic engine.
369
     */
370
0
    if (strcmp(id, "dynamic")) {
371
# ifdef OPENSSL_SYS_VMS
372
        if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
373
            load_dir = "SSLROOT:[ENGINES]";
374
# else
375
0
        if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
376
0
            load_dir = ENGINESDIR;
377
0
# endif
378
0
        iterator = ENGINE_by_id("dynamic");
379
0
        if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
380
0
            !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
381
0
            !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
382
0
                                    load_dir, 0) ||
383
0
            !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
384
0
            !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
385
0
            goto notfound;
386
0
        return iterator;
387
0
    }
388
0
 notfound:
389
0
    ENGINE_free(iterator);
390
0
    ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
391
0
    ERR_add_error_data(2, "id=", id);
392
0
    return NULL;
393
    /* EEK! Experimental code ends */
394
0
#endif
395
0
}
396
397
int ENGINE_up_ref(ENGINE *e)
398
0
{
399
0
    if (e == NULL) {
400
0
        ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
401
0
        return 0;
402
0
    }
403
0
    CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
404
0
    return 1;
405
0
}