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