sl@0: /* ==================================================================== sl@0: * Copyright (c) 2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include "eng_int.h" sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: /* The type of the items in the table */ sl@0: typedef struct st_engine_pile sl@0: { sl@0: /* The 'nid' of this algorithm/mode */ sl@0: int nid; sl@0: /* ENGINEs that implement this algorithm/mode. */ sl@0: STACK_OF(ENGINE) *sk; sl@0: /* The default ENGINE to perform this algorithm/mode. */ sl@0: ENGINE *funct; sl@0: /* Zero if 'sk' is newer than the cached 'funct', non-zero otherwise */ sl@0: int uptodate; sl@0: } ENGINE_PILE; sl@0: sl@0: /* The type exposed in eng_int.h */ sl@0: struct st_engine_table sl@0: { sl@0: LHASH piles; sl@0: }; /* ENGINE_TABLE */ sl@0: sl@0: /* Global flags (ENGINE_TABLE_FLAG_***). */ sl@0: #ifndef EMULATOR sl@0: static unsigned int table_flags = 0; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(table_flags,eng_table,unsigned int) sl@0: #define table_flags (*GET_WSD_VAR_NAME(table_flags,eng_table, s)()) sl@0: #endif sl@0: sl@0: /* API function manipulating 'table_flags' */ sl@0: EXPORT_C unsigned int ENGINE_get_table_flags(void) sl@0: { sl@0: return table_flags; sl@0: } sl@0: EXPORT_C void ENGINE_set_table_flags(unsigned int flags) sl@0: { sl@0: table_flags = flags; sl@0: } sl@0: sl@0: /* Internal functions for the "piles" hash table */ sl@0: static unsigned long engine_pile_hash(const ENGINE_PILE *c) sl@0: { sl@0: return c->nid; sl@0: } sl@0: static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b) sl@0: { sl@0: return a->nid - b->nid; sl@0: } sl@0: static IMPLEMENT_LHASH_HASH_FN(engine_pile_hash, const ENGINE_PILE *) sl@0: static IMPLEMENT_LHASH_COMP_FN(engine_pile_cmp, const ENGINE_PILE *) sl@0: static int int_table_check(ENGINE_TABLE **t, int create) sl@0: { sl@0: LHASH *lh; sl@0: if(*t) return 1; sl@0: if(!create) return 0; sl@0: if((lh = lh_new(LHASH_HASH_FN(engine_pile_hash), sl@0: LHASH_COMP_FN(engine_pile_cmp))) == NULL) sl@0: return 0; sl@0: *t = (ENGINE_TABLE *)lh; sl@0: return 1; sl@0: } sl@0: sl@0: /* Privately exposed (via eng_int.h) functions for adding and/or removing sl@0: * ENGINEs from the implementation table */ sl@0: EXPORT_C int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup, sl@0: ENGINE *e, const int *nids, int num_nids, int setdefault) sl@0: { sl@0: int ret = 0, added = 0; sl@0: ENGINE_PILE tmplate, *fnd; sl@0: CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); sl@0: if(!(*table)) sl@0: added = 1; sl@0: if(!int_table_check(table, 1)) sl@0: goto end; sl@0: if(added) sl@0: /* The cleanup callback needs to be added */ sl@0: engine_cleanup_add_first(cleanup); sl@0: while(num_nids--) sl@0: { sl@0: tmplate.nid = *nids; sl@0: fnd = lh_retrieve(&(*table)->piles, &tmplate); sl@0: if(!fnd) sl@0: { sl@0: fnd = OPENSSL_malloc(sizeof(ENGINE_PILE)); sl@0: if(!fnd) goto end; sl@0: fnd->uptodate = 0; sl@0: fnd->nid = *nids; sl@0: fnd->sk = sk_ENGINE_new_null(); sl@0: if(!fnd->sk) sl@0: { sl@0: OPENSSL_free(fnd); sl@0: goto end; sl@0: } sl@0: fnd->funct = NULL; sl@0: lh_insert(&(*table)->piles, fnd); sl@0: } sl@0: /* A registration shouldn't add duplciate entries */ sl@0: (void)sk_ENGINE_delete_ptr(fnd->sk, e); sl@0: /* if 'setdefault', this ENGINE goes to the head of the list */ sl@0: if(!sk_ENGINE_push(fnd->sk, e)) sl@0: goto end; sl@0: /* "touch" this ENGINE_PILE */ sl@0: fnd->uptodate = 1; sl@0: if(setdefault) sl@0: { sl@0: if(!engine_unlocked_init(e)) sl@0: { sl@0: ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER, sl@0: ENGINE_R_INIT_FAILED); sl@0: goto end; sl@0: } sl@0: if(fnd->funct) sl@0: engine_unlocked_finish(fnd->funct, 0); sl@0: fnd->funct = e; sl@0: } sl@0: nids++; sl@0: } sl@0: ret = 1; sl@0: end: sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); sl@0: return ret; sl@0: } sl@0: static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e) sl@0: { sl@0: int n; sl@0: /* Iterate the 'c->sk' stack removing any occurance of 'e' */ sl@0: while((n = sk_ENGINE_find(pile->sk, e)) >= 0) sl@0: { sl@0: (void)sk_ENGINE_delete(pile->sk, n); sl@0: /* "touch" this ENGINE_CIPHER */ sl@0: pile->uptodate = 1; sl@0: } sl@0: if(pile->funct == e) sl@0: { sl@0: engine_unlocked_finish(e, 0); sl@0: pile->funct = NULL; sl@0: } sl@0: } sl@0: static IMPLEMENT_LHASH_DOALL_ARG_FN(int_unregister_cb,ENGINE_PILE *,ENGINE *) sl@0: EXPORT_C void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e) sl@0: { sl@0: CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); sl@0: if(int_table_check(table, 0)) sl@0: lh_doall_arg(&(*table)->piles, sl@0: LHASH_DOALL_ARG_FN(int_unregister_cb), e); sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); sl@0: } sl@0: sl@0: static void int_cleanup_cb(ENGINE_PILE *p) sl@0: { sl@0: sk_ENGINE_free(p->sk); sl@0: if(p->funct) sl@0: engine_unlocked_finish(p->funct, 0); sl@0: OPENSSL_free(p); sl@0: } sl@0: static IMPLEMENT_LHASH_DOALL_FN(int_cleanup_cb,ENGINE_PILE *) sl@0: EXPORT_C void engine_table_cleanup(ENGINE_TABLE **table) sl@0: { sl@0: CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); sl@0: if(*table) sl@0: { sl@0: lh_doall(&(*table)->piles, LHASH_DOALL_FN(int_cleanup_cb)); sl@0: lh_free(&(*table)->piles); sl@0: *table = NULL; sl@0: } sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); sl@0: } sl@0: sl@0: /* return a functional reference for a given 'nid' */ sl@0: #ifndef ENGINE_TABLE_DEBUG sl@0: EXPORT_C ENGINE *engine_table_select(ENGINE_TABLE **table, int nid) sl@0: #else sl@0: EXPORT_C ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, int l) sl@0: #endif sl@0: { sl@0: ENGINE *ret = NULL; sl@0: ENGINE_PILE tmplate, *fnd=NULL; sl@0: int initres, loop = 0; sl@0: sl@0: if(!(*table)) sl@0: { sl@0: #ifdef ENGINE_TABLE_DEBUG sl@0: fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing " sl@0: "registered!\n", f, l, nid); sl@0: #endif sl@0: return NULL; sl@0: } sl@0: CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); sl@0: /* Check again inside the lock otherwise we could race against cleanup sl@0: * operations. But don't worry about a fprintf(stderr). */ sl@0: if(!int_table_check(table, 0)) goto end; sl@0: tmplate.nid = nid; sl@0: fnd = lh_retrieve(&(*table)->piles, &tmplate); sl@0: if(!fnd) goto end; sl@0: if(fnd->funct && engine_unlocked_init(fnd->funct)) sl@0: { sl@0: #ifdef ENGINE_TABLE_DEBUG sl@0: fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using " sl@0: "ENGINE '%s' cached\n", f, l, nid, fnd->funct->id); sl@0: #endif sl@0: ret = fnd->funct; sl@0: goto end; sl@0: } sl@0: if(fnd->uptodate) sl@0: { sl@0: ret = fnd->funct; sl@0: goto end; sl@0: } sl@0: trynext: sl@0: ret = sk_ENGINE_value(fnd->sk, loop++); sl@0: if(!ret) sl@0: { sl@0: #ifdef ENGINE_TABLE_DEBUG sl@0: fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no " sl@0: "registered implementations would initialise\n", sl@0: f, l, nid); sl@0: #endif sl@0: goto end; sl@0: } sl@0: /* Try to initialise the ENGINE? */ sl@0: if((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT)) sl@0: initres = engine_unlocked_init(ret); sl@0: else sl@0: initres = 0; sl@0: if(initres) sl@0: { sl@0: /* Update 'funct' */ sl@0: if((fnd->funct != ret) && engine_unlocked_init(ret)) sl@0: { sl@0: /* If there was a previous default we release it. */ sl@0: if(fnd->funct) sl@0: engine_unlocked_finish(fnd->funct, 0); sl@0: fnd->funct = ret; sl@0: #ifdef ENGINE_TABLE_DEBUG sl@0: fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, " sl@0: "setting default to '%s'\n", f, l, nid, ret->id); sl@0: #endif sl@0: } sl@0: #ifdef ENGINE_TABLE_DEBUG sl@0: fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using " sl@0: "newly initialised '%s'\n", f, l, nid, ret->id); sl@0: #endif sl@0: goto end; sl@0: } sl@0: goto trynext; sl@0: end: sl@0: /* If it failed, it is unlikely to succeed again until some future sl@0: * registrations have taken place. In all cases, we cache. */ sl@0: if(fnd) fnd->uptodate = 1; sl@0: #ifdef ENGINE_TABLE_DEBUG sl@0: if(ret) sl@0: fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching " sl@0: "ENGINE '%s'\n", f, l, nid, ret->id); sl@0: else sl@0: fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching " sl@0: "'no matching ENGINE'\n", f, l, nid); sl@0: #endif sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); sl@0: /* Whatever happened, any failed init()s are not failures in this sl@0: * context, so clear our error state. */ sl@0: ERR_clear_error(); sl@0: return ret; sl@0: }