X509CertificateCollection.cs 4.6 KB

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