**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / CipherSuiteCollection.cs
blob2946013a517d369bde08f107794e75f89a144a38
1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 using System;
26 using System.Collections;
27 using System.Globalization;
28 using System.Security.Cryptography;
30 namespace Mono.Security.Protocol.Tls
32 internal sealed class CipherSuiteCollection : ICollection, IList, IEnumerable
34 #region Fields
36 private ArrayList cipherSuites;
37 private SecurityProtocolType protocol;
39 #endregion
41 #region Indexers
43 public CipherSuite this[string name]
45 get { return (CipherSuite)this.cipherSuites[this.IndexOf(name)]; }
46 set { this.cipherSuites[this.IndexOf(name)] = (CipherSuite)value; }
49 public CipherSuite this[int index]
51 get { return (CipherSuite)this.cipherSuites[index]; }
52 set { this.cipherSuites[index] = (CipherSuite)value; }
55 public CipherSuite this[short code]
57 get { return (CipherSuite)this.cipherSuites[this.IndexOf(code)]; }
58 set { this.cipherSuites[this.IndexOf(code)] = (CipherSuite)value; }
61 object IList.this[int index]
63 get { return this[index]; }
64 set { this[index] = (CipherSuite)value; }
67 #endregion
69 #region ICollection Properties
71 bool ICollection.IsSynchronized
73 get { return this.cipherSuites.IsSynchronized; }
76 object ICollection.SyncRoot
78 get { return this.cipherSuites.SyncRoot; }
81 public int Count
83 get { return this.cipherSuites.Count; }
86 #endregion
88 #region IList Properties
90 public bool IsFixedSize
92 get { return this.cipherSuites.IsFixedSize; }
95 public bool IsReadOnly
97 get { return this.cipherSuites.IsReadOnly; }
100 #endregion
102 #region Constructors
104 public CipherSuiteCollection(SecurityProtocolType protocol) : base()
106 this.protocol = protocol;
107 this.cipherSuites = new ArrayList();
110 #endregion
112 #region ICollection Methods
114 public void CopyTo(Array array, int index)
116 this.cipherSuites.CopyTo(array, index);
119 #endregion
121 #region IEnumerable Methods
123 IEnumerator IEnumerable.GetEnumerator()
125 return this.cipherSuites.GetEnumerator();
128 #endregion
130 #region IList Methods
132 public void Clear()
134 this.cipherSuites.Clear();
137 bool IList.Contains(object value)
139 return this.cipherSuites.Contains(value as CipherSuite);
142 public int IndexOf(string name)
144 int index = 0;
146 foreach (CipherSuite cipherSuite in this.cipherSuites)
148 if (this.cultureAwareCompare(cipherSuite.Name, name))
150 return index;
152 index++;
155 return -1;
158 public int IndexOf(short code)
160 int index = 0;
162 foreach (CipherSuite cipherSuite in this.cipherSuites)
164 if (cipherSuite.Code == code)
166 return index;
168 index++;
171 return -1;
174 int IList.IndexOf(object value)
176 return this.cipherSuites.IndexOf(value as CipherSuite);
179 void IList.Insert(int index, object value)
181 this.cipherSuites.Insert(index, value as CipherSuite);
184 void IList.Remove(object value)
186 this.cipherSuites.Remove(value as CipherSuite);
189 void IList.RemoveAt(int index)
191 this.cipherSuites.RemoveAt(index);
194 public CipherSuite Add(
195 short code, string name, CipherAlgorithmType cipherType,
196 HashAlgorithmType hashType, ExchangeAlgorithmType exchangeType,
197 bool exportable, bool blockMode, byte keyMaterialSize,
198 byte expandedKeyMaterialSize, short effectiveKeyBytes,
199 byte ivSize, byte blockSize)
201 switch (this.protocol)
203 case SecurityProtocolType.Default:
204 case SecurityProtocolType.Tls:
205 return this.add(
206 new TlsCipherSuite(
207 code, name, cipherType, hashType, exchangeType, exportable,
208 blockMode, keyMaterialSize, expandedKeyMaterialSize,
209 effectiveKeyBytes, ivSize, blockSize));
211 case SecurityProtocolType.Ssl3:
212 return this.add(
213 new SslCipherSuite(
214 code, name, cipherType, hashType, exchangeType, exportable,
215 blockMode, keyMaterialSize, expandedKeyMaterialSize,
216 effectiveKeyBytes, ivSize, blockSize));
218 case SecurityProtocolType.Ssl2:
219 default:
220 throw new NotSupportedException("Unsupported security protocol type.");
224 private TlsCipherSuite add(TlsCipherSuite cipherSuite)
226 this.cipherSuites.Add(cipherSuite);
228 return cipherSuite;
231 private SslCipherSuite add(SslCipherSuite cipherSuite)
233 this.cipherSuites.Add(cipherSuite);
235 return cipherSuite;
238 int IList.Add(object value)
240 return this.cipherSuites.Add(value as CipherSuite);
243 private bool cultureAwareCompare(string strA, string strB)
245 return CultureInfo.CurrentCulture.CompareInfo.Compare(
246 strA,
247 strB,
248 CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth |
249 CompareOptions.IgnoreCase) == 0 ? true : false;
252 #endregion