Security is a main concern in any application development especially where there is an exchange of critical information with an external system. There are ways to securely send and receive data such as HTTPS and Public Key Cryptography. In todays world, XML has become a standard means for data communication between applications. Recent specifications from W3C on XML Signature addresses the issue of securely sending and receiving data that complies with most of the algorithms, PGP, RSA DSA etc. In this article we will explore XML Signature specification from W3C and its implementation in .NET using C#.

Description

In any application when there is a need to access data from an external application, security plays an important role. Applications should take all possible measures to ensure that the information is not tampered with during its transmission. A couple of solutions that come to any developers mind are to use HTTPS or public key cryptography. HTTPS adds performance constraints and public key cryptography requires both sender and receiver to agree on a specific algorithm or tool. Furthermore the entire document is signed or encrypted in a HTTPS or Public key scenario. There is no option to partially sign a document. W3C has released a new set of standards, XML Signature, to digitally sign an XML Document. The biggest advantage is that application written in any language (Java based or Microsoft based) can understand the SignedXml and can validate the signature. Microsoft.NET Framework exposes a namespace specifically to address XML signature.

Differences between XML Signature and Public Key Cryptography: Before we try to understand about XML signature let us take a look at the differences between digital signature and public key encryption.

Public Key Encryption

Digital Signature

Complete Data is encrypted

Only the Message Digest is Encrypted

Public Key is used to encrypt the data and private key is used to decrypt the data.

Private key is used to encrypt the MessageDigest and Public key is used to decrypt to get the Digest Value and then recalculate the MessageDigest to check if the data was tampered.

The above table gives basic information on Encryption and Digital Signature. Given a situation where data encryption is not required but its authentication is, digital signature is the preferred way of implementation. In the next section we will explore a scenario where XML Signature can be used and how Microsoft .NET provides functionality to sign an XML Document.

Consider any business scenario like online shopping; where customers enter details about the products they are interested in, shipping address and also the Credit Card Information. Lets say all these details are stored across pages and are put into one XML Document. There are so many things that could happen in the background like placing an order to the concerned products department, informing the shipping department to ship the order and asking the payment department to charge the credit card. These can happen in stages and the information is transmitted between applications. There are a couple of things that must be taken into account. Firstly, Whether or not the data need to be encrypted. Secondly does it require only a part of the document to be secure? In a scenario where there is no need to encrypt the data but only a part of the document needs to be validated for any change in its content, there XML Signature can be used to Sign a part of or the complete document.

Now that we have identified the scenario to use XML Signature let us discuss about a scenario where it will reduce the development efforts. Consider a situation where the information from one company can go to multiple companies via FTP. Encryption using Public key Cryptography is the ideal solution. However, if the data is not critical but still needs to be authenticated to verify validity then digital signature is preferred. Using any tools like PGP, RSA to achieve this might result in increased development effort if any new business partner requires a different algorithm or tool. How about using the same algorithm like PGP, RSA but with a standard like XML Signature to achieve the same. XML signature supports multiple algorithms like RSA DSA, PGP etc and the standards are defined by W3C. Any application that can understand the XML signed as per W3C recommendations would be able to decipher the XML signed using Microsoft .NET.

Before we get into the details of implementations in .NET we will first take a look at W3C Specification on XML Signature and then we will discuss how to implement it using C#.

W3C Specification

Let us take a brief look at W3C Specification on XML Signature and then we will explore how .NET helps in reducing the development time and adding flexibility in developing a Secured Application.

This standard specification defines a common place in a SignedXML to look for SigningKey, Signature value, Digestvalue and Data. We will take a closer look at this XML Specification and how to sign an XML Document using C#.

Implementation in Microsoft .NET

SignedXML is the wrapper around W3C Specification to compute and verify the signature. In order to sign an XML Document, the required elements are Data to be signed and Key used to sign the data. Microsoft .NET provides classes and methods to perform these operations. SignedXML has methods to hold the XML data to be signed, transformation to be applied, SigningKey etc. Before we begin to explore the details of SignedXML let us take a look at various classes in Cryptography.XML namespace.

DataObject

DataObject is a placeholder to hold the Data to be Signed. DataObject in .NET refers to <Object> xml tag in W3C Specification. DataObject has two properties: Data and ID.Data: To get/set the Data to be signedID: This can be used to reference data in a different location.

The following code will create a DataObject and set the Data as CreditCardInfo.xml.

Alternatively you can refer to a file in the internet/intranet using the Property ID of DataObject.

Reference

