How to: Verify the Digital Signatures of XML Documents
You can use the classes in thenamespace 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.
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 aobject and specify the name of the key container that was used for signing.
Retrieve the public key using theclass. 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 anobject by loading an XML file from disk. The XmlDocument object contains the signed XML document to verify.
Create a newobject and pass the XmlDocument object to it.
Find the <signature> element and create a newobject.
Load the XML of the first <signature> element into the SignedXml object.
Check the signature using themethod and the RSA public key. This method returns a Boolean value that indicates success or failure.
Imports System Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Imports System.Xml Module VerifyXML Sub Main(ByVal args() As String) Try ' Create a new CspParameters object to specify ' a key container. Dim cspParams As New CspParameters() cspParams.KeyContainerName = "XML_DSIG_RSA_KEY" ' Create a new RSA signing key and save it in the container. Dim rsaKey As New RSACryptoServiceProvider(cspParams) ' Create a new XML document. Dim xmlDoc As 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...") Dim result As Boolean = VerifyXml(xmlDoc, rsaKey) ' Display the results of the signature verification to ' the console. If result Then Console.WriteLine("The XML signature is valid.") Else Console.WriteLine("The XML signature is not valid.") End If Catch e As Exception Console.WriteLine(e.Message) End Try End Sub ' Verify the signature of an XML file against an asymmetric ' algorithm and return the result. Function VerifyXml(ByVal Doc As XmlDocument, ByVal Key As RSA) As [Boolean] ' Check arguments. If Doc Is Nothing Then Throw New ArgumentException("Doc") End If If Key Is Nothing Then Throw New ArgumentException("Key") End If ' Create a new SignedXml object and pass it ' the XML document class. Dim signedXml As New SignedXml(Doc) ' Find the "Signature" node and create a new ' XmlNodeList object. Dim nodeList As XmlNodeList = Doc.GetElementsByTagName("Signature") ' Throw an exception if no signature was found. If nodeList.Count <= 0 Then Throw New CryptographicException("Verification failed: No Signature was found in the document.") End If ' 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 Then Throw New CryptographicException("Verification failed: More that one signature was found for the document.") End If ' Load the first <signature> node. signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. Return signedXml.CheckSignature(Key) End Function End Module
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.
Compiling the Code
To compile this example, you need to include a reference to System.Security.dll.
Include the following namespaces:, , 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.
Never embed a private key directly into your source code. Embedded keys can be easily read from an assembly using theor by opening the assembly in a text editor such as Notepad.