v3_cpols.c 14 KB

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