2
0

tasn_prn.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /* tasn_prn.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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. /* SET OF, SEQUENCE OF */
  322. if (fname)
  323. {
  324. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
  325. {
  326. if(flags & ASN1_TFLG_SET_OF)
  327. tname = "SET";
  328. else
  329. tname = "SEQUENCE";
  330. if (BIO_printf(out, "%*s%s OF %s {\n",
  331. indent, "", tname, tt->field_name) <= 0)
  332. return 0;
  333. }
  334. else if (BIO_printf(out, "%*s%s:\n", indent, "",
  335. fname) <= 0)
  336. return 0;
  337. }
  338. for(i = 0; i < sk_num((STACK *)*fld); i++)
  339. {
  340. if ((i > 0) && (BIO_puts(out, "\n") <= 0))
  341. return 0;
  342. skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
  343. if (!asn1_item_print_ctx(out, &skitem, indent + 2,
  344. ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
  345. return 0;
  346. }
  347. if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
  348. return 0;
  349. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
  350. {
  351. if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
  352. return 0;
  353. }
  354. return 1;
  355. }
  356. return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
  357. fname, sname, 0, pctx);
  358. }
  359. static int asn1_print_fsname(BIO *out, int indent,
  360. const char *fname, const char *sname,
  361. const ASN1_PCTX *pctx)
  362. {
  363. static char spaces[] = " ";
  364. const int nspaces = sizeof(spaces) - 1;
  365. #if 0
  366. if (!sname && !fname)
  367. return 1;
  368. #endif
  369. while (indent > nspaces)
  370. {
  371. if (BIO_write(out, spaces, nspaces) != nspaces)
  372. return 0;
  373. indent -= nspaces;
  374. }
  375. if (BIO_write(out, spaces, indent) != indent)
  376. return 0;
  377. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  378. sname = NULL;
  379. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  380. fname = NULL;
  381. if (!sname && !fname)
  382. return 1;
  383. if (fname)
  384. {
  385. if (BIO_puts(out, fname) <= 0)
  386. return 0;
  387. }
  388. if (sname)
  389. {
  390. if (fname)
  391. {
  392. if (BIO_printf(out, " (%s)", sname) <= 0)
  393. return 0;
  394. }
  395. else
  396. {
  397. if (BIO_puts(out, sname) <= 0)
  398. return 0;
  399. }
  400. }
  401. if (BIO_write(out, ": ", 2) != 2)
  402. return 0;
  403. return 1;
  404. }
  405. static int asn1_print_boolean_ctx(BIO *out, const int bool,
  406. const ASN1_PCTX *pctx)
  407. {
  408. const char *str;
  409. switch (bool)
  410. {
  411. case -1:
  412. str = "BOOL ABSENT";
  413. break;
  414. case 0:
  415. str = "FALSE";
  416. break;
  417. default:
  418. str = "TRUE";
  419. break;
  420. }
  421. if (BIO_puts(out, str) <= 0)
  422. return 0;
  423. return 1;
  424. }
  425. static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
  426. const ASN1_PCTX *pctx)
  427. {
  428. char *s;
  429. int ret = 1;
  430. s = i2s_ASN1_INTEGER(NULL, str);
  431. if (BIO_puts(out, s) <= 0)
  432. ret = 0;
  433. OPENSSL_free(s);
  434. return ret;
  435. }
  436. static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
  437. const ASN1_PCTX *pctx)
  438. {
  439. char objbuf[80];
  440. const char *ln;
  441. ln = OBJ_nid2ln(OBJ_obj2nid(oid));
  442. if(!ln)
  443. ln = "";
  444. OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
  445. if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
  446. return 0;
  447. return 1;
  448. }
  449. static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
  450. const ASN1_PCTX *pctx)
  451. {
  452. if (str->type == V_ASN1_BIT_STRING)
  453. {
  454. if (BIO_printf(out, " (%ld unused bits)\n",
  455. str->flags & 0x7) <= 0)
  456. return 0;
  457. }
  458. else if (BIO_puts(out, "\n") <= 0)
  459. return 0;
  460. if ((str->length > 0)
  461. && BIO_dump_indent(out, (char *)str->data, str->length,
  462. indent + 2) <= 0)
  463. return 0;
  464. return 1;
  465. }
  466. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  467. const ASN1_ITEM *it, int indent,
  468. const char *fname, const char *sname,
  469. const ASN1_PCTX *pctx)
  470. {
  471. long utype;
  472. ASN1_STRING *str;
  473. int ret = 1, needlf = 1;
  474. const char *pname;
  475. if (!asn1_print_fsname(out, indent, fname, sname, pctx))
  476. return 0;
  477. str = (ASN1_STRING *)*fld;
  478. if (it->itype == ASN1_ITYPE_MSTRING)
  479. utype = str->type & ~V_ASN1_NEG;
  480. else
  481. utype = it->utype;
  482. if (utype == V_ASN1_ANY)
  483. {
  484. ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
  485. utype = atype->type;
  486. fld = (ASN1_VALUE **)&atype->value.ptr;
  487. str = (ASN1_STRING *)*fld;
  488. if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
  489. pname = NULL;
  490. else
  491. pname = ASN1_tag2str(utype);
  492. }
  493. else
  494. {
  495. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
  496. pname = ASN1_tag2str(utype);
  497. else
  498. pname = NULL;
  499. }
  500. if (utype == V_ASN1_NULL)
  501. {
  502. if (BIO_puts(out, "NULL\n") <= 0)
  503. return 0;
  504. return 1;
  505. }
  506. if (pname)
  507. {
  508. if (BIO_puts(out, pname) <= 0)
  509. return 0;
  510. if (BIO_puts(out, ":") <= 0)
  511. return 0;
  512. }
  513. switch (utype)
  514. {
  515. case V_ASN1_BOOLEAN:
  516. {
  517. int bool = *(int *)fld;
  518. if (bool == -1)
  519. bool = it->size;
  520. ret = asn1_print_boolean_ctx(out, bool, pctx);
  521. }
  522. break;
  523. case V_ASN1_INTEGER:
  524. case V_ASN1_ENUMERATED:
  525. ret = asn1_print_integer_ctx(out, str, pctx);
  526. break;
  527. case V_ASN1_UTCTIME:
  528. ret = ASN1_UTCTIME_print(out, str);
  529. break;
  530. case V_ASN1_GENERALIZEDTIME:
  531. ret = ASN1_GENERALIZEDTIME_print(out, str);
  532. break;
  533. case V_ASN1_OBJECT:
  534. ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
  535. break;
  536. case V_ASN1_OCTET_STRING:
  537. case V_ASN1_BIT_STRING:
  538. ret = asn1_print_obstring_ctx(out, str, indent, pctx);
  539. needlf = 0;
  540. break;
  541. case V_ASN1_SEQUENCE:
  542. case V_ASN1_SET:
  543. case V_ASN1_OTHER:
  544. if (BIO_puts(out, "\n") <= 0)
  545. return 0;
  546. if (ASN1_parse_dump(out, str->data, str->length,
  547. indent, 0) <= 0)
  548. ret = 0;
  549. needlf = 0;
  550. break;
  551. default:
  552. ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
  553. }
  554. if (!ret)
  555. return 0;
  556. if (needlf && BIO_puts(out, "\n") <= 0)
  557. return 0;
  558. return 1;
  559. }