在mosquitto和Node-Red間使用基於的MQTTS雙向認證通訊( 私有CA)
概念說明
mosquitto(mqtt-broker)
Eclipse Mosquitto是實現MQTT協議版本5.0、3.1.1和3.1的開源消息代理。MQTT協議提供了使用發佈/訂閱模型執行消息傳遞的輕量級方法。
相關信息見 https://mosquitto.org/
Node-Red(mqtt-client)
Node-RED是構建物聯網應用程序的一個強大工具,其重點是簡化代碼塊的"連接"以執行任務。它使用可視化編程方法,允許開發人員將預定義的代碼塊(也叫做"節點")連接起來執行任務。連接的節點,通常是輸入節點、處理節點和輸出節點的組合,當它們連接在一起時,構成一個"流"。最初是IBM在2013年末開發的一個開源項目,以滿足他們快速連接硬件和設備到Web服務和其他軟件的需求–作爲物聯網的一種粘合劑–它很快發展成爲一種通用的物聯網編程工具。
相關信息見 https://nodered.org/
openssl
OpenSSL是用於傳輸層安全性(TLS)協議的健壯的,商業級,功能齊全的開源工具包,協議實現基於完整功能的通用密碼庫,該庫也可以獨立使用。
相關信息見 https://github.com/openssl/openssl
私有CA
CA(Certificate Authority)證書頒發機構主要負責證書的頒發、管理以及歸檔和吊銷。證書內包含了擁有證書者的姓名、地址、電子郵件帳號、公鑰、證書有效期、發放證書的CA、CA的數字簽名等信息。證書主要有三大功能:加密、簽名、身份驗證。
私有CA:在確定配置爲CA的服務器上生成一個自簽證書,併爲CA提供所需要的目錄及文件即可;
SSL單向/雙向認證
SSL單向認證只要求站點部署了ssl證書就行,任何用戶都可以去訪問(IP被限制除外等),只是服務端提供了身份認證。
雙向認證則是需要服務端與客戶端提供身份認證,只能是服務端允許的客戶能去訪問,安全性相對於要高一些。
相關信息
https://www.jianshu.com/p/fb5fe0165ef2
http://www.steves-internet-guide.com/ssl-certificates-explained/
搭建環境
服務端
操作系統:CentOS Linux release 7.8.2003
mosquitto: version 1.6.8
客戶端
Node-Red : mqtt 節點
基本流程
詳細操作
軟件安裝
mosquitto
What you'll need:
Port 1883 to be open to incoming MQTT messages
Step 1: Add the CentOS mosquitto repository to YUM's list of repositories
$ yum install epel-release
$ sudo yum install mosquitto
Step 2: Run mosquitto
As of writing, no init.d script exists for the CentOS distribution of mosquitto. However, it is a simple enough matter to set it running as a daemon, you'll just need to restart it yourself whenever your machine gets restarted.
$ sudo su
$ mosquitto -d -c /etc/mosquitto/mosquitto.conf > /var/log/mosquitto.log 2>&1
Mosquitto should now be running! You can test it by pointing mosquitto_pub and _sub at it.
認證配置
詳細見
http://www.steves-internet-guide.com/mosquitto-tls/
http://www.steves-internet-guide.com/mosquitto-tls/
mosquitto配置
服務端最終配置mosquitto.conf
port 8883
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
require_certificate true
tls_version tlsv1.2
use_identity_as_username true
Node-Red配置
- 上傳ca.crt,client.key,client.crt
- 更新節點即可連接
注:私有CA不要勾選“驗證服務器證書”
效果及測試
- mqttfx.exe (訂購主題)
- 連接
- 訂購主題
- 連接
- Node-Red (發佈主題)
- 連接
- 發佈數據
- 連接
- 數據通訊成功
總結
本文描述瞭如何在mosquitto和Node-Red間,使用基於的MQTTS雙向認證通訊( 私有CA),內容較多,需要使用者對MQTT、SSL、雙向認證等有基本的認識。許多關聯內容以鏈接形式在本文中引用,希望能對後來人有一定借鑑作用。