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.
 
 
 
 
 
 

503 lines
17 KiB

  1. /* v3_cpols.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/asn1t.h>
  63. #include <openssl/conf.h>
  64. #include <openssl/err.h>
  65. #include <openssl/mem.h>
  66. #include <openssl/obj.h>
  67. #include <openssl/stack.h>
  68. #include <openssl/x509v3.h>
  69. #include "pcy_int.h"
  70. /* Certificate policies extension support: this one is a bit complex... */
  71. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  72. BIO *out, int indent);
  73. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
  74. X509V3_CTX *ctx, char *value);
  75. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  76. int indent);
  77. static void print_notice(BIO *out, USERNOTICE *notice, int indent);
  78. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  79. STACK_OF(CONF_VALUE) *polstrs, int ia5org);
  80. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  81. STACK_OF(CONF_VALUE) *unot, int ia5org);
  82. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
  83. const X509V3_EXT_METHOD v3_cpols = {
  84. NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
  85. 0, 0, 0, 0,
  86. 0, 0,
  87. 0, 0,
  88. (X509V3_EXT_I2R)i2r_certpol,
  89. (X509V3_EXT_R2I)r2i_certpol,
  90. NULL
  91. };
  92. ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
  93. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
  94. ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
  95. IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
  96. ASN1_SEQUENCE(POLICYINFO) = {
  97. ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
  98. ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
  99. } ASN1_SEQUENCE_END(POLICYINFO)
  100. IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
  101. ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
  102. ASN1_ADB(POLICYQUALINFO) = {
  103. ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
  104. ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
  105. } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
  106. ASN1_SEQUENCE(POLICYQUALINFO) = {
  107. ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
  108. ASN1_ADB_OBJECT(POLICYQUALINFO)
  109. } ASN1_SEQUENCE_END(POLICYQUALINFO)
  110. IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
  111. ASN1_SEQUENCE(USERNOTICE) = {
  112. ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
  113. ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
  114. } ASN1_SEQUENCE_END(USERNOTICE)
  115. IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
  116. ASN1_SEQUENCE(NOTICEREF) = {
  117. ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
  118. ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
  119. } ASN1_SEQUENCE_END(NOTICEREF)
  120. IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
  121. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
  122. X509V3_CTX *ctx, char *value)
  123. {
  124. STACK_OF(POLICYINFO) *pols = NULL;
  125. char *pstr;
  126. POLICYINFO *pol;
  127. ASN1_OBJECT *pobj;
  128. STACK_OF(CONF_VALUE) *vals;
  129. CONF_VALUE *cnf;
  130. size_t i;
  131. int ia5org;
  132. pols = sk_POLICYINFO_new_null();
  133. if (pols == NULL) {
  134. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  135. return NULL;
  136. }
  137. vals = X509V3_parse_list(value);
  138. if (vals == NULL) {
  139. OPENSSL_PUT_ERROR(X509V3, ERR_R_X509V3_LIB);
  140. goto err;
  141. }
  142. ia5org = 0;
  143. for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
  144. cnf = sk_CONF_VALUE_value(vals, i);
  145. if (cnf->value || !cnf->name) {
  146. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_POLICY_IDENTIFIER);
  147. X509V3_conf_err(cnf);
  148. goto err;
  149. }
  150. pstr = cnf->name;
  151. if (!strcmp(pstr, "ia5org")) {
  152. ia5org = 1;
  153. continue;
  154. } else if (*pstr == '@') {
  155. STACK_OF(CONF_VALUE) *polsect;
  156. polsect = X509V3_get_section(ctx, pstr + 1);
  157. if (!polsect) {
  158. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION);
  159. X509V3_conf_err(cnf);
  160. goto err;
  161. }
  162. pol = policy_section(ctx, polsect, ia5org);
  163. X509V3_section_free(ctx, polsect);
  164. if (!pol)
  165. goto err;
  166. } else {
  167. if (!(pobj = OBJ_txt2obj(cnf->name, 0))) {
  168. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
  169. X509V3_conf_err(cnf);
  170. goto err;
  171. }
  172. pol = POLICYINFO_new();
  173. if (pol == NULL) {
  174. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  175. ASN1_OBJECT_free(pobj);
  176. goto err;
  177. }
  178. pol->policyid = pobj;
  179. }
  180. if (!sk_POLICYINFO_push(pols, pol)) {
  181. POLICYINFO_free(pol);
  182. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  183. goto err;
  184. }
  185. }
  186. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  187. return pols;
  188. err:
  189. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  190. sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
  191. return NULL;
  192. }
  193. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  194. STACK_OF(CONF_VALUE) *polstrs, int ia5org)
  195. {
  196. size_t i;
  197. CONF_VALUE *cnf;
  198. POLICYINFO *pol;
  199. POLICYQUALINFO *qual;
  200. if (!(pol = POLICYINFO_new()))
  201. goto merr;
  202. for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
  203. cnf = sk_CONF_VALUE_value(polstrs, i);
  204. if (!strcmp(cnf->name, "policyIdentifier")) {
  205. ASN1_OBJECT *pobj;
  206. if (!(pobj = OBJ_txt2obj(cnf->value, 0))) {
  207. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
  208. X509V3_conf_err(cnf);
  209. goto err;
  210. }
  211. pol->policyid = pobj;
  212. } else if (!name_cmp(cnf->name, "CPS")) {
  213. if (!pol->qualifiers)
  214. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  215. if (!(qual = POLICYQUALINFO_new()))
  216. goto merr;
  217. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  218. goto merr;
  219. /* TODO(fork): const correctness */
  220. qual->pqualid = (ASN1_OBJECT *)OBJ_nid2obj(NID_id_qt_cps);
  221. if (qual->pqualid == NULL) {
  222. OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR);
  223. goto err;
  224. }
  225. qual->d.cpsuri = M_ASN1_IA5STRING_new();
  226. if (qual->d.cpsuri == NULL) {
  227. goto err;
  228. }
  229. if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
  230. strlen(cnf->value)))
  231. goto merr;
  232. } else if (!name_cmp(cnf->name, "userNotice")) {
  233. STACK_OF(CONF_VALUE) *unot;
  234. if (*cnf->value != '@') {
  235. OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXPECTED_A_SECTION_NAME);
  236. X509V3_conf_err(cnf);
  237. goto err;
  238. }
  239. unot = X509V3_get_section(ctx, cnf->value + 1);
  240. if (!unot) {
  241. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION);
  242. X509V3_conf_err(cnf);
  243. goto err;
  244. }
  245. qual = notice_section(ctx, unot, ia5org);
  246. X509V3_section_free(ctx, unot);
  247. if (!qual)
  248. goto err;
  249. if (!pol->qualifiers)
  250. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  251. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  252. goto merr;
  253. } else {
  254. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION);
  255. X509V3_conf_err(cnf);
  256. goto err;
  257. }
  258. }
  259. if (!pol->policyid) {
  260. OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_POLICY_IDENTIFIER);
  261. goto err;
  262. }
  263. return pol;
  264. merr:
  265. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  266. err:
  267. POLICYINFO_free(pol);
  268. return NULL;
  269. }
  270. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  271. STACK_OF(CONF_VALUE) *unot, int ia5org)
  272. {
  273. size_t i;
  274. int ret;
  275. CONF_VALUE *cnf;
  276. USERNOTICE *not;
  277. POLICYQUALINFO *qual;
  278. if (!(qual = POLICYQUALINFO_new()))
  279. goto merr;
  280. /* TODO(fork): const correctness */
  281. qual->pqualid = (ASN1_OBJECT *)OBJ_nid2obj(NID_id_qt_unotice);
  282. if (qual->pqualid == NULL) {
  283. OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR);
  284. goto err;
  285. }
  286. if (!(not = USERNOTICE_new()))
  287. goto merr;
  288. qual->d.usernotice = not;
  289. for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
  290. cnf = sk_CONF_VALUE_value(unot, i);
  291. if (!strcmp(cnf->name, "explicitText")) {
  292. not->exptext = M_ASN1_VISIBLESTRING_new();
  293. if (not->exptext == NULL)
  294. goto merr;
  295. if (!ASN1_STRING_set(not->exptext, cnf->value,
  296. strlen(cnf->value)))
  297. goto merr;
  298. } else if (!strcmp(cnf->name, "organization")) {
  299. NOTICEREF *nref;
  300. if (!not->noticeref) {
  301. if (!(nref = NOTICEREF_new()))
  302. goto merr;
  303. not->noticeref = nref;
  304. } else
  305. nref = not->noticeref;
  306. if (ia5org)
  307. nref->organization->type = V_ASN1_IA5STRING;
  308. else
  309. nref->organization->type = V_ASN1_VISIBLESTRING;
  310. if (!ASN1_STRING_set(nref->organization, cnf->value,
  311. strlen(cnf->value)))
  312. goto merr;
  313. } else if (!strcmp(cnf->name, "noticeNumbers")) {
  314. NOTICEREF *nref;
  315. STACK_OF(CONF_VALUE) *nos;
  316. if (!not->noticeref) {
  317. if (!(nref = NOTICEREF_new()))
  318. goto merr;
  319. not->noticeref = nref;
  320. } else
  321. nref = not->noticeref;
  322. nos = X509V3_parse_list(cnf->value);
  323. if (!nos || !sk_CONF_VALUE_num(nos)) {
  324. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBERS);
  325. X509V3_conf_err(cnf);
  326. goto err;
  327. }
  328. ret = nref_nos(nref->noticenos, nos);
  329. sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
  330. if (!ret)
  331. goto err;
  332. } else {
  333. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION);
  334. X509V3_conf_err(cnf);
  335. goto err;
  336. }
  337. }
  338. if (not->noticeref &&
  339. (!not->noticeref->noticenos || !not->noticeref->organization)) {
  340. OPENSSL_PUT_ERROR(X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
  341. goto err;
  342. }
  343. return qual;
  344. merr:
  345. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  346. err:
  347. POLICYQUALINFO_free(qual);
  348. return NULL;
  349. }
  350. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
  351. {
  352. CONF_VALUE *cnf;
  353. ASN1_INTEGER *aint;
  354. size_t i;
  355. for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
  356. cnf = sk_CONF_VALUE_value(nos, i);
  357. if (!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
  358. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBER);
  359. goto err;
  360. }
  361. if (!sk_ASN1_INTEGER_push(nnums, aint))
  362. goto merr;
  363. }
  364. return 1;
  365. merr:
  366. ASN1_INTEGER_free(aint);
  367. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  368. err:
  369. return 0;
  370. }
  371. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  372. BIO *out, int indent)
  373. {
  374. size_t i;
  375. POLICYINFO *pinfo;
  376. /* First print out the policy OIDs */
  377. for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
  378. pinfo = sk_POLICYINFO_value(pol, i);
  379. BIO_printf(out, "%*sPolicy: ", indent, "");
  380. i2a_ASN1_OBJECT(out, pinfo->policyid);
  381. BIO_puts(out, "\n");
  382. if (pinfo->qualifiers)
  383. print_qualifiers(out, pinfo->qualifiers, indent + 2);
  384. }
  385. return 1;
  386. }
  387. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  388. int indent)
  389. {
  390. POLICYQUALINFO *qualinfo;
  391. size_t i;
  392. for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
  393. qualinfo = sk_POLICYQUALINFO_value(quals, i);
  394. switch (OBJ_obj2nid(qualinfo->pqualid)) {
  395. case NID_id_qt_cps:
  396. BIO_printf(out, "%*sCPS: %s\n", indent, "",
  397. qualinfo->d.cpsuri->data);
  398. break;
  399. case NID_id_qt_unotice:
  400. BIO_printf(out, "%*sUser Notice:\n", indent, "");
  401. print_notice(out, qualinfo->d.usernotice, indent + 2);
  402. break;
  403. default:
  404. BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
  405. i2a_ASN1_OBJECT(out, qualinfo->pqualid);
  406. BIO_puts(out, "\n");
  407. break;
  408. }
  409. }
  410. }
  411. static void print_notice(BIO *out, USERNOTICE *notice, int indent)
  412. {
  413. size_t i;
  414. if (notice->noticeref) {
  415. NOTICEREF *ref;
  416. ref = notice->noticeref;
  417. BIO_printf(out, "%*sOrganization: %s\n", indent, "",
  418. ref->organization->data);
  419. BIO_printf(out, "%*sNumber%s: ", indent, "",
  420. sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
  421. for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
  422. ASN1_INTEGER *num;
  423. char *tmp;
  424. num = sk_ASN1_INTEGER_value(ref->noticenos, i);
  425. if (i)
  426. BIO_puts(out, ", ");
  427. if (num == NULL)
  428. BIO_puts(out, "(null)");
  429. else {
  430. tmp = i2s_ASN1_INTEGER(NULL, num);
  431. if (tmp == NULL)
  432. return;
  433. BIO_puts(out, tmp);
  434. OPENSSL_free(tmp);
  435. }
  436. }
  437. BIO_puts(out, "\n");
  438. }
  439. if (notice->exptext)
  440. BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
  441. notice->exptext->data);
  442. }
  443. void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
  444. {
  445. const X509_POLICY_DATA *dat = node->data;
  446. BIO_printf(out, "%*sPolicy: ", indent, "");
  447. i2a_ASN1_OBJECT(out, dat->valid_policy);
  448. BIO_puts(out, "\n");
  449. BIO_printf(out, "%*s%s\n", indent + 2, "",
  450. node_data_critical(dat) ? "Critical" : "Non Critical");
  451. if (dat->qualifier_set)
  452. print_qualifiers(out, dat->qualifier_set, indent + 2);
  453. else
  454. BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
  455. }