2
0

x_crl.c 16 KB

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