ssl - 将 Mosquitto MQTT C 客户端连接到 Azure 物联网中心

标签 ssl mqtt tls1.2 mosquitto azure-iot-hub

如标题所述,我在将我的 Mosquitto MQTT 客户端(用 C 语言编写)连接到我的 Azure 物联网中心时遇到了困难。我之前已经成功连接到许多不同的平台(例如 Amazon EC2、ThingsBoard、TheThings.io、SierraWireless 等),所以我知道我的客户非常可靠。

这里的困难在于我需要某种证书才能允许连接,但我不确定我需要做什么。

为了让它正常工作,我添加了以下配置:

mosquitto_opts_set(client, MOSQ_OPT_PROTOCOL_VERSION, "MQTT_PROTOCOL_V311");
mosquitto_tls_set(client, "/home/ca-certificates.crt", NULL, NULL, NULL, NULL);
mosquitto_tls_insecure_set(client, 1);
mosquitto_tls_opts_set(client, 0, "tlsv1", NULL);
mosquitto_username_pw_set(client, "hubname.azure-devices.net/deviceName", "SharedAccessSignature=SharedAccessSignature sr=hubname.azure-devices.net%2Fdevices%2FdeviceName&sig=sigValue&se=1553087157");

在上面的代码中,“hubname”、“deviceName”和“sigValue”当然在我的代码中被替换为实际值。

你们中的任何人都可以指出我做错了什么,或者我需要采取哪些其他配置步骤吗?

最佳答案

我安装 mosquitto在 Windows 上并使用命令成功发送消息:

mosquitto_pub -d -h hubname.azure-devices.net -i "device1" -u "hubname.azure-devices.net/device1" -P "SharedAccessSignature sr=hubname.azure-devices.net%2Fdevices%2Fdevice1&sig=sig&se=1553325061" -m "hi from mosquitto client" -t "devices/device1/messages/events/" -p 8883 --cafile \path-to-cert-file\IoTHubTest.cer -V mqttv311

根据您提供的信息,可能是证书问题。

Azure IoT 中心使用 DigiCert 巴尔的摩根证书来保护设备连接。 (请注意,China Azure 没有使用 Cyber​​Trust Root CA,而是仍然使用 WoSign Root CA。)

You can create this file by copying the certificate information from certs.c in the Azure IoT SDK for C. Include the lines -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----, remove the " marks at the beginning and end of every line, and remove the \r\n characters at the end of every line.

这里我将它保存为 IoTHubTest.cer。

希望对您有所帮助。

关于ssl - 将 Mosquitto MQTT C 客户端连接到 Azure 物联网中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403601/

相关文章:

python - 在 python 和 heroku 网页中使用 MQTT 发布和订阅消息

ssl - TLS-通过2个不同的客户端端口连接到服务器端口80

java - 了解JAVA中设置http协议(protocol)

java - 在 Paho 中建立连接时的回调

java - 如何使用 CA 签名的服务器证书连接 SSL MQTT 客户端?

java - 附加 SSL 客户端证书 okhttp3 。握手失败

node.js - NodeJS/Express 自动检测 SSL over HTTP (HTTPS) 解释?

ssl - 织物订购者 TLS : failed to find any PEM data in certificate input

python-3.x - MacOS 上的 pip3 : SSL error while installing virtualenv

c# - 更改每个请求的安全协议(protocol) (HttpClient)