1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
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
36 private ArrayList cipherSuites
;
37 private SecurityProtocolType protocol
;
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; }
69 #region ICollection Properties
71 bool ICollection
.IsSynchronized
73 get { return this.cipherSuites.IsSynchronized; }
76 object ICollection
.SyncRoot
78 get { return this.cipherSuites.SyncRoot; }
83 get { return this.cipherSuites.Count; }
88 #region IList Properties
90 public bool IsFixedSize
92 get { return this.cipherSuites.IsFixedSize; }
95 public bool IsReadOnly
97 get { return this.cipherSuites.IsReadOnly; }
104 public CipherSuiteCollection(SecurityProtocolType protocol
) : base()
106 this.protocol
= protocol
;
107 this.cipherSuites
= new ArrayList();
112 #region ICollection Methods
114 public void CopyTo(Array array
, int index
)
116 this.cipherSuites
.CopyTo(array
, index
);
121 #region IEnumerable Methods
123 IEnumerator IEnumerable
.GetEnumerator()
125 return this.cipherSuites
.GetEnumerator();
130 #region IList Methods
134 this.cipherSuites
.Clear();
137 bool IList
.Contains(object value)
139 return this.cipherSuites
.Contains(value as CipherSuite
);
142 public int IndexOf(string name
)
146 foreach (CipherSuite cipherSuite
in this.cipherSuites
)
148 if (this.cultureAwareCompare(cipherSuite
.Name
, name
))
158 public int IndexOf(short code
)
162 foreach (CipherSuite cipherSuite
in this.cipherSuites
)
164 if (cipherSuite
.Code
== code
)
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
:
207 code
, name
, cipherType
, hashType
, exchangeType
, exportable
,
208 blockMode
, keyMaterialSize
, expandedKeyMaterialSize
,
209 effectiveKeyBytes
, ivSize
, blockSize
));
211 case SecurityProtocolType
.Ssl3
:
214 code
, name
, cipherType
, hashType
, exchangeType
, exportable
,
215 blockMode
, keyMaterialSize
, expandedKeyMaterialSize
,
216 effectiveKeyBytes
, ivSize
, blockSize
));
218 case SecurityProtocolType
.Ssl2
:
220 throw new NotSupportedException("Unsupported security protocol type.");
224 private TlsCipherSuite
add(TlsCipherSuite cipherSuite
)
226 this.cipherSuites
.Add(cipherSuite
);
231 private SslCipherSuite
add(SslCipherSuite cipherSuite
)
233 this.cipherSuites
.Add(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(
248 CompareOptions
.IgnoreKanaType
| CompareOptions
.IgnoreWidth
|
249 CompareOptions
.IgnoreCase
) == 0 ? true : false;