AsnEncodedData.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // AsnEncodedData.cs - System.Security.Cryptography.AsnEncodedData
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Security.Cryptography.X509Certificates;
  31. using System.Text;
  32. using Mono.Security;
  33. using Mono.Security.Cryptography;
  34. namespace System.Security.Cryptography {
  35. internal enum AsnDecodeStatus {
  36. NotDecoded = -1,
  37. Ok = 0,
  38. BadAsn = 1,
  39. BadTag = 2,
  40. BadLength = 3,
  41. InformationNotAvailable = 4
  42. }
  43. public class AsnEncodedData {
  44. internal Oid _oid;
  45. internal byte[] _raw;
  46. // constructors
  47. protected AsnEncodedData ()
  48. {
  49. }
  50. public AsnEncodedData (string oid, byte[] rawData)
  51. {
  52. _oid = new Oid (oid);
  53. RawData = rawData;
  54. }
  55. public AsnEncodedData (Oid oid, byte[] rawData)
  56. {
  57. Oid = oid;
  58. RawData = rawData;
  59. // yes, here oid == null is legal (by design),
  60. // but no, it would not be legal for an oid string
  61. // see MSDN FDBK11479
  62. }
  63. public AsnEncodedData (AsnEncodedData asnEncodedData)
  64. {
  65. if (asnEncodedData == null)
  66. throw new ArgumentNullException ("asnEncodedData");
  67. Oid = new Oid (asnEncodedData._oid);
  68. RawData = asnEncodedData._raw;
  69. }
  70. public AsnEncodedData (byte[] rawData)
  71. {
  72. RawData = rawData;
  73. }
  74. // properties
  75. public Oid Oid {
  76. get { return _oid; }
  77. set {
  78. if (value == null)
  79. _oid = null;
  80. else
  81. _oid = new Oid (value);
  82. }
  83. }
  84. public byte[] RawData {
  85. get { return _raw; }
  86. set {
  87. if (value == null)
  88. throw new ArgumentNullException ("RawData");
  89. _raw = (byte[])value.Clone ();
  90. }
  91. }
  92. // methods
  93. public virtual void CopyFrom (AsnEncodedData asnEncodedData)
  94. {
  95. if (asnEncodedData == null)
  96. throw new ArgumentNullException ("asnEncodedData");
  97. Oid = new Oid (asnEncodedData._oid);
  98. RawData = asnEncodedData._raw;
  99. }
  100. public virtual string Format (bool multiLine)
  101. {
  102. if (_raw == null)
  103. return String.Empty;
  104. if (_oid == null)
  105. return Default (multiLine);
  106. return ToString (multiLine);
  107. }
  108. // internal decoding/formatting methods
  109. internal virtual string ToString (bool multiLine)
  110. {
  111. switch (_oid.Value) {
  112. // fx supported objects
  113. case X509BasicConstraintsExtension.oid:
  114. return BasicConstraintsExtension (multiLine);
  115. case X509EnhancedKeyUsageExtension.oid:
  116. return EnhancedKeyUsageExtension (multiLine);
  117. case X509KeyUsageExtension.oid:
  118. return KeyUsageExtension (multiLine);
  119. case X509SubjectKeyIdentifierExtension.oid:
  120. return SubjectKeyIdentifierExtension (multiLine);
  121. // other known objects (i.e. supported structure) -
  122. // but without any corresponding framework class
  123. case Oid.oidSubjectAltName:
  124. return SubjectAltName (multiLine);
  125. case Oid.oidNetscapeCertType:
  126. return NetscapeCertType (multiLine);
  127. default:
  128. return Default (multiLine);
  129. }
  130. }
  131. internal string Default (bool multiLine)
  132. {
  133. StringBuilder sb = new StringBuilder ();
  134. for (int i=0; i < _raw.Length; i++) {
  135. sb.Append (_raw [i].ToString ("x2"));
  136. if (i != _raw.Length - 1)
  137. sb.Append (" ");
  138. }
  139. return sb.ToString ();
  140. }
  141. // Indirectly (undocumented but) supported extensions
  142. internal string BasicConstraintsExtension (bool multiLine)
  143. {
  144. try {
  145. X509BasicConstraintsExtension bc = new X509BasicConstraintsExtension (this, false);
  146. return bc.ToString (multiLine);
  147. }
  148. catch {
  149. return String.Empty;
  150. }
  151. }
  152. internal string EnhancedKeyUsageExtension (bool multiLine)
  153. {
  154. try {
  155. X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (this, false);
  156. return eku.ToString (multiLine);
  157. }
  158. catch {
  159. return String.Empty;
  160. }
  161. }
  162. internal string KeyUsageExtension (bool multiLine)
  163. {
  164. try {
  165. X509KeyUsageExtension ku = new X509KeyUsageExtension (this, false);
  166. return ku.ToString (multiLine);
  167. }
  168. catch {
  169. return String.Empty;
  170. }
  171. }
  172. internal string SubjectKeyIdentifierExtension (bool multiLine)
  173. {
  174. try {
  175. X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (this, false);
  176. return ski.ToString (multiLine);
  177. }
  178. catch {
  179. return String.Empty;
  180. }
  181. }
  182. // Indirectly (undocumented but) supported extensions
  183. internal string SubjectAltName (bool multiLine)
  184. {
  185. if (_raw.Length < 5)
  186. return "Information Not Available";
  187. try {
  188. ASN1 ex = new ASN1 (_raw);
  189. StringBuilder sb = new StringBuilder ();
  190. for (int i=0; i < ex.Count; i++) {
  191. ASN1 el = ex [i];
  192. string type = null;
  193. string name = null;
  194. switch (el.Tag) {
  195. case 0x81:
  196. type = "RFC822 Name=";
  197. name = Encoding.ASCII.GetString (el.Value);
  198. break;
  199. case 0x82:
  200. type = "DNS Name=";
  201. name = Encoding.ASCII.GetString (el.Value);
  202. break;
  203. default:
  204. type = String.Format ("Unknown ({0})=", el.Tag);
  205. name = CryptoConvert.ToHex (el.Value);
  206. break;
  207. }
  208. sb.Append (type);
  209. sb.Append (name);
  210. if (multiLine) {
  211. sb.Append (Environment.NewLine);
  212. } else if (i < ex.Count - 1) {
  213. sb.Append (", ");
  214. }
  215. }
  216. return sb.ToString ();
  217. }
  218. catch {
  219. return String.Empty;
  220. }
  221. }
  222. internal string NetscapeCertType (bool multiLine)
  223. {
  224. // 4 byte long, BITSTRING (0x03), Value length of 2
  225. if ((_raw.Length < 4) || (_raw [0] != 0x03) || (_raw [1] != 0x02))
  226. return "Information Not Available";
  227. // first value byte is the number of unused bits
  228. int value = (_raw [3] >> _raw [2]) << _raw [2];
  229. StringBuilder sb = new StringBuilder ();
  230. bool first = false;
  231. if ((value & 0x80) == 0x80) {
  232. sb.Append ("SSL Client Authentication");
  233. }
  234. if ((value & 0x40) == 0x40) {
  235. if (sb.Length > 0)
  236. sb.Append (", ");
  237. sb.Append ("SSL Server Authentication");
  238. }
  239. if ((value & 0x20) == 0x20) {
  240. if (sb.Length > 0)
  241. sb.Append (", ");
  242. sb.Append ("SMIME");
  243. }
  244. if ((value & 0x10) == 0x10) {
  245. if (sb.Length > 0)
  246. sb.Append (", ");
  247. sb.Append ("Signature"); // a.k.a. Object Signing / Code Signing
  248. }
  249. if ((value & 0x08) == 0x08) {
  250. if (sb.Length > 0)
  251. sb.Append (", ");
  252. sb.Append ("Unknown cert type");
  253. }
  254. if ((value & 0x04) == 0x04) {
  255. if (sb.Length > 0)
  256. sb.Append (", ");
  257. sb.Append ("SSL CA"); // CA == Certificate Authority
  258. }
  259. if ((value & 0x02) == 0x02) {
  260. if (sb.Length > 0)
  261. sb.Append (", ");
  262. sb.Append ("SMIME CA");
  263. }
  264. if ((value & 0x01) == 0x01) {
  265. if (sb.Length > 0)
  266. sb.Append (", ");
  267. sb.Append ("Signature CA");
  268. }
  269. sb.AppendFormat (" ({0})", value.ToString ("x2"));
  270. return sb.ToString ();
  271. }
  272. }
  273. }
  274. #endif