Fix memory overrun in rsa padding check functions
[oweals/openssl.git] / crypto / engine / eng_lib.c
1 /*
2  * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "eng_int.h"
11 #include <openssl/rand.h>
12
13 CRYPTO_RWLOCK *global_engine_lock;
14
15 CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT;
16
17 /* The "new"/"free" stuff first */
18
19 DEFINE_RUN_ONCE(do_engine_lock_init)
20 {
21     if (!OPENSSL_init_crypto(0, NULL))
22         return 0;
23     global_engine_lock = CRYPTO_THREAD_lock_new();
24     return global_engine_lock != NULL;
25 }
26
27 ENGINE *ENGINE_new(void)
28 {
29     ENGINE *ret;
30
31     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)
32         || (ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
33         ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
34         return NULL;
35     }
36     ret->struct_ref = 1;
37     engine_ref_debug(ret, 0, 1);
38     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data)) {
39         OPENSSL_free(ret);
40         return NULL;
41     }
42     return ret;
43 }
44
45 /*
46  * Placed here (close proximity to ENGINE_new) so that modifications to the
47  * elements of the ENGINE structure are more likely to be caught and changed
48  * here.
49  */
50 void engine_set_all_null(ENGINE *e)
51 {
52     e->id = NULL;
53     e->name = NULL;
54     e->rsa_meth = NULL;
55     e->dsa_meth = NULL;
56     e->dh_meth = NULL;
57     e->rand_meth = NULL;
58     e->ciphers = NULL;
59     e->digests = NULL;
60     e->destroy = NULL;
61     e->init = NULL;
62     e->finish = NULL;
63     e->ctrl = NULL;
64     e->load_privkey = NULL;
65     e->load_pubkey = NULL;
66     e->cmd_defns = NULL;
67     e->flags = 0;
68 }
69
70 int engine_free_util(ENGINE *e, int locked)
71 {
72     int i;
73
74     if (e == NULL)
75         return 1;
76     if (locked)
77         CRYPTO_atomic_add(&e->struct_ref, -1, &i, global_engine_lock);
78     else
79         i = --e->struct_ref;
80     engine_ref_debug(e, 0, -1)
81     if (i > 0)
82         return 1;
83     REF_ASSERT_ISNT(i < 0);
84     /* Free up any dynamically allocated public key methods */
85     engine_pkey_meths_free(e);
86     engine_pkey_asn1_meths_free(e);
87     /*
88      * Give the ENGINE a chance to do any structural cleanup corresponding to
89      * allocation it did in its constructor (eg. unload error strings)
90      */
91     if (e->destroy)
92         e->destroy(e);
93     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);
94     OPENSSL_free(e);
95     return 1;
96 }
97
98 int ENGINE_free(ENGINE *e)
99 {
100     return engine_free_util(e, 1);
101 }
102
103 /* Cleanup stuff */
104
105 /*
106  * engine_cleanup_int() is coded such that anything that does work that will
107  * need cleanup can register a "cleanup" callback here. That way we don't get
108  * linker bloat by referring to all *possible* cleanups, but any linker bloat
109  * into code "X" will cause X's cleanup function to end up here.
110  */
111 static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL;
112 static int int_cleanup_check(int create)
113 {
114     if (cleanup_stack)
115         return 1;
116     if (!create)
117         return 0;
118     cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null();
119     return (cleanup_stack ? 1 : 0);
120 }
121
122 static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb)
123 {
124     ENGINE_CLEANUP_ITEM *item = OPENSSL_malloc(sizeof(*item));
125     if (item == NULL)
126         return NULL;
127     item->cb = cb;
128     return item;
129 }
130
131 void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb)
132 {
133     ENGINE_CLEANUP_ITEM *item;
134     if (!int_cleanup_check(1))
135         return;
136     item = int_cleanup_item(cb);
137     if (item)
138         sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0);
139 }
140
141 void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb)
142 {
143     ENGINE_CLEANUP_ITEM *item;
144     if (!int_cleanup_check(1))
145         return;
146     item = int_cleanup_item(cb);
147     if (item != NULL) {
148         if (sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item) <= 0)
149             OPENSSL_free(item);
150     }
151 }
152
153 /* The API function that performs all cleanup */
154 static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item)
155 {
156     (*(item->cb)) ();
157     OPENSSL_free(item);
158 }
159
160 void engine_cleanup_int(void)
161 {
162     if (int_cleanup_check(0)) {
163         sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack,
164                                         engine_cleanup_cb_free);
165         cleanup_stack = NULL;
166     }
167     CRYPTO_THREAD_lock_free(global_engine_lock);
168 }
169
170 /* Now the "ex_data" support */
171
172 int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
173 {
174     return (CRYPTO_set_ex_data(&e->ex_data, idx, arg));
175 }
176
177 void *ENGINE_get_ex_data(const ENGINE *e, int idx)
178 {
179     return (CRYPTO_get_ex_data(&e->ex_data, idx));
180 }
181
182 /*
183  * Functions to get/set an ENGINE's elements - mainly to avoid exposing the
184  * ENGINE structure itself.
185  */
186
187 int ENGINE_set_id(ENGINE *e, const char *id)
188 {
189     if (id == NULL) {
190         ENGINEerr(ENGINE_F_ENGINE_SET_ID, ERR_R_PASSED_NULL_PARAMETER);
191         return 0;
192     }
193     e->id = id;
194     return 1;
195 }
196
197 int ENGINE_set_name(ENGINE *e, const char *name)
198 {
199     if (name == NULL) {
200         ENGINEerr(ENGINE_F_ENGINE_SET_NAME, ERR_R_PASSED_NULL_PARAMETER);
201         return 0;
202     }
203     e->name = name;
204     return 1;
205 }
206
207 int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f)
208 {
209     e->destroy = destroy_f;
210     return 1;
211 }
212
213 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
214 {
215     e->init = init_f;
216     return 1;
217 }
218
219 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
220 {
221     e->finish = finish_f;
222     return 1;
223 }
224
225 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
226 {
227     e->ctrl = ctrl_f;
228     return 1;
229 }
230
231 int ENGINE_set_flags(ENGINE *e, int flags)
232 {
233     e->flags = flags;
234     return 1;
235 }
236
237 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
238 {
239     e->cmd_defns = defns;
240     return 1;
241 }
242
243 const char *ENGINE_get_id(const ENGINE *e)
244 {
245     return e->id;
246 }
247
248 const char *ENGINE_get_name(const ENGINE *e)
249 {
250     return e->name;
251 }
252
253 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e)
254 {
255     return e->destroy;
256 }
257
258 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
259 {
260     return e->init;
261 }
262
263 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
264 {
265     return e->finish;
266 }
267
268 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
269 {
270     return e->ctrl;
271 }
272
273 int ENGINE_get_flags(const ENGINE *e)
274 {
275     return e->flags;
276 }
277
278 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
279 {
280     return e->cmd_defns;
281 }
282
283 /*
284  * eng_lib.o is pretty much linked into anything that touches ENGINE already,
285  * so put the "static_state" hack here.
286  */
287
288 static int internal_static_hack = 0;
289
290 void *ENGINE_get_static_state(void)
291 {
292     return &internal_static_hack;
293 }