New test.
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509CertificateCollection.cs
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 //
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
31 using System;
32 using System.Collections;
33
34 namespace Mono.Security.X509 {
35
36         [Serializable]
37 #if INSIDE_CORLIB
38         internal
39 #else
40         public 
41 #endif
42         class X509CertificateCollection : CollectionBase, IEnumerable {
43                 
44                 public X509CertificateCollection () 
45                 {
46                 }
47                 
48                 public X509CertificateCollection (X509Certificate [] value) 
49                 {
50                         AddRange (value);
51                 }
52                 
53                 public X509CertificateCollection (X509CertificateCollection value)
54                 {
55                         AddRange (value);
56                 }
57                 
58                 // Properties
59                 
60                 public X509Certificate this [int index] {
61                         get { return (X509Certificate) InnerList [index]; }
62                         set { InnerList [index] = value; }
63                 }
64                 
65                 // Methods
66
67                 public int Add (X509Certificate value)
68                 {
69                         if (value == null)
70                                 throw new ArgumentNullException ("value");
71                         
72                         return InnerList.Add (value);
73                 }
74                 
75                 public void AddRange (X509Certificate [] value) 
76                 {
77                         if (value == null)
78                                 throw new ArgumentNullException ("value");
79
80                         for (int i = 0; i < value.Length; i++) 
81                                 InnerList.Add (value [i]);
82                 }
83                 
84                 public void AddRange (X509CertificateCollection value)
85                 {
86                         if (value == null)
87                                 throw new ArgumentNullException ("value");
88
89                         for (int i = 0; i < value.InnerList.Count; i++) 
90                                 InnerList.Add (value [i]);
91                 }
92                 
93                 public bool Contains (X509Certificate value) 
94                 {
95                         return (IndexOf (value) != -1);
96                 }
97
98                 public void CopyTo (X509Certificate[] array, int index)
99                 {
100                         InnerList.CopyTo (array, index);
101                 }
102                 
103                 public new X509CertificateEnumerator GetEnumerator ()
104                 {
105                         return new X509CertificateEnumerator (this);
106                 }
107                 
108                 IEnumerator IEnumerable.GetEnumerator ()
109                 {
110                         return InnerList.GetEnumerator ();
111                 }
112                 
113                 public override int GetHashCode () 
114                 {
115                         return InnerList.GetHashCode ();
116                 }
117                 
118                 public int IndexOf (X509Certificate value)
119                 {
120                         if (value == null)
121                                 throw new ArgumentNullException ("value");
122
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;
128                         }
129                         return -1;
130                 }
131                 
132                 public void Insert (int index, X509Certificate value)
133                 {
134                         InnerList.Insert (index, value);
135                 }
136                 
137                 public void Remove (X509Certificate value)
138                 {
139                         InnerList.Remove (value);
140                 }
141
142                 // private stuff
143
144                 private bool Compare (byte[] array1, byte[] array2) 
145                 {
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;
155                         }
156                         return true;
157                 }
158
159                 // Inner Class
160                 
161                 public class X509CertificateEnumerator : IEnumerator {
162
163                         private IEnumerator enumerator;
164
165                         // Constructors
166                         
167                         public X509CertificateEnumerator (X509CertificateCollection mappings)
168                         {
169                                 enumerator = ((IEnumerable) mappings).GetEnumerator ();
170                         }
171
172                         // Properties
173                         
174                         public X509Certificate Current {
175                                 get { return (X509Certificate) enumerator.Current; }
176                         }
177                         
178                         object IEnumerator.Current {
179                                 get { return enumerator.Current; }
180                         }
181
182                         // Methods
183                         
184                         bool IEnumerator.MoveNext ()
185                         {
186                                 return enumerator.MoveNext ();
187                         }
188                         
189                         void IEnumerator.Reset () 
190                         {
191                                 enumerator.Reset ();
192                         }
193                         
194                         public bool MoveNext () 
195                         {
196                                 return enumerator.MoveNext ();
197                         }
198                         
199                         public void Reset ()
200                         {
201                                 enumerator.Reset ();
202                         }
203                 }               
204         }
205 }