CipherSuiteCollection.cs 5.8 KB

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