v3_cpols.c 17 KB

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