2 // MonoSslStreamImpl.cs
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2015 Xamarin, Inc.
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
30 extern alias PrebuiltSystem;
32 #if MONO_SECURITY_ALIAS
33 extern alias MonoSecurity;
36 #if MONO_SECURITY_ALIAS
37 using MSI = MonoSecurity::Mono.Security.Interface;
39 using MSI = Mono.Security.Interface;
42 using XSslProtocols = PrebuiltSystem::System.Security.Authentication.SslProtocols;
43 using XX509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
45 using XSslProtocols = System.Security.Authentication.SslProtocols;
46 using XX509CertificateCollection = System.Security.Cryptography.X509Certificates.X509CertificateCollection;
49 using CipherAlgorithmType = System.Security.Authentication.CipherAlgorithmType;
50 using HashAlgorithmType = System.Security.Authentication.HashAlgorithmType;
51 using ExchangeAlgorithmType = System.Security.Authentication.ExchangeAlgorithmType;
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;
62 using System.Threading.Tasks;
64 namespace Mono.Net.Security.Private
66 class MonoSslStreamWrapper : IMonoSslStream
68 MSI.IMonoSslStream impl;
70 internal MSI.IMonoSslStream Impl {
77 public MonoSslStreamWrapper (MSI.IMonoSslStream impl)
82 public void AuthenticateAsClient (string targetHost)
84 Impl.AuthenticateAsClient (targetHost);
87 public void AuthenticateAsClient (string targetHost, XX509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
89 Impl.AuthenticateAsClient (targetHost, (XX509CertificateCollection)(object)clientCertificates, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
92 public IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
94 return Impl.BeginAuthenticateAsClient (targetHost, asyncCallback, asyncState);
97 public IAsyncResult BeginAuthenticateAsClient (string targetHost, XX509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
99 return Impl.BeginAuthenticateAsClient (targetHost, (XX509CertificateCollection)(object)clientCertificates, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
102 public void EndAuthenticateAsClient (IAsyncResult asyncResult)
104 Impl.EndAuthenticateAsClient (asyncResult);
107 public void AuthenticateAsServer (X509Certificate serverCertificate)
109 Impl.AuthenticateAsServer (serverCertificate);
112 public void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
114 Impl.AuthenticateAsServer (serverCertificate, clientCertificateRequired, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
117 public IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
119 return Impl.BeginAuthenticateAsServer (serverCertificate, asyncCallback, asyncState);
122 public IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
124 return Impl.BeginAuthenticateAsServer (serverCertificate, clientCertificateRequired, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
127 public void EndAuthenticateAsServer (IAsyncResult asyncResult)
129 Impl.EndAuthenticateAsServer (asyncResult);
132 public Task AuthenticateAsClientAsync (string targetHost)
134 return Impl.AuthenticateAsClientAsync (targetHost);
137 public Task AuthenticateAsClientAsync (string targetHost, XX509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
139 return Impl.AuthenticateAsClientAsync (targetHost, clientCertificates, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
142 public Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
144 return Impl.AuthenticateAsServerAsync (serverCertificate);
147 public Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
149 return Impl.AuthenticateAsServerAsync (serverCertificate, clientCertificateRequired, (XSslProtocols)enabledSslProtocols, checkCertificateRevocation);
157 public int Read (byte[] buffer, int offset, int count)
159 return Impl.Read (buffer, offset, count);
162 public void Write (byte[] buffer)
167 public void Write (byte[] buffer, int offset, int count)
169 Impl.Write (buffer, offset, count);
172 public IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
174 return Impl.BeginRead (buffer, offset, count, asyncCallback, asyncState);
177 public int EndRead (IAsyncResult asyncResult)
179 return Impl.EndRead (asyncResult);
182 public IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
184 return Impl.BeginWrite (buffer, offset, count, asyncCallback, asyncState);
187 public void EndWrite (IAsyncResult asyncResult)
189 Impl.EndWrite (asyncResult);
192 public TransportContext TransportContext {
193 get { return (TransportContext)(object)Impl.TransportContext; }
196 public bool IsAuthenticated {
197 get { return Impl.IsAuthenticated; }
200 public bool IsMutuallyAuthenticated {
201 get { return Impl.IsMutuallyAuthenticated; }
204 public bool IsEncrypted {
205 get { return Impl.IsEncrypted; }
208 public bool IsSigned {
209 get { return Impl.IsSigned; }
212 public bool IsServer {
213 get { return Impl.IsServer; }
216 public CipherAlgorithmType CipherAlgorithm {
217 get { return (CipherAlgorithmType)Impl.CipherAlgorithm; }
220 public int CipherStrength {
221 get { return Impl.CipherStrength; }
224 public HashAlgorithmType HashAlgorithm {
225 get { return (HashAlgorithmType)Impl.HashAlgorithm; }
228 public int HashStrength {
229 get { return Impl.HashStrength; }
232 public ExchangeAlgorithmType KeyExchangeAlgorithm {
233 get { return (ExchangeAlgorithmType)Impl.KeyExchangeAlgorithm; }
236 public int KeyExchangeStrength {
237 get { return Impl.KeyExchangeStrength; }
240 public bool CanRead {
241 get { return Impl.CanRead; }
244 public bool CanTimeout {
245 get { return Impl.CanTimeout; }
248 public bool CanWrite {
249 get { return Impl.CanWrite; }
253 get { return Impl.Length; }
256 public long Position {
257 get { return Impl.Position; }
260 public void SetLength (long value)
262 Impl.SetLength (value);
265 public AuthenticatedStream AuthenticatedStream {
266 get { return (AuthenticatedStream)(object)Impl.AuthenticatedStream; }
269 public int ReadTimeout {
270 get { return Impl.ReadTimeout; }
271 set { Impl.ReadTimeout = value; }
274 public int WriteTimeout {
275 get { return Impl.WriteTimeout; }
276 set { Impl.WriteTimeout = value; }
279 public bool CheckCertRevocationStatus {
280 get { return Impl.CheckCertRevocationStatus; }
283 X509Certificate IMonoSslStream.InternalLocalCertificate {
284 get { return Impl.InternalLocalCertificate; }
287 public X509Certificate LocalCertificate {
288 get { return Impl.LocalCertificate; }
291 public X509Certificate RemoteCertificate {
292 get { return Impl.RemoteCertificate; }
295 public SslProtocols SslProtocol {
296 get { return (SslProtocols)Impl.SslProtocol; }
299 void CheckDisposed ()
302 throw new ObjectDisposedException ("MonoSslStream");
305 public void Dispose ()
308 GC.SuppressFinalize (this);
311 protected void Dispose (bool disposing)
313 if (impl != null && disposing) {