I hope, that you can help me here. I'm trying to make a MQTT client (azure function in an app service environment) that extracts data from MQTT broker (hivemq), but I'm kind of lost when it comes to certificates (I'm not an expert).
Anyways, I received 3 files (look below) from our partner (that's them with the MQTT broker), but the question is. How should I use these PEM files in the application??
- Client certificate == mqtt-client-cert.pem
- Client key == mqtt-client-key_nopass.pem
- CA certificate == server.pem
Here is the application and here is an example where I tested it locally (with a crt certificate) against an test broker (test.mosquitto.org). It worked perfectly, but now I just need to the same thing, just with 3 PEM files instead.
I also suspect that I need to do something on Azure (either on function app or app service environment level) in order to use these certificates?
// Create a new MQTT client.
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();
// Load certificate
X509Certificate caCertificate = new X509Certificate(@"..\mosquitto.org.crt");
// Create TLS based parameters.
var tlsParameters = new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
Certificates = new List<X509Certificate> { caCertificate },
SslProtocol = System.Security.Authentication.SslProtocols.Tls12
};
// Create TCP based options using the builder.
var connectOptions = new MqttClientOptionsBuilder()
.WithTcpServer("test.mosquitto.org", 8883)
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
.WithTls(tlsParameters)
.Build();
var conResult = await mqttClient.ConnectAsync(connectOptions);
mqttClient.UseConnectedHandler(e =>
{
Console.Write("Connected successfully with MQTT Brokers.");
});
mqttClient.UseDisconnectedHandler(e =>
{
Console.Write("Disconnected from MQTT Brokers.");
});