Refactor TLS 1.3 support.

This commit is contained in:
Christian Kratky
2020-10-31 11:23:14 +01:00
parent f8c956a9e2
commit 558d281a33
6 changed files with 14 additions and 18 deletions
+1 -1
View File
@@ -12,7 +12,7 @@
<requireLicenseAcceptance>true</requireLicenseAcceptance> <requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol.</description> <description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol.</description>
<releaseNotes> <releaseNotes>
* [Server] .. * [Core] Added support for TLS 1.3 (requires .NET Core 3.1+) (thanks to @Dvergatal).
</releaseNotes> </releaseNotes>
<copyright>Copyright Christian Kratky 2016-2020</copyright> <copyright>Copyright Christian Kratky 2016-2020</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 Blazor</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 Blazor</tags>
@@ -16,14 +16,14 @@ using WebSocket4Net;
namespace MQTTnet.Extensions.WebSocket4Net namespace MQTTnet.Extensions.WebSocket4Net
{ {
public class WebSocket4NetMqttChannel : IMqttChannel public sealed class WebSocket4NetMqttChannel : IMqttChannel
{ {
private readonly BlockingCollection<byte> _receiveBuffer = new BlockingCollection<byte>(); readonly BlockingCollection<byte> _receiveBuffer = new BlockingCollection<byte>();
private readonly IMqttClientOptions _clientOptions; readonly IMqttClientOptions _clientOptions;
private readonly MqttClientWebSocketOptions _webSocketOptions; readonly MqttClientWebSocketOptions _webSocketOptions;
private WebSocket _webSocket; WebSocket _webSocket;
public WebSocket4NetMqttChannel(IMqttClientOptions clientOptions, MqttClientWebSocketOptions webSocketOptions) public WebSocket4NetMqttChannel(IMqttClientOptions clientOptions, MqttClientWebSocketOptions webSocketOptions)
{ {
@@ -166,12 +166,12 @@ namespace MQTTnet.Extensions.WebSocket4Net
_webSocket = null; _webSocket = null;
} }
private void OnError(object sender, ErrorEventArgs e) static void OnError(object sender, ErrorEventArgs e)
{ {
System.Diagnostics.Debug.Write(e.Exception.ToString()); System.Diagnostics.Debug.Write(e.Exception.ToString());
} }
private void OnDataReceived(object sender, DataReceivedEventArgs e) void OnDataReceived(object sender, DataReceivedEventArgs e)
{ {
foreach (var @byte in e.Data) foreach (var @byte in e.Data)
{ {
@@ -179,7 +179,7 @@ namespace MQTTnet.Extensions.WebSocket4Net
} }
} }
private async Task ConnectInternalAsync(CancellationToken cancellationToken) async Task ConnectInternalAsync(CancellationToken cancellationToken)
{ {
_webSocket.Error += OnError; _webSocket.Error += OnError;
_webSocket.DataReceived += OnDataReceived; _webSocket.DataReceived += OnDataReceived;
@@ -212,7 +212,7 @@ namespace MQTTnet.Server.Mqtt
// Configure encrypted connections // Configure encrypted connections
if (_settings.EncryptedTcpEndPoint.Enabled) if (_settings.EncryptedTcpEndPoint.Enabled)
{ {
#if NETCOREAPP3_0 || NET5_0 #if NETCOREAPP3_1 || NET5_0
options options
.WithEncryptedEndpoint() .WithEncryptedEndpoint()
.WithEncryptionSslProtocol(SslProtocols.Tls13); .WithEncryptionSslProtocol(SslProtocols.Tls13);
@@ -19,7 +19,7 @@ namespace MQTTnet.Client.Options
public Func<MqttClientCertificateValidationCallbackContext, bool> CertificateValidationHandler { get; set; } public Func<MqttClientCertificateValidationCallbackContext, bool> CertificateValidationHandler { get; set; }
#if NETCOREAPP3_0 || NET5_0 #if NETCOREAPP3_1 || NET5_0
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls13; public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls13;
#else #else
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12; public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;
@@ -26,7 +26,7 @@ namespace MQTTnet.Client.Options
public List<SslApplicationProtocol> ApplicationProtocols { get; set; } public List<SslApplicationProtocol> ApplicationProtocols { get; set; }
#endif #endif
#if NETCOREAPP3_0 || NET5_0 #if NETCOREAPP3_1 || NET5_0
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls13; public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls13;
#else #else
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12; public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;
@@ -39,11 +39,7 @@ namespace MQTTnet.Implementations
CreateStreams(); CreateStreams();
#if NETCOREAPP3_0 || NET5_0
IsSecureConnection = socket.Information.ProtectionLevel >= SocketProtectionLevel.Tls13;
#else
IsSecureConnection = socket.Information.ProtectionLevel >= SocketProtectionLevel.Tls12; IsSecureConnection = socket.Information.ProtectionLevel >= SocketProtectionLevel.Tls12;
#endif
ClientCertificate = clientCertificate; ClientCertificate = clientCertificate;
Endpoint = _socket.Information.RemoteAddress + ":" + _socket.Information.RemotePort; Endpoint = _socket.Information.RemoteAddress + ":" + _socket.Information.RemotePort;
@@ -79,7 +75,7 @@ namespace MQTTnet.Implementations
_socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult); _socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult);
} }
#if NETCOREAPP3_0 || NET5_0 #if NETCOREAPP3_1 || NET5_0
var socketProtectionLevel = SocketProtectionLevel.Tls13; var socketProtectionLevel = SocketProtectionLevel.Tls13;
if (_options.TlsOptions.SslProtocol == SslProtocols.Tls12) if (_options.TlsOptions.SslProtocol == SslProtocols.Tls12)
{ {
@@ -89,7 +85,7 @@ namespace MQTTnet.Implementations
{ {
socketProtectionLevel = SocketProtectionLevel.Tls11; socketProtectionLevel = SocketProtectionLevel.Tls11;
} }
#else #else
var socketProtectionLevel = SocketProtectionLevel.Tls12; var socketProtectionLevel = SocketProtectionLevel.Tls12;
if (_options.TlsOptions.SslProtocol == SslProtocols.Tls11) if (_options.TlsOptions.SslProtocol == SslProtocols.Tls11)
{ {