基于mosquitto的MQTT服务器---SSL/TLS 单向认证+双向认证

本文为参考网上其他博文搭建出服务器后的步骤记录,如有冒犯,请私信!!!

目录... 3

第 1 章 安装Mosquitto. 4

1.1 方法一:手动编译安装... 4

1.2方法二:在Ubuntu下使用apt-get安装... 5

第 2 章 配置单/双向认证... 7

2.1 生成证书... 7

2.2 CA校验证书测试... 9

2.3 配置单/双向认证... 10

2.4 单双向切换... 12

扫描二维码关注公众号,回复: 2249191 查看本文章

2.5 同时打开单双向认证... 13

2.6 验证... 14

 

第 1 章 安装Mosquitto

在Ubuntu下安装该软件的常用方法有两种,两种均能使该软件作为中转服务器使用,不过笔者在使用方法二安装的mosquitto时,有些配置没有生效,不知是否是笔者当时在配置是否有误还是该方式安装的不完全(该方式安装的mosquitto要比编译安装的少一些示例配置文件)。要想使用双向认证,单向认证等功能,推荐使用方法一。

1.1 方法一:手动编译安装

step1.下载源码

Mosquitto官网:http://mosquitto.org/

 

Mosquitto最新的源码:http://mosquitto.org/files/source/

 

选择一个喜欢的版本即可,笔者在这里选择最新的1.5版

mosquitto-1.5.tar.gz

step2.解压源码

tar -xzvf mosquitto-1.5.tar.gz

step3.进入源码目录

cd mosquitto-1.5/

step4.编译和安装

make && make install

其中,make install需要root权限。

在安装过程中会由于缺失一些库而报错,直接百度,然后安装即可

可能遇到的问题及其解决方式:

【1】编译找不到openssl/ssl.h

    【解决方法】——安装openssl

sudo apt-get install libssl-dev

【2】编译过程找不到ares.h

sudo apt-get install libc-ares-dev

【3】编译过程找不到uuid/uuid.h

sudo apt-get install uuid-dev

【4】使用过程中找不到libmosquitto.so.1

error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

    【解决方法】——修改libmosquitto.so位置

# 创建链接

sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

# 更新动态链接库

sudo ldconfig

【5】make: g++:命令未找到 

    【解决方法】

    安装g++编译器

sudo apt-get install g++

step5.程序安装位置:

step6.本机测试

打开一个终端,执行以下命令订阅主题"mqtt"

$mosquitto_sub -h localhost -t "mqtt"-v

打开另外一个终端,发布消息到主题 “mqtt”

$mosquitto_pub -h localhost -t "mqtt"-m "Hello MQTT"

现在你会看到消息被显示在前一个终端上了.

 

