最近做MQTT的開(kāi)發(fā)禽作,需要加OpenSSL功能甘萧,記錄下來(lái)方便回顧
1.配置相關(guān)證書
1.1 生成ca證書和key
openssl req -new -x509 -days 36500 -keyout ca.key -out ca.crt
鍵入命令輸出如下:
Generating a RSA private key
.............................................................+++++
.........................................................................+++++
writing new private key to 'ca.key'
Enter PEM pass phrase:【輸入CA私鑰密碼间雀,后面簽名會(huì)用】
Verifying - Enter PEM pass phrase:【再次輸入】
——-----
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]:【國(guó)家代碼掀亥,兩個(gè)字母】
State or Province Name (full name) [Some-State]:【省】
Locality Name (eg, city) []:【市】
Organization Name (eg, company) [Internet Widgits Pty Ltd]:【公司】
Organizational Unit Name (eg, section) []:【組織名撩扒,可以不填】
Common Name (e.g. server FQDN or YOUR name) []:【可以不填辕羽,但是一定不要和sever逛尚、client端證書的這個(gè)字段相同】
Email Address []:【郵件,隨意填寫】
1.2 配置server端的相關(guān)證書
1.2.1 server私鑰文件
openssl genrsa -out server.key 2048
注:moquitto服務(wù)端最好不要設(shè)置密碼會(huì)導(dǎo)致無(wú)法連接刁愿,如果是自行開(kāi)發(fā)的服務(wù)端可以使用密碼:openssl genrsa -des3 -out server.key 2048
1.2.2 server證書請(qǐng)求文件
openssl req -out server.csr -key server.key -new
鍵入指令輸出如下:
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]:【國(guó)家代碼】
State or Province Name (full name) [Some-State]:【省】
Locality Name (eg, city) []:【市】
Organization Name (eg, company) [Internet Widgits Pty Ltd]:test_sdk
Organizational Unit Name (eg, section) []:【組織名绰寞,可不填】
Common Name (e.g. server FQDN or YOUR name) []:【服務(wù)端所在IP或域名】
Email Address []:【郵件,隨意填寫】
——
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:【可不填】
An optional company name []:【可不填】
1.2.3 自簽名server證書
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 36500
鍵入命令輸出如下:
Signature ok
subject=....【這里是上面證書請(qǐng)求文件輸入的內(nèi)容】
Enter pass phrase for ca.key:【這里輸入CA私鑰的密碼】
1.3 配置client端相關(guān)證書
配置步驟和【server】端相同,但是私鑰文件可以使用密碼滤钱,因?yàn)槲覀兛梢栽陂_(kāi)發(fā)時(shí)可以設(shè)置私鑰使用的密碼
2.配置mosquitto
修改mosquitto.conf配置文件
端口:
port 8883
——
ca文件絕對(duì)路徑:
cafile /Users/xxx/Desktop/ssl_1.1.0j/openssl_0315/ca.crt
——
server端證書文件絕對(duì)路徑:
certfile /Users/xxx/Desktop/ssl_1.1.0j/openssl_0315/server.crt
——
server端私鑰文件絕對(duì)路徑:
keyfile /Users/xxx/Desktop/ssl_1.1.0j/openssl_0315/server.key
——
如果是使用【mosquitto_sub/mosquitto_pub】簡(jiǎn)單測(cè)試可以設(shè)置下面兩項(xiàng):
require_certificate true
use_identity_as_username true
——
到此已經(jīng)配置完成了觉壶,啟動(dòng)服務(wù)端、sub件缸、pub就可以了铜靶,如果客戶端私鑰設(shè)置了密碼則啟動(dòng)時(shí)需要輸入密碼
啟動(dòng)sub:mosquitto_sub -h 【ip或域名】 -p 【端口】 -t 【主題】 --cafile 【ca證書文件路徑】 --cert 【client證書文件路徑】 --key 【client私鑰文件路徑】
|
啟動(dòng)pub:mosquitto_pub -h【ip或域名】 -p 【端口】 -t 【主題】 -m 【消息】--cafile 【ca證書文件路徑】 --cert 【client證書文件路徑】 --key 【client私鑰文件路徑】
注意:如果是單向認(rèn)證,則需要把服務(wù)端的【require_certificate 】設(shè)置為‘false’或注釋掉他炊,然后mosquitto的‘sub’争剿、‘pub’客戶端的參數(shù)中去掉【--cert】和【--key】?jī)蓚€(gè)參數(shù)即可,【CA】證書則服務(wù)和客戶端要一致痊末,要設(shè)置都設(shè)置否則都不設(shè)置蚕苇,即【--cafile】參數(shù)根據(jù)設(shè)置決定添加與否
如果是自己實(shí)現(xiàn)的實(shí)現(xiàn)pub、sub功能且需要【用戶名凿叠、密碼】則需要設(shè)置【password_file】文件涩笤,將【require_certificate】、【use_identity_as_username】設(shè)置為【false或者注釋掉】幔嫂。
使用【mosquitto_passwd】生成的【用戶名辆它、密碼】時(shí)則可以在上面啟動(dòng)sub誊薄、pub命令后添加參數(shù) "-u 【用戶名】 -P 【密碼】"履恩,這里的“P”是大寫的,小寫表示端口呢蔫。