runtime icon indicating copy to clipboard operation
runtime copied to clipboard

SignedXml - refuses valid signed XML file

Open karelz opened this issue 9 years ago • 63 comments

Created based on Connect report: http://connect.microsoft.com/VisualStudio/feedback/details/1765025/verify-xml-signature-getc14ndigest-problem-in-signedxml-class (internal bug 144292)

Description: I've a problem with verifying XML signature file. The signature is done by third-party application and I am verifying the signature of that XML file in .NET framework 4.0 (check with latestes 4.6 and there is no difference).

To identify the problem, I've taken a sample.xml

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><a><b>A</b></a>

When I sign the xml with the third-party app, I get signed.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<a>
  <b>A</b>
  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xds="http://uri.etsi.org/01903/v1.1.1#">
    <ds:SignedInfo>
      <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
      <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
      <ds:Reference URI="">
        <ds:Transforms>
          <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
          <ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        </ds:Transforms>
        <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
        <ds:DigestValue>/l5xchvEjxgLdPNt9RiB3TTQTcc=</ds:DigestValue>
      </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>
      cSUDo87XVNqbo+ljCZRAXvcuIXkak/10XyVWks9BNz5EdUmcVbiU84Y5qrJoAkMd
      yHPiIFkz8Dx5v5psK5oZjGtRleQ67nm1BryHA8F7EW4Otxe/8hEHsqVFK0zz3P79
      XzKD2vl4lUaTTOLlCMD+SbpbekyLkDIjXS/6IylPWbNNF3sH9MclGGhSSjREwOuJ
      Ayj8xqqibQEDvIytLN23+bZJtOGAU54ERXPnh4rccBvzByno08DHVnkQrSQCgY4E
      CvVbocIFo7GGtq8v9oj6rK3KpWUQnL1V1Aqj5fXRNRC8VnxJyIkHAXOBWC3Wr+DQ
      zEm4W0Xa+vZ8o0x/2Ct2cg==
    </ds:SignatureValue>
    <ds:KeyInfo>
      <ds:X509Data>
        <ds:X509Certificate>
          MIIEejCCA+OgAwIBAgIIBg5jkS3DgTkwDQYJKoZIhvcNAQEFBQAwOjETMBEGA1UE
          CxMKSW5mcmF4STRDQTEWMBQGA1UEChMNSW5mcmF4IGQuby5vLjELMAkGA1UEBhMC
          U0kwHhcNMTEwNTAzMDkxMzU2WhcNMTYwNTAxMDkxMzU2WjBfMRcwFQYDVQQDEw5Q
          cnZpIFVwb3JhYm5pazEQMA4GA1UECxMHaTQgdXNlcjEUMBIGA1UEChMLVGVzdCBk
          Lm8uby4xDzANBgNVBAcTBlRvbG1pbjELMAkGA1UEBhMCU0kwggEiMA0GCSqGSIb3
          DQEBAQUAA4IBDwAwggEKAoIBAQCkkkadn/Uapod4N0f7GVgDNNN/vRoNUzqbdsER
          vUA7f/MoARF0LoXAoxpAEk2DDf24oIkGjL60uqwx4di5LQr83zsdA/7Wh7RHb73j
          Cv0glfwK/YpfJcsxfLi/OajWNX5p63D9ISSDYtIgUb4k4JFElg+mbMsRFlPQNyR6
          bADQTWputmTE+reWRiHLlnXLNSH3wGO008BfYuBnsuWupK8ar5UQ5I2IyafZJONO
          4UQjsXsanYGqivWm147bihj7zfDjqEmVg0yYm9NS/jbhQSXRV9n8348engtu48ND
          g7Jsr363Ou1nvThzsxIyRBbqSCNSJPAiEYl/jF4WRO2zGmZzAgMBAAGjggHeMIIB
          2jAMBgNVHRMBAf8EAjAAMCAGA1UdJQEB/wQWMBQGCCsGAQUFBwMCBggrBgEFBQcD
          BDAfBgNVHSMEGDAWgBSjdHM8IbieDPxF1rwNvMKR0T6WQDB3BgNVHR8EcDBuMGyg
          aqBohmZodHRwOi8vY2EuaW5mcmF4LnNpL2VqYmNhL3B1YmxpY3dlYi93ZWJkaXN0
          L2NlcnRkaXN0P2NtZD1jcmwmaXNzdWVyPU9VPUluZnJheEk0Q0EsTz1JbmZyYXgg
          ZC5vLm8uLEM9U0kwDgYDVR0PAQH/BAQDAgSwMB0GA1UdDgQWBBSKMErpxIQp6TQ8
          Jqr0ywYrYla4NzCB3gYIKwYBBQUHAQEEgdEwgc4wgYkGCCsGAQUFBzAChn1odHRw
          Oi8vY2EuaW5mcmF4LnNpL2VqYmNhL3B1YmxpY3dlYi93ZWJkaXN0L2NlcnRkaXN0
          P2NtZD1pZWNhY2VydCZpc3N1ZXI9T1UlM2RJbmZyYXhJNENBJTJjTyUzZEluZnJh
          eCtkLm8uby4lMmNDJTNkU0kmbGV2ZWw9MDBABggrBgEFBQcwAYY0aHR0cDovL2Nh
          LmluZnJheC5zaTo4MDgwL2VqYmNhL3B1YmxpY3dlYi9zdGF0dXMvb2NzcDANBgkq
          hkiG9w0BAQUFAAOBgQAms5UL/f0AIavlVo9C5W5Ijdt12g/59PIZeAEDWzoEKi+W
          rE7ORaq527UHgcVGw7Gr2HhOEHmggUe6k43edQ0fkNg5WXfJzf18hHA+foVqsxra
          DxBeot442A1zw9GjRnIDCl2r91tHgkneqg2EE8kf7lkRtMRck1MUbHBnLnppKg==
        </ds:X509Certificate>
      </ds:X509Data>
    </ds:KeyInfo>
  </ds:Signature>
