This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / CipherSuiteCollection.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 /* Transport Security Layer (TLS)
23  * Copyright (c) 2003-2004 Carlos Guzman Alvarez
24  * 
25  * Permission is hereby granted, free of charge, to any person 
26  * obtaining a copy of this software and associated documentation 
27  * files (the "Software"), to deal in the Software without restriction, 
28  * including without limitation the rights to use, copy, modify, merge, 
29  * publish, distribute, sublicense, and/or sell copies of the Software, 
30  * and to permit persons to whom the Software is furnished to do so, 
31  * subject to the following conditions:
32  * 
33  * The above copyright notice and this permission notice shall be included 
34  * in all copies or substantial portions of the Software.
35  * 
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
37  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
38  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
39  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
40  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
41  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
42  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
43  * DEALINGS IN THE SOFTWARE.
44  */
45
46 using System;
47 using System.Collections;
48 using System.Globalization;
49 using System.Security.Cryptography;
50
51 namespace Mono.Security.Protocol.Tls
52 {
53         internal sealed class CipherSuiteCollection : ICollection, IList, IEnumerable
54         {
55                 #region Fields
56
57                 private ArrayList                               cipherSuites;
58                 private SecurityProtocolType    protocol;
59
60                 #endregion
61
62                 #region Indexers
63
64                 public CipherSuite this[string name] 
65                 {
66                         get { return (CipherSuite)this.cipherSuites[this.IndexOf(name)]; }
67                         set { this.cipherSuites[this.IndexOf(name)] = (CipherSuite)value; }
68                 }
69
70                 public CipherSuite this[int index] 
71                 {
72                         get { return (CipherSuite)this.cipherSuites[index]; }
73                         set { this.cipherSuites[index] = (CipherSuite)value; }
74                 }
75
76                 public CipherSuite this[short code] 
77                 {
78                         get { return (CipherSuite)this.cipherSuites[this.IndexOf(code)]; }
79                         set { this.cipherSuites[this.IndexOf(code)] = (CipherSuite)value; }
80                 }
81
82                 object IList.this[int index]
83                 {
84                         get { return this[index]; }
85                         set { this[index] = (CipherSuite)value; }
86                 }
87
88                 #endregion
89
90                 #region ICollection Properties
91
92                 bool ICollection.IsSynchronized
93                 {
94                         get { return this.cipherSuites.IsSynchronized; }
95                 }
96
97                 object ICollection.SyncRoot 
98                 {
99                         get { return this.cipherSuites.SyncRoot; }
100                 }
101
102                 public int Count 
103                 {
104                         get { return this.cipherSuites.Count; }
105                 }
106                 
107                 #endregion
108
109                 #region IList Properties
110
111                 public bool IsFixedSize 
112                 {
113                         get { return this.cipherSuites.IsFixedSize; }
114                 }
115
116                 public bool IsReadOnly
117                 {
118                         get { return this.cipherSuites.IsReadOnly; }
119                 }
120
121                 #endregion
122
123                 #region Constructors
124
125                 public CipherSuiteCollection(SecurityProtocolType protocol) : base()
126                 {
127                         this.protocol           = protocol;
128                         this.cipherSuites       = new ArrayList();
129                 }
130
131                 #endregion
132
133                 #region ICollection Methods
134
135                 public void CopyTo(Array array, int index)
136                 {
137                         this.cipherSuites.CopyTo(array, index);
138                 }
139                 
140                 #endregion
141
142                 #region IEnumerable Methods
143
144                 IEnumerator IEnumerable.GetEnumerator()
145                 {
146                         return this.cipherSuites.GetEnumerator();
147                 }
148
149                 #endregion
150
151                 #region IList Methods
152
153                 public void Clear()
154                 {
155                         this.cipherSuites.Clear();
156                 }
157                         
158                 bool IList.Contains(object value)
159                 {
160                         return this.cipherSuites.Contains(value as CipherSuite);
161                 }
162
163                 public int IndexOf(string name)
164                 {
165                         int index = 0;
166
167                         foreach (CipherSuite cipherSuite in this.cipherSuites)
168                         {
169                                 if (this.cultureAwareCompare(cipherSuite.Name, name))
170                                 {
171                                         return index;
172                                 }
173                                 index++;
174                         }
175
176                         return -1;
177                 }
178
179                 public int IndexOf(short code)
180                 {
181                         int index = 0;
182
183                         foreach (CipherSuite cipherSuite in this.cipherSuites)
184                         {
185                                 if (cipherSuite.Code == code)
186                                 {
187                                         return index;
188                                 }
189                                 index++;
190                         }
191
192                         return -1;
193                 }
194
195                 int IList.IndexOf(object value)
196                 {
197                         return this.cipherSuites.IndexOf(value as CipherSuite);
198                 }
199
200                 void IList.Insert(int index, object value)
201                 {
202                         this.cipherSuites.Insert(index, value as CipherSuite);
203                 }
204
205                 void IList.Remove(object value)
206                 {
207                         this.cipherSuites.Remove(value as CipherSuite);
208                 }
209
210                 void IList.RemoveAt(int index)
211                 {
212                         this.cipherSuites.RemoveAt(index);
213                 }
214
215                 public CipherSuite Add(
216                         short code, string name, CipherAlgorithmType cipherType, 
217                         HashAlgorithmType hashType, ExchangeAlgorithmType exchangeType,
218                         bool exportable, bool blockMode, byte keyMaterialSize, 
219                         byte expandedKeyMaterialSize, short effectiveKeyBytes, 
220                         byte ivSize, byte blockSize)
221                 {
222                         switch (this.protocol)
223                         {
224                                 case SecurityProtocolType.Default:
225                                 case SecurityProtocolType.Tls:
226                                         return this.add(
227                                                 new TlsCipherSuite(
228                                                 code, name, cipherType, hashType, exchangeType, exportable, 
229                                                 blockMode, keyMaterialSize, expandedKeyMaterialSize, 
230                                                 effectiveKeyBytes, ivSize, blockSize));
231
232                                 case SecurityProtocolType.Ssl3:
233                                         return this.add(
234                                                 new SslCipherSuite(
235                                                 code, name, cipherType, hashType, exchangeType, exportable, 
236                                                 blockMode, keyMaterialSize, expandedKeyMaterialSize, 
237                                                 effectiveKeyBytes, ivSize, blockSize));
238
239                                 case SecurityProtocolType.Ssl2:
240                                 default:
241                                         throw new NotSupportedException("Unsupported security protocol type.");
242                         }
243                 }
244
245                 private TlsCipherSuite add(TlsCipherSuite cipherSuite)
246                 {
247                         this.cipherSuites.Add(cipherSuite);
248
249                         return cipherSuite;
250                 }
251
252                 private SslCipherSuite add(SslCipherSuite cipherSuite)
253                 {
254                         this.cipherSuites.Add(cipherSuite);
255
256                         return cipherSuite;
257                 }
258
259                 int IList.Add(object value)
260                 {
261                         return this.cipherSuites.Add(value as CipherSuite);
262                 }
263                 
264                 private bool cultureAwareCompare(string strA, string strB)
265                 {
266                         return CultureInfo.CurrentCulture.CompareInfo.Compare(
267                                 strA, 
268                                 strB, 
269                                 CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | 
270                                 CompareOptions.IgnoreCase) == 0 ? true : false;
271                 }
272
273                 #endregion
274         }
275 }