x_crl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "crypto/x509.h"
  14. #include <openssl/x509v3.h>
  15. #include "x509_local.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_D2I_PRE:
  136. if (crl->meth->crl_free) {
  137. if (!crl->meth->crl_free(crl))
  138. return 0;
  139. }
  140. AUTHORITY_KEYID_free(crl->akid);
  141. ISSUING_DIST_POINT_free(crl->idp);
  142. ASN1_INTEGER_free(crl->crl_number);
  143. ASN1_INTEGER_free(crl->base_crl_number);
  144. sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
  145. /* fall thru */
  146. case ASN1_OP_NEW_POST:
  147. crl->idp = NULL;
  148. crl->akid = NULL;
  149. crl->flags = 0;
  150. crl->idp_flags = 0;
  151. crl->idp_reasons = CRLDP_ALL_REASONS;
  152. crl->meth = default_crl_method;
  153. crl->meth_data = NULL;
  154. crl->issuers = NULL;
  155. crl->crl_number = NULL;
  156. crl->base_crl_number = NULL;
  157. break;
  158. case ASN1_OP_D2I_POST:
  159. X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
  160. crl->idp = X509_CRL_get_ext_d2i(crl,
  161. NID_issuing_distribution_point, NULL,
  162. NULL);
  163. if (crl->idp)
  164. setup_idp(crl, crl->idp);
  165. crl->akid = X509_CRL_get_ext_d2i(crl,
  166. NID_authority_key_identifier, NULL,
  167. NULL);
  168. crl->crl_number = X509_CRL_get_ext_d2i(crl,
  169. NID_crl_number, NULL, NULL);
  170. crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
  171. NID_delta_crl, NULL,
  172. NULL);
  173. /* Delta CRLs must have CRL number */
  174. if (crl->base_crl_number && !crl->crl_number)
  175. crl->flags |= EXFLAG_INVALID;
  176. /*
  177. * See if we have any unhandled critical CRL extensions and indicate
  178. * this in a flag. We only currently handle IDP so anything else
  179. * critical sets the flag. This code accesses the X509_CRL structure
  180. * directly: applications shouldn't do this.
  181. */
  182. exts = crl->crl.extensions;
  183. for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
  184. int nid;
  185. ext = sk_X509_EXTENSION_value(exts, idx);
  186. nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
  187. if (nid == NID_freshest_crl)
  188. crl->flags |= EXFLAG_FRESHEST;
  189. if (X509_EXTENSION_get_critical(ext)) {
  190. /* We handle IDP and deltas */
  191. if ((nid == NID_issuing_distribution_point)
  192. || (nid == NID_authority_key_identifier)
  193. || (nid == NID_delta_crl))
  194. continue;
  195. crl->flags |= EXFLAG_CRITICAL;
  196. break;
  197. }
  198. }
  199. if (!crl_set_issuers(crl))
  200. return 0;
  201. if (crl->meth->crl_init) {
  202. if (crl->meth->crl_init(crl) == 0)
  203. return 0;
  204. }
  205. crl->flags |= EXFLAG_SET;
  206. break;
  207. case ASN1_OP_FREE_POST:
  208. if (crl->meth->crl_free) {
  209. if (!crl->meth->crl_free(crl))
  210. return 0;
  211. }
  212. AUTHORITY_KEYID_free(crl->akid);
  213. ISSUING_DIST_POINT_free(crl->idp);
  214. ASN1_INTEGER_free(crl->crl_number);
  215. ASN1_INTEGER_free(crl->base_crl_number);
  216. sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
  217. break;
  218. }
  219. return 1;
  220. }
  221. /* Convert IDP into a more convenient form */
  222. static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
  223. {
  224. int idp_only = 0;
  225. /* Set various flags according to IDP */
  226. crl->idp_flags |= IDP_PRESENT;
  227. if (idp->onlyuser > 0) {
  228. idp_only++;
  229. crl->idp_flags |= IDP_ONLYUSER;
  230. }
  231. if (idp->onlyCA > 0) {
  232. idp_only++;
  233. crl->idp_flags |= IDP_ONLYCA;
  234. }
  235. if (idp->onlyattr > 0) {
  236. idp_only++;
  237. crl->idp_flags |= IDP_ONLYATTR;
  238. }
  239. if (idp_only > 1)
  240. crl->idp_flags |= IDP_INVALID;
  241. if (idp->indirectCRL > 0)
  242. crl->idp_flags |= IDP_INDIRECT;
  243. if (idp->onlysomereasons) {
  244. crl->idp_flags |= IDP_REASONS;
  245. if (idp->onlysomereasons->length > 0)
  246. crl->idp_reasons = idp->onlysomereasons->data[0];
  247. if (idp->onlysomereasons->length > 1)
  248. crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
  249. crl->idp_reasons &= CRLDP_ALL_REASONS;
  250. }
  251. DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
  252. }
  253. ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
  254. ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
  255. ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
  256. ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
  257. } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
  258. IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
  259. IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
  260. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
  261. IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
  262. IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
  263. static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
  264. const X509_REVOKED *const *b)
  265. {
  266. return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
  267. (ASN1_STRING *)&(*b)->serialNumber));
  268. }
  269. int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
  270. {
  271. X509_CRL_INFO *inf;
  272. inf = &crl->crl;
  273. if (inf->revoked == NULL)
  274. inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
  275. if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
  276. ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
  277. return 0;
  278. }
  279. inf->enc.modified = 1;
  280. return 1;
  281. }
  282. int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
  283. {
  284. if (crl->meth->crl_verify)
  285. return crl->meth->crl_verify(crl, r);
  286. return 0;
  287. }
  288. int X509_CRL_get0_by_serial(X509_CRL *crl,
  289. X509_REVOKED **ret, ASN1_INTEGER *serial)
  290. {
  291. if (crl->meth->crl_lookup)
  292. return crl->meth->crl_lookup(crl, ret, serial, NULL);
  293. return 0;
  294. }
  295. int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
  296. {
  297. if (crl->meth->crl_lookup)
  298. return crl->meth->crl_lookup(crl, ret,
  299. X509_get_serialNumber(x),
  300. X509_get_issuer_name(x));
  301. return 0;
  302. }
  303. static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
  304. {
  305. return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
  306. &crl->sig_alg, &crl->signature, &crl->crl, r));
  307. }
  308. static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
  309. X509_REVOKED *rev)
  310. {
  311. int i;
  312. if (!rev->issuer) {
  313. if (!nm)
  314. return 1;
  315. if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  316. return 1;
  317. return 0;
  318. }
  319. if (!nm)
  320. nm = X509_CRL_get_issuer(crl);
  321. for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
  322. GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
  323. if (gen->type != GEN_DIRNAME)
  324. continue;
  325. if (!X509_NAME_cmp(nm, gen->d.directoryName))
  326. return 1;
  327. }
  328. return 0;
  329. }
  330. static int def_crl_lookup(X509_CRL *crl,
  331. X509_REVOKED **ret, ASN1_INTEGER *serial,
  332. X509_NAME *issuer)
  333. {
  334. X509_REVOKED rtmp, *rev;
  335. int idx, num;
  336. if (crl->crl.revoked == NULL)
  337. return 0;
  338. /*
  339. * Sort revoked into serial number order if not already sorted. Do this
  340. * under a lock to avoid race condition.
  341. */
  342. if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
  343. CRYPTO_THREAD_write_lock(crl->lock);
  344. sk_X509_REVOKED_sort(crl->crl.revoked);
  345. CRYPTO_THREAD_unlock(crl->lock);
  346. }
  347. rtmp.serialNumber = *serial;
  348. idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
  349. if (idx < 0)
  350. return 0;
  351. /* Need to look for matching name */
  352. for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
  353. rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
  354. if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
  355. return 0;
  356. if (crl_revoked_issuer_match(crl, issuer, rev)) {
  357. if (ret)
  358. *ret = rev;
  359. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  360. return 2;
  361. return 1;
  362. }
  363. }
  364. return 0;
  365. }
  366. void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
  367. {
  368. if (meth == NULL)
  369. default_crl_method = &int_crl_meth;
  370. else
  371. default_crl_method = meth;
  372. }
  373. X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl),
  374. int (*crl_free) (X509_CRL *crl),
  375. int (*crl_lookup) (X509_CRL *crl,
  376. X509_REVOKED **ret,
  377. ASN1_INTEGER *ser,
  378. X509_NAME *issuer),
  379. int (*crl_verify) (X509_CRL *crl,
  380. EVP_PKEY *pk))
  381. {
  382. X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
  383. if (m == NULL) {
  384. X509err(X509_F_X509_CRL_METHOD_NEW, ERR_R_MALLOC_FAILURE);
  385. return NULL;
  386. }
  387. m->crl_init = crl_init;
  388. m->crl_free = crl_free;
  389. m->crl_lookup = crl_lookup;
  390. m->crl_verify = crl_verify;
  391. m->flags = X509_CRL_METHOD_DYNAMIC;
  392. return m;
  393. }
  394. void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
  395. {
  396. if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
  397. return;
  398. OPENSSL_free(m);
  399. }
  400. void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
  401. {
  402. crl->meth_data = dat;
  403. }
  404. void *X509_CRL_get_meth_data(X509_CRL *crl)
  405. {
  406. return crl->meth_data;
  407. }