os/ossrv/ssl/libcrypto/src/crypto/store/str_mem.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/store/str_mem.c -*- mode:C; c-file-style: "eay" -*- */
sl@0
     2
/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
sl@0
     3
 * project 2003.
sl@0
     4
 */
sl@0
     5
/* ====================================================================
sl@0
     6
 * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 *
sl@0
    12
 * 1. Redistributions of source code must retain the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    14
 *
sl@0
    15
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    16
 *    notice, this list of conditions and the following disclaimer in
sl@0
    17
 *    the documentation and/or other materials provided with the
sl@0
    18
 *    distribution.
sl@0
    19
 *
sl@0
    20
 * 3. All advertising materials mentioning features or use of this
sl@0
    21
 *    software must display the following acknowledgment:
sl@0
    22
 *    "This product includes software developed by the OpenSSL Project
sl@0
    23
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    24
 *
sl@0
    25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    26
 *    endorse or promote products derived from this software without
sl@0
    27
 *    prior written permission. For written permission, please contact
sl@0
    28
 *    openssl-core@openssl.org.
sl@0
    29
 *
sl@0
    30
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    31
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    32
 *    permission of the OpenSSL Project.
sl@0
    33
 *
sl@0
    34
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    35
 *    acknowledgment:
sl@0
    36
 *    "This product includes software developed by the OpenSSL Project
sl@0
    37
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    38
 *
sl@0
    39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    51
 * ====================================================================
sl@0
    52
 *
sl@0
    53
 * This product includes cryptographic software written by Eric Young
sl@0
    54
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    55
 * Hudson (tjh@cryptsoft.com).
sl@0
    56
 *
sl@0
    57
 */
sl@0
    58
sl@0
    59
/*
sl@0
    60
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    61
 */
sl@0
    62
 
sl@0
    63
#include <string.h>
sl@0
    64
#include <openssl/err.h>
sl@0
    65
#include "str_locl.h"
sl@0
    66
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    67
#include "libcrypto_wsd_macros.h"
sl@0
    68
#include "libcrypto_wsd.h"
sl@0
    69
#endif
sl@0
    70
sl@0
    71
/* The memory store is currently highly experimental.  It's meant to become
sl@0
    72
   a base store used by other stores for internal caching (for full caching
sl@0
    73
   support, aging needs to be added).
sl@0
    74
sl@0
    75
   The database use is meant to support as much attribute association as
sl@0
    76
   possible, while providing for as small search ranges as possible.
sl@0
    77
   This is currently provided for by sorting the entries by numbers that
sl@0
    78
   are composed of bits set at the positions indicated by attribute type
sl@0
    79
   codes.  This provides for ranges determined by the highest attribute
sl@0
    80
   type code value.  A better idea might be to sort by values computed
sl@0
    81
   from the range of attributes associated with the object (basically,
sl@0
    82
   the difference between the highest and lowest attribute type code)
sl@0
    83
   and it's distance from a base (basically, the lowest associated
sl@0
    84
   attribute type code).
sl@0
    85
*/
sl@0
    86
sl@0
    87
struct mem_object_data_st
sl@0
    88
	{
sl@0
    89
	STORE_OBJECT *object;
sl@0
    90
	STORE_ATTR_INFO *attr_info;
sl@0
    91
	int references;
sl@0
    92
	};
sl@0
    93
sl@0
    94
struct mem_data_st
sl@0
    95
	{
sl@0
    96
	STACK *data;		/* A stack of mem_object_data_st,
sl@0
    97
				   sorted with STORE_ATTR_INFO_compare(). */
sl@0
    98
	unsigned int compute_components : 1; /* Currently unused, but can
sl@0
    99
						be used to add attributes
sl@0
   100
						from parts of the data. */
sl@0
   101
	};
sl@0
   102
sl@0
   103
struct mem_ctx_st
sl@0
   104
	{
sl@0
   105
	int type;		/* The type we're searching for */
sl@0
   106
	STACK *search_attributes; /* Sets of attributes to search for.
sl@0
   107
				     Each element is a STORE_ATTR_INFO. */
sl@0
   108
	int search_index;	/* which of the search attributes we found a match
sl@0
   109
				   for, -1 when we still haven't found any */
sl@0
   110
	int index;		/* -1 as long as we're searching for the first */
sl@0
   111
	};
