os/ossrv/ssl/libcrypto/src/crypto/engine/eng_lib.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/engine/eng_lib.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,345 @@
     1.4 +/* crypto/engine/eng_lib.c */
     1.5 +/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
     1.6 + * project 2000.
     1.7 + */
     1.8 +/* ====================================================================
     1.9 + * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + *
    1.15 + * 1. Redistributions of source code must retain the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer. 
    1.17 + *
    1.18 + * 2. Redistributions in binary form must reproduce the above copyright
    1.19 + *    notice, this list of conditions and the following disclaimer in
    1.20 + *    the documentation and/or other materials provided with the
    1.21 + *    distribution.
    1.22 + *
    1.23 + * 3. All advertising materials mentioning features or use of this
    1.24 + *    software must display the following acknowledgment:
    1.25 + *    "This product includes software developed by the OpenSSL Project
    1.26 + *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
    1.27 + *
    1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.29 + *    endorse or promote products derived from this software without
    1.30 + *    prior written permission. For written permission, please contact
    1.31 + *    licensing@OpenSSL.org.
    1.32 + *
    1.33 + * 5. Products derived from this software may not be called "OpenSSL"
    1.34 + *    nor may "OpenSSL" appear in their names without prior written
    1.35 + *    permission of the OpenSSL Project.
    1.36 + *
    1.37 + * 6. Redistributions of any form whatsoever must retain the following
    1.38 + *    acknowledgment:
    1.39 + *    "This product includes software developed by the OpenSSL Project
    1.40 + *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
    1.41 + *
    1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.45 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.54 + * ====================================================================
    1.55 + *
    1.56 + * This product includes cryptographic software written by Eric Young
    1.57 + * (eay@cryptsoft.com).  This product includes software written by Tim
    1.58 + * Hudson (tjh@cryptsoft.com).
    1.59 + *
    1.60 + */
    1.61 +/*
    1.62 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 +#include "eng_int.h"
    1.65 +#include <openssl/rand.h>
    1.66 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.67 +#include "libcrypto_wsd_macros.h"
    1.68 +#include "libcrypto_wsd.h"
    1.69 +#endif
    1.70 +
    1.71 +
    1.72 +/* The "new"/"free" stuff first */
    1.73 +
    1.74 +EXPORT_C ENGINE *ENGINE_new(void)
    1.75 +	{
    1.76 +	ENGINE *ret;
    1.77 +
    1.78 +	ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
    1.79 +	if(ret == NULL)
    1.80 +		{
    1.81 +		ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
    1.82 +		return NULL;
    1.83 +		}
    1.84 +	memset(ret, 0, sizeof(ENGINE));
    1.85 +	ret->struct_ref = 1;
    1.86 +	engine_ref_debug(ret, 0, 1)
    1.87 +	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data);
    1.88 +	return ret;
    1.89 +	}
    1.90 +
    1.91 +/* Placed here (close proximity to ENGINE_new) so that modifications to the
    1.92 + * elements of the ENGINE structure are more likely to be caught and changed
    1.93 + * here. */
    1.94 +EXPORT_C void engine_set_all_null(ENGINE *e)
    1.95 +	{
    1.96 +	e->id = NULL;
    1.97 +	e->name = NULL;
    1.98 +	e->rsa_meth = NULL;
    1.99 +	e->dsa_meth = NULL;
   1.100 +	e->dh_meth = NULL;
   1.101 +	e->rand_meth = NULL;
   1.102 +	e->store_meth = NULL;
   1.103 +	e->ciphers = NULL;
   1.104 +	e->digests = NULL;
   1.105 +	e->destroy = NULL;
   1.106 +	e->init = NULL;
   1.107 +	e->finish = NULL;
   1.108 +	e->ctrl = NULL;
   1.109 +	e->load_privkey = NULL;
   1.110 +	e->load_pubkey = NULL;
   1.111 +	e->cmd_defns = NULL;
   1.112 +	e->flags = 0;
   1.113 +	}
   1.114 +
   1.115 +EXPORT_C int engine_free_util(ENGINE *e, int locked)
   1.116 +	{
   1.117 +	int i;
   1.118 +
   1.119 +	if(e == NULL)
   1.120 +		{
   1.121 +		ENGINEerr(ENGINE_F_ENGINE_FREE_UTIL,
   1.122 +			ERR_R_PASSED_NULL_PARAMETER);
   1.123 +		return 0;
   1.124 +		}
   1.125 +	if(locked)
   1.126 +		i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
   1.127 +	else
   1.128 +		i = --e->struct_ref;
   1.129 +	engine_ref_debug(e, 0, -1)
   1.130 +	if (i > 0) return 1;
   1.131 +#ifdef REF_CHECK
   1.132 +	if (i < 0)
   1.133 +		{
   1.134 +		fprintf(stderr,"ENGINE_free, bad structural reference count\n");
   1.135 +		abort();
   1.136 +		}
   1.137 +#endif
   1.138 +	/* Give the ENGINE a chance to do any structural cleanup corresponding
   1.139 +	 * to allocation it did in its constructor (eg. unload error strings) */
   1.140 +	if(e->destroy)
   1.141 +		e->destroy(e);
   1.142 +	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);
   1.143 +	OPENSSL_free(e);
   1.144 +	return 1;
   1.145 +	}
   1.146 +
   1.147 +EXPORT_C int ENGINE_free(ENGINE *e)
   1.148 +	{
   1.149 +	return engine_free_util(e, 1);
   1.150 +	}
   1.151 +
   1.152 +/* Cleanup stuff */
   1.153 +
   1.154 +/* ENGINE_cleanup() is coded such that anything that does work that will need
   1.155 + * cleanup can register a "cleanup" callback here. That way we don't get linker
   1.156 + * bloat by referring to all *possible* cleanups, but any linker bloat into code
   1.157 + * "X" will cause X's cleanup function to end up here. */
   1.158 +#ifndef EMULATOR 
   1.159 +static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL;
   1.160 +#else
   1.161 +GET_STATIC_VAR_FROM_TLS(cleanup_stack,eng_lib,STACK_OF(ENGINE_CLEANUP_ITEM) *)
   1.162 +#define cleanup_stack (*GET_WSD_VAR_NAME(cleanup_stack,eng_lib, s)())
   1.163 +#endif
   1.164 +static int int_cleanup_check(int create)
   1.165 +	{
   1.166 +	if(cleanup_stack) return 1;
   1.167 +	if(!create) return 0;
   1.168 +	cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null();
   1.169 +	return (cleanup_stack ? 1 : 0);
   1.170 +	}
   1.171 +static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb)
   1.172 +	{
   1.173 +	ENGINE_CLEANUP_ITEM *item = OPENSSL_malloc(sizeof(
   1.174 +					ENGINE_CLEANUP_ITEM));
   1.175 +	if(!item) return NULL;
   1.176 +	item->cb = cb;
   1.177 +	return item;
   1.178 +	}
   1.179 +EXPORT_C void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb)
   1.180 +	{
   1.181 +	ENGINE_CLEANUP_ITEM *item;
   1.182 +	if(!int_cleanup_check(1)) return;
   1.183 +	item = int_cleanup_item(cb);
   1.184 +	if(item)
   1.185 +		sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0);
   1.186 +	}
   1.187 +EXPORT_C void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb)
   1.188 +	{
   1.189 +	ENGINE_CLEANUP_ITEM *item;
   1.190 +	if(!int_cleanup_check(1)) return;
   1.191 +	item = int_cleanup_item(cb);
   1.192 +	if(item)
   1.193 +		sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item);
   1.194 +	}
   1.195 +/* The API function that performs all cleanup */
   1.196 +static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item)
   1.197 +	{
   1.198 +	(*(item->cb))();
   1.199 +	OPENSSL_free(item);
   1.200 +	}
   1.201 +EXPORT_C void ENGINE_cleanup(void)
   1.202 +	{
   1.203 +	if(int_cleanup_check(0))
   1.204 +		{
   1.205 +		sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack,
   1.206 +			engine_cleanup_cb_free);
   1.207 +		cleanup_stack = NULL;
   1.208 +		}
   1.209 +	/* FIXME: This should be handled (somehow) through RAND, eg. by it
   1.210 +	 * registering a cleanup callback. */
   1.211 +	RAND_set_rand_method(NULL);
   1.212 +	}
   1.213 +
   1.214 +/* Now the "ex_data" support */
   1.215 +
   1.216 +EXPORT_C int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
   1.217 +		CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
   1.218 +	{
   1.219 +	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp,
   1.220 +			new_func, dup_func, free_func);
   1.221 +	}
   1.222 +
   1.223 +EXPORT_C int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
   1.224 +	{
   1.225 +	return(CRYPTO_set_ex_data(&e->ex_data, idx, arg));
   1.226 +	}
   1.227 +
   1.228 +EXPORT_C void *ENGINE_get_ex_data(const ENGINE *e, int idx)
   1.229 +	{
   1.230 +	return(CRYPTO_get_ex_data(&e->ex_data, idx));
   1.231 +	}
   1.232 +
   1.233 +/* Functions to get/set an ENGINE's elements - mainly to avoid exposing the
   1.234 + * ENGINE structure itself. */
   1.235 +
   1.236 +EXPORT_C int ENGINE_set_id(ENGINE *e, const char *id)
   1.237 +	{
   1.238 +	if(id == NULL)
   1.239 +		{
   1.240 +		ENGINEerr(ENGINE_F_ENGINE_SET_ID,
   1.241 +			ERR_R_PASSED_NULL_PARAMETER);
   1.242 +		return 0;
   1.243 +		}
   1.244 +	e->id = id;
   1.245 +	return 1;
   1.246 +	}
   1.247 +
   1.248 +EXPORT_C int ENGINE_set_name(ENGINE *e, const char *name)
   1.249 +	{
   1.250 +	if(name == NULL)
   1.251 +		{
   1.252 +		ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
   1.253 +			ERR_R_PASSED_NULL_PARAMETER);
   1.254 +		return 0;
   1.255 +		}
   1.256 +	e->name = name;
   1.257 +	return 1;
   1.258 +	}
   1.259 +
   1.260 +EXPORT_C int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f)
   1.261 +	{
   1.262 +	e->destroy = destroy_f;
   1.263 +	return 1;
   1.264 +	}
   1.265 +
   1.266 +EXPORT_C int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
   1.267 +	{
   1.268 +	e->init = init_f;
   1.269 +	return 1;
   1.270 +	}
   1.271 +
   1.272 +EXPORT_C int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
   1.273 +	{
   1.274 +	e->finish = finish_f;
   1.275 +	return 1;
   1.276 +	}
   1.277 +
   1.278 +EXPORT_C int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
   1.279 +	{
   1.280 +	e->ctrl = ctrl_f;
   1.281 +	return 1;
   1.282 +	}
   1.283 +
   1.284 +EXPORT_C int ENGINE_set_flags(ENGINE *e, int flags)
   1.285 +	{
   1.286 +	e->flags = flags;
   1.287 +	return 1;
   1.288 +	}
   1.289 +
   1.290 +EXPORT_C int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
   1.291 +	{
   1.292 +	e->cmd_defns = defns;
   1.293 +	return 1;
   1.294 +	}
   1.295 +
   1.296 +EXPORT_C const char *ENGINE_get_id(const ENGINE *e)
   1.297 +	{
   1.298 +	return e->id;
   1.299 +	}
   1.300 +
   1.301 +EXPORT_C const char *ENGINE_get_name(const ENGINE *e)
   1.302 +	{
   1.303 +	return e->name;
   1.304 +	}
   1.305 +
   1.306 +EXPORT_C ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e)
   1.307 +	{
   1.308 +	return e->destroy;
   1.309 +	}
   1.310 +
   1.311 +EXPORT_C ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
   1.312 +	{
   1.313 +	return e->init;
   1.314 +	}
   1.315 +
   1.316 +EXPORT_C ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
   1.317 +	{
   1.318 +	return e->finish;
   1.319 +	}
   1.320 +
   1.321 +EXPORT_C ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
   1.322 +	{
   1.323 +	return e->ctrl;
   1.324 +	}
   1.325 +
   1.326 +EXPORT_C int ENGINE_get_flags(const ENGINE *e)
   1.327 +	{
   1.328 +	return e->flags;
   1.329 +	}
   1.330 +
   1.331 +EXPORT_C const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
   1.332 +	{
   1.333 +	return e->cmd_defns;
   1.334 +	}
   1.335 +
   1.336 +/* eng_lib.o is pretty much linked into anything that touches ENGINE already, so
   1.337 + * put the "static_state" hack here. */
   1.338 +#ifndef EMULATOR
   1.339 +static int internal_static_hack = 0;
   1.340 +#else
   1.341 +GET_STATIC_VAR_FROM_TLS(internal_static_hack,eng_lib,int)
   1.342 +#define internal_static_hack (*GET_WSD_VAR_NAME(internal_static_hack,eng_lib, s)())
   1.343 +#endif
   1.344 +
   1.345 +EXPORT_C void *ENGINE_get_static_state(void)
   1.346 +	{
   1.347 +	return &internal_static_hack;
   1.348 +	}