AsnEncodedData.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. namespace System.Security.Cryptography {
  33. internal enum AsnDecodeStatus {
  34. NotDecoded = -1,
  35. Ok = 0,
  36. BadAsn = 1,
  37. BadTag = 2,
  38. BadLength = 3,
  39. InformationNotAvailable = 4
  40. }
  41. public class AsnEncodedData {
  42. internal Oid _oid;
  43. internal byte[] _raw;
  44. // constructors
  45. protected AsnEncodedData ()
  46. {
  47. }
  48. public AsnEncodedData (string oid, byte[] rawData)
  49. {
  50. _oid = new Oid (oid);
  51. RawData = rawData;
  52. }
  53. public AsnEncodedData (Oid oid, byte[] rawData)
  54. {
  55. Oid = oid;
  56. RawData = rawData;
  57. // yes, here oid == null is legal (by design),
  58. // but no, it would not be legal for an oid string
  59. // see MSDN FDBK11479
  60. }
  61. public AsnEncodedData (AsnEncodedData asnEncodedData)
  62. {
  63. if (asnEncodedData == null)
  64. throw new ArgumentNullException ("asnEncodedData");
  65. Oid = new Oid (asnEncodedData._oid);
  66. RawData = asnEncodedData._raw;
  67. }
  68. public AsnEncodedData (byte[] rawData)
  69. {
  70. RawData = rawData;
  71. }
  72. // properties
  73. public Oid Oid {
  74. get { return _oid; }
  75. set {
  76. if (value == null)
  77. _oid = null;
  78. else
  79. _oid = new Oid (value);
  80. }
  81. }
  82. public byte[] RawData {
  83. get { return _raw; }
  84. set {
  85. if (value == null)
  86. throw new ArgumentNullException ("RawData");
  87. _raw = (byte[])value.Clone ();
  88. }
  89. }
  90. // methods
  91. public virtual void CopyFrom (AsnEncodedData asnEncodedData)
  92. {
  93. if (asnEncodedData == null)
  94. throw new ArgumentNullException ("asnEncodedData");
  95. Oid = new Oid (asnEncodedData._oid);
  96. RawData = asnEncodedData._raw;
  97. }
  98. public virtual string Format (bool multiLine)
  99. {
  100. if (_raw == null)
  101. return String.Empty;
  102. if (_oid == null)
  103. return Default (multiLine);
  104. return ToString (multiLine);
  105. }
  106. // internal decoding/formatting methods
  107. internal virtual string ToString (bool multiLine)
  108. {
  109. switch (_oid.Value) {
  110. // fx supported objects
  111. case X509BasicConstraintsExtension.oid:
  112. return BasicConstraintsExtension (multiLine);
  113. case X509EnhancedKeyUsageExtension.oid:
  114. return EnhancedKeyUsageExtension (multiLine);
  115. case X509KeyUsageExtension.oid:
  116. return KeyUsageExtension (multiLine);
  117. case X509SubjectKeyIdentifierExtension.oid:
  118. return SubjectKeyIdentifierExtension (multiLine);
  119. // other known objects (i.e. supported structure) -
  120. // but without any corresponding framework class
  121. case Oid.oidNetscapeCertType:
  122. return NetscapeCertType (multiLine);
  123. default:
  124. return Default (multiLine);
  125. }
  126. }
  127. internal string Default (bool multiLine)
  128. {
  129. StringBuilder sb = new StringBuilder ();
  130. for (int i=0; i < _raw.Length; i++) {
  131. sb.Append (_raw [i].ToString ("x2"));
  132. if (i != _raw.Length - 1)
  133. sb.Append (" ");
  134. }
  135. return sb.ToString ();
  136. }
  137. // Indirectly (undocumented but) supported extensions
  138. internal string BasicConstraintsExtension (bool multiLine)
  139. {
  140. try {
  141. X509BasicConstraintsExtension bc = new X509BasicConstraintsExtension (this, false);
  142. return bc.ToString (multiLine);
  143. }
  144. catch {
  145. return String.Empty;
  146. }
  147. }
  148. internal string EnhancedKeyUsageExtension (bool multiLine)
  149. {
  150. try {
  151. X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (this, false);
  152. return eku.ToString (multiLine);
  153. }
  154. catch {
  155. return String.Empty;
  156. }
  157. }
  158. internal string KeyUsageExtension (bool multiLine)
  159. {
  160. try {
  161. X509KeyUsageExtension ku = new X509KeyUsageExtension (this, false);
  162. return ku.ToString (multiLine);
  163. }
  164. catch {
  165. return String.Empty;
  166. }
  167. }
  168. internal string SubjectKeyIdentifierExtension (bool multiLine)
  169. {
  170. try {
  171. X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (this, false);
  172. return ski.ToString (multiLine);
  173. }
  174. catch {
  175. return String.Empty;
  176. }
  177. }
  178. // Indirectly (undocumented but) supported extensions
  179. internal string NetscapeCertType (bool multiLine)
  180. {
  181. // 4 byte long, BITSTRING (0x03), Value length of 2
  182. if ((_raw.Length < 4) || (_raw [0] != 0x03) || (_raw [1] != 0x02))
  183. return "Information Not Available";
  184. // first value byte is the number of unused bits
  185. int value = (_raw [3] >> _raw [2]) << _raw [2];
  186. StringBuilder sb = new StringBuilder ();
  187. bool first = false;
  188. if ((value & 0x80) == 0x80) {
  189. sb.Append ("SSL Client Authentication");
  190. }
  191. if ((value & 0x40) == 0x40) {
  192. if (sb.Length > 0)
  193. sb.Append (", ");
  194. sb.Append ("SSL Server Authentication");
  195. }
  196. if ((value & 0x20) == 0x20) {
  197. if (sb.Length > 0)
  198. sb.Append (", ");
  199. sb.Append ("SMIME");
  200. }
  201. if ((value & 0x10) == 0x10) {
  202. if (sb.Length > 0)
  203. sb.Append (", ");
  204. sb.Append ("Signature"); // a.k.a. Object Signing / Code Signing
  205. }
  206. if ((value & 0x08) == 0x08) {
  207. if (sb.Length > 0)
  208. sb.Append (", ");
  209. sb.Append ("Unknown cert type");
  210. }
  211. if ((value & 0x04) == 0x04) {
  212. if (sb.Length > 0)
  213. sb.Append (", ");
  214. sb.Append ("SSL CA"); // CA == Certificate Authority
  215. }
  216. if ((value & 0x02) == 0x02) {
  217. if (sb.Length > 0)
  218. sb.Append (", ");
  219. sb.Append ("SMIME CA");
  220. }
  221. if ((value & 0x01) == 0x01) {
  222. if (sb.Length > 0)
  223. sb.Append (", ");
  224. sb.Append ("Signature CA");
  225. }
  226. sb.AppendFormat (" ({0})", value.ToString ("x2"));
  227. return sb.ToString ();
  228. }
  229. }
  230. }
  231. #endif