sl@0
   112
sl@0
   113
static int mem_init(STORE *s);
sl@0
   114
static void mem_clean(STORE *s);
sl@0
   115
static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   116
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
sl@0
   117
static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   118
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
sl@0
   119
static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   120
	STORE_OBJECT *data, OPENSSL_ITEM attributes[],
sl@0
   121
	OPENSSL_ITEM parameters[]);
sl@0
   122
static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   123
	OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
sl@0
   124
	OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
sl@0
   125
	OPENSSL_ITEM parameters[]);
sl@0
   126
static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   127
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
sl@0
   128
static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   129
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
sl@0
   130
static STORE_OBJECT *mem_list_next(STORE *s, void *handle);
sl@0
   131
static int mem_list_end(STORE *s, void *handle);
sl@0
   132
static int mem_list_endp(STORE *s, void *handle);
sl@0
   133
static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
sl@0
   134
	OPENSSL_ITEM parameters[]);
sl@0
   135
static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
sl@0
   136
	OPENSSL_ITEM parameters[]);
sl@0
   137
static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void));
sl@0
   138
sl@0
   139
sl@0
   140
#ifndef EMULATOR
sl@0
   141
static STORE_METHOD store_memory =
sl@0
   142
	{
sl@0
   143
	"OpenSSL memory store interface",
sl@0
   144
	mem_init,
sl@0
   145
	mem_clean,
sl@0
   146
	mem_generate,
sl@0
   147
	mem_get,
sl@0
   148
	mem_store,
sl@0
   149
	mem_modify,
sl@0
   150
	NULL, /* revoke */
sl@0
   151
	mem_delete,
sl@0
   152
	mem_list_start,
sl@0
   153
	mem_list_next,
sl@0
   154
	mem_list_end,
sl@0
   155
	mem_list_endp,
sl@0
   156
	NULL, /* update */
sl@0
   157
	mem_lock,
sl@0
   158
	mem_unlock,
sl@0
   159
	mem_ctrl
sl@0
   160
	};
sl@0
   161
#else
sl@0
   162
GET_STATIC_VAR_FROM_TLS(store_memory,str_mem,STORE_METHOD)
sl@0
   163
#define store_memory (*GET_WSD_VAR_NAME(store_memory,str_mem, s)())
sl@0
   164
const STORE_METHOD temp_s_store_memory =
sl@0
   165
	{
sl@0
   166
	"OpenSSL memory store interface",
sl@0
   167
	mem_init,
sl@0
   168
	mem_clean,
sl@0
   169
	mem_generate,
sl@0
   170
	mem_get,
sl@0
   171
	mem_store,
sl@0
   172
	mem_modify,
sl@0
   173
	NULL, /* revoke */
sl@0
   174
	mem_delete,
sl@0
   175
	mem_list_start,
sl@0
   176
	mem_list_next,
sl@0
   177
	mem_list_end,
sl@0
   178
	mem_list_endp,
sl@0
   179
	NULL, /* update */
sl@0
   180
	mem_lock,
sl@0
   181
	mem_unlock,
sl@0
   182
	mem_ctrl
sl@0
   183
	};
sl@0
   184
#endif
sl@0
   185
EXPORT_C const STORE_METHOD *STORE_Memory(void)
sl@0
   186
	{
sl@0
   187
	return &store_memory;
sl@0
   188
	}
sl@0
   189
sl@0
   190
static int mem_init(STORE *s)
sl@0
   191
	{
sl@0
   192
	return 1;
sl@0
   193
	}
sl@0
   194
sl@0
   195
static void mem_clean(STORE *s)
sl@0
   196
	{
sl@0
   197
	return;
sl@0
   198
	}
sl@0
   199
sl@0
   200
static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   201
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
sl@0
   202
	{
sl@0
   203
	STOREerr(STORE_F_MEM_GENERATE, STORE_R_NOT_IMPLEMENTED);
sl@0
   204
	return 0;
sl@0
   205
	}
sl@0
   206
static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   207
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
sl@0
   208
	{
sl@0
   209
	void *context = mem_list_start(s, type, attributes, parameters);
sl@0
   210
	
sl@0
   211
	if (context)
sl@0
   212
		{
sl@0
   213
		STORE_OBJECT *object = mem_list_next(s, context);
sl@0
   214
sl@0
   215
		if (mem_list_end(s, context))
sl@0
   216
			return object;
sl@0
   217
		}
sl@0
   218
	return NULL;
sl@0
   219
	}
