X509CertificateCollection.cs 4.6 KB

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