dotnet / dotnet/MQTTnet

Can't connect to broker using CA Cert ClientCert and Key

Open
#1,986 2 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
5.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I try to connect to my MQTT broker with TLS. I have these 3 certificates as follows
-CA Cert (CRT)
-Client Cert (PEM)
-Client Key (PEM)

These certificates are valid and work if I use them with a software like MQTTX.

Now I am trying to use MqttNet (3.1.2) to connect to broker. I have read the documentation and accordingly tried by converting clientCert and Key to pfx file using following command.

`openssl pkcs12 -export -out certificate.pfx -inkey clientkey.pem -in clientCert.crt`

My MQTTnet code :
`// Creates a new client
MqttClientOptionsBuilder builder = new MqttClientOptionsBuilder()
.WithClientId("Test")
.WithTcpServer("mqtt.cmec.iotexpress.io", 8889);

var clientCert_mos = new X509Certificate2(@"C:\Temp\certificate.pfx");
var caCrt_mos = new X509Certificate2(@"C:\Temp\ca-chain2.pem");

builder
.WithCredentials("User", "Password")
.WithTls(
new MqttClientOptionsBuilderTlsParameters()
{
UseTls = true,
SslProtocol = System.Security.Authentication.SslProtocols.Tls12,
AllowUntrustedCertificates = true,

CertificateValidationHandler = (certContext) => {
X509Chain chain = new X509Chain();
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_mos);
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;

// convert provided X509Certificate to X509Certificate2
var x5092 = new X509Certificate2(certContext.Certificate);
return chain.Build(x5092);
},

Certificates = new List()
{
caCrt_mos ,
}
})
.Build(); `

I can't connect.

I did try to connet to Test.mosquitto.org on port 8884 (Encrypted and client certificate required (Ca, Cert, Key), same as mine) and I can establish the connection...

Any idea what I'm doing wrong?

Thanks,

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.