首页 > 解决方案 > 如何正确地将证书传递给 mosquitto_pub 和 mosquitto_sub 以便进行 TLS 通信?

问题描述

我有这个设置

在远程服务器中。我试过了

mosquitto_sub -h 127.0.0.1 -t 'myTopic' -i 'myId'

在我的电脑我试过

mosquitto_pub -h 'remote_ip_here' -t 'myTopic' -m 'the message'

远程服务器能够获取我从我的计算机发布的消息

远程服务器有这些密钥

certificate file = cert.pem
certificate key file = privkey.pem
certification chain file = chain.pem

如果我想在我的计算机和远程计算机之间进行 ssl/tls 通信。

- How do I use those keys ?
- Am I suppose to copy those keys from the remote computer and put them also in my computer ?
- can someone please help what's the proper command to execute in order to have an ssl and tls communication.

在我试过的远程服务器中

mosquitto_sub -h 127.0.0.1 -t 'myTopic' -i 'myId' --capath /etc/myPemPath -p 1883

在我的电脑里,我试过了

mosquitto_pub -h remote_ip -t 'myTopic' -m 'the message' --capath /etc/localPemPath -p 1883 

它没有用,那怎么办?

标签: mosquitto

解决方案


您似乎没有理解 MQTT 的工作原理。两者都是mosquitto_submosquitto_pubMQTT 代理 ( mosquitto) 通信的 MQTT 客户端。它不是直接的客户端/服务器关系。

为了获得 TLS 安全的 MQTT 连接,您首先需要配置代理以使用证书来识别它自己,然后配置客户端以验证该证书作为 TLS 握手的一部分。

有关如何配置 TLS 的 mosquitto 文档可在此处获得。您需要在文件中添加一个cafilecapathcertfilekeyfile选项mosquitto.conf。请注意,TLS 设置适用于上次listener配置的设置,因此您可能需要在与 1883 不同的端口上设置新的侦听器。

至于客户端,假设您没有进行相互身份验证的 TLS,那么您只需将-cafile/-capath选项传递给mosquitto_pubmosquitto_sub启用 TLS 会话。


推荐阅读