一、Mosquitto安装
二、OpenSSL安装
三、OpenSSL生成所需证书
1.生成CA的key和证书文件
使用命令为:
openssl req -new -x509 -days 36500 -extensions v3_ca -keyout ca.key -out ca.crt
该命令将为CA产生一个名字为“ca.key”的key文件和一个名字为“ca.crt”的证书文件,这个crt就是CA自己给自己签名的证书文件。
该命令中选项“-x509”表示该条命令将产生自签名的证书,一般都是测试的时候采用。
设置内容:
Country Name (2 letter code) [AU]:CN ← 国家
State or Province Name (full name) [Some-State]:BeiJing ← 省
Locality Name (eg, city) []:BeiJing ← 市
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 此时不输入
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
2.创建服务器证书密钥server.key
使用去除key文件口令的命令:
openssl rsa -in server.key -out server.key
3.创建服务器证书的申请文件server.csr
使用命令为:
openssl req -new -key server.key -out server.csr
设置内容:
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音
Locality Name (eg, city) []:BeiJing ← 市名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []:10.8.4.205 ← 服务器IP地址
Email Address []:admin@mycompany.com ← 电子邮箱,可随便填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
4.创建自当前日期起有效期为期两年的服务器证书server.crt:
使用命令:
openssl x509 -req -days 730 -sha1 -extensions v3_req -CA ca.crt -CAkey ca.key -CAserial ca.srl -CAcreateserial -in server.csr -out server.crt
设置内容:
Signature ok
subject=C = CN, ST = GUANGDONG, L = zhuhai, O = test1, OU = bb, CN = localhost, emailAddress = test2
Getting Private key
Enter pass phrase for server.key: -->输入之前设置的密码
5.创建客户端证书密钥文件client.key:
使用命令:
openssl genrsa -des3 -out client.key 2048
设置内容:
[lenin@archer ~]$ openssl genrsa -des3 -out client.key 2048
Generating RSA private key, 2048 bit long modulus
……………………………………………………………………………..+++
……………………………………………………………………………………………………….+++ e is 65537 (0×10001)
Enter pass phrase for client.key: ← 输入一个新密码
Verifying – Enter pass phrase for client.key: ← 重新输入一遍密码
6.创建客户端证书的申请文件client.csr:
使用命令:
openssl req -new -key client.key -out client.csr
设置内容为:
[lenin@archer ~]$ openssl req -new -key client.key -out client.csr
Enter pass phrase for client.key: ← 输入上一步中创建的密码
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省名称,拼音
Locality Name (eg, city) []:BeiJing ← 市名称,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不填
Common Name (eg, YOUR name) []:Lenin ← 自己的英文名,可以随便填
Email Address []:admin@mycompany.com ← 电子邮箱,可以随便填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不填
An optional company name []: ← 可以不填
7.创建一个自当前日期起有效期为两年的客户端证书client.crt:
命令输入:
openssl x509 -req -days 730 -sha1 -extensions v3_req -CA ca.crt -CAkey root.key -CAserial ca.srl -CAcreateserial -in client.csr -out client.crt
输出内容为:
[lenin@archer ~]$ openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in client.csr -out client.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com
Getting CA Private Key
Enter pass phrase for root.key: ← 输入上面创建的密码
8.所需证书生成完毕
四、启动Mosquitto测试
1.配置mosquitto.conf文件
port 8883
cafile C:/OpenSSL-Win64/bin/ca.crt ----> 指向生成的ca文件
certfile C:/OpenSSL-Win64/bin/server.crt
keyfile C:/OpenSSL-Win64/bin/server.key
allow_anonymous true
require_certificate true
use_identity_as_username true
2.启动mosquitto
1.进入cmd模式,进入mosquitto目录下,执行以下命令使用配置文件启动mosquitto
mosquitto -c mosquitto.conf
2.打开新的cmd界面,输入订阅命令:
mosquitto_sub -h 10.8.4.205 -p 8883 -t "x主题x" --cafile C:/OpenSSL-Win64/bin/ca.crt --cert C:/OpenSSL-Win64/bin/client.crt --key C:/OpenSSL-Win64/bin/client.key
3.打开新的cmd界面,输入发布命令:
mosquitto_pub -h 10.8.4.205 -p 8883 -t "x主题x" -m "this is w show" --cafile C:/OpenSSL-Win64/bin/ca.crt --cert C:/OpenSSL-Win64/bin/client.crt --key C:/OpenSSL-Win64/bin/client.key
4.订阅端即可收到发布端发布的信息:this is w show
五、结束
成功生成ssl证书,并且客户端服务端通讯成功
网友评论