fix: improve C14N handling for inline and transformed references#98
fix: improve C14N handling for inline and transformed references#98leifj wants to merge 3 commits intomoov-io:masterfrom
Conversation
This commit addresses XML signature validation for various EU Trust Service Lists (TSLs): 1. XAdES SignedProperties (no transforms): Keep element in original namespace context for proper C14N, which ensures inherited namespaces are included. 2. References WITH c14n transforms (e.g., exclusive c14n): Apply the c14n transform to the element IN ITS CONTEXT using ProcessElement, which properly handles inherited namespace declarations (needed for Sweden TSL). 3. References with non-c14n transforms but NO explicit c14n transform (e.g., only enveloped-signature): Extract the element to a new document before hashing, which correctly excludes inherited namespaces (needed for root-level signature test case). Tested with: - Liechtenstein TSL (XAdES with no transforms on SignedProperties) - Sweden TSL (exclusive c14n transform) - France TSL (enveloped-signature + c14n) - Root-level signature test case (enveloped-signature only)
…lement When signing/validating XML with exc-c14n and InclusiveNamespaces in the CanonicalizationMethod element, the transform content (containing the PrefixList) was not being passed to ProcessElement. This caused signature verification to fail on the receiving end because the canonical form used for signing differed from what the verifier computed. Fix by: 1. Adding canonTransform field to signatureData struct 2. Extracting InclusiveNamespaces content in parseCanonAlgorithm() 3. Passing canonTransform to ProcessElement in both Signer and Validator
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses critical issues in XML signature validation related to incorrect handling of namespace contexts during canonicalization. By refining how XML elements are processed and hashed, especially concerning inherited namespaces and various transform types, it ensures robust and accurate validation for diverse XML digital signatures, including those found in EU Trust Service Lists (TSLs). The changes improve the library's compliance with XML Signature specifications, resolving validation failures for complex signature structures. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request provides a significant and necessary fix for XML Canonicalization (C14N) handling, addressing several subtle but critical issues with namespace contexts during signature validation. The refactoring of validateReferences to handle different transform scenarios (no transforms, C14N transforms, and non-C14N transforms) is well-executed and correctly addresses the problems outlined in the description. The introduction of findReferencedElement and the changes to process elements within their original context are key improvements. My review includes a few suggestions to enhance robustness by handling potential errors and to improve maintainability by reducing code duplication.
…hm helper, remove redundant assignment
Incorporates all review feedback from upstream PRs: - PR moov-io#98 (c14n-fixes): WriteToString error handling, redundant assignment removal - PR moov-io#99 (rsa-pss): getDigestAlgorithm helper, rsaPSSHashAlgorithms shared map - PR moov-io#100 (ecdsa-fix): hash messages before ECDSA sign/verify in tests, nolint:exhaustive - PR moov-io#101 (ed25519-xmlenc-cryptosigner): error handling in xmlenc parse functions, gosec nolint for safe integer conversions, NewSigner error check in tests, .gitleaks.toml for W3C test vectors Also updates golang.org/x/crypto to v0.49.0.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request significantly refactors the XML canonicalization logic to correctly handle namespace declarations, especially when elements are extracted or processed in different contexts. It introduces new functions like collectAncestorNamespaces, processDocumentWithAncestorNS, findReferencedElement, and several specialized calculateHash functions to ensure proper C14N canonicalization, particularly for inherited namespaces and XAdES signatures. The signatureData struct now includes canonTransform to pass specific canonicalization instructions. The validateReferences and setSignature methods were updated to utilize these new mechanisms. Additionally, a minor improvement was made to an error message in getCertFromPEMString to be more accurate.
|
These commits have been included in master. Thanks! I'll keep looking at your other PRs. |
Summary
Fix XML Canonicalization (C14N) handling for both inline and transformed references, and pass InclusiveNamespaces from CanonicalizationMethod to ProcessElement.
Problem
XML signature validation was failing for various EU Trust Service Lists (TSLs) due to incorrect handling of namespace contexts during canonicalization:
Changes
validateReferences()andsetDigest()to handle three cases: no transforms, C14N transforms (element-in-context), and non-C14N-only transforms (extract-then-hash)ProcessElementmethod toExclusiveCanonicalizationfor canonicalizing an element within its document contextcalculateHashFromElementandcalculateHashFromStringhelper functionsfindReferencedElementmethod to locate elements without extractioncanonTransform(InclusiveNamespaces content) fromCanonicalizationMethodtoProcessElementin both Signer and ValidatorparseSignedInfoTesting
Validated with: