fix(update): Replace manual ASN.1 DER parsing with mbedtls_asn1_get_tag() in ECDSA verifier#12504
Merged
lucasssvaz merged 2 commits intoApr 8, 2026
Conversation
|
|
…ag() 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>
Copilot
AI
changed the title
[WIP] Replace manual ASN.1 parsing in verify method with mbedtls function
fix(update): Replace manual ASN.1 DER parsing with mbedtls_asn1_get_tag() in ECDSA verifier
Apr 8, 2026
41ed233
into
copilot/fix-signing-verification-mismatch
1 check was pending
me-no-dev
pushed a commit
that referenced
this pull request
Apr 9, 2026
…2503) * Initial plan * Fix RSA-PSS signature verification mismatch in UpdaterRSAVerifier::verify() 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> * Fix unsigned subtraction underflow in RSA-PSS salt length calculation 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> * Fix signature length mismatch: use actual sig length instead of padded 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> * fix(update): Replace manual ASN.1 DER parsing with mbedtls_asn1_get_tag() 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> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lucasssvaz <32426024+lucasssvaz@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The ECDSA verifier in
Updater_Signing.cpphand-rolled ASN.1 DER length parsing to strip zero-padding from the 512-byte signature buffer, only handling short-form and0x81long-form lengths. Replaced with mbedtls's ownmbedtls_asn1_get_tag(), which handles all DER length forms, validates the tag and bounds internally, and is already present in ESP-IDF.Changes
libraries/Update/src/Updater_Signing.cpp#include "mbedtls/asn1.h"UpdaterECDSAVerifier::verify()withmbedtls_asn1_get_tag():actualSigLenfalls back tosignatureLen(same behaviour as before)Original prompt
Problem
In
libraries/Update/src/Updater_Signing.cpp, theUpdaterECDSAVerifier::verify()method currently has hand-rolled ASN.1 DER parsing to determine the actual ECDSA signature length from a zero-padded 512-byte buffer. This manual parsing is fragile and only handles specific length encoding forms.Required Change
Replace the manual ASN.1 parsing in
UpdaterECDSAVerifier::verify()with mbedtls's ownmbedtls_asn1_get_tag()function, which handles all DER length forms correctly and is already available in ESP-IDF.Current code to replace (around lines 153-170):
Replace with:
Also add the following include at the top of the file, alongside the other mbedtls includes:
Important constraints:
libraries/Update/src/Updater_Signing.cpp.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Review this PR if the changes are actually needed and, if yes, if they are properly implemented
Assistant:
I'll review this PR by examining the changes and understanding the context. Let me start by gathering the PR details and the actual code changes.
Assistant: Now let me look at the original (pre-PR) version of the file to understand the baseline, and also check how the
signatureLenis passed from the caller:Assistant: Now I have a comprehensive understanding of the issue and the PR changes. Let me also check an important detail about the mbedtls
mbedtls_pk_verify_extAPI to validate the approach:Assistant: Now let me also check the
mbedtls_pk_verifybehavior for ECDSA with padded signature lengths:Assistant: Now I have a thorough understanding of the issue, the original code on
master, the signing tool, and the PR's changes. Here's my review:Review of PR #12503
Is the fix needed?
Yes, absolutely. There are two real bugs in the current
mastercode:RSA: PSS vs PKCS#1 v1.5 mismatch —
bin_signing.pysigns with RSA-PSS padding (padding.PSSwithsalt_length=PSS.MAX_LENGTH), but the C++ verifier on master callsmbedtls_pk_verify(), which performs PKCS#1 v1.5 verification. These are completely different schemes—PSS-signed binaries will always fail PKCS#1 v1.5 verification. This is confirmed by issue #12427 with error-0x4380(MBEDTLS_ERR_RSA_VERIFY_FAILED).ECDSA/RSA: Signature length mismatch — The caller in
Updater.cppalways passesmaxSigSize = 512assignatureLen:But
mbedtls_pk_verify/mbedtls_pk_verify_extexpects the exact signature length, not a padded buffer size. For RSA-2048 the actual signature is 256 bytes, and for ECDSA the DER signature is variable-length (~70-72 bytes for P-256). Passing 512 causesMBEDTLS_ERR_PK_SIG_LEN_MISMATCHfor everything except RSA-4096.Is the fix properly implemented?
The RSA fix is correct. The ECDSA fix has a concern:
✅ RSA Verifier — Correct