| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- #if !WINDOWS_UWP
- using MQTTnet.Channel;
- using MQTTnet.Client.Options;
- using System;
- using System.IO;
- using System.Linq;
- using System.Net.Security;
- using System.Net.Sockets;
- using System.Runtime.ExceptionServices;
- using System.Security.Cryptography.X509Certificates;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MQTTnet.Implementations
- {
- public sealed class MqttTcpChannel : IDisposable, IMqttChannel
- {
- readonly IMqttClientOptions _clientOptions;
- readonly MqttClientTcpOptions _options;
- Stream _stream;
- public MqttTcpChannel(IMqttClientOptions clientOptions)
- {
- _clientOptions = clientOptions ?? throw new ArgumentNullException(nameof(clientOptions));
- _options = (MqttClientTcpOptions)clientOptions.ChannelOptions;
- IsSecureConnection = clientOptions.ChannelOptions?.TlsOptions?.UseTls == true;
- }
- public MqttTcpChannel(Stream stream, string endpoint, X509Certificate2 clientCertificate)
- {
- _stream = stream ?? throw new ArgumentNullException(nameof(stream));
- Endpoint = endpoint;
- IsSecureConnection = stream is SslStream;
- ClientCertificate = clientCertificate;
- }
- public string Endpoint { get; private set; }
- public bool IsSecureConnection { get; }
- public X509Certificate2 ClientCertificate { get; }
- public async Task ConnectAsync(CancellationToken cancellationToken)
- {
- CrossPlatformSocket socket = null;
- try
- {
- if (_options.AddressFamily == AddressFamily.Unspecified)
- {
- socket = new CrossPlatformSocket();
- }
- else
- {
- socket = new CrossPlatformSocket(_options.AddressFamily);
- }
- socket.ReceiveBufferSize = _options.BufferSize;
- socket.SendBufferSize = _options.BufferSize;
- socket.NoDelay = _options.NoDelay;
- if (_options.DualMode.HasValue)
- {
- // It is important to avoid setting the flag if no specific value is set by the user
- // because on IPv4 only networks the setter will always throw an exception. Regardless
- // of the actual value.
- socket.DualMode = _options.DualMode.Value;
- }
- await socket.ConnectAsync(_options.Server, _options.GetPort(), cancellationToken).ConfigureAwait(false);
- cancellationToken.ThrowIfCancellationRequested();
- var networkStream = socket.GetStream();
- if (_options.TlsOptions?.UseTls == true)
- {
- var sslStream = new SslStream(networkStream, false, InternalUserCertificateValidationCallback);
- try
- {
- await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), _options.TlsOptions.SslProtocol, !_options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
- }
- catch
- {
- sslStream.Dispose();
- throw;
- }
- _stream = sslStream;
- }
- else
- {
- _stream = networkStream;
- }
- Endpoint = socket.RemoteEndPoint?.ToString();
- }
- catch (Exception)
- {
- socket?.Dispose();
- throw;
- }
- }
- public Task DisconnectAsync(CancellationToken cancellationToken)
- {
- Dispose();
- return Task.FromResult(0);
- }
- public async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
- {
- if (buffer is null) throw new ArgumentNullException(nameof(buffer));
- try
- {
- // Workaround for: https://github.com/dotnet/corefx/issues/24430
- using (cancellationToken.Register(Dispose))
- {
- cancellationToken.ThrowIfCancellationRequested();
- return await _stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
- }
- }
- catch (ObjectDisposedException)
- {
- return -1;
- }
- catch (IOException exception)
- {
- if (exception.InnerException is SocketException socketException)
- {
- ExceptionDispatchInfo.Capture(socketException).Throw();
- }
- throw;
- }
- }
- public async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
- {
- if (buffer is null) throw new ArgumentNullException(nameof(buffer));
- try
- {
- // Workaround for: https://github.com/dotnet/corefx/issues/24430
- using (cancellationToken.Register(Dispose))
- {
- cancellationToken.ThrowIfCancellationRequested();
- await _stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
- }
- }
- catch (ObjectDisposedException)
- {
- return;
- }
- catch (IOException exception)
- {
- if (exception.InnerException is SocketException socketException)
- {
- ExceptionDispatchInfo.Capture(socketException).Throw();
- }
- throw;
- }
- }
- public void Dispose()
- {
- // When the stream is disposed it will also close the socket and this will also dispose it.
- // So there is no need to dispose the socket again.
- // https://stackoverflow.com/questions/3601521/should-i-manually-dispose-the-socket-after-closing-it
- try
- {
- _stream?.Dispose();
- }
- catch (ObjectDisposedException)
- {
- }
- catch (NullReferenceException)
- {
- }
- _stream = null;
- }
- bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
- {
- #region OBSOLETE
- var certificateValidationCallback = _options?.TlsOptions?.CertificateValidationCallback;
- if (certificateValidationCallback != null)
- {
- return certificateValidationCallback(x509Certificate, chain, sslPolicyErrors, _clientOptions);
- }
- #endregion
- var certificateValidationHandler = _options?.TlsOptions?.CertificateValidationHandler;
- if (certificateValidationHandler != null)
- {
- var context = new MqttClientCertificateValidationCallbackContext
- {
- Certificate = x509Certificate,
- Chain = chain,
- SslPolicyErrors = sslPolicyErrors,
- ClientOptions = _options
- };
- return certificateValidationHandler(context);
- }
- if (sslPolicyErrors == SslPolicyErrors.None)
- {
- return true;
- }
- if (chain.ChainStatus.Any(c => c.Status == X509ChainStatusFlags.RevocationStatusUnknown || c.Status == X509ChainStatusFlags.Revoked || c.Status == X509ChainStatusFlags.OfflineRevocation))
- {
- if (!_options.TlsOptions.IgnoreCertificateRevocationErrors)
- {
- return false;
- }
- }
- if (chain.ChainStatus.Any(c => c.Status == X509ChainStatusFlags.PartialChain))
- {
- if (!_options.TlsOptions.IgnoreCertificateChainErrors)
- {
- return false;
- }
- }
- return _options.TlsOptions.AllowUntrustedCertificates;
- }
- X509CertificateCollection LoadCertificates()
- {
- var certificates = new X509CertificateCollection();
- if (_options.TlsOptions.Certificates == null)
- {
- return certificates;
- }
- foreach (var certificate in _options.TlsOptions.Certificates)
- {
- certificates.Add(certificate);
- }
- return certificates;
- }
- }
- }
- #endif
|