str_mem.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/* crypto/store/str_mem.c -*- mode:C; c-file-style: "eay" -*- */
/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
 * project 2003.
 */
/* ====================================================================
 * Copyright (c) 2003 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
 *    the documentation and/or other materials provided with the
 *    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
 *    openssl-core@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 <string.h>
#include <openssl/err.h>
#include "str_locl.h"

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/* The memory store is currently highly experimental.  It's meant to become
   a base store used by other stores for internal caching (for full caching
   support, aging needs to be added).

   The database use is meant to support as much attribute association as
   possible, while providing for as small search ranges as possible.
   This is currently provided for by sorting the entries by numbers that
   are composed of bits set at the positions indicated by attribute type
   codes.  This provides for ranges determined by the highest attribute
   type code value.  A better idea might be to sort by values computed
   from the range of attributes associated with the object (basically,
   the difference between the highest and lowest attribute type code)
   and it's distance from a base (basically, the lowest associated
   attribute type code).
*/

B
Ben Laurie 已提交
79
typedef struct mem_object_data_st
80 81 82 83
	{
	STORE_OBJECT *object;
	STORE_ATTR_INFO *attr_info;
	int references;
B
Ben Laurie 已提交
84
	} MEM_OBJECT_DATA;
85

R
Richard Levitte 已提交
86
DECLARE_STACK_OF(MEM_OBJECT_DATA)
87 88
struct mem_data_st
	{
89 90 91 92 93 94 95 96 97 98 99
	/*
	 * sorted with
	 * STORE_ATTR_INFO_compare().
	 */
	STACK_OF(MEM_OBJECT_DATA) *data; 
	/*
	 * Currently unused, but can
	 * be used to add attributes
	 * from parts of the data.
	 */
	unsigned int compute_components : 1;
100 101
	};

R
Richard Levitte 已提交
102
DECLARE_STACK_OF(STORE_ATTR_INFO)
103 104
struct mem_ctx_st
	{
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	/* The type we're searching for */
	int type;
	/*
	 * Sets of
	 * attributes to search for.  Each
	 * element is a STORE_ATTR_INFO.
	 */
	STACK_OF(STORE_ATTR_INFO) *search_attributes;
	/*
	 * which of the search attributes we
	 * found a match for, -1 when we still
	 * haven't found any
	 */ 
	int search_index;
	/* -1 as long as we're searching for the first */
	int index;
121 122 123 124 125
	};

static int mem_init(STORE *s);
static void mem_clean(STORE *s);
static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
126
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
127
static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
128
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
129
static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
130 131
	STORE_OBJECT *data, OPENSSL_ITEM attributes[],
	OPENSSL_ITEM parameters[]);
132 133
static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
	OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
134 135
	OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
	OPENSSL_ITEM parameters[]);
136
static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
137
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
138
static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
139
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
140 141 142
static STORE_OBJECT *mem_list_next(STORE *s, void *handle);
static int mem_list_end(STORE *s, void *handle);
static int mem_list_endp(STORE *s, void *handle);
143 144 145 146
static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
	OPENSSL_ITEM parameters[]);
static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
	OPENSSL_ITEM parameters[]);
R
Richard Levitte 已提交
147
static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void));
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

static STORE_METHOD store_memory =
	{
	"OpenSSL memory store interface",
	mem_init,
	mem_clean,
	mem_generate,
	mem_get,
	mem_store,
	mem_modify,
	NULL, /* revoke */
	mem_delete,
	mem_list_start,
	mem_list_next,
	mem_list_end,
	mem_list_endp,
	NULL, /* update */
	mem_lock,
	mem_unlock,
	mem_ctrl
	};

const STORE_METHOD *STORE_Memory(void)
	{
	return &store_memory;
	}

static int mem_init(STORE *s)
	{
	return 1;
	}

static void mem_clean(STORE *s)
	{
	return;
	}

static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
186
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
187 188 189 190 191
	{
	STOREerr(STORE_F_MEM_GENERATE, STORE_R_NOT_IMPLEMENTED);
	return 0;
	}
static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
192
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
193
	{
194
	void *context = mem_list_start(s, type, attributes, parameters);
195 196 197 198 199 200 201 202 203 204 205
	
	if (context)
		{
		STORE_OBJECT *object = mem_list_next(s, context);

		if (mem_list_end(s, context))
			return object;
		}
	return NULL;
	}
static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
206 207
	STORE_OBJECT *data, OPENSSL_ITEM attributes[],
	OPENSSL_ITEM parameters[])
208 209 210 211 212 213
	{
	STOREerr(STORE_F_MEM_STORE, STORE_R_NOT_IMPLEMENTED);
	return 0;
	}
static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
	OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
214 215
	OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
	OPENSSL_ITEM parameters[])
216
	{
B
Bodo Möller 已提交
217
	STOREerr(STORE_F_MEM_MODIFY, STORE_R_NOT_IMPLEMENTED);
218 219 220
	return 0;
	}
static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
221
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
222 223 224 225
	{
	STOREerr(STORE_F_MEM_DELETE, STORE_R_NOT_IMPLEMENTED);
	return 0;
	}
226