</a>

(Note: The original issue had no (non-required) whitespace in the XML, it was pretty-printed here to allow for better human understanding)

This signed XML is valid, according to let's say this (https://www.signatur.rtr.at/en/elsi/Pruefung.html) tool, and I've verified it with a few other tools and all confirm that it's OK, except when I verify it with SignedXml class from .NET Framework. The code I'am using to verify the XML.

Now the problem as I've managed to identify is in the SignedXml class in function GetC14NDigest where Utils.GetPropagatedAttributes(m_context) is called. m_context is signed XML document, but the namespace xmlns:xds="http://uri.etsi.org/01903/v1.1.1#" is only in Signature node, which was propagated into the SignedInfo node, but the Utils.GetPropagatedAttributes(m_context) did not return that one, and the SignedInfo node is missing that one, so the SignedInfo node is not the same as it was when the signature was constructed.

I've tried to sign an xml like this:

<a xmlns:xds="http://uri.etsi.org/01903/v1.1.1#">
  <b>A</b>
</a>

and this signed XML I was able to verify successfully with SignedXml class.

karelz avatar May 01 '17 20:05 karelz

Next steps:

  1. Turn the repro into test. Confirm it repros.
  2. Fix the bug.

karelz avatar May 01 '17 20:05 karelz

cc @StanislavUshakov @anthonylangsworth @tintoy @peterwurzinger - anyone interested to tackle it?

karelz avatar May 01 '17 20:05 karelz

I could take a look tomorrow morning (AU timezone) if nobody else wants it?

tintoy avatar May 02 '17 05:05 tintoy

I was going to put up my hand for it but the earliest I can get to it is tomorrow evening (also AU timezone).

anthonylangsworth avatar May 02 '17 05:05 anthonylangsworth

Appreciate your help guys, thanks! I guess whoever starts first should ping this issue ... thanks again!

karelz avatar May 02 '17 05:05 karelz

Ok - I'll have a look at this shortly.

