x_crl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include "internal/x509_int.h"
  14. #include <openssl/x509v3.h>
  15. #include "x509_lcl.h"
  16. static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
  17. const X509_REVOKED *const *b);
  18. static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
  19. ASN1_SEQUENCE(X509_REVOKED) = {
  20. ASN1_EMBED(X509_REVOKED,serialNumber, ASN1_INTEGER),
  21. ASN1_SIMPLE(X509_REVOKED,revocationDate, ASN1_TIME),
  22. ASN1_SEQUENCE_OF_OPT(X509_REVOKED,extensions, X509_EXTENSION)
  23. } ASN1_SEQUENCE_END(X509_REVOKED)
  24. static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
  25. static int def_crl_lookup(X509_CRL *crl,
  26. X509_REVOKED **ret, ASN1_INTEGER *serial,
  27. X509_NAME *issuer);
  28. static X509_CRL_METHOD int_crl_meth = {
  29. 0,
  30. 0, 0,
  31. def_crl_lookup,
  32. def_crl_verify
  33. };
  34. static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
  35. /*
  36. * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
  37. * the original encoding the signature won't be affected by reordering of the
  38. * revoked field.
  39. */
  40. static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  41. void *exarg)
  42. {
  43. X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
  44. if (!a || !a->revoked)
  45. return 1;
  46. switch (operation) {
  47. /*
  48. * Just set cmp function here. We don't sort because that would
  49. * affect the output of X509_CRL_print().
  50. */
  51. case ASN1_OP_D2I_POST:
  52. (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
  53. break;
  54. }
  55. return 1;
  56. }
  57. ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
  58. ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
  59. ASN1_EMBED(X509_CRL_INFO, sig_alg, X509_ALGOR),
  60. ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
  61. ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
  62. ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
  63. ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
  64. ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
  65. } ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
  66. /*
  67. * Set CRL entry issuer according to CRL certificate issuer extension. Check
  68. * for unhandled critical CRL entry extensions.
  69. */
  70. static int crl_set_issuers(X509_CRL *crl)
  71. {
  72. int i, j;
  73. GENERAL_NAMES *gens, *gtmp;
  74. STACK_OF(X509_REVOKED) *revoked;
  75. revoked = X509_CRL_get_REVOKED(crl);
  76. gens = NULL;
  77. for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
  78. X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
  79. STACK_OF(X509_EXTENSION) *exts;
  80. ASN1_ENUMERATED *reason;
  81. X509_EXTENSION *ext;
  82. gtmp = X509_REVOKED_get_ext_d2i(rev,
  83. NID_certificate_issuer, &j, NULL);
  84. if (!gtmp && (j != -1)) {
  85. crl->flags |= EXFLAG_INVALID;
  86. return 1;
  87. }
  88. if (gtmp) {
  89. gens = gtmp;
  90. if (!crl->issuers) {
  91. crl->issuers = sk_GENERAL_NAMES_new_null();
  92. if (!crl->issuers)
  93. return 0;
  94. }
  95. if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
  96. return 0;
  97. }
  98. rev->issuer = gens;
  99. reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
  100. if (!reason && (j != -1)) {
  101. crl->flags |= EXFLAG_INVALID;
  102. return 1;
  103. }
  104. if (reason) {
  105. rev->reason = ASN1_ENUMERATED_get(reason);
  106. ASN1_ENUMERATED_free(reason);
  107. } else
  108. rev->reason = CRL_REASON_NONE;
  109. /* Check for critical CRL entry extensions */
  110. exts = rev->extensions;
  111. for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
  112. ext = sk_X509_EXTENSION_value(exts, j);
  113. if (X509_EXTENSION_get_critical(ext)) {
  114. if (OBJ_obj2nid(X509_EXTENSION_get_object(ext)) == NID_certificate_issuer)
  115. continue;
  116. crl->flags |= EXFLAG_CRITICAL;
  117. break;
  118. }
  119. }
  120. }
  121. return 1;
  122. }
  123. /*
  124. * The X509_CRL structure needs a bit of customisation. Cache some extensions
  125. * and hash of the whole CRL.
  126. */
  127. static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  128. void *exarg)
  129. {
  130. X509_CRL *crl = (X509_CRL *)*pval;
  131. STACK_OF(X509_EXTENSION) *exts;
  132. X509_EXTENSION *ext;
  133. int idx;
  134. switch (operation) {
  135. case ASN1_OP_NEW_POST:
  136. crl->idp = NULL;
  137. crl->akid = NULL;
  138. crl->flags = 0;
  139. crl->idp_flags = 0;
  140. crl->idp_reasons = CRLDP_ALL_REASONS;
  141. crl->meth = default_crl_method;
  142. crl->meth_data = NULL;
  143. crl->issuers = NULL;
  144. crl->crl_number = NULL;
  145. crl->base_crl_number = NULL;
  146. break;
  147. case ASN1_OP_D2I_POST:
  148. X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
  149. crl->idp = X509_CRL_get_ext_d2i(crl,
  150. NID_issuing_distribution_point, NULL,
  151. NULL);
  152. if (crl->idp)
  153. setup_idp(crl, crl->idp);
  154. crl->akid = X509_CRL_get_ext_d2i(crl,
  155. NID_authority_key_identifier, NULL,
  156. NULL);
  157. crl->crl_number = X509_CRL_get_ext_d2i(crl,
  158. NID_crl_number, NULL, NULL);
  159. crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
  160. NID_delta_crl, NULL,
  161. NULL);
  162. /* Delta CRLs must have CRL number */
  163. if (crl->base_crl_number && !crl->crl_number)
  164. crl->flags |= EXFLAG_INVALID;
  165. /*
  166. * See if we have any unhandled critical CRL extensions and indicate
  167. * this in a flag. We only currently handle IDP so anything else
  168. * critical sets the flag. This code accesses the X509_CRL structure
  169. * directly: applications shouldn't do this.
  170. */
  171. exts = crl->crl.extensions;
  172. for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
  173. int nid;
  174. ext = sk_X509_EXTENSION_value(exts, idx);
  175. nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
  176. if (nid == NID_freshest_crl)
  177. crl->flags |= EXFLAG_FRESHEST;
  178. if (X509_EXTENSION_get_critical(ext)) {
  179. /* We handle IDP and deltas */
  180. if ((nid == NID_issuing_distribution_point)
  181. || (nid == NID_authority_key_identifier)
  182. || (nid == NID_delta_crl))
  183. continue;
  184. crl->flags |= EXFLAG_CRITICAL;
  185. break;
  186. }
  187. }
  188. if (!crl_set_issuers(crl))
  189. return 0;
  190. if (crl->meth->crl_init) {
  191. if (crl->meth->crl_init(crl) == 0)
  192. return 0;
  193. }
  194. crl->flags |= EXFLAG_SET;
  195. break;
  196. case ASN1_OP_FREE_POST:
  197. if (crl->meth->crl_free) {
  198. if (!crl->meth->crl_free(crl))
  199. return 0;
  200. }
  201. AUTHORITY_KEYID_free(crl->akid);
  202. ISSUING_DIST_POINT_free(crl->idp);
  203. ASN1_INTEGER_free(crl->crl_number);
  204. ASN1_INTEGER_free(crl->base_crl_number);
  205. sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
  206. break;
  207. }
  208. return 1;
  209. }
  210. /* Convert IDP into a more convenient form */
  211. static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
  212. {
  213. int idp_only = 0;
  214. /* Set various flags according to IDP */
  215. crl->idp_flags |= IDP_PRESENT;
  216. if (idp->onlyuser > 0) {
  217. idp_only++;
  218. crl->idp_flags |= IDP_ONLYUSER;
  219. }
  220. if (idp->onlyCA > 0) {
  221. idp_only++;
  222. crl->idp_flags |= IDP_ONLYCA;
  223. }
  224. if (idp->onlyattr > 0) {
  225. idp_only++;
  226. crl->idp_flags |= IDP_ONLYATTR;
  227. }
  228. if (idp_only > 1)
  229. crl->idp_flags |= IDP_INVALID;
  230. if (idp->indirectCRL > 0)
  231. crl->idp_flags |= IDP_INDIRECT;
  232. if (idp->onlysomereasons) {
  233. crl->idp_flags |= IDP_REASONS;
  234. if (idp->onlysomereasons->length > 0)
  235. crl->idp_reasons = idp->onlysomereasons->data[0];
  236. if (idp->onlysomereasons->length > 1)
  237. crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
  238. crl->idp_reasons &= CRLDP_ALL_REASONS;
  239. }
  240. DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
  241. }
  242. ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
  243. ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
  244. ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
  245. ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
  246. } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
  247. IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
  248. IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
  249. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
  250. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
  251. IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
  252. static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
  253. const X509_REVOKED *const *b)
  254. {
  255. return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
  256. (ASN1_STRING *)&(*b)->serialNumber));
  257. }
  258. int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
  259. {
  260. X509_CRL_INFO *inf;
  261. inf = &crl->crl;
  262. if (inf->revoked == NULL)
  263. inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
  264. if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
  265. ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
  266. return 0;
  267. }
  268. inf->enc.modified = 1;
  269. return 1;
  270. }
  271. int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
  272. {
  273. if (crl->meth->crl_verify)
  274. return crl->meth->crl_verify(crl, r);
  275. return 0;
  276. }
  277. int X509_CRL_get0_by_serial(X509_CRL *crl,
  278. X509_REVOKED **ret, ASN1_INTEGER *serial)
  279. {
  280. if (crl->meth->crl_lookup)
  281. return crl->meth->crl_lookup(crl, ret, serial, NULL);
  282. return 0;
  283. }
  284. int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
  285. {
  286. if (crl->meth->crl_lookup)
  287. return crl->meth->crl_lookup(crl, ret,
  288. X509_get_serialNumber(x),
  289. X509_get_issuer_name(x));
  290. return 0;
  291. }
  292. static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
  293. {
  294. return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
  295. &crl->sig_alg, &crl->signature, &crl->crl, r));
  296. }
  297. static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
  298. X509_REVOKED *rev)
  299. {
  300. int i;
  301. if (!rev->issuer) {
  302. if (!nm)
  303. return 1;
  304. if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  305. return 1;
  306. return 0;
  307. }
  308. if (!nm)
  309. nm = X509_CRL_get_issuer(crl);
  310. for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
  311. GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
  312. if (gen->type != GEN_DIRNAME)
  313. continue;
  314. if (!X509_NAME_cmp(nm, gen->d.directoryName))
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. static int def_crl_lookup(X509_CRL *crl,
  320. X509_REVOKED **ret, ASN1_INTEGER *serial,
  321. X509_NAME *issuer)
  322. {
  323. X509_REVOKED rtmp, *rev;
  324. int idx;
  325. rtmp.serialNumber = *serial;
  326. /*
  327. * Sort revoked into serial number order if not already sorted. Do this
  328. * under a lock to avoid race condition.
  329. */
  330. if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
  331. CRYPTO_THREAD_write_lock(crl->lock);
  332. sk_X509_REVOKED_sort(crl->crl.revoked);
  333. CRYPTO_THREAD_unlock(crl->lock);
  334. }
  335. idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
  336. if (idx < 0)
  337. return 0;
  338. /* Need to look for matching name */
  339. for (; idx < sk_X509_REVOKED_num(crl->crl.revoked); idx++) {
  340. rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
  341. if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
  342. return 0;
  343. if (crl_revoked_issuer_match(crl, issuer, rev)) {
  344. if (ret)
  345. *ret = rev;
  346. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  347. return 2;
  348. return 1;
  349. }
  350. }
  351. return 0;
  352. }
  353. void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
  354. {
  355. if (meth == NULL)
  356. default_crl_method = &int_crl_meth;
  357. else
  358. default_crl_method = meth;
  359. }
  360. X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl),
  361. int (*crl_free) (X509_CRL *crl),
  362. int (*crl_lookup) (X509_CRL *crl,
  363. X509_REVOKED **ret,
  364. ASN1_INTEGER *ser,
  365. X509_NAME *issuer),
  366. int (*crl_verify) (X509_CRL *crl,
  367. EVP_PKEY *pk))
  368. {
  369. X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
  370. if (m == NULL) {
  371. X509err(X509_F_X509_CRL_METHOD_NEW, ERR_R_MALLOC_FAILURE);
  372. return NULL;
  373. }
  374. m->crl_init = crl_init;
  375. m->crl_free = crl_free;
  376. m->crl_lookup = crl_lookup;
  377. m->crl_verify = crl_verify;
  378. m->flags = X509_CRL_METHOD_DYNAMIC;
  379. return m;
  380. }
  381. void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
  382. {
  383. if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
  384. return;
  385. OPENSSL_free(m);
  386. }
  387. void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
  388. {
  389. crl->meth_data = dat;
  390. }
  391. void *X509_CRL_get_meth_data(X509_CRL *crl)
  392. {
  393. return crl->meth_data;
  394. }