基于mosquitto的MQTT服務器---SSL/TLS 單向認證+雙向認證

配置單/雙向認證

1.生成證書

使用如下shell 來生成證書:

# * Redistributions in binary form must reproduce the above copyright#   notice, this list of conditions and the following disclaimer in the#   documentation and/or other materials provided with the distribution.# * Neither the name of the axTLS project nor the names of its#   contributors may be used to endorse or promote products derived#   from this software without specific prior written permission.## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.### Generate the certificates and keys for testing.#PROJECT_NAME="TLS Project"# Generate the openssl configuration files.cat > ca_cert.conf << EOF [ req ]distinguished_name     = req_distinguished_nameprompt                 = no[ req_distinguished_name ]O                      = $PROJECT_NAME Dodgy Certificate AuthorityEOFcat > server_cert.conf << EOF [ req ]distinguished_name     = req_distinguished_nameprompt                 = no[ req_distinguished_name ]O                      = $PROJECT_NAMECN                     = 192.168.111.100EOFcat > client_cert.conf << EOF [ req ]distinguished_name     = req_distinguished_nameprompt                 = no[ req_distinguished_name ]O                      = $PROJECT_NAME Device CertificateCN                     = 192.168.111.101EOFmkdir camkdir servermkdir clientmkdir certDER# private key generationopenssl genrsa -out ca.key 1024openssl genrsa -out server.key 1024openssl genrsa -out client.key 1024# cert requestsopenssl req -out ca.req -key ca.key -new -config ./ca_cert.confopenssl req -out server.req -key server.key -new -config ./server_cert.confopenssl req -out client.req -key client.key -new -config ./client_cert.conf# generate the actual certs.openssl x509 -req -in ca.req -out ca.crt -sha1 -days 5000 -signkey ca.keyopenssl x509 -req -in server.req -out server.crt -sha1 -CAcreateserial -days 5000  -CA ca.crt -CAkey ca.keyopenssl x509 -req -in client.req -out client.crt -sha1 -CAcreateserial -days 5000  -CA ca.crt -CAkey ca.keyopenssl x509 -in ca.crt -outform DER -out ca.deropenssl x509 -in server.crt -outform DER -out server.deropenssl x509 -in client.crt -outform DER -out client.dermv ca.crt ca.key ca/mv server.crt server.key server/mv client.crt client.key client/mv ca.der server.der client.der certDER/rm *.confrm *.reqrm *.srl

將上述代碼保存為makefile.sh

做如下修改,終端執行。

  • 修改 CN 域中 IP 地址為你主機/設備的 IP 地址
  • [可選]加密位數 1024 修改為你需要的加密位數

2. CA校驗證書測試

進行如下測試,以驗證證書是否可用:

$openssl verify -CAfile ca/ca.crt server/server.crt$openssl verify -CAfile ca/ca.crt client/client.crt

3.配置單/雙向認證

step 1.進入配置文件

sudo vim /etc/mosquitto/mosquitto.conf

step 2.找到 Default listener,在該欄下進行如下配置,再找到Certificate based SSL/TLS support字段.

port 8883cafile /etc/mosquitto/CA/ca.crtcertfile /etc/mosquitto/CA/server/server.crtkeyfile /etc/mosquitto/CA/server/server.keyrequire_certificate trueuse_identity_as_username true

根據自己路徑不同配置校驗文件路徑,筆者是把文件放在/etc/mosquitto/CA/下

注意!!!

根據單向認證和雙向認證需要,可能需修改的字段有:

a) port 8883 // MQTT服務器將選擇此端口 listen

b) cafile /etc/mosquitto/CA/ca.crt

雙向認證必須配置為你的CA證書
單向認證(通常認為是client校驗server證書,下同)可選配置
單向認證中,server 和 client 端 ca 配置必須保持一致。即 server 若配置 ca.crt ,則 client 必須配置 ca.crt, server 不配置ca.crt ,client 也不可配置 ca.crt
路徑必須為絕對路徑!!!

c) certfile /etc/mosquitto/CA/server/server.crt
單項認證和雙向認證都必須配置為你的server證書

d) keyfile /etc/mosquitto/CA/server/server.key
單項認證和雙向認證都必須配置為你的server私鑰

e) require_certificate true
單向認證需設置為 false,注釋此行,默認也是 false

雙向認證必須配置為true

f) use_identity_as_username true
單向認證設置為 false,注釋此行,默認也是 false,注意設置為false,則需要用戶名和密碼鑒權,反之,則不需要
雙向認證通常設置為true

step 3.重啟服務

mosquitto -c /etc/mosquitto/mosquitto.conf

如果提示端口被占用,先ps出mosquitto,再kill掉
ps -aux | grep "mosquitto"
kill -9 XXXXX

4.單雙向切換

單向認證只需要注釋兩行即可:

