mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cea751ff9d |
@@ -10,20 +10,12 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
public sealed class MqttClientCertificateValidationEventArgs : EventArgs
|
public sealed class MqttClientCertificateValidationEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public MqttClientCertificateValidationEventArgs(X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, IMqttClientChannelOptions clientOptions)
|
public X509Certificate Certificate { get; set; }
|
||||||
{
|
|
||||||
Certificate = certificate ?? throw new ArgumentNullException(nameof(certificate));
|
|
||||||
Chain = chain ?? throw new ArgumentNullException(nameof(chain));
|
|
||||||
SslPolicyErrors = sslPolicyErrors;
|
|
||||||
ClientOptions = clientOptions ?? throw new ArgumentNullException(nameof(clientOptions));
|
|
||||||
}
|
|
||||||
|
|
||||||
public X509Certificate Certificate { get; }
|
public X509Chain Chain { get; set; }
|
||||||
|
|
||||||
public X509Chain Chain { get; }
|
public SslPolicyErrors SslPolicyErrors { get; set; }
|
||||||
|
|
||||||
public IMqttClientChannelOptions ClientOptions { get; }
|
public IMqttClientChannelOptions ClientOptions { get; set; }
|
||||||
|
|
||||||
public SslPolicyErrors SslPolicyErrors { get; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,18 +12,11 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
public static bool Handle(MqttClientCertificateValidationEventArgs eventArgs)
|
public static bool Handle(MqttClientCertificateValidationEventArgs eventArgs)
|
||||||
{
|
{
|
||||||
var sslPolicyErrors = eventArgs.SslPolicyErrors;
|
if (eventArgs.SslPolicyErrors == SslPolicyErrors.None)
|
||||||
|
|
||||||
if (eventArgs.ClientOptions?.TlsOptions?.IgnoreCertificateChainErrors ?? false)
|
|
||||||
{
|
|
||||||
sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateChainErrors;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sslPolicyErrors == SslPolicyErrors.None)
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (eventArgs.Chain.ChainStatus.Any(c =>
|
if (eventArgs.Chain.ChainStatus.Any(c =>
|
||||||
c.Status == X509ChainStatusFlags.RevocationStatusUnknown || c.Status == X509ChainStatusFlags.Revoked || c.Status == X509ChainStatusFlags.OfflineRevocation))
|
c.Status == X509ChainStatusFlags.RevocationStatusUnknown || c.Status == X509ChainStatusFlags.Revoked || c.Status == X509ChainStatusFlags.OfflineRevocation))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ namespace MQTTnet.Client
|
|||||||
public sealed class MqttClientOptionsBuilderTlsParameters
|
public sealed class MqttClientOptionsBuilderTlsParameters
|
||||||
{
|
{
|
||||||
public bool UseTls { get; set; }
|
public bool UseTls { get; set; }
|
||||||
|
|
||||||
public Func<MqttClientCertificateValidationEventArgs, bool> CertificateValidationHandler { get; set; } = MqttClientDefaultCertificateValidationHandler.Handle;
|
public Func<MqttClientCertificateValidationEventArgs, bool> CertificateValidationHandler { get; set; }
|
||||||
|
|
||||||
#if NET48 || NETCOREAPP3_1 || NET5 || NET6
|
#if NET48 || NETCOREAPP3_1 || NET5 || NET6
|
||||||
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12 | SslProtocols.Tls13;
|
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12 | SslProtocols.Tls13;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
public sealed class MqttClientTlsOptions
|
public sealed class MqttClientTlsOptions
|
||||||
{
|
{
|
||||||
public Func<MqttClientCertificateValidationEventArgs, bool> CertificateValidationHandler { get; set; } = MqttClientDefaultCertificateValidationHandler.Handle;
|
public Func<MqttClientCertificateValidationEventArgs, bool> CertificateValidationHandler { get; set; }
|
||||||
|
|
||||||
public bool UseTls { get; set; }
|
public bool UseTls { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -264,15 +264,27 @@ namespace MQTTnet.Implementations
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InternalUserCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
{
|
{
|
||||||
var certificateValidationHandler = _tcpOptions?.TlsOptions?.CertificateValidationHandler;
|
var certificateValidationHandler = _tcpOptions?.TlsOptions?.CertificateValidationHandler;
|
||||||
if (certificateValidationHandler != null)
|
if (certificateValidationHandler != null)
|
||||||
{
|
{
|
||||||
var eventArgs = new MqttClientCertificateValidationEventArgs(certificate, chain, sslPolicyErrors, _tcpOptions);
|
var eventArgs = new MqttClientCertificateValidationEventArgs
|
||||||
|
{
|
||||||
|
Certificate = x509Certificate,
|
||||||
|
Chain = chain,
|
||||||
|
SslPolicyErrors = sslPolicyErrors,
|
||||||
|
ClientOptions = _tcpOptions
|
||||||
|
};
|
||||||
|
|
||||||
return certificateValidationHandler(eventArgs);
|
return certificateValidationHandler(eventArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_tcpOptions?.TlsOptions?.IgnoreCertificateChainErrors ?? false)
|
||||||
|
{
|
||||||
|
sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateChainErrors;
|
||||||
|
}
|
||||||
|
|
||||||
return sslPolicyErrors == SslPolicyErrors.None;
|
return sslPolicyErrors == SslPolicyErrors.None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -224,7 +224,14 @@ namespace MQTTnet.Implementations
|
|||||||
clientWebSocket.Options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
|
clientWebSocket.Options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
|
||||||
{
|
{
|
||||||
// TODO: Find a way to add client options to same callback. Problem is that they have a different type.
|
// TODO: Find a way to add client options to same callback. Problem is that they have a different type.
|
||||||
var context = new MqttClientCertificateValidationEventArgs(certificate, chain, sslPolicyErrors, _options);
|
var context = new MqttClientCertificateValidationEventArgs
|
||||||
|
{
|
||||||
|
Certificate = certificate,
|
||||||
|
Chain = chain,
|
||||||
|
SslPolicyErrors = sslPolicyErrors,
|
||||||
|
ClientOptions = _options
|
||||||
|
};
|
||||||
|
|
||||||
return certificateValidationHandler(context);
|
return certificateValidationHandler(context);
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,13 +5,78 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
// ReSharper disable MemberCanBePrivate.Global
|
||||||
|
|
||||||
namespace MQTTnet
|
namespace MQTTnet
|
||||||
{
|
{
|
||||||
public static class MqttApplicationMessageExtensions
|
public static class MqttApplicationMessageExtensions
|
||||||
{
|
{
|
||||||
|
public static short ConvertPayloadToByte(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (applicationMessage.Payload.Length != 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Payload requires to be 1 bytes long.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return applicationMessage.Payload[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static short ConvertPayloadToInt16(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (applicationMessage.Payload.Length != 2)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Payload requires to be 2 bytes long.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return BitConverter.ToInt16(applicationMessage.Payload, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int ConvertPayloadToInt32(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (applicationMessage.Payload?.Length != 4)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Payload requires to be 4 bytes long.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return BitConverter.ToInt32(applicationMessage.Payload, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long ConvertPayloadToInt64(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (applicationMessage.Payload.Length != 8)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Payload requires to be 8 bytes long.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return BitConverter.ToInt64(applicationMessage.Payload, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public static string ConvertPayloadToString(this MqttApplicationMessage applicationMessage)
|
public static string ConvertPayloadToString(this MqttApplicationMessage applicationMessage)
|
||||||
{
|
{
|
||||||
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
if (applicationMessage.Payload == null)
|
if (applicationMessage.Payload == null)
|
||||||
{
|
{
|
||||||
@@ -25,5 +90,65 @@ namespace MQTTnet
|
|||||||
|
|
||||||
return Encoding.UTF8.GetString(applicationMessage.Payload, 0, applicationMessage.Payload.Length);
|
return Encoding.UTF8.GetString(applicationMessage.Payload, 0, applicationMessage.Payload.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static decimal ParsePayloadToDecimal(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
return decimal.Parse(ConvertPayloadToString(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double ParsePayloadToDouble(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
return double.Parse(ConvertPayloadToString(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static short ParsePayloadToInt16(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
return short.Parse(ConvertPayloadToString(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int ParsePayloadToInt32(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
return int.Parse(ConvertPayloadToString(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long ParsePayloadToInt64(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
return long.Parse(ConvertPayloadToString(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float ParsePayloadToSingle(this MqttApplicationMessage applicationMessage)
|
||||||
|
{
|
||||||
|
if (applicationMessage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
return float.Parse(ConvertPayloadToString(applicationMessage));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user