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