tasn_prn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 (*fld == NULL) {
  184. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
  185. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  186. return 0;
  187. if (BIO_puts(out, "<ABSENT>\n") <= 0)
  188. return 0;
  189. }
  190. return 1;
  191. }
  192. switch (it->itype) {
  193. case ASN1_ITYPE_PRIMITIVE:
  194. if (it->templates) {
  195. if (!asn1_template_print_ctx(out, fld, indent,
  196. it->templates, pctx))
  197. return 0;
  198. break;
  199. }
  200. /* fall thru */
  201. case ASN1_ITYPE_MSTRING:
  202. if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
  203. return 0;
  204. break;
  205. case ASN1_ITYPE_EXTERN:
  206. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  207. return 0;
  208. /* Use new style print routine if possible */
  209. ef = it->funcs;
  210. if (ef && ef->asn1_ex_print) {
  211. i = ef->asn1_ex_print(out, fld, indent, "", pctx);
  212. if (!i)
  213. return 0;
  214. if ((i == 2) && (BIO_puts(out, "\n") <= 0))
  215. return 0;
  216. return 1;
  217. } else if (sname &&
  218. BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
  219. return 0;
  220. break;
  221. case ASN1_ITYPE_CHOICE:
  222. #if 0
  223. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  224. return 0;
  225. #endif
  226. /* CHOICE type, get selector */
  227. i = asn1_get_choice_selector(fld, it);
  228. /* This should never happen... */
  229. if ((i < 0) || (i >= it->tcount)) {
  230. if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
  231. return 0;
  232. return 1;
  233. }
  234. tt = it->templates + i;
  235. tmpfld = asn1_get_field_ptr(fld, tt);
  236. if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
  237. return 0;
  238. break;
  239. case ASN1_ITYPE_SEQUENCE:
  240. case ASN1_ITYPE_NDEF_SEQUENCE:
  241. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  242. return 0;
  243. if (fname || sname) {
  244. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  245. if (BIO_puts(out, " {\n") <= 0)
  246. return 0;
  247. } else {
  248. if (BIO_puts(out, "\n") <= 0)
  249. return 0;
  250. }
  251. }
  252. if (asn1_cb) {
  253. i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
  254. if (i == 0)
  255. return 0;
  256. if (i == 2)
  257. return 1;
  258. }
  259. /* Print each field entry */
  260. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  261. const ASN1_TEMPLATE *seqtt;
  262. seqtt = asn1_do_adb(fld, tt, 1);
  263. if (!seqtt)
  264. return 0;
  265. tmpfld = asn1_get_field_ptr(fld, seqtt);
  266. if (!asn1_template_print_ctx(out, tmpfld,
  267. indent + 2, seqtt, pctx))
  268. return 0;
  269. }
  270. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  271. if (BIO_printf(out, "%*s}\n", indent, "") < 0)
  272. return 0;
  273. }
  274. if (asn1_cb) {
  275. i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
  276. if (i == 0)
  277. return 0;
  278. }
  279. break;
  280. default:
  281. BIO_printf(out, "Unprocessed type %d\n", it->itype);
  282. return 0;
  283. }
  284. return 1;
  285. }
  286. int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  287. const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
  288. {
  289. int i, flags;
  290. const char *sname, *fname;
  291. flags = tt->flags;
  292. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
  293. sname = ASN1_ITEM_ptr(tt->item)->sname;
  294. else
  295. sname = NULL;
  296. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  297. fname = NULL;
  298. else
  299. fname = tt->field_name;
  300. if (flags & ASN1_TFLG_SK_MASK) {
  301. char *tname;
  302. ASN1_VALUE *skitem;
  303. STACK_OF(ASN1_VALUE) *stack;
  304. /* SET OF, SEQUENCE OF */
  305. if (fname) {
  306. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
  307. if (flags & ASN1_TFLG_SET_OF)
  308. tname = "SET";
  309. else
  310. tname = "SEQUENCE";
  311. if (BIO_printf(out, "%*s%s OF %s {\n",
  312. indent, "", tname, tt->field_name) <= 0)
  313. return 0;
  314. } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
  315. return 0;
  316. }
  317. stack = (STACK_OF(ASN1_VALUE) *)*fld;
  318. for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
  319. if ((i > 0) && (BIO_puts(out, "\n") <= 0))
  320. return 0;
  321. skitem = sk_ASN1_VALUE_value(stack, i);
  322. if (!asn1_item_print_ctx(out, &skitem, indent + 2,
  323. ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
  324. pctx))
  325. return 0;
  326. }
  327. if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
  328. return 0;
  329. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
  330. if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
  331. return 0;
  332. }
  333. return 1;
  334. }
  335. return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
  336. fname, sname, 0, pctx);
  337. }
  338. static int asn1_print_fsname(BIO *out, int indent,
  339. const char *fname, const char *sname,
  340. const ASN1_PCTX *pctx)
  341. {
  342. static char spaces[] = " ";
  343. const int nspaces = sizeof(spaces) - 1;
  344. #if 0
  345. if (!sname && !fname)
  346. return 1;
  347. #endif
  348. while (indent > nspaces) {
  349. if (BIO_write(out, spaces, nspaces) != nspaces)
  350. return 0;
  351. indent -= nspaces;
  352. }
  353. if (BIO_write(out, spaces, indent) != indent)
  354. return 0;
  355. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  356. sname = NULL;
  357. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  358. fname = NULL;
  359. if (!sname && !fname)
  360. return 1;
  361. if (fname) {
  362. if (BIO_puts(out, fname) <= 0)
  363. return 0;
  364. }
  365. if (sname) {
  366. if (fname) {
  367. if (BIO_printf(out, " (%s)", sname) <= 0)
  368. return 0;
  369. } else {
  370. if (BIO_puts(out, sname) <= 0)
  371. return 0;
  372. }
  373. }
  374. if (BIO_write(out, ": ", 2) != 2)
  375. return 0;
  376. return 1;
  377. }
  378. static int asn1_print_boolean_ctx(BIO *out, int boolval,
  379. const ASN1_PCTX *pctx)
  380. {
  381. const char *str;
  382. switch (boolval) {
  383. case -1:
  384. str = "BOOL ABSENT";
  385. break;
  386. case 0:
  387. str = "FALSE";
  388. break;
  389. default:
  390. str = "TRUE";
  391. break;
  392. }
  393. if (BIO_puts(out, str) <= 0)
  394. return 0;
  395. return 1;
  396. }
  397. static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
  398. const ASN1_PCTX *pctx)
  399. {
  400. char *s;
  401. int ret = 1;
  402. s = i2s_ASN1_INTEGER(NULL, str);
  403. if (BIO_puts(out, s) <= 0)
  404. ret = 0;
  405. OPENSSL_free(s);
  406. return ret;
  407. }
  408. static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
  409. const ASN1_PCTX *pctx)
  410. {
  411. char objbuf[80];
  412. const char *ln;
  413. ln = OBJ_nid2ln(OBJ_obj2nid(oid));
  414. if (!ln)
  415. ln = "";
  416. OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
  417. if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
  418. return 0;
  419. return 1;
  420. }
  421. static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
  422. const ASN1_PCTX *pctx)
  423. {
  424. if (str->type == V_ASN1_BIT_STRING) {
  425. if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
  426. return 0;
  427. } else if (BIO_puts(out, "\n") <= 0)
  428. return 0;
  429. if ((str->length > 0)
  430. && BIO_dump_indent(out, (char *)str->data, str->length,
  431. indent + 2) <= 0)
  432. return 0;
  433. return 1;
  434. }
  435. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  436. const ASN1_ITEM *it, int indent,
  437. const char *fname, const char *sname,
  438. const ASN1_PCTX *pctx)
  439. {
  440. long utype;
  441. ASN1_STRING *str;
  442. int ret = 1, needlf = 1;
  443. const char *pname;
  444. const ASN1_PRIMITIVE_FUNCS *pf;
  445. pf = it->funcs;
  446. if (!asn1_print_fsname(out, indent, fname, sname, pctx))
  447. return 0;
  448. if (pf && pf->prim_print)
  449. return pf->prim_print(out, fld, it, indent, pctx);
  450. str = (ASN1_STRING *)*fld;
  451. if (it->itype == ASN1_ITYPE_MSTRING)
  452. utype = str->type & ~V_ASN1_NEG;
  453. else
  454. utype = it->utype;
  455. if (utype == V_ASN1_ANY) {
  456. ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
  457. utype = atype->type;
  458. fld = &atype->value.asn1_value;
  459. str = (ASN1_STRING *)*fld;
  460. if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
  461. pname = NULL;
  462. else
  463. pname = ASN1_tag2str(utype);
  464. } else {
  465. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
  466. pname = ASN1_tag2str(utype);
  467. else
  468. pname = NULL;
  469. }
  470. if (utype == V_ASN1_NULL) {
  471. if (BIO_puts(out, "NULL\n") <= 0)
  472. return 0;
  473. return 1;
  474. }
  475. if (pname) {
  476. if (BIO_puts(out, pname) <= 0)
  477. return 0;
  478. if (BIO_puts(out, ":") <= 0)
  479. return 0;
  480. }
  481. switch (utype) {
  482. case V_ASN1_BOOLEAN:
  483. {
  484. int boolval = *(int *)fld;
  485. if (boolval == -1)
  486. boolval = it->size;
  487. ret = asn1_print_boolean_ctx(out, boolval, pctx);
  488. }
  489. break;
  490. case V_ASN1_INTEGER:
  491. case V_ASN1_ENUMERATED:
  492. ret = asn1_print_integer_ctx(out, str, pctx);
  493. break;
  494. case V_ASN1_UTCTIME:
  495. ret = ASN1_UTCTIME_print(out, str);
  496. break;
  497. case V_ASN1_GENERALIZEDTIME:
  498. ret = ASN1_GENERALIZEDTIME_print(out, str);
  499. break;
  500. case V_ASN1_OBJECT:
  501. ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
  502. break;
  503. case V_ASN1_OCTET_STRING:
  504. case V_ASN1_BIT_STRING:
  505. ret = asn1_print_obstring_ctx(out, str, indent, pctx);
  506. needlf = 0;
  507. break;
  508. case V_ASN1_SEQUENCE:
  509. case V_ASN1_SET:
  510. case V_ASN1_OTHER:
  511. if (BIO_puts(out, "\n") <= 0)
  512. return 0;
  513. if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
  514. ret = 0;
  515. needlf = 0;
  516. break;
  517. default:
  518. ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
  519. }
  520. if (!ret)
  521. return 0;
  522. if (needlf && BIO_puts(out, "\n") <= 0)
  523. return 0;
  524. return 1;
  525. }