@@ -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."); | |||
} | |||
} | |||
} | |||
@@ -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)); | |||
} | |||
@@ -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."); | |||
} | |||
} | |||
} | |||
@@ -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)); | |||
} | |||
@@ -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<MqttClientConnectedEventArgs> 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."); | |||
} | |||
} | |||
} | |||
} |
@@ -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()); | |||
} | |||
} | |||
} |
@@ -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; } | |||
@@ -13,7 +13,7 @@ namespace MQTTnet.Core.Client | |||
return options.Port.Value; | |||
} | |||
return !options.SslOptions.UseSsl ? 1883 : 8883; | |||
return !options.TlsOptions.UseTls ? 1883 : 8883; | |||
} | |||
} | |||
} |
@@ -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; } | |||
@@ -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; | |||
@@ -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) | |||
@@ -1,6 +1,6 @@ | |||
namespace MQTTnet.Core.Server | |||
{ | |||
public sealed class SslEndpointOptions | |||
public sealed class MqttServerTlsEndpointOptions | |||
{ | |||
public bool IsEnabled { get; set; } | |||
@@ -2,7 +2,7 @@ | |||
namespace MQTTnet.Core.Server | |||
{ | |||
public static class SslEndpointOptionsExtensions | |||
public static class MqttServerTlsEndpointOptionsExtensions | |||
{ | |||
public static int GetPort(this DefaultEndpointOptions options) | |||
{ |
@@ -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)) | |||
@@ -24,7 +24,7 @@ | |||
<TextBlock>ClientId:</TextBlock> | |||
<TextBox x:Name="ClientId"></TextBox> | |||
<CheckBox x:Name="UseSsl">Use SSL</CheckBox> | |||
<CheckBox x:Name="UseTls">Use TLS</CheckBox> | |||
<Button Click="Connect">Connect</Button> | |||
<TextBlock>Trace:</TextBlock> | |||
</StackPanel> | |||
@@ -41,7 +41,7 @@ namespace MQTTnet.TestApp.UniversalWindows | |||
ClientId = ClientId.Text | |||
}; | |||
options.SslOptions.UseSsl = UseSsl.IsChecked == true; | |||
options.TlsOptions.UseTls = UseTls.IsChecked == true; | |||
try | |||
{ | |||