v3_cpols.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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") == 0) {
  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. pol->policyid = pobj;
  171. }
  172. if (!sk_POLICYINFO_push(pols, pol)) {
  173. POLICYINFO_free(pol);
  174. X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
  175. goto err;
  176. }
  177. }
  178. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  179. return pols;
  180. err:
  181. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  182. sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
  183. return NULL;
  184. }
  185. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  186. STACK_OF(CONF_VALUE) *polstrs, int ia5org)
  187. {
  188. int i;
  189. CONF_VALUE *cnf;
  190. POLICYINFO *pol;
  191. POLICYQUALINFO *qual;
  192. if (!(pol = POLICYINFO_new()))
  193. goto merr;
  194. for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
  195. cnf = sk_CONF_VALUE_value(polstrs, i);
  196. if (strcmp(cnf->name, "policyIdentifier") == 0) {
  197. ASN1_OBJECT *pobj;
  198. if (!(pobj = OBJ_txt2obj(cnf->value, 0))) {
  199. X509V3err(X509V3_F_POLICY_SECTION,
  200. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  201. X509V3_conf_err(cnf);
  202. goto err;
  203. }
  204. pol->policyid = pobj;
  205. } else if (!name_cmp(cnf->name, "CPS")) {
  206. if (!pol->qualifiers)
  207. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  208. if (!(qual = POLICYQUALINFO_new()))
  209. goto merr;
  210. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  211. goto merr;
  212. if (!(qual->pqualid = OBJ_nid2obj(NID_id_qt_cps))) {
  213. X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
  214. goto err;
  215. }
  216. if (!(qual->d.cpsuri = ASN1_IA5STRING_new()))
  217. goto merr;
  218. if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
  219. strlen(cnf->value)))
  220. goto merr;
  221. } else if (!name_cmp(cnf->name, "userNotice")) {
  222. STACK_OF(CONF_VALUE) *unot;
  223. if (*cnf->value != '@') {
  224. X509V3err(X509V3_F_POLICY_SECTION,
  225. X509V3_R_EXPECTED_A_SECTION_NAME);
  226. X509V3_conf_err(cnf);
  227. goto err;
  228. }
  229. unot = X509V3_get_section(ctx, cnf->value + 1);
  230. if (!unot) {
  231. X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
  232. X509V3_conf_err(cnf);
  233. goto err;
  234. }
  235. qual = notice_section(ctx, unot, ia5org);
  236. X509V3_section_free(ctx, unot);
  237. if (!qual)
  238. goto err;
  239. if (!pol->qualifiers)
  240. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  241. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  242. goto merr;
  243. } else {
  244. X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
  245. X509V3_conf_err(cnf);
  246. goto err;
  247. }
  248. }
  249. if (!pol->policyid) {
  250. X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
  251. goto err;
  252. }
  253. return pol;
  254. merr:
  255. X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
  256. err:
  257. POLICYINFO_free(pol);
  258. return NULL;
  259. }
  260. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  261. STACK_OF(CONF_VALUE) *unot, int ia5org)
  262. {
  263. int i, ret;
  264. CONF_VALUE *cnf;
  265. USERNOTICE *not;
  266. POLICYQUALINFO *qual;
  267. if (!(qual = POLICYQUALINFO_new()))
  268. goto merr;
  269. if (!(qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice))) {
  270. X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
  271. goto err;
  272. }
  273. if (!(not = USERNOTICE_new()))
  274. goto merr;
  275. qual->d.usernotice = not;
  276. for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
  277. cnf = sk_CONF_VALUE_value(unot, i);
  278. if (strcmp(cnf->name, "explicitText") == 0) {
  279. if (!(not->exptext = ASN1_VISIBLESTRING_new()))
  280. goto merr;
  281. if (!ASN1_STRING_set(not->exptext, cnf->value,
  282. strlen(cnf->value)))
  283. goto merr;
  284. } else if (strcmp(cnf->name, "organization") == 0) {
  285. NOTICEREF *nref;
  286. if (!not->noticeref) {
  287. if (!(nref = NOTICEREF_new()))
  288. goto merr;
  289. not->noticeref = nref;
  290. } else
  291. nref = not->noticeref;
  292. if (ia5org)
  293. nref->organization->type = V_ASN1_IA5STRING;
  294. else
  295. nref->organization->type = V_ASN1_VISIBLESTRING;
  296. if (!ASN1_STRING_set(nref->organization, cnf->value,
  297. strlen(cnf->value)))
  298. goto merr;
  299. } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
  300. NOTICEREF *nref;
  301. STACK_OF(CONF_VALUE) *nos;
  302. if (!not->noticeref) {
  303. if (!(nref = NOTICEREF_new()))
  304. goto merr;
  305. not->noticeref = nref;
  306. } else
  307. nref = not->noticeref;
  308. nos = X509V3_parse_list(cnf->value);
  309. if (!nos || !sk_CONF_VALUE_num(nos)) {
  310. X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
  311. X509V3_conf_err(cnf);
  312. goto err;
  313. }
  314. ret = nref_nos(nref->noticenos, nos);
  315. sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
  316. if (!ret)
  317. goto err;
  318. } else {
  319. X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
  320. X509V3_conf_err(cnf);
  321. goto err;
  322. }
  323. }
  324. if (not->noticeref &&
  325. (!not->noticeref->noticenos || !not->noticeref->organization)) {
  326. X509V3err(X509V3_F_NOTICE_SECTION,
  327. X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
  328. goto err;
  329. }
  330. return qual;
  331. merr:
  332. X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
  333. err:
  334. POLICYQUALINFO_free(qual);
  335. return NULL;
  336. }
  337. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
  338. {
  339. CONF_VALUE *cnf;
  340. ASN1_INTEGER *aint;
  341. int i;
  342. for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
  343. cnf = sk_CONF_VALUE_value(nos, i);
  344. if (!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
  345. X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
  346. goto err;
  347. }
  348. if (!sk_ASN1_INTEGER_push(nnums, aint))
  349. goto merr;
  350. }
  351. return 1;
  352. merr:
  353. X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
  354. err:
  355. sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
  356. return 0;
  357. }
  358. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  359. BIO *out, int indent)
  360. {
  361. int i;
  362. POLICYINFO *pinfo;
  363. /* First print out the policy OIDs */
  364. for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
  365. pinfo = sk_POLICYINFO_value(pol, i);
  366. BIO_printf(out, "%*sPolicy: ", indent, "");
  367. i2a_ASN1_OBJECT(out, pinfo->policyid);
  368. BIO_puts(out, "\n");
  369. if (pinfo->qualifiers)
  370. print_qualifiers(out, pinfo->qualifiers, indent + 2);
  371. }
  372. return 1;
  373. }
  374. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  375. int indent)
  376. {
  377. POLICYQUALINFO *qualinfo;
  378. int i;
  379. for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
  380. qualinfo = sk_POLICYQUALINFO_value(quals, i);
  381. switch (OBJ_obj2nid(qualinfo->pqualid)) {
  382. case NID_id_qt_cps:
  383. BIO_printf(out, "%*sCPS: %s\n", indent, "",
  384. qualinfo->d.cpsuri->data);
  385. break;
  386. case NID_id_qt_unotice:
  387. BIO_printf(out, "%*sUser Notice:\n", indent, "");
  388. print_notice(out, qualinfo->d.usernotice, indent + 2);
  389. break;
  390. default:
  391. BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
  392. i2a_ASN1_OBJECT(out, qualinfo->pqualid);
  393. BIO_puts(out, "\n");
  394. break;
  395. }
  396. }
  397. }
  398. static void print_notice(BIO *out, USERNOTICE *notice, int indent)
  399. {
  400. int i;
  401. if (notice->noticeref) {
  402. NOTICEREF *ref;
  403. ref = notice->noticeref;
  404. BIO_printf(out, "%*sOrganization: %s\n", indent, "",
  405. ref->organization->data);
  406. BIO_printf(out, "%*sNumber%s: ", indent, "",
  407. sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
  408. for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
  409. ASN1_INTEGER *num;
  410. char *tmp;
  411. num = sk_ASN1_INTEGER_value(ref->noticenos, i);
  412. if (i)
  413. BIO_puts(out, ", ");
  414. tmp = i2s_ASN1_INTEGER(NULL, num);
  415. BIO_puts(out, tmp);
  416. OPENSSL_free(tmp);
  417. }
  418. BIO_puts(out, "\n");
  419. }
  420. if (notice->exptext)
  421. BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
  422. notice->exptext->data);
  423. }
  424. void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
  425. {
  426. const X509_POLICY_DATA *dat = node->data;
  427. BIO_printf(out, "%*sPolicy: ", indent, "");
  428. i2a_ASN1_OBJECT(out, dat->valid_policy);
  429. BIO_puts(out, "\n");
  430. BIO_printf(out, "%*s%s\n", indent + 2, "",
  431. node_data_critical(dat) ? "Critical" : "Non Critical");
  432. if (dat->qualifier_set)
  433. print_qualifiers(out, dat->qualifier_set, indent + 2);
  434. else
  435. BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
  436. }