X509KeyUsageExtension.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 ArgumentNullException ("encodedData");
  80. X509Extension ex = (encodedData as X509Extension);
  81. if (ex == null)
  82. throw new ArgumentException (Locale.GetText ("Wrong type."), "encodedData");
  83. if (ex._oid == null)
  84. _oid = new Oid (oid, friendlyName);
  85. else
  86. _oid = new Oid (ex._oid);
  87. RawData = ex.RawData;
  88. base.Critical = ex.Critical;
  89. // and we deal with the rest later
  90. _status = Decode (this.RawData);
  91. }
  92. // internal
  93. internal X509KeyUsageFlags GetValidFlags (X509KeyUsageFlags flags)
  94. {
  95. if ((flags & all) != flags)
  96. return (X509KeyUsageFlags) 0;
  97. return flags;
  98. }
  99. internal AsnDecodeStatus Decode (byte[] extension)
  100. {
  101. if ((extension == null) || (extension.Length == 0))
  102. return AsnDecodeStatus.BadAsn;
  103. if (extension [0] != 0x03)
  104. return AsnDecodeStatus.BadTag;
  105. if (extension.Length < 3)
  106. return AsnDecodeStatus.BadLength;
  107. if (extension.Length < 4)
  108. return AsnDecodeStatus.InformationNotAvailable;
  109. try {
  110. ASN1 ex = new ASN1 (extension);
  111. int kubits = 0;
  112. int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
  113. while (i < ex.Value.Length)
  114. kubits = (kubits << 8) + ex.Value [i++];
  115. _keyUsages = GetValidFlags ((X509KeyUsageFlags)kubits);
  116. }
  117. catch {
  118. return AsnDecodeStatus.BadAsn;
  119. }
  120. return AsnDecodeStatus.Ok;
  121. }
  122. internal byte[] Encode ()
  123. {
  124. ASN1 ex = null;
  125. int kubits = (int)_keyUsages;
  126. byte empty = 0;
  127. if (kubits == 0) {
  128. ex = new ASN1 (0x03, new byte[] { empty });
  129. } else {
  130. // count empty bits (applicable to first byte only)
  131. int ku = ((kubits < Byte.MaxValue) ? kubits : (kubits >> 8));
  132. while (((ku & 0x01) == 0x00) && (empty < 8)) {
  133. empty++;
  134. ku >>= 1;
  135. }
  136. if (kubits <= Byte.MaxValue) {
  137. ex = new ASN1 (0x03, new byte[] { empty, (byte)kubits });
  138. } else {
  139. ex = new ASN1 (0x03, new byte[] { empty, (byte)kubits, (byte)(kubits >> 8) });
  140. }
  141. }
  142. return ex.GetBytes ();
  143. }
  144. internal override string ToString (bool multiLine)
  145. {
  146. switch (_status) {
  147. case AsnDecodeStatus.BadAsn:
  148. return String.Empty;
  149. case AsnDecodeStatus.BadTag:
  150. case AsnDecodeStatus.BadLength:
  151. return FormatUnkownData (_raw);
  152. case AsnDecodeStatus.InformationNotAvailable:
  153. return "Information Not Available";
  154. }
  155. if (_oid.Value != oid)
  156. return String.Format ("Unknown Key Usage ({0})", _oid.Value);
  157. if (_keyUsages == 0)
  158. return "Information Not Available";
  159. StringBuilder sb = new StringBuilder ();
  160. if ((_keyUsages & X509KeyUsageFlags.DigitalSignature) != 0) {
  161. sb.Append ("Digital Signature");
  162. }
  163. if ((_keyUsages & X509KeyUsageFlags.NonRepudiation) != 0) {
  164. if (sb.Length > 0)
  165. sb.Append (", ");
  166. sb.Append ("Non-Repudiation");
  167. }
  168. if ((_keyUsages & X509KeyUsageFlags.KeyEncipherment) != 0) {
  169. if (sb.Length > 0)
  170. sb.Append (", ");
  171. sb.Append ("Key Encipherment");
  172. }
  173. if ((_keyUsages & X509KeyUsageFlags.DataEncipherment) != 0) {
  174. if (sb.Length > 0)
  175. sb.Append (", ");
  176. sb.Append ("Data Encipherment");
  177. }
  178. if ((_keyUsages & X509KeyUsageFlags.KeyAgreement) != 0) {
  179. if (sb.Length > 0)
  180. sb.Append (", ");
  181. sb.Append ("Key Agreement");
  182. }
  183. if ((_keyUsages & X509KeyUsageFlags.KeyCertSign) != 0) {
  184. if (sb.Length > 0)
  185. sb.Append (", ");
  186. sb.Append ("Certificate Signing");
  187. }
  188. if ((_keyUsages & X509KeyUsageFlags.CrlSign) != 0) {
  189. if (sb.Length > 0)
  190. sb.Append (", ");
  191. sb.Append ("Off-line CRL Signing, CRL Signing");
  192. }
  193. if ((_keyUsages & X509KeyUsageFlags.EncipherOnly) != 0) {
  194. if (sb.Length > 0)
  195. sb.Append (", ");
  196. sb.Append ("Encipher Only");
  197. }
  198. if ((_keyUsages & X509KeyUsageFlags.DecipherOnly) != 0) {
  199. if (sb.Length > 0)
  200. sb.Append (", ");
  201. sb.Append ("Decipher Only");
  202. }
  203. int ku = (int)_keyUsages;
  204. sb.Append (" (");
  205. sb.Append (((byte)ku).ToString ("x2"));
  206. if (ku > Byte.MaxValue) {
  207. sb.Append (" ");
  208. sb.Append (((byte)(ku >> 8)).ToString ("x2"));
  209. }
  210. sb.Append (")");
  211. if (multiLine)
  212. sb.Append (Environment.NewLine);
  213. return sb.ToString ();
  214. }
  215. }
  216. }
  217. #endif