tasn_prn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* tasn_prn.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000,2005 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stddef.h>
  60. #include "cryptlib.h"
  61. #include <openssl/asn1.h>
  62. #include <openssl/asn1t.h>
  63. #include <openssl/objects.h>
  64. #include <openssl/buffer.h>
  65. #include <openssl/err.h>
  66. #include <openssl/x509v3.h>
  67. #include "asn1_locl.h"
  68. /*
  69. * Print routines.
  70. */
  71. /* ASN1_PCTX routines */
  72. 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_malloc(sizeof(ASN1_PCTX));
  83. if (ret == NULL) {
  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, NULL, sname, 0, pctx);
  163. }
  164. static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  165. const ASN1_ITEM *it,
  166. const char *fname, const char *sname,
  167. int nohdr, const ASN1_PCTX *pctx)
  168. {
  169. const ASN1_TEMPLATE *tt;
  170. const ASN1_EXTERN_FUNCS *ef;
  171. ASN1_VALUE **tmpfld;
  172. const ASN1_AUX *aux = it->funcs;
  173. ASN1_aux_cb *asn1_cb;
  174. ASN1_PRINT_ARG parg;
  175. int i;
  176. if (aux && aux->asn1_cb) {
  177. parg.out = out;
  178. parg.indent = indent;
  179. parg.pctx = pctx;
  180. asn1_cb = aux->asn1_cb;
  181. } else
  182. asn1_cb = 0;
  183. if (((it->itype != ASN1_ITYPE_PRIMITIVE)
  184. || (it->utype != V_ASN1_BOOLEAN)) && *fld == NULL) {
  185. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
  186. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  187. return 0;
  188. if (BIO_puts(out, "<ABSENT>\n") <= 0)
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. switch (it->itype) {
  194. case ASN1_ITYPE_PRIMITIVE:
  195. if (it->templates) {
  196. if (!asn1_template_print_ctx(out, fld, indent,
  197. it->templates, pctx))
  198. return 0;
  199. break;
  200. }
  201. /* fall thru */
  202. case ASN1_ITYPE_MSTRING:
  203. if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
  204. return 0;
  205. break;
  206. case ASN1_ITYPE_EXTERN:
  207. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  208. return 0;
  209. /* Use new style print routine if possible */
  210. ef = it->funcs;
  211. if (ef && ef->asn1_ex_print) {
  212. i = ef->asn1_ex_print(out, fld, indent, "", pctx);
  213. if (!i)
  214. return 0;
  215. if ((i == 2) && (BIO_puts(out, "\n") <= 0))
  216. return 0;
  217. return 1;
  218. } else if (sname &&
  219. BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
  220. return 0;
  221. break;
  222. case ASN1_ITYPE_CHOICE:
  223. #if 0
  224. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  225. return 0;
  226. #endif
  227. /* CHOICE type, get selector */
  228. i = asn1_get_choice_selector(fld, it);
  229. /* This should never happen... */
  230. if ((i < 0) || (i >= it->tcount)) {
  231. if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
  232. return 0;
  233. return 1;
  234. }
  235. tt = it->templates + i;
  236. tmpfld = asn1_get_field_ptr(fld, tt);
  237. if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
  238. return 0;
  239. break;
  240. case ASN1_ITYPE_SEQUENCE:
  241. case ASN1_ITYPE_NDEF_SEQUENCE:
  242. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  243. return 0;
  244. if (fname || sname) {
  245. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  246. if (BIO_puts(out, " {\n") <= 0)
  247. return 0;
  248. } else {
  249. if (BIO_puts(out, "\n") <= 0)
  250. return 0;
  251. }
  252. }
  253. if (asn1_cb) {
  254. i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
  255. if (i == 0)
  256. return 0;
  257. if (i == 2)
  258. return 1;
  259. }
  260. /* Print each field entry */
  261. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  262. const ASN1_TEMPLATE *seqtt;
  263. seqtt = asn1_do_adb(fld, tt, 1);
  264. if (!seqtt)
  265. return 0;
  266. tmpfld = asn1_get_field_ptr(fld, seqtt);
  267. if (!asn1_template_print_ctx(out, tmpfld,
  268. indent + 2, seqtt, pctx))
  269. return 0;
  270. }
  271. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  272. if (BIO_printf(out, "%*s}\n", indent, "") < 0)
  273. return 0;
  274. }
  275. if (asn1_cb) {
  276. i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
  277. if (i == 0)
  278. return 0;
  279. }
  280. break;
  281. default:
  282. BIO_printf(out, "Unprocessed type %d\n", it->itype);
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  288. const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
  289. {
  290. int i, flags;
  291. const char *sname, *fname;
  292. flags = tt->flags;
  293. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
  294. sname = ASN1_ITEM_ptr(tt->item)->sname;
  295. else
  296. sname = NULL;
  297. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  298. fname = NULL;
  299. else
  300. fname = tt->field_name;
  301. if (flags & ASN1_TFLG_SK_MASK) {
  302. char *tname;
  303. ASN1_VALUE *skitem;
  304. STACK_OF(ASN1_VALUE) *stack;
  305. /* SET OF, SEQUENCE OF */
  306. if (fname) {
  307. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
  308. if (flags & ASN1_TFLG_SET_OF)
  309. tname = "SET";
  310. else
  311. tname = "SEQUENCE";
  312. if (BIO_printf(out, "%*s%s OF %s {\n",
  313. indent, "", tname, tt->field_name) <= 0)
  314. return 0;
  315. } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
  316. return 0;
  317. }
  318. stack = (STACK_OF(ASN1_VALUE) *)*fld;
  319. for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
  320. if ((i > 0) && (BIO_puts(out, "\n") <= 0))
  321. return 0;
  322. skitem = sk_ASN1_VALUE_value(stack, i);
  323. if (!asn1_item_print_ctx(out, &skitem, indent + 2,
  324. ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
  325. pctx))
  326. return 0;
  327. }
  328. if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
  329. return 0;
  330. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  331. if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
  332. return 0;
  333. }
  334. return 1;
  335. }
  336. return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
  337. fname, sname, 0, pctx);
  338. }
  339. static int asn1_print_fsname(BIO *out, int indent,
  340. const char *fname, const char *sname,
  341. const ASN1_PCTX *pctx)
  342. {
  343. static char spaces[] = " ";
  344. const int nspaces = sizeof(spaces) - 1;
  345. #if 0
  346. if (!sname && !fname)
  347. return 1;
  348. #endif
  349. while (indent > nspaces) {
  350. if (BIO_write(out, spaces, nspaces) != nspaces)
  351. return 0;
  352. indent -= nspaces;
  353. }
  354. if (BIO_write(out, spaces, indent) != indent)
  355. return 0;
  356. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  357. sname = NULL;
  358. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  359. fname = NULL;
  360. if (!sname && !fname)
  361. return 1;
  362. if (fname) {
  363. if (BIO_puts(out, fname) <= 0)
  364. return 0;
  365. }
  366. if (sname) {
  367. if (fname) {
  368. if (BIO_printf(out, " (%s)", sname) <= 0)
  369. return 0;
  370. } else {
  371. if (BIO_puts(out, sname) <= 0)
  372. return 0;
  373. }
  374. }
  375. if (BIO_write(out, ": ", 2) != 2)
  376. return 0;
  377. return 1;
  378. }
  379. static int asn1_print_boolean_ctx(BIO *out, int boolval,
  380. const ASN1_PCTX *pctx)
  381. {
  382. const char *str;
  383. switch (boolval) {
  384. case -1:
  385. str = "BOOL ABSENT";
  386. break;
  387. case 0:
  388. str = "FALSE";
  389. break;
  390. default:
  391. str = "TRUE";
  392. break;
  393. }
  394. if (BIO_puts(out, str) <= 0)
  395. return 0;
  396. return 1;
  397. }
  398. static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
  399. const ASN1_PCTX *pctx)
  400. {
  401. char *s;
  402. int ret = 1;
  403. s = i2s_ASN1_INTEGER(NULL, str);
  404. if (s == NULL)
  405. return 0;
  406. if (BIO_puts(out, s) <= 0)
  407. ret = 0;
  408. OPENSSL_free(s);
  409. return ret;
  410. }
  411. static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
  412. const ASN1_PCTX *pctx)
  413. {
  414. char objbuf[80];
  415. const char *ln;
  416. ln = OBJ_nid2ln(OBJ_obj2nid(oid));
  417. if (!ln)
  418. ln = "";
  419. OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
  420. if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
  421. return 0;
  422. return 1;
  423. }
  424. static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
  425. const ASN1_PCTX *pctx)
  426. {
  427. if (str->type == V_ASN1_BIT_STRING) {
  428. if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
  429. return 0;
  430. } else if (BIO_puts(out, "\n") <= 0)
  431. return 0;
  432. if ((str->length > 0)
  433. && BIO_dump_indent(out, (char *)str->data, str->length,
  434. indent + 2) <= 0)
  435. return 0;
  436. return 1;
  437. }
  438. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  439. const ASN1_ITEM *it, int indent,
  440. const char *fname, const char *sname,
  441. const ASN1_PCTX *pctx)
  442. {
  443. long utype;
  444. ASN1_STRING *str;
  445. int ret = 1, needlf = 1;
  446. const char *pname;
  447. const ASN1_PRIMITIVE_FUNCS *pf;
  448. pf = it->funcs;
  449. if (!asn1_print_fsname(out, indent, fname, sname, pctx))
  450. return 0;
  451. if (pf && pf->prim_print)
  452. return pf->prim_print(out, fld, it, indent, pctx);
  453. if (it->itype == ASN1_ITYPE_MSTRING) {
  454. str = (ASN1_STRING *)*fld;
  455. utype = str->type & ~V_ASN1_NEG;
  456. } else {
  457. utype = it->utype;
  458. if (utype == V_ASN1_BOOLEAN)
  459. str = NULL;
  460. else
  461. str = (ASN1_STRING *)*fld;
  462. }
  463. if (utype == V_ASN1_ANY) {
  464. ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
  465. utype = atype->type;
  466. fld = &atype->value.asn1_value;
  467. str = (ASN1_STRING *)*fld;
  468. if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
  469. pname = NULL;
  470. else
  471. pname = ASN1_tag2str(utype);
  472. } else {
  473. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
  474. pname = ASN1_tag2str(utype);
  475. else
  476. pname = NULL;
  477. }
  478. if (utype == V_ASN1_NULL) {
  479. if (BIO_puts(out, "NULL\n") <= 0)
  480. return 0;
  481. return 1;
  482. }
  483. if (pname) {
  484. if (BIO_puts(out, pname) <= 0)
  485. return 0;
  486. if (BIO_puts(out, ":") <= 0)
  487. return 0;
  488. }
  489. switch (utype) {
  490. case V_ASN1_BOOLEAN:
  491. {
  492. int boolval = *(int *)fld;
  493. if (boolval == -1)
  494. boolval = it->size;
  495. ret = asn1_print_boolean_ctx(out, boolval, pctx);
  496. }
  497. break;
  498. case V_ASN1_INTEGER:
  499. case V_ASN1_ENUMERATED:
  500. ret = asn1_print_integer_ctx(out, str, pctx);
  501. break;
  502. case V_ASN1_UTCTIME:
  503. ret = ASN1_UTCTIME_print(out, str);
  504. break;
  505. case V_ASN1_GENERALIZEDTIME:
  506. ret = ASN1_GENERALIZEDTIME_print(out, str);
  507. break;
  508. case V_ASN1_OBJECT:
  509. ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
  510. break;
  511. case V_ASN1_OCTET_STRING:
  512. case V_ASN1_BIT_STRING:
  513. ret = asn1_print_obstring_ctx(out, str, indent, pctx);
  514. needlf = 0;
  515. break;
  516. case V_ASN1_SEQUENCE:
  517. case V_ASN1_SET:
  518. case V_ASN1_OTHER:
  519. if (BIO_puts(out, "\n") <= 0)
  520. return 0;
  521. if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
  522. ret = 0;
  523. needlf = 0;
  524. break;
  525. default:
  526. ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
  527. }
  528. if (!ret)
  529. return 0;
  530. if (needlf && BIO_puts(out, "\n") <= 0)
  531. return 0;
  532. return 1;
  533. }