2 // DotNetSslStreamImpl.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 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 MSI = Mono.Security.Interface;
38 namespace Mono.Security.Providers.DotNet
40 class DotNetSslStreamImpl : MSI.IMonoSslStream
42 DotNetTlsProvider provider;
45 internal SslStream Impl {
52 public DotNetSslStreamImpl (
53 Stream innerStream, bool leaveInnerStreamOpen, DotNetTlsProvider provider,
54 RemoteCertificateValidationCallback userCertificateValidationCallback,
55 LocalCertificateSelectionCallback userCertificateSelectionCallback)
57 this.provider = provider;
58 impl = new SslStream (
59 innerStream, leaveInnerStreamOpen,
60 userCertificateValidationCallback,
61 userCertificateSelectionCallback);
64 public void AuthenticateAsClient (string targetHost)
66 Impl.AuthenticateAsClient (targetHost);
69 public void AuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
71 Impl.AuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
74 public IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
76 return Impl.BeginAuthenticateAsClient (targetHost, asyncCallback, asyncState);
79 public IAsyncResult BeginAuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
81 return Impl.BeginAuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
84 public void EndAuthenticateAsClient (IAsyncResult asyncResult)
86 Impl.EndAuthenticateAsClient (asyncResult);
89 public void AuthenticateAsServer (X509Certificate serverCertificate)
91 Impl.AuthenticateAsServer (serverCertificate);
94 public void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
96 Impl.AuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
99 public IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
101 return Impl.BeginAuthenticateAsServer (serverCertificate, asyncCallback, asyncState);
104 public IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
106 return Impl.BeginAuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
109 public void EndAuthenticateAsServer (IAsyncResult asyncResult)
111 Impl.EndAuthenticateAsServer (asyncResult);
114 public Task AuthenticateAsClientAsync (string targetHost)
116 return Impl.AuthenticateAsClientAsync (targetHost);
119 public Task AuthenticateAsClientAsync (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
121 return Impl.AuthenticateAsClientAsync (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
124 public Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
126 return Impl.AuthenticateAsServerAsync (serverCertificate);
129 public Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
131 return Impl.AuthenticateAsServerAsync (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
139 public int Read (byte[] buffer, int offset, int count)
141 return Impl.Read (buffer, offset, count);
144 public void Write (byte[] buffer)
149 public void Write (byte[] buffer, int offset, int count)
151 Impl.Write (buffer, offset, count);
154 public IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
156 return Impl.BeginRead (buffer, offset, count, asyncCallback, asyncState);
159 public int EndRead (IAsyncResult asyncResult)
161 return Impl.EndRead (asyncResult);
164 public IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
166 return Impl.BeginWrite (buffer, offset, count, asyncCallback, asyncState);
169 public void EndWrite (IAsyncResult asyncResult)
171 Impl.EndWrite (asyncResult);
174 public TransportContext TransportContext {
175 get { throw new NotSupportedException (); }
178 public bool IsAuthenticated {
179 get { return Impl.IsAuthenticated; }
182 public bool IsMutuallyAuthenticated {
183 get { return Impl.IsMutuallyAuthenticated; }
186 public bool IsEncrypted {
187 get { return Impl.IsEncrypted; }
190 public bool IsSigned {
191 get { return Impl.IsSigned; }
194 public bool IsServer {
195 get { return Impl.IsServer; }
198 public CipherAlgorithmType CipherAlgorithm {
199 get { return Impl.CipherAlgorithm; }
202 public int CipherStrength {
203 get { return Impl.CipherStrength; }
206 public HashAlgorithmType HashAlgorithm {
207 get { return Impl.HashAlgorithm; }
210 public int HashStrength {
211 get { return Impl.HashStrength; }
214 public ExchangeAlgorithmType KeyExchangeAlgorithm {
215 get { return Impl.KeyExchangeAlgorithm; }
218 public int KeyExchangeStrength {
219 get { return KeyExchangeStrength; }
222 public bool CanRead {
223 get { return Impl.CanRead; }
226 public bool CanTimeout {
227 get { return Impl.CanTimeout; }
230 public bool CanWrite {
231 get { return Impl.CanWrite; }
235 get { return Impl.Length; }
238 public long Position {
239 get { return Impl.Position; }
242 public void SetLength (long value)
244 Impl.SetLength (value);
247 public AuthenticatedStream AuthenticatedStream {
251 public int ReadTimeout {
252 get { return Impl.ReadTimeout; }
253 set { Impl.ReadTimeout = value; }
256 public int WriteTimeout {
257 get { return Impl.WriteTimeout; }
258 set { Impl.WriteTimeout = value; }
261 public bool CheckCertRevocationStatus {
262 get { return Impl.CheckCertRevocationStatus; }
265 public X509Certificate InternalLocalCertificate {
268 return LocalCertificate;
275 public X509Certificate LocalCertificate {
276 get { return Impl.LocalCertificate; }
279 public X509Certificate RemoteCertificate {
280 get { return Impl.RemoteCertificate; }
283 public SslProtocols SslProtocol {
284 get { return Impl.SslProtocol; }
287 MSI.MonoTlsProvider MSI.IMonoSslStream.Provider {
288 get { return provider; }
291 MSI.MonoTlsConnectionInfo MSI.IMonoSslStream.GetConnectionInfo ()
296 void CheckDisposed ()
299 throw new ObjectDisposedException ("MonoSslStream");
302 public void Dispose ()
305 GC.SuppressFinalize (this);
308 protected void Dispose (bool disposing)
310 if (impl != null && disposing) {