]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
3c2bdd7d | 2 | * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved. |
14cfde9c | 3 | * |
3c120f91 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
14cfde9c UM |
8 | */ |
9 | ||
677963e5 | 10 | #include "e_os.h" |
706457b7 | 11 | #include "eng_local.h" |
60a938c6 | 12 | #include <openssl/rand.h> |
2f545ae4 | 13 | #include "internal/refcount.h" |
14cfde9c | 14 | |
40e068d5 MC |
15 | CRYPTO_RWLOCK *global_engine_lock; |
16 | ||
17 | CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT; | |
18 | ||
b6d1e52d | 19 | /* The "new"/"free" stuff first */ |
14cfde9c | 20 | |
c2e4e5d2 | 21 | DEFINE_RUN_ONCE(do_engine_lock_init) |
40e068d5 | 22 | { |
63ab5ea1 | 23 | global_engine_lock = CRYPTO_THREAD_lock_new(); |
c2e4e5d2 | 24 | return global_engine_lock != NULL; |
40e068d5 MC |
25 | } |
26 | ||
b6d1e52d | 27 | ENGINE *ENGINE_new(void) |
0f113f3e MC |
28 | { |
29 | ENGINE *ret; | |
30 | ||
c2e4e5d2 RL |
31 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init) |
32 | || (ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { | |
9311d0c4 | 33 | ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
34 | return NULL; |
35 | } | |
0f113f3e | 36 | ret->struct_ref = 1; |
43d6702d | 37 | engine_ref_debug(ret, 0, 1); |
25a807bc F |
38 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data)) { |
39 | OPENSSL_free(ret); | |
40 | return NULL; | |
41 | } | |
0f113f3e MC |
42 | return ret; |
43 | } | |
44 | ||
45 | /* | |
46 | * Placed here (close proximity to ENGINE_new) so that modifications to the | |
e4a6cf42 | 47 | * elements of the ENGINE structure are more likely to be caught and changed |
0f113f3e MC |
48 | * here. |
49 | */ | |
e4a6cf42 | 50 | void engine_set_all_null(ENGINE *e) |
0f113f3e MC |
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; | |
0f113f3e MC |
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 | } | |
e4a6cf42 | 69 | |
0a3dce82 | 70 | int engine_free_util(ENGINE *e, int not_locked) |
0f113f3e MC |
71 | { |
72 | int i; | |
73 | ||
efa7dd64 RS |
74 | if (e == NULL) |
75 | return 1; | |
0a3dce82 | 76 | if (not_locked) |
e519d6b5 | 77 | CRYPTO_DOWN_REF(&e->struct_ref, &i, global_engine_lock); |
0f113f3e MC |
78 | else |
79 | i = --e->struct_ref; | |
92b1b9a8 | 80 | engine_ref_debug(e, 0, -1); |
efa7dd64 | 81 | if (i > 0) |
0f113f3e | 82 | return 1; |
f3f1cf84 | 83 | REF_ASSERT_ISNT(i < 0); |
0f113f3e MC |
84 | /* Free up any dynamically allocated public key methods */ |
85 | engine_pkey_meths_free(e); | |
86 | engine_pkey_asn1_meths_free(e); | |
87 | /* | |
88 | * Give the ENGINE a chance to do any structural cleanup corresponding to | |
89 | * allocation it did in its constructor (eg. unload error strings) | |
90 | */ | |
91 | if (e->destroy) | |
92 | e->destroy(e); | |
93 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data); | |
94 | OPENSSL_free(e); | |
95 | return 1; | |
96 | } | |
14cfde9c | 97 | |
b6d1e52d | 98 | int ENGINE_free(ENGINE *e) |
0f113f3e MC |
99 | { |
100 | return engine_free_util(e, 1); | |
101 | } | |
14cfde9c | 102 | |
b6d1e52d | 103 | /* Cleanup stuff */ |
14cfde9c | 104 | |
0f113f3e | 105 | /* |
b3599dbb | 106 | * engine_cleanup_int() is coded such that anything that does work that will |
6d4fb1d5 | 107 | * need cleanup can register a "cleanup" callback here. That way we don't get |
0f113f3e MC |
108 | * linker bloat by referring to all *possible* cleanups, but any linker bloat |
109 | * into code "X" will cause X's cleanup function to end up here. | |
110 | */ | |
5c32657c | 111 | static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL; |
b6d1e52d | 112 | static int int_cleanup_check(int create) |
0f113f3e MC |
113 | { |
114 | if (cleanup_stack) | |
115 | return 1; | |
116 | if (!create) | |
117 | return 0; | |
118 | cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null(); | |
119 | return (cleanup_stack ? 1 : 0); | |
120 | } | |
121 | ||
5c32657c | 122 | static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb) |
0f113f3e | 123 | { |
cdb10bae | 124 | ENGINE_CLEANUP_ITEM *item; |
df443918 | 125 | |
cdb10bae | 126 | if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) { |
9311d0c4 | 127 | ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE); |
0f113f3e | 128 | return NULL; |
cdb10bae | 129 | } |
0f113f3e MC |
130 | item->cb = cb; |
131 | return item; | |
132 | } | |
133 | ||
b6d1e52d | 134 | void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb) |
0f113f3e MC |
135 | { |
136 | ENGINE_CLEANUP_ITEM *item; | |
cdb10bae | 137 | |
0f113f3e MC |
138 | if (!int_cleanup_check(1)) |
139 | return; | |
140 | item = int_cleanup_item(cb); | |
141 | if (item) | |
142 | sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0); | |
143 | } | |
144 | ||
b6d1e52d | 145 | void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb) |
0f113f3e MC |
146 | { |
147 | ENGINE_CLEANUP_ITEM *item; | |
148 | if (!int_cleanup_check(1)) | |
149 | return; | |
150 | item = int_cleanup_item(cb); | |
aebd0e5c PK |
151 | if (item != NULL) { |
152 | if (sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item) <= 0) | |
153 | OPENSSL_free(item); | |
154 | } | |
0f113f3e MC |
155 | } |
156 | ||
b6d1e52d | 157 | /* The API function that performs all cleanup */ |
1cf9d58c | 158 | static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item) |
0f113f3e MC |
159 | { |
160 | (*(item->cb)) (); | |
161 | OPENSSL_free(item); | |
162 | } | |
163 | ||
b3599dbb | 164 | void engine_cleanup_int(void) |
0f113f3e MC |
165 | { |
166 | if (int_cleanup_check(0)) { | |
167 | sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack, | |
168 | engine_cleanup_cb_free); | |
169 | cleanup_stack = NULL; | |
170 | } | |
40e068d5 | 171 | CRYPTO_THREAD_lock_free(global_engine_lock); |
e12813d0 | 172 | global_engine_lock = NULL; |
0f113f3e | 173 | } |
14cfde9c | 174 | |
b6d1e52d GT |
175 | /* Now the "ex_data" support */ |
176 | ||
b6d1e52d | 177 | int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg) |
0f113f3e | 178 | { |
26a7d938 | 179 | return CRYPTO_set_ex_data(&e->ex_data, idx, arg); |
0f113f3e | 180 | } |
14cfde9c | 181 | |
b6d1e52d | 182 | void *ENGINE_get_ex_data(const ENGINE *e, int idx) |
0f113f3e | 183 | { |
26a7d938 | 184 | return CRYPTO_get_ex_data(&e->ex_data, idx); |
0f113f3e | 185 | } |
14cfde9c | 186 | |
0f113f3e MC |
187 | /* |
188 | * Functions to get/set an ENGINE's elements - mainly to avoid exposing the | |
189 | * ENGINE structure itself. | |
190 | */ | |
14cfde9c | 191 | |
b6d1e52d | 192 | int ENGINE_set_id(ENGINE *e, const char *id) |
0f113f3e MC |
193 | { |
194 | if (id == NULL) { | |
9311d0c4 | 195 | ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
196 | return 0; |
197 | } | |
198 | e->id = id; | |
199 | return 1; | |
200 | } | |
14cfde9c | 201 | |
b6d1e52d | 202 | int ENGINE_set_name(ENGINE *e, const char *name) |
0f113f3e MC |
203 | { |
204 | if (name == NULL) { | |
9311d0c4 | 205 | ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
206 | return 0; |
207 | } | |
208 | e->name = name; | |
209 | return 1; | |
210 | } | |
14cfde9c | 211 | |
b6d1e52d | 212 | int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f) |
0f113f3e MC |
213 | { |
214 | e->destroy = destroy_f; | |
215 | return 1; | |
216 | } | |
14cfde9c | 217 | |
b6d1e52d | 218 | int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f) |
0f113f3e MC |
219 | { |
220 | e->init = init_f; | |
221 | return 1; | |
222 | } | |
14cfde9c | 223 | |
b6d1e52d | 224 | int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f) |
0f113f3e MC |
225 | { |
226 | e->finish = finish_f; | |
227 | return 1; | |
228 | } | |
14cfde9c | 229 | |
b6d1e52d | 230 | int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f) |
0f113f3e MC |
231 | { |
232 | e->ctrl = ctrl_f; | |
233 | return 1; | |
234 | } | |
14cfde9c | 235 | |
b6d1e52d | 236 | int ENGINE_set_flags(ENGINE *e, int flags) |
0f113f3e MC |
237 | { |
238 | e->flags = flags; | |
239 | return 1; | |
240 | } | |
14cfde9c | 241 | |
b6d1e52d | 242 | int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns) |
0f113f3e MC |
243 | { |
244 | e->cmd_defns = defns; | |
245 | return 1; | |
246 | } | |
14cfde9c | 247 | |
b6d1e52d | 248 | const char *ENGINE_get_id(const ENGINE *e) |
0f113f3e MC |
249 | { |
250 | return e->id; | |
251 | } | |
14cfde9c | 252 | |
b6d1e52d | 253 | const char *ENGINE_get_name(const ENGINE *e) |
0f113f3e MC |
254 | { |
255 | return e->name; | |
256 | } | |
14cfde9c | 257 | |
b6d1e52d | 258 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e) |
0f113f3e MC |
259 | { |
260 | return e->destroy; | |
261 | } | |
14cfde9c | 262 | |
b6d1e52d | 263 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e) |
0f113f3e MC |
264 | { |
265 | return e->init; | |
266 | } | |
14cfde9c | 267 | |
b6d1e52d | 268 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e) |
0f113f3e MC |
269 | { |
270 | return e->finish; | |
271 | } | |
14cfde9c | 272 | |
b6d1e52d | 273 | ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e) |
0f113f3e MC |
274 | { |
275 | return e->ctrl; | |
276 | } | |
14cfde9c | 277 | |
b6d1e52d | 278 | int ENGINE_get_flags(const ENGINE *e) |
0f113f3e MC |
279 | { |
280 | return e->flags; | |
281 | } | |
14cfde9c | 282 | |
b6d1e52d | 283 | const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e) |
0f113f3e MC |
284 | { |
285 | return e->cmd_defns; | |
286 | } | |
0587ec26 | 287 | |
0f113f3e MC |
288 | /* |
289 | * eng_lib.o is pretty much linked into anything that touches ENGINE already, | |
290 | * so put the "static_state" hack here. | |
291 | */ | |
0587ec26 GT |
292 | |
293 | static int internal_static_hack = 0; | |
294 | ||
295 | void *ENGINE_get_static_state(void) | |
0f113f3e MC |
296 | { |
297 | return &internal_static_hack; | |
298 | } |