x_crl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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,
  75. X509_NAME *issuer);
  76. static X509_CRL_METHOD int_crl_meth = {
  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. /*
  84. * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
  85. * the original encoding the signature wont be affected by reordering of the
  86. * revoked field.
  87. */
  88. static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  89. void *exarg)
  90. {
  91. X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
  92. if (!a || !a->revoked)
  93. return 1;
  94. switch (operation) {
  95. /*
  96. * Just set cmp function here. We don't sort because that would
  97. * affect the output of X509_CRL_print().
  98. */
  99. case ASN1_OP_D2I_POST:
  100. (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
  101. break;
  102. }
  103. return 1;
  104. }
  105. ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
  106. ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
  107. ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR),
  108. ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
  109. ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
  110. ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
  111. ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
  112. ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
  113. } ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
  114. /*
  115. * Set CRL entry issuer according to CRL certificate issuer extension. Check
  116. * for unhandled critical CRL entry extensions.
  117. */
  118. static int crl_set_issuers(X509_CRL *crl)
  119. {
  120. int i, j;
  121. GENERAL_NAMES *gens, *gtmp;
  122. STACK_OF(X509_REVOKED) *revoked;
  123. revoked = X509_CRL_get_REVOKED(crl);
  124. gens = NULL;
  125. for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
  126. X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
  127. STACK_OF(X509_EXTENSION) *exts;
  128. ASN1_ENUMERATED *reason;
  129. X509_EXTENSION *ext;
  130. gtmp = X509_REVOKED_get_ext_d2i(rev,
  131. NID_certificate_issuer, &j, NULL);
  132. if (!gtmp && (j != -1)) {
  133. crl->flags |= EXFLAG_INVALID;
  134. return 1;
  135. }
  136. if (gtmp) {
  137. gens = gtmp;
  138. if (!crl->issuers) {
  139. crl->issuers = sk_GENERAL_NAMES_new_null();
  140. if (!crl->issuers)
  141. return 0;
  142. }
  143. if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
  144. return 0;
  145. }
  146. rev->issuer = gens;
  147. reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
  148. if (!reason && (j != -1)) {
  149. crl->flags |= EXFLAG_INVALID;
  150. return 1;
  151. }
  152. if (reason) {
  153. rev->reason = ASN1_ENUMERATED_get(reason);
  154. ASN1_ENUMERATED_free(reason);
  155. } else
  156. rev->reason = CRL_REASON_NONE;
  157. /* Check for critical CRL entry extensions */
  158. exts = rev->extensions;
  159. for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
  160. ext = sk_X509_EXTENSION_value(exts, j);
  161. if (ext->critical > 0) {
  162. if (OBJ_obj2nid(ext->object) == NID_certificate_issuer)
  163. continue;
  164. crl->flags |= EXFLAG_CRITICAL;
  165. break;
  166. }
  167. }
  168. }
  169. return 1;
  170. }
  171. /*
  172. * The X509_CRL structure needs a bit of customisation. Cache some extensions
  173. * and hash of the whole CRL.
  174. */
  175. static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  176. void *exarg)
  177. {
  178. X509_CRL *crl = (X509_CRL *)*pval;
  179. STACK_OF(X509_EXTENSION) *exts;
  180. X509_EXTENSION *ext;
  181. int idx;
  182. switch (operation) {
  183. case ASN1_OP_NEW_POST:
  184. crl->idp = NULL;
  185. crl->akid = NULL;
  186. crl->flags = 0;
  187. crl->idp_flags = 0;
  188. crl->idp_reasons = CRLDP_ALL_REASONS;
  189. crl->meth = default_crl_method;
  190. crl->meth_data = NULL;
  191. crl->issuers = NULL;
  192. crl->crl_number = NULL;
  193. crl->base_crl_number = NULL;
  194. break;
  195. case ASN1_OP_D2I_POST:
  196. #ifndef OPENSSL_NO_SHA
  197. X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
  198. #endif
  199. crl->idp = X509_CRL_get_ext_d2i(crl,
  200. NID_issuing_distribution_point, NULL,
  201. NULL);
  202. if (crl->idp)
  203. setup_idp(crl, crl->idp);
  204. crl->akid = X509_CRL_get_ext_d2i(crl,
  205. NID_authority_key_identifier, NULL,
  206. NULL);
  207. crl->crl_number = X509_CRL_get_ext_d2i(crl,
  208. NID_crl_number, NULL, NULL);
  209. crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
  210. NID_delta_crl, NULL,
  211. NULL);
  212. /* Delta CRLs must have CRL number */
  213. if (crl->base_crl_number && !crl->crl_number)
  214. crl->flags |= EXFLAG_INVALID;
  215. /*
  216. * See if we have any unhandled critical CRL extensions and indicate
  217. * this in a flag. We only currently handle IDP so anything else
  218. * critical sets the flag. This code accesses the X509_CRL structure
  219. * directly: applications shouldn't do this.
  220. */
  221. exts = crl->crl->extensions;
  222. for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
  223. int nid;
  224. ext = sk_X509_EXTENSION_value(exts, idx);
  225. nid = OBJ_obj2nid(ext->object);
  226. if (nid == NID_freshest_crl)
  227. crl->flags |= EXFLAG_FRESHEST;
  228. if (ext->critical > 0) {
  229. /* We handle IDP and deltas */
  230. if ((nid == NID_issuing_distribution_point)
  231. || (nid == NID_authority_key_identifier)
  232. || (nid == NID_delta_crl))
  233. continue;
  234. crl->flags |= EXFLAG_CRITICAL;
  235. break;
  236. }
  237. }
  238. if (!crl_set_issuers(crl))
  239. return 0;
  240. if (crl->meth->crl_init) {
  241. if (crl->meth->crl_init(crl) == 0)
  242. return 0;
  243. }
  244. break;
  245. case ASN1_OP_FREE_POST:
  246. if (crl->meth->crl_free) {
  247. if (!crl->meth->crl_free(crl))
  248. return 0;
  249. }
  250. if (crl->akid)
  251. AUTHORITY_KEYID_free(crl->akid);
  252. if (crl->idp)
  253. ISSUING_DIST_POINT_free(crl->idp);
  254. ASN1_INTEGER_free(crl->crl_number);
  255. ASN1_INTEGER_free(crl->base_crl_number);
  256. sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
  257. break;
  258. }
  259. return 1;
  260. }
  261. /* Convert IDP into a more convenient form */
  262. static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
  263. {
  264. int idp_only = 0;
  265. /* Set various flags according to IDP */
  266. crl->idp_flags |= IDP_PRESENT;
  267. if (idp->onlyuser > 0) {
  268. idp_only++;
  269. crl->idp_flags |= IDP_ONLYUSER;
  270. }
  271. if (idp->onlyCA > 0) {
  272. idp_only++;
  273. crl->idp_flags |= IDP_ONLYCA;
  274. }
  275. if (idp->onlyattr > 0) {
  276. idp_only++;
  277. crl->idp_flags |= IDP_ONLYATTR;
  278. }
  279. if (idp_only > 1)
  280. crl->idp_flags |= IDP_INVALID;
  281. if (idp->indirectCRL > 0)
  282. crl->idp_flags |= IDP_INDIRECT;
  283. if (idp->onlysomereasons) {
  284. crl->idp_flags |= IDP_REASONS;
  285. if (idp->onlysomereasons->length > 0)
  286. crl->idp_reasons = idp->onlysomereasons->data[0];
  287. if (idp->onlysomereasons->length > 1)
  288. crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
  289. crl->idp_reasons &= CRLDP_ALL_REASONS;
  290. }
  291. DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
  292. }
  293. ASN1_SEQUENCE_ref(X509_CRL, crl_cb, CRYPTO_LOCK_X509_CRL) = {
  294. ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
  295. ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
  296. ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING)
  297. } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
  298. IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
  299. IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
  300. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
  301. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
  302. IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
  303. static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
  304. const X509_REVOKED *const *b)
  305. {
  306. return (ASN1_STRING_cmp((ASN1_STRING *)(*a)->serialNumber,
  307. (ASN1_STRING *)(*b)->serialNumber));
  308. }
  309. int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
  310. {
  311. X509_CRL_INFO *inf;
  312. inf = crl->crl;
  313. if (!inf->revoked)
  314. inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
  315. if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
  316. ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
  317. return 0;
  318. }
  319. inf->enc.modified = 1;
  320. return 1;
  321. }
  322. int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
  323. {
  324. if (crl->meth->crl_verify)
  325. return crl->meth->crl_verify(crl, r);
  326. return 0;
  327. }
  328. int X509_CRL_get0_by_serial(X509_CRL *crl,
  329. X509_REVOKED **ret, ASN1_INTEGER *serial)
  330. {
  331. if (crl->meth->crl_lookup)
  332. return crl->meth->crl_lookup(crl, ret, serial, NULL);
  333. return 0;
  334. }
  335. int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
  336. {
  337. if (crl->meth->crl_lookup)
  338. return crl->meth->crl_lookup(crl, ret,
  339. X509_get_serialNumber(x),
  340. X509_get_issuer_name(x));
  341. return 0;
  342. }
  343. static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
  344. {
  345. return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
  346. crl->sig_alg, crl->signature, crl->crl, r));
  347. }
  348. static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
  349. X509_REVOKED *rev)
  350. {
  351. int i;
  352. if (!rev->issuer) {
  353. if (!nm)
  354. return 1;
  355. if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  356. return 1;
  357. return 0;
  358. }
  359. if (!nm)
  360. nm = X509_CRL_get_issuer(crl);
  361. for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
  362. GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
  363. if (gen->type != GEN_DIRNAME)
  364. continue;
  365. if (!X509_NAME_cmp(nm, gen->d.directoryName))
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. static int def_crl_lookup(X509_CRL *crl,
  371. X509_REVOKED **ret, ASN1_INTEGER *serial,
  372. X509_NAME *issuer)
  373. {
  374. X509_REVOKED rtmp, *rev;
  375. int idx;
  376. rtmp.serialNumber = serial;
  377. /*
  378. * Sort revoked into serial number order if not already sorted. Do this
  379. * under a lock to avoid race condition.
  380. */
  381. if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) {
  382. CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
  383. sk_X509_REVOKED_sort(crl->crl->revoked);
  384. CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
  385. }
  386. idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
  387. if (idx < 0)
  388. return 0;
  389. /* Need to look for matching name */
  390. for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) {
  391. rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
  392. if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
  393. return 0;
  394. if (crl_revoked_issuer_match(crl, issuer, rev)) {
  395. if (ret)
  396. *ret = rev;
  397. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  398. return 2;
  399. return 1;
  400. }
  401. }
  402. return 0;
  403. }
  404. void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
  405. {
  406. if (meth == NULL)
  407. default_crl_method = &int_crl_meth;
  408. else
  409. default_crl_method = meth;
  410. }
  411. X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl),
  412. int (*crl_free) (X509_CRL *crl),
  413. int (*crl_lookup) (X509_CRL *crl,
  414. X509_REVOKED **ret,
  415. ASN1_INTEGER *ser,
  416. X509_NAME *issuer),
  417. int (*crl_verify) (X509_CRL *crl,
  418. EVP_PKEY *pk))
  419. {
  420. X509_CRL_METHOD *m;
  421. m = OPENSSL_malloc(sizeof(X509_CRL_METHOD));
  422. if (!m)
  423. return NULL;
  424. m->crl_init = crl_init;
  425. m->crl_free = crl_free;
  426. m->crl_lookup = crl_lookup;
  427. m->crl_verify = crl_verify;
  428. m->flags = X509_CRL_METHOD_DYNAMIC;
  429. return m;
  430. }
  431. void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
  432. {
  433. if (!(m->flags & X509_CRL_METHOD_DYNAMIC))
  434. return;
  435. OPENSSL_free(m);
  436. }
  437. void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
  438. {
  439. crl->meth_data = dat;
  440. }
  441. void *X509_CRL_get_meth_data(X509_CRL *crl)
  442. {
  443. return crl->meth_data;
  444. }
  445. IMPLEMENT_STACK_OF(X509_REVOKED)
  446. IMPLEMENT_ASN1_SET_OF(X509_REVOKED)
  447. IMPLEMENT_STACK_OF(X509_CRL)
  448. IMPLEMENT_ASN1_SET_OF(X509_CRL)