Tweene
Tweene

Reputation: 297

XPath validation returns false

EDIT. I have a problem with XmlDsigXPathTransform valiation. Sad to say even when I copied 1:1 the example from docs the xpath validations ends failed. What am I missing? I can't figure anything anymore about this when even the docs example fails.

https://learn.microsoft.com/pl-pl/dotnet/api/system.security.cryptography.xml.xmldsigxpathtransform?view=netframework-4.6.1

var signatureReference = new Reference { Uri = "", };
XmlDsigXPathTransform XPathTransform = 
CreateXPathTransform(XPathString);
signatureReference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";
signatureReference.AddTransform(XPathTransform);
signedXml.AddReference(signatureReference);

 private static XmlDsigXPathTransform CreateXPathTransform(string XPathString)
    {
        XmlDocument doc = new XmlDocument();
        XmlElement xPathElem = doc.CreateElement("XPath");
        xPathElem.InnerText = XPathString;
        XmlDsigXPathTransform xForm = new XmlDsigXPathTransform();
        xForm.LoadInnerXml(xPathElem.SelectNodes("."));
        return xForm;
    }

Upvotes: 0

Views: 111

Answers (1)

bartonjs
bartonjs

Reputation: 33098

The XmlDsigXPathTransform is no longer considered safe, so any document using it is automatically considered to have an invalid signature.

https://referencesource.microsoft.com/#System.Security/system/security/cryptography/xml/signedxml.cs,8b616077b30145cd

If you really want to use it, you have to enable it in the Windows Registry on whatever computers are going to call CheckSignature.

https://support.microsoft.com/en-us/topic/after-you-apply-security-update-3141780-net-framework-applications-encounter-exception-errors-or-unexpected-failures-while-processing-files-that-contain-signedxml-922edd45-a91e-c755-bb30-2604acf37362

SignedXml is old and outdated, my recommendation is to not use it at all, unless you have to for compatibility (the .NET team calls it legacy and says it's not being invested in on issues, e.g. https://github.com/dotnet/runtime/issues/44674#issuecomment-875163316).

Upvotes: 1

Related Questions