asn1pars.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright 1995-2018 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 <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 (strictpem) {
  161. if (PEM_read_bio(in, &name, &header, &str, &num) !=
  162. 1) {
  163. BIO_printf(bio_err, "Error reading PEM file\n");
  164. ERR_print_errors(bio_err);
  165. goto end;
  166. }
  167. } else {
  168. if ((buf = BUF_MEM_new()) == NULL)
  169. goto end;
  170. if (!BUF_MEM_grow(buf, BUFSIZ * 8))
  171. goto end; /* Pre-allocate :-) */
  172. if (genstr || genconf) {
  173. num = do_generate(genstr, genconf, buf);
  174. if (num < 0) {
  175. ERR_print_errors(bio_err);
  176. goto end;
  177. }
  178. } else {
  179. if (informat == FORMAT_PEM) {
  180. BIO *tmp;
  181. if ((b64 = BIO_new(BIO_f_base64())) == NULL)
  182. goto end;
  183. BIO_push(b64, in);
  184. tmp = in;
  185. in = b64;
  186. b64 = tmp;
  187. }
  188. num = 0;
  189. for (;;) {
  190. if (!BUF_MEM_grow(buf, num + BUFSIZ))
  191. goto end;
  192. i = BIO_read(in, &(buf->data[num]), BUFSIZ);
  193. if (i <= 0)
  194. break;
  195. num += i;
  196. }
  197. }
  198. str = (unsigned char *)buf->data;
  199. }
  200. /* If any structs to parse go through in sequence */
  201. if (sk_OPENSSL_STRING_num(osk)) {
  202. tmpbuf = str;
  203. tmplen = num;
  204. for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) {
  205. ASN1_TYPE *atmp;
  206. int typ;
  207. j = strtol(sk_OPENSSL_STRING_value(osk, i), NULL, 0);
  208. if (j <= 0 || j >= tmplen) {
  209. BIO_printf(bio_err, "'%s' is out of range\n",
  210. sk_OPENSSL_STRING_value(osk, i));
  211. continue;
  212. }
  213. tmpbuf += j;
  214. tmplen -= j;
  215. atmp = at;
  216. ctmpbuf = tmpbuf;
  217. at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen);
  218. ASN1_TYPE_free(atmp);
  219. if (!at) {
  220. BIO_printf(bio_err, "Error parsing structure\n");
  221. ERR_print_errors(bio_err);
  222. goto end;
  223. }
  224. typ = ASN1_TYPE_get(at);
  225. if ((typ == V_ASN1_OBJECT)
  226. || (typ == V_ASN1_BOOLEAN)
  227. || (typ == V_ASN1_NULL)) {
  228. BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ));
  229. ERR_print_errors(bio_err);
  230. goto end;
  231. }
  232. /* hmm... this is a little evil but it works */
  233. tmpbuf = at->value.asn1_string->data;
  234. tmplen = at->value.asn1_string->length;
  235. }
  236. str = tmpbuf;
  237. num = tmplen;
  238. }
  239. if (offset < 0 || offset >= num) {
  240. BIO_printf(bio_err, "Error: offset out of range\n");
  241. goto end;
  242. }
  243. num -= offset;
  244. if (length == 0 || length > (unsigned int)num)
  245. length = (unsigned int)num;
  246. if (derout != NULL) {
  247. if (BIO_write(derout, str + offset, length) != (int)length) {
  248. BIO_printf(bio_err, "Error writing output\n");
  249. ERR_print_errors(bio_err);
  250. goto end;
  251. }
  252. }
  253. if (!noout) {
  254. const unsigned char *p = str + offset;
  255. if (it != NULL) {
  256. ASN1_VALUE *value = ASN1_item_d2i(NULL, &p, length, it);
  257. if (value == NULL) {
  258. BIO_printf(bio_err, "Error parsing item %s\n", it->sname);
  259. ERR_print_errors(bio_err);
  260. goto end;
  261. }
  262. ASN1_item_print(bio_out, value, 0, it, NULL);
  263. ASN1_item_free(value, it);
  264. } else {
  265. if (!ASN1_parse_dump(bio_out, p, length, indent, dump)) {
  266. ERR_print_errors(bio_err);
  267. goto end;
  268. }
  269. }
  270. }
  271. ret = 0;
  272. end:
  273. BIO_free(derout);
  274. BIO_free(in);
  275. BIO_free(b64);
  276. if (ret != 0)
  277. ERR_print_errors(bio_err);
  278. BUF_MEM_free(buf);
  279. OPENSSL_free(name);
  280. OPENSSL_free(header);
  281. if (strictpem)
  282. OPENSSL_free(str);
  283. ASN1_TYPE_free(at);
  284. sk_OPENSSL_STRING_free(osk);
  285. return ret;
  286. }
  287. static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf)
  288. {
  289. CONF *cnf = NULL;
  290. int len;
  291. unsigned char *p;
  292. ASN1_TYPE *atyp = NULL;
  293. if (genconf != NULL) {
  294. if ((cnf = app_load_config(genconf)) == NULL)
  295. goto err;
  296. if (genstr == NULL)
  297. genstr = NCONF_get_string(cnf, "default", "asn1");
  298. if (genstr == NULL) {
  299. BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf);
  300. goto err;
  301. }
  302. }
  303. atyp = ASN1_generate_nconf(genstr, cnf);
  304. NCONF_free(cnf);
  305. cnf = NULL;
  306. if (atyp == NULL)
  307. return -1;
  308. len = i2d_ASN1_TYPE(atyp, NULL);
  309. if (len <= 0)
  310. goto err;
  311. if (!BUF_MEM_grow(buf, len))
  312. goto err;
  313. p = (unsigned char *)buf->data;
  314. i2d_ASN1_TYPE(atyp, &p);
  315. ASN1_TYPE_free(atyp);
  316. return len;
  317. err:
  318. NCONF_free(cnf);
  319. ASN1_TYPE_free(atyp);
  320. return -1;
  321. }