[TOC]
参考自EMQ X MQTT 服务器启用 SSL/TLS 安全连接 - 简书 (jianshu.com)
原文为emqx-server-ssl-tls-secure-connection-configuration-guide,目前打开已404。应该是改成了EMQX MQTT 服务器启用 SSL/TLS 安全连接 | EMQ
阅读本文请注意
- 
所有以
$开头的命令均为命令行中的输入,只需要复制$后面的内容即可,无需输入$。 - 
生成证书的步骤可在任意计算机上执行,即可在服务器之外(例如个人计算机)上生成,再将证书拷贝到服务器上。
 - 
最好新建一个文件夹,在这个文件夹下执行命令,例如
1 2$ mkdir ~/new_crt $ cd ~/new_crt 
主要步骤
- 生成ca私钥
 - 生成ca证书
 - 生成服务端私钥
 - 配置
openssl.cnf - 生成服务端证书请求
 - 使用ca证书签发服务端证书
 
1. 生成ca私钥
 | 
 | 
2. 生成ca证书
 | 
 | 
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]:
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) []:
Email Address []:
以上所有内容均直接回车,没有输入任何内容。
3. 生成服务端私钥
 | 
 | 
4. 配置或者新建openssl.cnf
最好在当前文件夹新建一个openssl.cnf文件
 | 
 | 
注意:[req]部分中的req_distinguished_name与后面的req_distinguished_name的名称要对应起来,例如
 | 
 | 
其他部分如req_ext、[v3_req]、alt_names同样需要一一对应。
注意:alt_names中的IP.1 = BROKER_ADDRESS要改成IP.1 = x.x.x.x,即改成服务端的ip。DNS部分可以省略。
5. 生成服务端证书请求
 | 
 | 
6. 使用ca证书签发服务端证书
 | 
 | 
服务端使用的是服务端证书和服务端私钥
需要在服务端上传两个文件
| 文件名 | 说明 | 
|---|---|
| emqx.pem | 服务端证书 | 
| emqx.key | 服务端私钥 | 
**注意:**服务端私钥一定要保护好不能泄露!
客户端使用的是ca证书
| 文件名 | 说明 | 
|---|---|
| my_root_ca.pem | ca证书 |