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