Asn1Dump.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Text;
  6. using Org.BouncyCastle.Utilities;
  7. using Org.BouncyCastle.Utilities.Encoders;
  8. namespace Org.BouncyCastle.Asn1.Utilities
  9. {
  10. public sealed class Asn1Dump
  11. {
  12. private static readonly string NewLine = Org.BouncyCastle.Utilities.Platform.NewLine;
  13. private Asn1Dump()
  14. {
  15. }
  16. private const string Tab = " ";
  17. private const int SampleSize = 32;
  18. /**
  19. * dump a Der object as a formatted string with indentation
  20. *
  21. * @param obj the Asn1Object to be dumped out.
  22. */
  23. private static void AsString(
  24. string indent,
  25. bool verbose,
  26. Asn1Object obj,
  27. StringBuilder buf)
  28. {
  29. if (obj is Asn1Sequence)
  30. {
  31. string tab = indent + Tab;
  32. buf.Append(indent);
  33. if (obj is BerSequence)
  34. {
  35. buf.Append("BER Sequence");
  36. }
  37. else if (obj is DerSequence)
  38. {
  39. buf.Append("DER Sequence");
  40. }
  41. else
  42. {
  43. buf.Append("Sequence");
  44. }
  45. buf.Append(NewLine);
  46. foreach (Asn1Encodable o in ((Asn1Sequence)obj))
  47. {
  48. if (o == null || o is Asn1Null)
  49. {
  50. buf.Append(tab);
  51. buf.Append("NULL");
  52. buf.Append(NewLine);
  53. }
  54. else
  55. {
  56. AsString(tab, verbose, o.ToAsn1Object(), buf);
  57. }
  58. }
  59. }
  60. else if (obj is DerTaggedObject)
  61. {
  62. string tab = indent + Tab;
  63. buf.Append(indent);
  64. if (obj is BerTaggedObject)
  65. {
  66. buf.Append("BER Tagged [");
  67. }
  68. else
  69. {
  70. buf.Append("Tagged [");
  71. }
  72. DerTaggedObject o = (DerTaggedObject)obj;
  73. buf.Append(((int)o.TagNo).ToString());
  74. buf.Append(']');
  75. if (!o.IsExplicit())
  76. {
  77. buf.Append(" IMPLICIT ");
  78. }
  79. buf.Append(NewLine);
  80. if (o.IsEmpty())
  81. {
  82. buf.Append(tab);
  83. buf.Append("EMPTY");
  84. buf.Append(NewLine);
  85. }
  86. else
  87. {
  88. AsString(tab, verbose, o.GetObject(), buf);
  89. }
  90. }
  91. else if (obj is BerSet)
  92. {
  93. string tab = indent + Tab;
  94. buf.Append(indent);
  95. buf.Append("BER SaveLocal");
  96. buf.Append(NewLine);
  97. foreach (Asn1Encodable o in ((Asn1Set)obj))
  98. {
  99. if (o == null)
  100. {
  101. buf.Append(tab);
  102. buf.Append("NULL");
  103. buf.Append(NewLine);
  104. }
  105. else
  106. {
  107. AsString(tab, verbose, o.ToAsn1Object(), buf);
  108. }
  109. }
  110. }
  111. else if (obj is DerSet)
  112. {
  113. string tab = indent + Tab;
  114. buf.Append(indent);
  115. buf.Append("DER SaveLocal");
  116. buf.Append(NewLine);
  117. foreach (Asn1Encodable o in ((Asn1Set)obj))
  118. {
  119. if (o == null)
  120. {
  121. buf.Append(tab);
  122. buf.Append("NULL");
  123. buf.Append(NewLine);
  124. }
  125. else
  126. {
  127. AsString(tab, verbose, o.ToAsn1Object(), buf);
  128. }
  129. }
  130. }
  131. else if (obj is DerObjectIdentifier)
  132. {
  133. buf.Append(indent + "ObjectIdentifier(" + ((DerObjectIdentifier)obj).Id + ")" + NewLine);
  134. }
  135. else if (obj is DerBoolean)
  136. {
  137. buf.Append(indent + "Boolean(" + ((DerBoolean)obj).IsTrue + ")" + NewLine);
  138. }
  139. else if (obj is DerInteger)
  140. {
  141. buf.Append(indent + "Integer(" + ((DerInteger)obj).Value + ")" + NewLine);
  142. }
  143. else if (obj is BerOctetString)
  144. {
  145. byte[] octets = ((Asn1OctetString)obj).GetOctets();
  146. string extra = verbose ? dumpBinaryDataAsString(indent, octets) : "";
  147. buf.Append(indent + "BER Octet String" + "[" + octets.Length + "] " + extra + NewLine);
  148. }
  149. else if (obj is DerOctetString)
  150. {
  151. byte[] octets = ((Asn1OctetString)obj).GetOctets();
  152. string extra = verbose ? dumpBinaryDataAsString(indent, octets) : "";
  153. buf.Append(indent + "DER Octet String" + "[" + octets.Length + "] " + extra + NewLine);
  154. }
  155. else if (obj is DerBitString)
  156. {
  157. DerBitString bt = (DerBitString)obj;
  158. byte[] bytes = bt.GetBytes();
  159. string extra = verbose ? dumpBinaryDataAsString(indent, bytes) : "";
  160. buf.Append(indent + "DER Bit String" + "[" + bytes.Length + ", " + bt.PadBits + "] " + extra + NewLine);
  161. }
  162. else if (obj is DerIA5String)
  163. {
  164. buf.Append(indent + "IA5String(" + ((DerIA5String)obj).GetString() + ") " + NewLine);
  165. }
  166. else if (obj is DerUtf8String)
  167. {
  168. buf.Append(indent + "UTF8String(" + ((DerUtf8String)obj).GetString() + ") " + NewLine);
  169. }
  170. else if (obj is DerPrintableString)
  171. {
  172. buf.Append(indent + "PrintableString(" + ((DerPrintableString)obj).GetString() + ") " + NewLine);
  173. }
  174. else if (obj is DerVisibleString)
  175. {
  176. buf.Append(indent + "VisibleString(" + ((DerVisibleString)obj).GetString() + ") " + NewLine);
  177. }
  178. else if (obj is DerBmpString)
  179. {
  180. buf.Append(indent + "BMPString(" + ((DerBmpString)obj).GetString() + ") " + NewLine);
  181. }
  182. else if (obj is DerT61String)
  183. {
  184. buf.Append(indent + "T61String(" + ((DerT61String)obj).GetString() + ") " + NewLine);
  185. }
  186. else if (obj is DerGraphicString)
  187. {
  188. buf.Append(indent + "GraphicString(" + ((DerGraphicString)obj).GetString() + ") " + NewLine);
  189. }
  190. else if (obj is DerVideotexString)
  191. {
  192. buf.Append(indent + "VideotexString(" + ((DerVideotexString)obj).GetString() + ") " + NewLine);
  193. }
  194. else if (obj is DerUtcTime)
  195. {
  196. buf.Append(indent + "UTCTime(" + ((DerUtcTime)obj).TimeString + ") " + NewLine);
  197. }
  198. else if (obj is DerGeneralizedTime)
  199. {
  200. buf.Append(indent + "GeneralizedTime(" + ((DerGeneralizedTime)obj).GetTime() + ") " + NewLine);
  201. }
  202. else if (obj is BerApplicationSpecific)
  203. {
  204. buf.Append(outputApplicationSpecific("BER", indent, verbose, (BerApplicationSpecific)obj));
  205. }
  206. else if (obj is DerApplicationSpecific)
  207. {
  208. buf.Append(outputApplicationSpecific("DER", indent, verbose, (DerApplicationSpecific)obj));
  209. }
  210. else if (obj is DerEnumerated)
  211. {
  212. DerEnumerated en = (DerEnumerated)obj;
  213. buf.Append(indent + "DER Enumerated(" + en.Value + ")" + NewLine);
  214. }
  215. else if (obj is DerExternal)
  216. {
  217. DerExternal ext = (DerExternal)obj;
  218. buf.Append(indent + "External " + NewLine);
  219. string tab = indent + Tab;
  220. if (ext.DirectReference != null)
  221. {
  222. buf.Append(tab + "Direct Reference: " + ext.DirectReference.Id + NewLine);
  223. }
  224. if (ext.IndirectReference != null)
  225. {
  226. buf.Append(tab + "Indirect Reference: " + ext.IndirectReference.ToString() + NewLine);
  227. }
  228. if (ext.DataValueDescriptor != null)
  229. {
  230. AsString(tab, verbose, ext.DataValueDescriptor, buf);
  231. }
  232. buf.Append(tab + "Encoding: " + ext.Encoding + NewLine);
  233. AsString(tab, verbose, ext.ExternalContent, buf);
  234. }
  235. else
  236. {
  237. buf.Append(indent + obj.ToString() + NewLine);
  238. }
  239. }
  240. private static string outputApplicationSpecific(
  241. string type,
  242. string indent,
  243. bool verbose,
  244. DerApplicationSpecific app)
  245. {
  246. StringBuilder buf = new StringBuilder();
  247. if (app.IsConstructed())
  248. {
  249. try
  250. {
  251. Asn1Sequence s = Asn1Sequence.GetInstance(app.GetObject(Asn1Tags.Sequence));
  252. buf.Append(indent + type + " ApplicationSpecific[" + app.ApplicationTag + "]" + NewLine);
  253. foreach (Asn1Encodable ae in s)
  254. {
  255. AsString(indent + Tab, verbose, ae.ToAsn1Object(), buf);
  256. }
  257. }
  258. catch (IOException e)
  259. {
  260. buf.Append(e);
  261. }
  262. return buf.ToString();
  263. }
  264. return indent + type + " ApplicationSpecific[" + app.ApplicationTag + "] ("
  265. + Hex.ToHexString(app.GetContents()) + ")" + NewLine;
  266. }
  267. [Obsolete("Use version accepting Asn1Encodable")]
  268. public static string DumpAsString(
  269. object obj)
  270. {
  271. if (obj is Asn1Encodable)
  272. {
  273. StringBuilder buf = new StringBuilder();
  274. AsString("", false, ((Asn1Encodable)obj).ToAsn1Object(), buf);
  275. return buf.ToString();
  276. }
  277. return "unknown object type " + obj.ToString();
  278. }
  279. /**
  280. * dump out a DER object as a formatted string, in non-verbose mode
  281. *
  282. * @param obj the Asn1Encodable to be dumped out.
  283. * @return the resulting string.
  284. */
  285. public static string DumpAsString(
  286. Asn1Encodable obj)
  287. {
  288. return DumpAsString(obj, false);
  289. }
  290. /**
  291. * Dump out the object as a string
  292. *
  293. * @param obj the Asn1Encodable to be dumped out.
  294. * @param verbose if true, dump out the contents of octet and bit strings.
  295. * @return the resulting string.
  296. */
  297. public static string DumpAsString(
  298. Asn1Encodable obj,
  299. bool verbose)
  300. {
  301. StringBuilder buf = new StringBuilder();
  302. AsString("", verbose, obj.ToAsn1Object(), buf);
  303. return buf.ToString();
  304. }
  305. private static string dumpBinaryDataAsString(string indent, byte[] bytes)
  306. {
  307. indent += Tab;
  308. StringBuilder buf = new StringBuilder(NewLine);
  309. for (int i = 0; i < bytes.Length; i += SampleSize)
  310. {
  311. if (bytes.Length - i > SampleSize)
  312. {
  313. buf.Append(indent);
  314. buf.Append(Hex.ToHexString(bytes, i, SampleSize));
  315. buf.Append(Tab);
  316. buf.Append(calculateAscString(bytes, i, SampleSize));
  317. buf.Append(NewLine);
  318. }
  319. else
  320. {
  321. buf.Append(indent);
  322. buf.Append(Hex.ToHexString(bytes, i, bytes.Length - i));
  323. for (int j = bytes.Length - i; j != SampleSize; j++)
  324. {
  325. buf.Append(" ");
  326. }
  327. buf.Append(Tab);
  328. buf.Append(calculateAscString(bytes, i, bytes.Length - i));
  329. buf.Append(NewLine);
  330. }
  331. }
  332. return buf.ToString();
  333. }
  334. private static string calculateAscString(
  335. byte[] bytes,
  336. int off,
  337. int len)
  338. {
  339. StringBuilder buf = new StringBuilder();
  340. for (int i = off; i != off + len; i++)
  341. {
  342. char c = (char)bytes[i];
  343. if (c >= ' ' && c <= '~')
  344. {
  345. buf.Append(c);
  346. }
  347. }
  348. return buf.ToString();
  349. }
  350. }
  351. }
  352. #endif