str_lib.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /* crypto/store/str_lib.c */
  2. /*
  3. * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
  4. * 2003.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@openssl.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <string.h>
  60. #include <openssl/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(const unsigned char *abits,
  1388. const 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(const STORE_ATTR_INFO *const *a,
  1488. const STORE_ATTR_INFO *const *b)
  1489. {
  1490. if (a == b)
  1491. return 0;
  1492. if (!a)
  1493. return -1;
  1494. if (!b)
  1495. return 1;
  1496. return attr_info_compare_compute_range((*a)->set, (*b)->set, 0, 0, 0, 0);
  1497. }
  1498. int STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1499. {
  1500. unsigned int alow, ahigh, blow, bhigh;
  1501. if (a == b)
  1502. return 1;
  1503. if (!a)
  1504. return 0;
  1505. if (!b)
  1506. return 0;
  1507. attr_info_compare_compute_range(a->set, b->set,
  1508. &alow, &ahigh, &blow, &bhigh);
  1509. if (alow >= blow && ahigh <= bhigh)
  1510. return 1;
  1511. return 0;
  1512. }
  1513. int STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1514. {
  1515. unsigned char *abits, *bbits;
  1516. int i;
  1517. if (a == b)
  1518. return 1;
  1519. if (!a)
  1520. return 0;
  1521. if (!b)
  1522. return 0;
  1523. abits = a->set;
  1524. bbits = b->set;
  1525. for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
  1526. if (*abits && (*bbits & *abits) != *abits)
  1527. return 0;
  1528. }
  1529. return 1;
  1530. }
  1531. int STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1532. {
  1533. STORE_ATTR_TYPES i;
  1534. if (a == b)
  1535. return 1;
  1536. if (!STORE_ATTR_INFO_in(a, b))
  1537. return 0;
  1538. for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
  1539. if (ATTR_IS_SET(a, i)) {
  1540. switch (i) {
  1541. case STORE_ATTR_FRIENDLYNAME:
  1542. case STORE_ATTR_EMAIL:
  1543. case STORE_ATTR_FILENAME:
  1544. if (strcmp(a->values[i].cstring, b->values[i].cstring))
  1545. return 0;
  1546. break;
  1547. case STORE_ATTR_KEYID:
  1548. case STORE_ATTR_ISSUERKEYID:
  1549. case STORE_ATTR_SUBJECTKEYID:
  1550. case STORE_ATTR_ISSUERSERIALHASH:
  1551. case STORE_ATTR_CERTHASH:
  1552. if (memcmp(a->values[i].sha1string,
  1553. b->values[i].sha1string, a->value_sizes[i]))
  1554. return 0;
  1555. break;
  1556. case STORE_ATTR_ISSUER:
  1557. case STORE_ATTR_SUBJECT:
  1558. if (X509_NAME_cmp(a->values[i].dn, b->values[i].dn))
  1559. return 0;
  1560. break;
  1561. case STORE_ATTR_SERIAL:
  1562. if (BN_cmp(a->values[i].number, b->values[i].number))
  1563. return 0;
  1564. break;
  1565. default:
  1566. break;
  1567. }
  1568. }
  1569. return 1;
  1570. }