You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

436 lines
11 KiB

  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. #include <openssl/asn1.h>
  57. #include <openssl/bio.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. static int asn1_print_info(BIO *bp, int tag, int xclass,int constructed,
  61. int indent);
  62. static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
  63. int offset, int depth, int indent, int dump);
  64. static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
  65. int indent)
  66. {
  67. static const char fmt[]="%-18s";
  68. char str[128];
  69. const char *p;
  70. if (constructed & V_ASN1_CONSTRUCTED)
  71. p="cons: ";
  72. else
  73. p="prim: ";
  74. if (BIO_write(bp,p,6) < 6) goto err;
  75. BIO_indent(bp,indent,128);
  76. p=str;
  77. if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
  78. BIO_snprintf(str,sizeof str,"priv [ %d ] ",tag);
  79. else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
  80. BIO_snprintf(str,sizeof str,"cont [ %d ]",tag);
  81. else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
  82. BIO_snprintf(str,sizeof str,"appl [ %d ]",tag);
  83. else if (tag > 30)
  84. BIO_snprintf(str,sizeof str,"<ASN1 %d>",tag);
  85. else
  86. p = ASN1_tag2str(tag);
  87. if (BIO_printf(bp,fmt,p) <= 0)
  88. goto err;
  89. return(1);
  90. err:
  91. return(0);
  92. }
  93. int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
  94. {
  95. return(asn1_parse2(bp,&pp,len,0,0,indent,0));
  96. }
  97. int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent, int dump)
  98. {
  99. return(asn1_parse2(bp,&pp,len,0,0,indent,dump));
  100. }
  101. static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offset,
  102. int depth, int indent, int dump)
  103. {
  104. const unsigned char *p,*ep,*tot,*op,*opp;
  105. long len;
  106. int tag,xclass,ret=0;
  107. int nl,hl,j,r;
  108. ASN1_OBJECT *o=NULL;
  109. ASN1_OCTET_STRING *os=NULL;
  110. /* ASN1_BMPSTRING *bmp=NULL;*/
  111. int dump_indent;
  112. #if 0
  113. dump_indent = indent;
  114. #else
  115. dump_indent = 6; /* Because we know BIO_dump_indent() */
  116. #endif
  117. p= *pp;
  118. tot=p+length;
  119. op=p-1;
  120. while ((p < tot) && (op < p))
  121. {
  122. op=p;
  123. j=ASN1_get_object(&p,&len,&tag,&xclass,length);
  124. #ifdef LINT
  125. j=j;
  126. #endif
  127. if (j & 0x80)
  128. {
  129. if (BIO_puts(bp, "Error in encoding\n") <= 0)
  130. goto end;
  131. ret=0;
  132. goto end;
  133. }
  134. hl=(p-op);
  135. length-=hl;
  136. /* if j == 0x21 it is a constructed indefinite length object */
  137. if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp))
  138. <= 0) goto end;
  139. if (j != (V_ASN1_CONSTRUCTED | 1))
  140. {
  141. if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ",
  142. depth,(long)hl,len) <= 0)
  143. goto end;
  144. }
  145. else
  146. {
  147. if (BIO_printf(bp,"d=%-2d hl=%ld l=inf ",
  148. depth,(long)hl) <= 0)
  149. goto end;
  150. }
  151. if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0))
  152. goto end;
  153. if (j & V_ASN1_CONSTRUCTED)
  154. {
  155. ep=p+len;
  156. if (BIO_puts(bp, "\n") <= 0) goto end;
  157. if (len > length)
  158. {
  159. BIO_printf(bp,
  160. "length is greater than %ld\n",length);
  161. ret=0;
  162. goto end;
  163. }
  164. if ((j == 0x21) && (len == 0))
  165. {
  166. for (;;)
  167. {
  168. r=asn1_parse2(bp,&p,(long)(tot-p),
  169. offset+(p - *pp),depth+1,
  170. indent,dump);
  171. if (r == 0) { ret=0; goto end; }
  172. if ((r == 2) || (p >= tot)) break;
  173. }
  174. }
  175. else
  176. while (p < ep)
  177. {
  178. r=asn1_parse2(bp,&p,(long)len,
  179. offset+(p - *pp),depth+1,
  180. indent,dump);
  181. if (r == 0) { ret=0; goto end; }
  182. }
  183. }
  184. else if (xclass != 0)
  185. {
  186. p+=len;
  187. if (BIO_puts(bp, "\n") <= 0) goto end;
  188. }
  189. else
  190. {
  191. nl=0;
  192. if ( (tag == V_ASN1_PRINTABLESTRING) ||
  193. (tag == V_ASN1_T61STRING) ||
  194. (tag == V_ASN1_IA5STRING) ||
  195. (tag == V_ASN1_VISIBLESTRING) ||
  196. (tag == V_ASN1_NUMERICSTRING) ||
  197. (tag == V_ASN1_UTF8STRING) ||
  198. (tag == V_ASN1_UTCTIME) ||
  199. (tag == V_ASN1_GENERALIZEDTIME))
  200. {
  201. if (BIO_puts(bp, ":") <= 0) goto end;
  202. if ((len > 0) &&
  203. BIO_write(bp,(const char *)p,(int)len)
  204. != (int)len)
  205. goto end;
  206. }
  207. else if (tag == V_ASN1_OBJECT)
  208. {
  209. opp=op;
  210. if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL)
  211. {
  212. if (BIO_puts(bp, ":") <= 0) goto end;
  213. i2a_ASN1_OBJECT(bp,o);
  214. }
  215. else
  216. {
  217. if (BIO_puts(bp, ":BAD OBJECT") <= 0)
  218. goto end;
  219. }
  220. }
  221. else if (tag == V_ASN1_BOOLEAN)
  222. {
  223. int ii;
  224. opp=op;
  225. ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl);
  226. if (ii < 0)
  227. {
  228. if (BIO_puts(bp, "Bad boolean\n") <= 0)
  229. goto end;
  230. }
  231. BIO_printf(bp,":%d",ii);
  232. }
  233. else if (tag == V_ASN1_BMPSTRING)
  234. {
  235. /* do the BMP thang */
  236. }
  237. else if (tag == V_ASN1_OCTET_STRING)
  238. {
  239. int i,printable=1;
  240. opp=op;
  241. os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);
  242. if (os != NULL && os->length > 0)
  243. {
  244. opp = os->data;
  245. /* testing whether the octet string is
  246. * printable */
  247. for (i=0; i<os->length; i++)
  248. {
  249. if (( (opp[i] < ' ') &&
  250. (opp[i] != '\n') &&
  251. (opp[i] != '\r') &&
  252. (opp[i] != '\t')) ||
  253. (opp[i] > '~'))
  254. {
  255. printable=0;
  256. break;
  257. }
  258. }
  259. if (printable)
  260. /* printable string */
  261. {
  262. if (BIO_puts(bp, ":") <= 0)
  263. goto end;
  264. if (BIO_write(bp,(const char *)opp,
  265. os->length) <= 0)
  266. goto end;
  267. }
  268. else if (!dump)
  269. /* not printable => print octet string
  270. * as hex dump */
  271. {
  272. if (BIO_puts(bp, "[HEX DUMP]:") <= 0)
  273. goto end;
  274. for (i=0; i<os->length; i++)
  275. {
  276. if (BIO_printf(bp,"%02X"
  277. , opp[i]) <= 0)
  278. goto end;
  279. }
  280. }
  281. else
  282. /* print the normal dump */
  283. {
  284. if (!nl)
  285. {
  286. if (BIO_puts(bp, "\n") <= 0)
  287. goto end;
  288. }
  289. if (!BIO_hexdump(bp, opp,
  290. ((dump == -1 || dump >
  291. os->length)?os->length:dump),
  292. dump_indent))
  293. goto end;
  294. nl=1;
  295. }
  296. }
  297. if (os != NULL)
  298. {
  299. M_ASN1_OCTET_STRING_free(os);
  300. os=NULL;
  301. }
  302. }
  303. else if (tag == V_ASN1_INTEGER)
  304. {
  305. ASN1_INTEGER *bs;
  306. int i;
  307. opp=op;
  308. bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl);
  309. if (bs != NULL)
  310. {
  311. if (BIO_puts(bp, ":") <= 0) goto end;
  312. if (bs->type == V_ASN1_NEG_INTEGER)
  313. if (BIO_puts(bp, "-") <= 0)
  314. goto end;
  315. for (i=0; i<bs->length; i++)
  316. {
  317. if (BIO_printf(bp,"%02X",
  318. bs->data[i]) <= 0)
  319. goto end;
  320. }
  321. if (bs->length == 0)
  322. {
  323. if (BIO_puts(bp, "00") <= 0)
  324. goto end;
  325. }
  326. }
  327. else
  328. {
  329. if (BIO_puts(bp, "BAD INTEGER") <= 0)
  330. goto end;
  331. }
  332. M_ASN1_INTEGER_free(bs);
  333. }
  334. else if (tag == V_ASN1_ENUMERATED)
  335. {
  336. ASN1_ENUMERATED *bs;
  337. int i;
  338. opp=op;
  339. bs=d2i_ASN1_ENUMERATED(NULL,&opp,len+hl);
  340. if (bs != NULL)
  341. {
  342. if (BIO_puts(bp, ":") <= 0) goto end;
  343. if (bs->type == V_ASN1_NEG_ENUMERATED)
  344. if (BIO_puts(bp, "-") <= 0)
  345. goto end;
  346. for (i=0; i<bs->length; i++)
  347. {
  348. if (BIO_printf(bp,"%02X",
  349. bs->data[i]) <= 0)
  350. goto end;
  351. }
  352. if (bs->length == 0)
  353. {
  354. if (BIO_puts(bp, "00") <= 0)
  355. goto end;
  356. }
  357. }
  358. else
  359. {
  360. if (BIO_puts(bp, "BAD ENUMERATED") <= 0)
  361. goto end;
  362. }
  363. M_ASN1_ENUMERATED_free(bs);
  364. }
  365. else if (len > 0 && dump)
  366. {
  367. if (!nl)
  368. {
  369. if (BIO_puts(bp, "\n") <= 0)
  370. goto end;
  371. }
  372. if (!BIO_hexdump(bp,p,
  373. ((dump == -1 || dump > len)?len:dump),
  374. dump_indent))
  375. goto end;
  376. nl=1;
  377. }
  378. if (!nl)
  379. {
  380. if (BIO_puts(bp, "\n") <= 0) goto end;
  381. }
  382. p+=len;
  383. if ((tag == V_ASN1_EOC) && (xclass == 0))
  384. {
  385. ret=2; /* End of sequence */
  386. goto end;
  387. }
  388. }
  389. length-=len;
  390. }
  391. ret=1;
  392. end:
  393. if (o != NULL) ASN1_OBJECT_free(o);
  394. if (os != NULL) M_ASN1_OCTET_STRING_free(os);
  395. *pp=p;
  396. return(ret);
  397. }
  398. const char *ASN1_tag2str(int tag)
  399. {
  400. static const char * const tag2str[] = {
  401. "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", /* 0-4 */
  402. "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", /* 5-9 */
  403. "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>", /* 10-13 */
  404. "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET", /* 15-17 */
  405. "NUMERICSTRING", "PRINTABLESTRING", "T61STRING", /* 18-20 */
  406. "VIDEOTEXSTRING", "IA5STRING", "UTCTIME","GENERALIZEDTIME", /* 21-24 */
  407. "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", /* 25-27 */
  408. "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING" /* 28-30 */
  409. };
  410. if((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
  411. tag &= ~0x100;
  412. if(tag < 0 || tag > 30) return "(unknown)";
  413. return tag2str[tag];
  414. }