eng_table.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* ====================================================================
* Copyright (c) 2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include "eng_int.h"
/* This is the type of item in the 'implementation' table. Each 'nid' hashes to
* a (potentially NULL) ENGINE_PILE structure which contains a stack of ENGINE*
* pointers. These pointers aren't references, because they're inserted and
* removed during ENGINE creation and ENGINE destruction. They point to ENGINEs
* that *exist* (ie. have a structural reference count greater than zero) rather
* than ENGINEs that are *functional*. Each pointer in those stacks are to
* ENGINEs that implements the algorithm corresponding to each 'nid'. */
/* The type of the items in the table */
typedef struct st_engine_pile
{
* */
int nid;
/* A stack of ENGINE pointers for ENGINEs that support this
* this stack that initialises will be set as 'funct' and assumed as the
* default for operations of this type. */
/* This value optimises engine_table_select(). If it is called it sets
* this value to 1. Any changes to this ENGINE_PILE resets it to zero.
* As such, no ENGINE_init() thrashing is done unless ENGINEs
int uptodate;
} ENGINE_PILE;
/* The type of the hash table of ENGINE_PILE structures such that each are
* unique and keyed by the 'nid' value. */
struct st_engine_table
{
}; /* ENGINE_TABLE */
/* This value stores global options controlling behaviour of (mostly) the
* engine_table_select() function. It's a bitmask of flag values of the form
* ENGINE_TABLE_FLAG_*** (as defined in engine.h) and is controlled by the
* ENGINE_[get|set]_table_flags() function. */
static unsigned int table_flags = 0;
/* API function manipulating 'table_flags' */
unsigned int ENGINE_get_table_flags(void)
{
return table_flags;
}
void ENGINE_set_table_flags(unsigned int flags)
{
table_flags = flags;
}
/* Internal functions for the "piles" hash table */
static unsigned long engine_pile_hash(const ENGINE_PILE *c)
{
return c->nid;
}
{
}
{
if(*t)
return 1;
if(!create)
return 0;
return 0;
*t = (ENGINE_TABLE *)lh;
return 1;
}
* ENGINEs from the implementation table */
{
if(!(*table))
added = 1;
goto end;
if(added)
/* The cleanup callback needs to be added */
while(num_nids--)
{
if(!fnd)
{
if(!fnd)
goto end;
{
goto end;
}
}
/* A registration shouldn't add duplciate entries */
/* if 'setdefault', this ENGINE goes to the head of the list */
goto end;
/* "touch" this ENGINE_PILE */
if(setdefault)
{
if(!engine_unlocked_init(e))
{
goto end;
}
}
nids++;
}
ret = 1;
end:
return ret;
}
{
int n;
/* Iterate the 'c->sk' stack removing any occurance of 'e' */
{
/* "touch" this ENGINE_CIPHER */
}
{
engine_unlocked_finish(e, 0);
}
}
{
if(int_table_check(table, 0))
}
static void int_cleanup_cb(ENGINE_PILE *p)
{
sk_ENGINE_free(p->sk);
if(p->funct)
engine_unlocked_finish(p->funct, 0);
OPENSSL_free(p);
}
{
if(*table)
{
}
}
/* Exposed API function to get a functional reference from the implementation
* table (ie. try to get a functional reference from the tabled structural
* references) for a given cipher 'nid' */
#ifndef ENGINE_TABLE_DEBUG
#else
#endif
{
/* If 'engine_ciphers' is NULL, then it's absolutely *sure* that no
* ENGINEs have registered any implementations! */
if(!(*table))
{
#ifdef ENGINE_TABLE_DEBUG
"registered for anything!\n", f, l, nid);
#endif
return NULL;
}
/* Check again inside the lock otherwise we could race against cleanup
* operations. But don't worry about a fprintf(stderr). */
if(!int_table_check(table, 0))
goto end;
if(!fnd)
goto end;
{
#ifdef ENGINE_TABLE_DEBUG
#endif
goto end;
}
{
goto end;
}
if(!ret)
{
#ifdef ENGINE_TABLE_DEBUG
"registered implementations would initialise\n",
f, l, nid);
#endif
goto end;
}
#if 0
/* Don't need to get a reference if we hold the lock. If the locking has
* to change in future, that would be different ... */
#endif
/* Try and initialise the ENGINE if it's already functional *or* if the
* ENGINE_TABLE_FLAG_NOINIT flag is not set. */
else
initres = 0;
#if 0
/* Release the structural reference */
#endif
if(initres)
{
/* If we didn't have a default (functional reference) for this
* 'nid' (or we had one but for whatever reason we're now
* initialising a different one), use this opportunity to set
* 'funct'. */
{
/* If there was a previous default we release it. */
/* We got an extra functional reference for the
* per-'nid' default */
#ifdef ENGINE_TABLE_DEBUG
#endif
}
#ifdef ENGINE_TABLE_DEBUG
#endif
goto end;
}
goto trynext;
end:
/* Whatever happened - we should "untouch" our uptodate file seeing as
* we have tried our best to find a functional reference for 'nid'. If
* it failed, it is unlikely to succeed again until some future
* registrations (or unregistrations) have taken place that affect that
* 'nid'. */
if(fnd)
#ifdef ENGINE_TABLE_DEBUG
if(ret)
else
"'no matching ENGINE'\n", f, l, nid);
#endif
/* Whatever happened, any failed init()s are not failures in this
* context, so clear our error state. */
return ret;
}