v3_cpols.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* v3_cpols.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/conf.h>
  62. #include <openssl/asn1.h>
  63. #include <openssl/asn1t.h>
  64. #include <openssl/x509v3.h>
  65. #include "pcy_int.h"
  66. /* Certificate policies extension support: this one is a bit complex... */
  67. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  68. BIO *out, int indent);
  69. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
  70. X509V3_CTX *ctx, char *value);
  71. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  72. int indent);
  73. static void print_notice(BIO *out, USERNOTICE *notice, int indent);
  74. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  75. STACK_OF(CONF_VALUE) *polstrs, int ia5org);
  76. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  77. STACK_OF(CONF_VALUE) *unot, int ia5org);
  78. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
  79. const X509V3_EXT_METHOD v3_cpols = {
  80. NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
  81. 0, 0, 0, 0,
  82. 0, 0,
  83. 0, 0,
  84. (X509V3_EXT_I2R)i2r_certpol,
  85. (X509V3_EXT_R2I)r2i_certpol,
  86. NULL
  87. };
  88. ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
  89. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
  90. ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
  91. IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
  92. ASN1_SEQUENCE(POLICYINFO) = {
  93. ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
  94. ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
  95. } ASN1_SEQUENCE_END(POLICYINFO)
  96. IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
  97. ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
  98. ASN1_ADB(POLICYQUALINFO) = {
  99. ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
  100. ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
  101. } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
  102. ASN1_SEQUENCE(POLICYQUALINFO) = {
  103. ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
  104. ASN1_ADB_OBJECT(POLICYQUALINFO)
  105. } ASN1_SEQUENCE_END(POLICYQUALINFO)
  106. IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
  107. ASN1_SEQUENCE(USERNOTICE) = {
  108. ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
  109. ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
  110. } ASN1_SEQUENCE_END(USERNOTICE)
  111. IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
  112. ASN1_SEQUENCE(NOTICEREF) = {
  113. ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
  114. ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
  115. } ASN1_SEQUENCE_END(NOTICEREF)
  116. IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
  117. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
  118. X509V3_CTX *ctx, char *value)
  119. {
  120. STACK_OF(POLICYINFO) *pols = NULL;
  121. char *pstr;
  122. POLICYINFO *pol;
  123. ASN1_OBJECT *pobj;
  124. STACK_OF(CONF_VALUE) *vals;
  125. CONF_VALUE *cnf;
  126. int i, ia5org;
  127. pols = sk_POLICYINFO_new_null();
  128. if (pols == NULL) {
  129. X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
  130. return NULL;
  131. }
  132. vals = X509V3_parse_list(value);
  133. if (vals == NULL) {
  134. X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
  135. goto err;
  136. }
  137. ia5org = 0;
  138. for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
  139. cnf = sk_CONF_VALUE_value(vals, i);
  140. if (cnf->value || !cnf->name) {
  141. X509V3err(X509V3_F_R2I_CERTPOL,
  142. X509V3_R_INVALID_POLICY_IDENTIFIER);
  143. X509V3_conf_err(cnf);
  144. goto err;
  145. }
  146. pstr = cnf->name;
  147. if (!strcmp(pstr, "ia5org")) {
  148. ia5org = 1;
  149. continue;
  150. } else if (*pstr == '@') {
  151. STACK_OF(CONF_VALUE) *polsect;
  152. polsect = X509V3_get_section(ctx, pstr + 1);
  153. if (!polsect) {
  154. X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
  155. X509V3_conf_err(cnf);
  156. goto err;
  157. }
  158. pol = policy_section(ctx, polsect, ia5org);
  159. X509V3_section_free(ctx, polsect);
  160. if (!pol)
  161. goto err;
  162. } else {
  163. if (!(pobj = OBJ_txt2obj(cnf->name, 0))) {
  164. X509V3err(X509V3_F_R2I_CERTPOL,
  165. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  166. X509V3_conf_err(cnf);
  167. goto err;
  168. }
  169. pol = POLICYINFO_new();
  170. if (pol == NULL) {
  171. X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
  172. goto err;
  173. }
  174. pol->policyid = pobj;
  175. }
  176. if (!sk_POLICYINFO_push(pols, pol)) {
  177. POLICYINFO_free(pol);
  178. X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
  179. goto err;
  180. }
  181. }
  182. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  183. return pols;
  184. err:
  185. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  186. sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
  187. return NULL;
  188. }
  189. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  190. STACK_OF(CONF_VALUE) *polstrs, int ia5org)
  191. {
  192. int i;
  193. CONF_VALUE *cnf;
  194. POLICYINFO *pol;
  195. POLICYQUALINFO *qual;
  196. if (!(pol = POLICYINFO_new()))
  197. goto merr;
  198. for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
  199. cnf = sk_CONF_VALUE_value(polstrs, i);
  200. if (!strcmp(cnf->name, "policyIdentifier")) {
  201. ASN1_OBJECT *pobj;
  202. if (!(pobj = OBJ_txt2obj(cnf->value, 0))) {
  203. X509V3err(X509V3_F_POLICY_SECTION,
  204. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  205. X509V3_conf_err(cnf);
  206. goto err;
  207. }
  208. pol->policyid = pobj;
  209. } else if (!name_cmp(cnf->name, "CPS")) {
  210. if (!pol->qualifiers)
  211. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  212. if (!(qual = POLICYQUALINFO_new()))
  213. goto merr;
  214. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  215. goto merr;
  216. if (!(qual->pqualid = OBJ_nid2obj(NID_id_qt_cps))) {
  217. X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
  218. goto err;
  219. }
  220. if (!(qual->d.cpsuri = M_ASN1_IA5STRING_new()))
  221. goto merr;
  222. if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
  223. strlen(cnf->value)))
  224. goto merr;
  225. } else if (!name_cmp(cnf->name, "userNotice")) {
  226. STACK_OF(CONF_VALUE) *unot;
  227. if (*cnf->value != '@') {
  228. X509V3err(X509V3_F_POLICY_SECTION,
  229. X509V3_R_EXPECTED_A_SECTION_NAME);
  230. X509V3_conf_err(cnf);
  231. goto err;
  232. }
  233. unot = X509V3_get_section(ctx, cnf->value + 1);
  234. if (!unot) {
  235. X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
  236. X509V3_conf_err(cnf);
  237. goto err;
  238. }
  239. qual = notice_section(ctx, unot, ia5org);
  240. X509V3_section_free(ctx, unot);
  241. if (!qual)
  242. goto err;
  243. if (!pol->qualifiers)
  244. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  245. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  246. goto merr;
  247. } else {
  248. X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
  249. X509V3_conf_err(cnf);
  250. goto err;
  251. }
  252. }
  253. if (!pol->policyid) {
  254. X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
  255. goto err;
  256. }
  257. return pol;
  258. merr:
  259. X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
  260. err:
  261. POLICYINFO_free(pol);
  262. return NULL;
  263. }
  264. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  265. STACK_OF(CONF_VALUE) *unot, int ia5org)
  266. {
  267. int i, ret;
  268. CONF_VALUE *cnf;
  269. USERNOTICE *not;
  270. POLICYQUALINFO *qual;
  271. if (!(qual = POLICYQUALINFO_new()))
  272. goto merr;
  273. if (!(qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice))) {
  274. X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
  275. goto err;
  276. }
  277. if (!(not = USERNOTICE_new()))
  278. goto merr;
  279. qual->d.usernotice = not;
  280. for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
  281. cnf = sk_CONF_VALUE_value(unot, i);
  282. if (!strcmp(cnf->name, "explicitText")) {
  283. if (!(not->exptext = M_ASN1_VISIBLESTRING_new()))
  284. goto merr;
  285. if (!ASN1_STRING_set(not->exptext, cnf->value,
  286. strlen(cnf->value)))
  287. goto merr;
  288. } else if (!strcmp(cnf->name, "organization")) {
  289. NOTICEREF *nref;
  290. if (!not->noticeref) {
  291. if (!(nref = NOTICEREF_new()))
  292. goto merr;
  293. not->noticeref = nref;
  294. } else
  295. nref = not->noticeref;
  296. if (ia5org)
  297. nref->organization->type = V_ASN1_IA5STRING;
  298. else
  299. nref->organization->type = V_ASN1_VISIBLESTRING;
  300. if (!ASN1_STRING_set(nref->organization, cnf->value,
  301. strlen(cnf->value)))
  302. goto merr;
  303. } else if (!strcmp(cnf->name, "noticeNumbers")) {
  304. NOTICEREF *nref;
  305. STACK_OF(CONF_VALUE) *nos;
  306. if (!not->noticeref) {
  307. if (!(nref = NOTICEREF_new()))
  308. goto merr;
  309. not->noticeref = nref;
  310. } else
  311. nref = not->noticeref;
  312. nos = X509V3_parse_list(cnf->value);
  313. if (!nos || !sk_CONF_VALUE_num(nos)) {
  314. X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
  315. X509V3_conf_err(cnf);
  316. goto err;
  317. }
  318. ret = nref_nos(nref->noticenos, nos);
  319. sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
  320. if (!ret)
  321. goto err;
  322. } else {
  323. X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
  324. X509V3_conf_err(cnf);
  325. goto err;
  326. }
  327. }
  328. if (not->noticeref &&
  329. (!not->noticeref->noticenos || !not->noticeref->organization)) {
  330. X509V3err(X509V3_F_NOTICE_SECTION,
  331. X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
  332. goto err;
  333. }
  334. return qual;
  335. merr:
  336. X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
  337. err:
  338. POLICYQUALINFO_free(qual);
  339. return NULL;
  340. }
  341. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
  342. {
  343. CONF_VALUE *cnf;
  344. ASN1_INTEGER *aint;
  345. int i;
  346. for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
  347. cnf = sk_CONF_VALUE_value(nos, i);
  348. if (!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
  349. X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
  350. goto err;
  351. }
  352. if (!sk_ASN1_INTEGER_push(nnums, aint))
  353. goto merr;
  354. }
  355. return 1;
  356. merr:
  357. ASN1_INTEGER_free(aint);
  358. X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
  359. err:
  360. return 0;
  361. }
  362. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  363. BIO *out, int indent)
  364. {
  365. int i;
  366. POLICYINFO *pinfo;
  367. /* First print out the policy OIDs */
  368. for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
  369. pinfo = sk_POLICYINFO_value(pol, i);
  370. BIO_printf(out, "%*sPolicy: ", indent, "");
  371. i2a_ASN1_OBJECT(out, pinfo->policyid);
  372. BIO_puts(out, "\n");
  373. if (pinfo->qualifiers)
  374. print_qualifiers(out, pinfo->qualifiers, indent + 2);
  375. }
  376. return 1;
  377. }
  378. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  379. int indent)
  380. {
  381. POLICYQUALINFO *qualinfo;
  382. int i;
  383. for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
  384. qualinfo = sk_POLICYQUALINFO_value(quals, i);
  385. switch (OBJ_obj2nid(qualinfo->pqualid)) {
  386. case NID_id_qt_cps:
  387. BIO_printf(out, "%*sCPS: %s\n", indent, "",
  388. qualinfo->d.cpsuri->data);
  389. break;
  390. case NID_id_qt_unotice:
  391. BIO_printf(out, "%*sUser Notice:\n", indent, "");
  392. print_notice(out, qualinfo->d.usernotice, indent + 2);
  393. break;
  394. default:
  395. BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
  396. i2a_ASN1_OBJECT(out, qualinfo->pqualid);
  397. BIO_puts(out, "\n");
  398. break;
  399. }
  400. }
  401. }
  402. static void print_notice(BIO *out, USERNOTICE *notice, int indent)
  403. {
  404. int i;
  405. if (notice->noticeref) {
  406. NOTICEREF *ref;
  407. ref = notice->noticeref;
  408. BIO_printf(out, "%*sOrganization: %s\n", indent, "",
  409. ref->organization->data);
  410. BIO_printf(out, "%*sNumber%s: ", indent, "",
  411. sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
  412. for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
  413. ASN1_INTEGER *num;
  414. char *tmp;
  415. num = sk_ASN1_INTEGER_value(ref->noticenos, i);
  416. if (i)
  417. BIO_puts(out, ", ");
  418. if (num == NULL)
  419. BIO_puts(out, "(null)");
  420. else {
  421. tmp = i2s_ASN1_INTEGER(NULL, num);
  422. if (tmp == NULL)
  423. return;
  424. BIO_puts(out, tmp);
  425. OPENSSL_free(tmp);
  426. }
  427. }
  428. BIO_puts(out, "\n");
  429. }
  430. if (notice->exptext)
  431. BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
  432. notice->exptext->data);
  433. }
  434. void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
  435. {
  436. const X509_POLICY_DATA *dat = node->data;
  437. BIO_printf(out, "%*sPolicy: ", indent, "");
  438. i2a_ASN1_OBJECT(out, dat->valid_policy);
  439. BIO_puts(out, "\n");
  440. BIO_printf(out, "%*s%s\n", indent + 2, "",
  441. node_data_critical(dat) ? "Critical" : "Non Critical");
  442. if (dat->qualifier_set)
  443. print_qualifiers(out, dat->qualifier_set, indent + 2);
  444. else
  445. BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
  446. }
  447. IMPLEMENT_STACK_OF(X509_POLICY_NODE)
  448. IMPLEMENT_STACK_OF(X509_POLICY_DATA)