tintoy avatar May 02 '17 19:05 tintoy

@karelz - I'll create a test for this scenario; assuming it fails, is that sufficient for a repro?

tintoy avatar May 02 '17 20:05 tintoy

Yep, that's what I had in mind. Sorry for not being clear. (updated above)

karelz avatar May 02 '17 20:05 karelz

NP - just bootstrapping the build on my new laptop and then I'll get right onto it.

tintoy avatar May 02 '17 21:05 tintoy

BTW: We have new docs for contributors (mostly new contribs): https://github.com/dotnet/corefx/wiki/New-contributor-Docs If you have gotchas, please add them - it is writeable wiki without PR system.

karelz avatar May 02 '17 21:05 karelz

No worries - I have a Powershell profile that sets up environment variables for corefx / netfx dev, but building corefx itself has more subtle requirements it seems; had to drop back to using "Developer Command Prompt for VS2017" to get it to work.

tintoy avatar May 02 '17 21:05 tintoy

Hmm, interesting. After the build, I see src/shims/ApiCompatBaseline.netcoreapp.netfx461.txt has changed:

diff --git a/src/shims/ApiCompatBaseline.netcoreapp.netfx461.txt b/src/shims/ApiCompatBaseline.netcoreapp.netfx461.txt
index 845369c..fe4e873 100644
--- a/src/shims/ApiCompatBaseline.netcoreapp.netfx461.txt
+++ b/src/shims/ApiCompatBaseline.netcoreapp.netfx461.txt
@@ -5,6 +5,11 @@ ApiCompat Error: 0 : Unable to resolve assembly 'Assembly(Name=System.ServiceMod
 ApiCompat Error: 0 : Unable to resolve assembly 'Assembly(Name=System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)' referenced by the contract assembly 'Assembly(Name=System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)'.
 ApiCompat Error: 0 : Unable to resolve assembly 'Assembly(Name=System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)' referenced by the contract assembly 'Assembly(Name=System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)'.
 ApiCompat Error: 0 : Unable to resolve assembly 'Assembly(Name=System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)' referenced by the contract assembly 'Assembly(Name=System.Xml.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)'.
+Compat issues with assembly mscorlib:
+TypesMustExist : Type 'System.ArgIterator' does not exist in the implementation but it does exist in the contract.
+MembersMustExist : Member 'System.Console.Write(System.String, System.Object, System.Object, System.Object, System.Object, __arglist)' does not exist in the implementation but it does exist in the contract.
+MembersMustExist : Member 'System.Console.WriteLine(System.String, System.Object, System.Object, System.Object, System.Object, __arglist)' does not exist in the implementation but it does exist in the contract.
+MembersMustExist : Member 'System.String.Concat(System.Object, System.Object, System.Object, System.Object, __arglist)' does not exist in the implementation but it does exist in the contract.
 Compat issues with assembly System:
 MembersMustExist : Member 'System.CodeDom.Compiler.CompilerParameters.Evidence.get()' does not exist in the implementation but it does exist in the contract.
 MembersMustExist : Member 'System.CodeDom.Compiler.CompilerParameters.Evidence.set(System.Security.Policy.Evidence)' does not exist in the implementation but it does exist in the contract.

tintoy avatar May 02 '17 21:05 tintoy

Urk! Ok, have run into a more serious problem. Opened System.Security.Cryptography.Xml.sln in VS2017, hit build, and run into a wall of errors:

