]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
6738bf14 | 2 | * Copyright 2001-2018 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 | ||
677963e5 | 10 | #include "e_os.h" |
07016a8a | 11 | #include "eng_int.h" |
60a938c6 | 12 | #include <openssl/rand.h> |
2f545ae4 | 13 | #include "internal/refcount.h" |
14cfde9c | 14 | |
40e068d5 MC |
15 | CRYPTO_RWLOCK *global_engine_lock; |
16 | ||
17 | CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT; | |
18 | ||
b6d1e52d | 19 | /* The "new"/"free" stuff first */ |
14cfde9c | 20 | |
c2e4e5d2 | 21 | DEFINE_RUN_ONCE(do_engine_lock_init) |
40e068d5 | 22 | { |
135648bc | 23 | OPENSSL_init_crypto(0, NULL); |
63ab5ea1 | 24 | global_engine_lock = CRYPTO_THREAD_lock_new(); |
c2e4e5d2 | 25 | return global_engine_lock != NULL; |
40e068d5 MC |
26 | } |
27 | ||
b6d1e52d | 28 | ENGINE *ENGINE_new(void) |
0f113f3e MC |
29 | { |
30 | ENGINE *ret; | |
31 | ||
c2e4e5d2 RL |
32 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init) |
33 | || (ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { | |
0f113f3e MC |
34 | ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE); |
35 | return NULL; | |
36 | } | |
0f113f3e | 37 | ret->struct_ref = 1; |
43d6702d | 38 | engine_ref_debug(ret, 0, 1); |
25a807bc F |
39 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data)) { |
40 | OPENSSL_free(ret); | |
41 | return NULL; | |
42 | } | |
0f113f3e MC |
43 | return ret; |
44 | } | |
45 | ||
46 | /* | |
47 | * Placed here (close proximity to ENGINE_new) so that modifications to the | |
e4a6cf42 | 48 | * elements of the ENGINE structure are more likely to be caught and changed |
0f113f3e MC |
49 | * here. |
50 | */ | |
e4a6cf42 | 51 | void engine_set_all_null(ENGINE *e) |
0f113f3e MC |
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; | |
0f113f3e MC |
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 | } | |
e4a6cf42 | 70 | |
0a3dce82 | 71 | int engine_free_util(ENGINE *e, int not_locked) |
0f113f3e MC |
72 | { |
73 | int i; | |
74 | ||
efa7dd64 RS |
75 | if (e == NULL) |
76 | return 1; | |
2f545ae4 KR |
77 | #ifdef HAVE_ATOMICS |
78 | CRYPTO_DOWN_REF(&e->struct_ref, &i, global_engine_lock); | |
79 | #else | |
0a3dce82 | 80 | if (not_locked) |
40e068d5 | 81 | CRYPTO_atomic_add(&e->struct_ref, -1, &i, global_engine_lock); |
0f113f3e MC |
82 | else |
83 | i = --e->struct_ref; | |
2f545ae4 | 84 | #endif |
92b1b9a8 | 85 | engine_ref_debug(e, 0, -1); |
efa7dd64 | 86 | if (i > 0) |
0f113f3e | 87 | return 1; |
f3f1cf84 | 88 | REF_ASSERT_ISNT(i < 0); |
0f113f3e MC |
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 | } | |
14cfde9c | 102 | |
b6d1e52d | 103 | int ENGINE_free(ENGINE *e) |
0f113f3e MC |
104 | { |
105 | return engine_free_util(e, 1); | |
106 | } | |
14cfde9c | 107 | |
b6d1e52d | 108 | /* Cleanup stuff */ |
14cfde9c | 109 | |
0f113f3e | 110 | /* |
b3599dbb | 111 | * engine_cleanup_int() is coded such that anything that does work that will |
6d4fb1d5 | 112 | * need cleanup can register a "cleanup" callback here. That way we don't get |
0f113f3e MC |
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 | */ | |
5c32657c | 116 | static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL; |
b6d1e52d | 117 | static int int_cleanup_check(int create) |
0f113f3e MC |
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 | ||
5c32657c | 127 | static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb) |
0f113f3e | 128 | { |
b4faea50 | 129 | ENGINE_CLEANUP_ITEM *item = OPENSSL_malloc(sizeof(*item)); |
90945fa3 | 130 | if (item == NULL) |
0f113f3e MC |
131 | return NULL; |
132 | item->cb = cb; | |
133 | return item; | |
134 | } | |
135 | ||
b6d1e52d | 136 | void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb) |
0f113f3e MC |
137 | { |
138 | ENGINE_CLEANUP_ITEM *item; | |
139 | if (!int_cleanup_check(1)) | |
140 | return; | |
141 | item = int_cleanup_item(cb); | |
142 | if (item) | |
143 | sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0); | |
144 | } | |
145 | ||
b6d1e52d | 146 | void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb) |
0f113f3e MC |
147 | { |
148 | ENGINE_CLEANUP_ITEM *item; | |
149 | if (!int_cleanup_check(1)) | |
150 | return; | |
151 | item = int_cleanup_item(cb); | |
152 | if (item) | |
153 | sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item); | |
154 | } | |
155 | ||
b6d1e52d | 156 | /* The API function that performs all cleanup */ |
1cf9d58c | 157 | static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item) |
0f113f3e MC |
158 | { |
159 | (*(item->cb)) (); | |
160 | OPENSSL_free(item); | |
161 | } | |
162 | ||
b3599dbb | 163 | void engine_cleanup_int(void) |
0f113f3e MC |
164 | { |
165 | if (int_cleanup_check(0)) { | |
166 | sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack, | |
167 | engine_cleanup_cb_free); | |
168 | cleanup_stack = NULL; | |
169 | } | |
40e068d5 | 170 | CRYPTO_THREAD_lock_free(global_engine_lock); |
0f113f3e | 171 | } |
14cfde9c | 172 | |
b6d1e52d GT |
173 | /* Now the "ex_data" support */ |
174 | ||
b6d1e52d | 175 | int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg) |
0f113f3e | 176 | { |
26a7d938 | 177 | return CRYPTO_set_ex_data(&e->ex_data, idx, arg); |
0f113f3e | 178 | } |
14cfde9c | 179 | |
b6d1e52d | 180 | void *ENGINE_get_ex_data(const ENGINE *e, int idx) |
0f113f3e | 181 | { |
26a7d938 | 182 | return CRYPTO_get_ex_data(&e->ex_data, idx); |
0f113f3e | 183 | } |
14cfde9c | 184 | |
0f113f3e MC |
185 | /* |
186 | * Functions to get/set an ENGINE's elements - mainly to avoid exposing the | |
187 | * ENGINE structure itself. | |
188 | */ | |
14cfde9c | 189 | |
b6d1e52d | 190 | int ENGINE_set_id(ENGINE *e, const char *id) |
0f113f3e MC |
191 | { |
192 | if (id == NULL) { | |
193 | ENGINEerr(ENGINE_F_ENGINE_SET_ID, ERR_R_PASSED_NULL_PARAMETER); | |
194 | return 0; | |
195 | } | |
196 | e->id = id; | |
197 | return 1; | |
198 | } | |
14cfde9c | 199 | |
b6d1e52d | 200 | int ENGINE_set_name(ENGINE *e, const char *name) |
0f113f3e MC |
201 | { |
202 | if (name == NULL) { | |
203 | ENGINEerr(ENGINE_F_ENGINE_SET_NAME, ERR_R_PASSED_NULL_PARAMETER); | |
204 | return 0; | |
205 | } | |
206 | e->name = name; | |
207 | return 1; | |
208 | } | |
14cfde9c | 209 | |
b6d1e52d | 210 | int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f) |
0f113f3e MC |
211 | { |
212 | e->destroy = destroy_f; | |
213 | return 1; | |
214 | } | |
14cfde9c | 215 | |
b6d1e52d | 216 | int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f) |
0f113f3e MC |
217 | { |
218 | e->init = init_f; | |
219 | return 1; | |
220 | } | |
14cfde9c | 221 | |
b6d1e52d | 222 | int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f) |
0f113f3e MC |
223 | { |
224 | e->finish = finish_f; | |
225 | return 1; | |
226 | } | |
14cfde9c | 227 | |
b6d1e52d | 228 | int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f) |
0f113f3e MC |
229 | { |
230 | e->ctrl = ctrl_f; | |
231 | return 1; | |
232 | } | |
14cfde9c | 233 | |
b6d1e52d | 234 | int ENGINE_set_flags(ENGINE *e, int flags) |
0f113f3e MC |
235 | { |
236 | e->flags = flags; | |
237 | return 1; | |
238 | } | |
14cfde9c | 239 | |
b6d1e52d | 240 | int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns) |
0f113f3e MC |
241 | { |
242 | e->cmd_defns = defns; | |
243 | return 1; | |
244 | } | |
14cfde9c | 245 | |
b6d1e52d | 246 | const char *ENGINE_get_id(const ENGINE *e) |
0f113f3e MC |
247 | { |
248 | return e->id; | |
249 | } | |
14cfde9c | 250 | |
b6d1e52d | 251 | const char *ENGINE_get_name(const ENGINE *e) |
0f113f3e MC |
252 | { |
253 | return e->name; | |
254 | } | |
14cfde9c | 255 | |
b6d1e52d | 256 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e) |
0f113f3e MC |
257 | { |
258 | return e->destroy; | |
259 | } | |
14cfde9c | 260 | |
b6d1e52d | 261 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e) |
0f113f3e MC |
262 | { |
263 | return e->init; | |
264 | } | |
14cfde9c | 265 | |
b6d1e52d | 266 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e) |
0f113f3e MC |
267 | { |
268 | return e->finish; | |
269 | } | |
14cfde9c | 270 | |
b6d1e52d | 271 | ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e) |
0f113f3e MC |
272 | { |
273 | return e->ctrl; | |
274 | } | |
14cfde9c | 275 | |
b6d1e52d | 276 | int ENGINE_get_flags(const ENGINE *e) |
0f113f3e MC |
277 | { |
278 | return e->flags; | |
279 | } | |
14cfde9c | 280 | |
b6d1e52d | 281 | const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e) |
0f113f3e MC |
282 | { |
283 | return e->cmd_defns; | |
284 | } | |
0587ec26 | 285 | |
0f113f3e MC |
286 | /* |
287 | * eng_lib.o is pretty much linked into anything that touches ENGINE already, | |
288 | * so put the "static_state" hack here. | |
289 | */ | |
0587ec26 GT |
290 | |
291 | static int internal_static_hack = 0; | |
292 | ||
293 | void *ENGINE_get_static_state(void) | |
0f113f3e MC |
294 | { |
295 | return &internal_static_hack; | |
296 | } |