I am using MQTTNet with .Net 6 . My MQTT Broker provides 3 certificates as follows
- CA Cert
- Client Cert
- Client Key
These certificates are valid and work if i use them with a software like MQTTExplorer.
Now I am trying to use MqttNet (latest version) to connect to the 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
Now I have two certificates
- certificate.pfx (with password)
- ca cert
I am trying to use MQTTNet as follows in console app:
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();
var caCert = X509Certificate.CreateFromCertFile(@"certificates\cacert.cer");
var clientCert = new X509Certificate2(@"certificates\mycertificate.pfx", "mypassword");
var options = new MqttClientOptionsBuilder()
.WithClientId(clientId)
.WithTcpServer(mqttUri, mqttPort)
.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
SslProtocol = System.Security.Authentication.SslProtocols.Tls12,
Certificates = new List<X509Certificate>()
{
caCert,clientCert
}
}).Build();
await mqttClient.ConnectAsync(options, CancellationToken.None);
On connectAsync I get an error, "Unknown error occured while processing certificates. Not sure what is wrong here ? I have tried to follow the documentation as well
https://github.com/dotnet/MQTTnet/wiki/Client#certificate-based-authentication -- TLS using client Certificate
Error Message
An unknown error occurred while processing the certificate.
Stack Trace:
at System.Net.Security.SslStream.<ForceAuthenticationAsync>d__175`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Implementations.MqttTcpChannel.<ConnectAsync>d__17.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at MQTTnet.Implementations.MqttTcpChannel.<ConnectAsync>d__17.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Adapter.MqttChannelAdapter.<ConnectAsync>d__28.MoveNext()