mosquitto 测试MQTT TLS单向认证和双向认证

mosquitto 配置和使用(windows)

Windows环境下安装配置Mosquitto服务及入门操作介绍
https://www.cnblogs.com/zkwarrior/p/10972464.html

mosquitto 介绍

mosquitto 是一款实现了消息推送协议 MQTT v3.1/v3.1.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机、嵌入式计算机、微型控制器等移动设备。
本博客的测试是基于win10系统。其他平台没有测试

mosquitto 下载和安装

下载地址:http://mosquitto.org/download/
本次下载版本:mosquitto-2.0.13-install-windows-x64.exe

注意:安装路径不要有空格,这里安装在 C:\mosquitto 这个路径下。

安装完成。如果需要使用,直接启动服务即可。
在这里插入图片描述
注意:启动此服务后,默认监听1883,只能本地127.0.0.1使用。

mosquitto 设置用户名和密码

https://www.cnblogs.com/zkwarrior/p/10950294.html

插入新用户名及密码,输入密码时界面是不会显示的,直接输入后回车就可以,需要连续输入两次。保证pwfile.example的路径和上面的配置一致。下面打开CMD并进入mosquitto根目录输入:

mosquitto_passwd -c C:\mosquitto\passwordfile\pwfile.example FirstUserName (使用-c 参数会导致清空密码文件,重新插入用户)
mosquitto_passwd C:\mosquitto\passwordfile\pwfile.example SecondUserName (不使用-c 表示追加用户,不影响旧用户)

openssl 生成证书(这里linux生成)

create_ca.sh 脚本

# * 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                     = 10.1.36.95
EOF
 
 
 
cat > client_cert.conf << EOF 
[ req ]
distinguished_name     = req_distinguished_name
prompt                 = no
[ req_distinguished_name ]
 O                      = $PROJECT_NAME Device Certificate
 CN                     = 10.1.36.96
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

将上述代码保存为create_ca.sh

做如下修改,终端执行。

  • 修改 CN 域中 IP 地址为你主机/设备的 IP 地址
  • [可选]加密位数 1024 修改为你需要的加密位数

这里将生成的ca、server、client相关证书,统一拷贝到了 C:\mosquitto\ca 目录下。

mosquitto.conf 配置

mosquitto.conf man page
https://mosquitto.org/man/mosquitto-conf-5.html

mosquitto.conf 配置文件在安装路径下,这里在:C:\mosquitto

注意:mosquitto.conf 配置修改,重启mosquitto服务才能生效。为了方便调试和操作,我们后面的操作全部通过命令行模式进行。

mosquitto 登录认证的四种类型

  1. 允许匿名登录
  2. 进行用户名、密码校验
  3. TLS单向证书认证
  4. TLS双向认证认证:TLS双向证书的认证加密以及用户名密码认证、TLS双向证书的认证加密

mosquitto.conf 配置是否开启匿名登录

1.配置开启匿名登录

allow_anonymous true

2.重启mosquitto服务,cmd窗口启动服务:mosquitto -v -c mosquitto.conf
3.MQTT Client匿名尝试连接验证。(切换参数值,对比)

注:当关闭匿名登录时,client需要填用户名和密码。

password_file C:\mosquitto\passwordfile\pwfile.example
allow_anonymous false

mosquitto.conf 配置TLS单向认证

TLS单向认证:Client验证服务器端的证书,比较Client本地配置证书和服务器返回的证书。

修改mosquitto.conf文件,可直接在文件末尾追加

listener 8883
protocol mqtt
password_file C:\mosquitto\passwordfile\pwfile.example
cafile C:\mosquitto\ca\ca.crt
certfile C:\mosquitto\ca\server.crt
keyfile C:\mosquitto\ca\server.key
allow_anonymous false

2.重启mosquitto服务,cmd窗口启动服务:mosquitto -v -c mosquitto.conf
3.MQTT Client匿名尝试连接验证。(切换参数值,对比)

mosquitto.conf配置TLS双向认证

TLS双向认证:Client验证服务器端的证书,Server验证客户端证书。

修改mosquitto.conf文件,可直接在文件末尾追加

listener 8883
protocol mqtt
password_file C:\mosquitto\passwordfile\pwfile.example
cafile C:\mosquitto\ca\ca.crt
certfile C:\mosquitto\ca\server.crt
keyfile C:\mosquitto\ca\server.key
require_certificate true
use_identity_as_username false
allow_anonymous true

