How to: Verify the Digital Signatures of XML Documents
You can use the classes in the System.Security.Cryptography.Xml namespace to verify XML data signed with a digital signature. XML digital signatures (XMLDSIG) allow you to verify that data was not altered after it was signed. For more information about the XMLDSIG standard, see the World Wide Web Consortium (W3C) specification at http://www.w3.org/TR/xmldsig-core/.
The code example in this procedure demonstrates how to verify an XML digital signature contained in a <Signature> element. The example retrieves an RSA public key from a key container and then uses the key to verify the signature.
For information about how create a digital signature that can be verified using this technique, see How to: Sign XML Documents with Digital Signatures.
To verify the digital signature of an XML document
To verify the document, you must use the same asymmetric key that was used for signing. Create a CspParameters object and specify the name of the key container that was used for signing.
Retrieve the public key using the RSACryptoServiceProvider class. The key is automatically loaded from the key container by name when you pass the CspParameters object to the constructor of the RSACryptoServiceProvider class.
Create an XmlDocument object by loading an XML file from disk. The XmlDocument object contains the signed XML document to verify.
Create a new SignedXml object and pass the XmlDocument object to it.
Find the <signature> element and create a new XmlNodeList object.
Load the XML of the first <signature> element into the SignedXml object.
Check the signature using the CheckSignature method and the RSA public key. This method returns a Boolean value that indicates success or failure.
using System; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Xml; public class VerifyXML { 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("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("Signature"); // Throw an exception if no signature was found. if (nodeList.Count <= 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 >= 2) { throw new CryptographicException("Verification failed: More that one signature was found for the document."); } // Load the first <signature> node. signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. return signedXml.CheckSignature(Key); } }
This example assumes that a file named "test.xml" exists in the same directory as the compiled program. The "test.xml" file must be signed using the techniques described in How to: Sign XML Documents with Digital Signatures.
-
To compile this example, you need to include a reference to System.Security.dll.
-
Include the following namespaces: System.Xml, System.Security.Cryptography, and System.Security.Cryptography.Xml.
Never store or transfer the private key of an asymmetric key pair in plaintext. For more information about symmetric and asymmetric cryptographic keys, see Generating Keys for Encryption and Decryption.
Never embed a private key directly into your source code. Embedded keys can be easily read from an assembly using the Ildasm.exe (MSIL Disassembler) or by opening the assembly in a text editor such as Notepad.
The code example verifies the XML content against the signature. I tested the code and if you change any content of the xml file, the verification fails.
When signing in the other example, you can specify the "uri" for the reference. You can set this to point at a tiny element of the document, or even another document entirely. The signature checking in this example just checks you have a valid signature from a given key - it does not check that this document is signed.
So an attacker can give you a file signed with the uri pointing at either only a portion of the document, or another document somewhere else entirely. As long as they can get hold of any other document signed by that key, they can make this example code pass with
The XML signature is valid.
But it only means exactly that. The other content of this XML file could be anything the attacker wanted at all. You can verify this by setting Uri to "other.xml" when signing. Hand create "other.xml" with some valid xml. Then change the credit card number in test.xml. Now re-verify test.xml. It passes the CheckSignature with a completely different credit card number.I know example code can't cover anything, but this does something very different from what the reader will likely understand.
- 3/7/2011
- t_e