str_mem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #include <string.h>
  59. #include <openssl/err.h>
  60. #include "str_locl.h"
  61. /* The memory store is currently highly experimental. It's meant to become
  62. a base store used by other stores for internal caching (for full caching
  63. support, aging needs to be added).
  64. The database use is meant to support as much attribute association as
  65. possible, while providing for as small search ranges as possible.
  66. This is currently provided for by sorting the entries by numbers that
  67. are composed of bits set at the positions indicated by attribute type
  68. codes. This provides for ranges determined by the highest attribute
  69. type code value. A better idea might be to sort by values computed
  70. from the range of attributes associated with the object (basically,
  71. the difference between the highest and lowest attribute type code)
  72. and it's distance from a base (basically, the lowest associated
  73. attribute type code).
  74. */
  75. typedef struct mem_object_data_st
  76. {
  77. STORE_OBJECT *object;
  78. STORE_ATTR_INFO *attr_info;
  79. int references;
  80. } MEM_OBJECT_DATA;
  81. DECLARE_STACK_OF(MEM_OBJECT_DATA)
  82. struct mem_data_st
  83. {
  84. STACK_OF(MEM_OBJECT_DATA) *data; /* sorted with
  85. * STORE_ATTR_INFO_compare(). */
  86. unsigned int compute_components : 1; /* Currently unused, but can
  87. be used to add attributes
  88. from parts of the data. */
  89. };
  90. DECLARE_STACK_OF(STORE_ATTR_INFO)
  91. struct mem_ctx_st
  92. {
  93. int type; /* The type we're searching for */
  94. STACK_OF(STORE_ATTR_INFO) *search_attributes; /* Sets of
  95. attributes to search for. Each
  96. element is a STORE_ATTR_INFO. */
  97. int search_index; /* which of the search attributes we
  98. found a match for, -1 when we still
  99. haven't found any */
  100. int index; /* -1 as long as we're searching for
  101. the first */
  102. };
  103. static int mem_init(STORE *s);
  104. static void mem_clean(STORE *s);
  105. static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
  106. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
  107. static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
  108. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
  109. static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
  110. STORE_OBJECT *data, OPENSSL_ITEM attributes[],
  111. OPENSSL_ITEM parameters[]);
  112. static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
  113. OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
  114. OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
  115. OPENSSL_ITEM parameters[]);
  116. static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
  117. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
  118. static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
  119. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
  120. static STORE_OBJECT *mem_list_next(STORE *s, void *handle);
  121. static int mem_list_end(STORE *s, void *handle);
  122. static int mem_list_endp(STORE *s, void *handle);
  123. static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
  124. OPENSSL_ITEM parameters[]);
  125. static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
  126. OPENSSL_ITEM parameters[]);
  127. static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void));
  128. static STORE_METHOD store_memory =
  129. {
  130. "OpenSSL memory store interface",
  131. mem_init,
  132. mem_clean,
  133. mem_generate,
  134. mem_get,
  135. mem_store,
  136. mem_modify,
  137. NULL, /* revoke */
  138. mem_delete,
  139. mem_list_start,
  140. mem_list_next,
  141. mem_list_end,
  142. mem_list_endp,
  143. NULL, /* update */
  144. mem_lock,
  145. mem_unlock,
  146. mem_ctrl
  147. };
  148. const STORE_METHOD *STORE_Memory(void)
  149. {
  150. return &store_memory;
  151. }
  152. static int mem_init(STORE *s)
  153. {
  154. return 1;
  155. }
  156. static void mem_clean(STORE *s)
  157. {
  158. return;
  159. }
  160. static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
  161. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
  162. {
  163. STOREerr(STORE_F_MEM_GENERATE, STORE_R_NOT_IMPLEMENTED);
  164. return 0;
  165. }
  166. static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
  167. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
  168. {
  169. void *context = mem_list_start(s, type, attributes, parameters);
  170. if (context)
  171. {
  172. STORE_OBJECT *object = mem_list_next(s, context);
  173. if (mem_list_end(s, context))
  174. return object;
  175. }
  176. return NULL;
  177. }
  178. static int mem_store(STORE *s, STORE_OBJECT_TYPES type,
  179. STORE_OBJECT *data, OPENSSL_ITEM attributes[],
  180. OPENSSL_ITEM parameters[])
  181. {
  182. STOREerr(STORE_F_MEM_STORE, STORE_R_NOT_IMPLEMENTED);
  183. return 0;
  184. }
  185. static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
  186. OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
  187. OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
  188. OPENSSL_ITEM parameters[])
  189. {
  190. STOREerr(STORE_F_MEM_MODIFY, STORE_R_NOT_IMPLEMENTED);
  191. return 0;
  192. }
  193. static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
  194. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
  195. {
  196. STOREerr(STORE_F_MEM_DELETE, STORE_R_NOT_IMPLEMENTED);
  197. return 0;
  198. }
  199. /* The list functions may be the hardest to understand. Basically,
  200. mem_list_start compiles a stack of attribute info elements, and
  201. puts that stack into the context to be returned. mem_list_next
  202. will then find the first matching element in the store, and then
  203. walk all the way to the end of the store (since any combination
  204. of attribute bits above the starting point may match the searched
  205. for bit pattern...). */
  206. static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
  207. OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
  208. {
  209. struct mem_ctx_st *context =
  210. (struct mem_ctx_st *)OPENSSL_malloc(sizeof(struct mem_ctx_st));
  211. void *attribute_context = NULL;
  212. STORE_ATTR_INFO *attrs = NULL;
  213. if (!context)
  214. {
  215. STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE);
  216. return 0;
  217. }
  218. memset(context, 0, sizeof(struct mem_ctx_st));
  219. attribute_context = STORE_parse_attrs_start(attributes);
  220. if (!attribute_context)
  221. {
  222. STOREerr(STORE_F_MEM_LIST_START, ERR_R_STORE_LIB);
  223. goto err;
  224. }
  225. while((attrs = STORE_parse_attrs_next(attribute_context)))
  226. {
  227. if (context->search_attributes == NULL)
  228. {
  229. context->search_attributes =
  230. sk_STORE_ATTR_INFO_new(STORE_ATTR_INFO_compare);
  231. if (!context->search_attributes)
  232. {
  233. STOREerr(STORE_F_MEM_LIST_START,
  234. ERR_R_MALLOC_FAILURE);
  235. goto err;
  236. }
  237. }
  238. sk_STORE_ATTR_INFO_push(context->search_attributes,attrs);
  239. }
  240. if (!STORE_parse_attrs_endp(attribute_context))
  241. goto err;
  242. STORE_parse_attrs_end(attribute_context);
  243. context->search_index = -1;
  244. context->index = -1;
  245. return context;
  246. err:
  247. if (attribute_context) STORE_parse_attrs_end(attribute_context);
  248. mem_list_end(s, context);
  249. return NULL;
  250. }
  251. static STORE_OBJECT *mem_list_next(STORE *s, void *handle)
  252. {
  253. int i;
  254. struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
  255. struct mem_object_data_st key = { 0, 0, 1 };
  256. struct mem_data_st *store =
  257. (struct mem_data_st *)STORE_get_ex_data(s, 1);
  258. int srch;
  259. int cres = 0;
  260. if (!context)
  261. {
  262. STOREerr(STORE_F_MEM_LIST_NEXT, ERR_R_PASSED_NULL_PARAMETER);
  263. return NULL;
  264. }
  265. if (!store)
  266. {
  267. STOREerr(STORE_F_MEM_LIST_NEXT, STORE_R_NO_STORE);
  268. return NULL;
  269. }
  270. if (context->search_index == -1)
  271. {
  272. for (i = 0;
  273. i < sk_STORE_ATTR_INFO_num(context->search_attributes);
  274. i++)
  275. {
  276. key.attr_info
  277. = sk_STORE_ATTR_INFO_value(context->search_attributes,
  278. i);
  279. srch = sk_MEM_OBJECT_DATA_find_ex(store->data, &key);
  280. if (srch >= 0)
  281. {
  282. context->search_index = srch;
  283. break;
  284. }
  285. }
  286. }
  287. if (context->search_index < 0)
  288. return NULL;
  289. key.attr_info =
  290. sk_STORE_ATTR_INFO_value(context->search_attributes,
  291. context->search_index);
  292. for(srch = context->search_index;
  293. srch < sk_MEM_OBJECT_DATA_num(store->data)
  294. && STORE_ATTR_INFO_in_range(key.attr_info,
  295. sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info)
  296. && !(cres = STORE_ATTR_INFO_in_ex(key.attr_info,
  297. sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info));
  298. srch++)
  299. ;
  300. context->search_index = srch;
  301. if (cres)
  302. return (sk_MEM_OBJECT_DATA_value(store->data, srch))->object;
  303. return NULL;
  304. }
  305. static int mem_list_end(STORE *s, void *handle)
  306. {
  307. struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
  308. if (!context)
  309. {
  310. STOREerr(STORE_F_MEM_LIST_END, ERR_R_PASSED_NULL_PARAMETER);
  311. return 0;
  312. }
  313. if (context && context->search_attributes)
  314. sk_STORE_ATTR_INFO_free(context->search_attributes);
  315. if (context) OPENSSL_free(context);
  316. return 1;
  317. }
  318. static int mem_list_endp(STORE *s, void *handle)
  319. {
  320. struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
  321. if (!context
  322. || context->search_index
  323. == sk_STORE_ATTR_INFO_num(context->search_attributes))
  324. return 1;
  325. return 0;
  326. }
  327. static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
  328. OPENSSL_ITEM parameters[])
  329. {
  330. return 1;
  331. }
  332. static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
  333. OPENSSL_ITEM parameters[])
  334. {
  335. return 1;
  336. }
  337. static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void))
  338. {
  339. return 1;
  340. }