step7. 网络测试(test.mosquitto.org

从test.mosquitto.org订阅所有主题,任何人发布的任何消息你都可以收到,你会看到大量有趣消息滚动在你的屏幕上。

你也可以发布一些有趣的消息,所有订阅的人也同样会收到。

$mosquitto_sub -h test.mosquitto.org -t "#" -v

当然,你也可以只订阅特定主题,用来接收你自己的消息

$mosquitto_sub -h test.mosquitto.org -t "msg_only_from_sgks" -v

在另外一个终端上发布消息到特定主题,消息"My cat is Luna"应该会显示在上一个终端上

$mosquitto_pub -h test.mosquitto.org -t "msg_only_from_sgks" -m "My cat is Luna"

 

 

1.2方法二:在Ubuntu下使用apt-get安装

step 1. 引入mosquitto仓库并更新

$sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

$sudo apt-get update

step 2. 执行以下命令安装mosquitto

$sudo apt-get installmosquitto

step 3. 安装mosquitto开发包

$sudo apt-get installlibmosquitto-dev

step 4. 安装mosquitto客户端

$sudo apt-get installmosquitto-clients

step 5. 查询mosquitto是否正确运行

$sudo service mosquitto status

step 6. 本机测试

打开一个终端,执行以下命令订阅主题"mqtt"

$mosquitto_sub -h localhost -t "mqtt"-v

打开另外一个终端,发布消息到主题 “mqtt”

$mosquitto_pub -h localhost -t "mqtt"-m "Hello MQTT"

现在你会看到消息被显示在前一个终端上了.

step 7. 网络测试(test.mosquitto.org

从test.mosquitto.org订阅所有主题,任何人发布的任何消息你都可以收到,你会看到大量有趣消息滚动在你的屏幕上。

你也可以发布一些有趣的消息,所有订阅的人也同样会收到。

$mosquitto_sub -h test.mosquitto.org -t "#" -v

当然,你也可以只订阅特定主题,用来接收你自己的消息

$mosquitto_sub -h test.mosquitto.org -t "msg_only_from_sgks" -v

在另外一个终端上发布消息到特定主题,消息"My cat is Luna"应该会显示在上一个终端上

$mosquitto_pub -h test.mosquitto.org -t "msg_only_from_sgks" -m "My cat is Luna"

 

第 2 章 配置单/双向认证

2.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_name

prompt                 = no

 

[ req_distinguished_name ]

 O                      = $PROJECT_NAME Dodgy Certificate Authority

EOF

 

cat > server_cert.conf << EOF 

[ req ]

distinguished_name     = req_distinguished_name

prompt                 = no

 

[ req_distinguished_name ]

 O                      = $PROJECT_NAME

 CN                     = 192.168.111.100

EOF

 

cat > client_cert.conf << EOF 

[ req ]

distinguished_name     = req_distinguished_name

prompt                 = no

 

[ req_distinguished_name ]

 O                      = $PROJECT_NAME Device Certificate

 CN                     = 192.168.111.101

EOF

 

mkdir ca

mkdir server

mkdir client

mkdir certDER

 

# private key generation

openssl genrsa -out ca.key 1024

openssl genrsa -out server.key 1024

openssl genrsa -out client.key 1024

 

# cert requests

openssl req -out ca.req -key ca.key -new \

            -config ./ca_cert.conf

openssl req -out server.req -key server.key -new \

            -config ./server_cert.conf

openssl 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.key

openssl x509 -req -in server.req -out server.crt \

            -sha1 -CAcreateserial -days 5000 \

            -CA ca.crt -CAkey ca.key

openssl x509 -req -in client.req -out client.crt \

            -sha1 -CAcreateserial -days 5000 \

            -CA ca.crt -CAkey ca.key

 

openssl x509 -in ca.crt -outform DER -out ca.der

openssl x509 -in server.crt -outform DER -out server.der

openssl x509 -in client.crt -outform DER -out client.der

 

mv 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 *.conf

rm *.req

rm *.srl

 

 

 

 

将上述代码保存为makefile.sh

做如下修改,终端执行。

 

- 修改 CN 域中 IP 地址为你主机/设备的 IP 地址

- [可选]加密位数 1024 修改为你需要的加密位数

 

2.2 CA校验证书测试

进行如下测试,以验证证书是否可用:

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

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

结果如下:

2.3 配置单/双向认证

step 1.进入配置文件

sudo vim /etc/mosquitto/mosquitto.conf

step 2.找到 Default listener,在该栏下进行如下配置

 

再找到Certificate based SSL/TLS support字段.

 

即:

 

port 8883

cafile /etc/mosquitto/CA/ca.crt

certfile /etc/mosquitto/CA/server/server.crt

keyfile /etc/mosquitto/CA/server/server.key

require_certificate true

use_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

 

双向认证通常设置为true

step 3.重启服务

mosquitto -c /etc/mosquitto/mosquitto.conf

 

如果提示端口被占用,先ps出mosquitto,再kill掉

ps -aux | grep "mosquitto"

kill -9 XXXXX

2.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

 

2.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

2.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.pem --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.pem --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

 

猜你喜欢

转载自blog.csdn.net/ty1121466568/article/details/81118468
今日推荐