2
0

asn1pars.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright 1995-2019 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 <stdlib.h>
  11. #include <string.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/err.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/asn1t.h>
  19. typedef enum OPTION_choice {
  20. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  21. OPT_INFORM, OPT_IN, OPT_OUT, OPT_INDENT, OPT_NOOUT,
  22. OPT_OID, OPT_OFFSET, OPT_LENGTH, OPT_DUMP, OPT_DLIMIT,
  23. OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM,
  24. OPT_ITEM
  25. } OPTION_CHOICE;
  26. const OPTIONS asn1parse_options[] = {
  27. {"help", OPT_HELP, '-', "Display this summary"},
  28. {"inform", OPT_INFORM, 'F', "input format - one of DER PEM"},
  29. {"in", OPT_IN, '<', "input file"},
  30. {"out", OPT_OUT, '>', "output file (output format is always DER)"},
  31. {"i", OPT_INDENT, 0, "indents the output"},
  32. {"noout", OPT_NOOUT, 0, "do not produce any output"},
  33. {"offset", OPT_OFFSET, 'p', "offset into file"},
  34. {"length", OPT_LENGTH, 'p', "length of section in file"},
  35. {"oid", OPT_OID, '<', "file of extra oid definitions"},
  36. {"dump", OPT_DUMP, 0, "unknown data in hex form"},
  37. {"dlimit", OPT_DLIMIT, 'p',
  38. "dump the first arg bytes of unknown data in hex form"},
  39. {"strparse", OPT_STRPARSE, 'p',
  40. "offset; a series of these can be used to 'dig'"},
  41. {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"},
  42. {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"},
  43. {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"},
  44. {OPT_MORE_STR, 0, 0, "(-inform will be ignored)"},
  45. {"strictpem", OPT_STRICTPEM, 0,
  46. "do not attempt base64 decode outside PEM markers"},
  47. {"item", OPT_ITEM, 's', "item to parse and print"},
  48. {NULL}
  49. };
  50. static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf);
  51. int asn1parse_main(int argc, char **argv)
  52. {
  53. ASN1_TYPE *at = NULL;
  54. BIO *in = NULL, *b64 = NULL, *derout = NULL;
  55. BUF_MEM *buf = NULL;
  56. STACK_OF(OPENSSL_STRING) *osk = NULL;
  57. char *genstr = NULL, *genconf = NULL;
  58. char *infile = NULL, *oidfile = NULL, *derfile = NULL;
  59. unsigned char *str = NULL;
  60. char *name = NULL, *header = NULL, *prog;
  61. const unsigned char *ctmpbuf;
  62. int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM;
  63. int offset = 0, ret = 1, i, j;
  64. long num, tmplen;
  65. unsigned char *tmpbuf;
  66. unsigned int length = 0;
  67. OPTION_CHOICE o;
  68. const ASN1_ITEM *it = NULL;
  69. prog = opt_init(argc, argv, asn1parse_options);
  70. if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) {
  71. BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
  72. goto end;
  73. }
  74. while ((o = opt_next()) != OPT_EOF) {
  75. switch (o) {
  76. case OPT_EOF:
  77. case OPT_ERR:
  78. opthelp:
  79. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  80. goto end;
  81. case OPT_HELP:
  82. opt_help(asn1parse_options);
  83. ret = 0;
  84. goto end;
  85. case OPT_INFORM:
  86. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  87. goto opthelp;
  88. break;
  89. case OPT_IN:
  90. infile = opt_arg();
  91. break;
  92. case OPT_OUT:
  93. derfile = opt_arg();
  94. break;
  95. case OPT_INDENT:
  96. indent = 1;
  97. break;
  98. case OPT_NOOUT:
  99. noout = 1;
  100. break;
  101. case OPT_OID:
  102. oidfile = opt_arg();
  103. break;
  104. case OPT_OFFSET:
  105. offset = strtol(opt_arg(), NULL, 0);
  106. break;
  107. case OPT_LENGTH:
  108. length = strtol(opt_arg(), NULL, 0);
  109. break;
  110. case OPT_DUMP:
  111. dump = -1;
  112. break;
  113. case OPT_DLIMIT:
  114. dump = strtol(opt_arg(), NULL, 0);
  115. break;
  116. case OPT_STRPARSE:
  117. sk_OPENSSL_STRING_push(osk, opt_arg());
  118. break;
  119. case OPT_GENSTR:
  120. genstr = opt_arg();
  121. break;
  122. case OPT_GENCONF:
  123. genconf = opt_arg();
  124. break;
  125. case OPT_STRICTPEM:
  126. strictpem = 1;
  127. informat = FORMAT_PEM;
  128. break;
  129. case OPT_ITEM:
  130. it = ASN1_ITEM_lookup(opt_arg());
  131. if (it == NULL) {
  132. size_t tmp;
  133. BIO_printf(bio_err, "Unknown item name %s\n", opt_arg());
  134. BIO_puts(bio_err, "Supported types:\n");
  135. for (tmp = 0;; tmp++) {
  136. it = ASN1_ITEM_get(tmp);
  137. if (it == NULL)
  138. break;
  139. BIO_printf(bio_err, " %s\n", it->sname);
  140. }
  141. goto end;
  142. }
  143. break;
  144. }
  145. }
  146. argc = opt_num_rest();
  147. if (argc != 0)
  148. goto opthelp;
  149. if (oidfile != NULL) {
  150. in = bio_open_default(oidfile, 'r', FORMAT_TEXT);
  151. if (in == NULL)
  152. goto end;
  153. OBJ_create_objects(in);
  154. BIO_free(in);
  155. }
  156. if ((in = bio_open_default(infile, 'r', informat)) == NULL)
  157. goto end;
  158. if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL)
  159. goto end;
  160. if ((buf = BUF_MEM_new()) == NULL)
  161. goto end;
  162. if (strictpem) {
  163. if (PEM_read_bio(in, &name, &header, &str, &num) != 1) {
  164. BIO_printf(bio_err, "Error reading PEM file\n");
  165. ERR_print_errors(bio_err);
  166. goto end;
  167. }
  168. buf->data = (char *)str;
  169. buf->length = buf->max = num;
  170. } else {
  171. if (!BUF_MEM_grow(buf, BUFSIZ * 8))
  172. goto end; /* Pre-allocate :-) */
  173. if (genstr || genconf) {
  174. num = do_generate(genstr, genconf, buf);
  175. if (num < 0) {
  176. ERR_print_errors(bio_err);
  177. goto end;
  178. }
  179. } else {
  180. if (informat == FORMAT_PEM) {
  181. BIO *tmp;
  182. if ((b64 = BIO_new(BIO_f_base64())) == NULL)
  183. goto end;
  184. BIO_push(b64, in);
  185. tmp = in;
  186. in = b64;
  187. b64 = tmp;
  188. }
  189. num = 0;
  190. for (;;) {
  191. if (!BUF_MEM_grow(buf, num + BUFSIZ))
  192. goto end;
  193. i = BIO_read(in, &(buf->data[num]), BUFSIZ);
  194. if (i <= 0)
  195. break;
  196. num += i;
  197. }
  198. }
  199. str = (unsigned char *)buf->data;
  200. }
  201. /* If any structs to parse go through in sequence */
  202. if (sk_OPENSSL_STRING_num(osk)) {
  203. tmpbuf = str;
  204. tmplen = num;
  205. for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) {
  206. ASN1_TYPE *atmp;
  207. int typ;
  208. j = strtol(sk_OPENSSL_STRING_value(osk, i), NULL, 0);
  209. if (j <= 0 || j >= tmplen) {
  210. BIO_printf(bio_err, "'%s' is out of range\n",
  211. sk_OPENSSL_STRING_value(osk, i));
  212. continue;
  213. }
  214. tmpbuf += j;
  215. tmplen -= j;
  216. atmp = at;
  217. ctmpbuf = tmpbuf;
  218. at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen);
  219. ASN1_TYPE_free(atmp);
  220. if (!at) {
  221. BIO_printf(bio_err, "Error parsing structure\n");
  222. ERR_print_errors(bio_err);
  223. goto end;
  224. }
  225. typ = ASN1_TYPE_get(at);
  226. if ((typ == V_ASN1_OBJECT)
  227. || (typ == V_ASN1_BOOLEAN)
  228. || (typ == V_ASN1_NULL)) {
  229. BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ));
  230. ERR_print_errors(bio_err);
  231. goto end;
  232. }
  233. /* hmm... this is a little evil but it works */
  234. tmpbuf = at->value.asn1_string->data;
  235. tmplen = at->value.asn1_string->length;
  236. }
  237. str = tmpbuf;
  238. num = tmplen;
  239. }
  240. if (offset < 0 || offset >= num) {
  241. BIO_printf(bio_err, "Error: offset out of range\n");
  242. goto end;
  243. }
  244. num -= offset;
  245. if (length == 0 || length > (unsigned int)num)
  246. length = (unsigned int)num;
  247. if (derout != NULL) {
  248. if (BIO_write(derout, str + offset, length) != (int)length) {
  249. BIO_printf(bio_err, "Error writing output\n");
  250. ERR_print_errors(bio_err);
  251. goto end;
  252. }
  253. }
  254. if (!noout) {
  255. const unsigned char *p = str + offset;
  256. if (it != NULL) {
  257. ASN1_VALUE *value = ASN1_item_d2i(NULL, &p, length, it);
  258. if (value == NULL) {
  259. BIO_printf(bio_err, "Error parsing item %s\n", it->sname);
  260. ERR_print_errors(bio_err);
  261. goto end;
  262. }
  263. ASN1_item_print(bio_out, value, 0, it, NULL);
  264. ASN1_item_free(value, it);
  265. } else {
  266. if (!ASN1_parse_dump(bio_out, p, length, indent, dump)) {
  267. ERR_print_errors(bio_err);
  268. goto end;
  269. }
  270. }
  271. }
  272. ret = 0;
  273. end:
  274. BIO_free(derout);
  275. BIO_free(in);
  276. BIO_free(b64);
  277. if (ret != 0)
  278. ERR_print_errors(bio_err);
  279. BUF_MEM_free(buf);
  280. OPENSSL_free(name);
  281. OPENSSL_free(header);
  282. ASN1_TYPE_free(at);
  283. sk_OPENSSL_STRING_free(osk);
  284. return ret;
  285. }
  286. static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf)
  287. {
  288. CONF *cnf = NULL;
  289. int len;
  290. unsigned char *p;
  291. ASN1_TYPE *atyp = NULL;
  292. if (genconf != NULL) {
  293. if ((cnf = app_load_config(genconf)) == NULL)
  294. goto err;
  295. if (genstr == NULL)
  296. genstr = NCONF_get_string(cnf, "default", "asn1");
  297. if (genstr == NULL) {
  298. BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf);
  299. goto err;
  300. }
  301. }
  302. atyp = ASN1_generate_nconf(genstr, cnf);
  303. NCONF_free(cnf);
  304. cnf = NULL;
  305. if (atyp == NULL)
  306. return -1;
  307. len = i2d_ASN1_TYPE(atyp, NULL);
  308. if (len <= 0)
  309. goto err;
  310. if (!BUF_MEM_grow(buf, len))
  311. goto err;
  312. p = (unsigned char *)buf->data;
  313. i2d_ASN1_TYPE(atyp, &p);
  314. ASN1_TYPE_free(atyp);
  315. return len;
  316. err:
  317. NCONF_free(cnf);
  318. ASN1_TYPE_free(atyp);
  319. return -1;
  320. }