mosquitto_pub 拒绝 iot.eclipse.org mqtt 服务器证书说 "Unknown CA"

mosquitto_pub rejecting iot.eclipse.org mqtt server certificate saying "Unknown CA"

我正在尝试 运行 mosquitto_pub 在 Ubuntu linux 机器上,如下所示:

vbhadra@vbhadra-VirtualBox:~$ mosquitto_pub  -h iot.eclipse.org -p 8883 --capath /etc/ssl/certs/ -t house/s1 -m "test message" -d
Client mosqpub/9204-vbhadra-Vi sending CONNECT
Client mosqpub/9204-vbhadra-Vi received CONNACK
Client mosqpub/9204-vbhadra-Vi sending PUBLISH (d0, q0, r0, m1, 'house/s1', ... (12 bytes))
Client mosqpub/9204-vbhadra-Vi sending DISCONNECT

可以看到mosquitto_pub工程文件。现在我想尝试手动下载 iot.eclipse.org 证书,然后将其与 mosquitto_pub 一起使用,而不是使用 Ubuntus /etc/ssl/certs/ 证书。

所以我做了以下操作:

ex +'/BEGIN CERTIFICATE/,/END CERTIFICATE/p' <(echo | openssl s_client -showcerts -connect iot.eclipse.org:8883) -scq > file.crt

将 file.crt 保存在以下位置:/home/vbhadra/remote_certificate/。

现在,我再次尝试以下操作:

mosquitto_pub  -h iot.eclipse.org -p 8883 --capath /home/vbhadra/remote_certificate/ -t house/s1 -m "test message" -d 

但是上面的方法失败得很惨。通过捕获 tcpdump,我可以在 Wireshark 中看到我的 Ubuntu 客户端正在将 "Fatal: Unknown CA" 发送回 iot.eclipse.org。

根据我目前的了解,我可以认为该证书未由任何 CA 签名,因此 mosquitto 客户端拒绝了它。我一直在试图弄清楚如何才能获得证书签名(自签名??),但到目前为止还没有任何线索。

我尝试使用 openssl verify 验证我保存 file.crt 的证书文件,如下所示:

openssl verify -CApath /home/vbhadra/remote_certs/ /home/vbhadra/remote_certs/file2.crt  
/home/vbhadra/remote_certs/file2.crt: CN = iot.eclipse.org
error 20 at 0 depth lookup:unable to get local issuer certificate

此时我有点迷茫。人们似乎建议将 .pem 证书文件与 openssl 验证一起使用,但不确定该怎么做,基本上我迷路了。请帮助任何指针进一步采取它。

当您 运行 echo | openssl s_client -showcerts -connect iot.eclipse.org:8883 命令并查看输出时,它包含多个证书,我认为这可能与您使用 ex.

您将需要这两个证书来提供完整的证书链来验证 iot.eclipse.org 的最终用户证书。

你 运行 ca_rehash(或 ubuntu 上的 c_rehash)是否也在你的 ca 文件目录中?

--capath

Define the path to a directory containing PEM encoded CA certificates that are trusted. Used to enable SSL communication.

For --capath to work correctly, the certificate files must have ".crt" as the file ending and you must run "c_rehash " each time you add/remove a certificate.

我不得不将文件重命名为以 .pem 结尾(因为它们实际上是这样),并将 DST_Root_CA_X3.pem 文件也从 /etc/ssl/certs 复制到 ca 目录中。

此外,如果您想使用自己的私人 CA,那么使用 --cafile 可能会更简单一些。

这些是我按照 hardillb 上面的回答执行的步骤:

vbhadra@vbhadra-VirtualBox:~/remote_certs$ openssl s_client -showcerts -CApath /etc/ssl/certs/ -connect iot.eclipse.org:8883
CONNECTED(00000003)
depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3
verify return:1
depth=1 C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
verify return:1
depth=0 CN = iot.eclipse.org
verify return:1
---
Certificate chain
 0 s:/CN=iot.eclipse.org
   i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
-----BEGIN CERTIFICATE-----
MIIFATCCA+mgAwIBAgISBC9SZX2gUAjJc1w00bxPBlvYMA0GCSqGSIb3DQEBCwUA
MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD
ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMzAeFw0xNzExMDMwOTE1MzFaFw0x
ODAyMDEwOTE1MzFaMBoxGDAWBgNVBAMTD2lvdC5lY2xpcHNlLm9yZzCCASIwDQYJ
KoZIhvcNAQEBBQADggEPADCCAQoCggEBAMIBN2DDEwV17T9JnHRe7OL4Cz4tKxc8
oB+wV+QFmXjObtYTlH9bbT+TidNHhHnnUKtVr/2hOyoEwNpjQsR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2bgdv7psvYRYsvM1m9fEscox0UuQEvOGGts76Hrr1lpVPdUDh
JPduBX+8rtzgs3uA2PdwvoEAQsbb2ulcVBe10wF0W0bUky6ZHutDp/GK9pHiWWTu
RFYkJ74rzuPVNvFJ1jS4ow7gIg3rY6mLpMwcSQo7t2sb54i63PggzepPenqJkXC9
YwWJJs/zovrZjioMXKC/Lp+ROSK8L2Z+JzyQp0Yu/vyebHZ4WL0xlXBPrRvVx5J1
3+ebwxdehisKt78Xo/HgCiwyloCQILimldCLR5LdpgPS4N0Ang==
-----END CERTIFICATE-----
 1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
   i:/O=Digital Signature Trust Co./CN=DST Root CA X3
