asn1pars.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* apps/asn1pars.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* A nice addition from Dr Stephen Henson <steve@openssl.org> to
  59. * add the -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. /* -inform arg - input format - default PEM (DER or PEM)
  70. * -in arg - input file - default stdin
  71. * -i - indent the details by depth
  72. * -offset - where in the file to start
  73. * -length - how many bytes to use
  74. * -oid file - extra oid description file
  75. */
  76. #undef PROG
  77. #define PROG asn1parse_main
  78. int MAIN(int, char **);
  79. static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf);
  80. int MAIN(int argc, char **argv)
  81. {
  82. int i,badops=0,offset=0,ret=1,j;
  83. unsigned int length=0;
  84. long num,tmplen;
  85. BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
  86. int informat,indent=0, noout = 0, dump = 0;
  87. char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL;
  88. char *genstr=NULL, *genconf=NULL;
  89. unsigned char *tmpbuf;
  90. const unsigned char *ctmpbuf;
  91. BUF_MEM *buf=NULL;
  92. STACK_OF(OPENSSL_STRING) *osk=NULL;
  93. ASN1_TYPE *at=NULL;
  94. informat=FORMAT_PEM;
  95. apps_startup();
  96. if (bio_err == NULL)
  97. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  98. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  99. if (!load_config(bio_err, NULL))
  100. goto end;
  101. prog=argv[0];
  102. argc--;
  103. argv++;
  104. if ((osk=sk_OPENSSL_STRING_new_null()) == NULL)
  105. {
  106. BIO_printf(bio_err,"Memory allocation failure\n");
  107. goto end;
  108. }
  109. while (argc >= 1)
  110. {
  111. if (strcmp(*argv,"-inform") == 0)
  112. {
  113. if (--argc < 1) goto bad;
  114. informat=str2fmt(*(++argv));
  115. }
  116. else if (strcmp(*argv,"-in") == 0)
  117. {
  118. if (--argc < 1) goto bad;
  119. infile= *(++argv);
  120. }
  121. else if (strcmp(*argv,"-out") == 0)
  122. {
  123. if (--argc < 1) goto bad;
  124. derfile= *(++argv);
  125. }
  126. else if (strcmp(*argv,"-i") == 0)
  127. {
  128. indent=1;
  129. }
  130. else if (strcmp(*argv,"-noout") == 0) noout = 1;
  131. else if (strcmp(*argv,"-oid") == 0)
  132. {
  133. if (--argc < 1) goto bad;
  134. oidfile= *(++argv);
  135. }
  136. else if (strcmp(*argv,"-offset") == 0)
  137. {
  138. if (--argc < 1) goto bad;
  139. offset= atoi(*(++argv));
  140. }
  141. else if (strcmp(*argv,"-length") == 0)
  142. {
  143. if (--argc < 1) goto bad;
  144. length= atoi(*(++argv));
  145. if (length == 0) goto bad;
  146. }
  147. else if (strcmp(*argv,"-dump") == 0)
  148. {
  149. dump= -1;
  150. }
  151. else if (strcmp(*argv,"-dlimit") == 0)
  152. {
  153. if (--argc < 1) goto bad;
  154. dump= atoi(*(++argv));
  155. if (dump <= 0) goto bad;
  156. }
  157. else if (strcmp(*argv,"-strparse") == 0)
  158. {
  159. if (--argc < 1) goto bad;
  160. sk_OPENSSL_STRING_push(osk,*(++argv));
  161. }
  162. else if (strcmp(*argv,"-genstr") == 0)
  163. {
  164. if (--argc < 1) goto bad;
  165. genstr= *(++argv);
  166. }
  167. else if (strcmp(*argv,"-genconf") == 0)
  168. {
  169. if (--argc < 1) goto bad;
  170. genconf= *(++argv);
  171. }
  172. else
  173. {
  174. BIO_printf(bio_err,"unknown option %s\n",*argv);
  175. badops=1;
  176. break;
  177. }
  178. argc--;
  179. argv++;
  180. }
  181. if (badops)
  182. {
  183. bad:
  184. BIO_printf(bio_err,"%s [options] <infile\n",prog);
  185. BIO_printf(bio_err,"where options are\n");
  186. BIO_printf(bio_err," -inform arg input format - one of DER PEM\n");
  187. BIO_printf(bio_err," -in arg input file\n");
  188. BIO_printf(bio_err," -out arg output file (output format is always DER\n");
  189. BIO_printf(bio_err," -noout arg don't produce any output\n");
  190. BIO_printf(bio_err," -offset arg offset into file\n");
  191. BIO_printf(bio_err," -length arg length of section in file\n");
  192. BIO_printf(bio_err," -i indent entries\n");
  193. BIO_printf(bio_err," -dump dump unknown data in hex form\n");
  194. BIO_printf(bio_err," -dlimit arg dump the first arg bytes of unknown data in hex form\n");
  195. BIO_printf(bio_err," -oid file file of extra oid definitions\n");
  196. BIO_printf(bio_err," -strparse offset\n");
  197. BIO_printf(bio_err," a series of these can be used to 'dig' into multiple\n");
  198. BIO_printf(bio_err," ASN1 blob wrappings\n");
  199. BIO_printf(bio_err," -genstr str string to generate ASN1 structure from\n");
  200. BIO_printf(bio_err," -genconf file file to generate ASN1 structure from\n");
  201. goto end;
  202. }
  203. ERR_load_crypto_strings();
  204. in=BIO_new(BIO_s_file());
  205. out=BIO_new(BIO_s_file());
  206. if ((in == NULL) || (out == NULL))
  207. {
  208. ERR_print_errors(bio_err);
  209. goto end;
  210. }
  211. BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
  212. #ifdef OPENSSL_SYS_VMS
  213. {
  214. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  215. out = BIO_push(tmpbio, out);
  216. }
  217. #endif
  218. if (oidfile != NULL)
  219. {
  220. if (BIO_read_filename(in,oidfile) <= 0)
  221. {
  222. BIO_printf(bio_err,"problems opening %s\n",oidfile);
  223. ERR_print_errors(bio_err);
  224. goto end;
  225. }
  226. OBJ_create_objects(in);
  227. }
  228. if (infile == NULL)
  229. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  230. else
  231. {
  232. if (BIO_read_filename(in,infile) <= 0)
  233. {
  234. perror(infile);
  235. goto end;
  236. }
  237. }
  238. if (derfile) {
  239. if(!(derout = BIO_new_file(derfile, "wb"))) {
  240. BIO_printf(bio_err,"problems opening %s\n",derfile);
  241. ERR_print_errors(bio_err);
  242. goto end;
  243. }
  244. }
  245. if ((buf=BUF_MEM_new()) == NULL) goto end;
  246. if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) */
  247. if (genstr || genconf)
  248. {
  249. num = do_generate(bio_err, genstr, genconf, buf);
  250. if (num < 0)
  251. {
  252. ERR_print_errors(bio_err);
  253. goto end;
  254. }
  255. }
  256. else
  257. {
  258. if (informat == FORMAT_PEM)
  259. {
  260. BIO *tmp;
  261. if ((b64=BIO_new(BIO_f_base64())) == NULL)
  262. goto end;
  263. BIO_push(b64,in);
  264. tmp=in;
  265. in=b64;
  266. b64=tmp;
  267. }
  268. num=0;
  269. for (;;)
  270. {
  271. if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
  272. i=BIO_read(in,&(buf->data[num]),BUFSIZ);
  273. if (i <= 0) break;
  274. num+=i;
  275. }
  276. }
  277. str=buf->data;
  278. /* If any structs to parse go through in sequence */
  279. if (sk_OPENSSL_STRING_num(osk))
  280. {
  281. tmpbuf=(unsigned char *)str;
  282. tmplen=num;
  283. for (i=0; i<sk_OPENSSL_STRING_num(osk); i++)
  284. {
  285. ASN1_TYPE *atmp;
  286. int typ;
  287. j=atoi(sk_OPENSSL_STRING_value(osk,i));
  288. if (j == 0)
  289. {
  290. BIO_printf(bio_err,"'%s' is an invalid number\n",sk_OPENSSL_STRING_value(osk,i));
  291. continue;
  292. }
  293. tmpbuf+=j;
  294. tmplen-=j;
  295. atmp = at;
  296. ctmpbuf = tmpbuf;
  297. at = d2i_ASN1_TYPE(NULL,&ctmpbuf,tmplen);
  298. ASN1_TYPE_free(atmp);
  299. if(!at)
  300. {
  301. BIO_printf(bio_err,"Error parsing structure\n");
  302. ERR_print_errors(bio_err);
  303. goto end;
  304. }
  305. typ = ASN1_TYPE_get(at);
  306. if ((typ == V_ASN1_OBJECT)
  307. || (typ == V_ASN1_NULL))
  308. {
  309. BIO_printf(bio_err, "Can't parse %s type\n",
  310. typ == V_ASN1_NULL ? "NULL" : "OBJECT");
  311. ERR_print_errors(bio_err);
  312. goto end;
  313. }
  314. /* hmm... this is a little evil but it works */
  315. tmpbuf=at->value.asn1_string->data;
  316. tmplen=at->value.asn1_string->length;
  317. }
  318. str=(char *)tmpbuf;
  319. num=tmplen;
  320. }
  321. if (offset >= num)
  322. {
  323. BIO_printf(bio_err, "Error: offset too large\n");
  324. goto end;
  325. }
  326. num -= offset;
  327. if ((length == 0) || ((long)length > num)) length=(unsigned int)num;
  328. if(derout) {
  329. if(BIO_write(derout, str + offset, length) != (int)length) {
  330. BIO_printf(bio_err, "Error writing output\n");
  331. ERR_print_errors(bio_err);
  332. goto end;
  333. }
  334. }
  335. if (!noout &&
  336. !ASN1_parse_dump(out,(unsigned char *)&(str[offset]),length,
  337. indent,dump))
  338. {
  339. ERR_print_errors(bio_err);
  340. goto end;
  341. }
  342. ret=0;
  343. end:
  344. BIO_free(derout);
  345. if (in != NULL) BIO_free(in);
  346. if (out != NULL) BIO_free_all(out);
  347. if (b64 != NULL) BIO_free(b64);
  348. if (ret != 0)
  349. ERR_print_errors(bio_err);
  350. if (buf != NULL) BUF_MEM_free(buf);
  351. if (at != NULL) ASN1_TYPE_free(at);
  352. if (osk != NULL) sk_OPENSSL_STRING_free(osk);
  353. OBJ_cleanup();
  354. apps_shutdown();
  355. OPENSSL_EXIT(ret);
  356. }
  357. static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf)
  358. {
  359. CONF *cnf = NULL;
  360. int len;
  361. long errline;
  362. unsigned char *p;
  363. ASN1_TYPE *atyp = NULL;
  364. if (genconf)
  365. {
  366. cnf = NCONF_new(NULL);
  367. if (!NCONF_load(cnf, genconf, &errline))
  368. goto conferr;
  369. if (!genstr)
  370. genstr = NCONF_get_string(cnf, "default", "asn1");
  371. if (!genstr)
  372. {
  373. BIO_printf(bio, "Can't find 'asn1' in '%s'\n", genconf);
  374. goto err;
  375. }
  376. }
  377. atyp = ASN1_generate_nconf(genstr, cnf);
  378. NCONF_free(cnf);
  379. cnf = NULL;
  380. if (!atyp)
  381. return -1;
  382. len = i2d_ASN1_TYPE(atyp, NULL);
  383. if (len <= 0)
  384. goto err;
  385. if (!BUF_MEM_grow(buf,len))
  386. goto err;
  387. p=(unsigned char *)buf->data;
  388. i2d_ASN1_TYPE(atyp, &p);
  389. ASN1_TYPE_free(atyp);
  390. return len;
  391. conferr:
  392. if (errline > 0)
  393. BIO_printf(bio, "Error on line %ld of config file '%s'\n",
  394. errline, genconf);
  395. else
  396. BIO_printf(bio, "Error loading config file '%s'\n", genconf);
  397. err:
  398. NCONF_free(cnf);
  399. ASN1_TYPE_free(atyp);
  400. return -1;
  401. }