tasn_prn.c 15 KB

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