Merge pull request #429 from JanEggers/supportTlsProtolOptionOnServer

added ability to specify tls protocol version
This commit is contained in:
Christian
2018-10-07 15:03:17 +02:00
committed by GitHub
4 changed files with 21 additions and 3 deletions
+1
View File
@@ -11,6 +11,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance> <requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description> <description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description>
<releaseNotes>* [Client] Fixed a deadlock when an exception is fired while connecting (thanks to @malibVB). <releaseNotes>* [Client] Fixed a deadlock when an exception is fired while connecting (thanks to @malibVB).
* [Server] Added ability to specify tls protocol version
</releaseNotes> </releaseNotes>
<copyright>Copyright Christian Kratky 2016-2018</copyright> <copyright>Copyright Christian Kratky 2016-2018</copyright>
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags> <tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
@@ -20,8 +20,8 @@ namespace MQTTnet.Implementations
private readonly CancellationToken _cancellationToken; private readonly CancellationToken _cancellationToken;
private readonly AddressFamily _addressFamily; private readonly AddressFamily _addressFamily;
private readonly MqttServerTcpEndpointBaseOptions _options; private readonly MqttServerTcpEndpointBaseOptions _options;
private readonly MqttServerTlsTcpEndpointOptions _tlsOptions;
private readonly X509Certificate2 _tlsCertificate; private readonly X509Certificate2 _tlsCertificate;
private Socket _socket; private Socket _socket;
public MqttTcpServerListener( public MqttTcpServerListener(
@@ -36,6 +36,11 @@ namespace MQTTnet.Implementations
_tlsCertificate = tlsCertificate; _tlsCertificate = tlsCertificate;
_cancellationToken = cancellationToken; _cancellationToken = cancellationToken;
_logger = logger.CreateChildLogger(nameof(MqttTcpServerListener)); _logger = logger.CreateChildLogger(nameof(MqttTcpServerListener));
if (_options is MqttServerTlsTcpEndpointOptions tlsOptions)
{
_tlsOptions = tlsOptions;
}
} }
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted; public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
@@ -75,7 +80,7 @@ namespace MQTTnet.Implementations
if (_tlsCertificate != null) if (_tlsCertificate != null)
{ {
sslStream = new SslStream(new NetworkStream(clientSocket), false); sslStream = new SslStream(new NetworkStream(clientSocket), false);
await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false).ConfigureAwait(false); await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, _tlsOptions.SslProtocol, false).ConfigureAwait(false);
} }
_logger.Verbose("Client '{0}' accepted by TCP listener '{1}, {2}'.", _logger.Verbose("Client '{0}' accepted by TCP listener '{1}, {2}'.",
@@ -1,5 +1,6 @@
using System; using System;
using System.Net; using System.Net;
using System.Security.Authentication;
namespace MQTTnet.Server namespace MQTTnet.Server
{ {
@@ -80,6 +81,12 @@ namespace MQTTnet.Server
return this; return this;
} }
public MqttServerOptionsBuilder WithEncryptionSslProtocol(SslProtocols value)
{
_options.TlsEndpointOptions.SslProtocol = value;
return this;
}
public MqttServerOptionsBuilder WithoutEncryptedEndpoint() public MqttServerOptionsBuilder WithoutEncryptedEndpoint()
{ {
_options.TlsEndpointOptions.IsEnabled = false; _options.TlsEndpointOptions.IsEnabled = false;
@@ -1,4 +1,6 @@
namespace MQTTnet.Server using System.Security.Authentication;
namespace MQTTnet.Server
{ {
public class MqttServerTlsTcpEndpointOptions : MqttServerTcpEndpointBaseOptions public class MqttServerTlsTcpEndpointOptions : MqttServerTcpEndpointBaseOptions
{ {
@@ -8,5 +10,8 @@
} }
public byte[] Certificate { get; set; } public byte[] Certificate { get; set; }
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;
} }
} }