asn1_par.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <openssl/bio.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #define ASN1_PARSE_MAXDEPTH 128
  61. static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
  62. int indent);
  63. static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
  64. int offset, int depth, int indent, int dump);
  65. static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
  66. int indent)
  67. {
  68. static const char fmt[] = "%-18s";
  69. char str[128];
  70. const char *p;
  71. if (constructed & V_ASN1_CONSTRUCTED)
  72. p = "cons: ";
  73. else
  74. p = "prim: ";
  75. if (BIO_write(bp, p, 6) < 6)
  76. goto err;
  77. BIO_indent(bp, indent, 128);
  78. p = str;
  79. if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
  80. BIO_snprintf(str, sizeof str, "priv [ %d ] ", tag);
  81. else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
  82. BIO_snprintf(str, sizeof str, "cont [ %d ]", tag);
  83. else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
  84. BIO_snprintf(str, sizeof str, "appl [ %d ]", tag);
  85. else if (tag > 30)
  86. BIO_snprintf(str, sizeof str, "<ASN1 %d>", tag);
  87. else
  88. p = ASN1_tag2str(tag);
  89. if (BIO_printf(bp, fmt, p) <= 0)
  90. goto err;
  91. return (1);
  92. err:
  93. return (0);
  94. }
  95. int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
  96. {
  97. return (asn1_parse2(bp, &pp, len, 0, 0, indent, 0));
  98. }
  99. int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
  100. int dump)
  101. {
  102. return (asn1_parse2(bp, &pp, len, 0, 0, indent, dump));
  103. }
  104. static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
  105. int offset, int depth, int indent, int dump)
  106. {
  107. const unsigned char *p, *ep, *tot, *op, *opp;
  108. long len;
  109. int tag, xclass, ret = 0;
  110. int nl, hl, j, r;
  111. ASN1_OBJECT *o = NULL;
  112. ASN1_OCTET_STRING *os = NULL;
  113. /* ASN1_BMPSTRING *bmp=NULL; */
  114. int dump_indent;
  115. #if 0
  116. dump_indent = indent;
  117. #else
  118. dump_indent = 6; /* Because we know BIO_dump_indent() */
  119. #endif
  120. if (depth > ASN1_PARSE_MAXDEPTH) {
  121. BIO_puts(bp, "BAD RECURSION DEPTH\n");
  122. return 0;
  123. }
  124. p = *pp;
  125. tot = p + length;
  126. op = p - 1;
  127. while ((p < tot) && (op < p)) {
  128. op = p;
  129. j = ASN1_get_object(&p, &len, &tag, &xclass, length);
  130. #ifdef LINT
  131. j = j;
  132. #endif
  133. if (j & 0x80) {
  134. if (BIO_puts(bp, "Error in encoding\n") <= 0)
  135. goto end;
  136. ret = 0;
  137. goto end;
  138. }
  139. hl = (p - op);
  140. length -= hl;
  141. /*
  142. * if j == 0x21 it is a constructed indefinite length object
  143. */
  144. if (BIO_printf(bp, "%5ld:", (long)offset + (long)(op - *pp))
  145. <= 0)
  146. goto end;
  147. if (j != (V_ASN1_CONSTRUCTED | 1)) {
  148. if (BIO_printf(bp, "d=%-2d hl=%ld l=%4ld ",
  149. depth, (long)hl, len) <= 0)
  150. goto end;
  151. } else {
  152. if (BIO_printf(bp, "d=%-2d hl=%ld l=inf ", depth, (long)hl) <= 0)
  153. goto end;
  154. }
  155. if (!asn1_print_info(bp, tag, xclass, j, (indent) ? depth : 0))
  156. goto end;
  157. if (j & V_ASN1_CONSTRUCTED) {
  158. ep = p + len;
  159. if (BIO_puts(bp, "\n") <= 0)
  160. goto end;
  161. if (len > length) {
  162. BIO_printf(bp, "length is greater than %ld\n", length);
  163. ret = 0;
  164. goto end;
  165. }
  166. if ((j == 0x21) && (len == 0)) {
  167. for (;;) {
  168. r = asn1_parse2(bp, &p, (long)(tot - p),
  169. offset + (p - *pp), depth + 1,
  170. indent, dump);
  171. if (r == 0) {
  172. ret = 0;
  173. goto end;
  174. }
  175. if ((r == 2) || (p >= tot))
  176. break;
  177. }
  178. } else
  179. while (p < ep) {
  180. r = asn1_parse2(bp, &p, (long)len,
  181. offset + (p - *pp), depth + 1,
  182. indent, dump);
  183. if (r == 0) {
  184. ret = 0;
  185. goto end;
  186. }
  187. }
  188. } else if (xclass != 0) {
  189. p += len;
  190. if (BIO_puts(bp, "\n") <= 0)
  191. goto end;
  192. } else {
  193. nl = 0;
  194. if ((tag == V_ASN1_PRINTABLESTRING) ||
  195. (tag == V_ASN1_T61STRING) ||
  196. (tag == V_ASN1_IA5STRING) ||
  197. (tag == V_ASN1_VISIBLESTRING) ||
  198. (tag == V_ASN1_NUMERICSTRING) ||
  199. (tag == V_ASN1_UTF8STRING) ||
  200. (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
  201. if (BIO_puts(bp, ":") <= 0)
  202. goto end;
  203. if ((len > 0) && BIO_write(bp, (const char *)p, (int)len)
  204. != (int)len)
  205. goto end;
  206. } else if (tag == V_ASN1_OBJECT) {
  207. opp = op;
  208. if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
  209. if (BIO_puts(bp, ":") <= 0)
  210. goto end;
  211. i2a_ASN1_OBJECT(bp, o);
  212. } else {
  213. if (BIO_puts(bp, ":BAD OBJECT") <= 0)
  214. goto end;
  215. }
  216. } else if (tag == V_ASN1_BOOLEAN) {
  217. int ii;
  218. opp = op;
  219. ii = d2i_ASN1_BOOLEAN(NULL, &opp, len + hl);
  220. if (ii < 0) {
  221. if (BIO_puts(bp, "Bad boolean\n") <= 0)
  222. goto end;
  223. }
  224. BIO_printf(bp, ":%d", ii);
  225. } else if (tag == V_ASN1_BMPSTRING) {
  226. /* do the BMP thang */
  227. } else if (tag == V_ASN1_OCTET_STRING) {
  228. int i, printable = 1;
  229. opp = op;
  230. os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
  231. if (os != NULL && os->length > 0) {
  232. opp = os->data;
  233. /*
  234. * testing whether the octet string is printable
  235. */
  236. for (i = 0; i < os->length; i++) {
  237. if (((opp[i] < ' ') &&
  238. (opp[i] != '\n') &&
  239. (opp[i] != '\r') &&
  240. (opp[i] != '\t')) || (opp[i] > '~')) {
  241. printable = 0;
  242. break;
  243. }
  244. }
  245. if (printable)
  246. /* printable string */
  247. {
  248. if (BIO_puts(bp, ":") <= 0)
  249. goto end;
  250. if (BIO_write(bp, (const char *)opp, os->length) <= 0)
  251. goto end;
  252. } else if (!dump)
  253. /*
  254. * not printable => print octet string as hex dump
  255. */
  256. {
  257. if (BIO_puts(bp, "[HEX DUMP]:") <= 0)
  258. goto end;
  259. for (i = 0; i < os->length; i++) {
  260. if (BIO_printf(bp, "%02X", opp[i]) <= 0)
  261. goto end;
  262. }
  263. } else
  264. /* print the normal dump */
  265. {
  266. if (!nl) {
  267. if (BIO_puts(bp, "\n") <= 0)
  268. goto end;
  269. }
  270. if (!BIO_hexdump(bp, opp,
  271. ((dump == -1 || dump >
  272. os->length) ? os->length : dump),
  273. dump_indent))
  274. goto end;
  275. nl = 1;
  276. }
  277. }
  278. if (os != NULL) {
  279. M_ASN1_OCTET_STRING_free(os);
  280. os = NULL;
  281. }
  282. } else if (tag == V_ASN1_INTEGER) {
  283. ASN1_INTEGER *bs;
  284. int i;
  285. opp = op;
  286. bs = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
  287. if (bs != NULL) {
  288. if (BIO_puts(bp, ":") <= 0)
  289. goto end;
  290. if (bs->type == V_ASN1_NEG_INTEGER)
  291. if (BIO_puts(bp, "-") <= 0)
  292. goto end;
  293. for (i = 0; i < bs->length; i++) {
  294. if (BIO_printf(bp, "%02X", bs->data[i]) <= 0)
  295. goto end;
  296. }
  297. if (bs->length == 0) {
  298. if (BIO_puts(bp, "00") <= 0)
  299. goto end;
  300. }
  301. } else {
  302. if (BIO_puts(bp, "BAD INTEGER") <= 0)
  303. goto end;
  304. }
  305. M_ASN1_INTEGER_free(bs);
  306. } else if (tag == V_ASN1_ENUMERATED) {
  307. ASN1_ENUMERATED *bs;
  308. int i;
  309. opp = op;
  310. bs = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
  311. if (bs != NULL) {
  312. if (BIO_puts(bp, ":") <= 0)
  313. goto end;
  314. if (bs->type == V_ASN1_NEG_ENUMERATED)
  315. if (BIO_puts(bp, "-") <= 0)
  316. goto end;
  317. for (i = 0; i < bs->length; i++) {
  318. if (BIO_printf(bp, "%02X", bs->data[i]) <= 0)
  319. goto end;
  320. }
  321. if (bs->length == 0) {
  322. if (BIO_puts(bp, "00") <= 0)
  323. goto end;
  324. }
  325. } else {
  326. if (BIO_puts(bp, "BAD ENUMERATED") <= 0)
  327. goto end;
  328. }
  329. M_ASN1_ENUMERATED_free(bs);
  330. } else if (len > 0 && dump) {
  331. if (!nl) {
  332. if (BIO_puts(bp, "\n") <= 0)
  333. goto end;
  334. }
  335. if (!BIO_hexdump(bp, p,
  336. ((dump == -1 || dump > len) ? len : dump),
  337. dump_indent))
  338. goto end;
  339. nl = 1;
  340. }
  341. if (!nl) {
  342. if (BIO_puts(bp, "\n") <= 0)
  343. goto end;
  344. }
  345. p += len;
  346. if ((tag == V_ASN1_EOC) && (xclass == 0)) {
  347. ret = 2; /* End of sequence */
  348. goto end;
  349. }
  350. }
  351. length -= len;
  352. }
  353. ret = 1;
  354. end:
  355. if (o != NULL)
  356. ASN1_OBJECT_free(o);
  357. if (os != NULL)
  358. M_ASN1_OCTET_STRING_free(os);
  359. *pp = p;
  360. return (ret);
  361. }
  362. const char *ASN1_tag2str(int tag)
  363. {
  364. static const char *const tag2str[] = {
  365. "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", /* 0-4 */
  366. "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", /* 5-9 */
  367. "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>", /* 10-13 */
  368. "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET", /* 15-17 */
  369. "NUMERICSTRING", "PRINTABLESTRING", "T61STRING", /* 18-20 */
  370. "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME", /* 21-24
  371. */
  372. "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", /* 25-27 */
  373. "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING" /* 28-30 */
  374. };
  375. if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
  376. tag &= ~0x100;
  377. if (tag < 0 || tag > 30)
  378. return "(unknown)";
  379. return tag2str[tag];
  380. }