SECURITY: Requests module HTTPS - no server certificate verification.
While looking at the MicroPython Requests module (on the git HEAD), I noticed this nightmare:
context = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
context.verify_mode = tls.CERT_NONE
s = context.wrap_socket(s, server_hostname=host)
Assuming that it has the same meaning in MicroPython as cPython (I haven't checked), that line in the middle totally disables TLS security. The attacker pretending to be the server can send any certificate they like, and the client will blindly accept it.
If people are using HTTPS as "the new HTTP", and are happy with the HTTP you-get-no-security model, that's fine. But anyone relying on HTTPS for security, and expecting the normal level of security you'd get from HTTPS, is going to be in trouble.
At a minimum this should be documented clearly on the MicroPython requests documentation ... which doesn't seem to exist anywhere?
Ideally, MicroPython should default to a proper secure HTTPS implementation, including certificate verification, and have a way to opt-out.
python-stdlib/ssl: Add SSLContext.
Follow-up to:
- https://github.com/micropython/micropython/pull/11888
The problem is, that a
chain.deris required.It must be loaded before the check could be done.
With
ctx.load_verify_locations(cafile="chain.der")I'm able to load the cafile and the check works.The
chain.derhas a size of ~1 kB. I guess this is why this option is set toNone.Flash is limited on microcontrollers.
But it should be documented, that the default behavior is unsafe and the why.
EDIT: Tested on a RP Pico W and it works with letsencrypt.
Yes, you need root certificates. Some embedded platforms have root certificates built-in as part of the standard manufacturer-supplied drivers. (Example for ESP32). Ideally, MicroPython should use those, if available. In cPython, that's done with the SSLContext.load_default_certs() method.
I used this command to convert the existing ca-bundle on my system into
certs.der:It's on an Arch Linux and the resulting
certs.deris 2007 Bytes big. I expected much more.But if Micropython should include this, it must also get updates of the root certificates.
@jonfoster you may be interested in #633, see the README
March update made request/urequest module in invalid default, because mbdetls 3.X tls 1.3 forces cert_verify required; loading entire os cacert files or disable tls 1.3 in default ssl context looks like only option
Even if you load
ctx.load_verify_locations(cafile="chain.der")as above, the requests module overwrites the context on line 43. I was able to work around this by modifyingrequests/__init__.pyas follows. Added ansslparameter, and a check to see if it's already set before wrapping the socket. If no parameter is passed, it sets the context withCERT_NONEas before. I plan to submit a pull request later.To use:
Another solution for esp32 is to use the cert bundle and disable ability to disable the verification check. No need to change existing .py modules.
@mzakharocsc , what is the impact on firmware size of that change ?
Even if the impact is significant if may be useful to enable it based on a condition
MICROPY_MBEDTLS_CONFIG_CRT_BUNDLE?and create boards VARIANT that enable that.