X509Extension.cs 5.2 KB

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