asn1_par.c 13 KB

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