Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix MQTT cert validation with custom CA #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/HASS.Agent.Staging/HASS.Agent/MQTT/MqttManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,22 @@ private static ManagedMqttClientOptions GetOptions()
if (!string.IsNullOrEmpty(Variables.AppSettings.MqttRootCertificate))
{
if (!File.Exists(Variables.AppSettings.MqttRootCertificate)) Log.Error("[MQTT] Provided root certificate not found: {cert}", Variables.AppSettings.MqttRootCertificate);
else certificates.Add(new X509Certificate2(Variables.AppSettings.MqttRootCertificate));
else
{
X509Certificate2 caCrt = new(Variables.AppSettings.MqttRootCertificate);
certificates.Add(caCrt);
tlsParameters.CertificateValidationHandler = (certContext) => {
X509Chain chain = new();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
chain.ChainPolicy.VerificationTime = DateTime.Now;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 0, 0);
chain.ChainPolicy.CustomTrustStore.Add(caCrt);
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
return chain.Build(new X509Certificate2(certContext.Certificate));
};
}
}

if (!string.IsNullOrEmpty(Variables.AppSettings.MqttClientCertificate))
Expand Down