X509Extensions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // X509Extensions.cs: Handles X.509 extensions.
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  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. using System;
  31. using System.Collections;
  32. namespace Emby.Server.Core.Cryptography
  33. {
  34. /*
  35. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  36. *
  37. * Note: 1..MAX -> There shouldn't be 0 Extensions in the ASN1 structure
  38. */
  39. public sealed class X509ExtensionCollection : CollectionBase, IEnumerable {
  40. private bool readOnly;
  41. public X509ExtensionCollection () : base ()
  42. {
  43. }
  44. public X509ExtensionCollection (ASN1 asn1) : this ()
  45. {
  46. readOnly = true;
  47. if (asn1 == null)
  48. return;
  49. if (asn1.Tag != 0x30)
  50. throw new Exception ("Invalid extensions format");
  51. for (int i=0; i < asn1.Count; i++) {
  52. X509Extension extension = new X509Extension (asn1 [i]);
  53. InnerList.Add (extension);
  54. }
  55. }
  56. public int Add (X509Extension extension)
  57. {
  58. if (extension == null)
  59. throw new ArgumentNullException ("extension");
  60. if (readOnly)
  61. throw new NotSupportedException ("Extensions are read only");
  62. return InnerList.Add (extension);
  63. }
  64. public void AddRange (X509Extension[] extension)
  65. {
  66. if (extension == null)
  67. throw new ArgumentNullException ("extension");
  68. if (readOnly)
  69. throw new NotSupportedException ("Extensions are read only");
  70. for (int i = 0; i < extension.Length; i++)
  71. InnerList.Add (extension [i]);
  72. }
  73. public void AddRange (X509ExtensionCollection collection)
  74. {
  75. if (collection == null)
  76. throw new ArgumentNullException ("collection");
  77. if (readOnly)
  78. throw new NotSupportedException ("Extensions are read only");
  79. for (int i = 0; i < collection.InnerList.Count; i++)
  80. InnerList.Add (collection [i]);
  81. }
  82. public bool Contains (X509Extension extension)
  83. {
  84. return (IndexOf (extension) != -1);
  85. }
  86. public bool Contains (string oid)
  87. {
  88. return (IndexOf (oid) != -1);
  89. }
  90. public void CopyTo (X509Extension[] extensions, int index)
  91. {
  92. if (extensions == null)
  93. throw new ArgumentNullException ("extensions");
  94. InnerList.CopyTo (extensions, index);
  95. }
  96. public int IndexOf (X509Extension extension)
  97. {
  98. if (extension == null)
  99. throw new ArgumentNullException ("extension");
  100. for (int i=0; i < InnerList.Count; i++) {
  101. X509Extension ex = (X509Extension) InnerList [i];
  102. if (ex.Equals (extension))
  103. return i;
  104. }
  105. return -1;
  106. }
  107. public int IndexOf (string oid)
  108. {
  109. if (oid == null)
  110. throw new ArgumentNullException ("oid");
  111. for (int i=0; i < InnerList.Count; i++) {
  112. X509Extension ex = (X509Extension) InnerList [i];
  113. if (ex.Oid == oid)
  114. return i;
  115. }
  116. return -1;
  117. }
  118. public void Insert (int index, X509Extension extension)
  119. {
  120. if (extension == null)
  121. throw new ArgumentNullException ("extension");
  122. InnerList.Insert (index, extension);
  123. }
  124. public void Remove (X509Extension extension)
  125. {
  126. if (extension == null)
  127. throw new ArgumentNullException ("extension");
  128. InnerList.Remove (extension);
  129. }
  130. public void Remove (string oid)
  131. {
  132. if (oid == null)
  133. throw new ArgumentNullException ("oid");
  134. int index = IndexOf (oid);
  135. if (index != -1)
  136. InnerList.RemoveAt (index);
  137. }
  138. IEnumerator IEnumerable.GetEnumerator ()
  139. {
  140. return InnerList.GetEnumerator ();
  141. }
  142. public X509Extension this [int index] {
  143. get { return (X509Extension) InnerList [index]; }
  144. }
  145. public X509Extension this [string oid] {
  146. get {
  147. int index = IndexOf (oid);
  148. if (index == -1)
  149. return null;
  150. return (X509Extension) InnerList [index];
  151. }
  152. }
  153. public byte[] GetBytes ()
  154. {
  155. if (InnerList.Count < 1)
  156. return null;
  157. ASN1 sequence = new ASN1 (0x30);
  158. for (int i=0; i < InnerList.Count; i++) {
  159. X509Extension x = (X509Extension) InnerList [i];
  160. sequence.Add (x.ASN1);
  161. }
  162. return sequence.GetBytes ();
  163. }
  164. }
  165. }