x_crl.c 15 KB

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