Thanks to visit codestin.com
Credit goes to github.com

Skip to content

lib/mbedtls: Update to mbedtls-v3.5.1. #8988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2024
Merged

Conversation

Carglglz
Copy link
Contributor

@Carglglz Carglglz commented Jul 30, 2022

This is a draft for a possible future mbedTLS 3.x migration.
Following 3.0-migration-guide I made the necessary changes to update mbedTLS:

$ git submodule
...
 8c89224991adff88d53cd380f42a2baa36f91454  lib/mbedtls (v3.3.0)
...

Unix port builds:

  • Linux: OK
  • macOS: OK (although needs adding #if...#endif in lib/mbedtls/x509.c:137 to avoid error: unused function 'md_type_to_string'

Tests:

$ ./run-test.py 
....
837 tests performed (24562 individual testcases)
837 tests passed
....

Multi:

$ ./run-multitests.py multi_net/*.py
multi_net/ssl_cert_rsa.py on micropython|micropython: pass
multi_net/ssl_context_rsa.py on micropython|micropython: pass
multi_net/ssl_data.py on micropython|micropython: pass
multi_net/tcp_accept_recv.py on micropython|micropython: pass
multi_net/tcp_client_rst.py on micropython|micropython: pass
multi_net/tcp_data.py on micropython|micropython: pass
multi_net/uasyncio_tcp_client_rst.py on micropython|micropython: pass
multi_net/uasyncio_tcp_close_write.py on micropython|micropython: pass
multi_net/uasyncio_tcp_readall.py on micropython|micropython: pass
multi_net/uasyncio_tcp_readexactly.py on micropython|micropython: pass
multi_net/uasyncio_tcp_readinto.py on micropython|micropython: pass
multi_net/uasyncio_tcp_server_client.py on micropython|micropython: pass
multi_net/udp_data.py on micropython|micropython: pass
13 tests performed
13 tests passed

For reference esp-idf v.5 will use mbedTLS 3.x too mbed-tls-support-in-esp-idf.

TLDR:

Big changes affect configuration options in mbedtls_config.h, some functions renamed in extmod/moduhashlib.c and
error codes deprecated/ renamed in lib/mbedtls_errors/mp_mbedtls_errors.c.
Other than that, shouldn't be too difficult to update 👍🏼 .

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Aug 11, 2022
@dpgeorge
Copy link
Member

Thanks for this. It's a good change but let's wait until after the next release (after v1.20).

@Carglglz
Copy link
Contributor Author

Carglglz commented Aug 11, 2022

Thanks for this. It's a good change but let's wait until after the next release (after v1.20).

Yes, it's just something I bumped into while doing #8968 and I figured out it would be nice to at least have some reference when the time comes.

@Carglglz Carglglz force-pushed the mbedtls3 branch 2 times, most recently from 93af87e to 83cca00 Compare August 20, 2022 13:28
@Carglglz Carglglz changed the title lib/mbedtls: Migrate to mbedTLS 3.2.1. lib/mbedtls: Migrate to mbedtls-v3.3.0. Jun 30, 2023
@github-actions
Copy link

github-actions bot commented Jun 30, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64: +26224 +3.245% standard[incl +6744(data) -96(bss)]
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:  -232 -0.070% RPI_PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@Carglglz Carglglz force-pushed the mbedtls3 branch 3 times, most recently from 5d73ef6 to 1df37f8 Compare July 1, 2023 16:50
@codecov
Copy link

codecov bot commented Jul 1, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d5b9681) 98.36% compared to head (f3d1495) 98.36%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8988   +/-   ##
=======================================
  Coverage   98.36%   98.36%           
=======================================
  Files         159      159           
  Lines       21088    21088           
=======================================
  Hits        20743    20743           
  Misses        345      345           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Carglglz Carglglz force-pushed the mbedtls3 branch 2 times, most recently from 61df480 to 3167e03 Compare July 1, 2023 17:53
@Carglglz Carglglz marked this pull request as ready for review July 1, 2023 19:38
@Carglglz Carglglz changed the title lib/mbedtls: Migrate to mbedtls-v3.3.0. lib/mbedtls: Update to mbedtls-v3.3.0. Jul 2, 2023
@Carglglz Carglglz force-pushed the mbedtls3 branch 2 times, most recently from da16a68 to a68a280 Compare July 3, 2023 13:37
@Carglglz
Copy link
Contributor Author

Carglglz commented Jul 17, 2023

UPDATE:

  • I had to change mbedtls_config.h to mbedtls_mpconfig.h to avoid name conflicts with mbedtls_config.h file from lib/mbedtls (see the configuration file).
  • I had to delete the tests/multi_net/ssl_data.py test since the option that made it possible i.e. # MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE is deprecated.

@Carglglz Carglglz force-pushed the mbedtls3 branch 2 times, most recently from 2e34fb8 to 32eb485 Compare December 13, 2023 05:54
@Carglglz Carglglz force-pushed the mbedtls3 branch 2 times, most recently from e46e3b8 to 32fb72b Compare January 16, 2024 23:50
@Carglglz
Copy link
Contributor Author

Thanks for this. It's a good change but let's wait until after the next release (after v1.20).

@dpgeorge I think this is ready to be reviewed, to sum up the changes:

If there is anything else I can do let me know 👍🏼

@dpgeorge
Copy link
Member

mbedtls_config.h renamed to mbedtls_mpconfig.h see the-configuration-file (there is a MBEDTLS_CONFIG_FILE macro but I couldn't make it work, in case you want to test it)

Hmm, that's annoying that it's renamed to exactly what we name our config file!

I suggest calling ours mbedtls_config_port.h. That matches the naming style of mbedtls_config_common.h and mbedtls_config_board.h that are already used in the MicroPython repo.

@dpgeorge
Copy link
Member

MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE is deprecated (so I had to delete this tests/multi_net/ssl_data.py test

Does that test really need to be deleted, or is it possible to just update the key/cert data to something else? The aim of that test is to do a very simple ssl.wrap_socket() call and it doesn't really matter what certificates are used.

Well, I guess there is the existing ssl_cert_rsa.py and sslcontext_server_client.py tests which do very similar, simple SSL data transfer. So maybe those two are enough and indeed ssl_data.py can be removed.

@Carglglz
Copy link
Contributor Author

Well, I guess there is the existing ssl_cert_rsa.py and sslcontext_server_client.py tests which do very similar, simple SSL data transfer. So maybe those two are enough and indeed ssl_data.py can be removed.

Yes that was my conclusion too. The key/cert there was only 512 bits and there is no clue about how it was generated...

@Carglglz
Copy link
Contributor Author

Also I don't have any device available to test these changes so it would be better if you or anyone could test this on mbedtls ports before merging 👍🏼

Running `./do-mp.sh` now generates this `mp_mbedtls_errors.c` file.  The
`esp32_mbedtls_errors.c` file is already up-to-date.

Signed-off-by: Carlos Gil <[email protected]>
Changes include:

- Some mbedtls source files renamed or deprecated.

- Our `mbedtls_config.h` files are renamed to `mbedtls_config_port.h`, so
  they don't clash with mbedtls's new default configuration file named
  `mbedtls_config.h`.

- MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE is deprecated.

- MBEDTLS_HAVE_TIME now requires an `mbedtls_ms_time` function to be
  defined but it's only used for TLSv1.3 (currently not enabled in
  MicroPython so there is a lazy implementation, i.e. seconds * 1000).

- `tests/multi_net/ssl_data.py` is removed (due to deprecation of
  MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE), there are the existing
  `ssl_cert_rsa.py` and `sslcontext_server_client.py` tests which do very
  similar, simple SSL data transfer.

- Tests now use an EC key by default (they are smaller and faster), and the
  RSA key has been regenerated due to the old PKCS encoding used by openssl
  rsa command, see
  https://stackoverflow.com/questions/40822328/openssl-rsa-key-pem-and-der-conversion-does-not-match
  (and `tests/README.md` has been updated accordingly).

Signed-off-by: Carlos Gil <[email protected]>
@dpgeorge
Copy link
Member

I have tested this on unix, esp32, rp2 (Pico W) and stm32 (PYBD-SF6) and all SSL-related tests pass. The tests I ran were from tests/extmod, tests/net_* and tests/multi_net. I also tested a requests get from a HTTPS site. Everything works!

I have also split this PR up into 3 commits: update mbedtls, update mbedtls_errors, and then the rest of the changes.

@dpgeorge dpgeorge merged commit f3d1495 into micropython:master Jan 30, 2024
@dpgeorge
Copy link
Member

Now merged!

Thanks @Carglglz for sticking with this for over a year and a half!

@Carglglz
Copy link
Contributor Author

Carglglz commented Jan 30, 2024

Thanks @Carglglz for sticking with this for over a year and a half!

Happy to see MicroPython up to date with MbedTLS now! 🎉

Also I think #11355 can be closed now 👍🏼

[EDIT]
It looks like mbedtls 3.5.2 was released 4 days ago ( a bug fix release) but now it can be updated with a simple tag checkout 👌🏼

@Carglglz Carglglz deleted the mbedtls3 branch January 30, 2024 02:36
tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Mar 5, 2024
Incorrect error handling in send/recv would raise an OSError with
an incorrect (negative) code.

It's likely that this bug was always happening in the Pico W
implementation, which became the basis of the current shared
implementation.

Push handling of WANT_{READ,WRITE} down into mbedtls_raise_error
and use it in recv_into and send.

Tested by connecting to google.com:443, sending nothing, and trying
to read a byte:

```py
import socketpool, ssl, time, wifi
socket = socketpool.SocketPool(wifi.radio)
ctx = ssl.SSLContext()
with ctx.wrap_socket(socket.socket()) as ss:
    ss.connect(("google.com", 443))
    ss.settimeout(1)
    b = bytearray(1)
    try:
        t0 = time.monotonic()
        ss.recv_into(b)
    except Exception as ee:
        t1 = time.monotonic()
        exc = ee
        print(t1-t0)
        raise exc
```

As desired, an exception `OSError: [Errno 116] ETIMEDOUT` occurred
and the time delta value was 1.0 seconds.

(tested on pycamera)

Closes: micropython#8988
@orangepizza
Copy link

orangepizza commented Apr 12, 2024

how long it will land on a release branch? it looks like it didn't landed on 1.23.0-preview, so I guess at least 1.24?
in openwrt, micropython is patched to external tls lib, which trying to update

@robert-hh
Copy link
Contributor

This PR is merged. So it is present in the v1.23 preview versions.

@orangepizza
Copy link

this PR moved mbedtls_config.h to mbedtls_config_port.h, but that didn't happen in 1.23.0-preview tag and still have havege_c which not valid in mbedtls 3.X
https://github.com/micropython/micropython/blob/v1.23.0-preview/ports/unix/mbedtls/mbedtls_config.h

@robert-hh
Copy link
Contributor

I see in the actual preview e.g the file stm32/mbedtls/mbedtls_config_port.h. At which port & version do you look?

@orangepizza
Copy link

I only see that on master branch but that path still has old config.h in 1.23 preview tag: not but it isn't a branch so maybe it will branch at later point?

@robert-hh
Copy link
Contributor

Please show the full path name of the file you are concerned about.

@orangepizza
Copy link

the link I saw you is already full path? ports/unix/mbedtls/mbedtls_config.h

@robert-hh
Copy link
Contributor

robert-hh commented Apr 12, 2024

The actual state of the repository's master branch shows:
https://github.com/micropython/micropython/blob/master/ports/unix/mbedtls/mbedtls_config_port.h
So maybe you have to update your fork and/or clone of the repository.
Edit: Or just use the master branch.

@orangepizza
Copy link

ha x.preview tag is just after x-1 release, well before actual version x branches, never mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants