asn1_par.c 15 KB

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