hkdf: when HMAC key is all zeros, still set a valid key length
[openssl.git] / crypto / engine / eng_list.c
1 /*
2  * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 /* We need to use some engine deprecated APIs */
12 #define OPENSSL_SUPPRESS_DEPRECATED
13
14 #include "eng_local.h"
15
16 /*
17  * The linked-list of pointers to engine types. engine_list_head incorporates
18  * an implicit structural reference but engine_list_tail does not - the
19  * latter is a computational optimization and only points to something that
20  * is already pointed to by its predecessor in the list (or engine_list_head
21  * itself). In the same way, the use of the "prev" pointer in each ENGINE is
22  * to save excessive list iteration, it doesn't correspond to an extra
23  * structural reference. Hence, engine_list_head, and each non-null "next"
24  * pointer account for the list itself assuming exactly 1 structural
25  * reference on each list member.
26  */
27 static ENGINE *engine_list_head = NULL;
28 static ENGINE *engine_list_tail = NULL;
29
30 /*
31  * The linked list of currently loaded dynamic engines.
32  */
33 static ENGINE *engine_dyn_list_head = NULL;
34 static ENGINE *engine_dyn_list_tail = NULL;
35
36 /*
37  * This cleanup function is only needed internally. If it should be called,
38  * we register it with the "engine_cleanup_int()" stack to be called during
39  * cleanup.
40  */
41
42 static void engine_list_cleanup(void)
43 {
44     ENGINE *iterator = engine_list_head;
45
46     while (iterator != NULL) {
47         ENGINE_remove(iterator);
48         iterator = engine_list_head;
49     }
50     return;
51 }
52
53 /*
54  * These static functions starting with a lower case "engine_" always take
55  * place when global_engine_lock has been locked up.
56  */
57 static int engine_list_add(ENGINE *e)
58 {
59     int conflict = 0;
60     ENGINE *iterator = NULL;
61
62     if (e == NULL) {
63         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
64         return 0;
65     }
66     iterator = engine_list_head;
67     while (iterator && !conflict) {
68         conflict = (strcmp(iterator->id, e->id) == 0);
69         iterator = iterator->next;
70     }
71     if (conflict) {
72         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CONFLICTING_ENGINE_ID);
73         return 0;
74     }
75     if (engine_list_head == NULL) {
76         /* We are adding to an empty list. */
77         if (engine_list_tail) {
78             ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
79             return 0;
80         }
81         engine_list_head = e;
82         e->prev = NULL;
83         /*
84          * The first time the list allocates, we should register the cleanup.
85          */
86         engine_cleanup_add_last(engine_list_cleanup);
87     } else {
88         /* We are adding to the tail of an existing list. */
89         if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
90             ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
91             return 0;
92         }
93         engine_list_tail->next = e;
94         e->prev = engine_list_tail;
95     }
96     /*
97      * Having the engine in the list assumes a structural reference.
98      */
99     e->struct_ref++;
100     ENGINE_REF_PRINT(e, 0, 1);
101     /* However it came to be, e is the last item in the list. */
102     engine_list_tail = e;
103     e->next = NULL;
104     return 1;
105 }
106
107 static int engine_list_remove(ENGINE *e)
108 {
109     ENGINE *iterator;
110
111     if (e == NULL) {
112         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
113         return 0;
114     }
115     /* We need to check that e is in our linked list! */
116     iterator = engine_list_head;
117     while (iterator && (iterator != e))
118         iterator = iterator->next;
119     if (iterator == NULL) {
120         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ENGINE_IS_NOT_IN_LIST);
121         return 0;
122     }
123     /* un-link e from the chain. */
124     if (e->next)
125         e->next->prev = e->prev;
126     if (e->prev)
127         e->prev->next = e->next;
128     /* Correct our head/tail if necessary. */
129     if (engine_list_head == e)
130         engine_list_head = e->next;
131     if (engine_list_tail == e)
132         engine_list_tail = e->prev;
133     engine_free_util(e, 0);
134     return 1;
135 }
136
137 /* Add engine to dynamic engine list. */
138 int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
139                           int not_locked)
140 {
141     int result = 0;
142     ENGINE *iterator = NULL;
143
144     if (e == NULL)
145         return 0;
146
147     if (e->dynamic_id == NULL && dynamic_id == NULL)
148         return 0;
149
150     if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
151         return 0;
152
153     if (dynamic_id != NULL) {
154         iterator = engine_dyn_list_head;
155         while (iterator != NULL) {
156             if (iterator->dynamic_id == dynamic_id)
157                 goto err;
158             iterator = iterator->next;
159         }
160         if (e->dynamic_id != NULL)
161             goto err;
162         e->dynamic_id = dynamic_id;
163     }
164
165     if (engine_dyn_list_head == NULL) {
166         /* We are adding to an empty list. */
167         if (engine_dyn_list_tail != NULL)
168             goto err;
169         engine_dyn_list_head = e;
170         e->prev_dyn = NULL;
171     } else {
172         /* We are adding to the tail of an existing list. */
173         if (engine_dyn_list_tail == NULL
174             || engine_dyn_list_tail->next_dyn != NULL)
175             goto err;
176         engine_dyn_list_tail->next_dyn = e;
177         e->prev_dyn = engine_dyn_list_tail;
178     }
179
180     engine_dyn_list_tail = e;
181     e->next_dyn = NULL;
182     result = 1;
183
184  err:
185     if (not_locked)
186         CRYPTO_THREAD_unlock(global_engine_lock);
187     return result;
188 }
189
190 /* Remove engine from dynamic engine list. */
191 void engine_remove_dynamic_id(ENGINE *e, int not_locked)
192 {
193     if (e == NULL || e->dynamic_id == NULL)
194         return;
195
196     if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
197         return;
198
199     e->dynamic_id = NULL;
200
201     /* un-link e from the chain. */
202     if (e->next_dyn != NULL)
203         e->next_dyn->prev_dyn = e->prev_dyn;
204     if (e->prev_dyn != NULL)
205         e->prev_dyn->next_dyn = e->next_dyn;
206     /* Correct our head/tail if necessary. */
207     if (engine_dyn_list_head == e)
208         engine_dyn_list_head = e->next_dyn;
209     if (engine_dyn_list_tail == e)
210         engine_dyn_list_tail = e->prev_dyn;
211
212     if (not_locked)
213         CRYPTO_THREAD_unlock(global_engine_lock);
214 }
215
216 /* Get the first/last "ENGINE" type available. */
217 ENGINE *ENGINE_get_first(void)
218 {
219     ENGINE *ret;
220
221     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
222         /* Maybe this should be raised in do_engine_lock_init() */
223         ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
224         return NULL;
225     }
226
227     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
228         return NULL;
229     ret = engine_list_head;
230     if (ret) {
231         ret->struct_ref++;
232         ENGINE_REF_PRINT(ret, 0, 1);
233     }
234     CRYPTO_THREAD_unlock(global_engine_lock);
235     return ret;
236 }
237
238 ENGINE *ENGINE_get_last(void)
239 {
240     ENGINE *ret;
241
242     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
243         /* Maybe this should be raised in do_engine_lock_init() */
244         ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
245         return NULL;
246     }
247
248     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
249         return NULL;
250     ret = engine_list_tail;
251     if (ret) {
252         ret->struct_ref++;
253         ENGINE_REF_PRINT(ret, 0, 1);
254     }
255     CRYPTO_THREAD_unlock(global_engine_lock);
256     return ret;
257 }
258
259 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
260 ENGINE *ENGINE_get_next(ENGINE *e)
261 {
262     ENGINE *ret = NULL;
263     if (e == NULL) {
264         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
265         return NULL;
266     }
267     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
268         return NULL;
269     ret = e->next;
270     if (ret) {
271         /* Return a valid structural reference to the next ENGINE */
272         ret->struct_ref++;
273         ENGINE_REF_PRINT(ret, 0, 1);
274     }
275     CRYPTO_THREAD_unlock(global_engine_lock);
276     /* Release the structural reference to the previous ENGINE */
277     ENGINE_free(e);
278     return ret;
279 }
280
281 ENGINE *ENGINE_get_prev(ENGINE *e)
282 {
283     ENGINE *ret = NULL;
284     if (e == NULL) {
285         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
286         return NULL;
287     }
288     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
289         return NULL;
290     ret = e->prev;
291     if (ret) {
292         /* Return a valid structural reference to the next ENGINE */
293         ret->struct_ref++;
294         ENGINE_REF_PRINT(ret, 0, 1);
295     }
296     CRYPTO_THREAD_unlock(global_engine_lock);
297     /* Release the structural reference to the previous ENGINE */
298     ENGINE_free(e);
299     return ret;
300 }
301
302 /* Add another "ENGINE" type into the list. */
303 int ENGINE_add(ENGINE *e)
304 {
305     int to_return = 1;
306     if (e == NULL) {
307         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
308         return 0;
309     }
310     if ((e->id == NULL) || (e->name == NULL)) {
311         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ID_OR_NAME_MISSING);
312         return 0;
313     }
314     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
315         return 0;
316     if (!engine_list_add(e)) {
317         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
318         to_return = 0;
319     }
320     CRYPTO_THREAD_unlock(global_engine_lock);
321     return to_return;
322 }
323
324 /* Remove an existing "ENGINE" type from the array. */
325 int ENGINE_remove(ENGINE *e)
326 {
327     int to_return = 1;
328     if (e == NULL) {
329         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
330         return 0;
331     }
332     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
333         return 0;
334     if (!engine_list_remove(e)) {
335         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
336         to_return = 0;
337     }
338     CRYPTO_THREAD_unlock(global_engine_lock);
339     return to_return;
340 }
341
342 static void engine_cpy(ENGINE *dest, const ENGINE *src)
343 {
344     dest->id = src->id;
345     dest->name = src->name;
346     dest->rsa_meth = src->rsa_meth;
347 #ifndef OPENSSL_NO_DSA
348     dest->dsa_meth = src->dsa_meth;
349 #endif
350 #ifndef OPENSSL_NO_DH
351     dest->dh_meth = src->dh_meth;
352 #endif
353 #ifndef OPENSSL_NO_EC
354     dest->ec_meth = src->ec_meth;
355 #endif
356     dest->rand_meth = src->rand_meth;
357     dest->ciphers = src->ciphers;
358     dest->digests = src->digests;
359     dest->pkey_meths = src->pkey_meths;
360     dest->destroy = src->destroy;
361     dest->init = src->init;
362     dest->finish = src->finish;
363     dest->ctrl = src->ctrl;
364     dest->load_privkey = src->load_privkey;
365     dest->load_pubkey = src->load_pubkey;
366     dest->cmd_defns = src->cmd_defns;
367     dest->flags = src->flags;
368     dest->dynamic_id = src->dynamic_id;
369     engine_add_dynamic_id(dest, NULL, 0);
370 }
371
372 ENGINE *ENGINE_by_id(const char *id)
373 {
374     ENGINE *iterator;
375     char *load_dir = NULL;
376     if (id == NULL) {
377         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
378         return NULL;
379     }
380     ENGINE_load_builtin_engines();
381
382     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
383         /* Maybe this should be raised in do_engine_lock_init() */
384         ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
385         return NULL;
386     }
387
388     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
389         return NULL;
390     iterator = engine_list_head;
391     while (iterator && (strcmp(id, iterator->id) != 0))
392         iterator = iterator->next;
393     if (iterator != NULL) {
394         /*
395          * We need to return a structural reference. If this is an ENGINE
396          * type that returns copies, make a duplicate - otherwise increment
397          * the existing ENGINE's reference count.
398          */
399         if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
400             ENGINE *cp = ENGINE_new();
401             if (cp == NULL)
402                 iterator = NULL;
403             else {
404                 engine_cpy(cp, iterator);
405                 iterator = cp;
406             }
407         } else {
408             iterator->struct_ref++;
409             ENGINE_REF_PRINT(iterator, 0, 1);
410         }
411     }
412     CRYPTO_THREAD_unlock(global_engine_lock);
413     if (iterator != NULL)
414         return iterator;
415     /*
416      * Prevent infinite recursion if we're looking for the dynamic engine.
417      */
418     if (strcmp(id, "dynamic")) {
419         if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL)
420             load_dir = ENGINESDIR;
421         iterator = ENGINE_by_id("dynamic");
422         if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
423             !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
424             !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
425                                     load_dir, 0) ||
426             !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
427             !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
428             goto notfound;
429         return iterator;
430     }
431  notfound:
432     ENGINE_free(iterator);
433     ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_NO_SUCH_ENGINE, "id=%s", id);
434     return NULL;
435     /* EEK! Experimental code ends */
436 }
437
438 int ENGINE_up_ref(ENGINE *e)
439 {
440     int i;
441     if (e == NULL) {
442         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
443         return 0;
444     }
445     CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);
446     return 1;
447 }