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