eng_dyn.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
* project 2001.
*/
/* ====================================================================
* Copyright (c) 1999-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 <stdio.h>
#include "cryptlib.h"
#include "eng_int.h"
/* Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE loader
* should implement the hook-up functions with the following prototypes. */
/* Our ENGINE handlers */
static int dynamic_init(ENGINE *e);
static int dynamic_finish(ENGINE *e);
/* Predeclare our context type */
typedef struct st_dynamic_data_ctx dynamic_data_ctx;
/* The implementation for the important control command */
#define DYNAMIC_CMD_SO_PATH ENGINE_CMD_BASE
/* The constants used when creating the ENGINE */
static const char *engine_dynamic_id = "dynamic";
static const char *engine_dynamic_name = "Dynamic engine loading support";
static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = {
"SO_PATH",
"Specifies the path to the new ENGINE shared library",
"NO_VCHECK",
"Specifies to continue even if version checking fails (boolean)",
"ID",
"Specifies an ENGINE id name for loading",
"LIST_ADD",
"Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)",
"LOAD",
"Load up the ENGINE specified by other settings",
};
static const ENGINE_CMD_DEFN dynamic_cmd_defns_empty[] = {
};
/* Loading code stores state inside the ENGINE structure via the "ex_data"
* element. We load all our state into a single structure and use that as a
* single context in the "ex_data" stack. */
struct st_dynamic_data_ctx
{
/* The DSO object we load that supplies the ENGINE code */
/* The function pointer to the version checking shared library function */
/* The function pointer to the engine-binding shared library function */
const char *DYNAMIC_LIBNAME;
/* Whether to continue loading on a version check failure */
int no_vcheck;
/* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
const char *engine_id;
/* If non-zero, a successfully loaded ENGINE should be added to the internal
* ENGINE list. If 2, the add must succeed or the entire load should fail. */
int list_add_value;
/* The symbol name for the version checking function */
const char *DYNAMIC_F1;
/* The symbol name for the "initialise ENGINE structure" function */
const char *DYNAMIC_F2;
};
/* This is the "ex_data" index we obtain and reserve for use with our context
* structure. */
static int dynamic_ex_data_idx = -1;
/* Because our ex_data element may or may not get allocated depending on whether
* a "first-use" occurs before the ENGINE is freed, we have a memory leak
* problem to solve. We can't declare a "new" handler for the ex_data as we
* don't want a dynamic_data_ctx in *all* ENGINE structures of all types (this
* is a bug in the design of CRYPTO_EX_DATA). As such, we just declare a "free"
* handler and that will get called if an ENGINE is being destroyed and there
* was an ex_data element corresponding to our context type. */
{
if(ptr)
{
if(ctx->dynamic_dso)
if(ctx->DYNAMIC_LIBNAME)
}
}
/* Construct the per-ENGINE context. We create it blindly and then use a lock to
* check for a race - if so, all but one of the threads "racing" will have
* wasted their time. The alternative involves creating everything inside the
* lock which is far worse. */
{
dynamic_data_ctx *c;
c = OPENSSL_malloc(sizeof(dynamic_data_ctx));
if(!c)
{
return 0;
}
memset(c, 0, sizeof(dynamic_data_ctx));
c->dynamic_dso = NULL;
c->bind_engine = NULL;
c->DYNAMIC_LIBNAME = NULL;
c->no_vcheck = 0;
c->list_add_value = 0;
c->DYNAMIC_F1 = "v_check";
c->DYNAMIC_F2 = "bind_engine";
dynamic_ex_data_idx)) == NULL)
{
/* Good, we're the first */
*ctx = c;
c = NULL;
}
/* If we lost the race to set the context, c is non-NULL and *ctx is the
* context of the thread that won. */
if(c)
OPENSSL_free(c);
return 1;
}
/* This function retrieves the context structure from an ENGINE's "ex_data", or
* if it doesn't exist yet, sets it up. */
{
if(dynamic_ex_data_idx < 0)
{
/* Create and register the ENGINE ex_data, and associate our
* "free" function with it to ensure any allocated contexts get
* freed when an ENGINE goes underground. */
if(new_idx == -1)
{
return NULL;
}
/* Avoid a race by checking again inside this lock */
if(dynamic_ex_data_idx < 0)
{
/* Good, someone didn't beat us to it */
new_idx = -1;
}
/* In theory we could "give back" the index here if
* (new_idx>-1), but it's not possible and wouldn't gain us much
* if it were. */
}
/* Check if the context needs to be created */
/* "set_data" will set errors if necessary */
return NULL;
return ctx;
}
static ENGINE *engine_dynamic(void)
{
if(!ret)
return NULL;
{
return NULL;
}
return ret;
}
void ENGINE_load_dynamic(void)
{
if(!toadd) return;
/* If the "add" worked, it gets a structural reference. So either way,
* we release our just-created reference. */
/* If the "add" didn't work, it was probably a conflict because it was
* already added (eg. someone calling ENGINE_load_blah then calling
* ENGINE_load_builtin_engines() perhaps). */
}
static int dynamic_init(ENGINE *e)
{
/* We always return failure - the "dyanamic" engine itself can't be used
* for anything. */
return 0;
}
static int dynamic_finish(ENGINE *e)
{
/* This should never be called on account of "dynamic_init" always
* failing. */
return 0;
}
{
int initialised;
if(!ctx)
{
return 0;
}
/* All our control commands require the ENGINE to be uninitialised */
if(initialised)
{
return 0;
}
switch(cmd)
{
case DYNAMIC_CMD_SO_PATH:
/* a NULL 'p' or a string of zero-length is the same thing */
if(p && (strlen((const char *)p) < 1))
p = NULL;
if(ctx->DYNAMIC_LIBNAME)
if(p)
else
case DYNAMIC_CMD_NO_VCHECK:
return 1;
case DYNAMIC_CMD_ID:
/* a NULL 'p' or a string of zero-length is the same thing */
if(p && (strlen((const char *)p) < 1))
p = NULL;
if(p)
else
case DYNAMIC_CMD_LIST_ADD:
if((i < 0) || (i > 2))
{
return 0;
}
ctx->list_add_value = (int)i;
return 1;
case DYNAMIC_CMD_LOAD:
return dynamic_load(e, ctx);
default:
break;
}
return 0;
}
{
{
return 0;
}
/* We have to find a bind function otherwise it'll always end badly */
{
return 0;
}
/* Do we perform version checking? */
{
unsigned long vcheck_res = 0;
/* Now we try to find a version checking function and decide how
/* We fail if the version checker veto'd the load *or* if it is
* deferring to us (by returning its version) and we think it is
* too old. */
{
/* Fail */
return 0;
}
}
/* First binary copy the ENGINE structure so that we can roll back if
* the hand-over fails */
/* Provide the ERR, "ex_data", memory, and locking callbacks so the
* loaded library uses our state rather than its own. FIXME: As noted in
* engine.h, much of this would be simplified if each area of code
* provided its own "summary" structure of all related callbacks. It
* would also increase opaqueness. */
/* Now that we've loaded the dynamic engine, make sure no "dynamic"
* ENGINE elements will show through. */
/* Try to bind the ENGINE onto our own ENGINE structure */
{
/* Copy the original ENGINE structure back */
return 0;
}
/* Do we try to add this ENGINE to the internal list too? */
if(ctx->list_add_value > 0)
{
if(!ENGINE_add(e))
{
/* Do we tolerate this or fail? */
{
/* Fail - NB: By this time, it's too late to
* rollback, and trying to do so allows the
* bind_engine() code to have created leaks. We
* just have to fail where we are, after the
* ENGINE has changed. */
return 0;
}
/* Tolerate */
}
}
return 1;
}