Pico W TLS connection using umqtt.simple #10559
-
Hi, I just can't seem to crack this as I have been going around in circles for several days, so it's definitely now time to ask for help. I have mosquitto installed on a Linux server and on my Linux workstation (so that I have the CLI tools to remotely test my connection) and a Pico W micropython client. Using mosquitto_pub and mosquitto_sub on my workstation I can connect perfectly using TLS using self signed certificates. My Pico W fails to connect, giving the following error on my mosquitto server. What am I doing wrong please, as it's driving me crazy?
My micropython code is this:
This is my mosquitto.conf just in case:
|
Beta Was this translation helpful? Give feedback.
Replies: 21 comments 15 replies
-
This post may be of help. This may be relevant to setting the "server_hostname" value. This TLS stuff is a black art. |
Beta Was this translation helpful? Give feedback.
-
Oh woe, woe & thrice woe. Alas, but it was that forum post and the Hive MQ page that got me this far. Unfortunately, I fear that calling this TLS stuff a Black Art is a slight understatement. However, I really feel that I am right on the verge of finding the secret sauce that makes the magic recipe work. I will keep you posted... |
Beta Was this translation helpful? Give feedback.
-
ssl_params["cert_reqs"] = "ssl.CERT_REQUIRED" # --> this should be ssl.CERT_REQUIRED without ""
# read in DER formatted certs & user key
with open('/certs/der_user1.key', 'rb') as f:
key_data = f.read()
with open('/certs/der_user1.crt', 'rb') as f:
cert_data = f.read() For client side, you don't need key, cert unless you are doing
Hard to tell tbh, it could be multiple things, e.g. a mismatch of Could you test if this works? tests/net_inet/ssl_cert.py import ntptime
ntptime.settime() Also can you see what's the output in your Pico? PD: Wireshark can help with debugging |
Beta Was this translation helpful? Give feedback.
-
@peterhinch as soon as this is cracked I'll publish everything in the unofficial FAQ, as it unfortunately appears to be a very common problem with almost no documented solutions. @Carglglz oooooh. I haven't done anything involving the time yet. I'll add it & fix the
|
Beta Was this translation helpful? Give feedback.
-
@Carglglz - I think that I have multiple things that are nailing my attempt to connect via TLS. I'm going to rebuild it all from scratch with the most basic mosquitto config possible and brand new certificates. That way I can do my best to reduce potential points of failure, Keep It Simple, & build it up from there. This looks to be my first point of failure: https://mosquitto.org/blog/2014/10/unintended-change-of-behaviour-in-1-3-4/ I found the I am using micropython-firmware-pico-w-290622.uf2 which was the latest official version when I flashed it a week & a half ago, but will update to the current official release & see if it changes anything - before trying the micropython nightly builds, to see if that makes a difference. |
Beta Was this translation helpful? Give feedback.
-
I don't know if this is any help but I looked at the source recently and established that the valid keys for the
I don't know which platforms use mbedtls. |
Beta Was this translation helpful? Give feedback.
-
@peterhinch AFAIK the platforms that use
@JustinS-B Unfortunately that file is outdated as a I mentioned in #9222
👀
import ussl as ssl And use cadata keyword, that should work. |
Beta Was this translation helpful? Give feedback.
-
OK, following everything that @peterhinch and @Carglglz have advised, it is now 100% working. Yes, we have MQTT working over TLS on a Raspberry Pi Pico W. If you give me a day or two, I will pull what I have to pieces & rebuild it - just to make sure that I can do it from scratch & that it is now as simple as it appears to be. Then I'll post it all back here so that we'll have a nice, simple, easy to follow guide that anyone can follow. Or, at least, a guide to the way that I have got it to play nicely with your TLS magic sprinkled onto it. |
Beta Was this translation helpful? Give feedback.
-
My way of getting my Pico W to connect to my mosquitto broker over TLS uses the standard approach of a Self Signed CA, server key/cert, and user key/cert, along with a DH handshake. I haven't managed to work out yet how to only use the server key/cert on its own, without the user key/certs, but it's got to be easy now that the basic ssl_params work. I am in the process of documenting the way I created my different keys, certificates & DH dhparamfile, and as soon as it is clear & easy to follow I will upload it to my Github Gist and link it in here. This is the micropython code for my Pico W that finally got MQTT working over SSL/TLS for me - all down to @peterhinch and @Carglglz as I was completely lost on the ssl_params errors. NOTE: it takes around 7 seconds to do the DH handshake & get the TLS up and running.
this is my mosquitto.conf
and this is the choir singing the hallelujah chorus.
|
Beta Was this translation helpful? Give feedback.
-
@JustinS-B are you using RSA keys maybe?? Now that you have TLS running you may want to test ECDSA keys and see if that speeds up the handshake? (one of the reason is that RSA certs are bigger and therefore more data transfer in the handshake) |
Beta Was this translation helpful? Give feedback.
-
Apologies for the delay in continuing this, but I have spent the past few days discovering more than 20 or 30 ways of stopping MQTT TLS from working, just by trying to be clever. Apparently, it really doesn't let you stray too far from the true path. I'm still pinpointing precisely what it is that stands between a set of lovely keys and certificates that work beautifully & a set that appear to look nearly identical, but which cause everything to collapse in a smoking wreck. Thankfully, because I know that the micropython works, & that my Mosquitto CLI clients work, I can just cook up some new credentials, restart everything & see if/how/when it breaks. When I work out the recipe for the credential creation, I'll post it & identify the secret sauce. |
Beta Was this translation helpful? Give feedback.
-
Interestingly, mbedtls on the Pico W doesn't currently support tlsv1.3. It also has issues with certificates including X509v3 extensions, in that it will authenticate calls to any of the X509v3 Subject Alternative Name DNS entries, but fails to authenticate any calls to X509v3 Subject Alternative Name IP Address entries. The normal mosquitto_pub and mosquitto_sub clients will do either without issue. However, as a workaround, if you also add a DNS entry of your IP Address along with the normal IP Address, mbedtls will authenticate just fine. Giving this when you check the mbedtls workaround certificate:
|
Beta Was this translation helpful? Give feedback.
-
I ran some tests to time the TLS/SSL connection with different key types/strengths using micropython on Pico W to my Raspberry Pi based Mosquitto server. Curve P-256 is another way of referring to the ECDSA-secp256r1 curve
So there's not a lot in it really, although the ECDSA-secp256r1 curve (P-256) is the winner by nearly a second. I didn't include the ED25519 curve because it isn't supported on the Pico W because of mbedtls, and all my tests were done on the Pico W. I only included the 4096 bit RSA and the CDSA-secp521r1 for fun, just to see how long they took. |
Beta Was this translation helpful? Give feedback.
-
@peterhinch and @Carglglz - I have set up a Github Repo for my bash script which automatically creates a self-signed CA, a mosquitto server key and cert and a DH parameters file, along with its associated script that creates client keys and certs. You can set it to create an entirely EC based setup, using either P-256, P-384 or P-521 curves, or an ED25519 based setup, or an RSA setup with either 2048 0r 4096 bit keys. It renames any cert or key dirs that it finds, so it definitely won't delete anything. It should all work well, but naturally, it is very much a work in progress. It really helps when you are testing & timing different curves as it can generate an entire P-256 setup including a handful of client in a couple of seconds, then do it all again for a P-384 based setup instead in another couple of seconds. It takes longer to write the new keys/certs to the Pico W than it does to create them. |
Beta Was this translation helpful? Give feedback.
-
@JustinS-B Nice work! Also you may want to check cryptography.io to be able to do this in python (and to learn and explore other types of cryptography too) |
Beta Was this translation helpful? Give feedback.
-
Hi, I am trying to connect to my MQTT broker from an RPi Pico. The broker does not require a client certificate and key so I am running this code on the Pico:
On the broker I can see the connection attempt, but no error message. On the Pico I get this error:
There must be a way to make this work 🙄 |
Beta Was this translation helpful? Give feedback.
-
I searched a lot but some things are just not clear from the available documentation:
|
Beta Was this translation helpful? Give feedback.
-
Using the |
Beta Was this translation helpful? Give feedback.
-
Need some help!! |
Beta Was this translation helpful? Give feedback.
-
I don't see ssl_params as an argument for the umqtt.simple MQTTClient class. Is there a different version of umqtt.simple floating around out there that actually accepts that argument? Edit: I think the answer is to pass in an SSL context using the ssl argument, not ssl_params. Not sure how ssl_params above is working. |
Beta Was this translation helpful? Give feedback.
-
Found out why ssl_params is missing. It was removed earlier this year. Here's the previous code. AWS has some code that can be used for either version. For uPy 1.22 and below, use mqtt_connect(), and for 1.23+ use mqtt_connect_new(). |
Beta Was this translation helpful? Give feedback.
My way of getting my Pico W to connect to my mosquitto broker over TLS uses the standard approach of a Self Signed CA, server key/cert, and user key/cert, along with a DH handshake. I haven't managed to work out yet how to only use the server key/cert on its own, without the user key/certs, but it's got to be easy now that the basic ssl_params work.
I am in the process of documenting the way I created my different keys, certificates & DH dhparamfile, and as soon as it is clear & easy to follow I will upload it to my Github Gist and link it in here.
This is the micropython code for my Pico W that finally got MQTT working over SSL/TLS for me - all down to @peterhinch and @Carglglz as I was …