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