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 | {
|
---|
23 | global_engine_lock = CRYPTO_THREAD_lock_new();
|
---|
24 | return global_engine_lock != NULL;
|
---|
25 | }
|
---|
26 |
|
---|
27 | ENGINE *ENGINE_new(void)
|
---|
28 | {
|
---|
29 | ENGINE *ret;
|
---|
30 |
|
---|
31 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
|
---|
32 | /* Maybe this should be raised in do_engine_lock_init() */
|
---|
33 | ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
|
---|
34 | return 0;
|
---|
35 | }
|
---|
36 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
|
---|
37 | return NULL;
|
---|
38 | if (!CRYPTO_NEW_REF(&ret->struct_ref, 1)) {
|
---|
39 | OPENSSL_free(ret);
|
---|
40 | return NULL;
|
---|
41 | }
|
---|
42 | ENGINE_REF_PRINT(ret, 0, 1);
|
---|
43 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data)) {
|
---|
44 | CRYPTO_FREE_REF(&ret->struct_ref);
|
---|
45 | OPENSSL_free(ret);
|
---|
46 | return NULL;
|
---|
47 | }
|
---|
48 | return ret;
|
---|
49 | }
|
---|
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 | {
|
---|
58 | e->id = NULL;
|
---|
59 | e->name = NULL;
|
---|
60 | e->rsa_meth = NULL;
|
---|
61 | e->dsa_meth = NULL;
|
---|
62 | e->dh_meth = NULL;
|
---|
63 | e->rand_meth = NULL;
|
---|
64 | e->ciphers = NULL;
|
---|
65 | e->digests = NULL;
|
---|
66 | e->destroy = NULL;
|
---|
67 | e->init = NULL;
|
---|
68 | e->finish = NULL;
|
---|
69 | e->ctrl = NULL;
|
---|
70 | e->load_privkey = NULL;
|
---|
71 | e->load_pubkey = NULL;
|
---|
72 | e->cmd_defns = NULL;
|
---|
73 | e->flags = 0;
|
---|
74 | e->dynamic_id = NULL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | int engine_free_util(ENGINE *e, int not_locked)
|
---|
78 | {
|
---|
79 | int i;
|
---|
80 |
|
---|
81 | if (e == NULL)
|
---|
82 | return 1;
|
---|
83 | CRYPTO_DOWN_REF(&e->struct_ref, &i);
|
---|
84 | ENGINE_REF_PRINT(e, 0, -1);
|
---|
85 | if (i > 0)
|
---|
86 | return 1;
|
---|
87 | REF_ASSERT_ISNT(i < 0);
|
---|
88 | /* Free up any dynamically allocated public key methods */
|
---|
89 | engine_pkey_meths_free(e);
|
---|
90 | 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 | if (e->destroy)
|
---|
96 | e->destroy(e);
|
---|
97 | engine_remove_dynamic_id(e, not_locked);
|
---|
98 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);
|
---|
99 | CRYPTO_FREE_REF(&e->struct_ref);
|
---|
100 | OPENSSL_free(e);
|
---|
101 | return 1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | int ENGINE_free(ENGINE *e)
|
---|
105 | {
|
---|
106 | return engine_free_util(e, 1);
|
---|
107 | }
|
---|
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 | {
|
---|
120 | if (cleanup_stack)
|
---|
121 | return 1;
|
---|
122 | if (!create)
|
---|
123 | return 0;
|
---|
124 | cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null();
|
---|
125 | return (cleanup_stack ? 1 : 0);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb)
|
---|
129 | {
|
---|
130 | ENGINE_CLEANUP_ITEM *item;
|
---|
131 |
|
---|
132 | if ((item = OPENSSL_malloc(sizeof(*item))) == NULL)
|
---|
133 | return NULL;
|
---|
134 | item->cb = cb;
|
---|
135 | return item;
|
---|
136 | }
|
---|
137 |
|
---|
138 | int engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb)
|
---|
139 | {
|
---|
140 | ENGINE_CLEANUP_ITEM *item;
|
---|
141 |
|
---|
142 | if (!int_cleanup_check(1))
|
---|
143 | return 0;
|
---|
144 | item = int_cleanup_item(cb);
|
---|
145 | if (item != NULL) {
|
---|
146 | if (sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0))
|
---|
147 | return 1;
|
---|
148 | OPENSSL_free(item);
|
---|
149 | }
|
---|
150 | return 0;
|
---|
151 | }
|
---|
152 |
|
---|
153 | int engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb)
|
---|
154 | {
|
---|
155 | ENGINE_CLEANUP_ITEM *item;
|
---|
156 |
|
---|
157 | if (!int_cleanup_check(1))
|
---|
158 | return 0;
|
---|
159 | item = int_cleanup_item(cb);
|
---|
160 | if (item != NULL) {
|
---|
161 | if (sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item) > 0)
|
---|
162 | return 1;
|
---|
163 | OPENSSL_free(item);
|
---|
164 | }
|
---|
165 | return 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* The API function that performs all cleanup */
|
---|
169 | static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item)
|
---|
170 | {
|
---|
171 | (*(item->cb)) ();
|
---|
172 | OPENSSL_free(item);
|
---|
173 | }
|
---|
174 |
|
---|
175 | void engine_cleanup_int(void)
|
---|
176 | {
|
---|
177 | if (int_cleanup_check(0)) {
|
---|
178 | sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack,
|
---|
179 | engine_cleanup_cb_free);
|
---|
180 | cleanup_stack = NULL;
|
---|
181 | }
|
---|
182 | CRYPTO_THREAD_lock_free(global_engine_lock);
|
---|
183 | global_engine_lock = NULL;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Now the "ex_data" support */
|
---|
187 |
|
---|
188 | int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
|
---|
189 | {
|
---|
190 | return CRYPTO_set_ex_data(&e->ex_data, idx, arg);
|
---|
191 | }
|
---|
192 |
|
---|
193 | void *ENGINE_get_ex_data(const ENGINE *e, int idx)
|
---|
194 | {
|
---|
195 | return CRYPTO_get_ex_data(&e->ex_data, idx);
|
---|
196 | }
|
---|
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 | {
|
---|
205 | if (id == NULL) {
|
---|
206 | ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
|
---|
207 | return 0;
|
---|
208 | }
|
---|
209 | e->id = id;
|
---|
210 | return 1;
|
---|
211 | }
|
---|
212 |
|
---|
213 | int ENGINE_set_name(ENGINE *e, const char *name)
|
---|
214 | {
|
---|
215 | if (name == NULL) {
|
---|
216 | ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
|
---|
217 | return 0;
|
---|
218 | }
|
---|
219 | e->name = name;
|
---|
220 | return 1;
|
---|
221 | }
|
---|
222 |
|
---|
223 | int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f)
|
---|
224 | {
|
---|
225 | e->destroy = destroy_f;
|
---|
226 | return 1;
|
---|
227 | }
|
---|
228 |
|
---|
229 | int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
|
---|
230 | {
|
---|
231 | e->init = init_f;
|
---|
232 | return 1;
|
---|
233 | }
|
---|
234 |
|
---|
235 | int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
|
---|
236 | {
|
---|
237 | e->finish = finish_f;
|
---|
238 | return 1;
|
---|
239 | }
|
---|
240 |
|
---|
241 | int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
|
---|
242 | {
|
---|
243 | e->ctrl = ctrl_f;
|
---|
244 | return 1;
|
---|
245 | }
|
---|
246 |
|
---|
247 | int ENGINE_set_flags(ENGINE *e, int flags)
|
---|
248 | {
|
---|
249 | e->flags = flags;
|
---|
250 | return 1;
|
---|
251 | }
|
---|
252 |
|
---|
253 | int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
|
---|
254 | {
|
---|
255 | e->cmd_defns = defns;
|
---|
256 | return 1;
|
---|
257 | }
|
---|
258 |
|
---|
259 | const char *ENGINE_get_id(const ENGINE *e)
|
---|
260 | {
|
---|
261 | return e->id;
|
---|
262 | }
|
---|
263 |
|
---|
264 | const char *ENGINE_get_name(const ENGINE *e)
|
---|
265 | {
|
---|
266 | return e->name;
|
---|
267 | }
|
---|
268 |
|
---|
269 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e)
|
---|
270 | {
|
---|
271 | return e->destroy;
|
---|
272 | }
|
---|
273 |
|
---|
274 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
|
---|
275 | {
|
---|
276 | return e->init;
|
---|
277 | }
|
---|
278 |
|
---|
279 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
|
---|
280 | {
|
---|
281 | return e->finish;
|
---|
282 | }
|
---|
283 |
|
---|
284 | ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
|
---|
285 | {
|
---|
286 | return e->ctrl;
|
---|
287 | }
|
---|
288 |
|
---|
289 | int ENGINE_get_flags(const ENGINE *e)
|
---|
290 | {
|
---|
291 | return e->flags;
|
---|
292 | }
|
---|
293 |
|
---|
294 | const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
|
---|
295 | {
|
---|
296 | return e->cmd_defns;
|
---|
297 | }
|
---|
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 | {
|
---|
308 | return &internal_static_hack;
|
---|
309 | }
|
---|