os/ossrv/ssl/libcrypto/src/crypto/store/str_mem.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/store/str_mem.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,390 @@
     1.4 +/* crypto/store/str_mem.c -*- mode:C; c-file-style: "eay" -*- */
     1.5 +/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
     1.6 + * project 2003.
     1.7 + */
     1.8 +/* ====================================================================
     1.9 + * Copyright (c) 2003 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 + *    openssl-core@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 +/*
    1.63 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.64 + */
    1.65 + 
    1.66 +#include <string.h>
    1.67 +#include <openssl/err.h>
    1.68 +#include "str_locl.h"
    1.69 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.70 +#include "libcrypto_wsd_macros.h"
    1.71 +#include "libcrypto_wsd.h"
    1.72 +#endif
    1.73 +
    1.74 +/* The memory store is currently highly experimental.  It's meant to become
    1.75 +   a base store used by other stores for internal caching (for full caching
    1.76 +   support, aging needs to be added).
    1.77 +
    1.78 +   The database use is meant to support as much attribute association as
    1.79 +   possible, while providing for as small search ranges as possible.
    1.80 +   This is currently provided for by sorting the entries by numbers that
    1.81 +   are composed of bits set at the positions indicated by attribute type
    1.82 +   codes.  This provides for ranges determined by the highest attribute
    1.83 +   type code value.  A better idea might be to sort by values computed
    1.84 +   from the range of attributes associated with the object (basically,
    1.85 +   the difference between the highest and lowest attribute type code)
    1.86 +   and it's distance from a base (basically, the lowest associated
    1.87 +   attribute type code).
    1.88 +*/
    1.89 +
    1.90 +struct mem_object_data_st
    1.91 +	{
    1.92 +	STORE_OBJECT *object;
    1.93 +	STORE_ATTR_INFO *attr_info;
    1.94 +	int references;
    1.95 +	};
    1.96 +
    1.97 +struct mem_data_st
    1.98 +	{
    1.99 +	STACK *data;		/* A stack of mem_object_data_st,
   1.100 +				   sorted with STORE_ATTR_INFO_compare(). */
   1.101 +	unsigned int compute_components : 1; /* Currently unused, but can
   1.102 +						be used to add attributes
   1.103 +						from parts of the data. */
   1.104 +	};
   1.105 +
   1.106 +struct mem_ctx_st
   1.107 +	{
   1.108 +	int type;		/* The type we're searching for */
   1.109 +	STACK *search_attributes; /* Sets of attributes to search for.
   1.110 +				     Each element is a STORE_ATTR_INFO. */
   1.111 +	int search_index;	/* which of the search attributes we found a match
   1.112 +				   for, -1 when we still haven't found any */
   1.113 +	int index;		/* -1 as long as we're searching for the first */
   1.114 +	};
   1.115 +
   1.116 +static int mem_init(STORE *s);
   1.117 +static void mem_clean(STORE *s);
   1.118 +static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
   1.119 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
   1.120 +static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
   1.121 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
   1.122 +static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
   1.123 +	STORE_OBJECT *data, OPENSSL_ITEM attributes[],
   1.124 +	OPENSSL_ITEM parameters[]);
   1.125 +static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
   1.126 +	OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
   1.127 +	OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
   1.128 +	OPENSSL_ITEM parameters[]);
   1.129 +static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
   1.130 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
   1.131 +static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
   1.132 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
   1.133 +static STORE_OBJECT *mem_list_next(STORE *s, void *handle);
   1.134 +static int mem_list_end(STORE *s, void *handle);
   1.135 +static int mem_list_endp(STORE *s, void *handle);
   1.136 +static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
   1.137 +	OPENSSL_ITEM parameters[]);
   1.138 +static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
   1.139 +	OPENSSL_ITEM parameters[]);
   1.140 +static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void));
   1.141 +
   1.142 +
   1.143 +#ifndef EMULATOR
   1.144 +static STORE_METHOD store_memory =
   1.145 +	{
   1.146 +	"OpenSSL memory store interface",
   1.147 +	mem_init,
   1.148 +	mem_clean,
   1.149 +	mem_generate,
   1.150 +	mem_get,
   1.151 +	mem_store,
   1.152 +	mem_modify,
   1.153 +	NULL, /* revoke */
   1.154 +	mem_delete,
   1.155 +	mem_list_start,
   1.156 +	mem_list_next,
   1.157 +	mem_list_end,
   1.158 +	mem_list_endp,
   1.159 +	NULL, /* update */
   1.160 +	mem_lock,
   1.161 +	mem_unlock,
   1.162 +	mem_ctrl
   1.163 +	};
   1.164 +#else
   1.165 +GET_STATIC_VAR_FROM_TLS(store_memory,str_mem,STORE_METHOD)
   1.166 +#define store_memory (*GET_WSD_VAR_NAME(store_memory,str_mem, s)())
   1.167 +const STORE_METHOD temp_s_store_memory =
   1.168 +	{
   1.169 +	"OpenSSL memory store interface",
   1.170 +	mem_init,
   1.171 +	mem_clean,
   1.172 +	mem_generate,
   1.173 +	mem_get,
   1.174 +	mem_store,
   1.175 +	mem_modify,
   1.176 +	NULL, /* revoke */
   1.177 +	mem_delete,
   1.178 +	mem_list_start,
   1.179 +	mem_list_next,
   1.180 +	mem_list_end,
   1.181 +	mem_list_endp,
   1.182 +	NULL, /* update */
   1.183 +	mem_lock,
   1.184 +	mem_unlock,
   1.185 +	mem_ctrl
   1.186 +	};
   1.187 +#endif
   1.188 +EXPORT_C const STORE_METHOD *STORE_Memory(void)
   1.189 +	{
   1.190 +	return &store_memory;
   1.191 +	}
   1.192 +
   1.193 +static int mem_init(STORE *s)
   1.194 +	{
   1.195 +	return 1;
   1.196 +	}
   1.197 +
   1.198 +static void mem_clean(STORE *s)
   1.199 +	{
   1.200 +	return;
   1.201 +	}
   1.202 +
   1.203 +static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
   1.204 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
   1.205 +	{
   1.206 +	STOREerr(STORE_F_MEM_GENERATE, STORE_R_NOT_IMPLEMENTED);
   1.207 +	return 0;
   1.208 +	}
   1.209 +static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
   1.210 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
   1.211 +	{
   1.212 +	void *context = mem_list_start(s, type, attributes, parameters);
   1.213 +	
   1.214 +	if (context)
   1.215 +		{
   1.216 +		STORE_OBJECT *object = mem_list_next(s, context);
   1.217 +
   1.218 +		if (mem_list_end(s, context))
   1.219 +			return object;
   1.220 +		}
   1.221 +	return NULL;
   1.222 +	}
   1.223 +static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
   1.224 +	STORE_OBJECT *data, OPENSSL_ITEM attributes[],
   1.225 +	OPENSSL_ITEM parameters[])
   1.226 +	{
   1.227 +	STOREerr(STORE_F_MEM_STORE, STORE_R_NOT_IMPLEMENTED);
   1.228 +	return 0;
   1.229 +	}
   1.230 +static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
   1.231 +	OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
   1.232 +	OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
   1.233 +	OPENSSL_ITEM parameters[])
   1.234 +	{
   1.235 +	STOREerr(STORE_F_MEM_MODIFY, STORE_R_NOT_IMPLEMENTED);
   1.236 +	return 0;
   1.237 +	}
   1.238 +static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
   1.239 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
   1.240 +	{
   1.241 +	STOREerr(STORE_F_MEM_DELETE, STORE_R_NOT_IMPLEMENTED);
   1.242 +	return 0;
   1.243 +	}
   1.244 +
   1.245 +/* The list functions may be the hardest to understand.  Basically,
   1.246 +   mem_list_start compiles a stack of attribute info elements, and
   1.247 +   puts that stack into the context to be returned.  mem_list_next
   1.248 +   will then find the first matching element in the store, and then
   1.249 +   walk all the way to the end of the store (since any combination
   1.250 +   of attribute bits above the starting point may match the searched
   1.251 +   for bit pattern...). */
   1.252 +static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
   1.253 +	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
   1.254 +	{
   1.255 +	struct mem_ctx_st *context =
   1.256 +		(struct mem_ctx_st *)OPENSSL_malloc(sizeof(struct mem_ctx_st));
   1.257 +	void *attribute_context = NULL;
   1.258 +	STORE_ATTR_INFO *attrs = NULL;
   1.259 +
   1.260 +	if (!context)
   1.261 +		{
   1.262 +		STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE);
   1.263 +		return 0;
   1.264 +		}
   1.265 +	memset(context, 0, sizeof(struct mem_ctx_st));
   1.266 +
   1.267 +	attribute_context = STORE_parse_attrs_start(attributes);
   1.268 +	if (!attribute_context)
   1.269 +		{
   1.270 +		STOREerr(STORE_F_MEM_LIST_START, ERR_R_STORE_LIB);
   1.271 +		goto err;
   1.272 +		}
   1.273 +
   1.274 +	while((attrs = STORE_parse_attrs_next(attribute_context)))
   1.275 +		{
   1.276 +		if (context->search_attributes == NULL)
   1.277 +			{
   1.278 +			context->search_attributes =
   1.279 +				sk_new((int (*)(const char * const *, const char * const *))STORE_ATTR_INFO_compare);
   1.280 +			if (!context->search_attributes)
   1.281 +				{
   1.282 +				STOREerr(STORE_F_MEM_LIST_START,
   1.283 +					ERR_R_MALLOC_FAILURE);
   1.284 +				goto err;
   1.285 +				}
   1.286 +			}
   1.287 +		sk_push(context->search_attributes,(char *)attrs);
   1.288 +		}
   1.289 +	if (!STORE_parse_attrs_endp(attribute_context))
   1.290 +		goto err;
   1.291 +	STORE_parse_attrs_end(attribute_context);
   1.292 +	context->search_index = -1;
   1.293 +	context->index = -1;
   1.294 +	return context;
   1.295 + err:
   1.296 +	if (attribute_context) STORE_parse_attrs_end(attribute_context);
   1.297 +	mem_list_end(s, context);
   1.298 +	return NULL;
   1.299 +	}
   1.300 +static STORE_OBJECT *mem_list_next(STORE *s, void *handle)
   1.301 +	{
   1.302 +	int i;
   1.303 +	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
   1.304 +	struct mem_object_data_st key = { 0, 0, 1 };
   1.305 +	struct mem_data_st *store =
   1.306 +		(struct mem_data_st *)STORE_get_ex_data(s, 1);
   1.307 +	int srch;
   1.308 +	int cres = 0;
   1.309 +
   1.310 +	if (!context)
   1.311 +		{
   1.312 +		STOREerr(STORE_F_MEM_LIST_NEXT, ERR_R_PASSED_NULL_PARAMETER);
   1.313 +		return NULL;
   1.314 +		}
   1.315 +	if (!store)
   1.316 +		{
   1.317 +		STOREerr(STORE_F_MEM_LIST_NEXT, STORE_R_NO_STORE);
   1.318 +		return NULL;
   1.319 +		}
   1.320 +
   1.321 +	if (context->search_index == -1)
   1.322 +		{
   1.323 +		for (i = 0; i < sk_num(context->search_attributes); i++)
   1.324 +			{
   1.325 +			key.attr_info =
   1.326 +				(STORE_ATTR_INFO *)sk_value(context->search_attributes, i);
   1.327 +			srch = sk_find_ex(store->data, (char *)&key);
   1.328 +
   1.329 +			if (srch >= 0)
   1.330 +				{
   1.331 +				context->search_index = srch;
   1.332 +				break;
   1.333 +				}
   1.334 +			}
   1.335 +		}
   1.336 +	if (context->search_index < 0)
   1.337 +		return NULL;
   1.338 +	
   1.339 +	key.attr_info =
   1.340 +		(STORE_ATTR_INFO *)sk_value(context->search_attributes,
   1.341 +			context->search_index);
   1.342 +	for(srch = context->search_index;
   1.343 +	    srch < sk_num(store->data)
   1.344 +		    && STORE_ATTR_INFO_in_range(key.attr_info,
   1.345 +			    (STORE_ATTR_INFO *)sk_value(store->data, srch))
   1.346 +		    && !(cres = STORE_ATTR_INFO_in_ex(key.attr_info,
   1.347 +				 (STORE_ATTR_INFO *)sk_value(store->data, srch)));
   1.348 +	    srch++)
   1.349 +		;
   1.350 +
   1.351 +	context->search_index = srch;
   1.352 +	if (cres)
   1.353 +		return ((struct mem_object_data_st *)sk_value(store->data,
   1.354 +				srch))->object;
   1.355 +	return NULL;
   1.356 +	}
   1.357 +static int mem_list_end(STORE *s, void *handle)
   1.358 +	{
   1.359 +	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
   1.360 +
   1.361 +	if (!context)
   1.362 +		{
   1.363 +		STOREerr(STORE_F_MEM_LIST_END, ERR_R_PASSED_NULL_PARAMETER);
   1.364 +		return 0;
   1.365 +		}
   1.366 +	if (context && context->search_attributes)
   1.367 +		sk_free(context->search_attributes);
   1.368 +	if (context) OPENSSL_free(context);
   1.369 +	return 1;
   1.370 +	}
   1.371 +static int mem_list_endp(STORE *s, void *handle)
   1.372 +	{
   1.373 +	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
   1.374 +
   1.375 +	if (!context
   1.376 +		|| context->search_index == sk_num(context->search_attributes))
   1.377 +		return 1;
   1.378 +	return 0;
   1.379 +	}
   1.380 +static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
   1.381 +	OPENSSL_ITEM parameters[])
   1.382 +	{
   1.383 +	return 1;
   1.384 +	}
   1.385 +static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
   1.386 +	OPENSSL_ITEM parameters[])
   1.387 +	{
   1.388 +	return 1;
   1.389 +	}
   1.390 +static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void))
   1.391 +	{
   1.392 +	return 1;
   1.393 +	}