Asn1Dump.cs 12 KB

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