* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509CertificateCollection.cs
1 //
2 // System.Security.Cryptography.X509Certificates.X509CertificateCollection
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Globalization;
35 using System.Security.Cryptography;
36
37 namespace System.Security.Cryptography.X509Certificates {
38
39 [Serializable]
40 public class X509CertificateCollection : CollectionBase, IEnumerable {
41         
42         public X509CertificateCollection ()
43         {
44         }
45         
46         public X509CertificateCollection (X509Certificate [] value) 
47         {
48                 AddRange (value);
49         }
50         
51         public X509CertificateCollection (X509CertificateCollection value)
52         {
53                 AddRange (value);
54         }
55         
56         // Properties
57         
58         public X509Certificate this [int index] {
59                 get { return (X509Certificate) InnerList [index]; }
60                 set { InnerList [index] = value; }
61         }
62         
63         // Methods
64
65         public int Add (X509Certificate value)
66         {
67                 if (value == null)
68                         throw new ArgumentNullException ("value");
69                 
70                 return InnerList.Add (value);
71         }
72         
73         public void AddRange (X509Certificate [] value) 
74         {
75                 if (value == null)
76                         throw new ArgumentNullException ("value");
77
78                 for (int i = 0; i < value.Length; i++) 
79                         InnerList.Add (value [i]);
80         }
81         
82         public void AddRange (X509CertificateCollection value)
83         {
84                 if (value == null)
85                         throw new ArgumentNullException ("value");
86
87                 for (int i = 0; i < value.InnerList.Count; i++) 
88                         InnerList.Add (value [i]);
89         }
90         
91         public bool Contains (X509Certificate value) 
92         {
93                 if (value == null)
94                         return false;
95
96                 byte[] hash = value.GetCertHash ();
97                 for (int i=0; i < InnerList.Count; i++) {
98                         X509Certificate x509 = (X509Certificate) InnerList [i];
99                         if (Compare (x509.GetCertHash (), hash))
100                                 return true;
101                 }
102                 return false;
103         }
104
105         public void CopyTo (X509Certificate[] array, int index)
106         {
107                 InnerList.CopyTo (array, index);
108         }
109         
110         public new X509CertificateEnumerator GetEnumerator ()
111         {
112                 return new X509CertificateEnumerator (this);
113         }
114         
115         IEnumerator IEnumerable.GetEnumerator ()
116         {
117                 return InnerList.GetEnumerator ();
118         }
119         
120         public override int GetHashCode () 
121         {
122                 return InnerList.GetHashCode ();
123         }
124         
125         public int IndexOf (X509Certificate value)
126         {
127                 return InnerList.IndexOf (value);
128         }
129         
130         public void Insert (int index, X509Certificate value)
131         {
132                 InnerList.Insert (index, value);
133         }
134         
135         public void Remove (X509Certificate value)
136         {
137                 if (value == null)
138                         throw new ArgumentNullException ("value");
139                 if (IndexOf (value) == -1) {
140                         throw new ArgumentException ("value", 
141                                 Locale.GetText ("Not part of the collection."));
142                 }
143
144                 InnerList.Remove (value);
145         }
146
147         // private stuff
148
149         private bool Compare (byte[] array1, byte[] array2) 
150         {
151                 if ((array1 == null) && (array2 == null))
152                         return true;
153                 if ((array1 == null) || (array2 == null))
154                         return false;
155                 if (array1.Length != array2.Length)
156                         return false;
157                 for (int i=0; i < array1.Length; i++) {
158                         if (array1 [i] != array2 [i])
159                                 return false;
160                 }
161                 return true;
162         }
163
164         // Inner Class
165         
166         public class X509CertificateEnumerator : IEnumerator {
167
168                 private IEnumerator enumerator;
169
170                 // Constructors
171                 
172                 public X509CertificateEnumerator (X509CertificateCollection mappings)
173                 {
174                         enumerator = ((IEnumerable) mappings).GetEnumerator ();
175                 }
176
177                 // Properties
178                 
179                 public X509Certificate Current {
180                         get { return (X509Certificate) enumerator.Current; }
181                 }
182                 
183                 object IEnumerator.Current {
184                         get { return enumerator.Current; }
185                 }
186
187                 // Methods
188                 
189                 bool IEnumerator.MoveNext ()
190                 {
191                         return enumerator.MoveNext ();
192                 }
193                 
194                 void IEnumerator.Reset () 
195                 {
196                         enumerator.Reset ();
197                 }
198                 
199                 public bool MoveNext () 
200                 {
201                         return enumerator.MoveNext ();
202                 }
203                 
204                 public void Reset ()
205                 {
206                         enumerator.Reset ();
207                 }
208         }               
209 }
210
211 }
212