v3_crld.c 15 KB

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