]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/engine/eng_list.c
Introduce ASN1_TIME_set_string_X509 API
[thirdparty/openssl.git] / crypto / engine / eng_list.c
CommitLineData
0f113f3e 1/*
b1322259 2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
5270e702 3 *
b1322259
RS
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
5270e702 8 */
b1322259 9
e172d60d
BM
10/* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
0f113f3e 12 * ECDH support in OpenSSL originally developed by
e172d60d
BM
13 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
14 */
5270e702 15
14cfde9c 16#include "eng_int.h"
5270e702 17
0f113f3e
MC
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 */
5270e702
RL
29static ENGINE *engine_list_head = NULL;
30static ENGINE *engine_list_tail = NULL;
b6d1e52d 31
0f113f3e
MC
32/*
33 * This cleanup function is only needed internally. If it should be called,
b3599dbb 34 * we register it with the "engine_cleanup_int()" stack to be called during
0f113f3e
MC
35 * cleanup.
36 */
b6d1e52d
GT
37
38static void engine_list_cleanup(void)
0f113f3e
MC
39{
40 ENGINE *iterator = engine_list_head;
b6d1e52d 41
0f113f3e
MC
42 while (iterator != NULL) {
43 ENGINE_remove(iterator);
44 iterator = engine_list_head;
45 }
46 return;
47}
5270e702 48
0f113f3e
MC
49/*
50 * These static functions starting with a lower case "engine_" always take
40e068d5 51 * place when global_engine_lock has been locked up.
0f113f3e 52 */
5270e702 53static int engine_list_add(ENGINE *e)
0f113f3e
MC
54{
55 int conflict = 0;
56 ENGINE *iterator = NULL;
5270e702 57
0f113f3e
MC
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++;
43d6702d
F
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;
0f113f3e
MC
99 e->next = NULL;
100 return 1;
101}
5270e702
RL
102
103static int engine_list_remove(ENGINE *e)
0f113f3e
MC
104{
105 ENGINE *iterator;
5270e702 106
0f113f3e
MC
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}
5270e702
RL
133
134/* Get the first/last "ENGINE" type available. */
135ENGINE *ENGINE_get_first(void)
0f113f3e
MC
136{
137 ENGINE *ret;
5270e702 138
c2e4e5d2
RL
139 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
140 ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE);
141 return NULL;
142 }
143
40e068d5 144 CRYPTO_THREAD_write_lock(global_engine_lock);
0f113f3e
MC
145 ret = engine_list_head;
146 if (ret) {
147 ret->struct_ref++;
43d6702d 148 engine_ref_debug(ret, 0, 1);
0f113f3e 149 }
40e068d5 150 CRYPTO_THREAD_unlock(global_engine_lock);
0f113f3e
MC
151 return ret;
152}
b6d1e52d 153
5270e702 154ENGINE *ENGINE_get_last(void)
0f113f3e
MC
155{
156 ENGINE *ret;
5270e702 157
c2e4e5d2
RL
158 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
159 ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE);
160 return NULL;
161 }
162
40e068d5 163 CRYPTO_THREAD_write_lock(global_engine_lock);
0f113f3e
MC
164 ret = engine_list_tail;
165 if (ret) {
166 ret->struct_ref++;
43d6702d 167 engine_ref_debug(ret, 0, 1);
0f113f3e 168 }
40e068d5 169 CRYPTO_THREAD_unlock(global_engine_lock);
0f113f3e
MC
170 return ret;
171}
5270e702
RL
172
173/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
174ENGINE *ENGINE_get_next(ENGINE *e)
0f113f3e
MC
175{
176 ENGINE *ret = NULL;
177 if (e == NULL) {
178 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
179 return 0;
180 }
40e068d5 181 CRYPTO_THREAD_write_lock(global_engine_lock);
0f113f3e
MC
182 ret = e->next;
183 if (ret) {
0d4fb843 184 /* Return a valid structural reference to the next ENGINE */
0f113f3e 185 ret->struct_ref++;
43d6702d 186 engine_ref_debug(ret, 0, 1);
0f113f3e 187 }
40e068d5 188 CRYPTO_THREAD_unlock(global_engine_lock);
0f113f3e
MC
189 /* Release the structural reference to the previous ENGINE */
190 ENGINE_free(e);
191 return ret;
192}
b6d1e52d 193
5270e702 194ENGINE *ENGINE_get_prev(ENGINE *e)
0f113f3e
MC
195{
196 ENGINE *ret = NULL;
197 if (e == NULL) {
198 ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
199 return 0;
200 }
40e068d5 201 CRYPTO_THREAD_write_lock(global_engine_lock);
0f113f3e
MC
202 ret = e->prev;
203 if (ret) {
204 /* Return a valid structural reference to the next ENGINE */
205 ret->struct_ref++;
43d6702d 206 engine_ref_debug(ret, 0, 1);
0f113f3e 207 }
40e068d5 208 CRYPTO_THREAD_unlock(global_engine_lock);
0f113f3e
MC
209 /* Release the structural reference to the previous ENGINE */
210 ENGINE_free(e);
211 return ret;
212}
5270e702
RL
213
214/* Add another "ENGINE" type into the list. */
215int ENGINE_add(ENGINE *e)
0f113f3e
MC
216{
217 int to_return = 1;
218 if (e == NULL) {
219 ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
220 return 0;
221 }
222 if ((e->id == NULL) || (e->name == NULL)) {
223 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
5850cc75 224 return 0;
0f113f3e 225 }
40e068d5 226 CRYPTO_THREAD_write_lock(global_engine_lock);
0f113f3e
MC
227 if (!engine_list_add(e)) {
228 ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
229 to_return = 0;
230 }
40e068d5 231 CRYPTO_THREAD_unlock(global_engine_lock);
0f113f3e
MC
232 return to_return;
233}
5270e702
RL
234
235/* Remove an existing "ENGINE" type from the array. */
236int ENGINE_remove(ENGINE *e)
0f113f3e
MC
237{
238 int to_return = 1;
239 if (e == NULL) {
240 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
241 return 0;
242 }
40e068d5 243 CRYPTO_THREAD_write_lock(global_engine_lock);
0f113f3e
MC
244 if (!engine_list_remove(e)) {
245 ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
246 to_return = 0;
247 }
40e068d5 248 CRYPTO_THREAD_unlock(global_engine_lock);
0f113f3e
MC
249 return to_return;
250}
5270e702 251
b6d1e52d 252static void engine_cpy(ENGINE *dest, const ENGINE *src)
0f113f3e
MC
253{
254 dest->id = src->id;
255 dest->name = src->name;
b6d1e52d 256#ifndef OPENSSL_NO_RSA
0f113f3e 257 dest->rsa_meth = src->rsa_meth;
b6d1e52d
GT
258#endif
259#ifndef OPENSSL_NO_DSA
0f113f3e 260 dest->dsa_meth = src->dsa_meth;
b6d1e52d
GT
261#endif
262#ifndef OPENSSL_NO_DH
0f113f3e 263 dest->dh_meth = src->dh_meth;
4d94ae00 264#endif
10bf4fc2 265#ifndef OPENSSL_NO_EC
7d711cbc 266 dest->ec_meth = src->ec_meth;
b6d1e52d 267#endif
0f113f3e 268 dest->rand_meth = src->rand_meth;
0f113f3e
MC
269 dest->ciphers = src->ciphers;
270 dest->digests = src->digests;
271 dest->pkey_meths = src->pkey_meths;
272 dest->destroy = src->destroy;
273 dest->init = src->init;
274 dest->finish = src->finish;
275 dest->ctrl = src->ctrl;
276 dest->load_privkey = src->load_privkey;
277 dest->load_pubkey = src->load_pubkey;
278 dest->cmd_defns = src->cmd_defns;
279 dest->flags = src->flags;
280}
b6d1e52d 281
5270e702 282ENGINE *ENGINE_by_id(const char *id)
0f113f3e
MC
283{
284 ENGINE *iterator;
285 char *load_dir = NULL;
286 if (id == NULL) {
287 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
288 return NULL;
289 }
c2e4e5d2
RL
290 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
291 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE);
292 return NULL;
293 }
294
40e068d5 295 CRYPTO_THREAD_write_lock(global_engine_lock);
0f113f3e
MC
296 iterator = engine_list_head;
297 while (iterator && (strcmp(id, iterator->id) != 0))
298 iterator = iterator->next;
90945fa3 299 if (iterator != NULL) {
0f113f3e
MC
300 /*
301 * We need to return a structural reference. If this is an ENGINE
302 * type that returns copies, make a duplicate - otherwise increment
303 * the existing ENGINE's reference count.
304 */
305 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
306 ENGINE *cp = ENGINE_new();
90945fa3 307 if (cp == NULL)
0f113f3e
MC
308 iterator = NULL;
309 else {
310 engine_cpy(cp, iterator);
311 iterator = cp;
312 }
313 } else {
314 iterator->struct_ref++;
43d6702d 315 engine_ref_debug(iterator, 0, 1);
0f113f3e
MC
316 }
317 }
40e068d5 318 CRYPTO_THREAD_unlock(global_engine_lock);
90945fa3 319 if (iterator != NULL)
0f113f3e
MC
320 return iterator;
321 /*
0d4fb843 322 * Prevent infinite recursion if we're looking for the dynamic engine.
0f113f3e
MC
323 */
324 if (strcmp(id, "dynamic")) {
0f113f3e
MC
325 if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
326 load_dir = ENGINESDIR;
0f113f3e
MC
327 iterator = ENGINE_by_id("dynamic");
328 if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
329 !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
330 !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
331 load_dir, 0) ||
332 !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
333 !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
334 goto notfound;
335 return iterator;
336 }
337 notfound:
338 ENGINE_free(iterator);
339 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
340 ERR_add_error_data(2, "id=", id);
341 return NULL;
342 /* EEK! Experimental code ends */
0f113f3e 343}
314c6670
GT
344
345int ENGINE_up_ref(ENGINE *e)
0f113f3e 346{
40e068d5 347 int i;
0f113f3e
MC
348 if (e == NULL) {
349 ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
350 return 0;
351 }
2f545ae4 352 CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);
0f113f3e
MC
353 return 1;
354}