2
0

asn1parse.c 10 KB

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