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.
 
 
 
 
 
 

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