gpt4 book ai didi

ssl - 如何通过端口 8883 使 TLS 在 MQTT 中工作?

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:20 24 4
gpt4 key购买 nike

我需要帮助来配置此 MQTT 以在 TLS 模式下工作。我已经在服务器中设置了 MQTT。 服务器letsencrypt证书保护,这就是为什么它的中有https,然后我也设置了它在远程计算机中。

服务器运行这个命令

mosquitto_sub -h localhost -t 'testtopic' -p 1883

远程计算机运行此命令

mosquitto_pub -h domainName -t 'testtopic' -m "test message" -p 1883

两台电脑上的MQTT配置是这样的

pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
port 1883
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous true
tls_version tlsv1.2

这个有效。但是如何使通信进入 TLS 模式呢?有人可以让我知道什么是正确的配置吗?如何在两台电脑上传递参数?

  • 我试过将配置的端口更改为8883
  • 在两台计算机上,我都尝试通过 --cafile 来传递 cafile是一个.pem 证书,它来自 服务器,由生成 由letsencrypt .所以我只是将其复制并粘贴到远程计算机上

到目前为止,这些是我尝试过的方法,但是在 wireshark 中它根本无法检测到 TLS 通信并且连接被拒绝或 CA 文件有问题

最佳答案

通过设置allow anonymous true ,任何人都可以在没有身份验证的情况下发布到您的经纪人。

TLS 在 MQTT broker 中不直接使用,需要配置使用 TLS 的 MQTTS。要使用安全的 MQTT 连接,您在/etc/mosquitto/mosquitto.conf 中找到的 Mosquitto 配置文件需要包含以下几行:

listener 8883
cafile <path-to-cafile>
certfile <path-to-server-cert>
keyfile <path-to-server-key-file>

哪里<path-to-cafile> ,并且需要替换为您的 ca 文件、服务器证书(即您的 .pem 文件和服务器 key 证书所在的绝对路径)。

例如在我的例子中,我的 mosquitto.conf 看起来像这样:

    pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

listener 1883
protocol mqtt

listener 8883
cafile /etc/ssl/certs/broker/ca.pem
certfile /etc/ssl/certs/broker/server.pem
keyfile /etc/ssl/certs/broker/server-key.pem
tls_version tlsv1.2

保存这个配置并使用上面的配置重启mosquitto

您可以通过运行来测试您的 TLS 连接:

mosquitto_pub -t test -m test-message -p 8883 --cafile <path-to-your-ca-file> --insecure

在我的例子中是:

mosquitto_pub -t test -m test-message -p 8883 --cafile /etc/ssl/certs/broker/ca.pem --insecure

您需要使用--insecure 选项,因为证书都是自签名的。 mosquitto 客户端无法验证自签名服务器证书。如果您不使用 --insecure 选项,您将收到一条错误消息

Error: host name verification failed.
OpenSSL Error: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
Error: A TLS error occurred.

要设置身份验证,请创建一个包含内容的文本文件

<user>:<password>

其中 是您要允许的用户名的占位符 是您要设置的密码的占位符

例如:

panda:black

将创建一个名为 panda 的用户,用户 panda 的密码将是 black。要加密密码,请运行命令

mosquitto_passwd -U <path-to-passwordfile>

现在内容如下:

panda:$6$a2foLssTVgMG4QY6$3rWvoLqwo1uCz6jZH6KDK3yAcWtIFlATbOSbwx7XJx2Q5Mix2S+iRqWI7KDqp43nSDdPV7mMvnYJS6tgHb7QjA==

现在将这两行添加到 mosquitto 配置文件中

allow_anonymous false
password_file <path-to-passwordfile>

重新启动/重新加载 mosquitto 代理以获得完全验证的 MQTTS 连接

使用用户名和密码发布到代理:

mosquitto_pub -t test -m test-message -p 8883 --cafile <path-to-ca-file> -u panda -P black --insecure

关于ssl - 如何通过端口 8883 使 TLS 在 MQTT 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com