Asn1Dump.cs 13 KB

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