Skip to content

Commit

Permalink
change to cert collection
Browse files Browse the repository at this point in the history
  • Loading branch information
rido-min committed Oct 16, 2023
1 parent 8b22637 commit 0b5dff5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
35 changes: 34 additions & 1 deletion 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 @@ -439,19 +440,51 @@ public static void Reconnect_Using_Timer()
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()
.WithCertificationAuthoritiesFile("mosquitto.org.crt") // from https://test.mosquitto.org/ssl/mosquitto.org.crt
.WithTrustChain(caChain)
.WithRevocationMode(System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck) // no check, since this CA does not include CRL/OCSP endpoints
.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
BQAwgZAxCzAJBgNVBAYTAkdCMRcwFQYDVQQIDA5Vbml0ZWQgS2luZ2RvbTEOMAwG
A1UEBwwFRGVyYnkxEjAQBgNVBAoMCU1vc3F1aXR0bzELMAkGA1UECwwCQ0ExFjAU
BgNV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ue9ashlS4EB5IECdSR2TItnAIiIwimx839
LdUdRudafMu5T5Xma182OC0/u/xRlEm+tvKGGmfFcN0piqVl8OrSPBgIlb+1IKJE
m/XriWr/Cq4h/JfB7NTsezVslgkBaoU=
-----END CERTIFICATE-----
";

}
6 changes: 0 additions & 6 deletions Samples/MQTTnet.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@
<ProjectReference Include="..\Source\MQTTnet\MQTTnet.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="mosquitto.org.crt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
24 changes: 0 additions & 24 deletions Samples/mosquitto.org.crt

This file was deleted.

2 changes: 1 addition & 1 deletion Source/MQTTnet/Client/Options/MqttClientTlsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public sealed class MqttClientTlsOptions
#endif

#if NET7_0_OR_GREATER
public string CertificationAuthoritiesFile { get; set; }
public X509Certificate2Collection TrustChain { get; set; }
#endif
}
}
5 changes: 3 additions & 2 deletions Source/MQTTnet/Client/Options/MqttClientTlsOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ public MqttClientTlsOptionsBuilder WithCipherSuitesPolicy(EncryptionPolicy encry
#endif

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

#endif
}
}
5 changes: 2 additions & 3 deletions Source/MQTTnet/Implementations/MqttTcpChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
AllowRenegotiation = _tcpOptions.TlsOptions.AllowRenegotiation
};
#if NET7_0_OR_GREATER
if (!string.IsNullOrEmpty(_tcpOptions.TlsOptions.CertificationAuthoritiesFile))
if (_tcpOptions.TlsOptions.TrustChain?.Count > 0)
{
X509Certificate2Collection caCerts = new X509Certificate2Collection();
caCerts.ImportFromPemFile(_tcpOptions.TlsOptions.CertificationAuthoritiesFile);
X509Certificate2Collection caCerts = _tcpOptions.TlsOptions.TrustChain;
sslOptions.CertificateChainPolicy = new X509ChainPolicy();
sslOptions.CertificateChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
sslOptions.CertificateChainPolicy.RevocationMode = _tcpOptions.TlsOptions.RevocationMode;
Expand Down

0 comments on commit 0b5dff5

Please sign in to comment.