v3_cpols.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/conf.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509v3.h>
  15. #include "x509_local.h"
  16. #include "pcy_local.h"
  17. #include "ext_dat.h"
  18. /* Certificate policies extension support: this one is a bit complex... */
  19. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  20. BIO *out, int indent);
  21. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
  22. X509V3_CTX *ctx, const char *value);
  23. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  24. int indent);
  25. static void print_notice(BIO *out, USERNOTICE *notice, int indent);
  26. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  27. STACK_OF(CONF_VALUE) *polstrs, int ia5org);
  28. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  29. STACK_OF(CONF_VALUE) *unot, int ia5org);
  30. static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
  31. static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len);
  32. static int displaytext_get_tag_len(const char *tagstr);
  33. const X509V3_EXT_METHOD ossl_v3_cpols = {
  34. NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
  35. 0, 0, 0, 0,
  36. 0, 0,
  37. 0, 0,
  38. (X509V3_EXT_I2R)i2r_certpol,
  39. (X509V3_EXT_R2I)r2i_certpol,
  40. NULL
  41. };
  42. ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
  43. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
  44. ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
  45. IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
  46. ASN1_SEQUENCE(POLICYINFO) = {
  47. ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
  48. ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
  49. } ASN1_SEQUENCE_END(POLICYINFO)
  50. IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
  51. ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
  52. ASN1_ADB(POLICYQUALINFO) = {
  53. ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
  54. ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
  55. } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
  56. ASN1_SEQUENCE(POLICYQUALINFO) = {
  57. ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
  58. ASN1_ADB_OBJECT(POLICYQUALINFO)
  59. } ASN1_SEQUENCE_END(POLICYQUALINFO)
  60. IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
  61. ASN1_SEQUENCE(USERNOTICE) = {
  62. ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
  63. ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
  64. } ASN1_SEQUENCE_END(USERNOTICE)
  65. IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
  66. ASN1_SEQUENCE(NOTICEREF) = {
  67. ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
  68. ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
  69. } ASN1_SEQUENCE_END(NOTICEREF)
  70. IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
  71. static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
  72. X509V3_CTX *ctx, const char *value)
  73. {
  74. STACK_OF(POLICYINFO) *pols;
  75. char *pstr;
  76. POLICYINFO *pol;
  77. ASN1_OBJECT *pobj;
  78. STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value);
  79. CONF_VALUE *cnf;
  80. const int num = sk_CONF_VALUE_num(vals);
  81. int i, ia5org;
  82. if (vals == NULL) {
  83. ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
  84. return NULL;
  85. }
  86. pols = sk_POLICYINFO_new_reserve(NULL, num);
  87. if (pols == NULL) {
  88. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  89. goto err;
  90. }
  91. ia5org = 0;
  92. for (i = 0; i < num; i++) {
  93. cnf = sk_CONF_VALUE_value(vals, i);
  94. if (cnf->value != NULL || cnf->name == NULL) {
  95. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_POLICY_IDENTIFIER);
  96. X509V3_conf_add_error_name_value(cnf);
  97. goto err;
  98. }
  99. pstr = cnf->name;
  100. if (strcmp(pstr, "ia5org") == 0) {
  101. ia5org = 1;
  102. continue;
  103. } else if (*pstr == '@') {
  104. STACK_OF(CONF_VALUE) *polsect;
  105. polsect = X509V3_get_section(ctx, pstr + 1);
  106. if (polsect == NULL) {
  107. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION,
  108. "%s", cnf->name);
  109. goto err;
  110. }
  111. pol = policy_section(ctx, polsect, ia5org);
  112. X509V3_section_free(ctx, polsect);
  113. if (pol == NULL)
  114. goto err;
  115. } else {
  116. if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
  117. ERR_raise_data(ERR_LIB_X509V3,
  118. X509V3_R_INVALID_OBJECT_IDENTIFIER,
  119. "%s", cnf->name);
  120. goto err;
  121. }
  122. pol = POLICYINFO_new();
  123. if (pol == NULL) {
  124. ASN1_OBJECT_free(pobj);
  125. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  126. goto err;
  127. }
  128. pol->policyid = pobj;
  129. }
  130. if (!sk_POLICYINFO_push(pols, pol)) {
  131. POLICYINFO_free(pol);
  132. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  133. goto err;
  134. }
  135. }
  136. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  137. return pols;
  138. err:
  139. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  140. sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
  141. return NULL;
  142. }
  143. static POLICYINFO *policy_section(X509V3_CTX *ctx,
  144. STACK_OF(CONF_VALUE) *polstrs, int ia5org)
  145. {
  146. int i;
  147. CONF_VALUE *cnf;
  148. POLICYINFO *pol;
  149. POLICYQUALINFO *qual;
  150. if ((pol = POLICYINFO_new()) == NULL)
  151. goto merr;
  152. for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
  153. cnf = sk_CONF_VALUE_value(polstrs, i);
  154. if (strcmp(cnf->name, "policyIdentifier") == 0) {
  155. ASN1_OBJECT *pobj;
  156. if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
  157. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
  158. X509V3_conf_err(cnf);
  159. goto err;
  160. }
  161. pol->policyid = pobj;
  162. } else if (!ossl_v3_name_cmp(cnf->name, "CPS")) {
  163. if (pol->qualifiers == NULL)
  164. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  165. if ((qual = POLICYQUALINFO_new()) == NULL)
  166. goto merr;
  167. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  168. goto merr;
  169. if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
  170. ERR_raise(ERR_LIB_X509V3, ERR_R_INTERNAL_ERROR);
  171. goto err;
  172. }
  173. if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
  174. goto merr;
  175. if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
  176. strlen(cnf->value)))
  177. goto merr;
  178. } else if (!ossl_v3_name_cmp(cnf->name, "userNotice")) {
  179. STACK_OF(CONF_VALUE) *unot;
  180. if (*cnf->value != '@') {
  181. ERR_raise(ERR_LIB_X509V3, X509V3_R_EXPECTED_A_SECTION_NAME);
  182. X509V3_conf_err(cnf);
  183. goto err;
  184. }
  185. unot = X509V3_get_section(ctx, cnf->value + 1);
  186. if (!unot) {
  187. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION);
  188. X509V3_conf_err(cnf);
  189. goto err;
  190. }
  191. qual = notice_section(ctx, unot, ia5org);
  192. X509V3_section_free(ctx, unot);
  193. if (!qual)
  194. goto err;
  195. if (pol->qualifiers == NULL)
  196. pol->qualifiers = sk_POLICYQUALINFO_new_null();
  197. if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
  198. goto merr;
  199. } else {
  200. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OPTION);
  201. X509V3_conf_err(cnf);
  202. goto err;
  203. }
  204. }
  205. if (pol->policyid == NULL) {
  206. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_POLICY_IDENTIFIER);
  207. goto err;
  208. }
  209. return pol;
  210. merr:
  211. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  212. err:
  213. POLICYINFO_free(pol);
  214. return NULL;
  215. }
  216. static int displaytext_get_tag_len(const char *tagstr)
  217. {
  218. char *colon = strchr(tagstr, ':');
  219. return (colon == NULL) ? -1 : colon - tagstr;
  220. }
  221. static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
  222. {
  223. int len;
  224. *tag_len = 0;
  225. len = displaytext_get_tag_len(tagstr);
  226. if (len == -1)
  227. return V_ASN1_VISIBLESTRING;
  228. *tag_len = len;
  229. if (len == sizeof("UTF8") - 1 && strncmp(tagstr, "UTF8", len) == 0)
  230. return V_ASN1_UTF8STRING;
  231. if (len == sizeof("UTF8String") - 1 && strncmp(tagstr, "UTF8String", len) == 0)
  232. return V_ASN1_UTF8STRING;
  233. if (len == sizeof("BMP") - 1 && strncmp(tagstr, "BMP", len) == 0)
  234. return V_ASN1_BMPSTRING;
  235. if (len == sizeof("BMPSTRING") - 1 && strncmp(tagstr, "BMPSTRING", len) == 0)
  236. return V_ASN1_BMPSTRING;
  237. if (len == sizeof("VISIBLE") - 1 && strncmp(tagstr, "VISIBLE", len) == 0)
  238. return V_ASN1_VISIBLESTRING;
  239. if (len == sizeof("VISIBLESTRING") - 1 && strncmp(tagstr, "VISIBLESTRING", len) == 0)
  240. return V_ASN1_VISIBLESTRING;
  241. *tag_len = 0;
  242. return V_ASN1_VISIBLESTRING;
  243. }
  244. static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
  245. STACK_OF(CONF_VALUE) *unot, int ia5org)
  246. {
  247. int i, ret, len, tag;
  248. unsigned int tag_len;
  249. CONF_VALUE *cnf;
  250. USERNOTICE *not;
  251. POLICYQUALINFO *qual;
  252. char *value = NULL;
  253. if ((qual = POLICYQUALINFO_new()) == NULL)
  254. goto merr;
  255. if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
  256. ERR_raise(ERR_LIB_X509V3, ERR_R_INTERNAL_ERROR);
  257. goto err;
  258. }
  259. if ((not = USERNOTICE_new()) == NULL)
  260. 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. value = cnf->value;
  265. if (strcmp(cnf->name, "explicitText") == 0) {
  266. tag = displaytext_str2tag(value, &tag_len);
  267. if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL)
  268. goto merr;
  269. if (tag_len != 0)
  270. value += tag_len + 1;
  271. len = strlen(value);
  272. if (!ASN1_STRING_set(not->exptext, value, len))
  273. goto merr;
  274. } else if (strcmp(cnf->name, "organization") == 0) {
  275. NOTICEREF *nref;
  276. if (!not->noticeref) {
  277. if ((nref = NOTICEREF_new()) == NULL)
  278. goto merr;
  279. not->noticeref = nref;
  280. } else
  281. nref = not->noticeref;
  282. if (ia5org)
  283. nref->organization->type = V_ASN1_IA5STRING;
  284. else
  285. nref->organization->type = V_ASN1_VISIBLESTRING;
  286. if (!ASN1_STRING_set(nref->organization, cnf->value,
  287. strlen(cnf->value)))
  288. goto merr;
  289. } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
  290. NOTICEREF *nref;
  291. STACK_OF(CONF_VALUE) *nos;
  292. if (!not->noticeref) {
  293. if ((nref = NOTICEREF_new()) == NULL)
  294. goto merr;
  295. not->noticeref = nref;
  296. } else
  297. nref = not->noticeref;
  298. nos = X509V3_parse_list(cnf->value);
  299. if (!nos || !sk_CONF_VALUE_num(nos)) {
  300. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NUMBERS);
  301. X509V3_conf_add_error_name_value(cnf);
  302. sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
  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. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OPTION);
  311. X509V3_conf_add_error_name_value(cnf);
  312. goto err;
  313. }
  314. }
  315. if (not->noticeref &&
  316. (!not->noticeref->noticenos || !not->noticeref->organization)) {
  317. ERR_raise(ERR_LIB_X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
  318. goto err;
  319. }
  320. return qual;
  321. merr:
  322. ERR_raise(ERR_LIB_X509V3, 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. int 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)) == NULL) {
  335. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NUMBER);
  336. goto err;
  337. }
  338. if (!sk_ASN1_INTEGER_push(nnums, aint))
  339. goto merr;
  340. }
  341. return 1;
  342. merr:
  343. ASN1_INTEGER_free(aint);
  344. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  345. err:
  346. return 0;
  347. }
  348. static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
  349. BIO *out, int indent)
  350. {
  351. int i;
  352. POLICYINFO *pinfo;
  353. /* First print out the policy OIDs */
  354. for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
  355. if (i > 0)
  356. BIO_puts(out, "\n");
  357. pinfo = sk_POLICYINFO_value(pol, i);
  358. BIO_printf(out, "%*sPolicy: ", indent, "");
  359. i2a_ASN1_OBJECT(out, pinfo->policyid);
  360. if (pinfo->qualifiers) {
  361. BIO_puts(out, "\n");
  362. print_qualifiers(out, pinfo->qualifiers, indent + 2);
  363. }
  364. }
  365. return 1;
  366. }
  367. static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
  368. int indent)
  369. {
  370. POLICYQUALINFO *qualinfo;
  371. int i;
  372. for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
  373. if (i > 0)
  374. BIO_puts(out, "\n");
  375. qualinfo = sk_POLICYQUALINFO_value(quals, i);
  376. switch (OBJ_obj2nid(qualinfo->pqualid)) {
  377. case NID_id_qt_cps:
  378. BIO_printf(out, "%*sCPS: %.*s", indent, "",
  379. qualinfo->d.cpsuri->length,
  380. qualinfo->d.cpsuri->data);
  381. break;
  382. case NID_id_qt_unotice:
  383. BIO_printf(out, "%*sUser Notice:\n", indent, "");
  384. print_notice(out, qualinfo->d.usernotice, indent + 2);
  385. break;
  386. default:
  387. BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
  388. i2a_ASN1_OBJECT(out, qualinfo->pqualid);
  389. break;
  390. }
  391. }
  392. }
  393. static void print_notice(BIO *out, USERNOTICE *notice, int indent)
  394. {
  395. int i;
  396. if (notice->noticeref) {
  397. NOTICEREF *ref;
  398. ref = notice->noticeref;
  399. BIO_printf(out, "%*sOrganization: %.*s\n", indent, "",
  400. ref->organization->length,
  401. ref->organization->data);
  402. BIO_printf(out, "%*sNumber%s: ", indent, "",
  403. sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
  404. for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
  405. ASN1_INTEGER *num;
  406. char *tmp;
  407. num = sk_ASN1_INTEGER_value(ref->noticenos, i);
  408. if (i)
  409. BIO_puts(out, ", ");
  410. if (num == NULL)
  411. BIO_puts(out, "(null)");
  412. else {
  413. tmp = i2s_ASN1_INTEGER(NULL, num);
  414. if (tmp == NULL)
  415. return;
  416. BIO_puts(out, tmp);
  417. OPENSSL_free(tmp);
  418. }
  419. }
  420. if (notice->exptext)
  421. BIO_puts(out, "\n");
  422. }
  423. if (notice->exptext)
  424. BIO_printf(out, "%*sExplicit Text: %.*s", indent, "",
  425. notice->exptext->length,
  426. notice->exptext->data);
  427. }
  428. void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
  429. {
  430. const X509_POLICY_DATA *dat = node->data;
  431. BIO_printf(out, "%*sPolicy: ", indent, "");
  432. i2a_ASN1_OBJECT(out, dat->valid_policy);
  433. BIO_puts(out, "\n");
  434. BIO_printf(out, "%*s%s\n", indent + 2, "",
  435. node_data_critical(dat) ? "Critical" : "Non Critical");
  436. if (dat->qualifier_set) {
  437. print_qualifiers(out, dat->qualifier_set, indent + 2);
  438. BIO_puts(out, "\n");
  439. }
  440. else
  441. BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
  442. }