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