No Description
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.

Asn1Parser.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //+-------------------------------------------------------------------------------+
  2. //| Copyright (c) 2003 Liping Dai. All rights reserved. |
  3. //| Web: www.lipingshare.com |
  4. //| Email: lipingshare@yahoo.com |
  5. //| |
  6. //| Copyright and Permission Details: |
  7. //| ================================= |
  8. //| Permission is hereby granted, free of charge, to any person obtaining a copy |
  9. //| of this software and associated documentation files (the "Software"), to deal |
  10. //| in the Software without restriction, including without limitation the rights |
  11. //| to use, copy, modify, merge, publish, distribute, and/or sell copies of the |
  12. //| Software, subject to the following conditions: |
  13. //| |
  14. //| 1. Redistributions of source code must retain the above copyright notice, this|
  15. //| list of conditions and the following disclaimer. |
  16. //| |
  17. //| 2. Redistributions in binary form must reproduce the above copyright notice, |
  18. //| this list of conditions and the following disclaimer in the documentation |
  19. //| and/or other materials provided with the distribution. |
  20. //| |
  21. //| THE SOFTWARE PRODUCT IS PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, |
  22. //| EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
  23. //| WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR |
  24. //| A PARTICULAR PURPOSE. |
  25. //+-------------------------------------------------------------------------------+
  26. using System;
  27. using System.IO;
  28. namespace LipingShare.LCLib.Asn1Processor
  29. {
  30. /// <summary>
  31. /// ASN.1 encoded data parser.
  32. /// This a higher level class which unilized Asn1Node class functionality to
  33. /// provide functions for ASN.1 encoded files.
  34. /// </summary>
  35. internal class Asn1Parser
  36. {
  37. private byte[] rawData;
  38. private Asn1Node rootNode = new Asn1Node();
  39. /// <summary>
  40. /// Get/Set parseEncapsulatedData. Reloading data is required after this property is reset.
  41. /// </summary>
  42. bool ParseEncapsulatedData
  43. {
  44. get
  45. {
  46. return rootNode.ParseEncapsulatedData;
  47. }
  48. set
  49. {
  50. rootNode.ParseEncapsulatedData = value;
  51. }
  52. }
  53. /// <summary>
  54. /// Constructor.
  55. /// </summary>
  56. public Asn1Parser()
  57. {
  58. }
  59. /// <summary>
  60. /// Get raw ASN.1 encoded data.
  61. /// </summary>
  62. public byte[] RawData
  63. {
  64. get
  65. {
  66. return rawData;
  67. }
  68. }
  69. /// <summary>
  70. /// Load ASN.1 encoded data from a file.
  71. /// </summary>
  72. /// <param name="fileName">File name.</param>
  73. public void LoadData(string fileName)
  74. {
  75. FileStream fs = new FileStream(fileName, FileMode.Open);
  76. rawData = new byte[fs.Length];
  77. fs.Read(rawData, 0, (int)fs.Length);
  78. fs.Close();
  79. MemoryStream ms = new MemoryStream(rawData);
  80. LoadData(ms);
  81. }
  82. /// <summary>
  83. /// Load PEM formated file.
  84. /// </summary>
  85. /// <param name="fileName">PEM file name.</param>
  86. public void LoadPemData(string fileName)
  87. {
  88. FileStream fs = new FileStream(fileName, FileMode.Open);
  89. byte[] data = new byte[fs.Length];
  90. fs.Read(data, 0, data.Length);
  91. fs.Close();
  92. string dataStr = Asn1Util.BytesToString(data);
  93. if (Asn1Util.IsPemFormated(dataStr))
  94. {
  95. Stream ms = Asn1Util.PemToStream(dataStr);
  96. ms.Position = 0;
  97. LoadData(ms);
  98. }
  99. else
  100. {
  101. throw new Exception("It is a invalid PEM file: " + fileName);
  102. }
  103. }
  104. /// <summary>
  105. /// Load ASN.1 encoded data from Stream.
  106. /// </summary>
  107. /// <param name="stream">Stream data.</param>
  108. public void LoadData(Stream stream)
  109. {
  110. stream.Position = 0;
  111. if (!rootNode.LoadData(stream))
  112. {
  113. throw new ArgumentException("Failed to load data.");
  114. }
  115. rawData = new byte[stream.Length];
  116. stream.Position = 0;
  117. stream.Read(rawData, 0, rawData.Length);
  118. }
  119. /// <summary>
  120. /// Save data into a file.
  121. /// </summary>
  122. /// <param name="fileName">File name.</param>
  123. public void SaveData(string fileName)
  124. {
  125. FileStream fs = new FileStream(fileName, FileMode.Create);
  126. rootNode.SaveData(fs);
  127. fs.Close();
  128. }
  129. /// <summary>
  130. /// Get root node.
  131. /// </summary>
  132. public Asn1Node RootNode
  133. {
  134. get
  135. {
  136. return rootNode;
  137. }
  138. }
  139. /// <summary>
  140. /// Get a node by path string.
  141. /// </summary>
  142. /// <param name="nodePath">Path string.</param>
  143. /// <returns>Asn1Node or null.</returns>
  144. public Asn1Node GetNodeByPath(string nodePath)
  145. {
  146. return rootNode.GetDescendantNodeByPath(nodePath);
  147. }
  148. /// <summary>
  149. /// Get a node by OID.
  150. /// </summary>
  151. /// <param name="oid">OID string.</param>
  152. /// <returns>Asn1Node or null.</returns>
  153. public Asn1Node GetNodeByOid(string oid)
  154. {
  155. return Asn1Node.GetDecendantNodeByOid(oid, rootNode);
  156. }
  157. /// <summary>
  158. /// Generate node text header. This method is used by GetNodeText to put heading.
  159. /// </summary>
  160. /// <param name="lineLen">Line length.</param>
  161. /// <returns>Header string.</returns>
  162. static public string GetNodeTextHeader(int lineLen)
  163. {
  164. string header = String.Format("Offset| Len |LenByte|\r\n");
  165. header += "======+======+=======+" + Asn1Util.GenStr(lineLen + 10, '=') + "\r\n";
  166. return header;
  167. }
  168. /// <summary>
  169. /// Generate the root node text description.
  170. /// </summary>
  171. /// <returns>Text string.</returns>
  172. public override string ToString()
  173. {
  174. return GetNodeText(rootNode, 100);
  175. }
  176. /// <summary>
  177. /// Generate node text description. It uses GetNodeTextHeader to generate
  178. /// the heading and Asn1Node.GetText to generate the node text.
  179. /// </summary>
  180. /// <param name="node">Target node.</param>
  181. /// <param name="lineLen">Line length.</param>
  182. /// <returns>Text string.</returns>
  183. public static string GetNodeText(Asn1Node node, int lineLen)
  184. {
  185. string nodeStr = GetNodeTextHeader(lineLen);
  186. nodeStr += node.GetText(node, lineLen);
  187. return nodeStr;
  188. }
  189. }
  190. }