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