str_lib.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. /* crypto/store/str_lib.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/bn.h>
  60. #include <openssl/err.h>
  61. #ifndef OPENSSL_NO_ENGINE
  62. #include <openssl/engine.h>
  63. #endif
  64. #include <openssl/sha.h>
  65. #include <openssl/x509.h>
  66. #include "str_locl.h"
  67. const char * const STORE_object_type_string[STORE_OBJECT_TYPE_NUM+1] =
  68. {
  69. 0,
  70. "X.509 Certificate",
  71. "X.509 CRL",
  72. "Private Key",
  73. "Public Key",
  74. "Number",
  75. "Arbitrary Data"
  76. };
  77. const int STORE_param_sizes[STORE_PARAM_TYPE_NUM+1] =
  78. {
  79. 0,
  80. sizeof(int), /* EVP_TYPE */
  81. sizeof(size_t), /* BITS */
  82. -1, /* KEY_PARAMETERS */
  83. 0 /* KEY_NO_PARAMETERS */
  84. };
  85. const int STORE_attr_sizes[STORE_ATTR_TYPE_NUM+1] =
  86. {
  87. 0,
  88. -1, /* FRIENDLYNAME: C string */
  89. SHA_DIGEST_LENGTH, /* KEYID: SHA1 digest, 160 bits */
  90. SHA_DIGEST_LENGTH, /* ISSUERKEYID: SHA1 digest, 160 bits */
  91. SHA_DIGEST_LENGTH, /* SUBJECTKEYID: SHA1 digest, 160 bits */
  92. SHA_DIGEST_LENGTH, /* ISSUERSERIALHASH: SHA1 digest, 160 bits */
  93. sizeof(X509_NAME *), /* ISSUER: X509_NAME * */
  94. sizeof(BIGNUM *), /* SERIAL: BIGNUM * */
  95. sizeof(X509_NAME *), /* SUBJECT: X509_NAME * */
  96. SHA_DIGEST_LENGTH, /* CERTHASH: SHA1 digest, 160 bits */
  97. -1, /* EMAIL: C string */
  98. -1, /* FILENAME: C string */
  99. };
  100. STORE *STORE_new_method(const STORE_METHOD *method)
  101. {
  102. STORE *ret;
  103. if (method == NULL)
  104. {
  105. STOREerr(STORE_F_STORE_NEW_METHOD,ERR_R_PASSED_NULL_PARAMETER);
  106. return NULL;
  107. }
  108. ret=(STORE *)OPENSSL_malloc(sizeof(STORE));
  109. if (ret == NULL)
  110. {
  111. STOREerr(STORE_F_STORE_NEW_METHOD,ERR_R_MALLOC_FAILURE);
  112. return NULL;
  113. }
  114. ret->meth=method;
  115. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_STORE, ret, &ret->ex_data);
  116. if (ret->meth->init && !ret->meth->init(ret))
  117. {
  118. STORE_free(ret);
  119. ret = NULL;
  120. }
  121. return ret;
  122. }
  123. STORE *STORE_new_engine(ENGINE *engine)
  124. {
  125. STORE *ret = NULL;
  126. ENGINE *e = engine;
  127. const STORE_METHOD *meth = 0;
  128. #ifdef OPENSSL_NO_ENGINE
  129. e = NULL;
  130. #else
  131. if (engine)
  132. {
  133. if (!ENGINE_init(engine))
  134. {
  135. STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
  136. return NULL;
  137. }
  138. e = engine;
  139. }
  140. else
  141. {
  142. STOREerr(STORE_F_STORE_NEW_ENGINE,ERR_R_PASSED_NULL_PARAMETER);
  143. return NULL;
  144. }
  145. if(e)
  146. {
  147. meth = ENGINE_get_STORE(e);
  148. if(!meth)
  149. {
  150. STOREerr(STORE_F_STORE_NEW_ENGINE,
  151. ERR_R_ENGINE_LIB);
  152. ENGINE_finish(e);
  153. return NULL;
  154. }
  155. }
  156. #endif
  157. ret = STORE_new_method(meth);
  158. if (ret == NULL)
  159. {
  160. STOREerr(STORE_F_STORE_NEW_ENGINE,ERR_R_STORE_LIB);
  161. return NULL;
  162. }
  163. ret->engine = e;
  164. return(ret);
  165. }
  166. void STORE_free(STORE *store)
  167. {
  168. if (store == NULL)
  169. return;
  170. if (store->meth->clean)
  171. store->meth->clean(store);
  172. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_STORE, store, &store->ex_data);
  173. OPENSSL_free(store);
  174. }
  175. int STORE_ctrl(STORE *store, int cmd, long i, void *p, void (*f)(void))
  176. {
  177. if (store == NULL)
  178. {
  179. STOREerr(STORE_F_STORE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
  180. return 0;
  181. }
  182. if (store->meth->ctrl)
  183. return store->meth->ctrl(store, cmd, i, p, f);
  184. STOREerr(STORE_F_STORE_CTRL,STORE_R_NO_CONTROL_FUNCTION);
  185. return 0;
  186. }
  187. int STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  188. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  189. {
  190. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_STORE, argl, argp,
  191. new_func, dup_func, free_func);
  192. }
  193. int STORE_set_ex_data(STORE *r, int idx, void *arg)
  194. {
  195. return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
  196. }
  197. void *STORE_get_ex_data(STORE *r, int idx)
  198. {
  199. return(CRYPTO_get_ex_data(&r->ex_data,idx));
  200. }
  201. const STORE_METHOD *STORE_get_method(STORE *store)
  202. {
  203. return store->meth;
  204. }
  205. const STORE_METHOD *STORE_set_method(STORE *store, const STORE_METHOD *meth)
  206. {
  207. store->meth=meth;
  208. return store->meth;
  209. }
  210. /* API helpers */
  211. #define check_store(s,fncode,fnname,fnerrcode) \
  212. do \
  213. { \
  214. if ((s) == NULL || (s)->meth) \
  215. { \
  216. STOREerr((fncode), ERR_R_PASSED_NULL_PARAMETER); \
  217. return 0; \
  218. } \
  219. if ((s)->meth->fnname == NULL) \
  220. { \
  221. STOREerr((fncode), (fnerrcode)); \
  222. return 0; \
  223. } \
  224. } \
  225. while(0)
  226. /* API functions */
  227. X509 *STORE_get_certificate(STORE *s, OPENSSL_ITEM attributes[],
  228. OPENSSL_ITEM parameters[])
  229. {
  230. STORE_OBJECT *object;
  231. X509 *x;
  232. check_store(s,STORE_F_STORE_GET_CERTIFICATE,
  233. get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
  234. object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
  235. attributes, parameters);
  236. if (!object || !object->data.x509.certificate)
  237. {
  238. STOREerr(STORE_F_STORE_GET_CERTIFICATE,
  239. STORE_R_FAILED_GETTING_CERTIFICATE);
  240. return 0;
  241. }
  242. CRYPTO_add(&object->data.x509.certificate->references,1,CRYPTO_LOCK_X509);
  243. #ifdef REF_PRINT
  244. REF_PRINT("X509",data);
  245. #endif
  246. x = object->data.x509.certificate;
  247. STORE_OBJECT_free(object);
  248. return x;
  249. }
  250. int STORE_store_certificate(STORE *s, X509 *data, OPENSSL_ITEM attributes[],
  251. OPENSSL_ITEM parameters[])
  252. {
  253. STORE_OBJECT *object;
  254. int i;
  255. check_store(s,STORE_F_STORE_CERTIFICATE,
  256. store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
  257. object = STORE_OBJECT_new();
  258. if (!object)
  259. {
  260. STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
  261. ERR_R_MALLOC_FAILURE);
  262. return 0;
  263. }
  264. CRYPTO_add(&data->references,1,CRYPTO_LOCK_X509);
  265. #ifdef REF_PRINT
  266. REF_PRINT("X509",data);
  267. #endif
  268. object->data.x509.certificate = data;
  269. i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
  270. object, attributes, parameters);
  271. STORE_OBJECT_free(object);
  272. if (!i)
  273. {
  274. STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
  275. STORE_R_FAILED_STORING_CERTIFICATE);
  276. return 0;
  277. }
  278. return 1;
  279. }
  280. int STORE_modify_certificate(STORE *s, OPENSSL_ITEM search_attributes[],
  281. OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  282. OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  283. {
  284. check_store(s,STORE_F_STORE_MODIFY_CERTIFICATE,
  285. modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  286. if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
  287. search_attributes, add_attributes, modify_attributes,
  288. delete_attributes, parameters))
  289. {
  290. STOREerr(STORE_F_STORE_MODIFY_CERTIFICATE,
  291. STORE_R_FAILED_MODIFYING_CERTIFICATE);
  292. return 0;
  293. }
  294. return 1;
  295. }
  296. int STORE_revoke_certificate(STORE *s, OPENSSL_ITEM attributes[],
  297. OPENSSL_ITEM parameters[])
  298. {
  299. check_store(s,STORE_F_STORE_REVOKE_CERTIFICATE,
  300. revoke_object,STORE_R_NO_REVOKE_OBJECT_FUNCTION);
  301. if (!s->meth->revoke_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
  302. attributes, parameters))
  303. {
  304. STOREerr(STORE_F_STORE_REVOKE_CERTIFICATE,
  305. STORE_R_FAILED_REVOKING_CERTIFICATE);
  306. return 0;
  307. }
  308. return 1;
  309. }
  310. int STORE_delete_certificate(STORE *s, OPENSSL_ITEM attributes[],
  311. OPENSSL_ITEM parameters[])
  312. {
  313. check_store(s,STORE_F_STORE_DELETE_CERTIFICATE,
  314. delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
  315. if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
  316. attributes, parameters))
  317. {
  318. STOREerr(STORE_F_STORE_DELETE_CERTIFICATE,
  319. STORE_R_FAILED_DELETING_CERTIFICATE);
  320. return 0;
  321. }
  322. return 1;
  323. }
  324. void *STORE_list_certificate_start(STORE *s, OPENSSL_ITEM attributes[],
  325. OPENSSL_ITEM parameters[])
  326. {
  327. void *handle;
  328. check_store(s,STORE_F_STORE_LIST_CERTIFICATE_START,
  329. list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
  330. handle = s->meth->list_object_start(s,
  331. STORE_OBJECT_TYPE_X509_CERTIFICATE, attributes, parameters);
  332. if (!handle)
  333. {
  334. STOREerr(STORE_F_STORE_LIST_CERTIFICATE_START,
  335. STORE_R_FAILED_LISTING_CERTIFICATES);
  336. return 0;
  337. }
  338. return handle;
  339. }
  340. X509 *STORE_list_certificate_next(STORE *s, void *handle)
  341. {
  342. STORE_OBJECT *object;
  343. X509 *x;
  344. check_store(s,STORE_F_STORE_LIST_CERTIFICATE_NEXT,
  345. list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
  346. object = s->meth->list_object_next(s, handle);
  347. if (!object || !object->data.x509.certificate)
  348. {
  349. STOREerr(STORE_F_STORE_LIST_CERTIFICATE_NEXT,
  350. STORE_R_FAILED_LISTING_CERTIFICATES);
  351. return 0;
  352. }
  353. CRYPTO_add(&object->data.x509.certificate->references,1,CRYPTO_LOCK_X509);
  354. #ifdef REF_PRINT
  355. REF_PRINT("X509",data);
  356. #endif
  357. x = object->data.x509.certificate;
  358. STORE_OBJECT_free(object);
  359. return x;
  360. }
  361. int STORE_list_certificate_end(STORE *s, void *handle)
  362. {
  363. check_store(s,STORE_F_STORE_LIST_CERTIFICATE_END,
  364. list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
  365. if (!s->meth->list_object_end(s, handle))
  366. {
  367. STOREerr(STORE_F_STORE_LIST_CERTIFICATE_END,
  368. STORE_R_FAILED_LISTING_CERTIFICATES);
  369. return 0;
  370. }
  371. return 1;
  372. }
  373. int STORE_list_certificate_endp(STORE *s, void *handle)
  374. {
  375. check_store(s,STORE_F_STORE_LIST_CERTIFICATE_ENDP,
  376. list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
  377. if (!s->meth->list_object_endp(s, handle))
  378. {
  379. STOREerr(STORE_F_STORE_LIST_CERTIFICATE_ENDP,
  380. STORE_R_FAILED_LISTING_CERTIFICATES);
  381. return 0;
  382. }
  383. return 1;
  384. }
  385. EVP_PKEY *STORE_generate_key(STORE *s, OPENSSL_ITEM attributes[],
  386. OPENSSL_ITEM parameters[])
  387. {
  388. STORE_OBJECT *object;
  389. EVP_PKEY *pkey;
  390. check_store(s,STORE_F_STORE_GENERATE_KEY,
  391. generate_object,STORE_R_NO_GENERATE_OBJECT_FUNCTION);
  392. object = s->meth->generate_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
  393. attributes, parameters);
  394. if (!object || !object->data.key)
  395. {
  396. STOREerr(STORE_F_STORE_GENERATE_KEY,
  397. STORE_R_FAILED_GENERATING_KEY);
  398. return 0;
  399. }
  400. CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
  401. #ifdef REF_PRINT
  402. REF_PRINT("EVP_PKEY",data);
  403. #endif
  404. pkey = object->data.key;
  405. STORE_OBJECT_free(object);
  406. return pkey;
  407. }
  408. EVP_PKEY *STORE_get_private_key(STORE *s, OPENSSL_ITEM attributes[],
  409. OPENSSL_ITEM parameters[])
  410. {
  411. STORE_OBJECT *object;
  412. EVP_PKEY *pkey;
  413. check_store(s,STORE_F_STORE_GET_PRIVATE_KEY,
  414. get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
  415. object = s->meth->get_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
  416. attributes, parameters);
  417. if (!object || !object->data.key || !object->data.key)
  418. {
  419. STOREerr(STORE_F_STORE_GET_PRIVATE_KEY,
  420. STORE_R_FAILED_GETTING_KEY);
  421. return 0;
  422. }
  423. CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
  424. #ifdef REF_PRINT
  425. REF_PRINT("EVP_PKEY",data);
  426. #endif
  427. pkey = object->data.key;
  428. STORE_OBJECT_free(object);
  429. return pkey;
  430. }
  431. int STORE_store_private_key(STORE *s, EVP_PKEY *data, OPENSSL_ITEM attributes[],
  432. OPENSSL_ITEM parameters[])
  433. {
  434. STORE_OBJECT *object;
  435. int i;
  436. check_store(s,STORE_F_STORE_STORE_PRIVATE_KEY,
  437. store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
  438. object = STORE_OBJECT_new();
  439. if (!object)
  440. {
  441. STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
  442. ERR_R_MALLOC_FAILURE);
  443. return 0;
  444. }
  445. object->data.key = EVP_PKEY_new();
  446. if (!object->data.key)
  447. {
  448. STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
  449. ERR_R_MALLOC_FAILURE);
  450. return 0;
  451. }
  452. CRYPTO_add(&data->references,1,CRYPTO_LOCK_EVP_PKEY);
  453. #ifdef REF_PRINT
  454. REF_PRINT("EVP_PKEY",data);
  455. #endif
  456. object->data.key = data;
  457. i = s->meth->store_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY, object,
  458. attributes, parameters);
  459. STORE_OBJECT_free(object);
  460. if (!i)
  461. {
  462. STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
  463. STORE_R_FAILED_STORING_KEY);
  464. return 0;
  465. }
  466. return i;
  467. }
  468. int STORE_modify_private_key(STORE *s, OPENSSL_ITEM search_attributes[],
  469. OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  470. OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  471. {
  472. check_store(s,STORE_F_STORE_MODIFY_PRIVATE_KEY,
  473. modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  474. if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
  475. search_attributes, add_attributes, modify_attributes,
  476. delete_attributes, parameters))
  477. {
  478. STOREerr(STORE_F_STORE_MODIFY_PRIVATE_KEY,
  479. STORE_R_FAILED_MODIFYING_PRIVATE_KEY);
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. int STORE_revoke_private_key(STORE *s, OPENSSL_ITEM attributes[],
  485. OPENSSL_ITEM parameters[])
  486. {
  487. int i;
  488. check_store(s,STORE_F_STORE_REVOKE_PRIVATE_KEY,
  489. revoke_object,STORE_R_NO_REVOKE_OBJECT_FUNCTION);
  490. i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
  491. attributes, parameters);
  492. if (!i)
  493. {
  494. STOREerr(STORE_F_STORE_REVOKE_PRIVATE_KEY,
  495. STORE_R_FAILED_REVOKING_KEY);
  496. return 0;
  497. }
  498. return i;
  499. }
  500. int STORE_delete_private_key(STORE *s, OPENSSL_ITEM attributes[],
  501. OPENSSL_ITEM parameters[])
  502. {
  503. check_store(s,STORE_F_STORE_DELETE_PRIVATE_KEY,
  504. delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
  505. if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
  506. attributes, parameters))
  507. {
  508. STOREerr(STORE_F_STORE_DELETE_PRIVATE_KEY,
  509. STORE_R_FAILED_DELETING_KEY);
  510. return 0;
  511. }
  512. return 1;
  513. }
  514. void *STORE_list_private_key_start(STORE *s, OPENSSL_ITEM attributes[],
  515. OPENSSL_ITEM parameters[])
  516. {
  517. void *handle;
  518. check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_START,
  519. list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
  520. handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
  521. attributes, parameters);
  522. if (!handle)
  523. {
  524. STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_START,
  525. STORE_R_FAILED_LISTING_KEYS);
  526. return 0;
  527. }
  528. return handle;
  529. }
  530. EVP_PKEY *STORE_list_private_key_next(STORE *s, void *handle)
  531. {
  532. STORE_OBJECT *object;
  533. EVP_PKEY *pkey;
  534. check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
  535. list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
  536. object = s->meth->list_object_next(s, handle);
  537. if (!object || !object->data.key || !object->data.key)
  538. {
  539. STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
  540. STORE_R_FAILED_LISTING_KEYS);
  541. return 0;
  542. }
  543. CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
  544. #ifdef REF_PRINT
  545. REF_PRINT("EVP_PKEY",data);
  546. #endif
  547. pkey = object->data.key;
  548. STORE_OBJECT_free(object);
  549. return pkey;
  550. }
  551. int STORE_list_private_key_end(STORE *s, void *handle)
  552. {
  553. check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_END,
  554. list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
  555. if (!s->meth->list_object_end(s, handle))
  556. {
  557. STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_END,
  558. STORE_R_FAILED_LISTING_KEYS);
  559. return 0;
  560. }
  561. return 1;
  562. }
  563. int STORE_list_private_key_endp(STORE *s, void *handle)
  564. {
  565. check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
  566. list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
  567. if (!s->meth->list_object_endp(s, handle))
  568. {
  569. STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
  570. STORE_R_FAILED_LISTING_KEYS);
  571. return 0;
  572. }
  573. return 1;
  574. }
  575. EVP_PKEY *STORE_get_public_key(STORE *s, OPENSSL_ITEM attributes[],
  576. OPENSSL_ITEM parameters[])
  577. {
  578. STORE_OBJECT *object;
  579. EVP_PKEY *pkey;
  580. check_store(s,STORE_F_STORE_GET_PUBLIC_KEY,
  581. get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
  582. object = s->meth->get_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
  583. attributes, parameters);
  584. if (!object || !object->data.key || !object->data.key)
  585. {
  586. STOREerr(STORE_F_STORE_GET_PUBLIC_KEY,
  587. STORE_R_FAILED_GETTING_KEY);
  588. return 0;
  589. }
  590. CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
  591. #ifdef REF_PRINT
  592. REF_PRINT("EVP_PKEY",data);
  593. #endif
  594. pkey = object->data.key;
  595. STORE_OBJECT_free(object);
  596. return pkey;
  597. }
  598. int STORE_store_public_key(STORE *s, EVP_PKEY *data, OPENSSL_ITEM attributes[],
  599. OPENSSL_ITEM parameters[])
  600. {
  601. STORE_OBJECT *object;
  602. int i;
  603. check_store(s,STORE_F_STORE_STORE_PUBLIC_KEY,
  604. store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
  605. object = STORE_OBJECT_new();
  606. if (!object)
  607. {
  608. STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
  609. ERR_R_MALLOC_FAILURE);
  610. return 0;
  611. }
  612. object->data.key = EVP_PKEY_new();
  613. if (!object->data.key)
  614. {
  615. STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
  616. ERR_R_MALLOC_FAILURE);
  617. return 0;
  618. }
  619. CRYPTO_add(&data->references,1,CRYPTO_LOCK_EVP_PKEY);
  620. #ifdef REF_PRINT
  621. REF_PRINT("EVP_PKEY",data);
  622. #endif
  623. object->data.key = data;
  624. i = s->meth->store_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY, object,
  625. attributes, parameters);
  626. STORE_OBJECT_free(object);
  627. if (!i)
  628. {
  629. STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
  630. STORE_R_FAILED_STORING_KEY);
  631. return 0;
  632. }
  633. return i;
  634. }
  635. int STORE_modify_public_key(STORE *s, OPENSSL_ITEM search_attributes[],
  636. OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  637. OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  638. {
  639. check_store(s,STORE_F_STORE_MODIFY_PUBLIC_KEY,
  640. modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  641. if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
  642. search_attributes, add_attributes, modify_attributes,
  643. delete_attributes, parameters))
  644. {
  645. STOREerr(STORE_F_STORE_MODIFY_PUBLIC_KEY,
  646. STORE_R_FAILED_MODIFYING_PUBLIC_KEY);
  647. return 0;
  648. }
  649. return 1;
  650. }
  651. int STORE_revoke_public_key(STORE *s, OPENSSL_ITEM attributes[],
  652. OPENSSL_ITEM parameters[])
  653. {
  654. int i;
  655. check_store(s,STORE_F_STORE_REVOKE_PUBLIC_KEY,
  656. revoke_object,STORE_R_NO_REVOKE_OBJECT_FUNCTION);
  657. i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
  658. attributes, parameters);
  659. if (!i)
  660. {
  661. STOREerr(STORE_F_STORE_REVOKE_PUBLIC_KEY,
  662. STORE_R_FAILED_REVOKING_KEY);
  663. return 0;
  664. }
  665. return i;
  666. }
  667. int STORE_delete_public_key(STORE *s, OPENSSL_ITEM attributes[],
  668. OPENSSL_ITEM parameters[])
  669. {
  670. check_store(s,STORE_F_STORE_DELETE_PUBLIC_KEY,
  671. delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
  672. if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
  673. attributes, parameters))
  674. {
  675. STOREerr(STORE_F_STORE_DELETE_PUBLIC_KEY,
  676. STORE_R_FAILED_DELETING_KEY);
  677. return 0;
  678. }
  679. return 1;
  680. }
  681. void *STORE_list_public_key_start(STORE *s, OPENSSL_ITEM attributes[],
  682. OPENSSL_ITEM parameters[])
  683. {
  684. void *handle;
  685. check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_START,
  686. list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
  687. handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
  688. attributes, parameters);
  689. if (!handle)
  690. {
  691. STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_START,
  692. STORE_R_FAILED_LISTING_KEYS);
  693. return 0;
  694. }
  695. return handle;
  696. }
  697. EVP_PKEY *STORE_list_public_key_next(STORE *s, void *handle)
  698. {
  699. STORE_OBJECT *object;
  700. EVP_PKEY *pkey;
  701. check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
  702. list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
  703. object = s->meth->list_object_next(s, handle);
  704. if (!object || !object->data.key || !object->data.key)
  705. {
  706. STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
  707. STORE_R_FAILED_LISTING_KEYS);
  708. return 0;
  709. }
  710. CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
  711. #ifdef REF_PRINT
  712. REF_PRINT("EVP_PKEY",data);
  713. #endif
  714. pkey = object->data.key;
  715. STORE_OBJECT_free(object);
  716. return pkey;
  717. }
  718. int STORE_list_public_key_end(STORE *s, void *handle)
  719. {
  720. check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_END,
  721. list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
  722. if (!s->meth->list_object_end(s, handle))
  723. {
  724. STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_END,
  725. STORE_R_FAILED_LISTING_KEYS);
  726. return 0;
  727. }
  728. return 1;
  729. }
  730. int STORE_list_public_key_endp(STORE *s, void *handle)
  731. {
  732. check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
  733. list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
  734. if (!s->meth->list_object_endp(s, handle))
  735. {
  736. STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
  737. STORE_R_FAILED_LISTING_KEYS);
  738. return 0;
  739. }
  740. return 1;
  741. }
  742. X509_CRL *STORE_generate_crl(STORE *s, OPENSSL_ITEM attributes[],
  743. OPENSSL_ITEM parameters[])
  744. {
  745. STORE_OBJECT *object;
  746. X509_CRL *crl;
  747. check_store(s,STORE_F_STORE_GENERATE_CRL,
  748. generate_object,STORE_R_NO_GENERATE_CRL_FUNCTION);
  749. object = s->meth->generate_object(s, STORE_OBJECT_TYPE_X509_CRL,
  750. attributes, parameters);
  751. if (!object || !object->data.crl)
  752. {
  753. STOREerr(STORE_F_STORE_GENERATE_CRL,
  754. STORE_R_FAILED_GENERATING_CRL);
  755. return 0;
  756. }
  757. CRYPTO_add(&object->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
  758. #ifdef REF_PRINT
  759. REF_PRINT("X509_CRL",data);
  760. #endif
  761. crl = object->data.crl;
  762. STORE_OBJECT_free(object);
  763. return crl;
  764. }
  765. X509_CRL *STORE_get_crl(STORE *s, OPENSSL_ITEM attributes[],
  766. OPENSSL_ITEM parameters[])
  767. {
  768. STORE_OBJECT *object;
  769. X509_CRL *crl;
  770. check_store(s,STORE_F_STORE_GET_CRL,
  771. get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
  772. object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CRL,
  773. attributes, parameters);
  774. if (!object || !object->data.crl)
  775. {
  776. STOREerr(STORE_F_STORE_GET_CRL,
  777. STORE_R_FAILED_GETTING_KEY);
  778. return 0;
  779. }
  780. CRYPTO_add(&object->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
  781. #ifdef REF_PRINT
  782. REF_PRINT("X509_CRL",data);
  783. #endif
  784. crl = object->data.crl;
  785. STORE_OBJECT_free(object);
  786. return crl;
  787. }
  788. int STORE_store_crl(STORE *s, X509_CRL *data, OPENSSL_ITEM attributes[],
  789. OPENSSL_ITEM parameters[])
  790. {
  791. STORE_OBJECT *object;
  792. int i;
  793. check_store(s,STORE_F_STORE_STORE_CRL,
  794. store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
  795. object = STORE_OBJECT_new();
  796. if (!object)
  797. {
  798. STOREerr(STORE_F_STORE_STORE_CRL,
  799. ERR_R_MALLOC_FAILURE);
  800. return 0;
  801. }
  802. CRYPTO_add(&data->references,1,CRYPTO_LOCK_X509_CRL);
  803. #ifdef REF_PRINT
  804. REF_PRINT("X509_CRL",data);
  805. #endif
  806. object->data.crl = data;
  807. i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CRL, object,
  808. attributes, parameters);
  809. STORE_OBJECT_free(object);
  810. if (!i)
  811. {
  812. STOREerr(STORE_F_STORE_STORE_CRL,
  813. STORE_R_FAILED_STORING_KEY);
  814. return 0;
  815. }
  816. return i;
  817. }
  818. int STORE_modify_crl(STORE *s, OPENSSL_ITEM search_attributes[],
  819. OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  820. OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  821. {
  822. check_store(s,STORE_F_STORE_MODIFY_CRL,
  823. modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  824. if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CRL,
  825. search_attributes, add_attributes, modify_attributes,
  826. delete_attributes, parameters))
  827. {
  828. STOREerr(STORE_F_STORE_MODIFY_CRL,
  829. STORE_R_FAILED_MODIFYING_CRL);
  830. return 0;
  831. }
  832. return 1;
  833. }
  834. int STORE_delete_crl(STORE *s, OPENSSL_ITEM attributes[],
  835. OPENSSL_ITEM parameters[])
  836. {
  837. check_store(s,STORE_F_STORE_DELETE_CRL,
  838. delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
  839. if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CRL,
  840. attributes, parameters))
  841. {
  842. STOREerr(STORE_F_STORE_DELETE_CRL,
  843. STORE_R_FAILED_DELETING_KEY);
  844. return 0;
  845. }
  846. return 1;
  847. }
  848. void *STORE_list_crl_start(STORE *s, OPENSSL_ITEM attributes[],
  849. OPENSSL_ITEM parameters[])
  850. {
  851. void *handle;
  852. check_store(s,STORE_F_STORE_LIST_CRL_START,
  853. list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
  854. handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_X509_CRL,
  855. attributes, parameters);
  856. if (!handle)
  857. {
  858. STOREerr(STORE_F_STORE_LIST_CRL_START,
  859. STORE_R_FAILED_LISTING_KEYS);
  860. return 0;
  861. }
  862. return handle;
  863. }
  864. X509_CRL *STORE_list_crl_next(STORE *s, void *handle)
  865. {
  866. STORE_OBJECT *object;
  867. X509_CRL *crl;
  868. check_store(s,STORE_F_STORE_LIST_CRL_NEXT,
  869. list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
  870. object = s->meth->list_object_next(s, handle);
  871. if (!object || !object->data.crl)
  872. {
  873. STOREerr(STORE_F_STORE_LIST_CRL_NEXT,
  874. STORE_R_FAILED_LISTING_KEYS);
  875. return 0;
  876. }
  877. CRYPTO_add(&object->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
  878. #ifdef REF_PRINT
  879. REF_PRINT("X509_CRL",data);
  880. #endif
  881. crl = object->data.crl;
  882. STORE_OBJECT_free(object);
  883. return crl;
  884. }
  885. int STORE_list_crl_end(STORE *s, void *handle)
  886. {
  887. check_store(s,STORE_F_STORE_LIST_CRL_END,
  888. list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
  889. if (!s->meth->list_object_end(s, handle))
  890. {
  891. STOREerr(STORE_F_STORE_LIST_CRL_END,
  892. STORE_R_FAILED_LISTING_KEYS);
  893. return 0;
  894. }
  895. return 1;
  896. }
  897. int STORE_list_crl_endp(STORE *s, void *handle)
  898. {
  899. check_store(s,STORE_F_STORE_LIST_CRL_ENDP,
  900. list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
  901. if (!s->meth->list_object_endp(s, handle))
  902. {
  903. STOREerr(STORE_F_STORE_LIST_CRL_ENDP,
  904. STORE_R_FAILED_LISTING_KEYS);
  905. return 0;
  906. }
  907. return 1;
  908. }
  909. int STORE_store_number(STORE *s, BIGNUM *data, OPENSSL_ITEM attributes[],
  910. OPENSSL_ITEM parameters[])
  911. {
  912. STORE_OBJECT *object;
  913. int i;
  914. check_store(s,STORE_F_STORE_STORE_NUMBER,
  915. store_object,STORE_R_NO_STORE_OBJECT_NUMBER_FUNCTION);
  916. object = STORE_OBJECT_new();
  917. if (!object)
  918. {
  919. STOREerr(STORE_F_STORE_STORE_NUMBER,
  920. ERR_R_MALLOC_FAILURE);
  921. return 0;
  922. }
  923. object->data.number = data;
  924. i = s->meth->store_object(s, STORE_OBJECT_TYPE_NUMBER, object,
  925. attributes, parameters);
  926. STORE_OBJECT_free(object);
  927. if (!i)
  928. {
  929. STOREerr(STORE_F_STORE_STORE_NUMBER,
  930. STORE_R_FAILED_STORING_NUMBER);
  931. return 0;
  932. }
  933. return 1;
  934. }
  935. int STORE_modify_number(STORE *s, OPENSSL_ITEM search_attributes[],
  936. OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  937. OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  938. {
  939. check_store(s,STORE_F_STORE_MODIFY_NUMBER,
  940. modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  941. if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_NUMBER,
  942. search_attributes, add_attributes, modify_attributes,
  943. delete_attributes, parameters))
  944. {
  945. STOREerr(STORE_F_STORE_MODIFY_NUMBER,
  946. STORE_R_FAILED_MODIFYING_NUMBER);
  947. return 0;
  948. }
  949. return 1;
  950. }
  951. BIGNUM *STORE_get_number(STORE *s, OPENSSL_ITEM attributes[],
  952. OPENSSL_ITEM parameters[])
  953. {
  954. STORE_OBJECT *object;
  955. BIGNUM *n;
  956. check_store(s,STORE_F_STORE_GET_NUMBER,
  957. get_object,STORE_R_NO_GET_OBJECT_NUMBER_FUNCTION);
  958. object = s->meth->get_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
  959. parameters);
  960. if (!object || !object->data.number)
  961. {
  962. STOREerr(STORE_F_STORE_GET_NUMBER,
  963. STORE_R_FAILED_GETTING_NUMBER);
  964. return 0;
  965. }
  966. n = object->data.number;
  967. object->data.number = NULL;
  968. STORE_OBJECT_free(object);
  969. return n;
  970. }
  971. int STORE_delete_number(STORE *s, OPENSSL_ITEM attributes[],
  972. OPENSSL_ITEM parameters[])
  973. {
  974. check_store(s,STORE_F_STORE_DELETE_NUMBER,
  975. delete_object,STORE_R_NO_DELETE_NUMBER_FUNCTION);
  976. if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
  977. parameters))
  978. {
  979. STOREerr(STORE_F_STORE_DELETE_NUMBER,
  980. STORE_R_FAILED_DELETING_NUMBER);
  981. return 0;
  982. }
  983. return 1;
  984. }
  985. int STORE_store_arbitrary(STORE *s, BUF_MEM *data, OPENSSL_ITEM attributes[],
  986. OPENSSL_ITEM parameters[])
  987. {
  988. STORE_OBJECT *object;
  989. int i;
  990. check_store(s,STORE_F_STORE_STORE_ARBITRARY,
  991. store_object,STORE_R_NO_STORE_OBJECT_ARBITRARY_FUNCTION);
  992. object = STORE_OBJECT_new();
  993. if (!object)
  994. {
  995. STOREerr(STORE_F_STORE_STORE_ARBITRARY,
  996. ERR_R_MALLOC_FAILURE);
  997. return 0;
  998. }
  999. object->data.arbitrary = data;
  1000. i = s->meth->store_object(s, STORE_OBJECT_TYPE_ARBITRARY, object,
  1001. attributes, parameters);
  1002. STORE_OBJECT_free(object);
  1003. if (!i)
  1004. {
  1005. STOREerr(STORE_F_STORE_STORE_ARBITRARY,
  1006. STORE_R_FAILED_STORING_ARBITRARY);
  1007. return 0;
  1008. }
  1009. return 1;
  1010. }
  1011. int STORE_modify_arbitrary(STORE *s, OPENSSL_ITEM search_attributes[],
  1012. OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  1013. OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  1014. {
  1015. check_store(s,STORE_F_STORE_MODIFY_ARBITRARY,
  1016. modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  1017. if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_ARBITRARY,
  1018. search_attributes, add_attributes, modify_attributes,
  1019. delete_attributes, parameters))
  1020. {
  1021. STOREerr(STORE_F_STORE_MODIFY_ARBITRARY,
  1022. STORE_R_FAILED_MODIFYING_ARBITRARY);
  1023. return 0;
  1024. }
  1025. return 1;
  1026. }
  1027. BUF_MEM *STORE_get_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
  1028. OPENSSL_ITEM parameters[])
  1029. {
  1030. STORE_OBJECT *object;
  1031. BUF_MEM *b;
  1032. check_store(s,STORE_F_STORE_GET_ARBITRARY,
  1033. get_object,STORE_R_NO_GET_OBJECT_ARBITRARY_FUNCTION);
  1034. object = s->meth->get_object(s, STORE_OBJECT_TYPE_ARBITRARY,
  1035. attributes, parameters);
  1036. if (!object || !object->data.arbitrary)
  1037. {
  1038. STOREerr(STORE_F_STORE_GET_ARBITRARY,
  1039. STORE_R_FAILED_GETTING_ARBITRARY);
  1040. return 0;
  1041. }
  1042. b = object->data.arbitrary;
  1043. object->data.arbitrary = NULL;
  1044. STORE_OBJECT_free(object);
  1045. return b;
  1046. }
  1047. int STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
  1048. OPENSSL_ITEM parameters[])
  1049. {
  1050. check_store(s,STORE_F_STORE_DELETE_ARBITRARY,
  1051. delete_object,STORE_R_NO_DELETE_ARBITRARY_FUNCTION);
  1052. if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_ARBITRARY, attributes,
  1053. parameters))
  1054. {
  1055. STOREerr(STORE_F_STORE_DELETE_ARBITRARY,
  1056. STORE_R_FAILED_DELETING_ARBITRARY);
  1057. return 0;
  1058. }
  1059. return 1;
  1060. }
  1061. STORE_OBJECT *STORE_OBJECT_new(void)
  1062. {
  1063. STORE_OBJECT *object = OPENSSL_malloc(sizeof(STORE_OBJECT));
  1064. if (object) memset(object, 0, sizeof(STORE_OBJECT));
  1065. return object;
  1066. }
  1067. void STORE_OBJECT_free(STORE_OBJECT *data)
  1068. {
  1069. if (!data) return;
  1070. switch (data->type)
  1071. {
  1072. case STORE_OBJECT_TYPE_X509_CERTIFICATE:
  1073. X509_free(data->data.x509.certificate);
  1074. break;
  1075. case STORE_OBJECT_TYPE_X509_CRL:
  1076. X509_CRL_free(data->data.crl);
  1077. break;
  1078. case STORE_OBJECT_TYPE_PRIVATE_KEY:
  1079. case STORE_OBJECT_TYPE_PUBLIC_KEY:
  1080. EVP_PKEY_free(data->data.key);
  1081. break;
  1082. case STORE_OBJECT_TYPE_NUMBER:
  1083. BN_free(data->data.number);
  1084. break;
  1085. case STORE_OBJECT_TYPE_ARBITRARY:
  1086. BUF_MEM_free(data->data.arbitrary);
  1087. break;
  1088. }
  1089. OPENSSL_free(data);
  1090. }
  1091. IMPLEMENT_STACK_OF(STORE_OBJECT*)
  1092. struct STORE_attr_info_st
  1093. {
  1094. unsigned char set[(STORE_ATTR_TYPE_NUM + 8) / 8];
  1095. union
  1096. {
  1097. char *cstring;
  1098. unsigned char *sha1string;
  1099. X509_NAME *dn;
  1100. BIGNUM *number;
  1101. void *any;
  1102. } values[STORE_ATTR_TYPE_NUM+1];
  1103. size_t value_sizes[STORE_ATTR_TYPE_NUM+1];
  1104. };
  1105. #define ATTR_IS_SET(a,i) ((i) > 0 && (i) < STORE_ATTR_TYPE_NUM \
  1106. && ((a)->set[(i) / 8] & (1 << ((i) % 8))))
  1107. #define SET_ATTRBIT(a,i) ((a)->set[(i) / 8] |= (1 << ((i) % 8)))
  1108. #define CLEAR_ATTRBIT(a,i) ((a)->set[(i) / 8] &= ~(1 << ((i) % 8)))
  1109. STORE_ATTR_INFO *STORE_ATTR_INFO_new(void)
  1110. {
  1111. return (STORE_ATTR_INFO *)OPENSSL_malloc(sizeof(STORE_ATTR_INFO));
  1112. }
  1113. static void STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs,
  1114. STORE_ATTR_TYPES code)
  1115. {
  1116. if (ATTR_IS_SET(attrs,code))
  1117. {
  1118. switch(code)
  1119. {
  1120. case STORE_ATTR_FRIENDLYNAME:
  1121. case STORE_ATTR_EMAIL:
  1122. case STORE_ATTR_FILENAME:
  1123. STORE_ATTR_INFO_modify_cstr(attrs, code, NULL, 0);
  1124. break;
  1125. case STORE_ATTR_KEYID:
  1126. case STORE_ATTR_ISSUERKEYID:
  1127. case STORE_ATTR_SUBJECTKEYID:
  1128. case STORE_ATTR_ISSUERSERIALHASH:
  1129. case STORE_ATTR_CERTHASH:
  1130. STORE_ATTR_INFO_modify_sha1str(attrs, code, NULL, 0);
  1131. break;
  1132. case STORE_ATTR_ISSUER:
  1133. case STORE_ATTR_SUBJECT:
  1134. STORE_ATTR_INFO_modify_dn(attrs, code, NULL);
  1135. break;
  1136. case STORE_ATTR_SERIAL:
  1137. STORE_ATTR_INFO_modify_number(attrs, code, NULL);
  1138. break;
  1139. default:
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. int STORE_ATTR_INFO_free(STORE_ATTR_INFO *attrs)
  1145. {
  1146. if (attrs)
  1147. {
  1148. STORE_ATTR_TYPES i;
  1149. for(i = 0; i++ < STORE_ATTR_TYPE_NUM;)
  1150. STORE_ATTR_INFO_attr_free(attrs, i);
  1151. OPENSSL_free(attrs);
  1152. }
  1153. return 1;
  1154. }
  1155. char *STORE_ATTR_INFO_get0_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
  1156. {
  1157. if (!attrs)
  1158. {
  1159. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
  1160. ERR_R_PASSED_NULL_PARAMETER);
  1161. return NULL;
  1162. }
  1163. if (ATTR_IS_SET(attrs,code))
  1164. return attrs->values[code].cstring;
  1165. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
  1166. STORE_R_NO_VALUE);
  1167. return NULL;
  1168. }
  1169. unsigned char *STORE_ATTR_INFO_get0_sha1str(STORE_ATTR_INFO *attrs,
  1170. STORE_ATTR_TYPES code)
  1171. {
  1172. if (!attrs)
  1173. {
  1174. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
  1175. ERR_R_PASSED_NULL_PARAMETER);
  1176. return NULL;
  1177. }
  1178. if (ATTR_IS_SET(attrs,code))
  1179. return attrs->values[code].sha1string;
  1180. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
  1181. STORE_R_NO_VALUE);
  1182. return NULL;
  1183. }
  1184. X509_NAME *STORE_ATTR_INFO_get0_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
  1185. {
  1186. if (!attrs)
  1187. {
  1188. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
  1189. ERR_R_PASSED_NULL_PARAMETER);
  1190. return NULL;
  1191. }
  1192. if (ATTR_IS_SET(attrs,code))
  1193. return attrs->values[code].dn;
  1194. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
  1195. STORE_R_NO_VALUE);
  1196. return NULL;
  1197. }
  1198. BIGNUM *STORE_ATTR_INFO_get0_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
  1199. {
  1200. if (!attrs)
  1201. {
  1202. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
  1203. ERR_R_PASSED_NULL_PARAMETER);
  1204. return NULL;
  1205. }
  1206. if (ATTR_IS_SET(attrs,code))
  1207. return attrs->values[code].number;
  1208. STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
  1209. STORE_R_NO_VALUE);
  1210. return NULL;
  1211. }
  1212. int STORE_ATTR_INFO_set_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1213. char *cstr, size_t cstr_size)
  1214. {
  1215. if (!attrs)
  1216. {
  1217. STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
  1218. ERR_R_PASSED_NULL_PARAMETER);
  1219. return 0;
  1220. }
  1221. if (!ATTR_IS_SET(attrs,code))
  1222. {
  1223. if ((attrs->values[code].cstring = BUF_strndup(cstr, cstr_size)))
  1224. return 1;
  1225. STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
  1226. ERR_R_MALLOC_FAILURE);
  1227. return 0;
  1228. }
  1229. STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, STORE_R_ALREADY_HAS_A_VALUE);
  1230. return 0;
  1231. }
  1232. int STORE_ATTR_INFO_set_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1233. unsigned char *sha1str, size_t sha1str_size)
  1234. {
  1235. if (!attrs)
  1236. {
  1237. STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
  1238. ERR_R_PASSED_NULL_PARAMETER);
  1239. return 0;
  1240. }
  1241. if (!ATTR_IS_SET(attrs,code))
  1242. {
  1243. if ((attrs->values[code].sha1string =
  1244. (unsigned char *)BUF_memdup(sha1str,
  1245. sha1str_size)))
  1246. return 1;
  1247. STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
  1248. ERR_R_MALLOC_FAILURE);
  1249. return 0;
  1250. }
  1251. STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, STORE_R_ALREADY_HAS_A_VALUE);
  1252. return 0;
  1253. }
  1254. int STORE_ATTR_INFO_set_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1255. X509_NAME *dn)
  1256. {
  1257. if (!attrs)
  1258. {
  1259. STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN,
  1260. ERR_R_PASSED_NULL_PARAMETER);
  1261. return 0;
  1262. }
  1263. if (!ATTR_IS_SET(attrs,code))
  1264. {
  1265. if ((attrs->values[code].dn = X509_NAME_dup(dn)))
  1266. return 1;
  1267. STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN,
  1268. ERR_R_MALLOC_FAILURE);
  1269. return 0;
  1270. }
  1271. STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, STORE_R_ALREADY_HAS_A_VALUE);
  1272. return 0;
  1273. }
  1274. int STORE_ATTR_INFO_set_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1275. BIGNUM *number)
  1276. {
  1277. if (!attrs)
  1278. {
  1279. STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
  1280. ERR_R_PASSED_NULL_PARAMETER);
  1281. return 0;
  1282. }
  1283. if (!ATTR_IS_SET(attrs,code))
  1284. {
  1285. if ((attrs->values[code].number = BN_dup(number)))
  1286. return 1;
  1287. STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
  1288. ERR_R_MALLOC_FAILURE);
  1289. return 0;
  1290. }
  1291. STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, STORE_R_ALREADY_HAS_A_VALUE);
  1292. return 0;
  1293. }
  1294. int STORE_ATTR_INFO_modify_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1295. char *cstr, size_t cstr_size)
  1296. {
  1297. if (!attrs)
  1298. {
  1299. STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_CSTR,
  1300. ERR_R_PASSED_NULL_PARAMETER);
  1301. return 0;
  1302. }
  1303. if (ATTR_IS_SET(attrs,code))
  1304. {
  1305. OPENSSL_free(attrs->values[code].cstring);
  1306. attrs->values[code].cstring = NULL;
  1307. CLEAR_ATTRBIT(attrs, code);
  1308. }
  1309. return STORE_ATTR_INFO_set_cstr(attrs, code, cstr, cstr_size);
  1310. }
  1311. int STORE_ATTR_INFO_modify_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1312. unsigned char *sha1str, size_t sha1str_size)
  1313. {
  1314. if (!attrs)
  1315. {
  1316. STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_SHA1STR,
  1317. ERR_R_PASSED_NULL_PARAMETER);
  1318. return 0;
  1319. }
  1320. if (ATTR_IS_SET(attrs,code))
  1321. {
  1322. OPENSSL_free(attrs->values[code].sha1string);
  1323. attrs->values[code].sha1string = NULL;
  1324. CLEAR_ATTRBIT(attrs, code);
  1325. }
  1326. return STORE_ATTR_INFO_set_sha1str(attrs, code, sha1str, sha1str_size);
  1327. }
  1328. int STORE_ATTR_INFO_modify_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1329. X509_NAME *dn)
  1330. {
  1331. if (!attrs)
  1332. {
  1333. STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_DN,
  1334. ERR_R_PASSED_NULL_PARAMETER);
  1335. return 0;
  1336. }
  1337. if (ATTR_IS_SET(attrs,code))
  1338. {
  1339. OPENSSL_free(attrs->values[code].dn);
  1340. attrs->values[code].dn = NULL;
  1341. CLEAR_ATTRBIT(attrs, code);
  1342. }
  1343. return STORE_ATTR_INFO_set_dn(attrs, code, dn);
  1344. }
  1345. int STORE_ATTR_INFO_modify_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1346. BIGNUM *number)
  1347. {
  1348. if (!attrs)
  1349. {
  1350. STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_NUMBER,
  1351. ERR_R_PASSED_NULL_PARAMETER);
  1352. return 0;
  1353. }
  1354. if (ATTR_IS_SET(attrs,code))
  1355. {
  1356. OPENSSL_free(attrs->values[code].number);
  1357. attrs->values[code].number = NULL;
  1358. CLEAR_ATTRBIT(attrs, code);
  1359. }
  1360. return STORE_ATTR_INFO_set_number(attrs, code, number);
  1361. }
  1362. struct attr_list_ctx_st
  1363. {
  1364. OPENSSL_ITEM *attributes;
  1365. };
  1366. void *STORE_parse_attrs_start(OPENSSL_ITEM *attributes)
  1367. {
  1368. if (attributes)
  1369. {
  1370. struct attr_list_ctx_st *context =
  1371. (struct attr_list_ctx_st *)OPENSSL_malloc(sizeof(struct attr_list_ctx_st));
  1372. if (context)
  1373. context->attributes = attributes;
  1374. else
  1375. STOREerr(STORE_F_STORE_PARSE_ATTRS_START,
  1376. ERR_R_MALLOC_FAILURE);
  1377. return context;
  1378. }
  1379. STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_PASSED_NULL_PARAMETER);
  1380. return 0;
  1381. }
  1382. STORE_ATTR_INFO *STORE_parse_attrs_next(void *handle)
  1383. {
  1384. struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
  1385. if (context && context->attributes)
  1386. {
  1387. STORE_ATTR_INFO *attrs = NULL;
  1388. while(context->attributes
  1389. && context->attributes->code != STORE_ATTR_OR
  1390. && context->attributes->code != STORE_ATTR_END)
  1391. {
  1392. switch(context->attributes->code)
  1393. {
  1394. case STORE_ATTR_FRIENDLYNAME:
  1395. case STORE_ATTR_EMAIL:
  1396. case STORE_ATTR_FILENAME:
  1397. if (!attrs) attrs = STORE_ATTR_INFO_new();
  1398. if (attrs == NULL)
  1399. {
  1400. STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1401. ERR_R_MALLOC_FAILURE);
  1402. goto err;
  1403. }
  1404. STORE_ATTR_INFO_set_cstr(attrs,
  1405. context->attributes->code,
  1406. context->attributes->value,
  1407. context->attributes->value_size);
  1408. break;
  1409. case STORE_ATTR_KEYID:
  1410. case STORE_ATTR_ISSUERKEYID:
  1411. case STORE_ATTR_SUBJECTKEYID:
  1412. case STORE_ATTR_ISSUERSERIALHASH:
  1413. case STORE_ATTR_CERTHASH:
  1414. if (!attrs) attrs = STORE_ATTR_INFO_new();
  1415. if (attrs == NULL)
  1416. {
  1417. STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1418. ERR_R_MALLOC_FAILURE);
  1419. goto err;
  1420. }
  1421. STORE_ATTR_INFO_set_sha1str(attrs,
  1422. context->attributes->code,
  1423. context->attributes->value,
  1424. context->attributes->value_size);
  1425. break;
  1426. case STORE_ATTR_ISSUER:
  1427. case STORE_ATTR_SUBJECT:
  1428. if (!attrs) attrs = STORE_ATTR_INFO_new();
  1429. if (attrs == NULL)
  1430. {
  1431. STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1432. ERR_R_MALLOC_FAILURE);
  1433. goto err;
  1434. }
  1435. STORE_ATTR_INFO_modify_dn(attrs,
  1436. context->attributes->code,
  1437. context->attributes->value);
  1438. break;
  1439. case STORE_ATTR_SERIAL:
  1440. if (!attrs) attrs = STORE_ATTR_INFO_new();
  1441. if (attrs == NULL)
  1442. {
  1443. STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1444. ERR_R_MALLOC_FAILURE);
  1445. goto err;
  1446. }
  1447. STORE_ATTR_INFO_modify_number(attrs,
  1448. context->attributes->code,
  1449. context->attributes->value);
  1450. break;
  1451. }
  1452. context->attributes++;
  1453. }
  1454. if (context->attributes->code == STORE_ATTR_OR)
  1455. context->attributes++;
  1456. return attrs;
  1457. err:
  1458. while(context->attributes
  1459. && context->attributes->code != STORE_ATTR_OR
  1460. && context->attributes->code != STORE_ATTR_END)
  1461. context->attributes++;
  1462. if (context->attributes->code == STORE_ATTR_OR)
  1463. context->attributes++;
  1464. return NULL;
  1465. }
  1466. STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_PASSED_NULL_PARAMETER);
  1467. return NULL;
  1468. }
  1469. int STORE_parse_attrs_end(void *handle)
  1470. {
  1471. struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
  1472. if (context && context->attributes)
  1473. {
  1474. #if 0
  1475. OPENSSL_ITEM *attributes = context->attributes;
  1476. #endif
  1477. OPENSSL_free(context);
  1478. return 1;
  1479. }
  1480. STOREerr(STORE_F_STORE_PARSE_ATTRS_END, ERR_R_PASSED_NULL_PARAMETER);
  1481. return 0;
  1482. }
  1483. int STORE_parse_attrs_endp(void *handle)
  1484. {
  1485. struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
  1486. if (context && context->attributes)
  1487. {
  1488. return context->attributes->code == STORE_ATTR_END;
  1489. }
  1490. STOREerr(STORE_F_STORE_PARSE_ATTRS_ENDP, ERR_R_PASSED_NULL_PARAMETER);
  1491. return 0;
  1492. }
  1493. static int attr_info_compare_compute_range(
  1494. unsigned char *abits, unsigned char *bbits,
  1495. unsigned int *alowp, unsigned int *ahighp,
  1496. unsigned int *blowp, unsigned int *bhighp)
  1497. {
  1498. unsigned int alow = (unsigned int)-1, ahigh = 0;
  1499. unsigned int blow = (unsigned int)-1, bhigh = 0;
  1500. int i, res = 0;
  1501. for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++)
  1502. {
  1503. if (res == 0)
  1504. {
  1505. if (*abits < *bbits) res = -1;
  1506. if (*abits > *bbits) res = 1;
  1507. }
  1508. if (*abits)
  1509. {
  1510. if (alow == (unsigned int)-1)
  1511. {
  1512. alow = i * 8;
  1513. if (!(*abits & 0x01)) alow++;
  1514. if (!(*abits & 0x02)) alow++;
  1515. if (!(*abits & 0x04)) alow++;
  1516. if (!(*abits & 0x08)) alow++;
  1517. if (!(*abits & 0x10)) alow++;
  1518. if (!(*abits & 0x20)) alow++;
  1519. if (!(*abits & 0x40)) alow++;
  1520. }
  1521. ahigh = i * 8 + 7;
  1522. if (!(*abits & 0x80)) ahigh++;
  1523. if (!(*abits & 0x40)) ahigh++;
  1524. if (!(*abits & 0x20)) ahigh++;
  1525. if (!(*abits & 0x10)) ahigh++;
  1526. if (!(*abits & 0x08)) ahigh++;
  1527. if (!(*abits & 0x04)) ahigh++;
  1528. if (!(*abits & 0x02)) ahigh++;
  1529. }
  1530. if (*bbits)
  1531. {
  1532. if (blow == (unsigned int)-1)
  1533. {
  1534. blow = i * 8;
  1535. if (!(*bbits & 0x01)) blow++;
  1536. if (!(*bbits & 0x02)) blow++;
  1537. if (!(*bbits & 0x04)) blow++;
  1538. if (!(*bbits & 0x08)) blow++;
  1539. if (!(*bbits & 0x10)) blow++;
  1540. if (!(*bbits & 0x20)) blow++;
  1541. if (!(*bbits & 0x40)) blow++;
  1542. }
  1543. bhigh = i * 8 + 7;
  1544. if (!(*bbits & 0x80)) bhigh++;
  1545. if (!(*bbits & 0x40)) bhigh++;
  1546. if (!(*bbits & 0x20)) bhigh++;
  1547. if (!(*bbits & 0x10)) bhigh++;
  1548. if (!(*bbits & 0x08)) bhigh++;
  1549. if (!(*bbits & 0x04)) bhigh++;
  1550. if (!(*bbits & 0x02)) bhigh++;
  1551. }
  1552. }
  1553. if (ahigh + alow < bhigh + blow) res = -1;
  1554. if (ahigh + alow > bhigh + blow) res = 1;
  1555. if (alowp) *alowp = alow;
  1556. if (ahighp) *ahighp = ahigh;
  1557. if (blowp) *blowp = blow;
  1558. if (bhighp) *bhighp = bhigh;
  1559. return res;
  1560. }
  1561. int STORE_ATTR_INFO_compare(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1562. {
  1563. if (a == b) return 0;
  1564. if (!a) return -1;
  1565. if (!b) return 1;
  1566. return attr_info_compare_compute_range(a->set, b->set, 0, 0, 0, 0);
  1567. }
  1568. int STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1569. {
  1570. unsigned int alow, ahigh, blow, bhigh;
  1571. if (a == b) return 1;
  1572. if (!a) return 0;
  1573. if (!b) return 0;
  1574. attr_info_compare_compute_range(a->set, b->set,
  1575. &alow, &ahigh, &blow, &bhigh);
  1576. if (alow >= blow && ahigh <= bhigh)
  1577. return 1;
  1578. return 0;
  1579. }
  1580. int STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1581. {
  1582. unsigned char *abits, *bbits;
  1583. int i;
  1584. if (a == b) return 1;
  1585. if (!a) return 0;
  1586. if (!b) return 0;
  1587. abits = a->set;
  1588. bbits = b->set;
  1589. for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++)
  1590. {
  1591. if (*abits && (*bbits & *abits) != *abits)
  1592. return 0;
  1593. }
  1594. return 1;
  1595. }
  1596. int STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1597. {
  1598. STORE_ATTR_TYPES i;
  1599. if (a == b) return 1;
  1600. if (!STORE_ATTR_INFO_in(a, b)) return 0;
  1601. for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
  1602. if (ATTR_IS_SET(a, i))
  1603. {
  1604. switch(i)
  1605. {
  1606. case STORE_ATTR_FRIENDLYNAME:
  1607. case STORE_ATTR_EMAIL:
  1608. case STORE_ATTR_FILENAME:
  1609. if (strcmp(a->values[i].cstring,
  1610. b->values[i].cstring))
  1611. return 0;
  1612. break;
  1613. case STORE_ATTR_KEYID:
  1614. case STORE_ATTR_ISSUERKEYID:
  1615. case STORE_ATTR_SUBJECTKEYID:
  1616. case STORE_ATTR_ISSUERSERIALHASH:
  1617. case STORE_ATTR_CERTHASH:
  1618. if (memcmp(a->values[i].sha1string,
  1619. b->values[i].sha1string,
  1620. a->value_sizes[i]))
  1621. return 0;
  1622. break;
  1623. case STORE_ATTR_ISSUER:
  1624. case STORE_ATTR_SUBJECT:
  1625. if (X509_NAME_cmp(a->values[i].dn,
  1626. b->values[i].dn))
  1627. return 0;
  1628. break;
  1629. case STORE_ATTR_SERIAL:
  1630. if (BN_cmp(a->values[i].number,
  1631. b->values[i].number))
  1632. return 0;
  1633. break;
  1634. default:
  1635. break;
  1636. }
  1637. }
  1638. return 1;
  1639. }