sl@0
   220
static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   221
	STORE_OBJECT *data, OPENSSL_ITEM attributes[],
sl@0
   222
	OPENSSL_ITEM parameters[])
sl@0
   223
	{
sl@0
   224
	STOREerr(STORE_F_MEM_STORE, STORE_R_NOT_IMPLEMENTED);
sl@0
   225
	return 0;
sl@0
   226
	}
sl@0
   227
static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   228
	OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
sl@0
   229
	OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
sl@0
   230
	OPENSSL_ITEM parameters[])
sl@0
   231
	{
sl@0
   232
	STOREerr(STORE_F_MEM_MODIFY, STORE_R_NOT_IMPLEMENTED);
sl@0
   233
	return 0;
sl@0
   234
	}
sl@0
   235
static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   236
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
sl@0
   237
	{
sl@0
   238
	STOREerr(STORE_F_MEM_DELETE, STORE_R_NOT_IMPLEMENTED);
sl@0
   239
	return 0;
sl@0
   240
	}
sl@0
   241
sl@0
   242
/* The list functions may be the hardest to understand.  Basically,
sl@0
   243
   mem_list_start compiles a stack of attribute info elements, and
sl@0
   244
   puts that stack into the context to be returned.  mem_list_next
sl@0
   245
   will then find the first matching element in the store, and then
sl@0
   246
   walk all the way to the end of the store (since any combination
sl@0
   247
   of attribute bits above the starting point may match the searched
sl@0
   248
   for bit pattern...). */
sl@0
   249
static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
sl@0
   250
	OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
sl@0
   251
	{
sl@0
   252
	struct mem_ctx_st *context =
sl@0
   253
		(struct mem_ctx_st *)OPENSSL_malloc(sizeof(struct mem_ctx_st));
sl@0
   254
	void *attribute_context = NULL;
sl@0
   255
	STORE_ATTR_INFO *attrs = NULL;
sl@0
   256
sl@0
   257
	if (!context)
sl@0
   258
		{
sl@0
   259
		STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE);
sl@0
   260
		return 0;
sl@0
   261
		}
sl@0
   262
	memset(context, 0, sizeof(struct mem_ctx_st));
sl@0
   263
sl@0
   264
	attribute_context = STORE_parse_attrs_start(attributes);
sl@0
   265
	if (!attribute_context)
sl@0
   266
		{
sl@0
   267
		STOREerr(STORE_F_MEM_LIST_START, ERR_R_STORE_LIB);
sl@0
   268
		goto err;
sl@0
   269
		}
sl@0
   270
sl@0
   271
	while((attrs = STORE_parse_attrs_next(attribute_context)))
sl@0
   272
		{
sl@0
   273
		if (context->search_attributes == NULL)
sl@0
   274
			{
sl@0
   275
			context->search_attributes =
sl@0
   276
				sk_new((int (*)(const char * const *, const char * const *))STORE_ATTR_INFO_compare);
sl@0
   277
			if (!context->search_attributes)
sl@0
   278
				{
sl@0
   279
				STOREerr(STORE_F_MEM_LIST_START,
sl@0
   280
					ERR_R_MALLOC_FAILURE);
sl@0
   281
				goto err;
sl@0
   282
				}
sl@0
   283
			}
sl@0
   284
		sk_push(context->search_attributes,(char *)attrs);
sl@0
   285
		}
sl@0
   286
	if (!STORE_parse_attrs_endp(attribute_context))
sl@0
   287
		goto err;
sl@0
   288
	STORE_parse_attrs_end(attribute_context);
sl@0
   289
	context->search_index = -1;
sl@0
   290
	context->index = -1;
sl@0
   291
	return context;
sl@0
   292
 err:
sl@0
   293
	if (attribute_context) STORE_parse_attrs_end(attribute_context);
sl@0
   294
	mem_list_end(s, context);
sl@0
   295
	return NULL;
sl@0
   296
	}
sl@0
   297
