sl@0: /* crypto/store/str_mem.c -*- mode:C; c-file-style: "eay" -*- */ sl@0: /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL sl@0: * project 2003. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2003 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "str_locl.h" sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: /* The memory store is currently highly experimental. It's meant to become sl@0: a base store used by other stores for internal caching (for full caching sl@0: support, aging needs to be added). sl@0: sl@0: The database use is meant to support as much attribute association as sl@0: possible, while providing for as small search ranges as possible. sl@0: This is currently provided for by sorting the entries by numbers that sl@0: are composed of bits set at the positions indicated by attribute type sl@0: codes. This provides for ranges determined by the highest attribute sl@0: type code value. A better idea might be to sort by values computed sl@0: from the range of attributes associated with the object (basically, sl@0: the difference between the highest and lowest attribute type code) sl@0: and it's distance from a base (basically, the lowest associated sl@0: attribute type code). sl@0: */ sl@0: sl@0: struct mem_object_data_st sl@0: { sl@0: STORE_OBJECT *object; sl@0: STORE_ATTR_INFO *attr_info; sl@0: int references; sl@0: }; sl@0: sl@0: struct mem_data_st sl@0: { sl@0: STACK *data; /* A stack of mem_object_data_st, sl@0: sorted with STORE_ATTR_INFO_compare(). */ sl@0: unsigned int compute_components : 1; /* Currently unused, but can sl@0: be used to add attributes sl@0: from parts of the data. */ sl@0: }; sl@0: sl@0: struct mem_ctx_st sl@0: { sl@0: int type; /* The type we're searching for */ sl@0: STACK *search_attributes; /* Sets of attributes to search for. sl@0: Each element is a STORE_ATTR_INFO. */ sl@0: int search_index; /* which of the search attributes we found a match sl@0: for, -1 when we still haven't found any */ sl@0: int index; /* -1 as long as we're searching for the first */ sl@0: }; sl@0: sl@0: static int mem_init(STORE *s); sl@0: static void mem_clean(STORE *s); sl@0: static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); sl@0: static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); sl@0: static int mem_store(STORE *s, STORE_OBJECT_TYPES type, sl@0: STORE_OBJECT *data, OPENSSL_ITEM attributes[], sl@0: OPENSSL_ITEM parameters[]); sl@0: static int mem_modify(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[], sl@0: OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[], sl@0: OPENSSL_ITEM parameters[]); sl@0: static int mem_delete(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); sl@0: static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); sl@0: static STORE_OBJECT *mem_list_next(STORE *s, void *handle); sl@0: static int mem_list_end(STORE *s, void *handle); sl@0: static int mem_list_endp(STORE *s, void *handle); sl@0: static int mem_lock(STORE *s, OPENSSL_ITEM attributes[], sl@0: OPENSSL_ITEM parameters[]); sl@0: static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[], sl@0: OPENSSL_ITEM parameters[]); sl@0: static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void)); sl@0: sl@0: sl@0: #ifndef EMULATOR sl@0: static STORE_METHOD store_memory = sl@0: { sl@0: "OpenSSL memory store interface", sl@0: mem_init, sl@0: mem_clean, sl@0: mem_generate, sl@0: mem_get, sl@0: mem_store, sl@0: mem_modify, sl@0: NULL, /* revoke */ sl@0: mem_delete, sl@0: mem_list_start, sl@0: mem_list_next, sl@0: mem_list_end, sl@0: mem_list_endp, sl@0: NULL, /* update */ sl@0: mem_lock, sl@0: mem_unlock, sl@0: mem_ctrl sl@0: }; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(store_memory,str_mem,STORE_METHOD) sl@0: #define store_memory (*GET_WSD_VAR_NAME(store_memory,str_mem, s)()) sl@0: const STORE_METHOD temp_s_store_memory = sl@0: { sl@0: "OpenSSL memory store interface", sl@0: mem_init, sl@0: mem_clean, sl@0: mem_generate, sl@0: mem_get, sl@0: mem_store, sl@0: mem_modify, sl@0: NULL, /* revoke */ sl@0: mem_delete, sl@0: mem_list_start, sl@0: mem_list_next, sl@0: mem_list_end, sl@0: mem_list_endp, sl@0: NULL, /* update */ sl@0: mem_lock, sl@0: mem_unlock, sl@0: mem_ctrl sl@0: }; sl@0: #endif sl@0: EXPORT_C const STORE_METHOD *STORE_Memory(void) sl@0: { sl@0: return &store_memory; sl@0: } sl@0: sl@0: static int mem_init(STORE *s) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: static void mem_clean(STORE *s) sl@0: { sl@0: return; sl@0: } sl@0: sl@0: static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) sl@0: { sl@0: STOREerr(STORE_F_MEM_GENERATE, STORE_R_NOT_IMPLEMENTED); sl@0: return 0; sl@0: } sl@0: static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) sl@0: { sl@0: void *context = mem_list_start(s, type, attributes, parameters); sl@0: sl@0: if (context) sl@0: { sl@0: STORE_OBJECT *object = mem_list_next(s, context); sl@0: sl@0: if (mem_list_end(s, context)) sl@0: return object; sl@0: } sl@0: return NULL; sl@0: } sl@0: static int mem_store(STORE *s, STORE_OBJECT_TYPES type, sl@0: STORE_OBJECT *data, OPENSSL_ITEM attributes[], sl@0: OPENSSL_ITEM parameters[]) sl@0: { sl@0: STOREerr(STORE_F_MEM_STORE, STORE_R_NOT_IMPLEMENTED); sl@0: return 0; sl@0: } sl@0: static int mem_modify(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[], sl@0: OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[], sl@0: OPENSSL_ITEM parameters[]) sl@0: { sl@0: STOREerr(STORE_F_MEM_MODIFY, STORE_R_NOT_IMPLEMENTED); sl@0: return 0; sl@0: } sl@0: static int mem_delete(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) sl@0: { sl@0: STOREerr(STORE_F_MEM_DELETE, STORE_R_NOT_IMPLEMENTED); sl@0: return 0; sl@0: } sl@0: sl@0: /* The list functions may be the hardest to understand. Basically, sl@0: mem_list_start compiles a stack of attribute info elements, and sl@0: puts that stack into the context to be returned. mem_list_next sl@0: will then find the first matching element in the store, and then sl@0: walk all the way to the end of the store (since any combination sl@0: of attribute bits above the starting point may match the searched sl@0: for bit pattern...). */ sl@0: static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type, sl@0: OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) sl@0: { sl@0: struct mem_ctx_st *context = sl@0: (struct mem_ctx_st *)OPENSSL_malloc(sizeof(struct mem_ctx_st)); sl@0: void *attribute_context = NULL; sl@0: STORE_ATTR_INFO *attrs = NULL; sl@0: sl@0: if (!context) sl@0: { sl@0: STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: memset(context, 0, sizeof(struct mem_ctx_st)); sl@0: sl@0: attribute_context = STORE_parse_attrs_start(attributes); sl@0: if (!attribute_context) sl@0: { sl@0: STOREerr(STORE_F_MEM_LIST_START, ERR_R_STORE_LIB); sl@0: goto err; sl@0: } sl@0: sl@0: while((attrs = STORE_parse_attrs_next(attribute_context))) sl@0: { sl@0: if (context->search_attributes == NULL) sl@0: { sl@0: context->search_attributes = sl@0: sk_new((int (*)(const char * const *, const char * const *))STORE_ATTR_INFO_compare); sl@0: if (!context->search_attributes) sl@0: { sl@0: STOREerr(STORE_F_MEM_LIST_START, sl@0: ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: sk_push(context->search_attributes,(char *)attrs); sl@0: } sl@0: if (!STORE_parse_attrs_endp(attribute_context)) sl@0: goto err; sl@0: STORE_parse_attrs_end(attribute_context); sl@0: context->search_index = -1; sl@0: context->index = -1; sl@0: return context; sl@0: err: sl@0: if (attribute_context) STORE_parse_attrs_end(attribute_context); sl@0: mem_list_end(s, context); sl@0: return NULL; sl@0: } sl@0: static STORE_OBJECT *mem_list_next(STORE *s, void *handle) sl@0: { sl@0: int i; sl@0: struct mem_ctx_st *context = (struct mem_ctx_st *)handle; sl@0: struct mem_object_data_st key = { 0, 0, 1 }; sl@0: struct mem_data_st *store = sl@0: (struct mem_data_st *)STORE_get_ex_data(s, 1); sl@0: int srch; sl@0: int cres = 0; sl@0: sl@0: if (!context) sl@0: { sl@0: STOREerr(STORE_F_MEM_LIST_NEXT, ERR_R_PASSED_NULL_PARAMETER); sl@0: return NULL; sl@0: } sl@0: if (!store) sl@0: { sl@0: STOREerr(STORE_F_MEM_LIST_NEXT, STORE_R_NO_STORE); sl@0: return NULL; sl@0: } sl@0: sl@0: if (context->search_index == -1) sl@0: { sl@0: for (i = 0; i < sk_num(context->search_attributes); i++) sl@0: { sl@0: key.attr_info = sl@0: (STORE_ATTR_INFO *)sk_value(context->search_attributes, i); sl@0: srch = sk_find_ex(store->data, (char *)&key); sl@0: sl@0: if (srch >= 0) sl@0: { sl@0: context->search_index = srch; sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: if (context->search_index < 0) sl@0: return NULL; sl@0: sl@0: key.attr_info = sl@0: (STORE_ATTR_INFO *)sk_value(context->search_attributes, sl@0: context->search_index); sl@0: for(srch = context->search_index; sl@0: srch < sk_num(store->data) sl@0: && STORE_ATTR_INFO_in_range(key.attr_info, sl@0: (STORE_ATTR_INFO *)sk_value(store->data, srch)) sl@0: && !(cres = STORE_ATTR_INFO_in_ex(key.attr_info, sl@0: (STORE_ATTR_INFO *)sk_value(store->data, srch))); sl@0: srch++) sl@0: ; sl@0: sl@0: context->search_index = srch; sl@0: if (cres) sl@0: return ((struct mem_object_data_st *)sk_value(store->data, sl@0: srch))->object; sl@0: return NULL; sl@0: } sl@0: static int mem_list_end(STORE *s, void *handle) sl@0: { sl@0: struct mem_ctx_st *context = (struct mem_ctx_st *)handle; sl@0: sl@0: if (!context) sl@0: { sl@0: STOREerr(STORE_F_MEM_LIST_END, ERR_R_PASSED_NULL_PARAMETER); sl@0: return 0; sl@0: } sl@0: if (context && context->search_attributes) sl@0: sk_free(context->search_attributes); sl@0: if (context) OPENSSL_free(context); sl@0: return 1; sl@0: } sl@0: static int mem_list_endp(STORE *s, void *handle) sl@0: { sl@0: struct mem_ctx_st *context = (struct mem_ctx_st *)handle; sl@0: sl@0: if (!context sl@0: || context->search_index == sk_num(context->search_attributes)) sl@0: return 1; sl@0: return 0; sl@0: } sl@0: static int mem_lock(STORE *s, OPENSSL_ITEM attributes[], sl@0: OPENSSL_ITEM parameters[]) sl@0: { sl@0: return 1; sl@0: } sl@0: static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[], sl@0: OPENSSL_ITEM parameters[]) sl@0: { sl@0: return 1; sl@0: } sl@0: static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void)) sl@0: { sl@0: return 1; sl@0: }