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