2
0

v3_crld.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /* v3_crld.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 1999.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999, 2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/conf.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/asn1t.h>
  63. #include <openssl/x509v3.h>
  64. static void *v2i_crld(X509V3_EXT_METHOD *method,
  65. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
  66. static int i2r_crldp(X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
  67. int indent);
  68. X509V3_EXT_METHOD v3_crld =
  69. {
  70. NID_crl_distribution_points, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
  71. 0,0,0,0,
  72. 0,0,
  73. 0,
  74. v2i_crld,
  75. i2r_crldp,0,
  76. NULL
  77. };
  78. static STACK_OF(GENERAL_NAME) *gnames_from_sectname(X509V3_CTX *ctx, char *sect)
  79. {
  80. STACK_OF(CONF_VALUE) *gnsect;
  81. STACK_OF(GENERAL_NAME) *gens;
  82. if (*sect == '@')
  83. gnsect = X509V3_get_section(ctx, sect + 1);
  84. else
  85. gnsect = X509V3_parse_list(sect);
  86. if (!gnsect)
  87. {
  88. X509V3err(X509V3_F_GNAMES_FROM_SECTNAME,
  89. X509V3_R_SECTION_NOT_FOUND);
  90. return NULL;
  91. }
  92. gens = v2i_GENERAL_NAMES(NULL, ctx, gnsect);
  93. if (*sect == '@')
  94. X509V3_section_free(ctx, gnsect);
  95. else
  96. sk_CONF_VALUE_pop_free(gnsect, X509V3_conf_free);
  97. return gens;
  98. }
  99. static int set_dist_point_name(DIST_POINT_NAME **pdp, X509V3_CTX *ctx,
  100. CONF_VALUE *cnf)
  101. {
  102. STACK_OF(GENERAL_NAME) *fnm = NULL;
  103. STACK_OF(X509_NAME_ENTRY) *rnm = NULL;
  104. if (!strncmp(cnf->name, "fullname", 9))
  105. {
  106. fnm = gnames_from_sectname(ctx, cnf->value);
  107. if (!fnm)
  108. goto err;
  109. }
  110. else if (!strcmp(cnf->name, "relativename"))
  111. {
  112. int ret;
  113. STACK_OF(CONF_VALUE) *dnsect;
  114. X509_NAME *nm;
  115. nm = X509_NAME_new();
  116. if (!nm)
  117. return -1;
  118. dnsect = X509V3_get_section(ctx, cnf->value);
  119. if (!dnsect)
  120. {
  121. X509V3err(X509V3_F_SET_DIST_POINT_NAME,
  122. X509V3_R_SECTION_NOT_FOUND);
  123. return -1;
  124. }
  125. ret = X509V3_NAME_from_section(nm, dnsect, MBSTRING_ASC);
  126. X509V3_section_free(ctx, dnsect);
  127. rnm = nm->entries;
  128. nm->entries = NULL;
  129. X509_NAME_free(nm);
  130. if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0)
  131. goto err;
  132. /* Since its a name fragment can't have more than one
  133. * RDNSequence
  134. */
  135. if (sk_X509_NAME_ENTRY_value(rnm,
  136. sk_X509_NAME_ENTRY_num(rnm) - 1)->set)
  137. {
  138. X509V3err(X509V3_F_SET_DIST_POINT_NAME,
  139. X509V3_R_INVAID_MULTIPLE_RDNS);
  140. goto err;
  141. }
  142. }
  143. else
  144. return 0;
  145. if (*pdp)
  146. {
  147. X509V3err(X509V3_F_SET_DIST_POINT_NAME,
  148. X509V3_R_DISTPOINT_ALREADY_SET);
  149. goto err;
  150. }
  151. *pdp = DIST_POINT_NAME_new();
  152. if (!*pdp)
  153. goto err;
  154. if (fnm)
  155. {
  156. (*pdp)->type = 0;
  157. (*pdp)->name.fullname = fnm;
  158. }
  159. else
  160. {
  161. (*pdp)->type = 1;
  162. (*pdp)->name.relativename = rnm;
  163. }
  164. return 1;
  165. err:
  166. if (fnm)
  167. sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free);
  168. if (rnm)
  169. sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free);
  170. return -1;
  171. }
  172. static const BIT_STRING_BITNAME reason_flags[] = {
  173. {1, "Key Compromise", "keyCompromise"},
  174. {2, "CA Compromise", "CACompromise"},
  175. {3, "Affiliation Changed", "affiliationChanged"},
  176. {4, "Superseded", "superseded"},
  177. {5, "Cessation Of Operation", "cessationOfOperation"},
  178. {6, "Certificate Hold", "certificateHold"},
  179. {7, "Privilege Withdrawn", "privilegeWithdrawn"},
  180. {8, "AA Compromise", "AACompromise"},
  181. {-1, NULL, NULL}
  182. };
  183. static int set_reasons(ASN1_BIT_STRING **preas, char *value)
  184. {
  185. STACK_OF(CONF_VALUE) *rsk = NULL;
  186. const BIT_STRING_BITNAME *pbn;
  187. const char *bnam;
  188. int i, ret = 0;
  189. rsk = X509V3_parse_list(value);
  190. if (!rsk)
  191. return 0;
  192. if (*preas)
  193. return 0;
  194. for (i = 0; i < sk_CONF_VALUE_num(rsk); i++)
  195. {
  196. bnam = sk_CONF_VALUE_value(rsk, i)->name;
  197. if (!*preas)
  198. {
  199. *preas = ASN1_BIT_STRING_new();
  200. if (!*preas)
  201. goto err;
  202. }
  203. for (pbn = reason_flags; pbn->lname; pbn++)
  204. {
  205. if (!strcmp(pbn->sname, bnam))
  206. {
  207. if (!ASN1_BIT_STRING_set_bit(*preas,
  208. pbn->bitnum, 1))
  209. goto err;
  210. break;
  211. }
  212. }
  213. if (!pbn->lname)
  214. goto err;
  215. }
  216. ret = 1;
  217. err:
  218. sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free);
  219. return ret;
  220. }
  221. static int print_reasons(BIO *out, const char *rname,
  222. ASN1_BIT_STRING *rflags, int indent)
  223. {
  224. int first = 1;
  225. const BIT_STRING_BITNAME *pbn;
  226. BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, "");
  227. for (pbn = reason_flags; pbn->lname; pbn++)
  228. {
  229. if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum))
  230. {
  231. if (first)
  232. first = 0;
  233. else
  234. BIO_puts(out, ", ");
  235. BIO_puts(out, pbn->lname);
  236. }
  237. }
  238. if (first)
  239. BIO_puts(out, "<EMPTY>\n");
  240. else
  241. BIO_puts(out, "\n");
  242. return 1;
  243. }
  244. static DIST_POINT *crldp_from_section(X509V3_CTX *ctx,
  245. STACK_OF(CONF_VALUE) *nval)
  246. {
  247. int i;
  248. CONF_VALUE *cnf;
  249. DIST_POINT *point = NULL;
  250. point = DIST_POINT_new();
  251. if (!point)
  252. goto err;
  253. for(i = 0; i < sk_CONF_VALUE_num(nval); i++)
  254. {
  255. int ret;
  256. cnf = sk_CONF_VALUE_value(nval, i);
  257. ret = set_dist_point_name(&point->distpoint, ctx, cnf);
  258. if (ret > 0)
  259. continue;
  260. if (ret < 0)
  261. goto err;
  262. if (!strcmp(cnf->name, "reasons"))
  263. {
  264. if (!set_reasons(&point->reasons, cnf->value))
  265. goto err;
  266. }
  267. else if (!strcmp(cnf->name, "CRLissuer"))
  268. {
  269. point->CRLissuer =
  270. gnames_from_sectname(ctx, cnf->value);
  271. if (!point->CRLissuer)
  272. goto err;
  273. }
  274. }
  275. return point;
  276. err:
  277. if (point)
  278. DIST_POINT_free(point);
  279. return NULL;
  280. }
  281. static void *v2i_crld(X509V3_EXT_METHOD *method,
  282. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  283. {
  284. STACK_OF(DIST_POINT) *crld = NULL;
  285. GENERAL_NAMES *gens = NULL;
  286. GENERAL_NAME *gen = NULL;
  287. CONF_VALUE *cnf;
  288. int i;
  289. if(!(crld = sk_DIST_POINT_new_null())) goto merr;
  290. for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  291. DIST_POINT *point;
  292. cnf = sk_CONF_VALUE_value(nval, i);
  293. if (!cnf->value)
  294. {
  295. STACK_OF(CONF_VALUE) *dpsect;
  296. dpsect = X509V3_get_section(ctx, cnf->name);
  297. if (!dpsect)
  298. goto err;
  299. point = crldp_from_section(ctx, dpsect);
  300. X509V3_section_free(ctx, dpsect);
  301. if (!point)
  302. goto err;
  303. if(!sk_DIST_POINT_push(crld, point))
  304. {
  305. DIST_POINT_free(point);
  306. goto merr;
  307. }
  308. }
  309. else
  310. {
  311. if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
  312. goto err;
  313. if(!(gens = GENERAL_NAMES_new()))
  314. goto merr;
  315. if(!sk_GENERAL_NAME_push(gens, gen))
  316. goto merr;
  317. gen = NULL;
  318. if(!(point = DIST_POINT_new()))
  319. goto merr;
  320. if(!sk_DIST_POINT_push(crld, point))
  321. {
  322. DIST_POINT_free(point);
  323. goto merr;
  324. }
  325. if(!(point->distpoint = DIST_POINT_NAME_new()))
  326. goto merr;
  327. point->distpoint->name.fullname = gens;
  328. point->distpoint->type = 0;
  329. gens = NULL;
  330. }
  331. }
  332. return crld;
  333. merr:
  334. X509V3err(X509V3_F_V2I_CRLD,ERR_R_MALLOC_FAILURE);
  335. err:
  336. GENERAL_NAME_free(gen);
  337. GENERAL_NAMES_free(gens);
  338. sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
  339. return NULL;
  340. }
  341. IMPLEMENT_STACK_OF(DIST_POINT)
  342. IMPLEMENT_ASN1_SET_OF(DIST_POINT)
  343. ASN1_CHOICE(DIST_POINT_NAME) = {
  344. ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0),
  345. ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1)
  346. } ASN1_CHOICE_END(DIST_POINT_NAME)
  347. IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT_NAME)
  348. ASN1_SEQUENCE(DIST_POINT) = {
  349. ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0),
  350. ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1),
  351. ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2)
  352. } ASN1_SEQUENCE_END(DIST_POINT)
  353. IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT)
  354. ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) =
  355. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT)
  356. ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS)
  357. IMPLEMENT_ASN1_FUNCTIONS(CRL_DIST_POINTS)
  358. ASN1_SEQUENCE(ISSUING_DIST_POINT) = {
  359. ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0),
  360. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1),
  361. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2),
  362. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3),
  363. ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4),
  364. ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5)
  365. } ASN1_SEQUENCE_END(ISSUING_DIST_POINT)
  366. IMPLEMENT_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
  367. static int i2r_idp(X509V3_EXT_METHOD *method,
  368. void *pidp, BIO *out, int indent);
  369. static void *v2i_idp(X509V3_EXT_METHOD *method,
  370. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
  371. X509V3_EXT_METHOD v3_idp =
  372. {
  373. NID_issuing_distribution_point, X509V3_EXT_MULTILINE,
  374. ASN1_ITEM_ref(ISSUING_DIST_POINT),
  375. 0,0,0,0,
  376. 0,0,
  377. 0,
  378. v2i_idp,
  379. i2r_idp,0,
  380. NULL
  381. };
  382. static void *v2i_idp(X509V3_EXT_METHOD *method,
  383. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  384. {
  385. ISSUING_DIST_POINT *idp = NULL;
  386. CONF_VALUE *cnf;
  387. char *name, *val;
  388. int i, ret;
  389. idp = ISSUING_DIST_POINT_new();
  390. if (!idp)
  391. goto merr;
  392. for(i = 0; i < sk_CONF_VALUE_num(nval); i++)
  393. {
  394. cnf = sk_CONF_VALUE_value(nval, i);
  395. name = cnf->name;
  396. val = cnf->value;
  397. ret = set_dist_point_name(&idp->distpoint, ctx, cnf);
  398. if (ret > 0)
  399. continue;
  400. if (ret < 0)
  401. goto err;
  402. if (!strcmp(name, "onlyuser"))
  403. {
  404. if (!X509V3_get_value_bool(cnf, &idp->onlyuser))
  405. goto err;
  406. }
  407. else if (!strcmp(name, "onlyCA"))
  408. {
  409. if (!X509V3_get_value_bool(cnf, &idp->onlyCA))
  410. goto err;
  411. }
  412. else if (!strcmp(name, "onlyAA"))
  413. {
  414. if (!X509V3_get_value_bool(cnf, &idp->onlyattr))
  415. goto err;
  416. }
  417. else if (!strcmp(name, "indirectCRL"))
  418. {
  419. if (!X509V3_get_value_bool(cnf, &idp->indirectCRL))
  420. goto err;
  421. }
  422. else if (!strcmp(name, "onlysomereasons"))
  423. {
  424. if (!set_reasons(&idp->onlysomereasons, val))
  425. goto err;
  426. }
  427. else
  428. {
  429. X509V3err(X509V3_F_V2I_IDP, X509V3_R_INVALID_NAME);
  430. X509V3_conf_err(cnf);
  431. goto err;
  432. }
  433. }
  434. return idp;
  435. merr:
  436. X509V3err(X509V3_F_V2I_IDP,ERR_R_MALLOC_FAILURE);
  437. err:
  438. ISSUING_DIST_POINT_free(idp);
  439. return NULL;
  440. }
  441. static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent)
  442. {
  443. int i;
  444. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++)
  445. {
  446. BIO_printf(out, "%*s", indent + 2, "");
  447. GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
  448. BIO_puts(out, "\n");
  449. }
  450. return 1;
  451. }
  452. static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent)
  453. {
  454. if (dpn->type == 0)
  455. {
  456. BIO_printf(out, "%*sFull Name:\n", indent, "");
  457. print_gens(out, dpn->name.fullname, indent);
  458. }
  459. else
  460. {
  461. X509_NAME ntmp;
  462. ntmp.entries = dpn->name.relativename;
  463. BIO_printf(out, "%*sRelative Name:\n%*s",
  464. indent, "", indent + 2, "");
  465. X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE);
  466. BIO_puts(out, "\n");
  467. }
  468. return 1;
  469. }
  470. static int i2r_idp(X509V3_EXT_METHOD *method, void *pidp, BIO *out, int indent)
  471. {
  472. ISSUING_DIST_POINT *idp = pidp;
  473. if (idp->distpoint)
  474. print_distpoint(out, idp->distpoint, indent);
  475. if (idp->onlyuser > 0)
  476. BIO_printf(out, "%*sOnly User Certificates\n", indent, "");
  477. if (idp->onlyCA > 0)
  478. BIO_printf(out, "%*sOnly CA Certificates\n", indent, "");
  479. if (idp->indirectCRL > 0)
  480. BIO_printf(out, "%*sIndirect CRL\n", indent, "");
  481. if (idp->onlysomereasons)
  482. print_reasons(out, "Only Some Reasons",
  483. idp->onlysomereasons, indent);
  484. if (idp->onlyattr > 0)
  485. BIO_printf(out, "%*sOnly Attribute Certificates\n", indent, "");
  486. if (!idp->distpoint && (idp->onlyuser <= 0) && (idp->onlyCA <= 0)
  487. && (idp->indirectCRL <= 0) && !idp->onlysomereasons
  488. && (idp->onlyattr <= 0))
  489. BIO_printf(out, "%*s<EMPTY>\n", indent, "");
  490. return 1;
  491. }
  492. static int i2r_crldp(X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
  493. int indent)
  494. {
  495. STACK_OF(DIST_POINT) *crld = pcrldp;
  496. DIST_POINT *point;
  497. int i;
  498. for(i = 0; i < sk_DIST_POINT_num(crld); i++)
  499. {
  500. BIO_puts(out, "\n");
  501. point = sk_DIST_POINT_value(crld, i);
  502. if(point->distpoint)
  503. print_distpoint(out, point->distpoint, indent);
  504. if(point->reasons)
  505. print_reasons(out, "Reasons", point->reasons,
  506. indent);
  507. if(point->CRLissuer)
  508. {
  509. BIO_printf(out, "%*sCRL Issuer:\n", indent, "");
  510. print_gens(out, point->CRLissuer, indent);
  511. }
  512. }
  513. return 1;
  514. }