X509CertificateCollection.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // Based on System.Security.Cryptography.X509Certificates.X509CertificateCollection
  3. // in System assembly
  4. //
  5. // Authors:
  6. // Lawrence Pit (loz@cable.a2000.nl)
  7. // Sebastien Pouliot <sebastien@ximian.com>
  8. //
  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.Collections;
  31. namespace MediaBrowser.Server.Mono.Security {
  32. [Serializable]
  33. public class X509CertificateCollection : CollectionBase, IEnumerable {
  34. public X509CertificateCollection ()
  35. {
  36. }
  37. public X509CertificateCollection (X509Certificate [] value)
  38. {
  39. AddRange (value);
  40. }
  41. public X509CertificateCollection (X509CertificateCollection value)
  42. {
  43. AddRange (value);
  44. }
  45. // Properties
  46. public X509Certificate this [int index] {
  47. get { return (X509Certificate) InnerList [index]; }
  48. set { InnerList [index] = value; }
  49. }
  50. // Methods
  51. public int Add (X509Certificate value)
  52. {
  53. if (value == null)
  54. throw new ArgumentNullException ("value");
  55. return InnerList.Add (value);
  56. }
  57. public void AddRange (X509Certificate [] value)
  58. {
  59. if (value == null)
  60. throw new ArgumentNullException ("value");
  61. for (int i = 0; i < value.Length; i++)
  62. InnerList.Add (value [i]);
  63. }
  64. public void AddRange (X509CertificateCollection value)
  65. {
  66. if (value == null)
  67. throw new ArgumentNullException ("value");
  68. for (int i = 0; i < value.InnerList.Count; i++)
  69. InnerList.Add (value [i]);
  70. }
  71. public bool Contains (X509Certificate value)
  72. {
  73. return (IndexOf (value) != -1);
  74. }
  75. public void CopyTo (X509Certificate[] array, int index)
  76. {
  77. InnerList.CopyTo (array, index);
  78. }
  79. public new X509CertificateEnumerator GetEnumerator ()
  80. {
  81. return new X509CertificateEnumerator (this);
  82. }
  83. IEnumerator IEnumerable.GetEnumerator ()
  84. {
  85. return InnerList.GetEnumerator ();
  86. }
  87. public override int GetHashCode ()
  88. {
  89. return InnerList.GetHashCode ();
  90. }
  91. public int IndexOf (X509Certificate value)
  92. {
  93. if (value == null)
  94. throw new ArgumentNullException ("value");
  95. byte[] hash = value.Hash;
  96. for (int i=0; i < InnerList.Count; i++) {
  97. X509Certificate x509 = (X509Certificate) InnerList [i];
  98. if (Compare (x509.Hash, hash))
  99. return i;
  100. }
  101. return -1;
  102. }
  103. public void Insert (int index, X509Certificate value)
  104. {
  105. InnerList.Insert (index, value);
  106. }
  107. public void Remove (X509Certificate value)
  108. {
  109. InnerList.Remove (value);
  110. }
  111. // private stuff
  112. private bool Compare (byte[] array1, byte[] array2)
  113. {
  114. if ((array1 == null) && (array2 == null))
  115. return true;
  116. if ((array1 == null) || (array2 == null))
  117. return false;
  118. if (array1.Length != array2.Length)
  119. return false;
  120. for (int i=0; i < array1.Length; i++) {
  121. if (array1 [i] != array2 [i])
  122. return false;
  123. }
  124. return true;
  125. }
  126. // Inner Class
  127. public class X509CertificateEnumerator : IEnumerator {
  128. private IEnumerator enumerator;
  129. // Constructors
  130. public X509CertificateEnumerator (X509CertificateCollection mappings)
  131. {
  132. enumerator = ((IEnumerable) mappings).GetEnumerator ();
  133. }
  134. // Properties
  135. public X509Certificate Current {
  136. get { return (X509Certificate) enumerator.Current; }
  137. }
  138. object IEnumerator.Current {
  139. get { return enumerator.Current; }
  140. }
  141. // Methods
  142. bool IEnumerator.MoveNext ()
  143. {
  144. return enumerator.MoveNext ();
  145. }
  146. void IEnumerator.Reset ()
  147. {
  148. enumerator.Reset ();
  149. }
  150. public bool MoveNext ()
  151. {
  152. return enumerator.MoveNext ();
  153. }
  154. public void Reset ()
  155. {
  156. enumerator.Reset ();
  157. }
  158. }
  159. }
  160. }