tasn_prn.c 14 KB

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