1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/engine/eng_init.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,154 @@
1.4 +/* crypto/engine/eng_init.c */
1.5 +/* ====================================================================
1.6 + * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
1.7 + *
1.8 + * Redistribution and use in source and binary forms, with or without
1.9 + * modification, are permitted provided that the following conditions
1.10 + * are met:
1.11 + *
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + *
1.15 + * 2. Redistributions in binary form must reproduce the above copyright
1.16 + * notice, this list of conditions and the following disclaimer in
1.17 + * the documentation and/or other materials provided with the
1.18 + * distribution.
1.19 + *
1.20 + * 3. All advertising materials mentioning features or use of this
1.21 + * software must display the following acknowledgment:
1.22 + * "This product includes software developed by the OpenSSL Project
1.23 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.24 + *
1.25 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.26 + * endorse or promote products derived from this software without
1.27 + * prior written permission. For written permission, please contact
1.28 + * licensing@OpenSSL.org.
1.29 + *
1.30 + * 5. Products derived from this software may not be called "OpenSSL"
1.31 + * nor may "OpenSSL" appear in their names without prior written
1.32 + * permission of the OpenSSL Project.
1.33 + *
1.34 + * 6. Redistributions of any form whatsoever must retain the following
1.35 + * acknowledgment:
1.36 + * "This product includes software developed by the OpenSSL Project
1.37 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.38 + *
1.39 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.40 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.41 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.42 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.43 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.44 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.45 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.46 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.47 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.48 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.49 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.50 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.51 + * ====================================================================
1.52 + *
1.53 + * This product includes cryptographic software written by Eric Young
1.54 + * (eay@cryptsoft.com). This product includes software written by Tim
1.55 + * Hudson (tjh@cryptsoft.com).
1.56 + *
1.57 + */
1.58 +
1.59 +#include "eng_int.h"
1.60 +
1.61 +/* Initialise a engine type for use (or up its functional reference count
1.62 + * if it's already in use). This version is only used internally. */
1.63 +EXPORT_C int engine_unlocked_init(ENGINE *e)
1.64 + {
1.65 + int to_return = 1;
1.66 +
1.67 + if((e->funct_ref == 0) && e->init)
1.68 + /* This is the first functional reference and the engine
1.69 + * requires initialisation so we do it now. */
1.70 + to_return = e->init(e);
1.71 + if(to_return)
1.72 + {
1.73 + /* OK, we return a functional reference which is also a
1.74 + * structural reference. */
1.75 + e->struct_ref++;
1.76 + e->funct_ref++;
1.77 + engine_ref_debug(e, 0, 1)
1.78 + engine_ref_debug(e, 1, 1)
1.79 + }
1.80 + return to_return;
1.81 + }
1.82 +
1.83 +/* Free a functional reference to a engine type. This version is only used
1.84 + * internally. */
1.85 +EXPORT_C int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
1.86 + {
1.87 + int to_return = 1;
1.88 +
1.89 + /* Reduce the functional reference count here so if it's the terminating
1.90 + * case, we can release the lock safely and call the finish() handler
1.91 + * without risk of a race. We get a race if we leave the count until
1.92 + * after and something else is calling "finish" at the same time -
1.93 + * there's a chance that both threads will together take the count from
1.94 + * 2 to 0 without either calling finish(). */
1.95 + e->funct_ref--;
1.96 + engine_ref_debug(e, 1, -1);
1.97 + if((e->funct_ref == 0) && e->finish)
1.98 + {
1.99 + if(unlock_for_handlers)
1.100 + CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1.101 + to_return = e->finish(e);
1.102 + if(unlock_for_handlers)
1.103 + CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1.104 + if(!to_return)
1.105 + return 0;
1.106 + }
1.107 +#ifdef REF_CHECK
1.108 + if(e->funct_ref < 0)
1.109 + {
1.110 + fprintf(stderr,"ENGINE_finish, bad functional reference count\n");
1.111 + abort();
1.112 + }
1.113 +#endif
1.114 + /* Release the structural reference too */
1.115 + if(!engine_free_util(e, 0))
1.116 + {
1.117 + ENGINEerr(ENGINE_F_ENGINE_UNLOCKED_FINISH,ENGINE_R_FINISH_FAILED);
1.118 + return 0;
1.119 + }
1.120 + return to_return;
1.121 + }
1.122 +
1.123 +/* The API (locked) version of "init" */
1.124 +EXPORT_C int ENGINE_init(ENGINE *e)
1.125 + {
1.126 + int ret;
1.127 + if(e == NULL)
1.128 + {
1.129 + ENGINEerr(ENGINE_F_ENGINE_INIT,ERR_R_PASSED_NULL_PARAMETER);
1.130 + return 0;
1.131 + }
1.132 + CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1.133 + ret = engine_unlocked_init(e);
1.134 + CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1.135 + return ret;
1.136 + }
1.137 +
1.138 +/* The API (locked) version of "finish" */
1.139 +EXPORT_C int ENGINE_finish(ENGINE *e)
1.140 + {
1.141 + int to_return = 1;
1.142 +
1.143 + if(e == NULL)
1.144 + {
1.145 + ENGINEerr(ENGINE_F_ENGINE_FINISH,ERR_R_PASSED_NULL_PARAMETER);
1.146 + return 0;
1.147 + }
1.148 + CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1.149 + to_return = engine_unlocked_finish(e, 1);
1.150 + CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1.151 + if(!to_return)
1.152 + {
1.153 + ENGINEerr(ENGINE_F_ENGINE_FINISH,ENGINE_R_FINISH_FAILED);
1.154 + return 0;
1.155 + }
1.156 + return to_return;
1.157 + }