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