Skip to content

SignedXml cannot validate signature that contain XPath transforms #82363

Closed as not planned

Description

@petarpetrovt

Description

I have a few XML documents that have been signed using various tools, but their validation fails in .NET. However, these documents are deemed valid in tools such as the European Commission Digital Signature Service (DSS).

Reproduction Steps

Generate an enveloped XAdES signed XML document via DSS tool.

The transform that seems to not be supported.

<ds:Transform Algorithm="http://www.w3.org/2002/06/xmldsig-filter2">
  <dsig-filter2:XPath xmlns:dsig-filter2="http://www.w3.org/2002/06/xmldsig-filter2" Filter="subtract">/descendant::ds:Signature</dsig-filter2:XPath>
</ds:Transform>

The validation code.

XmlDocument document = new() { PreserveWhitespace = true };
document.Load("test-signed-xades-baseline-b.xml");
var signedXml = new SignedXml(document);
signedXml.LoadXml((XmlElement)document.GetElementsByTagName("Signature", SignedXml.XmlDsigNamespaceUrl)[0]!);
Console.WriteLine(signedXml.CheckSignature());

Expected behavior

The signature validation is successful.

Actual behavior

Unhandled exception. System.Security.Cryptography.CryptographicException: Unknown transform has been encountered.
   at System.Security.Cryptography.Xml.Reference.LoadXml(XmlElement value)
   at System.Security.Cryptography.Xml.SignedInfo.LoadXml(XmlElement value)
   at System.Security.Cryptography.Xml.Signature.LoadXml(XmlElement value)
   at System.Security.Cryptography.Xml.SignedXml.LoadXml(XmlElement value)

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

I have a signature produced by a Java application that uses a different XPath transform. This signature also cannot be validated.

<ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
  <ds:XPath>not(ancestor-or-self::ds:Signature)</ds:XPath>
</ds:Transform>

Adding http://www.w3.org/TR/1999/REC-xpath-19991116 algorithm in SafeCanonicalizationMethods property allows to bypass IsSafeTransform check but then throws XPathException, maybe related to this #21451 issue.

signedXml.SafeCanonicalizationMethods.Add("http://www.w3.org/TR/1999/REC-xpath-19991116");
Unhandled exception. System.Xml.XPath.XPathException: Namespace prefix 'ds' is not defined.
   at MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.LookupNamespace(String prefix)
   at MS.Internal.Xml.XPath.BaseAxisQuery.SetXsltContext(XsltContext context)
   at MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(IXmlNamespaceResolver nsResolver)
   at System.Security.Cryptography.Xml.XmlDsigXPathTransform.GetOutput()
   at System.Security.Cryptography.Xml.TransformChain.TransformToOctetStream(Object inputObject, Type inputType, XmlResolver resolver, String baseUri)
   at System.Security.Cryptography.Xml.TransformChain.TransformToOctetStream(XmlDocument document, XmlResolver resolver, String baseUri)
   at System.Security.Cryptography.Xml.Reference.CalculateHashValue(XmlDocument document, CanonicalXmlNodeList refList)
   at System.Security.Cryptography.Xml.SignedXml.CheckDigestedReferences()
   at System.Security.Cryptography.Xml.SignedXml.CheckSignature(AsymmetricAlgorithm key)
   at System.Security.Cryptography.Xml.SignedXml.CheckSignatureReturningKey(AsymmetricAlgorithm& signingKey)
   at System.Security.Cryptography.Xml.SignedXml.CheckSignature()

Activity

  1. ghost added
    untriaged
    on Feb 19, 2023
  2. changed the title [-]SignedXml can't validate signature that contain XPath transforms.[/-] [+]SignedXml cannot validate signature that contain XPath transforms[/+] on Feb 19, 2023
  3. jeffhandley commented on Jun 28, 2023

    @jeffhandley
    Member

    Thanks for reporting this, @petarpetrovt. We won't be able to investigate this further during .NET 8, so I'm moving this to Future. I'm going to label this as help wanted [up-for-grabs] Good issue for external contributors , inviting help on further investigation to be certain we know how to fix it.

  4. added
    help wanted
    on Jun 28, 2023
  5. added this to the Future milestone on Jun 28, 2023
  6. ghost removed
    untriaged
    on Jun 28, 2023
  7. ankyrawat1 commented on May 24, 2024

    @ankyrawat1

    I'm facing the same issue with the netstandard version of the "System.Security.Cryptography.Xml" dll.....with the net framework version of this dll , it works perfectly fine

    This exception was originally thrown at this call stack:
    System.Security.Cryptography.Xml.Reference.LoadXml(System.Xml.XmlElement) in Reference.cs
    System.Security.Cryptography.Xml.SignedInfo.LoadXml(System.Xml.XmlElement) in SignedInfo.cs
    System.Security.Cryptography.Xml.Signature.LoadXml(System.Xml.XmlElement) in Signature.cs
    System.Security.Cryptography.Xml.SignedXml.LoadXml(System.Xml.XmlElement) in SignedXml.cs

  8. bartonjs commented on Nov 22, 2024

    @bartonjs
    Member

    The XPath transform is not safe, it is intentionally not listed in the safe transforms list; and callers who need to use it have to explicitly register it in themselves.

    There are notes about the carriage return issue in #99856.

  9. locked and limited conversation to collaborators on Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Securityhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions