【问题标题】:Issues connecting to mosquitto broker with node mqtt client via SSL/TLS通过 SSL/TLS 使用节点 mqtt 客户端连接到 mosquitto 代理的问题
【发布时间】:2020-06-13 13:47:38
【问题描述】:

您好,我通过 eclipse docker 映像创建了一个 mosquitto 代理,最近按照本指南添加了 SSL/TLS 支持:http://www.steves-internet-guide.com/mosquitto-tls/

当我在运行代理的 VPS 中进行 sshed 时,我可以使用以下命令:

mosquitto_pub -h VPS_NAME -t test/topic -p 8883 --cafile ca.crt -m message -u BROKER_USERNAME -P BROKER_PASSWORD

它发布的一切都很好。但是,当我在本地计算机上运行相同的命令时,出现错误:

'Unable to connect (Lookup error.).

我没有从代理容器中获得任何新日志,所以我认为它甚至没有到达容器。但是当我运行时:

mosquitto_pub -h BROKER_IP_ADRESS -t test/topic -p 8883 --cafile ca.crt -m message -u BROKER_USERNAME -P BROKER_PASSWORD

我确实收到了响应错误:发生 TLS 错误,并且在我的 docker 日志中我得到:

1583004287: New connection from LOCAL_IP_ADDRESS on port 8883.
1583004287: OpenSSL Error: error:14037438:SSL routines:ACCEPT_SR_KEY_EXCH:tlsv1 alert internal error
1583004287: OpenSSL Error: error:140370E5:SSL routines:ACCEPT_SR_KEY_EXCH:ssl handshake failure
1583004287: Socket error on client <unknown>, disconnecting.

当我将 --insecure 命令添加到发布时,我只能获得成功的发布发送,但是我想确保客户端知道它正在与正确的服务器通信,所以我不认为这是正确的解决方案。

最后我想在节点应用程序上运行一个mqtt客户端,我试过这段代码:


const fs = require('fs');

const optionsz = {
  ca: [ fs.readFileSync(__dirname + '/ca.pem') ],
  host: 'BROKER_IP_ADDRESS',
  servername: 'VPS_NAME',
  port: 8883,
  rejectUnauthorized : false,
  username : 'BROKER_USERNAME', // mqtt credentials if these are needed to connect
  password : 'BROKER_PASSWORD',
  clientId : 'test',
  // Necessary only if the server's cert isn't for "localhost".
  checkServerIdentity: () => { return null; },
};


class MqttHandler {
  constructor() {
    this.mqttClient = null;

  };


  connect() {
    // Connect mqtt with credentials (in case of needed, otherwise we can omit 2nd param)
    this.mqttClient = mqtt.connect(this.host, optionsz);
...

当我运行它时,我不断收到断开连接事件,并且在我的 docker 日志中我得到:

1583004505: New connection from LOCAL_IP_ADDRESS on port 8883.
1583004505: OpenSSL Error: error:140260FC:SSL routines:ACCEPT_SR_CLNT_HELLO:unknown protocol
1583004505: Socket error on client <unknown>, disconnecting.

我真的很困惑如何解决这个问题,我已经能够连接到没有 SSL/TLS 保护的代理,但我想让我的设备通信更安全。 感谢您的宝贵时间!

【问题讨论】:

    标签: node.js ssl mqtt


    【解决方案1】:

    这里有两个单独的问题。

    1. 您的 VPS 似乎没有有效的 DNS 条目。 mosquitto_pub 失败,因为它无法将名称解析为 IP 地址。它适用于 --insecure 和 IP 地址,因为您告诉 mosquitto_pub 忽略代理证书中的 CN 或 SAN 不包含 IP 地址而仅包含名称这一事实。

    2. 您正在尝试连接原始 MQTT 而不是 MQTT over TLS,您需要使用 URL 而不仅仅是主机名或 connect() 函数的第一个参数。例如

      this.mqttClient = mqtt.connect("mqtts://" + this.host, optionsz);
      

    老实说,您需要同时解决这两个问题才能使事情正常运行。

    要修复 1,您需要对 DNS 条目进行排序,以便您拥有一个有效的完全限定主机名,该主机名指向您的 VPS 并与您在那里部署的证书相匹配。

    【讨论】:

    • 您好,感谢您的回复!例如,假设我的主机名是“example”,我在证书注册期间使用了它。我需要做的就是购买一个域名(例如:example.xyz),让我的 VPS 将其重定向到该 IP。我需要重新创建 FQDN 为 example.xyz 的证书还是只是示例?再次非常感谢!
    • 您的证书需要包含example.xyz,它需要与您用于查找机器的 fqdn 完全匹配
    猜你喜欢
    • 2013-09-24
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2022-01-02
    • 2012-10-11
    • 1970-01-01
    相关资源
    最近更新 更多