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

Skip to content

extmod/modssl_mbedtls: Add SSLContext methods. #13098

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
Dec 12, 2023

Conversation

Carglglz
Copy link
Contributor

@Carglglz Carglglz commented Nov 30, 2023

This commit adds:

  1. Methods to SSLContext class that match CPython signature:

    • SSLContext.load_cert_chain(certfile, keyfile)
    • SSLContext.load_verify_locations(cafile=, cadata=)
    • SSLContext.get_ciphers() --> ["CIPHERSUITE"]
    • SSLContext.set_ciphers(["CIPHERSUITE"])
  2. sslsocket.cipher() to get current ciphersuite and protocol
    version.

  3. ssl.MBEDTLS_VERSION string constant

  4. Certificate verification errors info instead of MBEDTLS_ERR_X509_CERT_VERIFY_FAILED

  5. Tests in net_inet and multi_net

SSLContext.load_cert_chain method allows loading key and cert from disk passing a filepath in certfile or keyfile options.

SSLContext.load_verify_locations's cafile option enables the same functionality for ca files.

This may close #10832 , #9071 and #8915

Copy link

github-actions bot commented Nov 30, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64: +5376 +0.670% standard[incl +640(data)]
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Copy link

codecov bot commented Nov 30, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (4365edb) 98.36% compared to head (c9eb6bc) 98.39%.

Files Patch % Lines
extmod/modssl_mbedtls.c 98.64% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #13098      +/-   ##
==========================================
+ Coverage   98.36%   98.39%   +0.02%     
==========================================
  Files         159      159              
  Lines       20989    21063      +74     
==========================================
+ Hits        20646    20724      +78     
+ Misses        343      339       -4     

β˜” View full report in Codecov by Sentry.
πŸ“’ Have feedback on the report? Share it here.

@Carglglz
Copy link
Contributor Author

Carglglz commented Nov 30, 2023

TODO:

@dpgeorge dpgeorge added this to the release-1.22.0 milestone Dec 1, 2023
@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Dec 1, 2023
@Carglglz Carglglz force-pushed the SSLContext branch 6 times, most recently from a692340 to df1612d Compare December 5, 2023 18:02
@Carglglz
Copy link
Contributor Author

Carglglz commented Dec 5, 2023

#11897 (comment)

@dpgeorge checking mbedtls programs, they use 512, so this may be a sensible default?,
otherwise if the buffer is too small the error info may be truncated?

mbedtls$ rg vrfy_buf
programs/x509/cert_app.c
331:                char vrfy_buf[512];
335:                mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "  ! ", flags);
337:                mbedtls_printf("%s\n", vrfy_buf);

programs/ssl/ssl_mail_client.c
195:        char vrfy_buf[512];
201:        mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "  ! ", flags);
203:        mbedtls_printf("%s\n", vrfy_buf);

programs/ssl/ssl_client1.c
207:        char vrfy_buf[512];
213:        mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "  ! ", flags);
215:        mbedtls_printf("%s\n", vrfy_buf);

programs/ssl/dtls_client.c
236:        char vrfy_buf[512];
242:        mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "  ! ", flags);
244:        mbedtls_printf("%s\n", vrfy_buf);

programs/ssl/ssl_server2.c
3431:            char vrfy_buf[512];
3434:            x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "  ! ", flags);
3436:            mbedtls_printf("%s\n", vrfy_buf);
3489:        char vrfy_buf[512];
3493:        x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "  ! ", flags);
3494:        mbedtls_printf("%s\n", vrfy_buf);

programs/ssl/ssl_client2.c
2347:        char vrfy_buf[512];
2350:        x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf),
2353:        mbedtls_printf("%s\n", vrfy_buf);

Other than this I will consider this PR done so let me know what you think πŸ‘πŸΌ

@Carglglz Carglglz force-pushed the SSLContext branch 4 times, most recently from 349ec17 to 4c9c285 Compare December 7, 2023 16:52
@dpgeorge
Copy link
Member

dpgeorge commented Dec 8, 2023

Regarding the load_cert_chain() signature: I really think we should keep this simple and just support positional arguments. Reasoning:

  • smaller code
  • still CPython compatible, CPython allows passing in both arguments as positional
  • the CPython docs and all code in the CPython stdlib either do load_cert_chain(cert, key) or load_cert_chain(certfile=cert, keyfile=key), ie they either pass both as keyword args or both as positional args, and never one positional one keyword

@dpgeorge
Copy link
Member

dpgeorge commented Dec 8, 2023

checking mbedtls programs, they use 512, so this may be a sensible default?,
otherwise if the buffer is too small the error info may be truncated?

512 bytes is a lot of stack space to allocate. It's not critical for the error message to fit, even if it's truncated at least it gives a good indication of the problem.

I suggest using 256. That's a good balance of stack usage and available space for the error.

Otherwise, it could be implemented using dynamically allocated memory.

@Carglglz Carglglz force-pushed the SSLContext branch 2 times, most recently from 10b8c29 to 87233ce Compare December 10, 2023 18:22
Carglglz and others added 2 commits December 12, 2023 16:25
This commit adds:

1) Methods to SSLContext class that match CPython signature:

	- `SSLContext.load_cert_chain(certfile, keyfile)`
	- `SSLContext.load_verify_locations(cafile=, cadata=)`
	- `SSLContext.get_ciphers()` --> ["CIPHERSUITE"]
	- `SSLContext.set_ciphers(["CIPHERSUITE"])`

2) `sslsocket.cipher()` to get current ciphersuite and protocol
   version.

3) `ssl.MBEDTLS_VERSION` string constant.

4) Certificate verification errors info instead of
   `MBEDTLS_ERR_X509_CERT_VERIFY_FAILED`.

5) Tests in `net_inet` and `multi_net` to test these new methods.

`SSLContext.load_cert_chain` method allows loading key and cert from disk
passing a filepath in `certfile` or `keyfile` options.

`SSLContext.load_verify_locations`'s `cafile` option enables the same
functionality for ca files.

Signed-off-by: Carlos Gil <[email protected]>
Running `./do-esp32.sh` now generates this esp32_mbedtls_errors.c file,
with IDF v5.0.4.

Signed-off-by: Damien George <[email protected]>
To match other ports that use mbedtls.

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

This looks good now. I have adjusted some of the tests so they pass on bare-metal targets (tested stm32 and esp32).

Thanks @Carglglz for all of your hard work on this, and replying promptly to all of the many code reviews!

@dpgeorge dpgeorge merged commit c9eb6bc into micropython:master Dec 12, 2023
@Carglglz Carglglz deleted the SSLContext branch December 12, 2023 16:53
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.

SSL certificate verify failure lacks specifics
2 participants