Reference Object in .NET Framework represents <Reference> xml tag in W3C Specification. There can be more than one Reference tag in a Signed XML document. Reference gives the flexibility to add multiple Data to be signed and apply different transformations for each DataObject. Digest values are specific to a Reference URI. Take a closer look at Reference tag in W3C Specification.

Note: After the KeyPair is generated you have an option to save only the PublicKey or with PrivateKey also.

In the above example the key is stored in two different files. One file has PrivateKey information along with PublicKey and the other file has only PublicKey information.

The following line of code is used to extract both Public and PrivateKey information.

string strDSAKeyXML = objDSAkey.ToXmlString(true);

The following line of code is used to extract only the PublicKey information.

strDSAKeyXML = objDSAkey.ToXmlString(false);

SignedXML:

The important element required to sign an XML Document is the key. Keys can be generated using classes of DSA, RSA etc provided by .NET Framework. Now that we have defined the necessary information like DataObject, Reference and Key required to sign an XML Document, we will take a look at SignedXML Class in .NET.

SignedXML class in .NET Framework combines these bits and pieces of information required for signing an XML Document and generating a SignatureValue. SignedXML exposes the following methods and property.

SigningKey

To set the Key used to Sign the XML Document.

AddObject

To add the DataObject (Data to be signed) to the SignedXML Object.

AddReference

To add Reference to the DataObject along with any transformation Information.

ComputeSignature

This calculates the XML Signature with the information provided.(SigningKey, DataObject, Reference)

We will now define the process of using the generated key to sign the XML Document using Signed XML. The steps involved in using SignedXML to sign an XML Document are as follows.Set the Signing KeyAdd the DataObject to SignedXML

Add the Reference Object to Signed XML

ComputeSignature

SignedXML has a property called SigningKey to set the signingkey.Set the SigningKey using KeyFile already generated.

The following line of code will load the generated key file and set the SigningKey of SignedXML.

XmlDocument objDSAKeyPairXMLdocument = new XmlDocument();objDSAKeyPairXMLdocument.Load("XMLSignatureDSAKeyPair.xml");string strDSAKeyPairXML = objDSAKeyPairXMLdocument.InnerXml;Console.WriteLine (strDSAKeyPairXML );SignedXml signedXml = new SignedXml();DSA objDSAkeyPair = DSA.Create();objDSAkeyPair.FromXmlString(strDSAKeyPairXML );signedXml.SigningKey = objDSAkeyPair ; Now that we have set the Key to sign the XML document, we will explore how to add a DataObject to the signed XML. The following line of code will add the DataObject to the SignedXML.

signedXml.AddObject(dataObject);

The following line of code will add the Reference object to the SignedXML.

Now we look at the above XML (Digitally Signed) and compare it with the W3C Specification,Signature - represents the entire signed XML. SignedInfo - represents the set of reference.

Reference-Each reference represents a URI to identify the DataObject, represents transforms to apply any transformation. (In our case we didnt apply any Transformation) and represents DigestMethod an algorithm used to calculate the DigestValue.

Verifying Signature Now that the XML Document is signed, the next step in the process is to verify the signature. In order to check the validity of the signed XML Document, the KeyValue should be known, that is, PublicKey of the SignedXML Document should be known. KeyInfo is not sent as a part of the original document. It is optional and left to the application developer to decide whether to send the KeyInfo or not. For Security reasons it is highly recommended to sign XML Document with PrivateKey and the PublicKey is given to the business partner separately. The business partner will then use the public key to validate the signed XML.

KeyInfo

KeyInfo is another class in System.Cryptography.Xml namespace. This gives the flexibility to add any KeyValues such as RSA, DSA, PGP, etc. KeyInfo in .NET Framework represents the KeyInfo XML tag in W3C specification. KeyInfo allows the application developer to add any KeyValue (DSA, RSA, PGP, etc) using the AddClause method.

Lets take approach one to send the KeyInfo along with the signed XML Document.

Let us take a second approach to verify the signature with PublicKey.(When a Document is Digitally signed, the sender uses KeyPair to sign the document i.e. a Digest value of the Document is created using a Hash Algorithm and then the senders Private Key is used to encrypt the MessageDigest to create the SignatureValue. Now when the receiving application receives the signed XML Document, it uses the public key to decrypt the data to retrieve the MessageDigest. Receiving application then creates the MessageDigest and compares to find whether the data has been modified or not.)

Note: When there are multiple DataObjects even if one fails to match with the DigestValue the core validation fails.

I am a Sr. Software Developer with more than 5 years of experience in Design and Development of Distributed applications, Web based applications using various technologies like Vb, Xml, Msmq, Java, Dot Net. Interested in... Read more