x_crl.c 15 KB

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