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

sas*_*ori 2 ssl mqtt mosquitto

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

服务器运行此命令

mosquitto_sub -h localhost -t 'testtopic' -p 1883
Run Code Online (Sandbox Code Playgroud)

远程计算机运行此命令

mosquitto_pub -h domainName -t 'testtopic' -m "test message" -p 1883
Run Code Online (Sandbox Code Playgroud)

两台电脑上的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
Run Code Online (Sandbox Code Playgroud)

这个有效。但如何让通讯变成TLS模式呢?有人可以让我知道什么是正确的配置吗?如何在两台计算机中传递参数?

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

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

Div*_*vya 5

通过设置allow anonymous true,任何人都可以发布到您的经纪人而无需身份验证。

MQTT 代理中不直接使用 TLS,您需要配置使用 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>
Run Code Online (Sandbox Code Playgroud)

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

例如,在我的例子中,我的 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
Run Code Online (Sandbox Code Playgroud)

保存此配置并使用上述配置重新启动 mosquitto

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

mosquitto_pub -t test -m test-message -p 8883 --cafile <path-to-your-ca-file> --insecure
Run Code Online (Sandbox Code Playgroud)

就我而言,它是:

mosquitto_pub -t test -m test-message -p 8883 --cafile /etc/ssl/certs/broker/ca.pem --insecure
Run Code Online (Sandbox Code Playgroud)

您需要使用--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.
Run Code Online (Sandbox Code Playgroud)

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

<user>:<password>
Run Code Online (Sandbox Code Playgroud)

在哪里是您想要允许的用户名的占位符,并且是您要设置的密码的占位符

例如:

panda:black
Run Code Online (Sandbox Code Playgroud)

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

mosquitto_passwd -U <path-to-passwordfile>
Run Code Online (Sandbox Code Playgroud)

现在内容如下:

panda:$6$a2foLssTVgMG4QY6$3rWvoLqwo1uCz6jZH6KDK3yAcWtIFlATbOSbwx7XJx2Q5Mix2S+iRqWI7KDqp43nSDdPV7mMvnYJS6tgHb7QjA==
Run Code Online (Sandbox Code Playgroud)

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

allow_anonymous false
password_file <path-to-passwordfile>
Run Code Online (Sandbox Code Playgroud)

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

要使用用户名和密码发布到经纪人,请使用:

mosquitto_pub -t test -m test-message -p 8883 --cafile <path-to-ca-file> -u panda -P black --insecure
Run Code Online (Sandbox Code Playgroud)