Added partial implementation of SSL3 protocol (not finished yet). Renamed TlsAbstract...
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / TlsSessionSettings.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.Text;
27 using System.Security.Cryptography.X509Certificates;
28
29 namespace Mono.Security.Protocol.Tls
30 {
31         public sealed class TlsSessionSettings
32         {
33                 #region FIELDS
34
35                 private string                                          serverName;
36                 private int                                                     serverPort;
37                 private Encoding                                        encoding;
38                 private TlsProtocol                                     protocol;
39                 private TlsCompressionMethod            compressionMethod;
40                 private X509CertificateCollection       certificates;
41         
42                 #endregion
43
44                 #region PROPERTIES
45
46                 public string ServerName
47                 {
48                         get { return serverName; }
49                         set { serverName = value; }
50                 }
51
52                 public int ServerPort
53                 {
54                         get { return serverPort; }
55                         set { serverPort = value; }
56                 }
57
58                 public Encoding Encoding
59                 {
60                         get { return encoding; }
61                         set { encoding = value; }
62                 }
63
64                 public TlsProtocol Protocol
65                 {
66                         get { return protocol; }
67                         set 
68                         { 
69                                 if (value != TlsProtocol.Tls1 &&
70                                         value != TlsProtocol.Ssl3)
71                                 {
72                                         throw new NotSupportedException("Specified protocol is not supported");
73                                 }
74                                 protocol = value; 
75                         }
76                 }
77
78                 public TlsCompressionMethod CompressionMethod
79                 {
80                         get { return compressionMethod; }
81                         set 
82                         { 
83                                 if (value != TlsCompressionMethod.None)
84                                 {
85                                         throw new NotSupportedException("Specified compression method is not supported");
86                                 }
87                                 compressionMethod = value; 
88                         }
89                 }
90
91                 public X509CertificateCollection Certificates
92                 {
93                         get { return certificates; }
94                         set { certificates = value; }
95                 }
96
97                 #endregion
98
99                 #region CONSTRUCTORS
100
101                 public TlsSessionSettings()
102                 {
103                         this.protocol                   = TlsProtocol.Tls1;
104                         this.compressionMethod  = TlsCompressionMethod.None;
105                         this.certificates               = new X509CertificateCollection();
106                         this.serverName                 = "localhost";
107                         this.serverPort                 = 443;
108                         this.encoding                   = Encoding.Default;
109                 }
110
111                 public TlsSessionSettings(TlsProtocol protocol) : this()
112                 {
113                         this.Protocol   = protocol;
114                 }
115
116                 public TlsSessionSettings(TlsProtocol protocol, Encoding encoding) : this(protocol)
117                 {
118                         this.encoding   = encoding;
119                 }
120
121                 public TlsSessionSettings(string serverName) : this()
122                 {
123                         this.serverName = serverName;
124                 }
125
126                 public TlsSessionSettings(string serverName, Encoding encoding) : this()
127                 {
128                         this.serverName = serverName;
129                         this.encoding   = encoding;
130                 }
131
132                 public TlsSessionSettings(string serverName, int serverPort) : this()
133                 {
134                         this.serverName = serverName;
135                         this.serverPort = serverPort;
136                 }
137
138                 public TlsSessionSettings(string serverName, int serverPort, Encoding encoding) : this()
139                 {
140                         this.serverName = serverName;
141                         this.serverPort = serverPort;
142                         this.encoding   = encoding;
143                 }
144
145                 public TlsSessionSettings(TlsProtocol protocol, string serverName) : this(protocol)
146                 {
147                         this.serverName = serverName;
148                 }
149
150                 public TlsSessionSettings(TlsProtocol protocol, string serverName, Encoding encoding) : this(protocol)
151                 {
152                         this.serverName = serverName;
153                         this.encoding   = encoding;
154                 }
155
156
157                 public TlsSessionSettings(TlsProtocol protocol, string serverName, int serverPort) : this(protocol)
158                 {
159                         this.serverName = serverName;
160                         this.serverPort = serverPort;
161                 }
162
163                 public TlsSessionSettings(TlsProtocol protocol, string serverName, int serverPort, Encoding encoding) : this(protocol)
164                 {
165                         this.serverName = serverName;
166                         this.serverPort = serverPort;
167                         this.encoding   = encoding;
168                 }
169
170                 public TlsSessionSettings(TlsProtocol protocol, X509CertificateCollection certificates) : this(protocol)
171                 {
172                         this.certificates       = certificates;
173                 }
174
175                 public TlsSessionSettings(TlsProtocol protocol, X509CertificateCollection certificates, Encoding encoding) : this(protocol)
176                 {
177                         this.certificates       = certificates;
178                         this.encoding           = encoding;
179                 }
180
181                 public TlsSessionSettings(TlsProtocol protocol, X509CertificateCollection certificates, string serverName, int serverPort) : this(protocol)
182                 {
183                         this.certificates       = certificates;
184                         this.serverName         = serverName;
185                         this.serverPort         = serverPort;
186                 }
187
188                 public TlsSessionSettings(TlsProtocol protocol, X509CertificateCollection certificates, string serverName, int serverPort, Encoding encoding) : this(protocol)
189                 {
190                         this.certificates       = certificates;
191                         this.serverName         = serverName;
192                         this.serverPort         = serverPort;
193                         this.encoding           = encoding;
194                 }
195
196                 public TlsSessionSettings(TlsProtocol protocol, X509Certificate[] certificates) 
197                         : this(protocol, new X509CertificateCollection(certificates))
198                 {
199                 }
200
201                 public TlsSessionSettings(TlsProtocol protocol, X509Certificate[] certificates, Encoding encoding) 
202                         : this(protocol, new X509CertificateCollection(certificates), encoding)
203                 {
204                 }
205
206                 public TlsSessionSettings(TlsProtocol protocol, X509Certificate[] certificates, string serverName, int serverPort) : 
207                         this(protocol, new X509CertificateCollection(certificates), serverName, serverPort)
208                 {
209                 }
210
211                 public TlsSessionSettings(TlsProtocol protocol, X509Certificate[] certificates, string serverName, int serverPort, Encoding encoding) : 
212                         this(protocol, new X509CertificateCollection(certificates), serverName, serverPort, encoding)
213                 {
214                 }
215
216                 #endregion
217         }
218 }