mosquitto使用与openssl证书配置

开发环境:ubuntu18.04 64bit

mqtt客户端测试工具:mqtt.fx 1.7.1

1.安装

# 引入库
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
# 安装
sudo apt-get install mosquitto
#安装客户端
sudo apt-get install mosquitto-clients

2.测试

2.1启动mosquitto

# 查看mosquitto是否启动
ps -aux | grep mosquitto
mosquit+  44224  0.0  0.1  48032  6208 ?        S    20:01   0:00 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

# 启动或停止mosquitto
sudo service mosquitto start
sudo service mosquitto stop

# 或指定配置文件启动
sudo mosquitto -d -c /etc/mosquitto/mosquitto.conf

2.2mqtt.fx配置

打开mqtt.fx,新建一个配置,填定服务器ip和mqtt服务默认端口,点connect看能不能连接上。

2.3 订阅或发布消息

# 订阅消息
mosquitto_sub -h ip -t "test"
-h:服务器的ip地址,也就是mosquitto程序所在系统的ip地址
-t:topic

# 发布消息
mosquitto_pub -h ip -t "test" -m "my name is xxx"
-h:服务器的ip地址,也就是mosquitto程序所在系统的ip地址
-t:topic
-m:消息的内容

在mqtt.fx上发布消息

在ubuntu上的订阅就收到了这条消息

在mqtt.fx上订阅消息,在ubuntu上发布消息

2.4 抓包

通过抓包可看到,此时mqtt传输的还是明文。

3.生成证书

生成证书可以参数这篇博文:mosquitto 证书的配置 | 码农家园

但我根据这篇博文生成的证书双向验证一直失败,没找到什么原因,后来我看了另一篇博文:MQTT Mosquitto broker – Client Authentication and Client Certificates – Primal Cortex's Weblog

用文中提供的一个脚本generate-CA.sh生成证书就没有问题了:

https://github.com/owntracks/tools/blob/master/TLS/generate-CA.sh

生成ca证书和server证书:

sy@ubuntu:~/key$ ./generate-CA.sh
Generating a RSA private key
......++++
...++++
writing new private key to './ca.key'
-----
Created CA certificate in ./ca.crt
subject=
    commonName                = An MQTT broker
    organizationName          = OwnTracks.org
    organizationalUnitName    = generate-CA
    emailAddress              = nobody@example.net
Warning: the CA key is not encrypted; store it safely!
--- Creating server key and signing request
Generating RSA private key, 4096 bit long modulus (2 primes)
......++++
..................................................................................++++
e is 65537 (0x010001)
--- Creating and signing server certificate
Signature ok
subject=CN = ubuntu, O = OwnTracks.org, OU = generate-CA, emailAddress = nobody@example.net
Getting CA Private Key
sy@ubuntu:~/key$ ls
ca.crt  ca.key  ca.srl  generate-CA.sh  ubuntu.crt  ubuntu.csr  ubuntu.key

生成客户端证书:

sy@ubuntu:~/key$ ./generate-CA.sh client client
--- Creating client key and signing request
Generating RSA private key, 4096 bit long modulus (2 primes)
...........................................................++++
.......................................................................................++++
e is 65537 (0x010001)
--- Creating and signing client certificate
Signature ok
subject=CN = client
Getting CA Private Key
sy@ubuntu:~/key$ ls
ca.crt  ca.srl      client.csr  generate-CA.sh  ubuntu.csr
ca.key  client.crt  client.key  ubuntu.crt      ubuntu.key

4.单向验证

4.1修改mosquitto的配置文件

在配置文件中配置openssl证书路径,修改后别忘了重启mosquitto

sudo vim /etc/mosquitto/mosquitto.conf
# 在配置文件中添加下面三行
cafile /home/sy/key/ca.crt
certfile /home/sy/key/ubuntu.crt
keyfile /home/sy/key/ubuntu.key

4.2mqtt.fx配置

填写配置名称,填写服务器地址和默认端口,选择开启ssl,配置CA证书。

4.3 订阅或发布消息

# 订阅消息
mosquitto_sub -h ip -t test --cafile ./ca.crt

#发布消息
mosquitto_pub -h ip -t test -m "my name is xxx" --cafile ./ca.crt

mqtt.fx的操作和之前一样。

4.4 抓包

通过抓包可以发现,此时已经看到不明文的消息了。

5.双向验证

5.1修改配置文件

sudo vim /etc/mosquitto/mosquitto.conf
# 在配置文件中添加下面一行,表示需要客户端提供证书
require_certificate true

5.2mqtt.fx配置

填写配置名称,填定服务器地址和默认端口,选择开启ssl,配置CA证书和客户端证书。

5.3订阅和发布消息

# 订阅消息
mosquitto_sub -h ip -t test --cafile ./ca.crt --cert ./client.crt --key ./client.key

#发布消息
mosquitto_pub -h ip -t test -m "my name is xxx" --cafile ./ca.crt --cert ./client.crt --key ./client.key

mqtt.fx的操作和之前一样。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值