asn1_par.c 13 KB

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