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.
 
 
 
 
 
 

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