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