Fix RSA-PSS signature verification mismatch in UpdaterRSAVerifier#12503
Conversation
…rify() Agent-Logs-Url: https://github.com/espressif/arduino-esp32/sessions/7d6a1ec3-a67e-4155-92b9-f0b8a7ed1f21 Co-authored-by: lucasssvaz <32426024+lucasssvaz@users.noreply.github.com>
|
|
Agent-Logs-Url: https://github.com/espressif/arduino-esp32/sessions/7d6a1ec3-a67e-4155-92b9-f0b8a7ed1f21 Co-authored-by: lucasssvaz <32426024+lucasssvaz@users.noreply.github.com>
|
|
👋 Hello Copilot, we appreciate your contribution to this project! 📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more. 🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project. Click to see more instructions ...
Review and merge process you can expect ...
|
There was a problem hiding this comment.
Pull request overview
Fixes OTA RSA signature verification in the Update library by aligning the verifier with the RSA-PSS scheme used by bin_signing.py, resolving consistent verification failures caused by a PKCS#1 v1.5 vs PSS mismatch.
Changes:
- Switch RSA verification from
mbedtls_pk_verify()(PKCS#1 v1.5) tombedtls_pk_verify_ext()configured forMBEDTLS_PK_RSASSA_PSS. - Derive and apply the PSS expected salt length (
key_len - hash_size - 2) to match Python’sPSS.MAX_LENGTH.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…d maxSigSize Agent-Logs-Url: https://github.com/espressif/arduino-esp32/sessions/46ab3ed6-6120-4141-8682-a99dd740c27f Co-authored-by: lucasssvaz <32426024+lucasssvaz@users.noreply.github.com>
…ag() in ECDSA verifier (#12504) * Initial plan * fix(update): Replace manual ASN.1 DER parsing with mbedtls_asn1_get_tag() in ECDSA verifier Agent-Logs-Url: https://github.com/espressif/arduino-esp32/sessions/85a80838-3bb0-45b2-a613-b6583ce036a6 Co-authored-by: lucasssvaz <32426024+lucasssvaz@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lucasssvaz <32426024+lucasssvaz@users.noreply.github.com>
Memory usage test (comparing PR against master branch)The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.
Click to expand the detailed deltas report [usage change in BYTES]
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Test Results101 files 101 suites 36m 9s ⏱️ For more details on these failures, see this check. Results for commit 41ed233. ♻️ This comment has been updated with latest results. |
|
Tested locally. Everything works fine. CI failure is unrelated. |
maxSigSizeissue - confirmed it causes verification failure for all key sizes except RSA-4096UpdaterRSAVerifier::verify()to usekey_len(frommbedtls_rsa_get_len()) as signature length instead of the padded 512 bytesUpdaterECDSAVerifier::verify()to compute actual DER signature length from the ASN.1 header instead of using the padded 512 bytesOriginal prompt
Problem
Issue: #12427
There is a signing/verification algorithm mismatch that causes RSA signature verification to always fail with error
-0x4380(MBEDTLS_ERR_RSA_VERIFY_FAILED) when using binaries signed bybin_signing.py.Root Cause
tools/bin_signing.pysigns using RSA-PSS padding withsalt_length=padding.PSS.MAX_LENGTH(line 138-140):libraries/Update/src/Updater_Signing.cppverifies usingmbedtls_pk_verify()(line 66), which performs PKCS#1 v1.5 verification — a completely different signature scheme:PSS signatures and PKCS#1 v1.5 signatures are not interchangeable. A PSS-signed binary will never pass PKCS#1 v1.5 verification.
Required Fix
Update
UpdaterRSAVerifier::verify()inlibraries/Update/src/Updater_Signing.cppto usembedtls_pk_verify_ext()withMBEDTLS_PK_RSASSA_PSSandmbedtls_pk_rsassa_pss_optionsto properly verify RSA-PSS signatures.The key change in
UpdaterRSAVerifier::verify()method (around line 66):mbedtls_pk_rsa().mbedtls_rsa_get_len().expected_salt_len = key_len - hash_size - 2(this matches Python'sPSS.MAX_LENGTHbehavior).mbedtls_pk_rsassa_pss_optionswith the correctmd_typeandexpected_salt_len.mbedtls_pk_verify_ext(MBEDTLS_PK_RSASSA_PSS, &pss_opts, ctx, md_type, hashBytes, hash_size, signature, signatureLen)instead ofmbedtls_pk_verify().Here is what the updated
verifymethod for RSA should look like:Also update the
verify_signaturefunction intools/bin_signing.pyto use the same PSS parameters for consistency — currently it already usesPSS.MAX_LENGTHon line 210 for verification, so it should already be consistent.Important: The ECDSA verification path (
UpdaterECDSAVerifier::verify()) usesmbedtls_pk_verify()which is correct for ECDSA, so do NOT change the ECDSA verifier.Files to modify:
libraries/Update/src/Updater_Signing.cpp— UpdateUpdaterRSAVerifier::verify()to use PSS verification as described above.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Analyze this issue and fix it if it is valid
Assistant:
I'll analyze this issue thoroughly. The problem is about RSA signature verification failing when using signatures created by
bin_signing.pybecause of a PSS padding salt length mismatch between the Python signing script and the mbedtls verification inUpdater_Signing.cpp.Let me examine the relevant code files first.
Assistant: Now let me look at the header file to understand the class structure and any existing includes:
Assistant: Now I have a thorough understanding of the issue. Let me analyze it:
Root Cause Analysis:
The issue is a **mismatch between the signing and verification ...
This pull request was created from Copilot chat.