Updated TLS/SSL implementation files with unix-like line endings
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / TlsCipherSuiteCollection.cs
1 /* Transport Security Layer (TLS)
2  * Copyright (c) 2003 Carlos Guzmán Álvarez
3  * 
4  * Permission is hereby granted, free of charge, to any person 
5  * obtaining a copy of this software and associated documentation 
6  * files (the "Software"), to deal in the Software without restriction, 
7  * including without limitation the rights to use, copy, modify, merge, 
8  * publish, distribute, sublicense, and/or sell copies of the Software, 
9  * and to permit persons to whom the Software is furnished to do so, 
10  * subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be included 
13  * 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 
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 using System;
26 using System.Collections;
27 using System.Globalization;
28 using System.Security.Cryptography;
29
30 namespace Mono.Security.Protocol.Tls
31 {
32         internal sealed class TlsCipherSuiteCollection : ArrayList
33         {
34                 #region FIELDS
35
36                 private TlsProtocol protocol;
37
38                 #endregion
39
40                 #region PROPERTIES
41
42                 public CipherSuite this[string name] 
43                 {
44                         get { return (CipherSuite)this[IndexOf(name)]; }
45                         set { this[IndexOf(name)] = (CipherSuite)value; }
46                 }
47
48                 public CipherSuite this[short code] 
49                 {
50                         get { return (CipherSuite)base[IndexOf(code)]; }
51                         set { base[IndexOf(code)] = (CipherSuite)value; }
52                 }
53
54                 public new CipherSuite this[int code] 
55                 {
56                         get { return (CipherSuite)base[code]; }
57                         set { base[code] = (CipherSuite)value; }
58                 }
59
60                 #endregion
61
62                 #region CONSTRUCTORS
63
64                 public TlsCipherSuiteCollection(TlsProtocol protocol) : base()
65                 {
66                         this.protocol = protocol;
67                 }
68
69                 #endregion
70
71                 #region METHODS
72         
73                 public bool Contains(string name)
74                 {
75                         return(-1 != IndexOf(name));
76                 }
77                 
78                 public int IndexOf(string name)
79                 {
80                         int index = 0;
81                         foreach (CipherSuite suite in this)
82                         {
83                                 if (cultureAwareCompare(suite.Name, name))
84                                 {
85                                         return index;
86                                 }
87                                 index++;
88                         }
89                         return -1;
90                 }
91
92                 public int IndexOf(short code)
93                 {
94                         int index = 0;
95                         foreach (CipherSuite suite in this)
96                         {
97                                 if (suite.Code == code)
98                                 {
99                                         return index;
100                                 }
101                                 index++;
102                         }
103                         return -1;
104                 }
105
106                 public void RemoveAt(string errorMessage)
107                 {
108                         RemoveAt(IndexOf(errorMessage));
109                 }
110
111                 public CipherSuite Add(short code, string name, string algName, string hashName, bool exportable, bool blockMode, byte keyMaterialSize, byte expandedKeyMaterialSize, short effectiveKeyBytes, byte ivSize, byte blockSize)
112                 {
113                         switch (this.protocol)
114                         {
115                                 case TlsProtocol.Tls1:
116                                         return this.add(
117                                                 new TlsCipherSuite(code, name, algName, hashName, exportable, blockMode, keyMaterialSize, expandedKeyMaterialSize, effectiveKeyBytes, ivSize, blockSize));
118
119                                 case TlsProtocol.Ssl3:
120                                         return this.add(
121                                                 new TlsSslCipherSuite(code, name, algName, hashName, exportable, blockMode, keyMaterialSize, expandedKeyMaterialSize, effectiveKeyBytes, ivSize, blockSize));
122
123                                 default:
124                                         throw new NotSupportedException();
125                         }
126                 }
127
128                 private TlsCipherSuite add(TlsCipherSuite cipherSuite)
129                 {
130                         base.Add(cipherSuite);
131
132                         return cipherSuite;
133                 }
134
135                 private TlsSslCipherSuite add(TlsSslCipherSuite cipherSuite)
136                 {
137                         base.Add(cipherSuite);
138
139                         return cipherSuite;
140                 }
141
142                 private bool cultureAwareCompare(string strA, string strB)
143                 {
144                         try
145                         {
146                                 return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase) == 0 ? true : false;
147                         }
148                         catch (NotSupportedException)
149                         {
150                                 return strA.ToUpper() == strB.ToUpper() ? true : false;
151                         }
152                 }
153
154                 #endregion
155         }
156 }