asn1_par.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright 1995-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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/asn1.h>
  14. #ifndef ASN1_PARSE_MAXDEPTH
  15. #define ASN1_PARSE_MAXDEPTH 128
  16. #endif
  17. static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
  18. int offset, int depth, int indent, int dump);
  19. static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
  20. int tag, int xclass, int constructed, int indent)
  21. {
  22. char str[128];
  23. const char *p;
  24. int pop_f_prefix = 0;
  25. long saved_indent = -1;
  26. int i = 0;
  27. if (constructed & V_ASN1_CONSTRUCTED)
  28. p = "cons: ";
  29. else
  30. p = "prim: ";
  31. if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
  32. if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
  33. offset, depth, (long)hl, len, p) <= 0)
  34. goto err;
  35. } else {
  36. if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf %s",
  37. offset, depth, (long)hl, p) <= 0)
  38. goto err;
  39. }
  40. if (BIO_set_prefix(bp, str) <= 0) {
  41. if ((bp = BIO_push(BIO_new(BIO_f_prefix()), bp)) == NULL)
  42. goto err;
  43. pop_f_prefix = 1;
  44. }
  45. saved_indent = BIO_get_indent(bp);
  46. if (BIO_set_prefix(bp, str) <= 0
  47. || BIO_set_indent(bp, indent) < 0)
  48. goto err;
  49. /*
  50. * BIO_set_prefix made a copy of |str|, so we can safely use it for
  51. * something else, ASN.1 tag printout.
  52. */
  53. p = str;
  54. if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
  55. BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
  56. else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
  57. BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
  58. else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
  59. BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
  60. else if (tag > 30)
  61. BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
  62. else
  63. p = ASN1_tag2str(tag);
  64. i = (BIO_printf(bp, "%-18s", p) > 0);
  65. err:
  66. if (saved_indent >= 0)
  67. BIO_set_indent(bp, saved_indent);
  68. if (pop_f_prefix) {
  69. BIO_pop(bp);
  70. BIO_free(bp);
  71. }
  72. return i;
  73. }
  74. int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
  75. {
  76. return asn1_parse2(bp, &pp, len, 0, 0, indent, 0);
  77. }
  78. int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
  79. int dump)
  80. {
  81. return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
  82. }
  83. static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
  84. int offset, int depth, int indent, int dump)
  85. {
  86. const unsigned char *p, *ep, *tot, *op, *opp;
  87. long len;
  88. int tag, xclass, ret = 0;
  89. int nl, hl, j, r;
  90. ASN1_OBJECT *o = NULL;
  91. ASN1_OCTET_STRING *os = NULL;
  92. ASN1_INTEGER *ai = NULL;
  93. ASN1_ENUMERATED *ae = NULL;
  94. /* ASN1_BMPSTRING *bmp=NULL; */
  95. int dump_indent, dump_cont = 0;
  96. if (depth > ASN1_PARSE_MAXDEPTH) {
  97. BIO_puts(bp, "BAD RECURSION DEPTH\n");
  98. return 0;
  99. }
  100. dump_indent = 6; /* Because we know BIO_dump_indent() */
  101. p = *pp;
  102. tot = p + length;
  103. while (length > 0) {
  104. op = p;
  105. j = ASN1_get_object(&p, &len, &tag, &xclass, length);
  106. if (j & 0x80) {
  107. if (BIO_write(bp, "Error in encoding\n", 18) <= 0)
  108. goto end;
  109. ret = 0;
  110. goto end;
  111. }
  112. hl = (p - op);
  113. length -= hl;
  114. /*
  115. * if j == 0x21 it is a constructed indefinite length object
  116. */
  117. if (!asn1_print_info(bp, (long)offset + (long)(op - *pp), depth,
  118. hl, len, tag, xclass, j, (indent) ? depth : 0))
  119. goto end;
  120. if (j & V_ASN1_CONSTRUCTED) {
  121. const unsigned char *sp = p;
  122. ep = p + len;
  123. if (BIO_write(bp, "\n", 1) <= 0)
  124. goto end;
  125. if (len > length) {
  126. BIO_printf(bp, "length is greater than %ld\n", length);
  127. ret = 0;
  128. goto end;
  129. }
  130. if ((j == 0x21) && (len == 0)) {
  131. for (;;) {
  132. r = asn1_parse2(bp, &p, (long)(tot - p),
  133. offset + (p - *pp), depth + 1,
  134. indent, dump);
  135. if (r == 0) {
  136. ret = 0;
  137. goto end;
  138. }
  139. if ((r == 2) || (p >= tot)) {
  140. len = p - sp;
  141. break;
  142. }
  143. }
  144. } else {
  145. long tmp = len;
  146. while (p < ep) {
  147. sp = p;
  148. r = asn1_parse2(bp, &p, tmp,
  149. offset + (p - *pp), depth + 1,
  150. indent, dump);
  151. if (r == 0) {
  152. ret = 0;
  153. goto end;
  154. }
  155. tmp -= p - sp;
  156. }
  157. }
  158. } else if (xclass != 0) {
  159. p += len;
  160. if (BIO_write(bp, "\n", 1) <= 0)
  161. goto end;
  162. } else {
  163. nl = 0;
  164. if ((tag == V_ASN1_PRINTABLESTRING) ||
  165. (tag == V_ASN1_T61STRING) ||
  166. (tag == V_ASN1_IA5STRING) ||
  167. (tag == V_ASN1_VISIBLESTRING) ||
  168. (tag == V_ASN1_NUMERICSTRING) ||
  169. (tag == V_ASN1_UTF8STRING) ||
  170. (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
  171. if (BIO_write(bp, ":", 1) <= 0)
  172. goto end;
  173. if ((len > 0) && BIO_write(bp, (const char *)p, (int)len)
  174. != (int)len)
  175. goto end;
  176. } else if (tag == V_ASN1_OBJECT) {
  177. opp = op;
  178. if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
  179. if (BIO_write(bp, ":", 1) <= 0)
  180. goto end;
  181. i2a_ASN1_OBJECT(bp, o);
  182. } else {
  183. if (BIO_puts(bp, ":BAD OBJECT") <= 0)
  184. goto end;
  185. dump_cont = 1;
  186. }
  187. } else if (tag == V_ASN1_BOOLEAN) {
  188. if (len != 1) {
  189. if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
  190. goto end;
  191. dump_cont = 1;
  192. }
  193. if (len > 0)
  194. BIO_printf(bp, ":%u", p[0]);
  195. } else if (tag == V_ASN1_BMPSTRING) {
  196. /* do the BMP thang */
  197. } else if (tag == V_ASN1_OCTET_STRING) {
  198. int i, printable = 1;
  199. opp = op;
  200. os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
  201. if (os != NULL && os->length > 0) {
  202. opp = os->data;
  203. /*
  204. * testing whether the octet string is printable
  205. */
  206. for (i = 0; i < os->length; i++) {
  207. if (((opp[i] < ' ') &&
  208. (opp[i] != '\n') &&
  209. (opp[i] != '\r') &&
  210. (opp[i] != '\t')) || (opp[i] > '~')) {
  211. printable = 0;
  212. break;
  213. }
  214. }
  215. if (printable)
  216. /* printable string */
  217. {
  218. if (BIO_write(bp, ":", 1) <= 0)
  219. goto end;
  220. if (BIO_write(bp, (const char *)opp, os->length) <= 0)
  221. goto end;
  222. } else if (!dump)
  223. /*
  224. * not printable => print octet string as hex dump
  225. */
  226. {
  227. if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
  228. goto end;
  229. for (i = 0; i < os->length; i++) {
  230. if (BIO_printf(bp, "%02X", opp[i]) <= 0)
  231. goto end;
  232. }
  233. } else
  234. /* print the normal dump */
  235. {
  236. if (!nl) {
  237. if (BIO_write(bp, "\n", 1) <= 0)
  238. goto end;
  239. }
  240. if (BIO_dump_indent(bp,
  241. (const char *)opp,
  242. ((dump == -1 || dump >
  243. os->
  244. length) ? os->length : dump),
  245. dump_indent) <= 0)
  246. goto end;
  247. nl = 1;
  248. }
  249. }
  250. ASN1_OCTET_STRING_free(os);
  251. os = NULL;
  252. } else if (tag == V_ASN1_INTEGER) {
  253. int i;
  254. opp = op;
  255. ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
  256. if (ai != NULL) {
  257. if (BIO_write(bp, ":", 1) <= 0)
  258. goto end;
  259. if (ai->type == V_ASN1_NEG_INTEGER)
  260. if (BIO_write(bp, "-", 1) <= 0)
  261. goto end;
  262. for (i = 0; i < ai->length; i++) {
  263. if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
  264. goto end;
  265. }
  266. if (ai->length == 0) {
  267. if (BIO_write(bp, "00", 2) <= 0)
  268. goto end;
  269. }
  270. } else {
  271. if (BIO_puts(bp, ":BAD INTEGER") <= 0)
  272. goto end;
  273. dump_cont = 1;
  274. }
  275. ASN1_INTEGER_free(ai);
  276. ai = NULL;
  277. } else if (tag == V_ASN1_ENUMERATED) {
  278. int i;
  279. opp = op;
  280. ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
  281. if (ae != NULL) {
  282. if (BIO_write(bp, ":", 1) <= 0)
  283. goto end;
  284. if (ae->type == V_ASN1_NEG_ENUMERATED)
  285. if (BIO_write(bp, "-", 1) <= 0)
  286. goto end;
  287. for (i = 0; i < ae->length; i++) {
  288. if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
  289. goto end;
  290. }
  291. if (ae->length == 0) {
  292. if (BIO_write(bp, "00", 2) <= 0)
  293. goto end;
  294. }
  295. } else {
  296. if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
  297. goto end;
  298. dump_cont = 1;
  299. }
  300. ASN1_ENUMERATED_free(ae);
  301. ae = NULL;
  302. } else if (len > 0 && dump) {
  303. if (!nl) {
  304. if (BIO_write(bp, "\n", 1) <= 0)
  305. goto end;
  306. }
  307. if (BIO_dump_indent(bp, (const char *)p,
  308. ((dump == -1 || dump > len) ? len : dump),
  309. dump_indent) <= 0)
  310. goto end;
  311. nl = 1;
  312. }
  313. if (dump_cont) {
  314. int i;
  315. const unsigned char *tmp = op + hl;
  316. if (BIO_puts(bp, ":[") <= 0)
  317. goto end;
  318. for (i = 0; i < len; i++) {
  319. if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
  320. goto end;
  321. }
  322. if (BIO_puts(bp, "]") <= 0)
  323. goto end;
  324. dump_cont = 0;
  325. }
  326. if (!nl) {
  327. if (BIO_write(bp, "\n", 1) <= 0)
  328. goto end;
  329. }
  330. p += len;
  331. if ((tag == V_ASN1_EOC) && (xclass == 0)) {
  332. ret = 2; /* End of sequence */
  333. goto end;
  334. }
  335. }
  336. length -= len;
  337. }
  338. ret = 1;
  339. end:
  340. ASN1_OBJECT_free(o);
  341. ASN1_OCTET_STRING_free(os);
  342. ASN1_INTEGER_free(ai);
  343. ASN1_ENUMERATED_free(ae);
  344. *pp = p;
  345. return ret;
  346. }
  347. const char *ASN1_tag2str(int tag)
  348. {
  349. static const char *const tag2str[] = {
  350. /* 0-4 */
  351. "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
  352. /* 5-9 */
  353. "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
  354. /* 10-13 */
  355. "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
  356. /* 15-17 */
  357. "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
  358. /* 18-20 */
  359. "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
  360. /* 21-24 */
  361. "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
  362. /* 25-27 */
  363. "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
  364. /* 28-30 */
  365. "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
  366. };
  367. if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
  368. tag &= ~0x100;
  369. if (tag < 0 || tag > 30)
  370. return "(unknown)";
  371. return tag2str[tag];
  372. }