tasn_prn.c 15 KB

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