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
29 #if MONO_SECURITY_ALIAS
30 extern alias MonoSecurity;
33 #if MONO_SECURITY_ALIAS
34 using MonoSecurity::Mono.Security.Interface;
36 using Mono.Security.Interface;
39 using CipherAlgorithmType = System.Security.Authentication.CipherAlgorithmType;
40 using HashAlgorithmType = System.Security.Authentication.HashAlgorithmType;
41 using ExchangeAlgorithmType = System.Security.Authentication.ExchangeAlgorithmType;
46 using System.Net.Security;
47 using System.Security.Authentication;
48 using System.Security.Cryptography.X509Certificates;
49 using System.Security.Permissions;
50 using System.Security.Principal;
51 using System.Security.Cryptography;
52 using System.Threading.Tasks;
54 using MNS = Mono.Net.Security;
56 namespace System.Net.Security
59 * These two are defined by the referencesource; add them heere to make
60 * it easy to switch between the two implementations.
63 internal delegate bool RemoteCertValidationCallback (
65 X509Certificate certificate,
67 SslPolicyErrors sslPolicyErrors);
69 internal delegate X509Certificate LocalCertSelectionCallback (
71 X509CertificateCollection localCertificates,
72 X509Certificate remoteCertificate,
73 string[] acceptableIssuers);
75 public class SslStream : AuthenticatedStream
78 MonoTlsProvider provider;
81 internal IMonoSslStream Impl {
88 internal MonoTlsProvider Provider {
95 static MonoTlsProvider GetProvider ()
97 return MonoTlsProviderFactory.GetProvider ();
100 public SslStream (Stream innerStream)
101 : this (innerStream, false)
105 public SslStream (Stream innerStream, bool leaveInnerStreamOpen)
106 : base (innerStream, leaveInnerStreamOpen)
108 provider = GetProvider ();
109 impl = provider.CreateSslStreamInternal (this, innerStream, leaveInnerStreamOpen, null);
112 public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback)
113 : this (innerStream, leaveInnerStreamOpen, userCertificateValidationCallback, null)
117 public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback, LocalCertificateSelectionCallback userCertificateSelectionCallback)
118 : base (innerStream, leaveInnerStreamOpen)
120 provider = GetProvider ();
121 var settings = MonoTlsSettings.CopyDefaultSettings ();
122 settings.RemoteCertificateValidationCallback = MNS.Private.CallbackHelpers.PublicToMono (userCertificateValidationCallback);
123 settings.ClientCertificateSelectionCallback = MNS.Private.CallbackHelpers.PublicToMono (userCertificateSelectionCallback);
124 impl = provider.CreateSslStream (innerStream, leaveInnerStreamOpen, settings);
127 [MonoLimitation ("encryptionPolicy is ignored")]
128 public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback, LocalCertificateSelectionCallback userCertificateSelectionCallback, EncryptionPolicy encryptionPolicy)
129 : this (innerStream, leaveInnerStreamOpen, userCertificateValidationCallback, userCertificateSelectionCallback)
133 internal SslStream (Stream innerStream, bool leaveInnerStreamOpen, MonoTlsProvider provider, MonoTlsSettings settings)
134 : base (innerStream, leaveInnerStreamOpen)
136 this.provider = provider;
137 impl = provider.CreateSslStreamInternal (this, innerStream, leaveInnerStreamOpen, settings);
140 internal static IMonoSslStream CreateMonoSslStream (Stream innerStream, bool leaveInnerStreamOpen, MonoTlsProvider provider, MonoTlsSettings settings)
142 var sslStream = new SslStream (innerStream, leaveInnerStreamOpen, provider, settings);
143 return sslStream.Impl;
146 public virtual void AuthenticateAsClient (string targetHost)
148 Impl.AuthenticateAsClient (targetHost);
151 public virtual void AuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
153 Impl.AuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
156 // [HostProtection (ExternalThreading=true)]
157 public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
159 return Impl.BeginAuthenticateAsClient (targetHost, asyncCallback, asyncState);
162 // [HostProtection (ExternalThreading=true)]
163 public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
165 return Impl.BeginAuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
168 public virtual void EndAuthenticateAsClient (IAsyncResult asyncResult)
170 Impl.EndAuthenticateAsClient (asyncResult);
173 public virtual void AuthenticateAsServer (X509Certificate serverCertificate)
175 Impl.AuthenticateAsServer (serverCertificate);
178 public virtual void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
180 Impl.AuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
183 // [HostProtection (ExternalThreading=true)]
184 public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
186 return Impl.BeginAuthenticateAsServer (serverCertificate, asyncCallback, asyncState);
189 public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
191 return Impl.BeginAuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
194 public virtual void EndAuthenticateAsServer (IAsyncResult asyncResult)
196 Impl.EndAuthenticateAsServer (asyncResult);
199 public TransportContext TransportContext {
201 throw new NotSupportedException();
205 // [HostProtection (ExternalThreading=true)]
206 public virtual Task AuthenticateAsClientAsync (string targetHost)
208 return Impl.AuthenticateAsClientAsync (targetHost);
211 public virtual Task AuthenticateAsClientAsync (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
213 return Impl.AuthenticateAsClientAsync (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
216 public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
218 return Impl.AuthenticateAsServerAsync (serverCertificate);
221 public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
223 return Impl.AuthenticateAsServerAsync (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
226 public virtual Task ShutdownAsync ()
228 return Impl.ShutdownAsync ();
231 public override bool IsAuthenticated {
232 get { return Impl.IsAuthenticated; }
235 public override bool IsMutuallyAuthenticated {
236 get { return Impl.IsMutuallyAuthenticated; }
239 public override bool IsEncrypted {
240 get { return Impl.IsEncrypted; }
243 public override bool IsSigned {
244 get { return Impl.IsSigned; }
247 public override bool IsServer {
248 get { return Impl.IsServer; }
251 public virtual SslProtocols SslProtocol {
252 get { return (SslProtocols)Impl.SslProtocol; }
255 public virtual bool CheckCertRevocationStatus {
256 get { return Impl.CheckCertRevocationStatus; }
259 public virtual X509Certificate LocalCertificate {
260 get { return Impl.LocalCertificate; }
263 public virtual X509Certificate RemoteCertificate {
264 get { return Impl.RemoteCertificate; }
267 public virtual CipherAlgorithmType CipherAlgorithm {
268 get { return (CipherAlgorithmType)Impl.CipherAlgorithm; }
271 public virtual int CipherStrength {
272 get { return Impl.CipherStrength; }
275 public virtual HashAlgorithmType HashAlgorithm {
276 get { return (HashAlgorithmType)Impl.HashAlgorithm; }
279 public virtual int HashStrength {
280 get { return Impl.HashStrength; }
283 public virtual ExchangeAlgorithmType KeyExchangeAlgorithm {
284 get { return (ExchangeAlgorithmType)Impl.KeyExchangeAlgorithm; }
287 public virtual int KeyExchangeStrength {
288 get { return Impl.KeyExchangeStrength; }
291 public override bool CanSeek {
292 get { return false; }
295 public override bool CanRead {
296 get { return impl != null && impl.CanRead; }
299 public override bool CanTimeout {
300 get { return InnerStream.CanTimeout; }
303 public override bool CanWrite {
304 get { return impl != null && impl.CanWrite; }
307 public override int ReadTimeout {
308 get { return Impl.ReadTimeout; }
309 set { Impl.ReadTimeout = value; }
312 public override int WriteTimeout {
313 get { return Impl.WriteTimeout; }
314 set { Impl.WriteTimeout = value; }
317 public override long Length {
318 get { return Impl.Length; }
321 public override long Position {
322 get { return Impl.Position; }
324 throw new NotSupportedException (SR.GetString (SR.net_noseek));
328 public override void SetLength (long value)
330 Impl.SetLength (value);
333 public override long Seek (long offset, SeekOrigin origin)
335 throw new NotSupportedException (SR.GetString (SR.net_noseek));
338 public override void Flush ()
340 InnerStream.Flush ();
343 void CheckDisposed ()
346 throw new ObjectDisposedException ("SslStream");
349 protected override void Dispose (bool disposing)
352 if (impl != null && disposing) {
357 base.Dispose (disposing);
361 public override int Read (byte[] buffer, int offset, int count)
363 return Impl.Read (buffer, offset, count);
366 public void Write (byte[] buffer)
371 public override void Write (byte[] buffer, int offset, int count)
373 Impl.Write (buffer, offset, count);
376 // [HostProtection (ExternalThreading=true)]
377 public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
379 return Impl.BeginRead (buffer, offset, count, asyncCallback, asyncState);
382 public override int EndRead (IAsyncResult asyncResult)
384 return Impl.EndRead (asyncResult);
387 // [HostProtection (ExternalThreading=true)]
388 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
390 return Impl.BeginWrite (buffer, offset, count, asyncCallback, asyncState);
393 public override void EndWrite (IAsyncResult asyncResult)
395 Impl.EndWrite (asyncResult);
397 #else // !SECURITY_DEP
398 const string EXCEPTION_MESSAGE = "System.Net.Security.SslStream is not supported on the current platform.";
400 public SslStream (Stream innerStream)
401 : this (innerStream, false)
405 public SslStream (Stream innerStream, bool leaveInnerStreamOpen)
406 : base (innerStream, leaveInnerStreamOpen)
408 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
411 public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback)
412 : this (innerStream, leaveInnerStreamOpen)
416 public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback, LocalCertificateSelectionCallback userCertificateSelectionCallback)
417 : this (innerStream, leaveInnerStreamOpen)
421 public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback, LocalCertificateSelectionCallback userCertificateSelectionCallback, EncryptionPolicy encryptionPolicy)
422 : this (innerStream, leaveInnerStreamOpen)
426 public virtual void AuthenticateAsClient (string targetHost)
428 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
431 public virtual void AuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
433 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
436 public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
438 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
441 public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
443 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
446 public virtual void EndAuthenticateAsClient (IAsyncResult asyncResult)
448 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
451 public virtual void AuthenticateAsServer (X509Certificate serverCertificate)
453 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
456 public virtual void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
458 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
461 public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
463 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
466 public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
468 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
471 public virtual void EndAuthenticateAsServer (IAsyncResult asyncResult)
473 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
476 public TransportContext TransportContext {
477 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
480 public virtual Task AuthenticateAsClientAsync (string targetHost)
482 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
485 public virtual Task AuthenticateAsClientAsync (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
487 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
490 public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
492 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
495 public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
497 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
500 public override bool IsAuthenticated {
501 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
504 public override bool IsMutuallyAuthenticated {
505 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
508 public override bool IsEncrypted {
509 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
512 public override bool IsSigned {
513 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
516 public override bool IsServer {
517 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
520 public virtual SslProtocols SslProtocol {
521 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
524 public virtual bool CheckCertRevocationStatus {
525 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
528 public virtual X509Certificate LocalCertificate {
529 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
532 public virtual X509Certificate RemoteCertificate {
533 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
536 public virtual CipherAlgorithmType CipherAlgorithm {
537 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
540 public virtual int CipherStrength {
541 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
544 public virtual HashAlgorithmType HashAlgorithm {
545 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
548 public virtual int HashStrength {
549 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
552 public virtual ExchangeAlgorithmType KeyExchangeAlgorithm {
553 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
556 public virtual int KeyExchangeStrength {
557 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
560 public override bool CanSeek {
561 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
564 public override bool CanRead {
565 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
568 public override bool CanTimeout {
569 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
572 public override bool CanWrite {
573 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
576 public override int ReadTimeout {
577 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
578 set { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
581 public override int WriteTimeout {
582 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
583 set { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
586 public override long Length {
587 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
590 public override long Position {
591 get { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
592 set { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
595 public override void SetLength (long value)
597 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
600 public override long Seek (long offset, SeekOrigin origin)
602 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
605 public override void Flush ()
607 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
610 protected override void Dispose (bool disposing)
614 public override int Read (byte[] buffer, int offset, int count)
616 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
619 public void Write (byte[] buffer)
621 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
624 public override void Write (byte[] buffer, int offset, int count)
626 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
629 public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
631 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
634 public override int EndRead (IAsyncResult asyncResult)
636 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
639 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
641 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
644 public override void EndWrite (IAsyncResult asyncResult)
646 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);