-----BEGIN CERTIFICATE-----
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow
SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT
GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF
q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8
SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0
Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA
a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj
/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T
AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG
CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv
bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k
c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw
VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC
ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz
MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu
Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF
AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo
uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/
wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu
X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG
PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
-----END CERTIFICATE-----
---
Server certificate
subject=/CN=iot.eclipse.org
issuer=/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
---
No client certificate CA names sent
---
SSL handshake has read 3137 bytes and written 421 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-GCM-SHA384
    Session-ID: 1341B87702E30012B8FB0B98090FAB047CA84AB74566837F5600DD9C8B5BE5F5
    Session-ID-ctx: 
    Master-Key: 8806ABE0992E57E28B628CE5A1AE29642298C37363C530DED1EB7322E023CD297687D0A0C662B7875DA00F4FA4ACE9A9
    Key-Arg   : None
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    TLS session ticket lifetime hint: 300 (seconds)
    TLS session ticket:
    0000 - ca de e0 ec 30 a1 17 f3-a9 e7 eb 0b 57 5c f0 8a   ....0.......W\..
    0010 - 2f ad 59 79 87 11 5c f6-05 39 fa 5c 0c 6b dd 6d   /.Yy..\..9.\.k.m
    0020 - 64 f3 4d 93 e9 1b 30 9a-08 fc e5 87 ca 34 d5 e3   d.M...0......4..
    0030 - 82 4b 60 67 b1 44 e6 4e-31 63 03 ec d8 e3 b4 0d   .K`g.D.N1c......
    0040 - 8d 03 e3 e2 a3 fb 6c ed-1f 0d 25 03 43 a8 b6 ff   ......l...%.C...
    0050 - 01 6a d0 48 a7 10 72 8c-6d aa ff 85 4e e8 b5 91   .j.H..r.m...N...
    0060 - 53 da 88 f2 03 88 88 54-94 f0 d5 e9 bd a0 55 06   S......T......U.
    0070 - 16 04 87 d8 ec 99 e5 cd-fc 9c 46 ff 1f cc 63 9e   ..........F...c.
    0080 - c3 cb 18 72 c9 ec ff fd-82 5a 79 fc 10 90 5a 10   ...r.....Zy...Z.
    0090 - 4d b8 5b 1e 9e 09 93 b6-cc 6d f8 06 10 6e f0 a4   M.[......m...n..
    00a0 - ab 71 c2 a8 58 01 25 43-3a b9 1d 38 52 b2 56 52   .q..X.%C:..8R.VR

    Start Time: 1513006076
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---
closed

请注意,在 BEGIN 和 END CERTIFICATE 部分之间有两个证书。将两个证书复制到两个单独的文件中,就像我将两个证书复制并保存到两个不同的文件中一样,这些文件分别名为 cert.pem 和 cert2.pem,位于我的主目录中名为 remote_certificates 的文件夹中。现在将 DST_Root_CA_X3.pem 文件从 /etc/ssl/certs/ 文件夹复制到 remote_certificates 文件夹。

现在,正如@hardillb 在上述评论中所建议的那样 运行 c_rehash:

vbhadra@vbhadra-VirtualBox:~/remote_certs$ c_rehash .
Doing .
DST_Root_CA_X3.crt => 2e5ac55d.0
DST_Root_CA_X3.crt => 12d55845.0
iot.cert2.pem => 4f06f81d.0
iot.cert2.pem => 4a0a35c0.0
iot.cert1.pem => c6cb63f9.0
iot.cert1.pem => 8a8c4ac3.0

现在,我在 remote_certificates 文件夹中有以下文件:

vbhadra@vbhadra-VirtualBox:~/remote_certs$ ls -la 
total 20
drwxrwxr-x  2 vbhadra vbhadra 4096 Dec 11 15:38 .
drwxr-xr-x 40 vbhadra vbhadra 4096 Dec 11 15:38 ..
lrwxrwxrwx  1 vbhadra vbhadra   18 Dec 11 15:38 12d55845.0 -> DST_Root_CA_X3.crt
lrwxrwxrwx  1 vbhadra vbhadra   18 Dec 11 15:38 2e5ac55d.0 -> DST_Root_CA_X3.crt
lrwxrwxrwx  1 vbhadra vbhadra   13 Dec 11 15:38 4a0a35c0.0 -> iot.cert2.pem
lrwxrwxrwx  1 vbhadra vbhadra   13 Dec 11 15:38 4f06f81d.0 -> iot.cert2.pem
lrwxrwxrwx  1 vbhadra vbhadra   13 Dec 11 15:38 8a8c4ac3.0 -> iot.cert1.pem
lrwxrwxrwx  1 vbhadra vbhadra   13 Dec 11 15:38 c6cb63f9.0 -> iot.cert1.pem
-rw-r--r--  1 vbhadra vbhadra 1200 Dec 11 15:33 DST_Root_CA_X3.crt
-rw-rw-r--  1 vbhadra vbhadra 1798 Dec 11 15:37 iot.cert1.pem
-rw-rw-r--  1 vbhadra vbhadra 1648 Dec 11 15:37 iot.cert2.pem

现在我 运行 mosquitto_pub 命令如下,它起作用了:

vbhadra@vbhadra-VirtualBox:~/remote_certs$ mosquitto_pub  -h iot.eclipse.org -p 8883 --capath /home/vbhadra/remote_certs/ -t house/s1 -m "test message" -d 
Client mosqpub/3728-vbhadra-Vi sending CONNECT
Client mosqpub/3728-vbhadra-Vi received CONNACK
Client mosqpub/3728-vbhadra-Vi sending PUBLISH (d0, q0, r0, m1, 'house/s1', ... (12 bytes))
Client mosqpub/3728-vbhadra-Vi sending DISCONNECT
vbhadra@vbhadra-VirtualBox:~/remote_certs$ 

再次感谢@hardillb。