**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509CertificateCollection.cs
blobed191767fc0841986d1a6babb69ef8fa99192d5d
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 //
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.
32 using System;
33 using System.Collections;
34 using System.Globalization;
35 using System.Security.Cryptography;
37 namespace System.Security.Cryptography.X509Certificates {
39 [Serializable]
40 public class X509CertificateCollection : CollectionBase, IEnumerable {
42 public X509CertificateCollection ()
46 public X509CertificateCollection (X509Certificate [] value)
48 AddRange (value);
51 public X509CertificateCollection (X509CertificateCollection value)
53 AddRange (value);
56 // Properties
58 public X509Certificate this [int index] {
59 get { return (X509Certificate) InnerList [index]; }
60 set { InnerList [index] = value; }
63 // Methods
65 public int Add (X509Certificate value)
67 if (value == null)
68 throw new ArgumentNullException ("value");
70 return InnerList.Add (value);
73 public void AddRange (X509Certificate [] value)
75 if (value == null)
76 throw new ArgumentNullException ("value");
78 for (int i = 0; i < value.Length; i++)
79 InnerList.Add (value [i]);
82 public void AddRange (X509CertificateCollection value)
84 if (value == null)
85 throw new ArgumentNullException ("value");
87 for (int i = 0; i < value.InnerList.Count; i++)
88 InnerList.Add (value [i]);
91 public bool Contains (X509Certificate value)
93 if (value == null)
94 return false;
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;
102 return false;
105 public void CopyTo (X509Certificate[] array, int index)
107 InnerList.CopyTo (array, index);
110 public new X509CertificateEnumerator GetEnumerator ()
112 return new X509CertificateEnumerator (this);
115 IEnumerator IEnumerable.GetEnumerator ()
117 return InnerList.GetEnumerator ();
120 public override int GetHashCode ()
122 return InnerList.GetHashCode ();
125 public int IndexOf (X509Certificate value)
127 return InnerList.IndexOf (value);
130 public void Insert (int index, X509Certificate value)
132 InnerList.Insert (index, value);
135 public void Remove (X509Certificate value)
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."));
144 InnerList.Remove (value);
147 // private stuff
149 private bool Compare (byte[] array1, byte[] array2)
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;
161 return true;
164 // Inner Class
166 public class X509CertificateEnumerator : IEnumerator {
168 private IEnumerator enumerator;
170 // Constructors
172 public X509CertificateEnumerator (X509CertificateCollection mappings)
174 enumerator = ((IEnumerable) mappings).GetEnumerator ();
177 // Properties
179 public X509Certificate Current {
180 get { return (X509Certificate) enumerator.Current; }
183 object IEnumerator.Current {
184 get { return enumerator.Current; }
187 // Methods
189 bool IEnumerator.MoveNext ()
191 return enumerator.MoveNext ();
194 void IEnumerator.Reset ()
196 enumerator.Reset ();
199 public bool MoveNext ()
201 return enumerator.MoveNext ();
204 public void Reset ()
206 enumerator.Reset ();