x_crl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* crypto/asn1/x_crl.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/asn1t.h>
  61. #include "asn1_locl.h"
  62. #include <openssl/x509.h>
  63. #include <openssl/x509v3.h>
  64. static int X509_REVOKED_cmp(const X509_REVOKED * const *a,
  65. const X509_REVOKED * const *b);
  66. static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
  67. ASN1_SEQUENCE(X509_REVOKED) = {
  68. ASN1_SIMPLE(X509_REVOKED,serialNumber, ASN1_INTEGER),
  69. ASN1_SIMPLE(X509_REVOKED,revocationDate, ASN1_TIME),
  70. ASN1_SEQUENCE_OF_OPT(X509_REVOKED,extensions, X509_EXTENSION)
  71. } ASN1_SEQUENCE_END(X509_REVOKED)
  72. static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
  73. static int def_crl_lookup(X509_CRL *crl,
  74. X509_REVOKED **ret, ASN1_INTEGER *serial, X509_NAME *issuer);
  75. static X509_CRL_METHOD int_crl_meth =
  76. {
  77. 0,
  78. 0,0,
  79. def_crl_lookup,
  80. def_crl_verify
  81. };
  82. static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
  83. /* The X509_CRL_INFO structure needs a bit of customisation.
  84. * Since we cache the original encoding the signature wont be affected by
  85. * reordering of the revoked field.
  86. */
  87. static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  88. void *exarg)
  89. {
  90. X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
  91. if(!a || !a->revoked) return 1;
  92. switch(operation) {
  93. /* Just set cmp function here. We don't sort because that
  94. * would affect the output of X509_CRL_print().
  95. */
  96. case ASN1_OP_D2I_POST:
  97. (void)sk_X509_REVOKED_set_cmp_func(a->revoked,X509_REVOKED_cmp);
  98. break;
  99. }
  100. return 1;
  101. }
  102. ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
  103. ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
  104. ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR),
  105. ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
  106. ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
  107. ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
  108. ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
  109. ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
  110. } ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
  111. /* Set CRL entry issuer according to CRL certificate issuer extension.
  112. * Check for unhandled critical CRL entry extensions.
  113. */
  114. static int crl_set_issuers(X509_CRL *crl)
  115. {
  116. int i, j;
  117. GENERAL_NAMES *gens, *gtmp;
  118. STACK_OF(X509_REVOKED) *revoked;
  119. revoked = X509_CRL_get_REVOKED(crl);
  120. gens = NULL;
  121. for (i = 0; i < sk_X509_REVOKED_num(revoked); i++)
  122. {
  123. X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
  124. STACK_OF(X509_EXTENSION) *exts;
  125. ASN1_ENUMERATED *reason;
  126. X509_EXTENSION *ext;
  127. gtmp = X509_REVOKED_get_ext_d2i(rev,
  128. NID_certificate_issuer,
  129. &j, NULL);
  130. if (!gtmp && (j != -1))
  131. {
  132. crl->flags |= EXFLAG_INVALID;
  133. return 1;
  134. }
  135. if (gtmp)
  136. {
  137. gens = gtmp;
  138. if (!crl->issuers)
  139. {
  140. crl->issuers = sk_GENERAL_NAMES_new_null();
  141. if (!crl->issuers)
  142. return 0;
  143. }
  144. if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
  145. return 0;
  146. }
  147. rev->issuer = gens;
  148. reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason,
  149. &j, NULL);
  150. if (!reason && (j != -1))
  151. {
  152. crl->flags |= EXFLAG_INVALID;
  153. return 1;
  154. }
  155. if (reason)
  156. {
  157. rev->reason = ASN1_ENUMERATED_get(reason);
  158. ASN1_ENUMERATED_free(reason);
  159. }
  160. else
  161. rev->reason = CRL_REASON_NONE;
  162. /* Check for critical CRL entry extensions */
  163. exts = rev->extensions;
  164. for (j = 0; j < sk_X509_EXTENSION_num(exts); j++)
  165. {
  166. ext = sk_X509_EXTENSION_value(exts, j);
  167. if (ext->critical > 0)
  168. {
  169. if (OBJ_obj2nid(ext->object) ==
  170. NID_certificate_issuer)
  171. continue;
  172. crl->flags |= EXFLAG_CRITICAL;
  173. break;
  174. }
  175. }
  176. }
  177. return 1;
  178. }
  179. /* The X509_CRL structure needs a bit of customisation. Cache some extensions
  180. * and hash of the whole CRL.
  181. */
  182. static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  183. void *exarg)
  184. {
  185. X509_CRL *crl = (X509_CRL *)*pval;
  186. STACK_OF(X509_EXTENSION) *exts;
  187. X509_EXTENSION *ext;
  188. int idx;
  189. switch(operation)
  190. {
  191. case ASN1_OP_NEW_POST:
  192. crl->idp = NULL;
  193. crl->akid = NULL;
  194. crl->flags = 0;
  195. crl->idp_flags = 0;
  196. crl->idp_reasons = CRLDP_ALL_REASONS;
  197. crl->meth = default_crl_method;
  198. crl->meth_data = NULL;
  199. crl->issuers = NULL;
  200. crl->crl_number = NULL;
  201. crl->base_crl_number = NULL;
  202. break;
  203. case ASN1_OP_D2I_POST:
  204. #ifndef OPENSSL_NO_SHA
  205. X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
  206. #endif
  207. crl->idp = X509_CRL_get_ext_d2i(crl,
  208. NID_issuing_distribution_point, NULL, NULL);
  209. if (crl->idp)
  210. setup_idp(crl, crl->idp);
  211. crl->akid = X509_CRL_get_ext_d2i(crl,
  212. NID_authority_key_identifier, NULL, NULL);
  213. crl->crl_number = X509_CRL_get_ext_d2i(crl,
  214. NID_crl_number, NULL, NULL);
  215. crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
  216. NID_delta_crl, NULL, NULL);
  217. /* Delta CRLs must have CRL number */
  218. if (crl->base_crl_number && !crl->crl_number)
  219. crl->flags |= EXFLAG_INVALID;
  220. /* See if we have any unhandled critical CRL extensions and
  221. * indicate this in a flag. We only currently handle IDP so
  222. * anything else critical sets the flag.
  223. *
  224. * This code accesses the X509_CRL structure directly:
  225. * applications shouldn't do this.
  226. */
  227. exts = crl->crl->extensions;
  228. for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++)
  229. {
  230. int nid;
  231. ext = sk_X509_EXTENSION_value(exts, idx);
  232. nid = OBJ_obj2nid(ext->object);
  233. if (nid == NID_freshest_crl)
  234. crl->flags |= EXFLAG_FRESHEST;
  235. if (ext->critical > 0)
  236. {
  237. /* We handle IDP and deltas */
  238. if ((nid == NID_issuing_distribution_point)
  239. || (nid == NID_delta_crl))
  240. break;;
  241. crl->flags |= EXFLAG_CRITICAL;
  242. break;
  243. }
  244. }
  245. if (!crl_set_issuers(crl))
  246. return 0;
  247. if (crl->meth->crl_init)
  248. {
  249. if (crl->meth->crl_init(crl) == 0)
  250. return 0;
  251. }
  252. break;
  253. case ASN1_OP_FREE_POST:
  254. if (crl->meth->crl_free)
  255. {
  256. if (!crl->meth->crl_free(crl))
  257. return 0;
  258. }
  259. if (crl->akid)
  260. AUTHORITY_KEYID_free(crl->akid);
  261. if (crl->idp)
  262. ISSUING_DIST_POINT_free(crl->idp);
  263. ASN1_INTEGER_free(crl->crl_number);
  264. ASN1_INTEGER_free(crl->base_crl_number);
  265. sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
  266. break;
  267. }
  268. return 1;
  269. }
  270. /* Convert IDP into a more convenient form */
  271. static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
  272. {
  273. int idp_only = 0;
  274. /* Set various flags according to IDP */
  275. crl->idp_flags |= IDP_PRESENT;
  276. if (idp->onlyuser > 0)
  277. {
  278. idp_only++;
  279. crl->idp_flags |= IDP_ONLYUSER;
  280. }
  281. if (idp->onlyCA > 0)
  282. {
  283. idp_only++;
  284. crl->idp_flags |= IDP_ONLYCA;
  285. }
  286. if (idp->onlyattr > 0)
  287. {
  288. idp_only++;
  289. crl->idp_flags |= IDP_ONLYATTR;
  290. }
  291. if (idp_only > 1)
  292. crl->idp_flags |= IDP_INVALID;
  293. if (idp->indirectCRL > 0)
  294. crl->idp_flags |= IDP_INDIRECT;
  295. if (idp->onlysomereasons)
  296. {
  297. crl->idp_flags |= IDP_REASONS;
  298. if (idp->onlysomereasons->length > 0)
  299. crl->idp_reasons = idp->onlysomereasons->data[0];
  300. if (idp->onlysomereasons->length > 1)
  301. crl->idp_reasons |=
  302. (idp->onlysomereasons->data[1] << 8);
  303. crl->idp_reasons &= CRLDP_ALL_REASONS;
  304. }
  305. DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
  306. }
  307. ASN1_SEQUENCE_ref(X509_CRL, crl_cb, CRYPTO_LOCK_X509_CRL) = {
  308. ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
  309. ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
  310. ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING)
  311. } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
  312. IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
  313. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
  314. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
  315. IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
  316. static int X509_REVOKED_cmp(const X509_REVOKED * const *a,
  317. const X509_REVOKED * const *b)
  318. {
  319. return(ASN1_STRING_cmp(
  320. (ASN1_STRING *)(*a)->serialNumber,
  321. (ASN1_STRING *)(*b)->serialNumber));
  322. }
  323. int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
  324. {
  325. X509_CRL_INFO *inf;
  326. inf = crl->crl;
  327. if(!inf->revoked)
  328. inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
  329. if(!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
  330. ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
  331. return 0;
  332. }
  333. inf->enc.modified = 1;
  334. return 1;
  335. }
  336. int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
  337. {
  338. if (crl->meth->crl_verify)
  339. return crl->meth->crl_verify(crl, r);
  340. return 0;
  341. }
  342. int X509_CRL_get0_by_serial(X509_CRL *crl,
  343. X509_REVOKED **ret, ASN1_INTEGER *serial)
  344. {
  345. if (crl->meth->crl_lookup)
  346. return crl->meth->crl_lookup(crl, ret, serial, NULL);
  347. return 0;
  348. }
  349. int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
  350. {
  351. if (crl->meth->crl_lookup)
  352. return crl->meth->crl_lookup(crl, ret,
  353. X509_get_serialNumber(x),
  354. X509_get_issuer_name(x));
  355. return 0;
  356. }
  357. static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
  358. {
  359. return(ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
  360. crl->sig_alg, crl->signature,crl->crl,r));
  361. }
  362. static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
  363. X509_REVOKED *rev)
  364. {
  365. int i;
  366. if (!rev->issuer)
  367. {
  368. if (!nm)
  369. return 1;
  370. if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  371. return 1;
  372. return 0;
  373. }
  374. if (!nm)
  375. nm = X509_CRL_get_issuer(crl);
  376. for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++)
  377. {
  378. GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
  379. if (gen->type != GEN_DIRNAME)
  380. continue;
  381. if (!X509_NAME_cmp(nm, gen->d.directoryName))
  382. return 1;
  383. }
  384. return 0;
  385. }
  386. static int def_crl_lookup(X509_CRL *crl,
  387. X509_REVOKED **ret, ASN1_INTEGER *serial, X509_NAME *issuer)
  388. {
  389. X509_REVOKED rtmp, *rev;
  390. int idx;
  391. rtmp.serialNumber = serial;
  392. /* Sort revoked into serial number order if not already sorted.
  393. * Do this under a lock to avoid race condition.
  394. */
  395. if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked))
  396. {
  397. CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
  398. sk_X509_REVOKED_sort(crl->crl->revoked);
  399. CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
  400. }
  401. idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
  402. if(idx < 0)
  403. return 0;
  404. /* Need to look for matching name */
  405. for(;idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++)
  406. {
  407. rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
  408. if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
  409. return 0;
  410. if (crl_revoked_issuer_match(crl, issuer, rev))
  411. {
  412. if (ret)
  413. *ret = rev;
  414. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  415. return 2;
  416. return 1;
  417. }
  418. }
  419. return 0;
  420. }
  421. void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
  422. {
  423. if (meth == NULL)
  424. default_crl_method = &int_crl_meth;
  425. else
  426. default_crl_method = meth;
  427. }
  428. X509_CRL_METHOD *X509_CRL_METHOD_new(
  429. int (*crl_init)(X509_CRL *crl),
  430. int (*crl_free)(X509_CRL *crl),
  431. int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
  432. ASN1_INTEGER *ser, X509_NAME *issuer),
  433. int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk))
  434. {
  435. X509_CRL_METHOD *m;
  436. m = OPENSSL_malloc(sizeof(X509_CRL_METHOD));
  437. if (!m)
  438. return NULL;
  439. m->crl_init = crl_init;
  440. m->crl_free = crl_free;
  441. m->crl_lookup = crl_lookup;
  442. m->crl_verify = crl_verify;
  443. m->flags = X509_CRL_METHOD_DYNAMIC;
  444. return m;
  445. }
  446. void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
  447. {
  448. if (!(m->flags & X509_CRL_METHOD_DYNAMIC))
  449. return;
  450. OPENSSL_free(m);
  451. }
  452. void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
  453. {
  454. crl->meth_data = dat;
  455. }
  456. void *X509_CRL_get_meth_data(X509_CRL *crl)
  457. {
  458. return crl->meth_data;
  459. }
  460. IMPLEMENT_STACK_OF(X509_REVOKED)
  461. IMPLEMENT_ASN1_SET_OF(X509_REVOKED)
  462. IMPLEMENT_STACK_OF(X509_CRL)
  463. IMPLEMENT_ASN1_SET_OF(X509_CRL)