x_crl.c 17 KB

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