Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

542 rindas
18 KiB

  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. continue;
  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. /* |crl->meth| may be NULL if constructing the object failed before
  263. * |ASN1_OP_NEW_POST| was run. */
  264. if (crl->meth && crl->meth->crl_free) {
  265. if (!crl->meth->crl_free(crl))
  266. return 0;
  267. }
  268. if (crl->akid)
  269. AUTHORITY_KEYID_free(crl->akid);
  270. if (crl->idp)
  271. ISSUING_DIST_POINT_free(crl->idp);
  272. ASN1_INTEGER_free(crl->crl_number);
  273. ASN1_INTEGER_free(crl->base_crl_number);
  274. sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
  275. break;
  276. }
  277. return 1;
  278. }
  279. /* Convert IDP into a more convenient form */
  280. static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
  281. {
  282. int idp_only = 0;
  283. /* Set various flags according to IDP */
  284. crl->idp_flags |= IDP_PRESENT;
  285. if (idp->onlyuser > 0) {
  286. idp_only++;
  287. crl->idp_flags |= IDP_ONLYUSER;
  288. }
  289. if (idp->onlyCA > 0) {
  290. idp_only++;
  291. crl->idp_flags |= IDP_ONLYCA;
  292. }
  293. if (idp->onlyattr > 0) {
  294. idp_only++;
  295. crl->idp_flags |= IDP_ONLYATTR;
  296. }
  297. if (idp_only > 1)
  298. crl->idp_flags |= IDP_INVALID;
  299. if (idp->indirectCRL > 0)
  300. crl->idp_flags |= IDP_INDIRECT;
  301. if (idp->onlysomereasons) {
  302. crl->idp_flags |= IDP_REASONS;
  303. if (idp->onlysomereasons->length > 0)
  304. crl->idp_reasons = idp->onlysomereasons->data[0];
  305. if (idp->onlysomereasons->length > 1)
  306. crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
  307. crl->idp_reasons &= CRLDP_ALL_REASONS;
  308. }
  309. DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
  310. }
  311. ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
  312. ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
  313. ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
  314. ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING)
  315. } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
  316. IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
  317. IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
  318. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
  319. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
  320. IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
  321. static int X509_REVOKED_cmp(const X509_REVOKED **a, const X509_REVOKED **b)
  322. {
  323. return (ASN1_STRING_cmp((ASN1_STRING *)(*a)->serialNumber,
  324. (ASN1_STRING *)(*b)->serialNumber));
  325. }
  326. int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
  327. {
  328. X509_CRL_INFO *inf;
  329. inf = crl->crl;
  330. if (!inf->revoked)
  331. inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
  332. if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
  333. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  334. return 0;
  335. }
  336. inf->enc.modified = 1;
  337. return 1;
  338. }
  339. int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
  340. {
  341. if (crl->meth->crl_verify)
  342. return crl->meth->crl_verify(crl, r);
  343. return 0;
  344. }
  345. int X509_CRL_get0_by_serial(X509_CRL *crl,
  346. X509_REVOKED **ret, ASN1_INTEGER *serial)
  347. {
  348. if (crl->meth->crl_lookup)
  349. return crl->meth->crl_lookup(crl, ret, serial, NULL);
  350. return 0;
  351. }
  352. int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
  353. {
  354. if (crl->meth->crl_lookup)
  355. return crl->meth->crl_lookup(crl, ret,
  356. X509_get_serialNumber(x),
  357. X509_get_issuer_name(x));
  358. return 0;
  359. }
  360. static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
  361. {
  362. return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
  363. crl->sig_alg, crl->signature, crl->crl, r));
  364. }
  365. static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
  366. X509_REVOKED *rev)
  367. {
  368. size_t i;
  369. if (!rev->issuer) {
  370. if (!nm)
  371. return 1;
  372. if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  373. return 1;
  374. return 0;
  375. }
  376. if (!nm)
  377. nm = X509_CRL_get_issuer(crl);
  378. for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
  379. GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
  380. if (gen->type != GEN_DIRNAME)
  381. continue;
  382. if (!X509_NAME_cmp(nm, gen->d.directoryName))
  383. return 1;
  384. }
  385. return 0;
  386. }
  387. static struct CRYPTO_STATIC_MUTEX g_crl_sort_lock = CRYPTO_STATIC_MUTEX_INIT;
  388. static int def_crl_lookup(X509_CRL *crl,
  389. X509_REVOKED **ret, ASN1_INTEGER *serial,
  390. X509_NAME *issuer)
  391. {
  392. X509_REVOKED rtmp, *rev;
  393. size_t idx;
  394. rtmp.serialNumber = serial;
  395. /*
  396. * Sort revoked into serial number order if not already sorted. Do this
  397. * under a lock to avoid race condition.
  398. */
  399. CRYPTO_STATIC_MUTEX_lock_read(&g_crl_sort_lock);
  400. const int is_sorted = sk_X509_REVOKED_is_sorted(crl->crl->revoked);
  401. CRYPTO_STATIC_MUTEX_unlock_read(&g_crl_sort_lock);
  402. if (!is_sorted) {
  403. CRYPTO_STATIC_MUTEX_lock_write(&g_crl_sort_lock);
  404. if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) {
  405. sk_X509_REVOKED_sort(crl->crl->revoked);
  406. }
  407. CRYPTO_STATIC_MUTEX_unlock_write(&g_crl_sort_lock);
  408. }
  409. if (!sk_X509_REVOKED_find(crl->crl->revoked, &idx, &rtmp))
  410. return 0;
  411. /* Need to look for matching name */
  412. for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) {
  413. rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
  414. if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
  415. return 0;
  416. if (crl_revoked_issuer_match(crl, issuer, rev)) {
  417. if (ret)
  418. *ret = rev;
  419. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  420. return 2;
  421. return 1;
  422. }
  423. }
  424. return 0;
  425. }
  426. void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
  427. {
  428. if (meth == NULL)
  429. default_crl_method = &int_crl_meth;
  430. else
  431. default_crl_method = meth;
  432. }
  433. X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl),
  434. int (*crl_free) (X509_CRL *crl),
  435. int (*crl_lookup) (X509_CRL *crl,
  436. X509_REVOKED **ret,
  437. ASN1_INTEGER *ser,
  438. X509_NAME *issuer),
  439. int (*crl_verify) (X509_CRL *crl,
  440. EVP_PKEY *pk))
  441. {
  442. X509_CRL_METHOD *m;
  443. m = OPENSSL_malloc(sizeof(X509_CRL_METHOD));
  444. if (!m)
  445. return NULL;
  446. m->crl_init = crl_init;
  447. m->crl_free = crl_free;
  448. m->crl_lookup = crl_lookup;
  449. m->crl_verify = crl_verify;
  450. m->flags = X509_CRL_METHOD_DYNAMIC;
  451. return m;
  452. }
  453. void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
  454. {
  455. if (!(m->flags & X509_CRL_METHOD_DYNAMIC))
  456. return;
  457. OPENSSL_free(m);
  458. }
  459. void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
  460. {
  461. crl->meth_data = dat;
  462. }
  463. void *X509_CRL_get_meth_data(X509_CRL *crl)
  464. {
  465. return crl->meth_data;
  466. }
  467. IMPLEMENT_ASN1_SET_OF(X509_REVOKED)
  468. IMPLEMENT_ASN1_SET_OF(X509_CRL)