You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

461 lines
14 KiB

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