static STORE_OBJECT *mem_list_next(STORE *s, void *handle)
sl@0
   298
	{
sl@0
   299
	int i;
sl@0
   300
	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
sl@0
   301
	struct mem_object_data_st key = { 0, 0, 1 };
sl@0
   302
	struct mem_data_st *store =
sl@0
   303
		(struct mem_data_st *)STORE_get_ex_data(s, 1);
sl@0
   304
	int srch;
sl@0
   305
	int cres = 0;
sl@0
   306
sl@0
   307
	if (!context)
sl@0
   308
		{
sl@0
   309
		STOREerr(STORE_F_MEM_LIST_NEXT, ERR_R_PASSED_NULL_PARAMETER);
sl@0
   310
		return NULL;
sl@0
   311
		}
sl@0
   312
	if (!store)
sl@0
   313
		{
sl@0
   314
		STOREerr(STORE_F_MEM_LIST_NEXT, STORE_R_NO_STORE);
sl@0
   315
		return NULL;
sl@0
   316
		}
sl@0
   317
sl@0
   318
	if (context->search_index == -1)
sl@0
   319
		{
sl@0
   320
		for (i = 0; i < sk_num(context->search_attributes); i++)
sl@0
   321
			{
sl@0
   322
			key.attr_info =
sl@0
   323
				(STORE_ATTR_INFO *)sk_value(context->search_attributes, i);
sl@0
   324
			srch = sk_find_ex(store->data, (char *)&key);
sl@0
   325
sl@0
   326
			if (srch >= 0)
sl@0
   327
				{
sl@0
   328
				context->search_index = srch;
sl@0
   329
				break;
sl@0
   330
				}
sl@0
   331
			}
sl@0
   332
		}
sl@0
   333
	if (context->search_index < 0)
sl@0
   334
		return NULL;
sl@0
   335
	
sl@0
   336
	key.attr_info =
sl@0
   337
		(STORE_ATTR_INFO *)sk_value(context->search_attributes,
sl@0
   338
			context->search_index);
sl@0
   339
	for(srch = context->search_index;
sl@0
   340
	    srch < sk_num(store->data)
sl@0
   341
		    && STORE_ATTR_INFO_in_range(key.attr_info,
sl@0
   342
			    (STORE_ATTR_INFO *)sk_value(store->data, srch))
sl@0
   343
		    && !(cres = STORE_ATTR_INFO_in_ex(key.attr_info,
sl@0
   344
				 (STORE_ATTR_INFO *)sk_value(store->data, srch)));
sl@0
   345
	    srch++)
sl@0
   346
		;
sl@0
   347
sl@0
   348
	context->search_index = srch;
sl@0
   349
	if (cres)
sl@0
   350
		return ((struct mem_object_data_st *)sk_value(store->data,
sl@0
   351
				srch))->object;
sl@0
   352
	return NULL;
sl@0
   353
	}
sl@0
   354
static int mem_list_end(STORE *s, void *handle)
sl@0
   355
	{
sl@0
   356
	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
sl@0
   357
sl@0
   358
	if (!context)
sl@0
   359
		{
sl@0
   360
		STOREerr(STORE_F_MEM_LIST_END, ERR_R_PASSED_NULL_PARAMETER);
sl@0
   361
		return 0;
sl@0
   362
		}
sl@0
   363
	if (context && context->search_attributes)
sl@0
   364
		sk_free(context->search_attributes);
sl@0
   365
	if (context) OPENSSL_free(context);
sl@0
   366
	return 1;
sl@0
   367
	}
sl@0
   368
static int mem_list_endp(STORE *s, void *handle)
sl@0
   369
	{
sl@0
   370
	struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
sl@0
   371
sl@0
   372
	if (!context
sl@0
   373
		|| context->search_index == sk_num(context->search_attributes))
sl@0
   374
		return 1;
sl@0
   375
	return 0;
sl@0
   376
	}
sl@0
   377
static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
sl@0
   378
	OPENSSL_ITEM parameters[])
sl@0
   379
	{
sl@0
   380
	return 1;
sl@0
   381
	}
sl@0
   382
static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
sl@0
   383
	OPENSSL_ITEM parameters[])
sl@0
   384
	{
sl@0
   385
	return 1;
sl@0
   386
	}
sl@0
   387
static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void))
sl@0
   388
	{
sl@0
   389
	return 1;
sl@0
   390
	}