You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

136 lines
4.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using System.Threading.Tasks;
  7. using Windows.Networking;
  8. using Windows.Networking.Sockets;
  9. using Windows.Security.Cryptography.Certificates;
  10. using MQTTnet.Core.Channel;
  11. using MQTTnet.Core.Client;
  12. namespace MQTTnet.Implementations
  13. {
  14. public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
  15. {
  16. private readonly MqttClientTcpOptions _options;
  17. private StreamSocket _socket;
  18. public MqttTcpChannel(MqttClientTcpOptions options)
  19. {
  20. _options = options ?? throw new ArgumentNullException(nameof(options));
  21. }
  22. public MqttTcpChannel(StreamSocket socket)
  23. {
  24. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  25. CreateStreams();
  26. }
  27. public Stream SendStream { get; private set; }
  28. public Stream ReceiveStream { get; private set; }
  29. public Stream RawReceiveStream { get; private set; }
  30. public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
  31. public async Task ConnectAsync()
  32. {
  33. if (_socket == null)
  34. {
  35. _socket = new StreamSocket();
  36. }
  37. if (!_options.TlsOptions.UseTls)
  38. {
  39. await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString());
  40. }
  41. else
  42. {
  43. _socket.Control.ClientCertificate = LoadCertificate(_options);
  44. foreach (var ignorableChainValidationResult in ResolveIgnorableServerCertificateErrors())
  45. {
  46. _socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult);
  47. }
  48. await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString(), SocketProtectionLevel.Tls12);
  49. }
  50. CreateStreams();
  51. }
  52. public Task DisconnectAsync()
  53. {
  54. Dispose();
  55. return Task.FromResult(0);
  56. }
  57. public void Dispose()
  58. {
  59. RawReceiveStream?.Dispose();
  60. RawReceiveStream = null;
  61. SendStream?.Dispose();
  62. SendStream = null;
  63. ReceiveStream?.Dispose();
  64. ReceiveStream = null;
  65. _socket?.Dispose();
  66. _socket = null;
  67. }
  68. private void CreateStreams()
  69. {
  70. SendStream = _socket.OutputStream.AsStreamForWrite();
  71. ReceiveStream = _socket.InputStream.AsStreamForRead();
  72. RawReceiveStream = ReceiveStream;
  73. }
  74. private static Certificate LoadCertificate(MqttClientTcpOptions options)
  75. {
  76. if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any())
  77. {
  78. return null;
  79. }
  80. if (options.TlsOptions.Certificates.Count > 1)
  81. {
  82. throw new NotSupportedException("Only one client certificate is supported for UWP.");
  83. }
  84. return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
  85. }
  86. private IEnumerable<ChainValidationResult> ResolveIgnorableServerCertificateErrors()
  87. {
  88. if (CustomIgnorableServerCertificateErrorsResolver != null)
  89. {
  90. return CustomIgnorableServerCertificateErrorsResolver(_options);
  91. }
  92. var result = new List<ChainValidationResult>();
  93. if (_options.TlsOptions.IgnoreCertificateRevocationErrors)
  94. {
  95. result.Add(ChainValidationResult.RevocationInformationMissing);
  96. //_socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Revoked); Not supported.
  97. result.Add(ChainValidationResult.RevocationFailure);
  98. }
  99. if (_options.TlsOptions.IgnoreCertificateChainErrors)
  100. {
  101. result.Add(ChainValidationResult.IncompleteChain);
  102. }
  103. if (_options.TlsOptions.AllowUntrustedCertificates)
  104. {
  105. result.Add(ChainValidationResult.Untrusted);
  106. }
  107. return result;
  108. }
  109. }
  110. }