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 10109998Smarkm * 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 58109998Smarkm/* Initialise a engine type for use (or up its functional reference count 59109998Smarkm * if it's already in use). This version is only used internally. */ 60109998Smarkmint engine_unlocked_init(ENGINE *e) 61109998Smarkm { 62109998Smarkm int to_return = 1; 63109998Smarkm 64109998Smarkm if((e->funct_ref == 0) && e->init) 65109998Smarkm /* This is the first functional reference and the engine 66109998Smarkm * requires initialisation so we do it now. */ 67109998Smarkm to_return = e->init(e); 68109998Smarkm if(to_return) 69109998Smarkm { 70109998Smarkm /* OK, we return a functional reference which is also a 71109998Smarkm * structural reference. */ 72109998Smarkm e->struct_ref++; 73109998Smarkm e->funct_ref++; 74109998Smarkm engine_ref_debug(e, 0, 1) 75109998Smarkm engine_ref_debug(e, 1, 1) 76109998Smarkm } 77109998Smarkm return to_return; 78109998Smarkm } 79109998Smarkm 80109998Smarkm/* Free a functional reference to a engine type. This version is only used 81109998Smarkm * internally. */ 82109998Smarkmint engine_unlocked_finish(ENGINE *e, int unlock_for_handlers) 83109998Smarkm { 84109998Smarkm int to_return = 1; 85109998Smarkm 86109998Smarkm /* Reduce the functional reference count here so if it's the terminating 87109998Smarkm * case, we can release the lock safely and call the finish() handler 88109998Smarkm * without risk of a race. We get a race if we leave the count until 89109998Smarkm * after and something else is calling "finish" at the same time - 90109998Smarkm * there's a chance that both threads will together take the count from 91109998Smarkm * 2 to 0 without either calling finish(). */ 92109998Smarkm e->funct_ref--; 93109998Smarkm engine_ref_debug(e, 1, -1); 94109998Smarkm if((e->funct_ref == 0) && e->finish) 95109998Smarkm { 96109998Smarkm if(unlock_for_handlers) 97109998Smarkm CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 98109998Smarkm to_return = e->finish(e); 99109998Smarkm if(unlock_for_handlers) 100109998Smarkm CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 101109998Smarkm if(!to_return) 102109998Smarkm return 0; 103109998Smarkm } 104109998Smarkm#ifdef REF_CHECK 105109998Smarkm if(e->funct_ref < 0) 106109998Smarkm { 107109998Smarkm fprintf(stderr,"ENGINE_finish, bad functional reference count\n"); 108109998Smarkm abort(); 109109998Smarkm } 110109998Smarkm#endif 111109998Smarkm /* Release the structural reference too */ 112109998Smarkm if(!engine_free_util(e, 0)) 113109998Smarkm { 114160814Ssimon ENGINEerr(ENGINE_F_ENGINE_UNLOCKED_FINISH,ENGINE_R_FINISH_FAILED); 115109998Smarkm return 0; 116109998Smarkm } 117109998Smarkm return to_return; 118109998Smarkm } 119109998Smarkm 120109998Smarkm/* The API (locked) version of "init" */ 121109998Smarkmint ENGINE_init(ENGINE *e) 122109998Smarkm { 123109998Smarkm int ret; 124109998Smarkm if(e == NULL) 125109998Smarkm { 126109998Smarkm ENGINEerr(ENGINE_F_ENGINE_INIT,ERR_R_PASSED_NULL_PARAMETER); 127109998Smarkm return 0; 128109998Smarkm } 129109998Smarkm CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 130109998Smarkm ret = engine_unlocked_init(e); 131109998Smarkm CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 132109998Smarkm return ret; 133109998Smarkm } 134109998Smarkm 135109998Smarkm/* The API (locked) version of "finish" */ 136109998Smarkmint ENGINE_finish(ENGINE *e) 137109998Smarkm { 138109998Smarkm int to_return = 1; 139109998Smarkm 140109998Smarkm if(e == NULL) 141109998Smarkm { 142109998Smarkm ENGINEerr(ENGINE_F_ENGINE_FINISH,ERR_R_PASSED_NULL_PARAMETER); 143109998Smarkm return 0; 144109998Smarkm } 145109998Smarkm CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 146109998Smarkm to_return = engine_unlocked_finish(e, 1); 147109998Smarkm CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 148109998Smarkm if(!to_return) 149109998Smarkm { 150109998Smarkm ENGINEerr(ENGINE_F_ENGINE_FINISH,ENGINE_R_FINISH_FAILED); 151109998Smarkm return 0; 152109998Smarkm } 153109998Smarkm return to_return; 154109998Smarkm } 155