mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7be55ce922 |
@@ -3,7 +3,10 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Security;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using MQTTnet.Implementations;
|
using MQTTnet.Implementations;
|
||||||
|
|
||||||
namespace MQTTnet.Tests;
|
namespace MQTTnet.Tests;
|
||||||
@@ -12,6 +15,21 @@ namespace MQTTnet.Tests;
|
|||||||
[TestClass]
|
[TestClass]
|
||||||
public class MqttTcpChannel_Tests
|
public class MqttTcpChannel_Tests
|
||||||
{
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Certificate_Validation_Uses_Default_Handler_When_No_Custom_Handler()
|
||||||
|
{
|
||||||
|
var options = new MqttClientOptionsBuilder().WithTcpServer("localhost")
|
||||||
|
.WithTlsOptions(o => o.WithAllowUntrustedCertificates().WithIgnoreCertificateRevocationErrors())
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var tcpChannel = new MqttTcpChannel(options);
|
||||||
|
|
||||||
|
using var chain = new X509Chain();
|
||||||
|
var isValid = InvokeCertificateValidationCallback(tcpChannel, chain, SslPolicyErrors.RemoteCertificateChainErrors);
|
||||||
|
|
||||||
|
Assert.IsTrue(isValid);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task Dispose_Channel_While_Used()
|
public async Task Dispose_Channel_While_Used()
|
||||||
{
|
{
|
||||||
@@ -77,4 +95,12 @@ public class MqttTcpChannel_Tests
|
|||||||
ct.Cancel(false);
|
ct.Cancel(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool InvokeCertificateValidationCallback(MqttTcpChannel tcpChannel, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
|
{
|
||||||
|
var method = typeof(MqttTcpChannel).GetMethod("InternalUserCertificateValidationCallback", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||||
|
Assert.IsNotNull(method);
|
||||||
|
|
||||||
|
return (bool)method.Invoke(tcpChannel, [null, null, chain, sslPolicyErrors]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -362,19 +362,14 @@ public sealed class MqttTcpChannel : IMqttChannel
|
|||||||
|
|
||||||
bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
{
|
{
|
||||||
|
var eventArgs = new MqttClientCertificateValidationEventArgs(x509Certificate, chain, sslPolicyErrors, _tcpOptions);
|
||||||
var certificateValidationHandler = _tcpOptions?.TlsOptions?.CertificateValidationHandler;
|
var certificateValidationHandler = _tcpOptions?.TlsOptions?.CertificateValidationHandler;
|
||||||
if (certificateValidationHandler != null)
|
if (certificateValidationHandler != null)
|
||||||
{
|
{
|
||||||
var eventArgs = new MqttClientCertificateValidationEventArgs(x509Certificate, chain, sslPolicyErrors, _tcpOptions);
|
|
||||||
return certificateValidationHandler(eventArgs);
|
return certificateValidationHandler(eventArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_tcpOptions?.TlsOptions?.IgnoreCertificateChainErrors ?? false)
|
return MqttClientDefaultCertificateValidationHandler.Handle(eventArgs);
|
||||||
{
|
|
||||||
sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateChainErrors;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sslPolicyErrors == SslPolicyErrors.None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
X509CertificateCollection LoadCertificates()
|
X509CertificateCollection LoadCertificates()
|
||||||
|
|||||||
Reference in New Issue
Block a user