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