2 * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/e_os.h"
11 #include "eng_local.h"
12 #include <openssl/rand.h>
13 #include "internal/refcount.h"
15 CRYPTO_RWLOCK *global_engine_lock;
17 CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT;
19 /* The "new"/"free" stuff first */
21 DEFINE_RUN_ONCE(do_engine_lock_init)
23 global_engine_lock = CRYPTO_THREAD_lock_new();
24 return global_engine_lock != NULL;
27 ENGINE *ENGINE_new(void)
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);
36 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
38 if (!CRYPTO_NEW_REF(&ret->struct_ref, 1)) {
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);
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
56 void engine_set_all_null(ENGINE *e)
70 e->load_privkey = NULL;
71 e->load_pubkey = NULL;
77 int engine_free_util(ENGINE *e, int not_locked)
83 CRYPTO_DOWN_REF(&e->struct_ref, &i);
84 ENGINE_REF_PRINT(e, 0, -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);
92 * Give the ENGINE a chance to do any structural cleanup corresponding to
93 * allocation it did in its constructor (eg. unload error strings)
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);
104 int ENGINE_free(ENGINE *e)
106 return engine_free_util(e, 1);
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.
117 static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL;
118 static int int_cleanup_check(int create)
124 cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null();
125 return (cleanup_stack ? 1 : 0);
128 static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb)
130 ENGINE_CLEANUP_ITEM *item;
132 if ((item = OPENSSL_malloc(sizeof(*item))) == NULL)
138 int engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb)
140 ENGINE_CLEANUP_ITEM *item;
142 if (!int_cleanup_check(1))
144 item = int_cleanup_item(cb);
146 if (sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0))
153 int engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb)
155 ENGINE_CLEANUP_ITEM *item;
157 if (!int_cleanup_check(1))
159 item = int_cleanup_item(cb);
161 if (sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item) > 0)
168 /* The API function that performs all cleanup */
169 static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item)
175 void engine_cleanup_int(void)
177 if (int_cleanup_check(0)) {
178 sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack,
179 engine_cleanup_cb_free);
180 cleanup_stack = NULL;
182 CRYPTO_THREAD_lock_free(global_engine_lock);
183 global_engine_lock = NULL;
186 /* Now the "ex_data" support */
188 int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
190 return CRYPTO_set_ex_data(&e->ex_data, idx, arg);
193 void *ENGINE_get_ex_data(const ENGINE *e, int idx)
195 return CRYPTO_get_ex_data(&e->ex_data, idx);
199 * Functions to get/set an ENGINE's elements - mainly to avoid exposing the
200 * ENGINE structure itself.
203 int ENGINE_set_id(ENGINE *e, const char *id)
206 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
213 int ENGINE_set_name(ENGINE *e, const char *name)
216 ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
223 int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f)
225 e->destroy = destroy_f;
229 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
235 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
237 e->finish = finish_f;
241 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
247 int ENGINE_set_flags(ENGINE *e, int flags)
253 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
255 e->cmd_defns = defns;
259 const char *ENGINE_get_id(const ENGINE *e)
264 const char *ENGINE_get_name(const ENGINE *e)
269 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e)
274 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
279 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
284 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
289 int ENGINE_get_flags(const ENGINE *e)
294 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
300 * eng_lib.o is pretty much linked into anything that touches ENGINE already,
301 * so put the "static_state" hack here.
304 static int internal_static_hack = 0;
306 void *ENGINE_get_static_state(void)
308 return &internal_static_hack;