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