SslStream.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. //
  2. // System.Net.Security.SslStream.cs
  3. //
  4. // Authors:
  5. // Tim Coleman ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. // Marek Safar ([email protected])
  8. //
  9. // Copyright (C) Tim Coleman, 2004
  10. // (c) 2004,2007 Novell, Inc. (http://www.novell.com)
  11. // Copyright 2011 Xamarin Inc.
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. #if SECURITY_DEP && !MONO_FEATURE_NEW_TLS
  34. #if MONO_X509_ALIAS
  35. extern alias PrebuiltSystem;
  36. #endif
  37. #if MONO_SECURITY_ALIAS
  38. extern alias MonoSecurity;
  39. #endif
  40. #if MONO_SECURITY_ALIAS
  41. using MonoCipherAlgorithmType = MonoSecurity::Mono.Security.Protocol.Tls.CipherAlgorithmType;
  42. using MonoHashAlgorithmType = MonoSecurity::Mono.Security.Protocol.Tls.HashAlgorithmType;
  43. using MonoExchangeAlgorithmType = MonoSecurity::Mono.Security.Protocol.Tls.ExchangeAlgorithmType;
  44. using MonoSecurityProtocolType = MonoSecurity::Mono.Security.Protocol.Tls.SecurityProtocolType;
  45. using MonoSecurity::Mono.Security.Protocol.Tls;
  46. using MonoSecurity::Mono.Security.Interface;
  47. #else
  48. using MonoCipherAlgorithmType = Mono.Security.Protocol.Tls.CipherAlgorithmType;
  49. using MonoHashAlgorithmType = Mono.Security.Protocol.Tls.HashAlgorithmType;
  50. using MonoExchangeAlgorithmType = Mono.Security.Protocol.Tls.ExchangeAlgorithmType;
  51. using MonoSecurityProtocolType = Mono.Security.Protocol.Tls.SecurityProtocolType;
  52. using Mono.Security.Protocol.Tls;
  53. using Mono.Security.Interface;
  54. #endif
  55. #if MONO_X509_ALIAS
  56. using X509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
  57. #endif
  58. using CipherAlgorithmType = System.Security.Authentication.CipherAlgorithmType;
  59. using HashAlgorithmType = System.Security.Authentication.HashAlgorithmType;
  60. using ExchangeAlgorithmType = System.Security.Authentication.ExchangeAlgorithmType;
  61. using System;
  62. using System.IO;
  63. using System.Net;
  64. using System.Security.Authentication;
  65. using System.Security.Cryptography.X509Certificates;
  66. using System.Security.Principal;
  67. using System.Security.Cryptography;
  68. using System.Threading.Tasks;
  69. using MNS = Mono.Net.Security;
  70. namespace System.Net.Security
  71. {
  72. /*
  73. * These two are defined by the referencesource; add them heere to make
  74. * it easy to switch between the two implementations.
  75. */
  76. internal delegate bool RemoteCertValidationCallback (
  77. string host,
  78. X509Certificate certificate,
  79. X509Chain chain,
  80. SslPolicyErrors sslPolicyErrors);
  81. internal delegate X509Certificate LocalCertSelectionCallback (
  82. string targetHost,
  83. X509CertificateCollection localCertificates,
  84. X509Certificate remoteCertificate,
  85. string[] acceptableIssuers);
  86. [MonoTODO ("Non-X509Certificate2 certificate is not supported")]
  87. public class SslStream : AuthenticatedStream
  88. {
  89. #region Fields
  90. SslStreamBase ssl_stream;
  91. RemoteCertificateValidationCallback validation_callback;
  92. LocalCertificateSelectionCallback selection_callback;
  93. #endregion // Fields
  94. #region Constructors
  95. public SslStream (Stream innerStream)
  96. : this (innerStream, false)
  97. {
  98. }
  99. public SslStream (Stream innerStream, bool leaveInnerStreamOpen)
  100. : base (innerStream, leaveInnerStreamOpen)
  101. {
  102. }
  103. [MonoTODO ("userCertificateValidationCallback is not passed X509Chain and SslPolicyErrors correctly")]
  104. public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback)
  105. : this (innerStream, leaveInnerStreamOpen, userCertificateValidationCallback, null)
  106. {
  107. }
  108. [MonoTODO ("userCertificateValidationCallback is not passed X509Chain and SslPolicyErrors correctly")]
  109. public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback, LocalCertificateSelectionCallback userCertificateSelectionCallback)
  110. : base (innerStream, leaveInnerStreamOpen)
  111. {
  112. // they are nullable.
  113. validation_callback = userCertificateValidationCallback;
  114. selection_callback = userCertificateSelectionCallback;
  115. }
  116. #endregion // Constructors
  117. #region Properties
  118. public override bool CanRead {
  119. get { return InnerStream.CanRead; }
  120. }
  121. public override bool CanSeek {
  122. get { return InnerStream.CanSeek; }
  123. }
  124. public override bool CanTimeout {
  125. get { return InnerStream.CanTimeout; }
  126. }
  127. public override bool CanWrite {
  128. get { return InnerStream.CanWrite; }
  129. }
  130. public override long Length {
  131. get { return InnerStream.Length; }
  132. }
  133. public override long Position {
  134. get { return InnerStream.Position; }
  135. set {
  136. throw new NotSupportedException ("This stream does not support seek operations");
  137. }
  138. }
  139. // AuthenticatedStream overrides
  140. public override bool IsAuthenticated {
  141. get { return ssl_stream != null; }
  142. }
  143. public override bool IsEncrypted {
  144. get { return IsAuthenticated; }
  145. }
  146. public override bool IsMutuallyAuthenticated {
  147. get { return IsAuthenticated && (IsServer ? RemoteCertificate != null : LocalCertificate != null); }
  148. }
  149. public override bool IsServer {
  150. get { return ssl_stream is SslServerStream; }
  151. }
  152. public override bool IsSigned {
  153. get { return IsAuthenticated; }
  154. }
  155. public override int ReadTimeout {
  156. get { return InnerStream.ReadTimeout; }
  157. set { InnerStream.ReadTimeout = value; }
  158. }
  159. public override int WriteTimeout {
  160. get { return InnerStream.WriteTimeout; }
  161. set { InnerStream.WriteTimeout = value; }
  162. }
  163. // SslStream
  164. public virtual bool CheckCertRevocationStatus {
  165. get {
  166. if (!IsAuthenticated)
  167. return false;
  168. return ssl_stream.CheckCertRevocationStatus;
  169. }
  170. }
  171. public virtual CipherAlgorithmType CipherAlgorithm {
  172. get {
  173. CheckConnectionAuthenticated ();
  174. switch (ssl_stream.CipherAlgorithm) {
  175. case MonoCipherAlgorithmType.Des:
  176. return CipherAlgorithmType.Des;
  177. case MonoCipherAlgorithmType.None:
  178. return CipherAlgorithmType.None;
  179. case MonoCipherAlgorithmType.Rc2:
  180. return CipherAlgorithmType.Rc2;
  181. case MonoCipherAlgorithmType.Rc4:
  182. return CipherAlgorithmType.Rc4;
  183. case MonoCipherAlgorithmType.SkipJack:
  184. break;
  185. case MonoCipherAlgorithmType.TripleDes:
  186. return CipherAlgorithmType.TripleDes;
  187. case MonoCipherAlgorithmType.Rijndael:
  188. switch (ssl_stream.CipherStrength) {
  189. case 128:
  190. return CipherAlgorithmType.Aes128;
  191. case 192:
  192. return CipherAlgorithmType.Aes192;
  193. case 256:
  194. return CipherAlgorithmType.Aes256;
  195. }
  196. break;
  197. }
  198. throw new InvalidOperationException ("Not supported cipher algorithm is in use. It is likely a bug in SslStream.");
  199. }
  200. }
  201. public virtual int CipherStrength {
  202. get {
  203. CheckConnectionAuthenticated ();
  204. return ssl_stream.CipherStrength;
  205. }
  206. }
  207. public virtual HashAlgorithmType HashAlgorithm {
  208. get {
  209. CheckConnectionAuthenticated ();
  210. switch (ssl_stream.HashAlgorithm) {
  211. case MonoHashAlgorithmType.Md5:
  212. return HashAlgorithmType.Md5;
  213. case MonoHashAlgorithmType.None:
  214. return HashAlgorithmType.None;
  215. case MonoHashAlgorithmType.Sha1:
  216. return HashAlgorithmType.Sha1;
  217. }
  218. throw new InvalidOperationException ("Not supported hash algorithm is in use. It is likely a bug in SslStream.");
  219. }
  220. }
  221. public virtual int HashStrength {
  222. get {
  223. CheckConnectionAuthenticated ();
  224. return ssl_stream.HashStrength;
  225. }
  226. }
  227. public virtual ExchangeAlgorithmType KeyExchangeAlgorithm {
  228. get {
  229. CheckConnectionAuthenticated ();
  230. switch (ssl_stream.KeyExchangeAlgorithm) {
  231. case MonoExchangeAlgorithmType.DiffieHellman:
  232. return ExchangeAlgorithmType.DiffieHellman;
  233. case MonoExchangeAlgorithmType.Fortezza:
  234. break;
  235. case MonoExchangeAlgorithmType.None:
  236. return ExchangeAlgorithmType.None;
  237. case MonoExchangeAlgorithmType.RsaKeyX:
  238. return ExchangeAlgorithmType.RsaKeyX;
  239. case MonoExchangeAlgorithmType.RsaSign:
  240. return ExchangeAlgorithmType.RsaSign;
  241. }
  242. throw new InvalidOperationException ("Not supported exchange algorithm is in use. It is likely a bug in SslStream.");
  243. }
  244. }
  245. public virtual int KeyExchangeStrength {
  246. get {
  247. CheckConnectionAuthenticated ();
  248. return ssl_stream.KeyExchangeStrength;
  249. }
  250. }
  251. public virtual X509Certificate LocalCertificate {
  252. get {
  253. CheckConnectionAuthenticated ();
  254. return IsServer ? ssl_stream.ServerCertificate : ((SslClientStream) ssl_stream).SelectedClientCertificate;
  255. }
  256. }
  257. public virtual X509Certificate RemoteCertificate {
  258. get {
  259. CheckConnectionAuthenticated ();
  260. return !IsServer ? ssl_stream.ServerCertificate : ((SslServerStream) ssl_stream).ClientCertificate;
  261. }
  262. }
  263. public virtual SslProtocols SslProtocol {
  264. get {
  265. CheckConnectionAuthenticated ();
  266. switch (ssl_stream.SecurityProtocol) {
  267. case MonoSecurityProtocolType.Default:
  268. return SslProtocols.Default;
  269. case MonoSecurityProtocolType.Ssl2:
  270. return SslProtocols.Ssl2;
  271. case MonoSecurityProtocolType.Ssl3:
  272. return SslProtocols.Ssl3;
  273. case MonoSecurityProtocolType.Tls:
  274. return SslProtocols.Tls;
  275. }
  276. throw new InvalidOperationException ("Not supported SSL/TLS protocol is in use. It is likely a bug in SslStream.");
  277. }
  278. }
  279. #endregion // Properties
  280. #region Methods
  281. /*
  282. AsymmetricAlgorithm GetPrivateKey (X509Certificate cert, string targetHost)
  283. {
  284. // FIXME: what can I do for non-X509Certificate2 ?
  285. X509Certificate2 cert2 = cert as X509Certificate2;
  286. return cert2 != null ? cert2.PrivateKey : null;
  287. }
  288. */
  289. X509Certificate OnCertificateSelection (X509CertificateCollection clientCerts, X509Certificate serverCert, string targetHost, X509CertificateCollection serverRequestedCerts)
  290. {
  291. string [] acceptableIssuers = new string [serverRequestedCerts != null ? serverRequestedCerts.Count : 0];
  292. for (int i = 0; i < acceptableIssuers.Length; i++)
  293. acceptableIssuers [i] = serverRequestedCerts [i].GetIssuerName ();
  294. return selection_callback (this, targetHost, clientCerts, serverCert, acceptableIssuers);
  295. }
  296. public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
  297. {
  298. return BeginAuthenticateAsClient (targetHost, new X509CertificateCollection (), SslProtocols.Tls, false, asyncCallback, asyncState);
  299. }
  300. public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
  301. {
  302. if (IsAuthenticated)
  303. throw new InvalidOperationException ("This SslStream is already authenticated");
  304. SslClientStream s = new SslClientStream (InnerStream, targetHost, !LeaveInnerStreamOpen, GetMonoSslProtocol (enabledSslProtocols), clientCertificates);
  305. s.CheckCertRevocationStatus = checkCertificateRevocation;
  306. // Due to the Mono.Security internal, it cannot reuse
  307. // the delegated argument, as Mono.Security creates
  308. // another instance of X509Certificate which lacks
  309. // private key but is filled the private key via this
  310. // delegate.
  311. s.PrivateKeyCertSelectionDelegate = delegate (X509Certificate cert, string host) {
  312. string hash = cert.GetCertHashString ();
  313. // ... so, we cannot use the delegate argument.
  314. foreach (X509Certificate cc in clientCertificates) {
  315. if (cc.GetCertHashString () != hash)
  316. continue;
  317. X509Certificate2 cert2 = cc as X509Certificate2;
  318. cert2 = cert2 ?? new X509Certificate2 (cc);
  319. return cert2.PrivateKey;
  320. }
  321. return null;
  322. };
  323. // Even if validation_callback is null this allows us to verify requests where the user
  324. // does not provide a verification callback but attempts to authenticate with the website
  325. // as a client (see https://bugzilla.xamarin.com/show_bug.cgi?id=18962 for an example)
  326. var settings = new MonoTlsSettings ();
  327. settings.RemoteCertificateValidationCallback = MNS.Private.CallbackHelpers.PublicToMono (validation_callback);
  328. var helper = MNS.ChainValidationHelper.Create (ref settings, null);
  329. s.ServerCertValidation2 += (certs) => helper.ValidateChain (targetHost, certs);
  330. if (selection_callback != null)
  331. s.ClientCertSelectionDelegate = OnCertificateSelection;
  332. ssl_stream = s;
  333. return BeginWrite (new byte [0], 0, 0, asyncCallback, asyncState);
  334. }
  335. public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
  336. {
  337. CheckConnectionAuthenticated ();
  338. return ssl_stream.BeginRead (buffer, offset, count, asyncCallback, asyncState);
  339. }
  340. public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
  341. {
  342. return BeginAuthenticateAsServer (serverCertificate, false, SslProtocols.Tls, false, asyncCallback, asyncState);
  343. }
  344. public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
  345. {
  346. if (IsAuthenticated)
  347. throw new InvalidOperationException ("This SslStream is already authenticated");
  348. SslServerStream s = new SslServerStream (InnerStream, serverCertificate, clientCertificateRequired, !LeaveInnerStreamOpen, GetMonoSslProtocol (enabledSslProtocols));
  349. s.CheckCertRevocationStatus = checkCertificateRevocation;
  350. // Due to the Mono.Security internal, it cannot reuse
  351. // the delegated argument, as Mono.Security creates
  352. // another instance of X509Certificate which lacks
  353. // private key but is filled the private key via this
  354. // delegate.
  355. s.PrivateKeyCertSelectionDelegate = delegate (X509Certificate cert, string targetHost) {
  356. // ... so, we cannot use the delegate argument.
  357. X509Certificate2 cert2 = serverCertificate as X509Certificate2 ?? new X509Certificate2 (serverCertificate);
  358. return cert2 != null ? cert2.PrivateKey : null;
  359. };
  360. if (validation_callback != null)
  361. s.ClientCertValidationDelegate = delegate (X509Certificate cert, int [] certErrors) {
  362. X509Chain chain = null;
  363. if (cert is X509Certificate2) {
  364. chain = new X509Chain ();
  365. chain.Build ((X509Certificate2) cert);
  366. }
  367. // FIXME: SslPolicyErrors is incomplete
  368. SslPolicyErrors errors = certErrors.Length > 0 ? SslPolicyErrors.RemoteCertificateChainErrors : SslPolicyErrors.None;
  369. return validation_callback (this, cert, chain, errors);
  370. };
  371. ssl_stream = s;
  372. return BeginWrite (new byte[0], 0, 0, asyncCallback, asyncState);
  373. }
  374. MonoSecurityProtocolType GetMonoSslProtocol (SslProtocols ms)
  375. {
  376. switch (ms) {
  377. case SslProtocols.Ssl2:
  378. return MonoSecurityProtocolType.Ssl2;
  379. case SslProtocols.Ssl3:
  380. return MonoSecurityProtocolType.Ssl3;
  381. case SslProtocols.Tls:
  382. return MonoSecurityProtocolType.Tls;
  383. default:
  384. return MonoSecurityProtocolType.Default;
  385. }
  386. }
  387. public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
  388. {
  389. CheckConnectionAuthenticated ();
  390. return ssl_stream.BeginWrite (buffer, offset, count, asyncCallback, asyncState);
  391. }
  392. public virtual void AuthenticateAsClient (string targetHost)
  393. {
  394. AuthenticateAsClient (targetHost, new X509CertificateCollection (), SslProtocols.Tls, false);
  395. }
  396. public virtual void AuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
  397. {
  398. EndAuthenticateAsClient (BeginAuthenticateAsClient (
  399. targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation, null, null));
  400. }
  401. public virtual void AuthenticateAsServer (X509Certificate serverCertificate)
  402. {
  403. AuthenticateAsServer (serverCertificate, false, SslProtocols.Tls, false);
  404. }
  405. public virtual void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
  406. {
  407. EndAuthenticateAsServer (BeginAuthenticateAsServer (
  408. serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation, null, null));
  409. }
  410. protected override void Dispose (bool disposing)
  411. {
  412. if (disposing) {
  413. if (ssl_stream != null)
  414. ssl_stream.Dispose ();
  415. ssl_stream = null;
  416. }
  417. base.Dispose (disposing);
  418. }
  419. public virtual void EndAuthenticateAsClient (IAsyncResult asyncResult)
  420. {
  421. CheckConnectionAuthenticated ();
  422. if (CanRead)
  423. ssl_stream.EndRead (asyncResult);
  424. else
  425. ssl_stream.EndWrite (asyncResult);
  426. }
  427. public virtual void EndAuthenticateAsServer (IAsyncResult asyncResult)
  428. {
  429. CheckConnectionAuthenticated ();
  430. if (CanRead)
  431. ssl_stream.EndRead (asyncResult);
  432. else
  433. ssl_stream.EndWrite (asyncResult);
  434. }
  435. public override int EndRead (IAsyncResult asyncResult)
  436. {
  437. CheckConnectionAuthenticated ();
  438. return ssl_stream.EndRead (asyncResult);
  439. }
  440. public override void EndWrite (IAsyncResult asyncResult)
  441. {
  442. CheckConnectionAuthenticated ();
  443. ssl_stream.EndWrite (asyncResult);
  444. }
  445. public override void Flush ()
  446. {
  447. CheckConnectionAuthenticated ();
  448. InnerStream.Flush ();
  449. }
  450. public override int Read (byte[] buffer, int offset, int count)
  451. {
  452. return EndRead (BeginRead (buffer, offset, count, null, null));
  453. }
  454. public override long Seek (long offset, SeekOrigin origin)
  455. {
  456. throw new NotSupportedException ("This stream does not support seek operations");
  457. }
  458. public override void SetLength (long value)
  459. {
  460. InnerStream.SetLength (value);
  461. }
  462. public override void Write (byte[] buffer, int offset, int count)
  463. {
  464. EndWrite (BeginWrite (buffer, offset, count, null, null));
  465. }
  466. public void Write (byte[] buffer)
  467. {
  468. Write (buffer, 0, buffer.Length);
  469. }
  470. void CheckConnectionAuthenticated ()
  471. {
  472. if (!IsAuthenticated)
  473. throw new InvalidOperationException ("This operation is invalid until it is successfully authenticated");
  474. }
  475. public virtual Task AuthenticateAsClientAsync (string targetHost)
  476. {
  477. return Task.Factory.FromAsync (BeginAuthenticateAsClient, EndAuthenticateAsClient, targetHost, null);
  478. }
  479. public virtual Task AuthenticateAsClientAsync (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
  480. {
  481. var t = Tuple.Create (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation, this);
  482. return Task.Factory.FromAsync ((callback, state) => {
  483. var d = (Tuple<string, X509CertificateCollection, SslProtocols, bool, SslStream>) state;
  484. return d.Item5.BeginAuthenticateAsClient (d.Item1, d.Item2, d.Item3, d.Item4, callback, null);
  485. }, EndAuthenticateAsClient, t);
  486. }
  487. public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
  488. {
  489. return Task.Factory.FromAsync (BeginAuthenticateAsServer, EndAuthenticateAsServer, serverCertificate, null);
  490. }
  491. public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
  492. {
  493. var t = Tuple.Create (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation, this);
  494. return Task.Factory.FromAsync ((callback, state) => {
  495. var d = (Tuple<X509Certificate, bool, SslProtocols, bool, SslStream>) state;
  496. return d.Item5.BeginAuthenticateAsServer (d.Item1, d.Item2, d.Item3, d.Item4, callback, null);
  497. }, EndAuthenticateAsServer, t);
  498. }
  499. #endregion // Methods
  500. }
  501. }
  502. #endif