Use the right socket for the data stream
[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 {
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         public override int GetHashCode () 
116         {
117                 return InnerList.GetHashCode ();
118         }
119         
120         public int IndexOf (X509Certificate value)
121         {
122                 return InnerList.IndexOf (value);
123         }
124         
125         public void Insert (int index, X509Certificate value)
126         {
127                 InnerList.Insert (index, value);
128         }
129         
130         public void Remove (X509Certificate value)
131         {
132                 if (value == null)
133                         throw new ArgumentNullException ("value");
134                 if (IndexOf (value) == -1) {
135                         throw new ArgumentException ("value", 
136                                 Locale.GetText ("Not part of the collection."));
137                 }
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
206 }
207