X509SubjectKeyIdentifierExtension.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension
  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. using Mono.Security.Cryptography;
  34. namespace System.Security.Cryptography.X509Certificates {
  35. public sealed class X509SubjectKeyIdentifierExtension : X509Extension {
  36. internal const string oid = "2.5.29.14";
  37. internal const string friendlyName = "Subject Key Identifier";
  38. private byte[] _subjectKeyIdentifier;
  39. private AsnDecodeStatus _status;
  40. // constructors
  41. public X509SubjectKeyIdentifierExtension ()
  42. {
  43. _oid = new Oid (oid, friendlyName);
  44. }
  45. public X509SubjectKeyIdentifierExtension (AsnEncodedData encodedSubjectKeyIdentifier, bool critical)
  46. {
  47. // ignore the Oid provided by encodedKeyUsage (our rules!)
  48. _oid = new Oid (oid, friendlyName);
  49. _raw = encodedSubjectKeyIdentifier.RawData;
  50. base.Critical = critical;
  51. _status = Decode (this.RawData);
  52. }
  53. public X509SubjectKeyIdentifierExtension (byte[] subjectKeyIdentifier, bool critical)
  54. {
  55. if (subjectKeyIdentifier == null)
  56. throw new ArgumentNullException ("subjectKeyIdentifier");
  57. if (subjectKeyIdentifier.Length == 0)
  58. throw new ArgumentException ("subjectKeyIdentifier");
  59. _oid = new Oid (oid, friendlyName);
  60. base.Critical = critical;
  61. _subjectKeyIdentifier = (byte[])subjectKeyIdentifier.Clone ();
  62. RawData = Encode ();
  63. }
  64. public X509SubjectKeyIdentifierExtension (string subjectKeyIdentifier, bool critical)
  65. {
  66. if (subjectKeyIdentifier == null)
  67. throw new ArgumentNullException ("subjectKeyIdentifier");
  68. if (subjectKeyIdentifier.Length < 2)
  69. throw new ArgumentException ("subjectKeyIdentifier");
  70. _oid = new Oid (oid, friendlyName);
  71. base.Critical = critical;
  72. _subjectKeyIdentifier = FromHex (subjectKeyIdentifier);
  73. RawData = Encode ();
  74. }
  75. [MonoTODO]
  76. public X509SubjectKeyIdentifierExtension (PublicKey key, bool critical)
  77. : this (key, X509SubjectKeyIdentifierHashAlgorithm.Sha1, critical)
  78. {
  79. }
  80. [MonoTODO]
  81. public X509SubjectKeyIdentifierExtension (PublicKey key, X509SubjectKeyIdentifierHashAlgorithm algorithm, bool critical)
  82. {
  83. if (key == null)
  84. throw new ArgumentNullException ("key");
  85. }
  86. // properties
  87. public string SubjectKeyIdentifier {
  88. get {
  89. switch (_status) {
  90. case AsnDecodeStatus.Ok:
  91. case AsnDecodeStatus.InformationNotAvailable:
  92. if (_subjectKeyIdentifier == null)
  93. return String.Empty;
  94. return CryptoConvert.ToHex (_subjectKeyIdentifier);
  95. default:
  96. throw new CryptographicException ("Badly encoded extension.");
  97. }
  98. }
  99. }
  100. // methods
  101. public override void CopyFrom (AsnEncodedData encodedData)
  102. {
  103. if (encodedData == null)
  104. throw new ArgumentException ("encodedData");
  105. // MS BUG throw new ArgumentNullException ("encodedData");
  106. X509Extension ex = (encodedData as X509Extension);
  107. if (ex == null)
  108. throw new ArgumentException (Locale.GetText ("Wrong type."), "encodedData");
  109. if (ex._oid == null)
  110. _oid = new Oid (oid, friendlyName);
  111. else
  112. _oid = new Oid (ex._oid);
  113. RawData = ex.RawData;
  114. base.Critical = ex.Critical;
  115. // and we deal with the rest later
  116. _status = Decode (this.RawData);
  117. }
  118. // internal
  119. static internal byte FromHexChar (char c)
  120. {
  121. if ((c >= 'a') && (c <= 'f'))
  122. return (byte) (c - 'a' + 10);
  123. if ((c >= 'A') && (c <= 'F'))
  124. return (byte) (c - 'A' + 10);
  125. if ((c >= '0') && (c <= '9'))
  126. return (byte) (c - '0');
  127. return 255; // F
  128. }
  129. static internal byte FromHexChars (char c1, char c2)
  130. {
  131. byte result = FromHexChar (c1);
  132. if (result < 255)
  133. result = (byte) ((result << 4) | FromHexChar (c2));
  134. return result;
  135. }
  136. static internal byte[] FromHex (string hex)
  137. {
  138. // here we can't use CryptoConvert.FromHex because we
  139. // must convert any *illegal* (non hex) 2 characters
  140. // to 'FF' and ignore last char on odd length
  141. if (hex == null)
  142. return null;
  143. int length = hex.Length >> 1;
  144. byte[] result = new byte [length]; // + (odd ? 1 : 0)];
  145. int n = 0;
  146. int i = 0;
  147. while (n < length) {
  148. result [n++] = FromHexChars (hex [i++], hex [i++]);
  149. }
  150. return result;
  151. }
  152. internal AsnDecodeStatus Decode (byte[] extension)
  153. {
  154. if ((extension == null) || (extension.Length == 0))
  155. return AsnDecodeStatus.BadAsn;
  156. if (extension [0] != 0x04)
  157. return AsnDecodeStatus.BadTag;
  158. if (extension.Length == 2)
  159. return AsnDecodeStatus.InformationNotAvailable;
  160. if (extension.Length < 3)
  161. return AsnDecodeStatus.BadLength;
  162. try {
  163. ASN1 ex = new ASN1 (extension);
  164. _subjectKeyIdentifier = ex.Value;
  165. }
  166. catch {
  167. return AsnDecodeStatus.BadAsn;
  168. }
  169. return AsnDecodeStatus.Ok;
  170. }
  171. internal byte[] Encode ()
  172. {
  173. ASN1 ex = new ASN1 (0x04, _subjectKeyIdentifier);
  174. return ex.GetBytes ();
  175. }
  176. internal override string ToString (bool multiLine)
  177. {
  178. switch (_status) {
  179. case AsnDecodeStatus.BadAsn:
  180. return String.Empty;
  181. case AsnDecodeStatus.BadTag:
  182. case AsnDecodeStatus.BadLength:
  183. return FormatUnkownData (_raw);
  184. case AsnDecodeStatus.InformationNotAvailable:
  185. return "Information Not Available";
  186. }
  187. if (_oid.Value != oid)
  188. return String.Format ("Unknown Key Usage ({0})", _oid.Value);
  189. StringBuilder sb = new StringBuilder ();
  190. for (int i=0; i < _subjectKeyIdentifier.Length; i++) {
  191. sb.Append (_subjectKeyIdentifier [i].ToString ("x2"));
  192. if (i != _subjectKeyIdentifier.Length - 1)
  193. sb.Append (" ");
  194. }
  195. if (multiLine)
  196. sb.Append (Environment.NewLine);
  197. return sb.ToString ();
  198. }
  199. }
  200. }
  201. #endif