asn1_par.c 13 KB

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