X509KeyUsageExtension.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // System.Security.Cryptography.X509Certificates.X509KeyUsageExtension
  3. //
  4. // Authors:
  5. // Tim Coleman ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) Tim Coleman, 2004
  9. // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System.Text;
  32. using Mono.Security;
  33. namespace System.Security.Cryptography.X509Certificates {
  34. public sealed class X509KeyUsageExtension : X509Extension {
  35. internal const string oid = "2.5.29.15";
  36. internal const string friendlyName = "Key Usage";
  37. internal const X509KeyUsageFlags all = X509KeyUsageFlags.EncipherOnly | X509KeyUsageFlags.CrlSign |
  38. X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.KeyAgreement | X509KeyUsageFlags.DataEncipherment |
  39. X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.NonRepudiation |
  40. X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.DecipherOnly;
  41. private X509KeyUsageFlags _keyUsages;
  42. private AsnDecodeStatus _status;
  43. // constructors
  44. public X509KeyUsageExtension ()
  45. {
  46. _oid = new Oid (oid, friendlyName);
  47. }
  48. public X509KeyUsageExtension (AsnEncodedData encodedKeyUsage, bool critical)
  49. {
  50. // ignore the Oid provided by encodedKeyUsage (our rules!)
  51. _oid = new Oid (oid, friendlyName);
  52. _raw = encodedKeyUsage.RawData;
  53. base.Critical = critical;
  54. _status = Decode (this.RawData);
  55. }
  56. public X509KeyUsageExtension (X509KeyUsageFlags keyUsages, bool critical)
  57. {
  58. _oid = new Oid (oid, friendlyName);
  59. base.Critical = critical;
  60. _keyUsages = GetValidFlags (keyUsages);
  61. RawData = Encode ();
  62. }
  63. // properties
  64. public X509KeyUsageFlags KeyUsages {
  65. get {
  66. switch (_status) {
  67. case AsnDecodeStatus.Ok:
  68. case AsnDecodeStatus.InformationNotAvailable:
  69. return _keyUsages;
  70. default:
  71. throw new CryptographicException ("Badly encoded extension.");
  72. }
  73. }
  74. }
  75. // methods
  76. public override void CopyFrom (AsnEncodedData encodedData)
  77. {
  78. if (encodedData == null)
  79. throw new ArgumentException ("encodedData");
  80. // MS BUG throw new ArgumentNullException ("encodedData");
  81. X509Extension ex = (encodedData as X509Extension);
  82. if (ex == null)
  83. throw new ArgumentException (Locale.GetText ("Wrong type."), "encodedData");
  84. if (ex._oid == null)
  85. _oid = new Oid (oid, friendlyName);
  86. else
  87. _oid = new Oid (ex._oid);
  88. RawData = ex.RawData;
  89. base.Critical = ex.Critical;
  90. // and we deal with the rest later
  91. _status = Decode (this.RawData);
  92. }
  93. // internal
  94. internal X509KeyUsageFlags GetValidFlags (X509KeyUsageFlags flags)
  95. {
  96. if ((flags & all) != flags)
  97. return (X509KeyUsageFlags) 0;
  98. return flags;
  99. }
  100. internal AsnDecodeStatus Decode (byte[] extension)
  101. {
  102. if ((extension == null) || (extension.Length == 0))
  103. return AsnDecodeStatus.BadAsn;
  104. if (extension [0] != 0x03)
  105. return AsnDecodeStatus.BadTag;
  106. if (extension.Length < 3)
  107. return AsnDecodeStatus.BadLength;
  108. if (extension.Length < 4)
  109. return AsnDecodeStatus.InformationNotAvailable;
  110. try {
  111. ASN1 ex = new ASN1 (extension);
  112. int kubits = 0;
  113. int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
  114. while (i < ex.Value.Length)
  115. kubits = (kubits << 8) + ex.Value [i++];
  116. _keyUsages = GetValidFlags ((X509KeyUsageFlags)kubits);
  117. }
  118. catch {
  119. return AsnDecodeStatus.BadAsn;
  120. }
  121. return AsnDecodeStatus.Ok;
  122. }
  123. internal byte[] Encode ()
  124. {
  125. ASN1 ex = null;
  126. int kubits = (int)_keyUsages;
  127. byte empty = 0;
  128. if (kubits == 0) {
  129. ex = new ASN1 (0x03, new byte[] { empty });
  130. } else {
  131. // count empty bits (applicable to first byte only)
  132. int ku = ((kubits < Byte.MaxValue) ? kubits : (kubits >> 8));
  133. while (((ku & 0x01) == 0x00) && (empty < 8)) {
  134. empty++;
  135. ku >>= 1;
  136. }
  137. if (kubits <= Byte.MaxValue) {
  138. ex = new ASN1 (0x03, new byte[] { empty, (byte)kubits });
  139. } else {
  140. ex = new ASN1 (0x03, new byte[] { empty, (byte)kubits, (byte)(kubits >> 8) });
  141. }
  142. }
  143. return ex.GetBytes ();
  144. }
  145. internal override string ToString (bool multiLine)
  146. {
  147. switch (_status) {
  148. case AsnDecodeStatus.BadAsn:
  149. return String.Empty;
  150. case AsnDecodeStatus.BadTag:
  151. case AsnDecodeStatus.BadLength:
  152. return FormatUnkownData (_raw);
  153. case AsnDecodeStatus.InformationNotAvailable:
  154. return "Information Not Available";
  155. }
  156. if (_oid.Value != oid)
  157. return String.Format ("Unknown Key Usage ({0})", _oid.Value);
  158. if (_keyUsages == 0)
  159. return "Information Not Available";
  160. StringBuilder sb = new StringBuilder ();
  161. if ((_keyUsages & X509KeyUsageFlags.DigitalSignature) != 0) {
  162. sb.Append ("Digital Signature");
  163. }
  164. if ((_keyUsages & X509KeyUsageFlags.NonRepudiation) != 0) {
  165. if (sb.Length > 0)
  166. sb.Append (", ");
  167. sb.Append ("Non-Repudiation");
  168. }
  169. if ((_keyUsages & X509KeyUsageFlags.KeyEncipherment) != 0) {
  170. if (sb.Length > 0)
  171. sb.Append (", ");
  172. sb.Append ("Key Encipherment");
  173. }
  174. if ((_keyUsages & X509KeyUsageFlags.DataEncipherment) != 0) {
  175. if (sb.Length > 0)
  176. sb.Append (", ");
  177. sb.Append ("Data Encipherment");
  178. }
  179. if ((_keyUsages & X509KeyUsageFlags.KeyAgreement) != 0) {
  180. if (sb.Length > 0)
  181. sb.Append (", ");
  182. sb.Append ("Key Agreement");
  183. }
  184. if ((_keyUsages & X509KeyUsageFlags.KeyCertSign) != 0) {
  185. if (sb.Length > 0)
  186. sb.Append (", ");
  187. sb.Append ("Certificate Signing");
  188. }
  189. if ((_keyUsages & X509KeyUsageFlags.CrlSign) != 0) {
  190. if (sb.Length > 0)
  191. sb.Append (", ");
  192. sb.Append ("Off-line CRL Signing, CRL Signing");
  193. }
  194. if ((_keyUsages & X509KeyUsageFlags.EncipherOnly) != 0) {
  195. if (sb.Length > 0)
  196. sb.Append (", ");
  197. sb.Append ("Encipher Only");
  198. }
  199. if ((_keyUsages & X509KeyUsageFlags.DecipherOnly) != 0) {
  200. if (sb.Length > 0)
  201. sb.Append (", ");
  202. sb.Append ("Decipher Only");
  203. }
  204. int ku = (int)_keyUsages;
  205. sb.Append (" (");
  206. sb.Append (((byte)ku).ToString ("x2"));
  207. if (ku > Byte.MaxValue) {
  208. sb.Append (" ");
  209. sb.Append (((byte)(ku >> 8)).ToString ("x2"));
  210. }
  211. sb.Append (")");
  212. if (multiLine)
  213. sb.Append (Environment.NewLine);
  214. return sb.ToString ();
  215. }
  216. }
  217. }
  218. #endif