Skip to content

Commit

Permalink
Merge branch 'master' into 1856-unobservedtaskexception-mqttchannelad…
Browse files Browse the repository at this point in the history
…apter-doesnt-observe-exception-if-cancelled-earlier
  • Loading branch information
chkr1011 committed Nov 1, 2023
2 parents b1926c0 + 01c90ba commit f6b2e72
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* [Client] Fixed wrong TLS options handling (#1830).
* [Client] Fixed NullReferenceExeption when performing a Ping when the client is not connected (#1831).
* [Server] Fixed not working _UpdateRetainedMessageAsync_ public api (#1858, thanks to @kimdiego2098)
51 changes: 51 additions & 0 deletions Samples/Client/Client_Connection_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// ReSharper disable InconsistentNaming

using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using MQTTnet.Client;
using MQTTnet.Extensions.WebSocket4Net;
using MQTTnet.Formatter;
Expand Down Expand Up @@ -435,4 +436,54 @@ public static void Reconnect_Using_Timer()
Console.ReadLine();
}
}

public static async Task ConnectTls_WithCaFile()
{
var mqttFactory = new MqttFactory();

X509Certificate2Collection caChain = new X509Certificate2Collection();
caChain.ImportFromPem(mosquitto_org); // from https://test.mosquitto.org/ssl/mosquitto.org.crt

using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("test.mosquitto.org", 8883)
.WithTlsOptions(new MqttClientTlsOptionsBuilder()
.WithTrustChain(caChain)
.Build())
.Build();

var connAck = await mqttClient.ConnectAsync(mqttClientOptions);
Console.WriteLine("Connected to test.moquitto.org:8883 with CaFile mosquitto.org.crt: " + connAck.ResultCode);
}


}
const string mosquitto_org = @"
-----BEGIN CERTIFICATE-----
MIIEAzCCAuugAwIBAgIUBY1hlCGvdj4NhBXkZ/uLUZNILAwwDQYJKoZIhvcNAQEL
BQAwgZAx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0TdzIjRj3cQ3KSm41JvUQ0hZ/c04iGDg/xWf
+pp58nfPAYwuerruPNWmlStWAXf0UTqRtg4hQDWBuUFDJTuWuuBvEXudz74eh/wK
sMwfu1HFvjy5Z0iMDU8PUDepjVolOCue9ashlS4EB5IECdSR2TItnAIiIwimx839
LdUdRudafMu5T5Xma182OC0/u/xRlEm+tvKGGmfFcN0piqVl8OrSPBgIlb+1IKJE
m/XriWr/Cq4h/JfB7NTsezVslgkBaoU=
-----END CERTIFICATE-----
";

}
2 changes: 1 addition & 1 deletion Samples/MQTTnet.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand Down
5 changes: 5 additions & 0 deletions Source/MQTTnet/Client/Options/MqttClientTlsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ public sealed class MqttClientTlsOptions

#if NET48 || NETCOREAPP3_1_OR_GREATER
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12 | SslProtocols.Tls13;

#else
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12 | (SslProtocols)0x00003000 /*Tls13*/;
#endif

#if NET7_0_OR_GREATER
public X509Certificate2Collection TrustChain { get; set; }
#endif
}
}
9 changes: 9 additions & 0 deletions Source/MQTTnet/Client/Options/MqttClientTlsOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public MqttClientTlsOptionsBuilder WithCipherSuitesPolicy(EncryptionPolicy encry
_tlsOptions.EncryptionPolicy = encryptionPolicy;
return this;
}
#endif

#if NET7_0_OR_GREATER
public MqttClientTlsOptionsBuilder WithTrustChain(X509Certificate2Collection chain)
{
_tlsOptions.TrustChain = chain;
return this;
}

#endif
}
}
15 changes: 12 additions & 3 deletions Source/MQTTnet/Implementations/MqttTcpChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,24 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
ApplicationProtocols = _tcpOptions.TlsOptions.ApplicationProtocols,
ClientCertificates = LoadCertificates(),
EnabledSslProtocols = _tcpOptions.TlsOptions.SslProtocol,
CertificateRevocationCheckMode =
_tcpOptions.TlsOptions.IgnoreCertificateRevocationErrors ? X509RevocationMode.NoCheck : _tcpOptions.TlsOptions.RevocationMode,
CertificateRevocationCheckMode = _tcpOptions.TlsOptions.IgnoreCertificateRevocationErrors ? X509RevocationMode.NoCheck : _tcpOptions.TlsOptions.RevocationMode,
TargetHost = targetHost,
CipherSuitesPolicy = _tcpOptions.TlsOptions.CipherSuitesPolicy,
EncryptionPolicy = _tcpOptions.TlsOptions.EncryptionPolicy,
AllowRenegotiation = _tcpOptions.TlsOptions.AllowRenegotiation
};
#if NET7_0_OR_GREATER
if (_tcpOptions.TlsOptions.TrustChain?.Count > 0)
{
sslOptions.CertificateChainPolicy = new X509ChainPolicy();
sslOptions.CertificateChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
sslOptions.CertificateChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreEndRevocationUnknown;
sslOptions.CertificateChainPolicy.RevocationMode = _tcpOptions.TlsOptions.IgnoreCertificateRevocationErrors ? X509RevocationMode.NoCheck : _tcpOptions.TlsOptions.RevocationMode;
sslOptions.CertificateChainPolicy.CustomTrustStore.AddRange(_tcpOptions.TlsOptions.TrustChain);
}
#endif

await sslStream.AuthenticateAsClientAsync(sslOptions, cancellationToken).ConfigureAwait(false);
await sslStream.AuthenticateAsClientAsync(sslOptions, cancellationToken).ConfigureAwait(false);
#else
await sslStream.AuthenticateAsClientAsync(
targetHost,
Expand Down
4 changes: 2 additions & 2 deletions Source/MQTTnet/Server/MqttServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public Task UpdateRetainedMessageAsync(MqttApplicationMessage retainedMessage)
ThrowIfDisposed();
ThrowIfNotStarted();

return _retainedMessagesManager?.UpdateMessage(null, retainedMessage);
return _retainedMessagesManager?.UpdateMessage(string.Empty, retainedMessage);
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -405,4 +405,4 @@ void ThrowIfStarted()
}
}
}
}
}

0 comments on commit f6b2e72

Please sign in to comment.