X509Extension.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // X509Extension.cs: Base class for all X.509 extensions.
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  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. using System;
  30. using System.Globalization;
  31. using System.Text;
  32. using Mono.Security;
  33. namespace Mono.Security.X509 {
  34. /*
  35. * Extension ::= SEQUENCE {
  36. * extnID OBJECT IDENTIFIER,
  37. * critical BOOLEAN DEFAULT FALSE,
  38. * extnValue OCTET STRING
  39. * }
  40. */
  41. #if INSIDE_CORLIB
  42. internal
  43. #else
  44. public
  45. #endif
  46. class X509Extension {
  47. protected string extnOid;
  48. protected bool extnCritical;
  49. protected ASN1 extnValue;
  50. protected X509Extension ()
  51. {
  52. extnCritical = false;
  53. }
  54. public X509Extension (ASN1 asn1)
  55. {
  56. if ((asn1.Tag != 0x30) || (asn1.Count < 2))
  57. throw new ArgumentException (("Invalid X.509 extension."));
  58. if (asn1[0].Tag != 0x06)
  59. throw new ArgumentException (("Invalid X.509 extension."));
  60. extnOid = ASN1Convert.ToOid (asn1[0]);
  61. extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
  62. // last element is an octet string which may need to be decoded
  63. extnValue = asn1 [asn1.Count - 1];
  64. if ((extnValue.Tag == 0x04) && (extnValue.Length > 0) && (extnValue.Count == 0)) {
  65. try {
  66. ASN1 encapsulated = new ASN1 (extnValue.Value);
  67. extnValue.Value = null;
  68. extnValue.Add (encapsulated);
  69. }
  70. catch {
  71. // data isn't ASN.1
  72. }
  73. }
  74. Decode ();
  75. }
  76. public X509Extension (X509Extension extension)
  77. {
  78. if (extension == null)
  79. throw new ArgumentNullException ("extension");
  80. if ((extension.Value == null) || (extension.Value.Tag != 0x04) || (extension.Value.Count != 1))
  81. throw new ArgumentException (("Invalid X.509 extension."));
  82. extnOid = extension.Oid;
  83. extnCritical = extension.Critical;
  84. extnValue = extension.Value;
  85. Decode ();
  86. }
  87. // encode the extension *into* an OCTET STRING
  88. protected virtual void Decode ()
  89. {
  90. }
  91. // decode the extension from *inside* an OCTET STRING
  92. protected virtual void Encode ()
  93. {
  94. }
  95. public ASN1 ASN1 {
  96. get {
  97. ASN1 extension = new ASN1 (0x30);
  98. extension.Add (ASN1Convert.FromOid (extnOid));
  99. if (extnCritical)
  100. extension.Add (new ASN1 (0x01, new byte [1] { 0xFF }));
  101. Encode ();
  102. extension.Add (extnValue);
  103. return extension;
  104. }
  105. }
  106. public string Oid {
  107. get { return extnOid; }
  108. }
  109. public bool Critical {
  110. get { return extnCritical; }
  111. set { extnCritical = value; }
  112. }
  113. // this gets overrided with more meaningful names
  114. public virtual string Name {
  115. get { return extnOid; }
  116. }
  117. public ASN1 Value {
  118. get {
  119. if (extnValue == null) {
  120. Encode ();
  121. }
  122. return extnValue;
  123. }
  124. }
  125. public override bool Equals (object obj)
  126. {
  127. if (obj == null)
  128. return false;
  129. X509Extension ex = (obj as X509Extension);
  130. if (ex == null)
  131. return false;
  132. if (extnCritical != ex.extnCritical)
  133. return false;
  134. if (extnOid != ex.extnOid)
  135. return false;
  136. if (extnValue.Length != ex.extnValue.Length)
  137. return false;
  138. for (int i=0; i < extnValue.Length; i++) {
  139. if (extnValue [i] != ex.extnValue [i])
  140. return false;
  141. }
  142. return true;
  143. }
  144. public byte[] GetBytes ()
  145. {
  146. return ASN1.GetBytes ();
  147. }
  148. public override int GetHashCode ()
  149. {
  150. // OID should be unique in a collection of extensions
  151. return extnOid.GetHashCode ();
  152. }
  153. private void WriteLine (StringBuilder sb, int n, int pos)
  154. {
  155. byte[] value = extnValue.Value;
  156. int p = pos;
  157. for (int j=0; j < 8; j++) {
  158. if (j < n) {
  159. sb.Append (value [p++].ToString ("X2", CultureInfo.InvariantCulture));
  160. sb.Append (" ");
  161. }
  162. else
  163. sb.Append (" ");
  164. }
  165. sb.Append (" ");
  166. p = pos;
  167. for (int j=0; j < n; j++) {
  168. byte b = value [p++];
  169. if (b < 0x20)
  170. sb.Append (".");
  171. else
  172. sb.Append (Convert.ToChar (b));
  173. }
  174. sb.Append (Environment.NewLine);
  175. }
  176. public override string ToString ()
  177. {
  178. StringBuilder sb = new StringBuilder ();
  179. int div = (extnValue.Length >> 3);
  180. int rem = (extnValue.Length - (div << 3));
  181. int x = 0;
  182. for (int i=0; i < div; i++) {
  183. WriteLine (sb, 8, x);
  184. x += 8;
  185. }
  186. WriteLine (sb, rem, x);
  187. return sb.ToString ();
  188. }
  189. }
  190. }