#require_certificate true#use_identity_as_username true

如下:

port 8883
cafile  /etc/mosquitto/CA/ca/ca.crt
certfile  /etc/mosquitto/CA/server/server.crt
keyfile  /etc/mosquitto/CA/server/server.key

5.同時打開單雙向認證

step 1.
在先將/etc/mosquitto/mosquitto.conf 文件按2.4節配置默認打開雙向認證,再找到 Extra listener字段
進行如下配置,打開另一個端口用作單向認證

step 2.
再在該字段下找到Certificate based SSL/TLS support字段

step 3.重啟服務
mosquitto -c /etc/mosquitto/mosquitto.conf

如果提示端口被占用,先ps出mosquitto,再kill掉
ps -aux | grep "mosquitto"
kill -9 XXXXX

6 驗證

step 1.進入到CA證書目錄下:

step 2.雙向:
終端一:訂閱
mosquitto_sub -h 10.30.11.47 -p 8883 -t "mqtt/server/topic" --cafile ./ca/ca.crt --cert ./client/client.crt --key ./client/client.key &

終端二:發布
mosquitto_pub -h 10.30.11.47 -p 8883 -t "mqtt/server/topic" -m "hello,world!" --cafile ./ca/ca.crt --cert ./server/server.crt --key ./server/server.key

step 3. 單向
終端一:訂閱
mosquitto_sub -h 10.30.11.47 -p 8884 -t "mqtt/server/topic" --cafile ./ca/ca.crt &

終端二:發布
mosquitto_pub -h 10.30.11.47 -p 8884 -t "mqtt/server/topic" -m " hello,world!" --cafile ./ca/ca.crt

轉載于:https://www.cnblogs.com/Paul-watermelon/p/11238388.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392228.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392228.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392228.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql復制的工作原理及主從復制的實現

mysql的復制功能主要有3個步驟主服務器將改變記錄到二進制日志中&#xff0c;&#xff08;這些記錄叫做二進制日志事件&#xff09;從服務器將主服務器的二進制日志事件拷貝到它的中繼日志中從服務器重做中繼日志中的事件。該過程的第一部分就是主服務器記錄二進制日志&#xf…

33條C#、.Net經典面試題目及答案

1&#xff0c; 請你說說.NET中類和結構的區別? 答&#xff1a;結構和類具有大體的語法&#xff0c;但是結構受到的限制比類要多。結構不能申明有默認的構造函數&#xff0c;為結構的副本是又編譯器創建和銷毀的&#xff0c;所以不需要默認的構造函數和析構函數。結構是值類型&…

pb 放棄數據窗口所做修改_為什么我放棄在線數據課程進行基于項目的學習

