v3_crld.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright 1999-2022 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/conf.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509v3.h>
  15. #include "crypto/x509.h"
  16. #include "ext_dat.h"
  17. #include "x509_local.h"
  18. static void *v2i_crld(const X509V3_EXT_METHOD *method,
  19. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
  20. static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
  21. int indent);
  22. const X509V3_EXT_METHOD ossl_v3_crld = {
  23. NID_crl_distribution_points, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
  24. 0, 0, 0, 0,
  25. 0, 0,
  26. 0,
  27. v2i_crld,
  28. i2r_crldp, 0,
  29. NULL
  30. };
  31. const X509V3_EXT_METHOD ossl_v3_freshest_crl = {
  32. NID_freshest_crl, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
  33. 0, 0, 0, 0,
  34. 0, 0,
  35. 0,
  36. v2i_crld,
  37. i2r_crldp, 0,
  38. NULL
  39. };
  40. static STACK_OF(GENERAL_NAME) *gnames_from_sectname(X509V3_CTX *ctx,
  41. char *sect)
  42. {
  43. STACK_OF(CONF_VALUE) *gnsect;
  44. STACK_OF(GENERAL_NAME) *gens;
  45. if (*sect == '@')
  46. gnsect = X509V3_get_section(ctx, sect + 1);
  47. else
  48. gnsect = X509V3_parse_list(sect);
  49. if (!gnsect) {
  50. ERR_raise(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND);
  51. return NULL;
  52. }
  53. gens = v2i_GENERAL_NAMES(NULL, ctx, gnsect);
  54. if (*sect == '@')
  55. X509V3_section_free(ctx, gnsect);
  56. else
  57. sk_CONF_VALUE_pop_free(gnsect, X509V3_conf_free);
  58. return gens;
  59. }
  60. static int set_dist_point_name(DIST_POINT_NAME **pdp, X509V3_CTX *ctx,
  61. CONF_VALUE *cnf)
  62. {
  63. STACK_OF(GENERAL_NAME) *fnm = NULL;
  64. STACK_OF(X509_NAME_ENTRY) *rnm = NULL;
  65. if (HAS_PREFIX(cnf->name, "fullname")) {
  66. fnm = gnames_from_sectname(ctx, cnf->value);
  67. if (!fnm)
  68. goto err;
  69. } else if (strcmp(cnf->name, "relativename") == 0) {
  70. int ret;
  71. STACK_OF(CONF_VALUE) *dnsect;
  72. X509_NAME *nm;
  73. nm = X509_NAME_new();
  74. if (nm == NULL)
  75. return -1;
  76. dnsect = X509V3_get_section(ctx, cnf->value);
  77. if (!dnsect) {
  78. X509_NAME_free(nm);
  79. ERR_raise(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND);
  80. return -1;
  81. }
  82. ret = X509V3_NAME_from_section(nm, dnsect, MBSTRING_ASC);
  83. X509V3_section_free(ctx, dnsect);
  84. rnm = nm->entries;
  85. nm->entries = NULL;
  86. X509_NAME_free(nm);
  87. if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0)
  88. goto err;
  89. /*
  90. * Since its a name fragment can't have more than one RDNSequence
  91. */
  92. if (sk_X509_NAME_ENTRY_value(rnm,
  93. sk_X509_NAME_ENTRY_num(rnm) - 1)->set) {
  94. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_MULTIPLE_RDNS);
  95. goto err;
  96. }
  97. } else
  98. return 0;
  99. if (*pdp) {
  100. ERR_raise(ERR_LIB_X509V3, X509V3_R_DISTPOINT_ALREADY_SET);
  101. goto err;
  102. }
  103. *pdp = DIST_POINT_NAME_new();
  104. if (*pdp == NULL)
  105. goto err;
  106. if (fnm) {
  107. (*pdp)->type = 0;
  108. (*pdp)->name.fullname = fnm;
  109. } else {
  110. (*pdp)->type = 1;
  111. (*pdp)->name.relativename = rnm;
  112. }
  113. return 1;
  114. err:
  115. sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free);
  116. sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free);
  117. return -1;
  118. }
  119. static const BIT_STRING_BITNAME reason_flags[] = {
  120. {0, "Unused", "unused"},
  121. {1, "Key Compromise", "keyCompromise"},
  122. {2, "CA Compromise", "CACompromise"},
  123. {3, "Affiliation Changed", "affiliationChanged"},
  124. {4, "Superseded", "superseded"},
  125. {5, "Cessation Of Operation", "cessationOfOperation"},
  126. {6, "Certificate Hold", "certificateHold"},
  127. {7, "Privilege Withdrawn", "privilegeWithdrawn"},
  128. {8, "AA Compromise", "AACompromise"},
  129. {-1, NULL, NULL}
  130. };
  131. static int set_reasons(ASN1_BIT_STRING **preas, char *value)
  132. {
  133. STACK_OF(CONF_VALUE) *rsk = NULL;
  134. const BIT_STRING_BITNAME *pbn;
  135. const char *bnam;
  136. int i, ret = 0;
  137. rsk = X509V3_parse_list(value);
  138. if (rsk == NULL)
  139. return 0;
  140. if (*preas != NULL)
  141. goto err;
  142. for (i = 0; i < sk_CONF_VALUE_num(rsk); i++) {
  143. bnam = sk_CONF_VALUE_value(rsk, i)->name;
  144. if (*preas == NULL) {
  145. *preas = ASN1_BIT_STRING_new();
  146. if (*preas == NULL)
  147. goto err;
  148. }
  149. for (pbn = reason_flags; pbn->lname; pbn++) {
  150. if (strcmp(pbn->sname, bnam) == 0) {
  151. if (!ASN1_BIT_STRING_set_bit(*preas, pbn->bitnum, 1))
  152. goto err;
  153. break;
  154. }
  155. }
  156. if (pbn->lname == NULL)
  157. goto err;
  158. }
  159. ret = 1;
  160. err:
  161. sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free);
  162. return ret;
  163. }
  164. static int print_reasons(BIO *out, const char *rname,
  165. ASN1_BIT_STRING *rflags, int indent)
  166. {
  167. int first = 1;
  168. const BIT_STRING_BITNAME *pbn;
  169. BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, "");
  170. for (pbn = reason_flags; pbn->lname; pbn++) {
  171. if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum)) {
  172. if (first)
  173. first = 0;
  174. else
  175. BIO_puts(out, ", ");
  176. BIO_puts(out, pbn->lname);
  177. }
  178. }
  179. if (first)
  180. BIO_puts(out, "<EMPTY>\n");
  181. else
  182. BIO_puts(out, "\n");
  183. return 1;
  184. }
  185. static DIST_POINT *crldp_from_section(X509V3_CTX *ctx,
  186. STACK_OF(CONF_VALUE) *nval)
  187. {
  188. int i;
  189. CONF_VALUE *cnf;
  190. DIST_POINT *point = DIST_POINT_new();
  191. if (point == NULL)
  192. goto err;
  193. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  194. int ret;
  195. cnf = sk_CONF_VALUE_value(nval, i);
  196. ret = set_dist_point_name(&point->distpoint, ctx, cnf);
  197. if (ret > 0)
  198. continue;
  199. if (ret < 0)
  200. goto err;
  201. if (strcmp(cnf->name, "reasons") == 0) {
  202. if (!set_reasons(&point->reasons, cnf->value))
  203. goto err;
  204. } else if (strcmp(cnf->name, "CRLissuer") == 0) {
  205. point->CRLissuer = gnames_from_sectname(ctx, cnf->value);
  206. if (point->CRLissuer == NULL)
  207. goto err;
  208. }
  209. }
  210. return point;
  211. err:
  212. DIST_POINT_free(point);
  213. return NULL;
  214. }
  215. static void *v2i_crld(const X509V3_EXT_METHOD *method,
  216. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  217. {
  218. STACK_OF(DIST_POINT) *crld;
  219. GENERAL_NAMES *gens = NULL;
  220. GENERAL_NAME *gen = NULL;
  221. CONF_VALUE *cnf;
  222. const int num = sk_CONF_VALUE_num(nval);
  223. int i;
  224. crld = sk_DIST_POINT_new_reserve(NULL, num);
  225. if (crld == NULL) {
  226. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  227. goto err;
  228. }
  229. for (i = 0; i < num; i++) {
  230. DIST_POINT *point;
  231. cnf = sk_CONF_VALUE_value(nval, i);
  232. if (cnf->value == NULL) {
  233. STACK_OF(CONF_VALUE) *dpsect;
  234. dpsect = X509V3_get_section(ctx, cnf->name);
  235. if (!dpsect)
  236. goto err;
  237. point = crldp_from_section(ctx, dpsect);
  238. X509V3_section_free(ctx, dpsect);
  239. if (point == NULL)
  240. goto err;
  241. sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
  242. } else {
  243. if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
  244. goto err;
  245. if ((gens = GENERAL_NAMES_new()) == NULL) {
  246. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  247. goto err;
  248. }
  249. if (!sk_GENERAL_NAME_push(gens, gen)) {
  250. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  251. goto err;
  252. }
  253. gen = NULL;
  254. if ((point = DIST_POINT_new()) == NULL) {
  255. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  256. goto err;
  257. }
  258. sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
  259. if ((point->distpoint = DIST_POINT_NAME_new()) == NULL) {
  260. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  261. goto err;
  262. }
  263. point->distpoint->name.fullname = gens;
  264. point->distpoint->type = 0;
  265. gens = NULL;
  266. }
  267. }
  268. return crld;
  269. err:
  270. GENERAL_NAME_free(gen);
  271. GENERAL_NAMES_free(gens);
  272. sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
  273. return NULL;
  274. }
  275. static int dpn_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  276. void *exarg)
  277. {
  278. DIST_POINT_NAME *dpn = (DIST_POINT_NAME *)*pval;
  279. switch (operation) {
  280. case ASN1_OP_NEW_POST:
  281. dpn->dpname = NULL;
  282. break;
  283. case ASN1_OP_FREE_POST:
  284. X509_NAME_free(dpn->dpname);
  285. break;
  286. }
  287. return 1;
  288. }
  289. ASN1_CHOICE_cb(DIST_POINT_NAME, dpn_cb) = {
  290. ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0),
  291. ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1)
  292. } ASN1_CHOICE_END_cb(DIST_POINT_NAME, DIST_POINT_NAME, type)
  293. IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT_NAME)
  294. ASN1_SEQUENCE(DIST_POINT) = {
  295. ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0),
  296. ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1),
  297. ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2)
  298. } ASN1_SEQUENCE_END(DIST_POINT)
  299. IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT)
  300. ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) =
  301. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT)
  302. ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS)
  303. IMPLEMENT_ASN1_FUNCTIONS(CRL_DIST_POINTS)
  304. ASN1_SEQUENCE(ISSUING_DIST_POINT) = {
  305. ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0),
  306. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1),
  307. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2),
  308. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3),
  309. ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4),
  310. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5)
  311. } ASN1_SEQUENCE_END(ISSUING_DIST_POINT)
  312. IMPLEMENT_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
  313. static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
  314. int indent);
  315. static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  316. STACK_OF(CONF_VALUE) *nval);
  317. const X509V3_EXT_METHOD ossl_v3_idp = {
  318. NID_issuing_distribution_point, X509V3_EXT_MULTILINE,
  319. ASN1_ITEM_ref(ISSUING_DIST_POINT),
  320. 0, 0, 0, 0,
  321. 0, 0,
  322. 0,
  323. v2i_idp,
  324. i2r_idp, 0,
  325. NULL
  326. };
  327. static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  328. STACK_OF(CONF_VALUE) *nval)
  329. {
  330. ISSUING_DIST_POINT *idp = NULL;
  331. CONF_VALUE *cnf;
  332. char *name, *val;
  333. int i, ret;
  334. idp = ISSUING_DIST_POINT_new();
  335. if (idp == NULL) {
  336. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  337. goto err;
  338. }
  339. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  340. cnf = sk_CONF_VALUE_value(nval, i);
  341. name = cnf->name;
  342. val = cnf->value;
  343. ret = set_dist_point_name(&idp->distpoint, ctx, cnf);
  344. if (ret > 0)
  345. continue;
  346. if (ret < 0)
  347. goto err;
  348. if (strcmp(name, "onlyuser") == 0) {
  349. if (!X509V3_get_value_bool(cnf, &idp->onlyuser))
  350. goto err;
  351. } else if (strcmp(name, "onlyCA") == 0) {
  352. if (!X509V3_get_value_bool(cnf, &idp->onlyCA))
  353. goto err;
  354. } else if (strcmp(name, "onlyAA") == 0) {
  355. if (!X509V3_get_value_bool(cnf, &idp->onlyattr))
  356. goto err;
  357. } else if (strcmp(name, "indirectCRL") == 0) {
  358. if (!X509V3_get_value_bool(cnf, &idp->indirectCRL))
  359. goto err;
  360. } else if (strcmp(name, "onlysomereasons") == 0) {
  361. if (!set_reasons(&idp->onlysomereasons, val))
  362. goto err;
  363. } else {
  364. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NAME);
  365. X509V3_conf_add_error_name_value(cnf);
  366. goto err;
  367. }
  368. }
  369. return idp;
  370. err:
  371. ISSUING_DIST_POINT_free(idp);
  372. return NULL;
  373. }
  374. static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent)
  375. {
  376. int i;
  377. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  378. if (i > 0)
  379. BIO_puts(out, "\n");
  380. BIO_printf(out, "%*s", indent + 2, "");
  381. GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
  382. }
  383. return 1;
  384. }
  385. static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent)
  386. {
  387. if (dpn->type == 0) {
  388. BIO_printf(out, "%*sFull Name:\n", indent, "");
  389. print_gens(out, dpn->name.fullname, indent);
  390. } else {
  391. X509_NAME ntmp;
  392. ntmp.entries = dpn->name.relativename;
  393. BIO_printf(out, "%*sRelative Name:\n%*s", indent, "", indent + 2, "");
  394. X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE);
  395. BIO_puts(out, "\n");
  396. }
  397. return 1;
  398. }
  399. static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
  400. int indent)
  401. {
  402. ISSUING_DIST_POINT *idp = pidp;
  403. if (idp->distpoint)
  404. print_distpoint(out, idp->distpoint, indent);
  405. if (idp->onlyuser > 0)
  406. BIO_printf(out, "%*sOnly User Certificates\n", indent, "");
  407. if (idp->onlyCA > 0)
  408. BIO_printf(out, "%*sOnly CA Certificates\n", indent, "");
  409. if (idp->indirectCRL > 0)
  410. BIO_printf(out, "%*sIndirect CRL\n", indent, "");
  411. if (idp->onlysomereasons)
  412. print_reasons(out, "Only Some Reasons", idp->onlysomereasons, indent);
  413. if (idp->onlyattr > 0)
  414. BIO_printf(out, "%*sOnly Attribute Certificates\n", indent, "");
  415. if (!idp->distpoint && (idp->onlyuser <= 0) && (idp->onlyCA <= 0)
  416. && (idp->indirectCRL <= 0) && !idp->onlysomereasons
  417. && (idp->onlyattr <= 0))
  418. BIO_printf(out, "%*s<EMPTY>\n", indent, "");
  419. return 1;
  420. }
  421. static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
  422. int indent)
  423. {
  424. STACK_OF(DIST_POINT) *crld = pcrldp;
  425. DIST_POINT *point;
  426. int i;
  427. for (i = 0; i < sk_DIST_POINT_num(crld); i++) {
  428. if (i > 0)
  429. BIO_puts(out, "\n");
  430. point = sk_DIST_POINT_value(crld, i);
  431. if (point->distpoint)
  432. print_distpoint(out, point->distpoint, indent);
  433. if (point->reasons)
  434. print_reasons(out, "Reasons", point->reasons, indent);
  435. if (point->CRLissuer) {
  436. BIO_printf(out, "%*sCRL Issuer:\n", indent, "");
  437. print_gens(out, point->CRLissuer, indent);
  438. }
  439. }
  440. return 1;
  441. }
  442. /* Append any nameRelativeToCRLIssuer in dpn to iname, set in dpn->dpname */
  443. int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, const X509_NAME *iname)
  444. {
  445. int i;
  446. STACK_OF(X509_NAME_ENTRY) *frag;
  447. X509_NAME_ENTRY *ne;
  448. if (dpn == NULL || dpn->type != 1)
  449. return 1;
  450. frag = dpn->name.relativename;
  451. X509_NAME_free(dpn->dpname); /* just in case it was already set */
  452. dpn->dpname = X509_NAME_dup(iname);
  453. if (dpn->dpname == NULL)
  454. return 0;
  455. for (i = 0; i < sk_X509_NAME_ENTRY_num(frag); i++) {
  456. ne = sk_X509_NAME_ENTRY_value(frag, i);
  457. if (!X509_NAME_add_entry(dpn->dpname, ne, -1, i ? 0 : 1))
  458. goto err;
  459. }
  460. /* generate cached encoding of name */
  461. if (i2d_X509_NAME(dpn->dpname, NULL) >= 0)
  462. return 1;
  463. err:
  464. X509_NAME_free(dpn->dpname);
  465. dpn->dpname = NULL;
  466. return 0;
  467. }