1>------ Build started: Project: System.Security.Cryptography.Xml (ref\System.Security.Cryptography.Xml), Configuration: netcoreapp-Debug Any CPU ------
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(3,27,3,55): error CS0234: The type or namespace name 'AllowPartiallyTrustedCallersAttribute' does not exist in the namespace 'System.Security' (are you missing an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(3,27,3,55): error CS0234: The type or namespace name 'AllowPartiallyTrustedCallers' does not exist in the namespace 'System.Security' (are you missing an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(4,18,4,25): error CS0234: The type or namespace name 'Runtime' does not exist in the namespace 'System' (are you missing an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(5,18,5,28): error CS0234: The type or namespace name 'Reflection' does not exist in the namespace 'System' (are you missing an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(6,11,6,24): error CS0246: The type or namespace name 'AssemblyTitleAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(6,11,6,24): error CS0246: The type or namespace name 'AssemblyTitle' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(7,11,7,30): error CS0246: The type or namespace name 'AssemblyDescriptionAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(7,11,7,30): error CS0246: The type or namespace name 'AssemblyDescription' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(8,11,8,31): error CS0246: The type or namespace name 'AssemblyDefaultAliasAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(8,11,8,31): error CS0246: The type or namespace name 'AssemblyDefaultAlias' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(9,11,9,26): error CS0246: The type or namespace name 'AssemblyCompanyAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(9,11,9,26): error CS0246: The type or namespace name 'AssemblyCompany' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(10,11,10,26): error CS0246: The type or namespace name 'AssemblyProductAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(10,11,10,26): error CS0246: The type or namespace name 'AssemblyProduct' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(11,11,11,28): error CS0246: The type or namespace name 'AssemblyCopyrightAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(11,11,11,28): error CS0246: The type or namespace name 'AssemblyCopyright' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(12,11,12,26): error CS0246: The type or namespace name 'AssemblyVersionAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(12,11,12,26): error CS0246: The type or namespace name 'AssemblyVersion' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(13,11,13,30): error CS0246: The type or namespace name 'AssemblyFileVersionAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(13,11,13,30): error CS0246: The type or namespace name 'AssemblyFileVersion' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(14,11,14,39): error CS0246: The type or namespace name 'AssemblyInformationalVersionAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(14,11,14,39): error CS0246: The type or namespace name 'AssemblyInformationalVersion' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(15,11,15,23): error CS0246: The type or namespace name 'CLSCompliantAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(15,11,15,23): error CS0246: The type or namespace name 'CLSCompliant' could not be found (are you missing a using directive or an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(16,18,16,28): error CS0234: The type or namespace name 'Reflection' does not exist in the namespace 'System' (are you missing an assembly reference?)
1>D:\Development\github\dotnet\corefx\bin\obj\ref\System.Security.Cryptography.Xml\4.0.0.0\netcoreapp\_AssemblyInfo.cs(17,18,17,28): error CS0234: The type or namespace name 'Reflection' does not exist 

<SNIP>

3>------ Build started: Project: System.Security.Cryptography.Xml.Tests, Configuration: netcoreapp-Debug Any CPU ------
3>  System.Security.Cryptography.Xml.Tests -> D:\Development\github\dotnet\corefx\bin\AnyOS.AnyCPU.Debug\System.Security.Cryptography.Xml.Tests\netcoreapp\System.Security.Cryptography.Xml.Tests.dll
========== Build: 1 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========

@karelz I'm sure this worked on my old machine, and I have verified that required VS components are installed (read the contributor docs to be sure). Anything obvious I've missed?

tintoy avatar May 02 '17 21:05 tintoy

(interestingly, the test project builds fine, it's just the other 2 that don't)

tintoy avatar May 02 '17 21:05 tintoy

Did you compile from root first? @mellinoe @weshaggard can hopefully help troubleshoot ...

karelz avatar May 02 '17 21:05 karelz

Yep, .\build.cmd. That works fine. Interestingly, if I run msbuild src\System.Security.Cryptography.Xml\System.Security.Cryptography.Xml.sln it also fails (can't find Microsoft.CSharp.Portable.targets).

tintoy avatar May 02 '17 21:05 tintoy

Or if I build from developer command prompt, it just spits out the same wall of errors listed above.

tintoy avatar May 02 '17 21:05 tintoy

Are you able to build the csproj directly from the command line?

mellinoe avatar May 02 '17 21:05 mellinoe

cc @krwq

karelz avatar May 02 '17 21:05 karelz

@mellinoe yes! Just not the solution file.

tintoy avatar May 02 '17 21:05 tintoy