title: How to verify signatures of XML File in C# with <x509certificate> (not cert file)?
body: <p>i am making some codes for goverment billing system and after reading for some weeks i reached a dead end.</p>
  
  <p>I need to validate at least two signatures inside a xml file i receive from a server, i had made some code based on public MSDN to verify signatures when I am the one who's signing with a cert file or a x509store, and works fine, i can sign and validate each signature based on their reference with no trouble.</p>
  
  <p>But, when i receive the file i don't know how to validate it as i don't have the certificate file, i am certainly aware there's the  tag inside and if i get that value and put it on openssl.exe i can verify the identity of the signer, but idk how to check if the signature was made correctly</p>
  
  <p><a href="http://puu.sh/dypRH/c45e200202.png" rel="nofollow noreferrer">http://puu.sh/dypRH/c45e200202.png</a></p>
  
  <p>in the past i used</p>
  
  <pre><code>if (signedXml.CheckSignature(cert, true))
  </code></pre>
  
  <p>now i try
      signedXml.CheckSignature())</p>
  
  <p>fails every time, i can't reference which signature to pick, so i made a for </p>
  
  <pre><code>                foreach (XmlNode node1 in nodeList)
              {
                      testt = node1.OuterXml;
                       testt = testt.Replace(Environment.NewLine, string.Empty);
                      ttt.PreserveWhitespace = true;     
                      ttt.LoadXml(testt);
  
                      testt = testt.Replace(Environment.NewLine, string.Empty);
                     signedXml.LoadXml(ttt.DocumentElement);
                      //if (signedXml.CheckSignature(cert, true))
                      if (signedXml.CheckSignature())
                      {
                          Console.WriteLine("The XML signature is valid.");
                      }
                      else
                      {
                          Console.WriteLine("The XML signature is not valid.");
                      }
  
  
              #endregion
          }
  </code></pre>
  
  <p>yet they still fail</p>
  
  <p>i've also made a certificate based on the xml file, </p>
  
  <pre><code>X509Certificate c = X509Certificate.CreateFromSignedFile("test.xml");
  theCertificate = new X509Certificate2(c);
  </code></pre>
  
  <p>to validate, but fails too</p>
  
  <p>I really don't know what to do now, i am kinda desperated, here's the xml file </p>
  
  <p><a href="http://puu.sh/dyqcv/356dd289ae.xml" rel="nofollow noreferrer">http://puu.sh/dyqcv/356dd289ae.xml</a></p>
  
  <p>i need to validate signatures with, after this i have to make a response file and send it back to the server (it's ready but as i can't validate signatures i don't want to go forth)</p>
  
  <p>Oh and yes, i have removed the namespaces and linarized everything before the signature check/calculation</p>
  
  <pre><code>    public static XElement RemoveAllNamespaces(XElement e)
      {
          return new XElement(e.Name.LocalName,
             (from n in e.Nodes()
              select ((n is XElement) ? RemoveAllNamespaces(n as XElement) : n)),
             (e.HasAttributes) ? (from a in e.Attributes()
                                  where (!a.IsNamespaceDeclaration)
                                  select new XAttribute(a.Name.LocalName, a.Value)) : null);
      }
  </code></pre>
  
  <p>I will really appreciate from heart all kinds of help, even a little pointer of where should i try it will help.</p>
  
  <p>Oh and sorry, but i really checked all the website, i hope this question ain't repeated, last time i got really bashed out and lost all my rep :(</p>
  
  <p>But i got motivated enough to ask again</p>
  
  <p><a href="https://stackoverflow.com/questions/27158035/replace-string-with-another-string-does-not-work">https://stackoverflow.com/questions/27158035/replace-string-with-another-string-does-not-work</a></p>
  
  <p>I suffered with this lol</p>
  
  <p>Regards!</p>
  
  <p>EDIT: as asked by user409104 here's the signature value for the entire file (the 1st of three), it inclused the signature nodes and keyinfo values too</p>
  
  <pre><code>&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
  &lt;SignedInfo&gt;
  &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/&gt;
  &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
  &lt;Reference URI="#SetDoc"&gt;
  &lt;Transforms&gt;
  &lt;Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/&gt;
  &lt;/Transforms&gt;
  &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
  &lt;DigestValue&gt;pIQ3XhBvaULXo7vPOktydkK+c3g=&lt;/DigestValue&gt;
  &lt;/Reference&gt;
  &lt;/SignedInfo&gt;
  &lt;SignatureValue&gt;
  n/bgCyPOI01quzrpt0OLRUbBnUd5vzjyv44S/3Oow56fS/DEtBF+83g1I4WXsw13h81rFhiApgku
  tffdWuu23ox3ubGtuAVihK1xaOYBqim8dIBoLBN84M6P3H2+NDi4x01/c+4w7wtNCGedGNrjguwQ
  0b864BTj0iavTpu0urUicWipsmxCv9p6JlkyVxmUdlgEvP5pPlv43qHopWvUDrYhXP9m45ap4ubg
  47Zwpa7QCadghf4vEYUQOcrGcf4oDYa7KUc71VKTY8cAgmdTool8ugYD2ipNV75R5i3Cfbe8Jsyt
  q9wy8iuo2Xc3FpUShojyr3JMl7meJupnmT4qGg==
  &lt;/SignatureValue&gt;
  &lt;KeyInfo&gt;
  &lt;KeyValue&gt;
  &lt;RSAKeyValue&gt;
  &lt;Modulus&gt;
  xUg5RWGM6yKTu1hctQ0J9VZtr6+7VOQ75lDq7MeqxDTJgkHxO+P49GV/hRjLv3rCXvo9JBM8AKj5
  U+/uue5OE6PgeqdS+M7HBF4ieD33wVsRYWuu2TLrw+/DRd6yEs61mPVRLDrIqjxxpa41VUAfwPXV
  ksCZ7RNklyfSa5D+Zm6sx3v5kGPylWVpdW+k3BFYmUCW2j1rjGb5X9zV9Egi2VGe2SGsYLNu1aJS
  HHAIPc+COJIBMENga/syQSNF2l4/GNUkn84RnlW5P75rPg6Oa+y2UwfUgmgrCeMcyJJKDMy6heqm
  6huvgaxOTr9DZqWkxJYW5GGWTUUiq64JB3EjOQ==
  &lt;/Modulus&gt;
  &lt;Exponent&gt;AQAB&lt;/Exponent&gt;
  &lt;/RSAKeyValue&gt;
  &lt;/KeyValue&gt;
  &lt;X509Data&gt;
  &lt;X509Certificate&gt;
  MIIElTCCA32gAwIBAgIBLjANBgkqhkiG9w0BAQUFADCBpTEUMBIGA1UEAxMLU0lJLUNBLTIwMTMx
  GjAYBgNVBAcTEVNhbnRpYWdvIGRlIENoaWxlMR0wGwYDVQQIExRSZWdpb24gTWV0cm9wb2xpdGFu
  YTELMAkGA1UEBhMCQ0wxHDAaBgkqhkiG9w0BCQEWDXNpaS1jYUBzaWkuY2wxJzAlBgNVBAoTHlNl
  cnZpY2l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ncx/6aJSgmMBj52Whe
  gHJf3+SCarDYgo2L7AzaIL7/nM1KtlKpcpqFU+LC+AFf4MctSe8nthdg7VaKze1f5W2ZKvVBDNwI
  1LFrbBEn6w3PlkcJ0AjvlJBEgPlMzQVEKSEGdPOTbsiWvSxdxj8HuFFKx3R+bH9ZkNjP5s06nTFh
  SFVVIYZvCQ/qIYyUCBe4ZvH02ekYK8KeO6suiDI8cctoC+DWGfSvcLEt07AuwuVbqzAde32ACnC2
  lqqstjt3FuB3nYymxbrnzhJjpPkTt9m9MbSFvJzrAgQ7joO1Dw==
  &lt;/X509Certificate&gt;
  &lt;/X509Data&gt;
  &lt;/KeyInfo&gt;
  &lt;/Signature&gt;
  </code></pre>
  
  <p>i have made this to create the key based on the xml file, yet i can't choose which  to pick, i guess it uses the first found, (but there are 3 in the xml) also still don't know how to use it to validate.</p>
  
  <p>i can't make a x509certificate/2 with a RSAKeyValue right?</p>
  
  <pre><code>XmlElement XmlSignature;
  XmlSignature = (XmlElement)fix2.DocumentElement.SelectSingleNode("//Signature");
  RSAKeyValue key = new RSAKeyValue();
  key.LoadXml(XmlSignature);
  </code></pre>