[HttpConnection] Bug fix: HttpListener's "IgnoreWriteExceptions" property value is...
[mono.git] / mcs / class / Mono.Security.Providers.DotNet / Mono.Security.Providers.DotNet / DotNetSslStreamImpl.cs
1 //
2 // DotNetSslStreamImpl.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.IO;
29 using System.Net;
30 using System.Net.Security;
31 using System.Threading.Tasks;
32 using System.Security.Authentication;
33 using System.Security.Cryptography.X509Certificates;
34 using System.Security.Principal;
35 using System.Security.Cryptography;
36 using Mono.Security.Interface;
37
38 namespace Mono.Security.Providers.DotNet
39 {
40         class DotNetSslStreamImpl : MonoSslStream
41         {
42                 SslStream impl;
43
44                 internal SslStream Impl {
45                         get {
46                                 CheckDisposed ();
47                                 return impl;
48                         }
49                 }
50
51                 public DotNetSslStreamImpl (
52                         Stream innerStream, bool leaveInnerStreamOpen,
53                         RemoteCertificateValidationCallback userCertificateValidationCallback,
54                         LocalCertificateSelectionCallback userCertificateSelectionCallback)
55                 {
56                         impl = new SslStream (
57                                 innerStream, leaveInnerStreamOpen,
58                                 userCertificateValidationCallback,
59                                 userCertificateSelectionCallback);
60                 }
61
62                 public override void AuthenticateAsClient (string targetHost)
63                 {
64                         Impl.AuthenticateAsClient (targetHost);
65                 }
66
67                 public override void AuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
68                 {
69                         Impl.AuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
70                 }
71
72                 public override IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
73                 {
74                         return Impl.BeginAuthenticateAsClient (targetHost, asyncCallback, asyncState);
75                 }
76
77                 public override IAsyncResult BeginAuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
78                 {
79                         return Impl.BeginAuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
80                 }
81
82                 public override void EndAuthenticateAsClient (IAsyncResult asyncResult)
83                 {
84                         Impl.EndAuthenticateAsClient (asyncResult);
85                 }
86
87                 public override void AuthenticateAsServer (X509Certificate serverCertificate)
88                 {
89                         Impl.AuthenticateAsServer (serverCertificate);
90                 }
91
92                 public override void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
93                 {
94                         Impl.AuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
95                 }
96
97                 public override IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
98                 {
99                         return Impl.BeginAuthenticateAsServer (serverCertificate, asyncCallback, asyncState);
100                 }
101
102                 public override IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
103                 {
104                         return Impl.BeginAuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
105                 }
106
107                 public override void EndAuthenticateAsServer (IAsyncResult asyncResult)
108                 {
109                         Impl.EndAuthenticateAsServer (asyncResult);
110                 }
111
112                 public override Task AuthenticateAsClientAsync (string targetHost)
113                 {
114                         return Impl.AuthenticateAsClientAsync (targetHost);
115                 }
116
117                 public override Task AuthenticateAsClientAsync (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
118                 {
119                         return Impl.AuthenticateAsClientAsync (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
120                 }
121
122                 public override Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
123                 {
124                         return Impl.AuthenticateAsServerAsync (serverCertificate);
125                 }
126
127                 public override Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
128                 {
129                         return Impl.AuthenticateAsServerAsync (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
130                 }
131
132                 public override void Flush ()
133                 {
134                         Impl.Flush ();
135                 }
136
137                 public override int Read (byte[] buffer, int offset, int count)
138                 {
139                         return Impl.Read (buffer, offset, count);
140                 }
141
142                 public override void Write (byte[] buffer)
143                 {
144                         Impl.Write (buffer);
145                 }
146
147                 public override void Write (byte[] buffer, int offset, int count)
148                 {
149                         Impl.Write (buffer, offset, count);
150                 }
151
152                 public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
153                 {
154                         return Impl.BeginRead (buffer, offset, count, asyncCallback, asyncState);
155                 }
156
157                 public override int EndRead (IAsyncResult asyncResult)
158                 {
159                         return Impl.EndRead (asyncResult);
160                 }
161
162                 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
163                 {
164                         return Impl.BeginWrite (buffer, offset, count, asyncCallback, asyncState);
165                 }
166
167                 public override void EndWrite (IAsyncResult asyncResult)
168                 {
169                         Impl.EndWrite (asyncResult);
170                 }
171
172                 public override TransportContext TransportContext {
173                         get { throw new NotSupportedException (); }
174                 }
175
176                 public override bool IsAuthenticated {
177                         get { return Impl.IsAuthenticated; }
178                 }
179
180                 public override bool IsMutuallyAuthenticated {
181                         get { return Impl.IsMutuallyAuthenticated; }
182                 }
183
184                 public override bool IsEncrypted {
185                         get { return Impl.IsEncrypted; }
186                 }
187
188                 public override bool IsSigned {
189                         get { return Impl.IsSigned; }
190                 }
191
192                 public override bool IsServer {
193                         get { return Impl.IsServer; }
194                 }
195
196                 public override CipherAlgorithmType CipherAlgorithm {
197                         get { return Impl.CipherAlgorithm; }
198                 }
199
200                 public override int CipherStrength {
201                         get { return Impl.CipherStrength; }
202                 }
203
204                 public override HashAlgorithmType HashAlgorithm {
205                         get { return Impl.HashAlgorithm; }
206                 }
207
208                 public override int HashStrength {
209                         get { return Impl.HashStrength; }
210                 }
211
212                 public override ExchangeAlgorithmType KeyExchangeAlgorithm {
213                         get { return Impl.KeyExchangeAlgorithm; }
214                 }
215
216                 public override int KeyExchangeStrength {
217                         get { return KeyExchangeStrength; }
218                 }
219
220                 public override bool CanRead {
221                         get { return Impl.CanRead; }
222                 }
223
224                 public override bool CanTimeout {
225                         get { return Impl.CanTimeout; }
226                 }
227
228                 public override bool CanWrite {
229                         get { return Impl.CanWrite; }
230                 }
231
232                 public override long Length {
233                         get { return Impl.Length; }
234                 }
235
236                 public override long Position {
237                         get { return Impl.Position; }
238                 }
239
240                 public override void SetLength (long value)
241                 {
242                         Impl.SetLength (value);
243                 }
244
245                 public override AuthenticatedStream AuthenticatedStream {
246                         get { return Impl; }
247                 }
248
249                 public override int ReadTimeout {
250                         get { return Impl.ReadTimeout; }
251                         set { Impl.ReadTimeout = value; }
252                 }
253
254                 public override int WriteTimeout {
255                         get { return Impl.WriteTimeout; }
256                         set { Impl.WriteTimeout = value; }
257                 }
258
259                 public override bool CheckCertRevocationStatus {
260                         get { return Impl.CheckCertRevocationStatus; }
261                 }
262
263                 public override X509Certificate InternalLocalCertificate {
264                         get {
265                                 try {
266                                         return LocalCertificate;
267                                 } catch {
268                                         return null;
269                                 }
270                         }
271                 }
272
273                 public override X509Certificate LocalCertificate {
274                         get { return Impl.LocalCertificate; }
275                 }
276
277                 public override X509Certificate RemoteCertificate {
278                         get { return Impl.RemoteCertificate; }
279                 }
280
281                 public override SslProtocols SslProtocol {
282                         get { return Impl.SslProtocol; }
283                 }
284
285                 void CheckDisposed ()
286                 {
287                         if (impl == null)
288                                 throw new ObjectDisposedException ("MonoSslStream");
289                 }
290
291                 protected override void Dispose (bool disposing)
292                 {
293                         if (impl != null && disposing) {
294                                 impl.Dispose ();
295                                 impl = null;
296                         }
297                 }
298         }
299 }
300