1109998Smarkm/* crypto/engine/eng_lib.c */
2296341Sdelphij/*
3296341Sdelphij * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4296341Sdelphij * 2000.
5109998Smarkm */
6109998Smarkm/* ====================================================================
7109998Smarkm * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
8109998Smarkm *
9109998Smarkm * Redistribution and use in source and binary forms, with or without
10109998Smarkm * modification, are permitted provided that the following conditions
11109998Smarkm * are met:
12109998Smarkm *
13109998Smarkm * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
15109998Smarkm *
16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
17109998Smarkm *    notice, this list of conditions and the following disclaimer in
18109998Smarkm *    the documentation and/or other materials provided with the
19109998Smarkm *    distribution.
20109998Smarkm *
21109998Smarkm * 3. All advertising materials mentioning features or use of this
22109998Smarkm *    software must display the following acknowledgment:
23109998Smarkm *    "This product includes software developed by the OpenSSL Project
24109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25109998Smarkm *
26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27109998Smarkm *    endorse or promote products derived from this software without
28109998Smarkm *    prior written permission. For written permission, please contact
29109998Smarkm *    licensing@OpenSSL.org.
30109998Smarkm *
31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
32109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
33109998Smarkm *    permission of the OpenSSL Project.
34109998Smarkm *
35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
36109998Smarkm *    acknowledgment:
37109998Smarkm *    "This product includes software developed by the OpenSSL Project
38109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39109998Smarkm *
40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
52109998Smarkm * ====================================================================
53109998Smarkm *
54109998Smarkm * This product includes cryptographic software written by Eric Young
55109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
56109998Smarkm * Hudson (tjh@cryptsoft.com).
57109998Smarkm *
58109998Smarkm */
59109998Smarkm
60109998Smarkm#include "eng_int.h"
61160814Ssimon#include <openssl/rand.h>
62109998Smarkm
63109998Smarkm/* The "new"/"free" stuff first */
64109998Smarkm
65109998SmarkmENGINE *ENGINE_new(void)
66296341Sdelphij{
67296341Sdelphij    ENGINE *ret;
68109998Smarkm
69296341Sdelphij    ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
70296341Sdelphij    if (ret == NULL) {
71296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
72296341Sdelphij        return NULL;
73296341Sdelphij    }
74296341Sdelphij    memset(ret, 0, sizeof(ENGINE));
75296341Sdelphij    ret->struct_ref = 1;
76296341Sdelphij    engine_ref_debug(ret, 0, 1)
77296341Sdelphij        CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data);
78296341Sdelphij    return ret;
79296341Sdelphij}
80109998Smarkm
81296341Sdelphij/*
82296341Sdelphij * Placed here (close proximity to ENGINE_new) so that modifications to the
83109998Smarkm * elements of the ENGINE structure are more likely to be caught and changed
84296341Sdelphij * here.
85296341Sdelphij */
86109998Smarkmvoid engine_set_all_null(ENGINE *e)
87296341Sdelphij{
88296341Sdelphij    e->id = NULL;
89296341Sdelphij    e->name = NULL;
90296341Sdelphij    e->rsa_meth = NULL;
91296341Sdelphij    e->dsa_meth = NULL;
92296341Sdelphij    e->dh_meth = NULL;
93296341Sdelphij    e->rand_meth = NULL;
94296341Sdelphij    e->store_meth = NULL;
95296341Sdelphij    e->ciphers = NULL;
96296341Sdelphij    e->digests = NULL;
97296341Sdelphij    e->destroy = NULL;
98296341Sdelphij    e->init = NULL;
99296341Sdelphij    e->finish = NULL;
100296341Sdelphij    e->ctrl = NULL;
101296341Sdelphij    e->load_privkey = NULL;
102296341Sdelphij    e->load_pubkey = NULL;
103296341Sdelphij    e->cmd_defns = NULL;
104296341Sdelphij    e->flags = 0;
105296341Sdelphij}
106109998Smarkm
107109998Smarkmint engine_free_util(ENGINE *e, int locked)
108296341Sdelphij{
109296341Sdelphij    int i;
110109998Smarkm
111296341Sdelphij    if (e == NULL) {
112296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_FREE_UTIL, ERR_R_PASSED_NULL_PARAMETER);
113296341Sdelphij        return 0;
114296341Sdelphij    }
115296341Sdelphij    if (locked)
116296341Sdelphij        i = CRYPTO_add(&e->struct_ref, -1, CRYPTO_LOCK_ENGINE);
117296341Sdelphij    else
118296341Sdelphij        i = --e->struct_ref;
119296341Sdelphij    engine_ref_debug(e, 0, -1)
120296341Sdelphij        if (i > 0)
121296341Sdelphij        return 1;
122109998Smarkm#ifdef REF_CHECK
123296341Sdelphij    if (i < 0) {
124296341Sdelphij        fprintf(stderr, "ENGINE_free, bad structural reference count\n");
125296341Sdelphij        abort();
126296341Sdelphij    }
127109998Smarkm#endif
128296341Sdelphij    /* Free up any dynamically allocated public key methods */
129296341Sdelphij    engine_pkey_meths_free(e);
130296341Sdelphij    engine_pkey_asn1_meths_free(e);
131296341Sdelphij    /*
132296341Sdelphij     * Give the ENGINE a chance to do any structural cleanup corresponding to
133296341Sdelphij     * allocation it did in its constructor (eg. unload error strings)
134296341Sdelphij     */
135296341Sdelphij    if (e->destroy)
136296341Sdelphij        e->destroy(e);
137296341Sdelphij    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);
138296341Sdelphij    OPENSSL_free(e);
139296341Sdelphij    return 1;
140296341Sdelphij}
141109998Smarkm
142109998Smarkmint ENGINE_free(ENGINE *e)
143296341Sdelphij{
144296341Sdelphij    return engine_free_util(e, 1);
145296341Sdelphij}
146109998Smarkm
147109998Smarkm/* Cleanup stuff */
148109998Smarkm
149296341Sdelphij/*
150296341Sdelphij * ENGINE_cleanup() is coded such that anything that does work that will need
151296341Sdelphij * cleanup can register a "cleanup" callback here. That way we don't get
152296341Sdelphij * linker bloat by referring to all *possible* cleanups, but any linker bloat
153296341Sdelphij * into code "X" will cause X's cleanup function to end up here.
154296341Sdelphij */
155109998Smarkmstatic STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL;
156109998Smarkmstatic int int_cleanup_check(int create)
157296341Sdelphij{
158296341Sdelphij    if (cleanup_stack)
159296341Sdelphij        return 1;
160296341Sdelphij    if (!create)
161296341Sdelphij        return 0;
162296341Sdelphij    cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null();
163296341Sdelphij    return (cleanup_stack ? 1 : 0);
164296341Sdelphij}
165296341Sdelphij
166109998Smarkmstatic ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb)
167296341Sdelphij{
168296341Sdelphij    ENGINE_CLEANUP_ITEM *item = OPENSSL_malloc(sizeof(ENGINE_CLEANUP_ITEM));
169296341Sdelphij    if (!item)
170296341Sdelphij        return NULL;
171296341Sdelphij    item->cb = cb;
172296341Sdelphij    return item;
173296341Sdelphij}
174296341Sdelphij
175109998Smarkmvoid engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb)
176296341Sdelphij{
177296341Sdelphij    ENGINE_CLEANUP_ITEM *item;
178296341Sdelphij    if (!int_cleanup_check(1))
179296341Sdelphij        return;
180296341Sdelphij    item = int_cleanup_item(cb);
181296341Sdelphij    if (item)
182296341Sdelphij        sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0);
183296341Sdelphij}
184296341Sdelphij
185109998Smarkmvoid engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb)
186296341Sdelphij{
187296341Sdelphij    ENGINE_CLEANUP_ITEM *item;
188296341Sdelphij    if (!int_cleanup_check(1))
189296341Sdelphij        return;
190296341Sdelphij    item = int_cleanup_item(cb);
191296341Sdelphij    if (item)
192296341Sdelphij        sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item);
193296341Sdelphij}
194296341Sdelphij
195109998Smarkm/* The API function that performs all cleanup */
196109998Smarkmstatic void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item)
197296341Sdelphij{
198296341Sdelphij    (*(item->cb)) ();
199296341Sdelphij    OPENSSL_free(item);
200296341Sdelphij}
201296341Sdelphij
202109998Smarkmvoid ENGINE_cleanup(void)
203296341Sdelphij{
204296341Sdelphij    if (int_cleanup_check(0)) {
205296341Sdelphij        sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack,
206296341Sdelphij                                        engine_cleanup_cb_free);
207296341Sdelphij        cleanup_stack = NULL;
208296341Sdelphij    }
209296341Sdelphij    /*
210296341Sdelphij     * FIXME: This should be handled (somehow) through RAND, eg. by it
211296341Sdelphij     * registering a cleanup callback.
212296341Sdelphij     */
213296341Sdelphij    RAND_set_rand_method(NULL);
214296341Sdelphij}
215109998Smarkm
216109998Smarkm/* Now the "ex_data" support */
217109998Smarkm
218109998Smarkmint ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
219296341Sdelphij                            CRYPTO_EX_dup *dup_func,
220296341Sdelphij                            CRYPTO_EX_free *free_func)
221296341Sdelphij{
222296341Sdelphij    return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp,
223296341Sdelphij                                   new_func, dup_func, free_func);
224296341Sdelphij}
225109998Smarkm
226109998Smarkmint ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
227296341Sdelphij{
228296341Sdelphij    return (CRYPTO_set_ex_data(&e->ex_data, idx, arg));
229296341Sdelphij}
230109998Smarkm
231109998Smarkmvoid *ENGINE_get_ex_data(const ENGINE *e, int idx)
232296341Sdelphij{
233296341Sdelphij    return (CRYPTO_get_ex_data(&e->ex_data, idx));
234296341Sdelphij}
235109998Smarkm
236296341Sdelphij/*
237296341Sdelphij * Functions to get/set an ENGINE's elements - mainly to avoid exposing the
238296341Sdelphij * ENGINE structure itself.
239296341Sdelphij */
240109998Smarkm
241109998Smarkmint ENGINE_set_id(ENGINE *e, const char *id)
242296341Sdelphij{
243296341Sdelphij    if (id == NULL) {
244296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_SET_ID, ERR_R_PASSED_NULL_PARAMETER);
245296341Sdelphij        return 0;
246296341Sdelphij    }
247296341Sdelphij    e->id = id;
248296341Sdelphij    return 1;
249296341Sdelphij}
250109998Smarkm
251109998Smarkmint ENGINE_set_name(ENGINE *e, const char *name)
252296341Sdelphij{
253296341Sdelphij    if (name == NULL) {
254296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_SET_NAME, ERR_R_PASSED_NULL_PARAMETER);
255296341Sdelphij        return 0;
256296341Sdelphij    }
257296341Sdelphij    e->name = name;
258296341Sdelphij    return 1;
259296341Sdelphij}
260109998Smarkm
261109998Smarkmint ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f)
262296341Sdelphij{
263296341Sdelphij    e->destroy = destroy_f;
264296341Sdelphij    return 1;
265296341Sdelphij}
266109998Smarkm
267109998Smarkmint ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
268296341Sdelphij{
269296341Sdelphij    e->init = init_f;
270296341Sdelphij    return 1;
271296341Sdelphij}
272109998Smarkm
273109998Smarkmint ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
274296341Sdelphij{
275296341Sdelphij    e->finish = finish_f;
276296341Sdelphij    return 1;
277296341Sdelphij}
278109998Smarkm
279109998Smarkmint ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
280296341Sdelphij{
281296341Sdelphij    e->ctrl = ctrl_f;
282296341Sdelphij    return 1;
283296341Sdelphij}
284109998Smarkm
285109998Smarkmint ENGINE_set_flags(ENGINE *e, int flags)
286296341Sdelphij{
287296341Sdelphij    e->flags = flags;
288296341Sdelphij    return 1;
289296341Sdelphij}
290109998Smarkm
291109998Smarkmint ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
292296341Sdelphij{
293296341Sdelphij    e->cmd_defns = defns;
294296341Sdelphij    return 1;
295296341Sdelphij}
296109998Smarkm
297109998Smarkmconst char *ENGINE_get_id(const ENGINE *e)
298296341Sdelphij{
299296341Sdelphij    return e->id;
300296341Sdelphij}
301109998Smarkm
302109998Smarkmconst char *ENGINE_get_name(const ENGINE *e)
303296341Sdelphij{
304296341Sdelphij    return e->name;
305296341Sdelphij}
306109998Smarkm
307109998SmarkmENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e)
308296341Sdelphij{
309296341Sdelphij    return e->destroy;
310296341Sdelphij}
311109998Smarkm
312109998SmarkmENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
313296341Sdelphij{
314296341Sdelphij    return e->init;
315296341Sdelphij}
316109998Smarkm
317109998SmarkmENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
318296341Sdelphij{
319296341Sdelphij    return e->finish;
320296341Sdelphij}
321109998Smarkm
322109998SmarkmENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
323296341Sdelphij{
324296341Sdelphij    return e->ctrl;
325296341Sdelphij}
326109998Smarkm
327109998Smarkmint ENGINE_get_flags(const ENGINE *e)
328296341Sdelphij{
329296341Sdelphij    return e->flags;
330296341Sdelphij}
331109998Smarkm
332109998Smarkmconst ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
333296341Sdelphij{
334296341Sdelphij    return e->cmd_defns;
335296341Sdelphij}
336160814Ssimon
337296341Sdelphij/*
338296341Sdelphij * eng_lib.o is pretty much linked into anything that touches ENGINE already,
339296341Sdelphij * so put the "static_state" hack here.
340296341Sdelphij */
341160814Ssimon
342160814Ssimonstatic int internal_static_hack = 0;
343160814Ssimon
344160814Ssimonvoid *ENGINE_get_static_state(void)
345296341Sdelphij{
346296341Sdelphij    return &internal_static_hack;
347296341Sdelphij}
348