Merge pull request #2247 from ivmai/match-ext-libgc-api
[mono.git] / mcs / class / System / Mono.Net.Security / MonoSslStreamWrapper.cs
1 //
2 // MonoSslStreamImpl.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 #if SECURITY_DEP
28
29 #if MONO_X509_ALIAS
30 extern alias PrebuiltSystem;
31 #endif
32 #if MONO_SECURITY_ALIAS
33 extern alias MonoSecurity;
34 #endif
35
36 #if MONO_SECURITY_ALIAS
37 using MSI = MonoSecurity::Mono.Security.Interface;
38 #else
39 using MSI = Mono.Security.Interface;
40 #endif
41 #if MONO_X509_ALIAS
42 using XSslProtocols = PrebuiltSystem::System.Security.Authentication.SslProtocols;
43 using XX509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
44 #else
45 using XSslProtocols = System.Security.Authentication.SslProtocols;
46 using XX509CertificateCollection = System.Security.Cryptography.X509Certificates.X509CertificateCollection;
47 #endif
48
49 using CipherAlgorithmType = System.Security.Authentication.CipherAlgorithmType;
50 using HashAlgorithmType = System.Security.Authentication.HashAlgorithmType;
51 using ExchangeAlgorithmType = System.Security.Authentication.ExchangeAlgorithmType;
52
53 using System;
54 using System.IO;
55 using System.Net;
56 using System.Net.Security;
57 using System.Security.Authentication;
58 using System.Security.Cryptography.X509Certificates;
59 using System.Security.Principal;
60 using System.Security.Cryptography;
61
62 using System.Threading.Tasks;
63
64 namespace Mono.Net.Security.Private
65 {
66         class MonoSslStreamWrapper : IMonoSslStream
67         {
68                 MSI.IMonoSslStream impl;
69
70                 internal MSI.IMonoSslStream Impl {
71                         get {
72                                 CheckDisposed ();
73                                 return impl;
74                         }
75                 }
76
77                 public MonoSslStreamWrapper (MSI.IMonoSslStream impl)
78                 {
79                         this.impl = impl;
80                 }
81
82                 public void AuthenticateAsClient (string targetHost)
83                 {
84                         Impl.AuthenticateAsClient (targetHost);
85                 }
86
87                 public void AuthenticateAsClient (string targetHost, XX509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
88                 {
89                         Impl.AuthenticateAsClient (targetHost, (XX509CertificateCollection)(object)clientCertificates, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
90                 }
91
92                 public IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
93                 {
94                         return Impl.BeginAuthenticateAsClient (targetHost, asyncCallback, asyncState);
95                 }
96
97                 public IAsyncResult BeginAuthenticateAsClient (string targetHost, XX509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
98                 {
99                         return Impl.BeginAuthenticateAsClient (targetHost, (XX509CertificateCollection)(object)clientCertificates, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
100                 }
101
102                 public void EndAuthenticateAsClient (IAsyncResult asyncResult)
103                 {
104                         Impl.EndAuthenticateAsClient (asyncResult);
105                 }
106
107                 public void AuthenticateAsServer (X509Certificate serverCertificate)
108                 {
109                         Impl.AuthenticateAsServer (serverCertificate);
110                 }
111
112                 public void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
113                 {
114                         Impl.AuthenticateAsServer (serverCertificate, clientCertificateRequired, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
115                 }
116
117                 public IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
118                 {
119                         return Impl.BeginAuthenticateAsServer (serverCertificate, asyncCallback, asyncState);
120                 }
121
122                 public IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
123                 {
124                         return Impl.BeginAuthenticateAsServer (serverCertificate, clientCertificateRequired, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
125                 }
126
127                 public void EndAuthenticateAsServer (IAsyncResult asyncResult)
128                 {
129                         Impl.EndAuthenticateAsServer (asyncResult);
130                 }
131
132                 public Task AuthenticateAsClientAsync (string targetHost)
133                 {
134                         return Impl.AuthenticateAsClientAsync (targetHost);
135                 }
136
137                 public Task AuthenticateAsClientAsync (string targetHost, XX509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
138                 {
139                         return Impl.AuthenticateAsClientAsync (targetHost, clientCertificates, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
140                 }
141
142                 public Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
143                 {
144                         return Impl.AuthenticateAsServerAsync (serverCertificate);
145                 }
146
147                 public Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
148                 {
149                         return Impl.AuthenticateAsServerAsync (serverCertificate, clientCertificateRequired, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
150                 }
151
152                 public void Flush ()
153                 {
154                         Impl.Flush ();
155                 }
156
157                 public int Read (byte[] buffer, int offset, int count)
158                 {
159                         return Impl.Read (buffer, offset, count);
160                 }
161
162                 public void Write (byte[] buffer)
163                 {
164                         Impl.Write (buffer);
165                 }
166
167                 public void Write (byte[] buffer, int offset, int count)
168                 {
169                         Impl.Write (buffer, offset, count);
170                 }
171
172                 public IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
173                 {
174                         return Impl.BeginRead (buffer, offset, count, asyncCallback, asyncState);
175                 }
176
177                 public int EndRead (IAsyncResult asyncResult)
178                 {
179                         return Impl.EndRead (asyncResult);
180                 }
181
182                 public IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
183                 {
184                         return Impl.BeginWrite (buffer, offset, count, asyncCallback, asyncState);
185                 }
186
187                 public void EndWrite (IAsyncResult asyncResult)
188                 {
189                         Impl.EndWrite (asyncResult);
190                 }
191
192                 public TransportContext TransportContext {
193                         get { return (TransportContext)(object)Impl.TransportContext; }
194                 }
195
196                 public bool IsAuthenticated {
197                         get { return Impl.IsAuthenticated; }
198                 }
199
200                 public bool IsMutuallyAuthenticated {
201                         get { return Impl.IsMutuallyAuthenticated; }
202                 }
203
204                 public bool IsEncrypted {
205                         get { return Impl.IsEncrypted; }
206                 }
207
208                 public bool IsSigned {
209                         get { return Impl.IsSigned; }
210                 }
211
212                 public bool IsServer {
213                         get { return Impl.IsServer; }
214                 }
215
216                 public CipherAlgorithmType CipherAlgorithm {
217                         get { return (CipherAlgorithmType)Impl.CipherAlgorithm; }
218                 }
219
220                 public int CipherStrength {
221                         get { return Impl.CipherStrength; }
222                 }
223
224                 public HashAlgorithmType HashAlgorithm {
225                         get { return (HashAlgorithmType)Impl.HashAlgorithm; }
226                 }
227
228                 public int HashStrength {
229                         get { return Impl.HashStrength; }
230                 }
231
232                 public ExchangeAlgorithmType KeyExchangeAlgorithm {
233                         get { return (ExchangeAlgorithmType)Impl.KeyExchangeAlgorithm; }
234                 }
235
236                 public int KeyExchangeStrength {
237                         get { return Impl.KeyExchangeStrength; }
238                 }
239
240                 public bool CanRead {
241                         get { return Impl.CanRead; }
242                 }
243
244                 public bool CanTimeout {
245                         get { return Impl.CanTimeout; }
246                 }
247
248                 public bool CanWrite {
249                         get { return Impl.CanWrite; }
250                 }
251
252                 public long Length {
253                         get { return Impl.Length; }
254                 }
255
256                 public long Position {
257                         get { return Impl.Position; }
258                 }
259
260                 public void SetLength (long value)
261                 {
262                         Impl.SetLength (value);
263                 }
264
265                 public AuthenticatedStream AuthenticatedStream {
266                         get { return (AuthenticatedStream)(object)Impl.AuthenticatedStream; }
267                 }
268
269                 public int ReadTimeout {
270                         get { return Impl.ReadTimeout; }
271                         set { Impl.ReadTimeout = value; }
272                 }
273
274                 public int WriteTimeout {
275                         get { return Impl.WriteTimeout; }
276                         set { Impl.WriteTimeout = value; }
277                 }
278
279                 public bool CheckCertRevocationStatus {
280                         get { return Impl.CheckCertRevocationStatus; }
281                 }
282
283                 X509Certificate IMonoSslStream.InternalLocalCertificate {
284                         get { return Impl.InternalLocalCertificate; }
285                 }
286
287                 public X509Certificate LocalCertificate {
288                         get { return Impl.LocalCertificate; }
289                 }
290
291                 public X509Certificate RemoteCertificate {
292                         get { return Impl.RemoteCertificate; }
293                 }
294
295                 public SslProtocols SslProtocol {
296                         get { return (SslProtocols)Impl.SslProtocol; }
297                 }
298
299                 void CheckDisposed ()
300                 {
301                         if (impl == null)
302                                 throw new ObjectDisposedException ("MonoSslStream");
303                 }
304
305                 public void Dispose ()
306                 {
307                         Dispose (true);
308                         GC.SuppressFinalize (this);
309                 }
310
311                 protected void Dispose (bool disposing)
312                 {
313                         if (impl != null && disposing) {
314                                 impl.Dispose ();
315                                 impl = null;
316                         }
317                 }
318         }
319 }
320
321 #endif