str_mem.c 13 KB

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