2021年7月

“欲速则不达,见小利则大事不成。”

Certificate error while pushing no… | Apple Developer Forums
We did a temporary fix by setting lower SSL security level.

In particular, the openSSL configuration file /etc/ssl/openssl.cnf shall be modified in the following way.
At the beginning, add openssl_conf = default_conf
At the end, add
Code Block 
[ default_conf ]
ssl_conf = ssl_sect
[ssl_sect]
system_default = ssl_default_sect
[ssl_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT:@SECLEVEL=0

After this modification, the certificate is recognized without security errors.

Referenced from: itectec.com/ubuntu/ubuntu-certificate-error-after-upgrade-to-20-04

使用mosquitto订阅消息的时候,出现host name verification failed错误。

mosquitto_sub -L mqtts://124.71.233.xx:8883/ -t topic --cert cert.pem --key key.pem --cafile ../trusted-certs.pem -q 1 -d 

Client (null) sending CONNECT
Error: host name verification failed.
OpenSSL Error[0]: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
Error: A TLS error occurred.

使用openssl s_client验证tls本身没有问题,问题应该出在mqtt的处理上面。

查看mosquitto 的源码。
在mosquitto-2.0.11/lib/tls_mosq.c中,看到如下实现。

if(mosq->tls_insecure == false
#ifndef WITH_BROKER
                        && mosq->port != 0 /* no hostname checking for unix sockets */
#endif
)
...
return preverify_ok;
}

直接设置mosq->tls_insecure为true就不进行这个判断了。
修改后如下:

mosquitto_sub -L mqtts://124.71.233.xx:8883/ -t topic --cert cert.pem --key key.pem --cafile ../trusted-certs.pem -q 1 -d --insecure

使用openssl为ssl证书增加“使用者备用名称(DNS)
主要修改在openssl.cnf

确保req下存在以下2行(默认第一行是有的,第2行被注释了)

[ req ]
distinguished_name = req_distinguished_name
req_extensions = v3_req

确保req_distinguished_name下没有 0.xxx 的标签,有的话把0.xxx的0. 去掉

[ req_distinguished_name ]
countryName              = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName             = State or Province Name (full name)
stateOrProvinceName_default = ShangHai
localityName              = Locality Name (eg, city)
localityName_default = ShangHai
organizationalUnitName             = Organizational Unit Name (eg, section)
organizationalUnitName_default = Domain Control Validated
commonName         = Internet Widgits Ltd
commonName_max = 64

新增最后一行内容 subjectAltName = @alt_names(前2行默认存在)

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

新增 alt_names,注意括号前后的空格,DNS.x 的数量可以自己加

[ alt_names ]
DNS.1 = abc.example.com
DNS.2 = dfe.example.org
DNS.3 = ex.abcexpale.net

Referenced from:https://blog.51cto.com/colinzhouyj/1566438