tasn_prn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000,2005 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 <stddef.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/asn1.h>
  61. #include <openssl/asn1t.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/buffer.h>
  64. #include <openssl/err.h>
  65. #include <openssl/x509v3.h>
  66. #include "internal/asn1_int.h"
  67. #include "asn1_locl.h"
  68. /*
  69. * Print routines.
  70. */
  71. /* ASN1_PCTX routines */
  72. static ASN1_PCTX default_pctx = {
  73. ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
  74. 0, /* nm_flags */
  75. 0, /* cert_flags */
  76. 0, /* oid_flags */
  77. 0 /* str_flags */
  78. };
  79. ASN1_PCTX *ASN1_PCTX_new(void)
  80. {
  81. ASN1_PCTX *ret;
  82. ret = OPENSSL_zalloc(sizeof(*ret));
  83. if (ret == NULL) {
  84. ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
  85. return NULL;
  86. }
  87. return ret;
  88. }
  89. void ASN1_PCTX_free(ASN1_PCTX *p)
  90. {
  91. OPENSSL_free(p);
  92. }
  93. unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
  94. {
  95. return p->flags;
  96. }
  97. void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
  98. {
  99. p->flags = flags;
  100. }
  101. unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
  102. {
  103. return p->nm_flags;
  104. }
  105. void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
  106. {
  107. p->nm_flags = flags;
  108. }
  109. unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
  110. {
  111. return p->cert_flags;
  112. }
  113. void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
  114. {
  115. p->cert_flags = flags;
  116. }
  117. unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
  118. {
  119. return p->oid_flags;
  120. }
  121. void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
  122. {
  123. p->oid_flags = flags;
  124. }
  125. unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
  126. {
  127. return p->str_flags;
  128. }
  129. void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
  130. {
  131. p->str_flags = flags;
  132. }
  133. /* Main print routines */
  134. static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  135. const ASN1_ITEM *it,
  136. const char *fname, const char *sname,
  137. int nohdr, const ASN1_PCTX *pctx);
  138. int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  139. const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
  140. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  141. const ASN1_ITEM *it, int indent,
  142. const char *fname, const char *sname,
  143. const ASN1_PCTX *pctx);
  144. static int asn1_print_fsname(BIO *out, int indent,
  145. const char *fname, const char *sname,
  146. const ASN1_PCTX *pctx);
  147. int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
  148. const ASN1_ITEM *it, const ASN1_PCTX *pctx)
  149. {
  150. const char *sname;
  151. if (pctx == NULL)
  152. pctx = &default_pctx;
  153. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  154. sname = NULL;
  155. else
  156. sname = it->sname;
  157. return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
  158. }
  159. static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  160. const ASN1_ITEM *it,
  161. const char *fname, const char *sname,
  162. int nohdr, const ASN1_PCTX *pctx)
  163. {
  164. const ASN1_TEMPLATE *tt;
  165. const ASN1_EXTERN_FUNCS *ef;
  166. ASN1_VALUE **tmpfld;
  167. const ASN1_AUX *aux = it->funcs;
  168. ASN1_aux_cb *asn1_cb;
  169. ASN1_PRINT_ARG parg;
  170. int i;
  171. if (aux && aux->asn1_cb) {
  172. parg.out = out;
  173. parg.indent = indent;
  174. parg.pctx = pctx;
  175. asn1_cb = aux->asn1_cb;
  176. } else
  177. asn1_cb = 0;
  178. if (*fld == NULL) {
  179. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
  180. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  181. return 0;
  182. if (BIO_puts(out, "<ABSENT>\n") <= 0)
  183. return 0;
  184. }
  185. return 1;
  186. }
  187. switch (it->itype) {
  188. case ASN1_ITYPE_PRIMITIVE:
  189. if (it->templates) {
  190. if (!asn1_template_print_ctx(out, fld, indent,
  191. it->templates, pctx))
  192. return 0;
  193. break;
  194. }
  195. /* fall thru */
  196. case ASN1_ITYPE_MSTRING:
  197. if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
  198. return 0;
  199. break;
  200. case ASN1_ITYPE_EXTERN:
  201. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  202. return 0;
  203. /* Use new style print routine if possible */
  204. ef = it->funcs;
  205. if (ef && ef->asn1_ex_print) {
  206. i = ef->asn1_ex_print(out, fld, indent, "", pctx);
  207. if (!i)
  208. return 0;
  209. if ((i == 2) && (BIO_puts(out, "\n") <= 0))
  210. return 0;
  211. return 1;
  212. } else if (sname &&
  213. BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
  214. return 0;
  215. break;
  216. case ASN1_ITYPE_CHOICE:
  217. /* CHOICE type, get selector */
  218. i = asn1_get_choice_selector(fld, it);
  219. /* This should never happen... */
  220. if ((i < 0) || (i >= it->tcount)) {
  221. if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
  222. return 0;
  223. return 1;
  224. }
  225. tt = it->templates + i;
  226. tmpfld = asn1_get_field_ptr(fld, tt);
  227. if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
  228. return 0;
  229. break;
  230. case ASN1_ITYPE_SEQUENCE:
  231. case ASN1_ITYPE_NDEF_SEQUENCE:
  232. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  233. return 0;
  234. if (fname || sname) {
  235. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  236. if (BIO_puts(out, " {\n") <= 0)
  237. return 0;
  238. } else {
  239. if (BIO_puts(out, "\n") <= 0)
  240. return 0;
  241. }
  242. }
  243. if (asn1_cb) {
  244. i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
  245. if (i == 0)
  246. return 0;
  247. if (i == 2)
  248. return 1;
  249. }
  250. /* Print each field entry */
  251. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  252. const ASN1_TEMPLATE *seqtt;
  253. seqtt = asn1_do_adb(fld, tt, 1);
  254. if (!seqtt)
  255. return 0;
  256. tmpfld = asn1_get_field_ptr(fld, seqtt);
  257. if (!asn1_template_print_ctx(out, tmpfld,
  258. indent + 2, seqtt, pctx))
  259. return 0;
  260. }
  261. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  262. if (BIO_printf(out, "%*s}\n", indent, "") < 0)
  263. return 0;
  264. }
  265. if (asn1_cb) {
  266. i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
  267. if (i == 0)
  268. return 0;
  269. }
  270. break;
  271. default:
  272. BIO_printf(out, "Unprocessed type %d\n", it->itype);
  273. return 0;
  274. }
  275. return 1;
  276. }
  277. int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  278. const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
  279. {
  280. int i, flags;
  281. const char *sname, *fname;
  282. flags = tt->flags;
  283. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
  284. sname = ASN1_ITEM_ptr(tt->item)->sname;
  285. else
  286. sname = NULL;
  287. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  288. fname = NULL;
  289. else
  290. fname = tt->field_name;
  291. if (flags & ASN1_TFLG_SK_MASK) {
  292. char *tname;
  293. ASN1_VALUE *skitem;
  294. STACK_OF(ASN1_VALUE) *stack;
  295. /* SET OF, SEQUENCE OF */
  296. if (fname) {
  297. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
  298. if (flags & ASN1_TFLG_SET_OF)
  299. tname = "SET";
  300. else
  301. tname = "SEQUENCE";
  302. if (BIO_printf(out, "%*s%s OF %s {\n",
  303. indent, "", tname, tt->field_name) <= 0)
  304. return 0;
  305. } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
  306. return 0;
  307. }
  308. stack = (STACK_OF(ASN1_VALUE) *)*fld;
  309. for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
  310. if ((i > 0) && (BIO_puts(out, "\n") <= 0))
  311. return 0;
  312. skitem = sk_ASN1_VALUE_value(stack, i);
  313. if (!asn1_item_print_ctx(out, &skitem, indent + 2,
  314. ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
  315. pctx))
  316. return 0;
  317. }
  318. if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
  319. return 0;
  320. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  321. if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
  322. return 0;
  323. }
  324. return 1;
  325. }
  326. return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
  327. fname, sname, 0, pctx);
  328. }
  329. static int asn1_print_fsname(BIO *out, int indent,
  330. const char *fname, const char *sname,
  331. const ASN1_PCTX *pctx)
  332. {
  333. static const char spaces[] = " ";
  334. static const int nspaces = sizeof(spaces) - 1;
  335. while (indent > nspaces) {
  336. if (BIO_write(out, spaces, nspaces) != nspaces)
  337. return 0;
  338. indent -= nspaces;
  339. }
  340. if (BIO_write(out, spaces, indent) != indent)
  341. return 0;
  342. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  343. sname = NULL;
  344. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  345. fname = NULL;
  346. if (!sname && !fname)
  347. return 1;
  348. if (fname) {
  349. if (BIO_puts(out, fname) <= 0)
  350. return 0;
  351. }
  352. if (sname) {
  353. if (fname) {
  354. if (BIO_printf(out, " (%s)", sname) <= 0)
  355. return 0;
  356. } else {
  357. if (BIO_puts(out, sname) <= 0)
  358. return 0;
  359. }
  360. }
  361. if (BIO_write(out, ": ", 2) != 2)
  362. return 0;
  363. return 1;
  364. }
  365. static int asn1_print_boolean_ctx(BIO *out, int boolval,
  366. const ASN1_PCTX *pctx)
  367. {
  368. const char *str;
  369. switch (boolval) {
  370. case -1:
  371. str = "BOOL ABSENT";
  372. break;
  373. case 0:
  374. str = "FALSE";
  375. break;
  376. default:
  377. str = "TRUE";
  378. break;
  379. }
  380. if (BIO_puts(out, str) <= 0)
  381. return 0;
  382. return 1;
  383. }
  384. static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
  385. const ASN1_PCTX *pctx)
  386. {
  387. char *s;
  388. int ret = 1;
  389. s = i2s_ASN1_INTEGER(NULL, str);
  390. if (BIO_puts(out, s) <= 0)
  391. ret = 0;
  392. OPENSSL_free(s);
  393. return ret;
  394. }
  395. static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
  396. const ASN1_PCTX *pctx)
  397. {
  398. char objbuf[80];
  399. const char *ln;
  400. ln = OBJ_nid2ln(OBJ_obj2nid(oid));
  401. if (!ln)
  402. ln = "";
  403. OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
  404. if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
  405. return 0;
  406. return 1;
  407. }
  408. static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
  409. const ASN1_PCTX *pctx)
  410. {
  411. if (str->type == V_ASN1_BIT_STRING) {
  412. if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
  413. return 0;
  414. } else if (BIO_puts(out, "\n") <= 0)
  415. return 0;
  416. if ((str->length > 0)
  417. && BIO_dump_indent(out, (char *)str->data, str->length,
  418. indent + 2) <= 0)
  419. return 0;
  420. return 1;
  421. }
  422. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  423. const ASN1_ITEM *it, int indent,
  424. const char *fname, const char *sname,
  425. const ASN1_PCTX *pctx)
  426. {
  427. long utype;
  428. ASN1_STRING *str;
  429. int ret = 1, needlf = 1;
  430. const char *pname;
  431. const ASN1_PRIMITIVE_FUNCS *pf;
  432. pf = it->funcs;
  433. if (!asn1_print_fsname(out, indent, fname, sname, pctx))
  434. return 0;
  435. if (pf && pf->prim_print)
  436. return pf->prim_print(out, fld, it, indent, pctx);
  437. str = (ASN1_STRING *)*fld;
  438. if (it->itype == ASN1_ITYPE_MSTRING)
  439. utype = str->type & ~V_ASN1_NEG;
  440. else
  441. utype = it->utype;
  442. if (utype == V_ASN1_ANY) {
  443. ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
  444. utype = atype->type;
  445. fld = &atype->value.asn1_value;
  446. str = (ASN1_STRING *)*fld;
  447. if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
  448. pname = NULL;
  449. else
  450. pname = ASN1_tag2str(utype);
  451. } else {
  452. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
  453. pname = ASN1_tag2str(utype);
  454. else
  455. pname = NULL;
  456. }
  457. if (utype == V_ASN1_NULL) {
  458. if (BIO_puts(out, "NULL\n") <= 0)
  459. return 0;
  460. return 1;
  461. }
  462. if (pname) {
  463. if (BIO_puts(out, pname) <= 0)
  464. return 0;
  465. if (BIO_puts(out, ":") <= 0)
  466. return 0;
  467. }
  468. switch (utype) {
  469. case V_ASN1_BOOLEAN:
  470. {
  471. int boolval = *(int *)fld;
  472. if (boolval == -1)
  473. boolval = it->size;
  474. ret = asn1_print_boolean_ctx(out, boolval, pctx);
  475. }
  476. break;
  477. case V_ASN1_INTEGER:
  478. case V_ASN1_ENUMERATED:
  479. ret = asn1_print_integer_ctx(out, str, pctx);
  480. break;
  481. case V_ASN1_UTCTIME:
  482. ret = ASN1_UTCTIME_print(out, str);
  483. break;
  484. case V_ASN1_GENERALIZEDTIME:
  485. ret = ASN1_GENERALIZEDTIME_print(out, str);
  486. break;
  487. case V_ASN1_OBJECT:
  488. ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
  489. break;
  490. case V_ASN1_OCTET_STRING:
  491. case V_ASN1_BIT_STRING:
  492. ret = asn1_print_obstring_ctx(out, str, indent, pctx);
  493. needlf = 0;
  494. break;
  495. case V_ASN1_SEQUENCE:
  496. case V_ASN1_SET:
  497. case V_ASN1_OTHER:
  498. if (BIO_puts(out, "\n") <= 0)
  499. return 0;
  500. if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
  501. ret = 0;
  502. needlf = 0;
  503. break;
  504. default:
  505. ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
  506. }
  507. if (!ret)
  508. return 0;
  509. if (needlf && BIO_puts(out, "\n") <= 0)
  510. return 0;
  511. return 1;
  512. }