From 1a08ec17ba95e54339084baf77542eaffae3217f Mon Sep 17 00:00:00 2001 From: troky Date: Sat, 19 Aug 2023 14:20:01 +0200 Subject: [PATCH] Added certificate validation callback for .NET framework 4.x (#1806) * Added certificate validation callback for .NET framework 4.x * Update ReleaseNotes.md * Also expose sender to event arguments --------- Co-authored-by: Christian <6939810+chkr1011@users.noreply.github.com> --- .github/workflows/ReleaseNotes.md | 1 + ...qttClientCertificateValidationEventArgs.cs | 12 +++++++--- .../Implementations/MqttWebSocketChannel.cs | 22 +++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 5ee275ee8..069f272e4 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1,4 +1,5 @@ * [Client] Fixed _PlatformNotSupportedException_ when using Blazor (#1755, thanks to @Nickztar). +* [Client] Added support for _RemoteCertificateValidationCallback_ for .NET 4.5.2, 4.6.1 and 4.8 (#1806, thanks to @troky). * [Client] Fixed wrong logging of obsolete feature when connection was not successful (#1801, thanks to @ramonsmits). * [Client] Fixed _NullReferenceException_ when performing several actions when not connected (#1800, thanks to @ramonsmits). * [Server] Fixed _NullReferenceException_ in retained messages management (#1762, thanks to @logicaloud). diff --git a/Source/MQTTnet/Client/Options/MqttClientCertificateValidationEventArgs.cs b/Source/MQTTnet/Client/Options/MqttClientCertificateValidationEventArgs.cs index f1842cb1e..ff4825612 100644 --- a/Source/MQTTnet/Client/Options/MqttClientCertificateValidationEventArgs.cs +++ b/Source/MQTTnet/Client/Options/MqttClientCertificateValidationEventArgs.cs @@ -14,8 +14,14 @@ public sealed class MqttClientCertificateValidationEventArgs : EventArgs public X509Chain Chain { get; set; } - public SslPolicyErrors SslPolicyErrors { get; set; } - public IMqttClientChannelOptions ClientOptions { get; set; } +#if NET452 || NET461 || NET48 + /// + /// Can be a host string name or an object derived from WebRequest. + /// + public object Sender { get; set; } +#endif + + public SslPolicyErrors SslPolicyErrors { get; set; } } -} +} \ No newline at end of file diff --git a/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs b/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs index fd4027a92..be31c2a51 100644 --- a/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs +++ b/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs @@ -248,12 +248,20 @@ void SetupClientWebSocket(ClientWebSocket clientWebSocket) throw new NotSupportedException("Remote certificate validation callback is not supported when using 'netstandard2.0'."); #elif WINDOWS_UWP throw new NotSupportedException("Remote certificate validation callback is not supported when using 'uap10.0'."); -#elif NET452 - throw new NotSupportedException("Remote certificate validation callback is not supported when using 'net452'."); -#elif NET461 - throw new NotSupportedException("Remote certificate validation callback is not supported when using 'net461'."); -#elif NET48 - throw new NotSupportedException("Remote certificate validation callback is not supported when using 'net48'."); +#elif NET452 || NET461 || NET48 + ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => + { + var context = new MqttClientCertificateValidationEventArgs + { + Sender = sender, + Certificate = certificate, + Chain = chain, + SslPolicyErrors = sslPolicyErrors, + ClientOptions = _options + }; + + return certificateValidationHandler(context); + }; #else clientWebSocket.Options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { @@ -272,4 +280,4 @@ void SetupClientWebSocket(ClientWebSocket clientWebSocket) } } } -} \ No newline at end of file +}