locked
Malformed element Signature error (Cryptography, XML, RSA, C#, VS2010) RRS feed

  • Question

  • Attempting to check the signature value in the code below, but getting error "Melformed element Signature" at line: "signedXml.LoadXml((XmlElement)nodeList[0]);". Code below had been taken from Microsoft linkhttp://msdn.microsoft.com/en-us/library/ms229950.aspx"

    And below is a piece of the xml used for this test - with the signature and digest values removed:

    <Message xmlns="http://www.blah.com/messaging"version="010" release="006">
       <Header>
          <To Qualifier="blah;/To>
          <From Qualifier=&blah</From>
          <MessageID>93585dc9571b49fda</MessageID>
          <SentTime>2013-08-26T17:27:43.80Z</SentTime>
          <SenderSoftware>
             <SenderSoftwareDeveloper>blahpts</SenderSoftwareDeveloper>
             <SenderSoftwareProduct>Certification Testing</SenderSoftwareProduct>
             <SenderSoftwareVersionRelease>2013</SenderSoftwareVersionRelease>
          </SenderSoftware>
          <PrescriberOrderNumber>blah 2.1 ES</PrescriberOrderNumber>
          <DigitalSignature version="
    2.0">
             <DigestValue>jblahlwT1Y=</DigestValue>
             <SignatureValue>blahblah></SignatureValue>

             <X509Data> blahblahblah</X509Data>

    </DigitalSignature>
    < /Header>

      <Body>

      .......

    </Body>

    </Message>

    // Code below used to process xml above

            public static void Main(String[] args)
            {
                try
                {
                    // Create a new CspParameters object to specify
                    // a key container.
                    CspParameters cspParams = new CspParameters();
                    cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";

                    // Create a new RSA signing key and save it in the container.
                    RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);

                    // Create a new XML document.
                    XmlDocument xmlDoc = new XmlDocument();

                    // Load an XML file into the XmlDocument object.
                    xmlDoc.PreserveWhitespace = true;
                    xmlDoc.Load(args[0]); //("test.xml");

                    // Verify the signature of the signed XML.
                    Console.WriteLine("Verifying signature...");
                    bool result = VerifyXml(xmlDoc, rsaKey);

                    // Display the results of the signature verification to
                    // the console.
                    if (result)
                    {
                        Console.WriteLine("The XML signature is valid.");
                    }
                    else
                    {
                        Console.WriteLine("The XML signature is not valid.");
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }


            // Verify the signature of an XML file against an asymmetric
            // algorithm and return the result.
            public static Boolean VerifyXml(XmlDocument Doc, RSA Key)
            {
                // Check arguments.
                if (Doc == null)
                    throw new ArgumentException("Doc");
                if (Key == null)
                    throw new ArgumentException("Key");

                // Create a new SignedXml object and pass it
                // the XML document class.
                SignedXml signedXml = new SignedXml(Doc);

                // Find the "Signature" node and create a new
                // XmlNodeList object.
                XmlNodeList nodeList = Doc.GetElementsByTagName("SignatureValue");

                // Throw an exception if no signature was found.
                if (nodeList.Count &lt;= 0)
                {
                    throw new CryptographicException("Verification failed: No Signature was found in the document.");
                }

                // This example only supports one signature for
                // the entire XML document.  Throw an exception
                // if more than one signature was found.
                if (nodeList.Count &gt;= 2)
                {
                    throw new CryptographicException("Verification failed: More that one signature was found for the document.");
                }

                //
                // Load the first &lt;signature&gt; node.
                signedXml.LoadXml((XmlElement)nodeList[0]); // Here is where the error is being generated.

                // Check the signature and return the result.
                return signedXml.CheckSignature(Key);
            }

    Need help in figuring out what is going wrong please, thank you



    Thursday, August 29, 2013 6:55 PM