1109998Smarkm/* crypto/engine/eng_init.c */
2109998Smarkm/* ====================================================================
3109998Smarkm * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
4109998Smarkm *
5109998Smarkm * Redistribution and use in source and binary forms, with or without
6109998Smarkm * modification, are permitted provided that the following conditions
7109998Smarkm * are met:
8109998Smarkm *
9109998Smarkm * 1. Redistributions of source code must retain the above copyright
10280304Sjkim *    notice, this list of conditions and the following disclaimer.
11109998Smarkm *
12109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
13109998Smarkm *    notice, this list of conditions and the following disclaimer in
14109998Smarkm *    the documentation and/or other materials provided with the
15109998Smarkm *    distribution.
16109998Smarkm *
17109998Smarkm * 3. All advertising materials mentioning features or use of this
18109998Smarkm *    software must display the following acknowledgment:
19109998Smarkm *    "This product includes software developed by the OpenSSL Project
20109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21109998Smarkm *
22109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23109998Smarkm *    endorse or promote products derived from this software without
24109998Smarkm *    prior written permission. For written permission, please contact
25109998Smarkm *    licensing@OpenSSL.org.
26109998Smarkm *
27109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
28109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
29109998Smarkm *    permission of the OpenSSL Project.
30109998Smarkm *
31109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
32109998Smarkm *    acknowledgment:
33109998Smarkm *    "This product includes software developed by the OpenSSL Project
34109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35109998Smarkm *
36109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
48109998Smarkm * ====================================================================
49109998Smarkm *
50109998Smarkm * This product includes cryptographic software written by Eric Young
51109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
52109998Smarkm * Hudson (tjh@cryptsoft.com).
53109998Smarkm *
54109998Smarkm */
55109998Smarkm
56109998Smarkm#include "eng_int.h"
57109998Smarkm
58280304Sjkim/*
59280304Sjkim * Initialise a engine type for use (or up its functional reference count if
60280304Sjkim * it's already in use). This version is only used internally.
61280304Sjkim */
62109998Smarkmint engine_unlocked_init(ENGINE *e)
63280304Sjkim{
64280304Sjkim    int to_return = 1;
65109998Smarkm
66280304Sjkim    if ((e->funct_ref == 0) && e->init)
67280304Sjkim        /*
68280304Sjkim         * This is the first functional reference and the engine requires
69280304Sjkim         * initialisation so we do it now.
70280304Sjkim         */
71280304Sjkim        to_return = e->init(e);
72280304Sjkim    if (to_return) {
73280304Sjkim        /*
74280304Sjkim         * OK, we return a functional reference which is also a structural
75280304Sjkim         * reference.
76280304Sjkim         */
77280304Sjkim        e->struct_ref++;
78280304Sjkim        e->funct_ref++;
79280304Sjkim        engine_ref_debug(e, 0, 1)
80280304Sjkim            engine_ref_debug(e, 1, 1)
81280304Sjkim    }
82280304Sjkim    return to_return;
83280304Sjkim}
84109998Smarkm
85280304Sjkim/*
86280304Sjkim * Free a functional reference to a engine type. This version is only used
87280304Sjkim * internally.
88280304Sjkim */
89109998Smarkmint engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
90280304Sjkim{
91280304Sjkim    int to_return = 1;
92109998Smarkm
93280304Sjkim    /*
94280304Sjkim     * Reduce the functional reference count here so if it's the terminating
95280304Sjkim     * case, we can release the lock safely and call the finish() handler
96280304Sjkim     * without risk of a race. We get a race if we leave the count until
97280304Sjkim     * after and something else is calling "finish" at the same time -
98280304Sjkim     * there's a chance that both threads will together take the count from 2
99280304Sjkim     * to 0 without either calling finish().
100280304Sjkim     */
101280304Sjkim    e->funct_ref--;
102280304Sjkim    engine_ref_debug(e, 1, -1);
103280304Sjkim    if ((e->funct_ref == 0) && e->finish) {
104280304Sjkim        if (unlock_for_handlers)
105280304Sjkim            CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
106280304Sjkim        to_return = e->finish(e);
107280304Sjkim        if (unlock_for_handlers)
108280304Sjkim            CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
109280304Sjkim        if (!to_return)
110280304Sjkim            return 0;
111280304Sjkim    }
112109998Smarkm#ifdef REF_CHECK
113280304Sjkim    if (e->funct_ref < 0) {
114280304Sjkim        fprintf(stderr, "ENGINE_finish, bad functional reference count\n");
115280304Sjkim        abort();
116280304Sjkim    }
117109998Smarkm#endif
118280304Sjkim    /* Release the structural reference too */
119280304Sjkim    if (!engine_free_util(e, 0)) {
120280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_UNLOCKED_FINISH, ENGINE_R_FINISH_FAILED);
121280304Sjkim        return 0;
122280304Sjkim    }
123280304Sjkim    return to_return;
124280304Sjkim}
125109998Smarkm
126109998Smarkm/* The API (locked) version of "init" */
127109998Smarkmint ENGINE_init(ENGINE *e)
128280304Sjkim{
129280304Sjkim    int ret;
130280304Sjkim    if (e == NULL) {
131280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);
132280304Sjkim        return 0;
133280304Sjkim    }
134280304Sjkim    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
135280304Sjkim    ret = engine_unlocked_init(e);
136280304Sjkim    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
137280304Sjkim    return ret;
138280304Sjkim}
139109998Smarkm
140109998Smarkm/* The API (locked) version of "finish" */
141109998Smarkmint ENGINE_finish(ENGINE *e)
142280304Sjkim{
143280304Sjkim    int to_return = 1;
144109998Smarkm
145280304Sjkim    if (e == NULL) {
146280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_FINISH, ERR_R_PASSED_NULL_PARAMETER);
147280304Sjkim        return 0;
148280304Sjkim    }
149280304Sjkim    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
150280304Sjkim    to_return = engine_unlocked_finish(e, 1);
151280304Sjkim    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
152280304Sjkim    if (!to_return) {
153280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_FINISH, ENGINE_R_FINISH_FAILED);
154280304Sjkim        return 0;
155280304Sjkim    }
156280304Sjkim    return to_return;
157280304Sjkim}
158