str_lib.c 54 KB

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