SslStream.cs 12 KB

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