str_lib.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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 == NULL) \
  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. const unsigned char *abits, const 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(const STORE_ATTR_INFO * const *a,
  1562. const STORE_ATTR_INFO * const *b)
  1563. {
  1564. if (a == b) return 0;
  1565. if (!a) return -1;
  1566. if (!b) return 1;
  1567. return attr_info_compare_compute_range((*a)->set, (*b)->set, 0, 0, 0, 0);
  1568. }
  1569. int STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1570. {
  1571. unsigned int alow, ahigh, blow, bhigh;
  1572. if (a == b) return 1;
  1573. if (!a) return 0;
  1574. if (!b) return 0;
  1575. attr_info_compare_compute_range(a->set, b->set,
  1576. &alow, &ahigh, &blow, &bhigh);
  1577. if (alow >= blow && ahigh <= bhigh)
  1578. return 1;
  1579. return 0;
  1580. }
  1581. int STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1582. {
  1583. unsigned char *abits, *bbits;
  1584. int i;
  1585. if (a == b) return 1;
  1586. if (!a) return 0;
  1587. if (!b) return 0;
  1588. abits = a->set;
  1589. bbits = b->set;
  1590. for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++)
  1591. {
  1592. if (*abits && (*bbits & *abits) != *abits)
  1593. return 0;
  1594. }
  1595. return 1;
  1596. }
  1597. int STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1598. {
  1599. STORE_ATTR_TYPES i;
  1600. if (a == b) return 1;
  1601. if (!STORE_ATTR_INFO_in(a, b)) return 0;
  1602. for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
  1603. if (ATTR_IS_SET(a, i))
  1604. {
  1605. switch(i)
  1606. {
  1607. case STORE_ATTR_FRIENDLYNAME:
  1608. case STORE_ATTR_EMAIL:
  1609. case STORE_ATTR_FILENAME:
  1610. if (strcmp(a->values[i].cstring,
  1611. b->values[i].cstring))
  1612. return 0;
  1613. break;
  1614. case STORE_ATTR_KEYID:
  1615. case STORE_ATTR_ISSUERKEYID:
  1616. case STORE_ATTR_SUBJECTKEYID:
  1617. case STORE_ATTR_ISSUERSERIALHASH:
  1618. case STORE_ATTR_CERTHASH:
  1619. if (memcmp(a->values[i].sha1string,
  1620. b->values[i].sha1string,
  1621. a->value_sizes[i]))
  1622. return 0;
  1623. break;
  1624. case STORE_ATTR_ISSUER:
  1625. case STORE_ATTR_SUBJECT:
  1626. if (X509_NAME_cmp(a->values[i].dn,
  1627. b->values[i].dn))
  1628. return 0;
  1629. break;
  1630. case STORE_ATTR_SERIAL:
  1631. if (BN_cmp(a->values[i].number,
  1632. b->values[i].number))
  1633. return 0;
  1634. break;
  1635. default:
  1636. break;
  1637. }
  1638. }
  1639. return 1;
  1640. }