SslStream.cs 12 KB

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