v3_cpols.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* v3_cpols.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 1999.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2004 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. #include "pcy_int.h"
  65. /* Certificate policies extension support: this one is a bit complex... */
  66. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol, BIO *out, int indent);
  67. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *value);
  68. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, int indent);
  69. static void print_notice(BIO *out, USERNOTICE *notice, int indent);
  70. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  71. STACK_OF(CONF_VALUE) *polstrs, int ia5org);
  72. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  73. STACK_OF(CONF_VALUE) *unot, int ia5org);
  74. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
  75. X509V3_EXT_METHOD v3_cpols = {
  76. NID_certificate_policies, 0,ASN1_ITEM_ref(CERTIFICATEPOLICIES),
  77. 0,0,0,0,
  78. 0,0,
  79. 0,0,
  80. (X509V3_EXT_I2R)i2r_certpol,
  81. (X509V3_EXT_R2I)r2i_certpol,
  82. NULL
  83. };
  84. ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
  85. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
  86. ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
  87. IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
  88. ASN1_SEQUENCE(POLICYINFO) = {
  89. ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
  90. ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
  91. } ASN1_SEQUENCE_END(POLICYINFO)
  92. IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
  93. ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
  94. ASN1_ADB(POLICYQUALINFO) = {
  95. ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
  96. ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
  97. } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
  98. ASN1_SEQUENCE(POLICYQUALINFO) = {
  99. ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
  100. ASN1_ADB_OBJECT(POLICYQUALINFO)
  101. } ASN1_SEQUENCE_END(POLICYQUALINFO)
  102. IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
  103. ASN1_SEQUENCE(USERNOTICE) = {
  104. ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
  105. ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
  106. } ASN1_SEQUENCE_END(USERNOTICE)
  107. IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
  108. ASN1_SEQUENCE(NOTICEREF) = {
  109. ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
  110. ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
  111. } ASN1_SEQUENCE_END(NOTICEREF)
  112. IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
  113. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
  114. X509V3_CTX *ctx, char *value)
  115. {
  116. STACK_OF(POLICYINFO) *pols = NULL;
  117. char *pstr;
  118. POLICYINFO *pol;
  119. ASN1_OBJECT *pobj;
  120. STACK_OF(CONF_VALUE) *vals;
  121. CONF_VALUE *cnf;
  122. int i, ia5org;
  123. pols = sk_POLICYINFO_new_null();
  124. if (pols == NULL) {
  125. X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
  126. return NULL;
  127. }
  128. vals = X509V3_parse_list(value);
  129. if (vals == NULL) {
  130. X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
  131. goto err;
  132. }
  133. ia5org = 0;
  134. for(i = 0; i < sk_CONF_VALUE_num(vals); i++) {
  135. cnf = sk_CONF_VALUE_value(vals, i);
  136. if(cnf->value || !cnf->name ) {
  137. X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
  138. X509V3_conf_err(cnf);
  139. goto err;
  140. }
  141. pstr = cnf->name;
  142. if(!strcmp(pstr,"ia5org")) {
  143. ia5org = 1;
  144. continue;
  145. } else if(*pstr == '@') {
  146. STACK_OF(CONF_VALUE) *polsect;
  147. polsect = X509V3_get_section(ctx, pstr + 1);
  148. if(!polsect) {
  149. X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_SECTION);
  150. X509V3_conf_err(cnf);
  151. goto err;
  152. }
  153. pol = policy_section(ctx, polsect, ia5org);
  154. X509V3_section_free(ctx, polsect);
  155. if(!pol) goto err;
  156. } else {
  157. if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
  158. X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
  159. X509V3_conf_err(cnf);
  160. goto err;
  161. }
  162. pol = POLICYINFO_new();
  163. pol->policyid = pobj;
  164. }
  165. sk_POLICYINFO_push(pols, pol);
  166. }
  167. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  168. return pols;
  169. err:
  170. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  171. sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
  172. return NULL;
  173. }
  174. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  175. STACK_OF(CONF_VALUE) *polstrs, int ia5org)
  176. {
  177. int i;
  178. CONF_VALUE *cnf;
  179. POLICYINFO *pol;
  180. POLICYQUALINFO *qual;
  181. if(!(pol = POLICYINFO_new())) goto merr;
  182. for(i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
  183. cnf = sk_CONF_VALUE_value(polstrs, i);
  184. if(!strcmp(cnf->name, "policyIdentifier")) {
  185. ASN1_OBJECT *pobj;
  186. if(!(pobj = OBJ_txt2obj(cnf->value, 0))) {
  187. X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OBJECT_IDENTIFIER);
  188. X509V3_conf_err(cnf);
  189. goto err;
  190. }
  191. pol->policyid = pobj;
  192. } else if(!name_cmp(cnf->name, "CPS")) {
  193. if(!pol->qualifiers) pol->qualifiers =
  194. sk_POLICYQUALINFO_new_null();
  195. if(!(qual = POLICYQUALINFO_new())) goto merr;
  196. if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  197. goto merr;
  198. qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
  199. qual->d.cpsuri = M_ASN1_IA5STRING_new();
  200. if(!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
  201. strlen(cnf->value))) goto merr;
  202. } else if(!name_cmp(cnf->name, "userNotice")) {
  203. STACK_OF(CONF_VALUE) *unot;
  204. if(*cnf->value != '@') {
  205. X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_EXPECTED_A_SECTION_NAME);
  206. X509V3_conf_err(cnf);
  207. goto err;
  208. }
  209. unot = X509V3_get_section(ctx, cnf->value + 1);
  210. if(!unot) {
  211. X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_SECTION);
  212. X509V3_conf_err(cnf);
  213. goto err;
  214. }
  215. qual = notice_section(ctx, unot, ia5org);
  216. X509V3_section_free(ctx, unot);
  217. if(!qual) goto err;
  218. if(!pol->qualifiers) pol->qualifiers =
  219. sk_POLICYQUALINFO_new_null();
  220. if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  221. goto merr;
  222. } else {
  223. X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OPTION);
  224. X509V3_conf_err(cnf);
  225. goto err;
  226. }
  227. }
  228. if(!pol->policyid) {
  229. X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_NO_POLICY_IDENTIFIER);
  230. goto err;
  231. }
  232. return pol;
  233. merr:
  234. X509V3err(X509V3_F_POLICY_SECTION,ERR_R_MALLOC_FAILURE);
  235. err:
  236. POLICYINFO_free(pol);
  237. return NULL;
  238. }
  239. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  240. STACK_OF(CONF_VALUE) *unot, int ia5org)
  241. {
  242. int i, ret;
  243. CONF_VALUE *cnf;
  244. USERNOTICE *not;
  245. POLICYQUALINFO *qual;
  246. if(!(qual = POLICYQUALINFO_new())) goto merr;
  247. qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
  248. if(!(not = USERNOTICE_new())) goto merr;
  249. qual->d.usernotice = not;
  250. for(i = 0; i < sk_CONF_VALUE_num(unot); i++) {
  251. cnf = sk_CONF_VALUE_value(unot, i);
  252. if(!strcmp(cnf->name, "explicitText")) {
  253. not->exptext = M_ASN1_VISIBLESTRING_new();
  254. if(!ASN1_STRING_set(not->exptext, cnf->value,
  255. strlen(cnf->value))) goto merr;
  256. } else if(!strcmp(cnf->name, "organization")) {
  257. NOTICEREF *nref;
  258. if(!not->noticeref) {
  259. if(!(nref = NOTICEREF_new())) goto merr;
  260. not->noticeref = nref;
  261. } else nref = not->noticeref;
  262. if(ia5org) nref->organization->type = V_ASN1_IA5STRING;
  263. else nref->organization->type = V_ASN1_VISIBLESTRING;
  264. if(!ASN1_STRING_set(nref->organization, cnf->value,
  265. strlen(cnf->value))) goto merr;
  266. } else if(!strcmp(cnf->name, "noticeNumbers")) {
  267. NOTICEREF *nref;
  268. STACK_OF(CONF_VALUE) *nos;
  269. if(!not->noticeref) {
  270. if(!(nref = NOTICEREF_new())) goto merr;
  271. not->noticeref = nref;
  272. } else nref = not->noticeref;
  273. nos = X509V3_parse_list(cnf->value);
  274. if(!nos || !sk_CONF_VALUE_num(nos)) {
  275. X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_NUMBERS);
  276. X509V3_conf_err(cnf);
  277. goto err;
  278. }
  279. ret = nref_nos(nref->noticenos, nos);
  280. sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
  281. if (!ret)
  282. goto err;
  283. } else {
  284. X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_OPTION);
  285. X509V3_conf_err(cnf);
  286. goto err;
  287. }
  288. }
  289. if(not->noticeref &&
  290. (!not->noticeref->noticenos || !not->noticeref->organization)) {
  291. X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
  292. goto err;
  293. }
  294. return qual;
  295. merr:
  296. X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
  297. err:
  298. POLICYQUALINFO_free(qual);
  299. return NULL;
  300. }
  301. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
  302. {
  303. CONF_VALUE *cnf;
  304. ASN1_INTEGER *aint;
  305. int i;
  306. for(i = 0; i < sk_CONF_VALUE_num(nos); i++) {
  307. cnf = sk_CONF_VALUE_value(nos, i);
  308. if(!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
  309. X509V3err(X509V3_F_NREF_NOS,X509V3_R_INVALID_NUMBER);
  310. goto err;
  311. }
  312. if(!sk_ASN1_INTEGER_push(nnums, aint)) goto merr;
  313. }
  314. return 1;
  315. merr:
  316. X509V3err(X509V3_F_NREF_NOS,ERR_R_MALLOC_FAILURE);
  317. err:
  318. sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
  319. return 0;
  320. }
  321. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  322. BIO *out, int indent)
  323. {
  324. int i;
  325. POLICYINFO *pinfo;
  326. /* First print out the policy OIDs */
  327. for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
  328. pinfo = sk_POLICYINFO_value(pol, i);
  329. BIO_printf(out, "%*sPolicy: ", indent, "");
  330. i2a_ASN1_OBJECT(out, pinfo->policyid);
  331. BIO_puts(out, "\n");
  332. if(pinfo->qualifiers)
  333. print_qualifiers(out, pinfo->qualifiers, indent + 2);
  334. }
  335. return 1;
  336. }
  337. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  338. int indent)
  339. {
  340. POLICYQUALINFO *qualinfo;
  341. int i;
  342. for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
  343. qualinfo = sk_POLICYQUALINFO_value(quals, i);
  344. switch(OBJ_obj2nid(qualinfo->pqualid))
  345. {
  346. case NID_id_qt_cps:
  347. BIO_printf(out, "%*sCPS: %s\n", indent, "",
  348. qualinfo->d.cpsuri->data);
  349. break;
  350. case NID_id_qt_unotice:
  351. BIO_printf(out, "%*sUser Notice:\n", indent, "");
  352. print_notice(out, qualinfo->d.usernotice, indent + 2);
  353. break;
  354. default:
  355. BIO_printf(out, "%*sUnknown Qualifier: ",
  356. indent + 2, "");
  357. i2a_ASN1_OBJECT(out, qualinfo->pqualid);
  358. BIO_puts(out, "\n");
  359. break;
  360. }
  361. }
  362. }
  363. static void print_notice(BIO *out, USERNOTICE *notice, int indent)
  364. {
  365. int i;
  366. if(notice->noticeref) {
  367. NOTICEREF *ref;
  368. ref = notice->noticeref;
  369. BIO_printf(out, "%*sOrganization: %s\n", indent, "",
  370. ref->organization->data);
  371. BIO_printf(out, "%*sNumber%s: ", indent, "",
  372. sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
  373. for(i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
  374. ASN1_INTEGER *num;
  375. char *tmp;
  376. num = sk_ASN1_INTEGER_value(ref->noticenos, i);
  377. if(i) BIO_puts(out, ", ");
  378. tmp = i2s_ASN1_INTEGER(NULL, num);
  379. BIO_puts(out, tmp);
  380. OPENSSL_free(tmp);
  381. }
  382. BIO_puts(out, "\n");
  383. }
  384. if(notice->exptext)
  385. BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
  386. notice->exptext->data);
  387. }
  388. void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
  389. {
  390. const X509_POLICY_DATA *dat = node->data;
  391. BIO_printf(out, "%*sPolicy: ", indent, "");
  392. i2a_ASN1_OBJECT(out, dat->valid_policy);
  393. BIO_puts(out, "\n");
  394. BIO_printf(out, "%*s%s\n", indent + 2, "",
  395. node_data_critical(dat) ? "Critical" : "Non Critical");
  396. if (dat->qualifier_set)
  397. print_qualifiers(out, dat->qualifier_set, indent + 2);
  398. else
  399. BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
  400. }