X509EnhancedKeyUsageExtension.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // System.Security.Cryptography.X509EnhancedKeyUsageExtension
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System.Text;
  30. using Mono.Security;
  31. namespace System.Security.Cryptography.X509Certificates {
  32. public sealed class X509EnhancedKeyUsageExtension : X509Extension {
  33. internal const string oid = "2.5.29.37";
  34. internal const string friendlyName = "Enhanced Key Usage";
  35. private OidCollection _enhKeyUsage;
  36. private AsnDecodeStatus _status;
  37. // constructors
  38. public X509EnhancedKeyUsageExtension ()
  39. {
  40. _oid = new Oid (oid, friendlyName);
  41. }
  42. public X509EnhancedKeyUsageExtension (AsnEncodedData encodedEnhancedKeyUsages, bool critical)
  43. {
  44. // ignore the Oid provided by encodedKeyUsage (our rules!)
  45. _oid = new Oid (oid, friendlyName);
  46. _raw = encodedEnhancedKeyUsages.RawData;
  47. base.Critical = critical;
  48. _status = Decode (this.RawData);
  49. }
  50. public X509EnhancedKeyUsageExtension (OidCollection enhancedKeyUsages, bool critical)
  51. {
  52. if (enhancedKeyUsages == null)
  53. throw new ArgumentNullException ("enhancedKeyUsages");
  54. _oid = new Oid (oid, friendlyName);
  55. base.Critical = critical;
  56. _enhKeyUsage = enhancedKeyUsages.ReadOnlyCopy ();
  57. RawData = Encode ();
  58. }
  59. // properties
  60. public OidCollection EnhancedKeyUsages {
  61. get {
  62. switch (_status) {
  63. case AsnDecodeStatus.Ok:
  64. case AsnDecodeStatus.InformationNotAvailable:
  65. _enhKeyUsage.ReadOnly = true;
  66. return _enhKeyUsage;
  67. default:
  68. throw new CryptographicException ("Badly encoded extension.");
  69. }
  70. }
  71. }
  72. // methods
  73. public override void CopyFrom (AsnEncodedData asnEncodedData)
  74. {
  75. if (asnEncodedData == null)
  76. throw new ArgumentException ("encodedData");
  77. // MS BUG throw new ArgumentNullException ("encodedData");
  78. X509Extension ex = (asnEncodedData as X509Extension);
  79. if (ex == null)
  80. throw new ArgumentException (Locale.GetText ("Wrong type."), "asnEncodedData");
  81. if (ex._oid == null)
  82. _oid = new Oid (oid, friendlyName);
  83. else
  84. _oid = new Oid (ex._oid);
  85. RawData = ex.RawData;
  86. base.Critical = ex.Critical;
  87. // and we deal with the rest later
  88. _status = Decode (this.RawData);
  89. }
  90. // internal
  91. internal AsnDecodeStatus Decode (byte[] extension)
  92. {
  93. if ((extension == null) || (extension.Length == 0))
  94. return AsnDecodeStatus.BadAsn;
  95. if (extension [0] != 0x30)
  96. return AsnDecodeStatus.BadTag;
  97. if (_enhKeyUsage == null)
  98. _enhKeyUsage = new OidCollection ();
  99. try {
  100. ASN1 ex = new ASN1 (extension);
  101. if (ex.Tag != 0x30)
  102. throw new CryptographicException (Locale.GetText ("Invalid ASN.1 Tag"));
  103. for (int i=0; i < ex.Count; i++) {
  104. _enhKeyUsage.Add (new Oid (ASN1Convert.ToOid (ex [i])));
  105. }
  106. }
  107. catch {
  108. return AsnDecodeStatus.BadAsn;
  109. }
  110. return AsnDecodeStatus.Ok;
  111. }
  112. internal byte[] Encode ()
  113. {
  114. ASN1 ex = new ASN1 (0x30);
  115. foreach (Oid oid in _enhKeyUsage) {
  116. ex.Add (ASN1Convert.FromOid (oid.Value));
  117. }
  118. return ex.GetBytes ();
  119. }
  120. internal override string ToString (bool multiLine)
  121. {
  122. switch (_status) {
  123. case AsnDecodeStatus.BadAsn:
  124. return String.Empty;
  125. case AsnDecodeStatus.BadTag:
  126. case AsnDecodeStatus.BadLength:
  127. return FormatUnkownData (_raw);
  128. case AsnDecodeStatus.InformationNotAvailable:
  129. return "Information Not Available";
  130. }
  131. if (_oid.Value != oid)
  132. return String.Format ("Unknown Key Usage ({0})", _oid.Value);
  133. if (_enhKeyUsage.Count == 0)
  134. return "Information Not Available";
  135. StringBuilder sb = new StringBuilder ();
  136. for (int i=0; i < _enhKeyUsage.Count; i++) {
  137. Oid o = _enhKeyUsage [i];
  138. switch (o.Value) {
  139. case "1.3.6.1.5.5.7.3.1":
  140. sb.Append ("Server Authentication (");
  141. break;
  142. default:
  143. sb.Append ("Unknown Key Usage (");
  144. break;
  145. }
  146. sb.Append (o.Value);
  147. sb.Append (")");
  148. if (multiLine)
  149. sb.Append (Environment.NewLine);
  150. else if (i != (_enhKeyUsage.Count - 1))
  151. sb.Append (", ");
  152. }
  153. return sb.ToString ();
  154. }
  155. }
  156. }
  157. #endif