两张示意图(link:https://superuser.com/questions/620121/what-is-the-difference-between-a-certificate-and-a-key-with-respect-to-ssl" rel="nofollow):
标准的 TLS 认证示意图:standard (1-way) TLS auth:

TLS 加密通讯的示意图:

过程,参考 Mosquitto SSL Configuration -MQTT TLS Security(link:http://www.steves-internet-guide.com/mosquitto-tls/)
创建密码保护的私钥 openssl genrsa -des3 -out ca.key 2048
➜ tls openssl genrsa -des3 -out ca.key 2048
Generating RSA private key, 2048 bit long modulus
.....+++
....................................+++
e is 65537 (0x10001)
Enter pass phrase for ca.key:
Verifying - Enter pass phrase for ca.key:
➜ tls ls -lh
total 8
-rw-r--r-- 1 bingoobjca staff 1.7K 7 15 14:10 ca.key
使用私钥创建根证书 openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/C=CN/ST=BEIJING/L=BEIJING/O=BJCA/OU=SA/CN=bjca.cn"
➜ tls openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/C=CN/ST=BEIJING/L=BEIJING/O=BJCA/OU=SA/CN=bjca.cn"
Enter pass phrase for ca.key:
➜ tls ls -lh
total 16
-rw-r--r-- 1 bingoobjca staff 1.2K 7 15 14:16 ca.crt
-rw-r--r-- 1 bingoobjca staff 1.7K 7 15 14:10 ca.key
生成服务端私钥 openssl genrsa -out server.key 2048
➜ tls openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
..............................+++
..................................................................................................................................+++
e is 65537 (0x10001)
➜ tls ls -lh
total 24
-rw-r--r-- 1 bingoobjca staff 1.2K 7 15 14:16 ca.crt
-rw-r--r-- 1 bingoobjca staff 1.7K 7 15 14:10 ca.key
-rw-r--r-- 1 bingoobjca staff 1.6K 7 15 14:18 server.key
生成服务端证书请求 openssl req -new -out server.csr -key server.key -subj "/C=CN/ST=BEIJING/L=BEIJING/O=BJCA/OU=SA/CN=bjca.cn"
注意:这里的 CN(Common Name)很重要,一般是域名。生成的证书请求,不需要真发送给证书机构,用作测试或者私有证书。
➜ tls openssl req -new -out server.csr -key server.key -subj "/C=CN/ST=BEIJING/L=BEIJING/O=BJCA/OU=SA/CN=bjca.cn"
➜ tls ls -lh
total 32
-rw-r--r-- 1 bingoobjca staff 1.2K 7 15 14:16 ca.crt
-rw-r--r-- 1 bingoobjca staff 1.7K 7 15 14:10 ca.key
-rw-r--r-- 1 bingoobjca staff 993B 7 15 14:21 server.csr
-rw-r--r-- 1 bingoobjca staff 1.6K 7 15 14:18 server.key
生成私有的服务端证书 openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650
➜ tls openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650
Signature ok
subject=/C=CN/ST=BEIJING/L=BEIJING/O=BJCA/OU=SA/CN=bjca.cn
Getting CA Private Key
Enter pass phrase for ca.key:
➜ tls ls -lh
total 48
-rw-r--r-- 1 bingoobjca staff 1.2K 7 15 14:16 ca.crt
-rw-r--r-- 1 bingoobjca staff 1.7K 7 15 14:10 ca.key
-rw-r--r-- 1 bingoobjca staff 17B 7 15 14:23 ca.srl
-rw-r--r-- 1 bingoobjca staff 1.2K 7 15 14:23 server.crt
-rw-r--r-- 1 bingoobjca staff 993B 7 15 14:21 server.csr
-rw-r--r-- 1 bingoobjca staff 1.6K 7 15 14:18 server.key
服务端需要: ca.crt, server,crt, server.key 三个文件
查看证书状态 openssl x509 -in ./server.crt -noout -text

