I've previously blogged about creating XML digital signatures using the .NET framework, but today I'd like to write about a more advanced technique using these signatures. My previous post signed an entire XML document, however, this is not always necessary or even desirable. For instance, if a particular XML document was being authored by several people, you might only want to sign the portions that you worked on.
In this post, I'm going to work with the following XML, signing the elements named signed, but not providing a signature for the unsigned element.
Previous signatures I've shown have all contained only one reference, which referred to the entire document. In order to sign more specific portions of the document, you can sign elements by id. An element's id is any element that contains an 'id', 'Id', or 'ID' attribute. Its important to keep in mind that signing an element will also sign any sub elements.
In order to refer to an element by ID, the reference must be created by passing a string of the form "#id". Here's some sample code that will create references to the signed data from above.
Notice that its perfectly legal to use multiple references in the same signature. This allows me to sign multiple portions of the document with my key. The process of actually signing the document is the same as it was with only one reference. Of course, in reality I would not be generating a random key to sign with, but I would use a key pair that was available for interested parties to find, so that they could verify the signature.
This produces signed XML that looks like the following:
Verifying this signature works exactly the same as verifying the signature over an entire document. You'll also notice that even if you modify the value of the unsigned element, the signature still verifies.
Updated: I needed to be more clear above. "id" and "ID" only work on Whidbey. If you're using v1.0 or 1.1 of the framework, you'll need to use "Id" attributes.