-->

mosquitto_pub rejecting iot.eclipse.org mqtt serve

2019-03-04 11:38发布

问题:

I am trying to run mosquitto_pub on a Ubuntu linux machine as follows:

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

As can be seen the mosquitto_pub works file. Now I want to experiment downloading the iot.eclipse.org certificate manually and then use that with mosquitto_pub instead of using Ubuntus /etc/ssl/certs/ certificate.

So I did the below:

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

Saved the file.crt in a location as: /home/vbhadra/remote_certificate/.

Now, I try the below again:

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

But the above fails miserably. By capturing the tcpdump I can see in the Wireshark that my Ubuntu client is sending "Fatal: Unknown CA" back to the iot.eclipse.org.

From my little understanding so far, I can think the certificate is not signed by any CA and hence the mosquitto client is rejecting it. I have been trying to figure out how I can I get the certificate signed (self sign ??) but no clue so far.

I tried verifying the certificate file I saved file.crt with openssl verify as below:

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

At this point I am bit lost. People seems to be suggesting to use .pem certificate file with openssl verify but not sure how to do that, basically I am lost. Please help with any pointer to take it further.

回答1:

When you run the echo | openssl s_client -showcerts -connect iot.eclipse.org:8883 command and look at the output, it contains multiple certificates which I believe may be an issue with the way you are chopping it up with ex.

You will need both of these certs to provide a full certificate chain to verify the end user certificate for iot.eclipse.org.

Also did you run ca_rehash (or c_rehash on ubuntu) in the directory with your ca files?

--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.

I had to rename the files to end with .pem (since this is what they actually are) and copy the DST_Root_CA_X3.pem file into the ca directory from /etc/ssl/certs as well.

Also if you want to use your own private CA then using --cafile might be a bit simpler.



回答2:

These are the steps I did following hardillb's answer above:

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/2hOyoEwNpjQsRmToesIWARfzyU
1RlGLKT4bWcheVzda5IoYqEDTQqbh5hpqeXqdqkW1X7NzqSHo8hfPRL3N8m8zaMA
tkeMzQ8nmedUBBPtK1Py2zadun1je4b53AwCBUYDrOo1vAm3Bg/Jz66AhNJ7zf7V
rHyBF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pMwcSQo7t2sb54i63PggzepPenqJkXC9
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

Notice there are two certificates between the BEGIN and END CERTIFICATE sections. Copy both the certificates in two separate files, like I did copy and saved two certificates in two different files called cert.pem and cert2.pem located in a folder called remote_certificates in my home directory. Now copy the DST_Root_CA_X3.pem file from the /etc/ssl/certs/ folder to the remote_certificates folder.

Now, as was suggested by @hardillb in the above comments run 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

Now, I have the below files in remote_certificates folder:

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

Now I run the mosquitto_pub command as below and it works:

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$ 

Thanks a lot again @hardillb.