227
/* The list functions may be the hardest to understand.  Basically,
228 229 230 231 232 233
   mem_list_start compiles a stack of attribute info elements, and
   puts that stack into the context to be returned.  mem_list_next
   will then find the first matching element in the store, and then
   walk all the way to the end of the store (since any combination
   of attribute bits above the starting point may match the searched
   for bit pattern...). */
234
static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
235
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	{
	struct mem_ctx_st *context =
		(struct mem_ctx_st *)OPENSSL_malloc(sizeof(struct mem_ctx_st));
	void *attribute_context = NULL;
	STORE_ATTR_INFO *attrs = NULL;

	if (!context)
		{
		STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE);
		return 0;
		}
	memset(context, 0, sizeof(struct mem_ctx_st));

	attribute_context = STORE_parse_attrs_start(attributes);
	if (!attribute_context)
		{
		STOREerr(STORE_F_MEM_LIST_START, ERR_R_STORE_LIB);
		goto err;
		}

	while((attrs = STORE_parse_attrs_next(attribute_context)))
		{
		if (context->search_attributes == NULL)
			{
			context->search_attributes =
B
Ben Laurie 已提交
261
				sk_STORE_ATTR_INFO_new(STORE_ATTR_INFO_compare);
262 263 264 265 266 267 268
			if (!context->search_attributes)
				{
				STOREerr(STORE_F_MEM_LIST_START,
					ERR_R_MALLOC_FAILURE);
				goto err;
				}
			}
B
Ben Laurie 已提交
269
		sk_STORE_ATTR_INFO_push(context->search_attributes,attrs);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
		}
	if (!STORE_parse_attrs_endp(attribute_context))
		goto err;
	STORE_parse_attrs_end(attribute_context);
	context->search_index = -1;
	context->index = -1;
	return context;
 err:
	if (attribute_context) STORE_parse_attrs_end(attribute_context);
	mem_list_end(s, context);
	return NULL;
	}
static STORE_OBJECT *mem_list_next(STORE *s, void *handle)
	{
	int i;
	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
	struct mem_object_data_st key = { 0, 0, 1 };
	struct mem_data_st *store =
		(struct mem_data_st *)STORE_get_ex_data(s, 1);
	int srch;
	int cres = 0;

	if (!context)
		{
		STOREerr(STORE_F_MEM_LIST_NEXT, ERR_R_PASSED_NULL_PARAMETER);
		return NULL;
		}
	if (!store)
		{
		STOREerr(STORE_F_MEM_LIST_NEXT, STORE_R_NO_STORE);
		return NULL;
		}

	if (context->search_index == -1)
		{
B
Ben Laurie 已提交
305 306 307
		for (i = 0;
		     i < sk_STORE_ATTR_INFO_num(context->search_attributes);
		     i++)
308
			{
B
Ben Laurie 已提交
309 310 311 312
			key.attr_info
			  = sk_STORE_ATTR_INFO_value(context->search_attributes,
						     i);
			srch = sk_MEM_OBJECT_DATA_find_ex(store->data, &key);
313 314 315 316 317 318 319 320 321 322 323 324

			if (srch >= 0)
				{
				context->search_index = srch;
				break;
				}
			}
		}
	if (context->search_index < 0)
		return NULL;
	
	key.attr_info =
B
Ben Laurie 已提交
325 326
		sk_STORE_ATTR_INFO_value(context->search_attributes,
					 context->search_index);
327
	for(srch = context->search_index;
B
Ben Laurie 已提交
328
	    srch < sk_MEM_OBJECT_DATA_num(store->data)
329
		    && STORE_ATTR_INFO_in_range(key.attr_info,
R
Richard Levitte 已提交
330
			    sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info)
331
		    && !(cres = STORE_ATTR_INFO_in_ex(key.attr_info,
R
Richard Levitte 已提交
332
				 sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info));
333 334 335 336 337
	    srch++)
		;

	context->search_index = srch;
	if (cres)
B
Ben Laurie 已提交
338
		return (sk_MEM_OBJECT_DATA_value(store->data, srch))->object;
339 340 341 342 343 344 345 346
	return NULL;
	}
static int mem_list_end(STORE *s, void *handle)
	{
	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;

	if (!context)
		{
B
Bodo Möller 已提交
347
		STOREerr(STORE_F_MEM_LIST_END, ERR_R_PASSED_NULL_PARAMETER);
348 349 350
		return 0;
		}
	if (context && context->search_attributes)
B
Ben Laurie 已提交
351
		sk_STORE_ATTR_INFO_free(context->search_attributes);
352 353 354 355 356 357 358 359
	if (context) OPENSSL_free(context);
	return 1;
	}
static int mem_list_endp(STORE *s, void *handle)
	{
	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;

	if (!context
B
Ben Laurie 已提交
360 361
	    || context->search_index
	       == sk_STORE_ATTR_INFO_num(context->search_attributes))
362 363 364
		return 1;
	return 0;
	}
365 366
static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
	OPENSSL_ITEM parameters[])
367 368 369
	{
	return 1;
	}
370 371
static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
	OPENSSL_ITEM parameters[])
372 373 374
	{
	return 1;
	}
R
Richard Levitte 已提交
375
static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void))
376 377 378
	{
	return 1;
	}