diff --git a/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs index 335de78..0203134 100644 --- a/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs +++ b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs @@ -17,8 +17,8 @@ namespace MQTTnet.Implementations { private CancellationTokenSource _cancellationTokenSource; private Socket _defaultEndpointSocket; - private Socket _sslEndpointSocket; - private X509Certificate2 _sslCertificate; + private Socket _tlsEndpointSocket; + private X509Certificate2 _tlsCertificate; private bool _isRunning; @@ -40,20 +40,20 @@ namespace MQTTnet.Implementations Task.Run(() => AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); } - if (options.SslEndpointOptions.IsEnabled) + if (options.TlsEndpointOptions.IsEnabled) { - if (options.SslEndpointOptions.Certificate == null) + if (options.TlsEndpointOptions.Certificate == null) { - throw new ArgumentException("SSL certificate is not set."); + throw new ArgumentException("TLS certificate is not set."); } - _sslCertificate = new X509Certificate2(options.SslEndpointOptions.Certificate); + _tlsCertificate = new X509Certificate2(options.TlsEndpointOptions.Certificate); - _sslEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _sslEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetSslEndpointPort())); - _sslEndpointSocket.Listen(options.ConnectionBacklog); + _tlsEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); + _tlsEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetTlsEndpointPort())); + _tlsEndpointSocket.Listen(options.ConnectionBacklog); - Task.Run(() => AcceptSslEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); + Task.Run(() => AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); } } @@ -67,8 +67,8 @@ namespace MQTTnet.Implementations _defaultEndpointSocket?.Dispose(); _defaultEndpointSocket = null; - _sslEndpointSocket?.Dispose(); - _sslEndpointSocket = null; + _tlsEndpointSocket?.Dispose(); + _tlsEndpointSocket = null; } public void Dispose() @@ -93,7 +93,7 @@ namespace MQTTnet.Implementations } } - private async Task AcceptSslEndpointConnectionsAsync(CancellationToken cancellationToken) + private async Task AcceptTlsEndpointConnectionsAsync(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { @@ -102,14 +102,14 @@ namespace MQTTnet.Implementations var clientSocket = await _defaultEndpointSocket.AcceptAsync(); var sslStream = new SslStream(new NetworkStream(clientSocket)); - await sslStream.AuthenticateAsServerAsync(_sslCertificate, false, SslProtocols.Tls12, false); + await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false); var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream), new DefaultMqttV311PacketSerializer()); ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); } catch (Exception exception) { - MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at SSL endpoint."); + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at TLS endpoint."); } } } diff --git a/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs index a5f873c..e78cd98 100644 --- a/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs @@ -33,10 +33,10 @@ namespace MQTTnet.Implementations { await _socket.ConnectAsync(options.Server, options.GetPort()); - if (options.SslOptions.UseSsl) + if (options.TlsOptions.UseTls) { _sslStream = new SslStream(new NetworkStream(_socket, true)); - await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.SslOptions.CheckCertificateRevocation); + await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation); } } catch (SocketException exception) @@ -106,12 +106,12 @@ namespace MQTTnet.Implementations private static X509CertificateCollection LoadCertificates(MqttClientOptions options) { var certificates = new X509CertificateCollection(); - if (options.SslOptions.Certificates == null) + if (options.TlsOptions.Certificates == null) { return certificates; } - foreach (var certificate in options.SslOptions.Certificates) + foreach (var certificate in options.TlsOptions.Certificates) { certificates.Add(new X509Certificate(certificate)); } diff --git a/Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs index f365efd..f739756 100644 --- a/Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs +++ b/Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs @@ -17,8 +17,8 @@ namespace MQTTnet.Implementations { private CancellationTokenSource _cancellationTokenSource; private Socket _defaultEndpointSocket; - private Socket _sslEndpointSocket; - private X509Certificate2 _sslCertificate; + private Socket _tlsEndpointSocket; + private X509Certificate2 _tlsCertificate; private bool _isRunning; @@ -26,6 +26,8 @@ namespace MQTTnet.Implementations public void Start(MqttServerOptions options) { + if (options == null) throw new ArgumentNullException(nameof(options)); + if (_isRunning) throw new InvalidOperationException("Server is already started."); _isRunning = true; @@ -40,20 +42,20 @@ namespace MQTTnet.Implementations Task.Run(() => AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); } - if (options.SslEndpointOptions.IsEnabled) + if (options.TlsEndpointOptions.IsEnabled) { - if (options.SslEndpointOptions.Certificate == null) + if (options.TlsEndpointOptions.Certificate == null) { - throw new ArgumentException("SSL certificate is not set."); + throw new ArgumentException("TLS certificate is not set."); } - _sslCertificate = new X509Certificate2(options.SslEndpointOptions.Certificate); + _tlsCertificate = new X509Certificate2(options.TlsEndpointOptions.Certificate); - _sslEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _sslEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetSslEndpointPort())); - _sslEndpointSocket.Listen(options.ConnectionBacklog); + _tlsEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); + _tlsEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetTlsEndpointPort())); + _tlsEndpointSocket.Listen(options.ConnectionBacklog); - Task.Run(() => AcceptSslEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); + Task.Run(() => AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); } } @@ -67,8 +69,8 @@ namespace MQTTnet.Implementations _defaultEndpointSocket?.Dispose(); _defaultEndpointSocket = null; - _sslEndpointSocket?.Dispose(); - _sslEndpointSocket = null; + _tlsEndpointSocket?.Dispose(); + _tlsEndpointSocket = null; } public void Dispose() @@ -93,7 +95,7 @@ namespace MQTTnet.Implementations } } - private async Task AcceptSslEndpointConnectionsAsync(CancellationToken cancellationToken) + private async Task AcceptTlsEndpointConnectionsAsync(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { @@ -102,14 +104,14 @@ namespace MQTTnet.Implementations var clientSocket = await Task.Factory.FromAsync(_defaultEndpointSocket.BeginAccept, _defaultEndpointSocket.EndAccept, null); var sslStream = new SslStream(new NetworkStream(clientSocket)); - await sslStream.AuthenticateAsServerAsync(_sslCertificate, false, SslProtocols.Tls12, false); + await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false); var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream), new DefaultMqttV311PacketSerializer()); ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); } catch (Exception exception) { - MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at SSL endpoint."); + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at TLS endpoint."); } } } diff --git a/Frameworks/MQTTnet.NetFramework/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.NetFramework/Implementations/MqttTcpChannel.cs index adea277..3a43a3e 100644 --- a/Frameworks/MQTTnet.NetFramework/Implementations/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.NetFramework/Implementations/MqttTcpChannel.cs @@ -33,10 +33,10 @@ namespace MQTTnet.Implementations { await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.GetPort(), null); - if (options.SslOptions.UseSsl) + if (options.TlsOptions.UseTls) { _sslStream = new SslStream(new NetworkStream(_socket, true)); - await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.SslOptions.CheckCertificateRevocation); + await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation); } } catch (SocketException exception) @@ -113,12 +113,12 @@ namespace MQTTnet.Implementations private static X509CertificateCollection LoadCertificates(MqttClientOptions options) { var certificates = new X509CertificateCollection(); - if (options.SslOptions.Certificates == null) + if (options.TlsOptions.Certificates == null) { return certificates; } - foreach (var certificate in options.SslOptions.Certificates) + foreach (var certificate in options.TlsOptions.Certificates) { certificates.Add(new X509Certificate(certificate)); } diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs index f8fe07f..ea251ca 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs @@ -1,5 +1,4 @@ using System; -using System.Security.Cryptography.X509Certificates; using MQTTnet.Core.Adapter; using MQTTnet.Core.Diagnostics; using MQTTnet.Core.Serializer; @@ -11,15 +10,15 @@ namespace MQTTnet.Implementations public class MqttServerAdapter : IMqttServerAdapter, IDisposable { private StreamSocketListener _defaultEndpointSocket; - private StreamSocketListener _sslEndpointSocket; - private X509Certificate2 _sslCertificate; - + private bool _isRunning; public event EventHandler ClientConnected; public void Start(MqttServerOptions options) { + if (options == null) throw new ArgumentNullException(nameof(options)); + if (_isRunning) throw new InvalidOperationException("Server is already started."); _isRunning = true; @@ -30,18 +29,9 @@ namespace MQTTnet.Implementations _defaultEndpointSocket.ConnectionReceived += AcceptDefaultEndpointConnectionsAsync; } - if (options.SslEndpointOptions.IsEnabled) + if (options.TlsEndpointOptions.IsEnabled) { - if (options.SslEndpointOptions.Certificate == null) - { - throw new ArgumentException("SSL certificate is not set."); - } - - _sslCertificate = new X509Certificate2(options.SslEndpointOptions.Certificate); - - _sslEndpointSocket = new StreamSocketListener(); - _sslEndpointSocket.BindServiceNameAsync(options.GetSslEndpointPort().ToString(), SocketProtectionLevel.Tls12).GetAwaiter().GetResult(); - _sslEndpointSocket.ConnectionReceived += AcceptSslEndpointConnectionsAsync; + throw new NotSupportedException("TLS servers are not supported for UWP apps."); } } @@ -51,9 +41,6 @@ namespace MQTTnet.Implementations _defaultEndpointSocket?.Dispose(); _defaultEndpointSocket = null; - - _sslEndpointSocket?.Dispose(); - _sslEndpointSocket = null; } public void Dispose() @@ -73,18 +60,5 @@ namespace MQTTnet.Implementations MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at default endpoint."); } } - - private void AcceptSslEndpointConnectionsAsync(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) - { - try - { - var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new DefaultMqttV311PacketSerializer()); - ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(args.Socket.Information.RemoteAddress.ToString(), clientAdapter)); - } - catch (Exception exception) - { - MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at SSL endpoint."); - } - } } } \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs index e1e6082..99681b7 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs @@ -32,7 +32,7 @@ namespace MQTTnet.Implementations if (options == null) throw new ArgumentNullException(nameof(options)); try { - if (!options.SslOptions.UseSsl) + if (!options.TlsOptions.UseTls) { await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString()); } @@ -40,7 +40,7 @@ namespace MQTTnet.Implementations { _socket.Control.ClientCertificate = LoadCertificate(options); - if (!options.SslOptions.CheckCertificateRevocation) + if (!options.TlsOptions.CheckCertificateRevocation) { _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain); _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing); @@ -104,17 +104,17 @@ namespace MQTTnet.Implementations private static Certificate LoadCertificate(MqttClientOptions options) { - if (options.SslOptions.Certificates == null || !options.SslOptions.Certificates.Any()) + if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any()) { return null; } - if (options.SslOptions.Certificates.Count > 1) + if (options.TlsOptions.Certificates.Count > 1) { throw new NotSupportedException("Only one client certificate is supported for UWP."); } - return new Certificate(options.SslOptions.Certificates.First().AsBuffer()); + return new Certificate(options.TlsOptions.Certificates.First().AsBuffer()); } } } \ No newline at end of file diff --git a/MQTTnet.Core/Client/MqttClientOptions.cs b/MQTTnet.Core/Client/MqttClientOptions.cs index 9eebdd5..0aaf43c 100644 --- a/MQTTnet.Core/Client/MqttClientOptions.cs +++ b/MQTTnet.Core/Client/MqttClientOptions.cs @@ -8,7 +8,7 @@ namespace MQTTnet.Core.Client public int? Port { get; set; } - public MqttClientSslOptions SslOptions { get; } = new MqttClientSslOptions(); + public MqttClientTlsOptions TlsOptions { get; } = new MqttClientTlsOptions(); public string UserName { get; set; } diff --git a/MQTTnet.Core/Client/MqttClientOptionsExtensions.cs b/MQTTnet.Core/Client/MqttClientOptionsExtensions.cs index 404dd01..7b01fbd 100644 --- a/MQTTnet.Core/Client/MqttClientOptionsExtensions.cs +++ b/MQTTnet.Core/Client/MqttClientOptionsExtensions.cs @@ -13,7 +13,7 @@ namespace MQTTnet.Core.Client return options.Port.Value; } - return !options.SslOptions.UseSsl ? 1883 : 8883; + return !options.TlsOptions.UseTls ? 1883 : 8883; } } } diff --git a/MQTTnet.Core/Client/MqttClientSslOptions.cs b/MQTTnet.Core/Client/MqttClientTlsOptions.cs similarity index 70% rename from MQTTnet.Core/Client/MqttClientSslOptions.cs rename to MQTTnet.Core/Client/MqttClientTlsOptions.cs index 3185d10..6a7b9cf 100644 --- a/MQTTnet.Core/Client/MqttClientSslOptions.cs +++ b/MQTTnet.Core/Client/MqttClientTlsOptions.cs @@ -2,9 +2,9 @@ namespace MQTTnet.Core.Client { - public sealed class MqttClientSslOptions + public sealed class MqttClientTlsOptions { - public bool UseSsl { get; set; } + public bool UseTls { get; set; } public bool CheckCertificateRevocation { get; set; } diff --git a/MQTTnet.Core/Server/MqttServerOptions.cs b/MQTTnet.Core/Server/MqttServerOptions.cs index c93870e..18e284d 100644 --- a/MQTTnet.Core/Server/MqttServerOptions.cs +++ b/MQTTnet.Core/Server/MqttServerOptions.cs @@ -8,7 +8,7 @@ namespace MQTTnet.Core.Server { public DefaultEndpointOptions DefaultEndpointOptions { get; } = new DefaultEndpointOptions(); - public SslEndpointOptions SslEndpointOptions { get; } = new SslEndpointOptions(); + public MqttServerTlsEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsEndpointOptions(); public int ConnectionBacklog { get; set; } = 10; diff --git a/MQTTnet.Core/Server/MqttServerOptionsExtensions.cs b/MQTTnet.Core/Server/MqttServerOptionsExtensions.cs index cad2a26..4917018 100644 --- a/MQTTnet.Core/Server/MqttServerOptionsExtensions.cs +++ b/MQTTnet.Core/Server/MqttServerOptionsExtensions.cs @@ -4,16 +4,16 @@ namespace MQTTnet.Core.Server { public static class MqttServerOptionsExtensions { - public static int GetSslEndpointPort(this MqttServerOptions options) + public static int GetTlsEndpointPort(this MqttServerOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); - if (!options.SslEndpointOptions.Port.HasValue) + if (!options.TlsEndpointOptions.Port.HasValue) { return 8883; } - return options.SslEndpointOptions.Port.Value; + return options.TlsEndpointOptions.Port.Value; } public static int GetDefaultEndpointPort(this MqttServerOptions options) diff --git a/MQTTnet.Core/Server/SslEndpointOptions.cs b/MQTTnet.Core/Server/MqttServerTlsEndpointOptions.cs similarity index 77% rename from MQTTnet.Core/Server/SslEndpointOptions.cs rename to MQTTnet.Core/Server/MqttServerTlsEndpointOptions.cs index 5a6a46d..0fb3839 100644 --- a/MQTTnet.Core/Server/SslEndpointOptions.cs +++ b/MQTTnet.Core/Server/MqttServerTlsEndpointOptions.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Server { - public sealed class SslEndpointOptions + public sealed class MqttServerTlsEndpointOptions { public bool IsEnabled { get; set; } diff --git a/MQTTnet.Core/Server/SslEndpointOptionsExtensions.cs b/MQTTnet.Core/Server/MqttServerTlsEndpointOptionsExtensions.cs similarity index 85% rename from MQTTnet.Core/Server/SslEndpointOptionsExtensions.cs rename to MQTTnet.Core/Server/MqttServerTlsEndpointOptionsExtensions.cs index 3839334..d790526 100644 --- a/MQTTnet.Core/Server/SslEndpointOptionsExtensions.cs +++ b/MQTTnet.Core/Server/MqttServerTlsEndpointOptionsExtensions.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Server { - public static class SslEndpointOptionsExtensions + public static class MqttServerTlsEndpointOptionsExtensions { public static int GetPort(this DefaultEndpointOptions options) { diff --git a/README.md b/README.md index 1333199..777e535 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ MQTTnet is a .NET library for MQTT based communication. It provides a MQTT clien ## Features * MQTT client included * MQTT server (broker) included -* TLS 1.2 support for client and server +* TLS 1.2 support for client and server (but not UWP servers) * Async support * List of connected clients available (server only) * Extensible communication channels (i.e. In-Memory, TCP, TCP+SSL, WebSockets (not included in this project)) diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml index e61b6e5..d4c4834 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml @@ -24,7 +24,7 @@ ClientId: - Use SSL + Use TLS Trace: diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index 0a74135..1104a33 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -41,7 +41,7 @@ namespace MQTTnet.TestApp.UniversalWindows ClientId = ClientId.Text }; - options.SslOptions.UseSsl = UseSsl.IsChecked == true; + options.TlsOptions.UseTls = UseTls.IsChecked == true; try {