2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Mono.Security.X509 / X509CertificateCollection.cs
blob1b7b8405dccd5f552450053e0ec44c9936ac2604
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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Collections;
36 namespace Mono.Security.X509 {
38 [Serializable]
39 #if INSIDE_CORLIB
40 internal
41 #else
42 public
43 #endif
44 class X509CertificateCollection : CollectionBase, IEnumerable {
46 public X509CertificateCollection ()
50 public X509CertificateCollection (X509Certificate [] value)
52 AddRange (value);
55 public X509CertificateCollection (X509CertificateCollection value)
57 AddRange (value);
60 // Properties
62 public X509Certificate this [int index] {
63 get { return (X509Certificate) InnerList [index]; }
64 set { InnerList [index] = value; }
67 // Methods
69 public int Add (X509Certificate value)
71 if (value == null)
72 throw new ArgumentNullException ("value");
74 return InnerList.Add (value);
77 public void AddRange (X509Certificate [] value)
79 if (value == null)
80 throw new ArgumentNullException ("value");
82 for (int i = 0; i < value.Length; i++)
83 InnerList.Add (value [i]);
86 public void AddRange (X509CertificateCollection value)
88 if (value == null)
89 throw new ArgumentNullException ("value");
91 for (int i = 0; i < value.InnerList.Count; i++)
92 InnerList.Add (value [i]);
95 public bool Contains (X509Certificate value)
97 return (IndexOf (value) != -1);
100 public void CopyTo (X509Certificate[] array, int index)
102 InnerList.CopyTo (array, index);
105 public new X509CertificateEnumerator GetEnumerator ()
107 return new X509CertificateEnumerator (this);
110 IEnumerator IEnumerable.GetEnumerator ()
112 return InnerList.GetEnumerator ();
115 public override int GetHashCode ()
117 return InnerList.GetHashCode ();
120 public int IndexOf (X509Certificate value)
122 if (value == null)
123 throw new ArgumentNullException ("value");
125 byte[] hash = value.Hash;
126 for (int i=0; i < InnerList.Count; i++) {
127 X509Certificate x509 = (X509Certificate) InnerList [i];
128 if (Compare (x509.Hash, hash))
129 return i;
131 return -1;
134 public void Insert (int index, X509Certificate value)
136 InnerList.Insert (index, value);
139 public void Remove (X509Certificate value)
141 InnerList.Remove (value);
144 // private stuff
146 private bool Compare (byte[] array1, byte[] array2)
148 if ((array1 == null) && (array2 == null))
149 return true;
150 if ((array1 == null) || (array2 == null))
151 return false;
152 if (array1.Length != array2.Length)
153 return false;
154 for (int i=0; i < array1.Length; i++) {
155 if (array1 [i] != array2 [i])
156 return false;
158 return true;
161 // Inner Class
163 public class X509CertificateEnumerator : IEnumerator {
165 private IEnumerator enumerator;
167 // Constructors
169 public X509CertificateEnumerator (X509CertificateCollection mappings)
171 enumerator = ((IEnumerable) mappings).GetEnumerator ();
174 // Properties
176 public X509Certificate Current {
177 get { return (X509Certificate) enumerator.Current; }
180 object IEnumerator.Current {
181 get { return enumerator.Current; }
184 // Methods
186 bool IEnumerator.MoveNext ()
188 return enumerator.MoveNext ();
191 void IEnumerator.Reset ()
193 enumerator.Reset ();
196 public bool MoveNext ()
198 return enumerator.MoveNext ();
201 public void Reset ()
203 enumerator.Reset ();