Fix wrong TLS usage (#1833)

* Fix wrong usage of TLS options

* Fix public broker tests

* Update ReleaseNotes.md
This commit is contained in:
Christian
2023-09-06 20:08:35 +02:00
committed by GitHub
parent cae7206733
commit 6fdee9e0db
5 changed files with 26 additions and 16 deletions
+1
View File
@@ -1 +1,2 @@
* [Client] Fixed wrong TLS options handling (#1830).
* [Client] Fixed NullReferenceExeption when performing a Ping when the client is not connected (#1831). * [Client] Fixed NullReferenceExeption when performing a Ping when the client is not connected (#1831).
@@ -47,13 +47,13 @@ namespace MQTTnet.Extensions.WebSocket4Net
var uri = _webSocketOptions.Uri; var uri = _webSocketOptions.Uri;
if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase)) if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase))
{ {
if (_webSocketOptions.TlsOptions?.UseTls == false) if (_webSocketOptions.TlsOptions?.UseTls == true)
{ {
uri = "ws://" + uri; uri = "wss://" + uri;
} }
else else
{ {
uri = "wss://" + uri; uri = "ws://" + uri;
} }
} }
+13 -9
View File
@@ -25,7 +25,9 @@ namespace MQTTnet.TestApp
UseTls = true, UseTls = true,
SslProtocol = SslProtocols.Tls13, SslProtocol = SslProtocols.Tls13,
// Don't use this in production code. This handler simply allows any invalid certificate to work. // Don't use this in production code. This handler simply allows any invalid certificate to work.
CertificateValidationHandler = w => true AllowUntrustedCertificates = true,
IgnoreCertificateChainErrors = true,
CertificateValidationHandler = _ => true
}; };
#endif #endif
// Also defining TLS12 for servers that don't seem no to support TLS13. // Also defining TLS12 for servers that don't seem no to support TLS13.
@@ -34,7 +36,9 @@ namespace MQTTnet.TestApp
UseTls = true, UseTls = true,
SslProtocol = SslProtocols.Tls12, SslProtocol = SslProtocols.Tls12,
// Don't use this in production code. This handler simply allows any invalid certificate to work. // Don't use this in production code. This handler simply allows any invalid certificate to work.
CertificateValidationHandler = w => true AllowUntrustedCertificates = true,
IgnoreCertificateChainErrors = true,
CertificateValidationHandler = _ => true
}; };
// mqtt.eclipseprojects.io // mqtt.eclipseprojects.io
@@ -97,10 +101,10 @@ namespace MQTTnet.TestApp
"test.mosquitto.org WS TLS12", "test.mosquitto.org WS TLS12",
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("test.mosquitto.org:8081/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build()); new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("test.mosquitto.org:8081/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build());
// await ExecuteTestAsync( await ExecuteTestAsync(
// "test.mosquitto.org WS TLS12 (WebSocket4Net)", "test.mosquitto.org WS TLS12 (WebSocket4Net)",
// new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8081/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build(), new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("test.mosquitto.org:8081/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build(),
// true); true);
// broker.emqx.io // broker.emqx.io
await ExecuteTestAsync( await ExecuteTestAsync(
@@ -150,7 +154,6 @@ namespace MQTTnet.TestApp
true); true);
// mqtt.swifitch.cz: Does not seem to operate any more // mqtt.swifitch.cz: Does not seem to operate any more
// cloudmqtt.com: Cannot test because it does not offer a free plan any more. // cloudmqtt.com: Cannot test because it does not offer a free plan any more.
Write("Finished.", ConsoleColor.White); Write("Finished.", ConsoleColor.White);
@@ -197,9 +200,10 @@ namespace MQTTnet.TestApp
Write("[OK]\n", ConsoleColor.Green); Write("[OK]\n", ConsoleColor.Green);
} }
catch (Exception e) catch (Exception exception)
{ {
Write("[FAILED] " + e.Message + "\n", ConsoleColor.Red); Write("[FAILED]" + Environment.NewLine, ConsoleColor.Red);
Write(exception + Environment.NewLine, ConsoleColor.Red);
} }
} }
@@ -32,7 +32,12 @@ namespace MQTTnet.Client
throw new InvalidOperationException("A channel must be set."); throw new InvalidOperationException("A channel must be set.");
} }
var tlsOptions = _tlsOptions; // The user can specify the TCP options with already configured TLS options
// or start with TLS settings not knowing which transport will be used (depending
// on the order of called methods from the builder).
// The builder prefers the explicitly set TLS options!
var tlsOptions = _tlsOptions ?? _tcpOptions?.TlsOptions;
if (_tlsParameters != null) if (_tlsParameters != null)
{ {
if (_tlsParameters?.UseTls == true) if (_tlsParameters?.UseTls == true)
@@ -46,13 +46,13 @@ namespace MQTTnet.Implementations
var uri = _options.Uri; var uri = _options.Uri;
if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase)) if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase))
{ {
if (_options.TlsOptions?.UseTls == false) if (_options.TlsOptions?.UseTls == true)
{ {
uri = "ws://" + uri; uri = "wss://" + uri;
} }
else else
{ {
uri = "wss://" + uri; uri = "ws://" + uri;
} }
} }