IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> mosquitto支持ssl/tls双向认证 -> 正文阅读

[网络协议]mosquitto支持ssl/tls双向认证

一、生成证书

1、生成根证书ca

#
# Generate the certificate.
#

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

mkdir ca

#生成证书
openssl genrsa -out ca.key 2048
openssl req -out ca.req -key ca.key -new -config ./ca_cert.conf
openssl x509 -req -in ca.req -out ca.crt -sha256 -days 365 -signkey ca.key

mv ca.crt ca.key ca/
rm ca.srl

说明:

? ? ? ? 上边shell脚本可直接使用,建议生成脚本文件createCa.sh

? ? ? ? 根证书ca目前秘钥长度一般都为2048,如上所示,如果改成更小的1024,可能会报错。

? ? ? ? 同样哈希算法建议采用sha256,如上所示,使用sha1也可能会报错。?

2、生成server端证书

#
# Generate the server certificates and keys.
#

PROJECT_NAME="TLS Project"

#配置文件
# Generate the openssl configuration files.
cat > server_cert.conf << EOF 
[ req ]
distinguished_name     = req_distinguished_name
prompt                 = no
[ req_distinguished_name ]
O                      = $PROJECT_NAME
CN                     = 192.168.0.11
EOF

mkdir server

#生成证书
openssl genrsa -out server.key 2048
openssl req -out server.req -key server.key -new -config ./server_cert.conf
openssl x509 -req -in server.req -out server.crt -sha256 -CAcreateserial -days 365 -CA ca/ca.crt -CAkey ca/ca.key

mv server.crt server.key server/

rm server_cert.conf
rm server.req

说明:

????????上边shell脚本可直接使用,建议生成脚本文件createServer.sh

? ? ? ? 上边CN是服务器的ip地址,使用者需要改成自己的服务器ip。?

3、生成client端证书

#
# Generate the client certificates and keys.
#

PROJECT_NAME="TLS Project"

#配置文件
# Generate the openssl configuration files.
cat > client_cert.conf << EOF 
[ req ]
distinguished_name     = req_distinguished_name
prompt                 = no
[ req_distinguished_name ]
O                      = $PROJECT_NAME Device Certificate
CN                     = 192.168.0.3
EOF

mkdir client

#生成证书
openssl genrsa -out client.key 2048
openssl req -out client.req -key client.key -new -config ./client_cert.conf
openssl x509 -req -in client.req -out client.crt -sha256 -CAcreateserial -days 365 -CA ./ca/ca.crt -CAkey ./ca/ca.key

mv client.crt client.key client/

rm client_cert.conf
rm client.req

说明:

????????上边shell脚本可直接使用,建议生成脚本文件createClient.sh

? ? ? ? 上边CN是客户端的ip地址,使用者需要改成自己的客户端ip。?

4、验证证书是否可用

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

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

?二、配置mosquitto服务器

1、拷贝证书

#创建文件夹
sudo mkdir /etc/mosquitto/CA
sudo mkdir /etc/mosquitto/CA/server

#拷贝证书文件
sudo cp ca/ca.crt /etc/mosquitto/CA/
sudo cp server/server.crt /etc/mosquitto/CA/server
sudo cp server/server.key /etc/mosquitto/CA/server

#设置权限
sudo chmod 777 /etc/mosquitto/CA/server/server.crt
sudo chmod 777 /etc/mosquitto/CA/server/server.key

2、设置mosquitto配置文件

1)双向认证配置如下:?

????????打开配置文件mosquitto.conf,增加如下脚本。

listener 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

说明:

? ? ? ? 配置文件默认是没有的,执行一次下边第6节的指令便会自动生成。

2)单向认证注释掉下边两行?

#require_certificate true

#use_identity_as_username true

?3)不需要认证时全部注释掉

#listener 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

4、是否允许匿名登入

#true允许匿名访问,false不允许。
allow_anonymous true

5、设置用户名和密码

mosquitto_passwd -c /etc/mosquitto/passwd.file admin

说明:

????????输入命令后,会提示输入两次密码,输入即可。

? ? ? ? ?-c为新建文件夹,如果增加用户名和密码,不要加此参数。

6、启动mosquitto服务器

sudo mosquitto -c /etc/mosquitto/mosquitto.conf

说明:

? ? ? ? 系统重启后会自动在后台启动以上命令。

?????????有时杀死后会自动重启该服务,如果想关闭,可修改mosquitto.conf文件名后,再杀死进程。

三、客户端调试

1、消息订阅

? ? ? ? 1)单向认证订阅

mosquitto_sub -h 192.168.0.11 -p 8883 -u "admin" -P "123456" -i "client1" -t "test" --cafile /home/xxx/xxx/ca/ca.crt

? ? ? ? 2)双向认证订阅?

mosquitto_sub -h 192.168.0.11 -p 8883 -u "admin" -P "123456" -i "client1" -t "test" --cafile /home/xxx/xxx/ca/ca.crt --cert /home/xxx/xxx/client/client.crt --key /home/xxx/xxx/client/client.key

?2、发布消息

? ? ? ? 1)单向认证发布

mosquitto_pub -h 192.168.0.11 -p 8883 -u "admin" -P "123456" -i "client2" -t "test" -m "hello world" --cafile /home/xxx/xxx/ca/ca.crt

? ? ? ? 2)双向认证发布?

mosquitto_pub -h 192.168.0.11 -p 8883 -u "admin" -P "123456" -i "client2" -t "test" -m "hello world" --cafile /home/xxx/xxx/ca/ca.crt --cert /home/xxx/xxx/client/client.crt --key /home/xxx/xxx/client/client.key
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-10-17 13:08:02  更:2022-10-17 13:10:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 6:40:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码