- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
To the uninitiated -- that's just about anybody who doesn't eat, drink, and sleep electronic security -- XML Signature can be very confusing. The recommendation is filled with talk of canonicalization, keystores, digests, hashes, and lots of other stuff that just doesn't come up in everyday programming. The result is that until you see it in action, it can be very hard to put the pieces together into something that seems intelligable.
Fortunately, once you do see it in action, it's not that difficult to get your head around. Here's the basic idea:
You have data of some sort. You want someone to vouch for it. That someone wants to make sure that the data you're looking at is the same data he or she vouched for. XML Signature let's you do that.
Here's how it works:
The person vouching for the data -- let's call that person the "signer" -- has some sort of
certificate to prove that they're really them. It's like when you go to a notary public to sign a document
and she takes a photocopy of your driver's license to make sure you're really you. That
certificate can be any number of things, such as a private key or an X.509 certificate.
The more you're worried about the signer, the more you need to worry about that certificate,
or "key". If you're only worried about making sure the data hasn't changed since it was
signed, the key only has to exist. For example, Java comes with a program called
keytool
that generates certficates.
The signer then takes a program, and signs the data. The process of signing the data creates a "message digest" that refers back to the data and packages it all up. Any change in the data means a change in the digest, so when the recipient -- let's call that person the "verifier" -- gets the data, he or she can check to make sure that the digest still matches the message. If it does, then the verifier knows that the data is intact.
That's the general idea, so let's look at how it actually happens. For this example, I used IBM's XML Security Suite, a set of Java classes that implements a number of XML and Web Services security-related specifications.
First, I had to generate a key to sign the data with. I did that using the keytool.exe
application located in Java's bin
directory:
keytool -genkey -dname "CN=Nick Chase, OU=InformIT, O=Pearson, L=NPR, S=Florida, C=US" -alias nick -storepass mystorepassword -keypass mykeypassword
Next I created a very simple XML document to sign:
<theroot> <thedata>data!</thedata> </theroot>
Next I used one of the sample applications, dsig.SampleSign2
, to actually
sign the data:
java -cp .;xml4j.jar;xss4j.jar;are.jar dsig.SampleSign2 nick mystorepassword mykeypassword -embxml stuff.xml > signature.xml
The jar files are part of the XML Security Suite distribution, and the SampleSign2
application
takes the id for the certificate and the certificate passwords to start with, and then the resource (or
resources) to sign, finally sending the to the file signature.xml. In this case, I've chosen to embed
the resource right into the signature file, but it's also possible to have external resources.
If we look at the signature.xml
file, we can see it all come together:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> <SignedInfo> <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></CanonicalizationMethod> <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1"></SignatureMethod> <Reference URI="#Res0"> <Transforms> <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></Transform> </Transforms> <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></DigestMethod> <DigestValue>psLjBQtzI7t7wiozRfLoNdaIb08=</DigestValue> </Reference> </SignedInfo> <SignatureValue> ZimJc+beK3HLLlpPcxh0kvBIPYB6YQs+as6SqXID4pY0tyF6qWQ3KQ== </SignatureValue> <KeyInfo> <KeyValue> <DSAKeyValue> <P> /X9TgR11EilS30qcLuzk5/YRt1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9s ubVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQIsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bT xR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZndFIAcc= </P> <Q>l2BQjxUjC8yykrmCouuEC/BYHPU=</Q> <G> 9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFn Ej6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTx vqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSo= </G> <Y> HPnTW4UAvR4MIYOqym1zdLFlzNREQpKCxOMbiSvD04es7RONiBECT6OtlMWAZ9k/9N gOK/HmxObSC5inAjXN2omNZzBburQuJAmRs/G2nwRp49yLRNHWq1922jCYoZ6iFmNA iraKSIOYTS1LnfGEEgl7zNc/XDsArRtZ+LBdrZo= </Y> </DSAKeyValue> </KeyValue> <X509Data> <X509IssuerSerial> <X509IssuerName>CN=Nick Chase,OU=InformIT,O=Pearson,L=New Port Richey,ST=Florida,C=US</X509IssuerName> <X509SerialNumber>1089899916</X509SerialNumber> </X509IssuerSerial> <X509SubjectName>CN=Nick Chase,OU=InformIT,O=Pearson,L=New Port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CFCKEwGg3Ah8jEBum+rbhmrvo3xp/ </X509Certificate> </X509Data> </KeyInfo> <dsig:Object xmlns="" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" Id="Res0"><theroot> <thedata>data!</thedata> </theroot></dsig:Object> </Signature>
Starting at the bottom, the file defines the object that I've signed, including
it in the document and assigning it an ID of Res0
so we can refer back to it later.
Above that, we have the KeyInfo
element, which includes information about
the keys and certificates I used to sign the document. Next up, we have the
actual signature. Finally, above that, we have the information on what was actually
signed.
This last bit takes a bit more looking into, so let's repeat it here:
<SignedInfo> <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></CanonicalizationMethod> <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1"></SignatureMethod> <Reference URI="#Res0"> <Transforms> <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></Transform> </Transforms> <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></DigestMethod> <DigestValue>psLjBQtzI7t7wiozRfLoNdaIb08=</DigestValue> </Reference> </SignedInfo>
First off, you have to remember that this process is extremely sensitive to changes in the document. For example, just changing the spacing in an XML document is still a change, and the signature would be invalid. Therefore, before signing a document, you need to make sure it's in "canonical" format, so here we're specifying what needs to be done to the document, and how to do it. We're also specifying the type of signature we're dealing with.
Next we've got the actual resource being signed, described by the Reference element. In this case, the data's embedded in the signature file, so we're pointing to it through the URL fragment #Res0, which matches the object ID for the data. From there we're specifying what we did with the data -- the transform and the method for creating the digest -- and then the digest itself.
Before I move on to verifying this file, note that you can also sign external resources. For example:
... <SignedInfo> <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></CanonicalizationMethod> <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1"></SignatureMethod> <Reference URI="http://www.nicholaschase.com/testsoap.php"> <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></DigestMethod> <DigestValue>pSw2lraC6kX9m85GUwG0a/+p00c=</DigestValue> </Reference> </SignedInfo> ...
In this case, the data is not included as part of the signature file.
Finally, you want to verify the data to make sure it's intact. The XML Security Suite has a sample called
VerifyGUI
that verifies the signature. This application takes the data and performs the same
canonicalization and transforms on it, then calculates a digest. If the digest matches what's in the signature
file, it passes. If not, it returns a message saying so. Don't believe me? Create a signature of your own,
change one caracter of the actual data, and run VerifyGUI
. You'll see.
So that's it. I have simplified some things in this explanation, but this is what you need to get you started. Check out the resources, or ask a question in the comments for the XML Reference Guide to find out more.