pb 放棄數據窗口所做修改by Josh Temple通過喬什坦普爾 為什么我放棄在線數據課程進行基于項目的學習 (Why I abandoned online data courses for project-based learning) 如何通過處理有趣的項目來發展基本數據技能 (How to develop essential data skills by tackling inte…

數字濾波器的matlab 與fpga實現,1 數字濾波器的MATLAB與FPGA實現——杜勇(配套光盤) 程序源碼 - 下載 - 搜珍網...

壓縮包 : f3d09239c2bf5ce6f06578c866ff06.rar 列表Chapter_3/E3_1/incremental_db/compiled_partitions/SymbExam.db_infoChapter_3/E3_1/incremental_db/READMEChapter_3/E3_1/simulation/modelsim/modelsim.iniChapter_3/E3_1/simulation/modelsim/msim_transcriptChapter_3…

Office 365 系列之九:配置和體驗 Exchange 和 Lync

在之前的篇章中&#xff0c;我們已經安裝好 Office 365 Pro Plus 和通過 O365 訂閱激活了。接下來我們來看看具體怎么配置和使用 Exchange 和 Skype, 這部分內容對于學習過 Exchange Server 2016 和 Skype For Business 2015 的同學來說就很簡單了。通過 OWA 訪問 Exchange 對于…

leetcode 1631. 最小體力消耗路徑(并查集)

你準備參加一場遠足活動。給你一個二維 rows x columns 的地圖 heights &#xff0c;其中 heights[row][col] 表示格子 (row, col) 的高度。一開始你在最左上角的格子 (0, 0) &#xff0c;且你希望去最右下角的格子 (rows-1, columns-1) &#xff08;注意下標從 0 開始編號&…

netflix_Netflix的Polynote

netflixNetflix open source Polynote is a new notebook environment and was born out of the necessity to accelerate data science experimentation at Netflix.Netflix開源Polynote是一種新的筆記本環境&#xff0c;其誕生是出于加速Netflix數據科學實驗的需要。 Over t…

Django-內置用戶系統

Django自帶的用戶認證 我們在開發一個網站的時候&#xff0c;無可避免的需要設計實現網站的用戶系統。此時我們需要實現包括 1.用戶注冊 2.用戶登錄 3.用戶認證 4.注銷 5.修改密碼 Django作為一個完美主義者的終極框架&#xff0c;當然也會想到用戶的這些痛點。它內置了強大的用…

React 與 可視化

一般會想到 canvas 和 svg ; svg更適合畫圖, 但由于cavans在移動端的良好兼容性, 使用的更廣; 什么是svg, scalable vector graphics 全稱 可縮放矢量圖形, 是一種來描述二維矢量圖形的xml語言 canvas 能做的 svg基本都可以做, 字體圖標, icon, logo 也能作 在ie上也是從ie9開…

css 跳動的心_如何用純CSS為您的情人打造一顆跳動的心

css 跳動的心Each year on February 14th, many people exchange cards, candies, gifts or flowers with their special “valentine”. The day of romance we call Valentine’s Day is named for a Christian martyr and dates back to the 5th century, but has origins i…

oracle怎么獲取行,在oracle中如何實現SELECT TOP N取得前幾行記錄

在sql server中可以通過SELECT TOP N來取得想要結果的前幾行的信息。但是在oracle中必須借助偽列rownum來完成一個查詢語句在取得結果集后&#xff0c;偽列rownum就會從1開始&#xff0c;從上往下依次遞增。rownum是對結果集的編序排列。例如下表&#xff1a;SQL> select id…

圖片管理程序(Java)

圖片管理程序 gitee完整代碼下載 github完整代碼下載 華南農業大學課程設計作品&#xff08;99分&#xff09; 問題描述 題目目的是編寫一個能夠對數字像片進行管理的應用程序。 程序能夠顯示的圖片格式包括,.JPG、.JPEG、.GIF、.PNG、和.BMP。 圖像文件尺寸,要求能夠處理從…

氣流與路易吉,阿戈,MLFlow,KubeFlow

任務編排工具和工作流程 (Task orchestration tools and workflows) Recently there’s been an explosion of new tools for orchestrating task- and data workflows (sometimes referred to as “MLOps”). The quantity of these tools can make it hard to choose which o…

移動WEB開發之JS內置touch事件[轉]

iOS上的Safari也支持click 和mouseover等傳統的交互事件&#xff0c;只是不推薦在iOS的瀏覽器應用上使用click和mouseover&#xff0c;因為這兩個事件是為了支持鼠標點擊而設計 出來的。Click事件在iOS上會有半秒左右的延遲&#xff0c;原因是iOS要highlight接收到click的eleme…

編寫代碼的軟件用什么編寫的_如何通過像編寫代碼一樣克服對編寫的恐懼

編寫代碼的軟件用什么編寫的by Chris Rowe通過克里斯羅 How often do you get the fear? What do I mean by fear? How about the knot I got in my stomach just before I plunged out of plane on a parachute jump? It’s more than the brain logically planning to av…

快速學習一個新的模塊

1、模塊名.__doc__查看函數簡介&#xff0c;為了便于查看&#xff0c;采用print&#xff08;模塊名.__doc__&#xff09;打印出來&#xff0c;這樣的格式便于查看。 2、采用內置方法dir()查看模塊的變量、函數、類等等&#xff1b;采用"模塊名.__all__"查看模塊中提供…

php 公鑰格式轉換,如何把OpenSSH公鑰轉換成OpenSSL格式

《如何把OpenSSH公鑰轉換成OpenSSL格式》要點&#xff1a;本文介紹了如何把OpenSSH公鑰轉換成OpenSSL格式&#xff0c;希望對您有用。如果有疑問&#xff0c;可以聯系我們。首先看看OpenSSL工具的簡單使用方法,我們以rsa加密算法為例生成一個私鑰&#xff1a;openssl genrsa -o…

模擬操作系統(Java)

gitee完整代碼下載 github完整代碼下載 一、 需求分析 模擬一個采用多道程序設計方法的單用戶操作系統&#xff0c;該操作系統包括進程管理、存儲管理、設備管理、文件管理和用戶接口四部分。預計程序所能達到的功能&#xff1a; 進程管理模擬&#xff1a;實現操作系統進程管…

數據庫面試復習_數據科學面試復習

數據庫面試復習大面試前先刷新 (REFRESH BEFORE THE BIG INTERVIEW) 介紹 (Introduction) I crafted this study guide from multiple sources to make it as comprehensive as possible. This guide helped me prepare for both the technical and behavioral aspects of the …

hibernate緩存

&#xff08;轉自&#xff1a;http://www.cnblogs.com/java-class/p/6108175.html&#xff09; 閱讀目錄 1. 為什么要用 Hibernate 緩存&#xff1f;2. 項目實戰3. Hibernate 緩存原理回到頂部1. 為什么要用 Hibernate 緩存&#xff1f; Hibernate是一個持久層框架&#xff0c;…