asn1pars.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /*
  58. * A nice addition from Dr Stephen Henson <steve@openssl.org> to add the
  59. * -strparse option which parses nested binary structures
  60. */
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include "apps.h"
  65. #include <openssl/err.h>
  66. #include <openssl/evp.h>
  67. #include <openssl/x509.h>
  68. #include <openssl/pem.h>
  69. typedef enum OPTION_choice {
  70. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  71. OPT_INFORM, OPT_IN, OPT_OUT, OPT_INDENT, OPT_NOOUT,
  72. OPT_OID, OPT_OFFSET, OPT_LENGTH, OPT_DUMP, OPT_DLIMIT,
  73. OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM
  74. } OPTION_CHOICE;
  75. OPTIONS asn1parse_options[] = {
  76. {"help", OPT_HELP, '-', "Display this summary"},
  77. {"inform", OPT_INFORM, 'F', "input format - one of DER PEM"},
  78. {"in", OPT_IN, '<', "input file"},
  79. {"out", OPT_OUT, '>', "output file (output format is always DER)"},
  80. {"i", OPT_INDENT, 0, "indents the output"},
  81. {"noout", OPT_NOOUT, 0, "don't produce any output"},
  82. {"offset", OPT_OFFSET, 'p', "offset into file"},
  83. {"length", OPT_LENGTH, 'p', "length of section in file"},
  84. {"oid", OPT_OID, '<', "file of extra oid definitions"},
  85. {"dump", OPT_DUMP, 0, "unknown data in hex form"},
  86. {"dlimit", OPT_DLIMIT, 'p',
  87. "dump the first arg bytes of unknown data in hex form"},
  88. {"strparse", OPT_STRPARSE, 's',
  89. "offset; a series of these can be used to 'dig'"},
  90. {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"},
  91. {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"},
  92. {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"},
  93. {OPT_MORE_STR, 0, 0, "(-inform will be ignored)"},
  94. {"strictpem", OPT_STRICTPEM, 0,
  95. "do not attempt base64 decode outside PEM markers"},
  96. {NULL}
  97. };
  98. static int do_generate(char *genstr, char *genconf, BUF_MEM *buf);
  99. int asn1parse_main(int argc, char **argv)
  100. {
  101. ASN1_TYPE *at = NULL;
  102. BIO *in = NULL, *b64 = NULL, *derout = NULL;
  103. BUF_MEM *buf = NULL;
  104. STACK_OF(OPENSSL_STRING) *osk = NULL;
  105. char *genstr = NULL, *genconf = NULL;
  106. char *infile = NULL, *str = NULL, *oidfile = NULL, *derfile = NULL;
  107. char *name = NULL, *header = NULL, *prog;
  108. const unsigned char *ctmpbuf;
  109. int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM;
  110. int offset = 0, ret = 1, i, j;
  111. long num, tmplen;
  112. unsigned char *tmpbuf;
  113. unsigned int length = 0;
  114. OPTION_CHOICE o;
  115. prog = opt_init(argc, argv, asn1parse_options);
  116. if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) {
  117. BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
  118. goto end;
  119. }
  120. while ((o = opt_next()) != OPT_EOF) {
  121. switch (o) {
  122. case OPT_EOF:
  123. case OPT_ERR:
  124. opthelp:
  125. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  126. goto end;
  127. case OPT_HELP:
  128. opt_help(asn1parse_options);
  129. ret = 0;
  130. goto end;
  131. case OPT_INFORM:
  132. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  133. goto opthelp;
  134. break;
  135. case OPT_IN:
  136. infile = opt_arg();
  137. break;
  138. case OPT_OUT:
  139. derfile = opt_arg();
  140. break;
  141. case OPT_INDENT:
  142. indent = 1;
  143. break;
  144. case OPT_NOOUT:
  145. noout = 1;
  146. break;
  147. case OPT_OID:
  148. oidfile = opt_arg();
  149. break;
  150. case OPT_OFFSET:
  151. offset = strtol(opt_arg(), NULL, 0);
  152. break;
  153. case OPT_LENGTH:
  154. length = atoi(opt_arg());
  155. break;
  156. case OPT_DUMP:
  157. dump = -1;
  158. break;
  159. case OPT_DLIMIT:
  160. dump = atoi(opt_arg());
  161. break;
  162. case OPT_STRPARSE:
  163. sk_OPENSSL_STRING_push(osk, opt_arg());
  164. break;
  165. case OPT_GENSTR:
  166. genstr = opt_arg();
  167. break;
  168. case OPT_GENCONF:
  169. genconf = opt_arg();
  170. break;
  171. case OPT_STRICTPEM:
  172. strictpem = 1;
  173. informat = FORMAT_PEM;
  174. break;
  175. }
  176. }
  177. argc = opt_num_rest();
  178. argv = opt_rest();
  179. if (oidfile != NULL) {
  180. in = bio_open_default(oidfile, 'r', FORMAT_TEXT);
  181. if (in == NULL)
  182. goto end;
  183. OBJ_create_objects(in);
  184. BIO_free(in);
  185. }
  186. if ((in = bio_open_default(infile, 'r', informat)) == NULL)
  187. goto end;
  188. if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL)
  189. goto end;
  190. if (strictpem) {
  191. if (PEM_read_bio(in, &name, &header, (unsigned char **)&str, &num) !=
  192. 1) {
  193. BIO_printf(bio_err, "Error reading PEM file\n");
  194. ERR_print_errors(bio_err);
  195. goto end;
  196. }
  197. } else {
  198. if ((buf = BUF_MEM_new()) == NULL)
  199. goto end;
  200. if (!BUF_MEM_grow(buf, BUFSIZ * 8))
  201. goto end; /* Pre-allocate :-) */
  202. if (genstr || genconf) {
  203. num = do_generate(genstr, genconf, buf);
  204. if (num < 0) {
  205. ERR_print_errors(bio_err);
  206. goto end;
  207. }
  208. }
  209. else {
  210. if (informat == FORMAT_PEM) {
  211. BIO *tmp;
  212. if ((b64 = BIO_new(BIO_f_base64())) == NULL)
  213. goto end;
  214. BIO_push(b64, in);
  215. tmp = in;
  216. in = b64;
  217. b64 = tmp;
  218. }
  219. num = 0;
  220. for (;;) {
  221. if (!BUF_MEM_grow(buf, (int)num + BUFSIZ))
  222. goto end;
  223. i = BIO_read(in, &(buf->data[num]), BUFSIZ);
  224. if (i <= 0)
  225. break;
  226. num += i;
  227. }
  228. }
  229. str = buf->data;
  230. }
  231. /* If any structs to parse go through in sequence */
  232. if (sk_OPENSSL_STRING_num(osk)) {
  233. tmpbuf = (unsigned char *)str;
  234. tmplen = num;
  235. for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) {
  236. ASN1_TYPE *atmp;
  237. int typ;
  238. j = atoi(sk_OPENSSL_STRING_value(osk, i));
  239. if (j == 0) {
  240. BIO_printf(bio_err, "'%s' is an invalid number\n",
  241. sk_OPENSSL_STRING_value(osk, i));
  242. continue;
  243. }
  244. tmpbuf += j;
  245. tmplen -= j;
  246. atmp = at;
  247. ctmpbuf = tmpbuf;
  248. at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen);
  249. ASN1_TYPE_free(atmp);
  250. if (!at) {
  251. BIO_printf(bio_err, "Error parsing structure\n");
  252. ERR_print_errors(bio_err);
  253. goto end;
  254. }
  255. typ = ASN1_TYPE_get(at);
  256. if ((typ == V_ASN1_OBJECT)
  257. || (typ == V_ASN1_BOOLEAN)
  258. || (typ == V_ASN1_NULL)) {
  259. BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ));
  260. ERR_print_errors(bio_err);
  261. goto end;
  262. }
  263. /* hmm... this is a little evil but it works */
  264. tmpbuf = at->value.asn1_string->data;
  265. tmplen = at->value.asn1_string->length;
  266. }
  267. str = (char *)tmpbuf;
  268. num = tmplen;
  269. }
  270. if (offset >= num) {
  271. BIO_printf(bio_err, "Error: offset too large\n");
  272. goto end;
  273. }
  274. num -= offset;
  275. if ((length == 0) || ((long)length > num))
  276. length = (unsigned int)num;
  277. if (derout) {
  278. if (BIO_write(derout, str + offset, length) != (int)length) {
  279. BIO_printf(bio_err, "Error writing output\n");
  280. ERR_print_errors(bio_err);
  281. goto end;
  282. }
  283. }
  284. if (!noout &&
  285. !ASN1_parse_dump(bio_out, (unsigned char *)&(str[offset]), length,
  286. indent, dump)) {
  287. ERR_print_errors(bio_err);
  288. goto end;
  289. }
  290. ret = 0;
  291. end:
  292. BIO_free(derout);
  293. BIO_free(in);
  294. BIO_free(b64);
  295. if (ret != 0)
  296. ERR_print_errors(bio_err);
  297. BUF_MEM_free(buf);
  298. OPENSSL_free(name);
  299. OPENSSL_free(header);
  300. if (strictpem)
  301. OPENSSL_free(str);
  302. ASN1_TYPE_free(at);
  303. sk_OPENSSL_STRING_free(osk);
  304. OBJ_cleanup();
  305. return (ret);
  306. }
  307. static int do_generate(char *genstr, char *genconf, BUF_MEM *buf)
  308. {
  309. CONF *cnf = NULL;
  310. int len;
  311. unsigned char *p;
  312. ASN1_TYPE *atyp = NULL;
  313. if (genconf) {
  314. if ((cnf = app_load_config(genconf)) == NULL)
  315. goto err;
  316. if (!genstr)
  317. genstr = NCONF_get_string(cnf, "default", "asn1");
  318. if (!genstr) {
  319. BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf);
  320. goto err;
  321. }
  322. }
  323. atyp = ASN1_generate_nconf(genstr, cnf);
  324. NCONF_free(cnf);
  325. cnf = NULL;
  326. if (!atyp)
  327. return -1;
  328. len = i2d_ASN1_TYPE(atyp, NULL);
  329. if (len <= 0)
  330. goto err;
  331. if (!BUF_MEM_grow(buf, len))
  332. goto err;
  333. p = (unsigned char *)buf->data;
  334. i2d_ASN1_TYPE(atyp, &p);
  335. ASN1_TYPE_free(atyp);
  336. return len;
  337. err:
  338. NCONF_free(cnf);
  339. ASN1_TYPE_free(atyp);
  340. return -1;
  341. }