You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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