我在使用TLS通过MQTT从NodeMCU向Raspberry Pi发送数据时遇到了问题 .

Configuration

Raspberry Pi(搭载Minibian)运行Mosquitto(版本1.4.11)经纪人 . 它配置如下:

allow_anonymous true

listener 8883
cafile /etc/mosquitto/ca.crt
certfile /etc/mosquitto/minibian.crt
keyfile /etc/mosquitto/minibian.key
require_certificate false

Certs由带有SHA256的https://github.com/owntracks/tools/tree/master/TLS的generate-CA.sh生成(我也尝试过MD5) . 在生成证书之前,IPLIST(使用者替代名称)环境变量是使用NodeMCU的IP定义的 .

NodeMCU的固件是由主分支的 Cloud 构建服务构建的,启用了MQTT和TLS支持 .

Code

代码上传后,我输入NodeMCU终端 dofile("cert.lua") . 以下是此文件的内容:

print (tls.cert.verify([[
-----BEGIN CERTIFICATE-----
cert here
-----END CERTIFICATE-----
]]))

它打印'真实' .

然后重启后:

function connect_to_broker()
   print ("Waiting for the broker")
   tls.cert.verify(true)
   m:connect(BROKER, BRPORT, 1, 1,
      function (client)
      print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID )
      end,
   handle_connection_error
)
m:on("offline", handle_broker_offline)
end

[...]

print "Connecting to MQTT broker. Please wait..."
m = mqtt.Client( CLIENTID, MQTT_KEEPALIVE, BRUSER, BRPWD)
connect_to_broker()

Result

Mosquitto印刷品:

1488542161: New connection from 192.168.0.101 on port 8883.
1488542162: OpenSSL Error: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure
1488542162: Socket error on client <unknown>, disconnecting.

当我使用相同的证书通过以下命令从我的PC连接到代理时,代理接受连接并将消息传递给订阅者 .

mosquitto_pub --cafile ca.crt -h 192.168.0.103 -p 8883 -t /test -m message

首先,我没有设置IPLIST变量 . 然后我发现我有过时的mosquittoI(需要支持MQTT 3.1.1) . 然后我发现NodeMCU只支持一些签名算法,因此我将其更改为SHA256,因为我知道它是受支持的 . 你知道我的代码/配置有什么问题吗?