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