v3_cpols.c 16 KB

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