asn1parse.c 10 KB

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