require_certificate 参数说明
只有配置了:就是需要SSL认证,要命是单向认证、要命是双向认证
在这里插入图片描述

use_identity_as_username 取值不同,表示两种情况:
use_identity_as_username 为true表示可以不验证server端的用户名和密码,此时要开启匿名登录 allow_anonymous
use_identity_as_username 为false表示需要验证server端的用户名和密码

2.重启mosquitto服务,cmd窗口启动服务:mosquitto -v -c mosquitto.conf
3.MQTT Client匿名尝试连接验证。(切换参数值,对比)

mosquitto.conf配置同时支持TLS单向认证和双向认证

#先配置TLS双向认证
listener 8883
protocol mqtt
password_file C:\mosquitto\passwordfile\pwfile.example
cafile C:\mosquitto\ca\ca.crt
certfile C:\mosquitto\ca\server.crt
keyfile C:\mosquitto\ca\server.key
require_certificate true
use_identity_as_username false
allow_anonymous true

#再配置TLS单向认证,换个监听端口
listener 8884
cafile C:\mosquitto\ca\ca.crt
certfile C:\mosquitto\ca\server.crt
keyfile C:\mosquitto\ca\server.key

其他

  1. mosquitto.conf 可配置TLS版本,参数tls_version,默认支持v1.3 and v1.2

MQTT Client 调测

工具是windows下的MQTT.fx,下载地址:http://www.jensd.de/apps/mqttfxhttp://mqttfx.jensd.de/index.php/download

MQTT.fx使用详解
https://kunaly.blog.csdn.net/article/details/107492604

单向认证的配置:
在这里插入图片描述
双向认证的配置1:
use_identity_as_username false

SSL/TLS配置:
在这里插入图片描述
用户名和密码配置:
在这里插入图片描述

双向认证的配置2:
use_identity_as_username true
在这里插入图片描述
password 应该是要和生成client.key时设置的密码一致。

参考资料

SSL/TLS 双向认证(二) – 基于mosquittto的MQTT双向认证
https://blog.csdn.net/ustccw/article/details/76905459

mqtt mosquitto TLS安全连接,单向和双向认证
https://blog.csdn.net/qq_36202485/article/details/88949091

windows下的mosquitto安装配置
https://blog.csdn.net/qq_22111417/article/details/84142509

windows平台Mosquitto使用
https://blog.csdn.net/sxpsxp12/article/details/77870109

基于mosquitto的MQTT服务器—SSL/TLS 单向认证+双向认证
https://blog.csdn.net/ty1121466568/article/details/81118468

mosquitto 常用命令
https://www.cnblogs.com/smartlife/articles/10182136.html

SSL3_GET_CLIENT_CERTIFICATE:peer did not return a certificate
https://blog.csdn.net/moxiaomomo/article/details/52995447

OpenSSL生成CA证书及终端用户证书
https://www.cnblogs.com/nidey/p/9041960.html

Paho -物联网 MQTT C Cient的实现和详解
https://www.cnblogs.com/homejim/p/8196763.html

五、其他信息记录

5.1 MQTT TLS 双向认证失败 (openssl 验证证书密码是否正确)

问题描述:我方设备MQTTS无法连接第三方平台,说另外厂家设备可以。

  1. 平台方提供多组:MQTTS TLS证书(ca.crt、client.crt、client_pkcs8.key)+ key 密码 + MQTT信息(IP、端口、用户名、密码)
  2. 现象:MQTT时好使,说明平台IP、用户名、密码对的。
  3. 现象:抓包显示没有进行TLS握手TCP就直接断开了、client没发送握手请求,推理:①平台/网络问题 ②证书密码不对,导致无法解析。

问题原因:client.key 密码不对

openssl 验证key密码是否正确:

  • 查看证书内容:openssl rsa -in client.key -text -noout 若无密码直接显示,有密码提示输入密码
  • pkcs8 转成 pkcs1:openssl pkcs8 -in U-JF0001_pkcs8.key -topk8 -out U-JF0001.pem
  • pkcs1 转成 pkcs8 并设置密码:openssl pkcs8 -in client.key -topk8 -v2 des3 -out client_pkcs8.key

5.2 mqtt.fx 工具无法使用 pkcs8 格式的证书文件

将 pkcs8 转成 pkcs1 格式。

遗留问题:

  • 证书文件有哪些格式
  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值