**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509CertificateCollection.cs
blob3fd834b0cf6c275f22826ab8924c8f072172c8eb
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 //
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.
31 using System;
32 using System.Collections;
34 namespace Mono.Security.X509 {
36 [Serializable]
37 #if INSIDE_CORLIB
38 internal
39 #else
40 public
41 #endif
42 class X509CertificateCollection : CollectionBase, IEnumerable {
44 public X509CertificateCollection ()
48 public X509CertificateCollection (X509Certificate [] value)
50 AddRange (value);
53 public X509CertificateCollection (X509CertificateCollection value)
55 AddRange (value);
58 // Properties
60 public X509Certificate this [int index] {
61 get { return (X509Certificate) InnerList [index]; }
62 set { InnerList [index] = value; }
65 // Methods
67 public int Add (X509Certificate value)
69 if (value == null)
70 throw new ArgumentNullException ("value");
72 return InnerList.Add (value);
75 public void AddRange (X509Certificate [] value)
77 if (value == null)
78 throw new ArgumentNullException ("value");
80 for (int i = 0; i < value.Length; i++)
81 InnerList.Add (value [i]);
84 public void AddRange (X509CertificateCollection value)
86 if (value == null)
87 throw new ArgumentNullException ("value");
89 for (int i = 0; i < value.InnerList.Count; i++)
90 InnerList.Add (value [i]);
93 public bool Contains (X509Certificate value)
95 return (IndexOf (value) != -1);
98 public void CopyTo (X509Certificate[] array, int index)
100 InnerList.CopyTo (array, index);
103 public new X509CertificateEnumerator GetEnumerator ()
105 return new X509CertificateEnumerator (this);
108 IEnumerator IEnumerable.GetEnumerator ()
110 return InnerList.GetEnumerator ();
113 public override int GetHashCode ()
115 return InnerList.GetHashCode ();
118 public int IndexOf (X509Certificate value)
120 if (value == null)
121 throw new ArgumentNullException ("value");
123 byte[] hash = value.Hash;
124 for (int i=0; i < InnerList.Count; i++) {
125 X509Certificate x509 = (X509Certificate) InnerList [i];
126 if (Compare (x509.Hash, hash))
127 return i;
129 return -1;
132 public void Insert (int index, X509Certificate value)
134 InnerList.Insert (index, value);
137 public void Remove (X509Certificate value)
139 InnerList.Remove (value);
142 // private stuff
144 private bool Compare (byte[] array1, byte[] array2)
146 if ((array1 == null) && (array2 == null))
147 return true;
148 if ((array1 == null) || (array2 == null))
149 return false;
150 if (array1.Length != array2.Length)
151 return false;
152 for (int i=0; i < array1.Length; i++) {
153 if (array1 [i] != array2 [i])
154 return false;
156 return true;
159 // Inner Class
161 public class X509CertificateEnumerator : IEnumerator {
163 private IEnumerator enumerator;
165 // Constructors
167 public X509CertificateEnumerator (X509CertificateCollection mappings)
169 enumerator = ((IEnumerable) mappings).GetEnumerator ();
172 // Properties
174 public X509Certificate Current {
175 get { return (X509Certificate) enumerator.Current; }
178 object IEnumerator.Current {
179 get { return enumerator.Current; }
182 // Methods
184 bool IEnumerator.MoveNext ()
186 return enumerator.MoveNext ();
189 void IEnumerator.Reset ()
191 enumerator.Reset ();
194 public bool MoveNext ()
196 return enumerator.MoveNext ();
199 public void Reset ()
201 enumerator.Reset ();