MqttTcpChannel.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #if !WINDOWS_UWP
  2. using MQTTnet.Channel;
  3. using MQTTnet.Client.Options;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Security;
  8. using System.Net.Sockets;
  9. using System.Runtime.ExceptionServices;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MQTTnet.Implementations
  14. {
  15. public sealed class MqttTcpChannel : IDisposable, IMqttChannel
  16. {
  17. readonly IMqttClientOptions _clientOptions;
  18. readonly MqttClientTcpOptions _options;
  19. Stream _stream;
  20. public MqttTcpChannel(IMqttClientOptions clientOptions)
  21. {
  22. _clientOptions = clientOptions ?? throw new ArgumentNullException(nameof(clientOptions));
  23. _options = (MqttClientTcpOptions)clientOptions.ChannelOptions;
  24. IsSecureConnection = clientOptions.ChannelOptions?.TlsOptions?.UseTls == true;
  25. }
  26. public MqttTcpChannel(Stream stream, string endpoint, X509Certificate2 clientCertificate)
  27. {
  28. _stream = stream ?? throw new ArgumentNullException(nameof(stream));
  29. Endpoint = endpoint;
  30. IsSecureConnection = stream is SslStream;
  31. ClientCertificate = clientCertificate;
  32. }
  33. public string Endpoint { get; private set; }
  34. public bool IsSecureConnection { get; }
  35. public X509Certificate2 ClientCertificate { get; }
  36. public async Task ConnectAsync(CancellationToken cancellationToken)
  37. {
  38. CrossPlatformSocket socket = null;
  39. try
  40. {
  41. if (_options.AddressFamily == AddressFamily.Unspecified)
  42. {
  43. socket = new CrossPlatformSocket();
  44. }
  45. else
  46. {
  47. socket = new CrossPlatformSocket(_options.AddressFamily);
  48. }
  49. socket.ReceiveBufferSize = _options.BufferSize;
  50. socket.SendBufferSize = _options.BufferSize;
  51. socket.NoDelay = _options.NoDelay;
  52. if (_options.DualMode.HasValue)
  53. {
  54. // It is important to avoid setting the flag if no specific value is set by the user
  55. // because on IPv4 only networks the setter will always throw an exception. Regardless
  56. // of the actual value.
  57. socket.DualMode = _options.DualMode.Value;
  58. }
  59. await socket.ConnectAsync(_options.Server, _options.GetPort(), cancellationToken).ConfigureAwait(false);
  60. cancellationToken.ThrowIfCancellationRequested();
  61. var networkStream = socket.GetStream();
  62. if (_options.TlsOptions?.UseTls == true)
  63. {
  64. var sslStream = new SslStream(networkStream, false, InternalUserCertificateValidationCallback);
  65. try
  66. {
  67. await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), _options.TlsOptions.SslProtocol, !_options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
  68. }
  69. catch
  70. {
  71. sslStream.Dispose();
  72. throw;
  73. }
  74. _stream = sslStream;
  75. }
  76. else
  77. {
  78. _stream = networkStream;
  79. }
  80. Endpoint = socket.RemoteEndPoint?.ToString();
  81. }
  82. catch (Exception)
  83. {
  84. socket?.Dispose();
  85. throw;
  86. }
  87. }
  88. public Task DisconnectAsync(CancellationToken cancellationToken)
  89. {
  90. Dispose();
  91. return Task.FromResult(0);
  92. }
  93. public async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  94. {
  95. if (buffer is null) throw new ArgumentNullException(nameof(buffer));
  96. try
  97. {
  98. // Workaround for: https://github.com/dotnet/corefx/issues/24430
  99. using (cancellationToken.Register(Dispose))
  100. {
  101. cancellationToken.ThrowIfCancellationRequested();
  102. return await _stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
  103. }
  104. }
  105. catch (ObjectDisposedException)
  106. {
  107. return -1;
  108. }
  109. catch (IOException exception)
  110. {
  111. if (exception.InnerException is SocketException socketException)
  112. {
  113. ExceptionDispatchInfo.Capture(socketException).Throw();
  114. }
  115. throw;
  116. }
  117. }
  118. public async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  119. {
  120. if (buffer is null) throw new ArgumentNullException(nameof(buffer));
  121. try
  122. {
  123. // Workaround for: https://github.com/dotnet/corefx/issues/24430
  124. using (cancellationToken.Register(Dispose))
  125. {
  126. cancellationToken.ThrowIfCancellationRequested();
  127. await _stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
  128. }
  129. }
  130. catch (ObjectDisposedException)
  131. {
  132. return;
  133. }
  134. catch (IOException exception)
  135. {
  136. if (exception.InnerException is SocketException socketException)
  137. {
  138. ExceptionDispatchInfo.Capture(socketException).Throw();
  139. }
  140. throw;
  141. }
  142. }
  143. public void Dispose()
  144. {
  145. // When the stream is disposed it will also close the socket and this will also dispose it.
  146. // So there is no need to dispose the socket again.
  147. // https://stackoverflow.com/questions/3601521/should-i-manually-dispose-the-socket-after-closing-it
  148. try
  149. {
  150. _stream?.Dispose();
  151. }
  152. catch (ObjectDisposedException)
  153. {
  154. }
  155. catch (NullReferenceException)
  156. {
  157. }
  158. _stream = null;
  159. }
  160. bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  161. {
  162. #region OBSOLETE
  163. var certificateValidationCallback = _options?.TlsOptions?.CertificateValidationCallback;
  164. if (certificateValidationCallback != null)
  165. {
  166. return certificateValidationCallback(x509Certificate, chain, sslPolicyErrors, _clientOptions);
  167. }
  168. #endregion
  169. var certificateValidationHandler = _options?.TlsOptions?.CertificateValidationHandler;
  170. if (certificateValidationHandler != null)
  171. {
  172. var context = new MqttClientCertificateValidationCallbackContext
  173. {
  174. Certificate = x509Certificate,
  175. Chain = chain,
  176. SslPolicyErrors = sslPolicyErrors,
  177. ClientOptions = _options
  178. };
  179. return certificateValidationHandler(context);
  180. }
  181. if (sslPolicyErrors == SslPolicyErrors.None)
  182. {
  183. return true;
  184. }
  185. if (chain.ChainStatus.Any(c => c.Status == X509ChainStatusFlags.RevocationStatusUnknown || c.Status == X509ChainStatusFlags.Revoked || c.Status == X509ChainStatusFlags.OfflineRevocation))
  186. {
  187. if (!_options.TlsOptions.IgnoreCertificateRevocationErrors)
  188. {
  189. return false;
  190. }
  191. }
  192. if (chain.ChainStatus.Any(c => c.Status == X509ChainStatusFlags.PartialChain))
  193. {
  194. if (!_options.TlsOptions.IgnoreCertificateChainErrors)
  195. {
  196. return false;
  197. }
  198. }
  199. return _options.TlsOptions.AllowUntrustedCertificates;
  200. }
  201. X509CertificateCollection LoadCertificates()
  202. {
  203. var certificates = new X509CertificateCollection();
  204. if (_options.TlsOptions.Certificates == null)
  205. {
  206. return certificates;
  207. }
  208. foreach (var certificate in _options.TlsOptions.Certificates)
  209. {
  210. certificates.Add(certificate);
  211. }
  212. return certificates;
  213. }
  214. }
  215. }
  216. #endif