See: Description
Package | Description |
---|---|
uk.me.nxg.xoxa |
Provides facilities for signing, verifying, and before that
normalising, XML
|
uk.me.nxg.xoxa.esis |
Writes out a SAX stream in a format based on the sgmls ESIS output.
|
uk.me.nxg.xoxa.gpg |
Handles the interaction with the GPG program.
|
uk.me.nxg.xoxa.util |
Various utility classes
|
The text below describes the need for a normalised form for XML, describes a normalised form which is simple to generate and is reversible, and which can be signed by GPG in a natural way.
Both normalising and signing XML appear to be hard problems, given the size and complexity of the work of the W3C Signature working group, which has produced recommendations on creating signatures for XML, as well as on the necessary problem of canonicalising XML prior to signature. Normalisation (or canonicalisation) is necessary because for every XML document, there is a set of other documents with trivially different syntax, but which mean ‘the same thing’ – that is, they might use single-quotes rather than double-quotes for marking attributes, or have the attributes in a different order, or have ‘unimportant’ whitespace differences between elements, or appear in a different encoding, while still being usefully regarded as the same document.
The key problem with attempting to sign XML is that all cryptographic signature mechanisms are designed to sign a bag of bytes. XML documents are not just bags of bytes, so there's a fundamental dislocation here between what's wanted and what's available.
One solution is not to normalise at all, but instead to regard the on-disk or on-the-wire XML document as the bag of bytes to be signed. This works, but throws away the mutability of XML, which means that if you want to actually do anything with the XML other than simply admire it, and if you want to round-trip the XML into and out of a system which doesn't know about your signature, you're presented with the dilemma of either abandoning the signature, or else worrying about how to reproduce exactly the same bag of bytes when the XML is serialised at some later stage.
It is part of the point of XML that XML documents are not just bags of bytes, and that there is a well-defined distinction between important content and meaninglessly mutable syntax. XML processors and editors freely take advantage of this, and this mutability is reflected in the fact that applications typically do not operate on the bytes of a document or stream, but instead on the abstracted content of a document, as exposed via an API such as SAX, DOM, or an XSL node-set. An XML database is free to store an XML document in any way it likes, as long as it produces an equivalent document when required.
These notions have been formalised in the concept of the XML Information Set, and the much simpler SAX model (the SAX model is defined in terms of Java, but there are exactly analogous APIs in other languages, which can consequently support the same model). Viewed through a SAX lens, an XML document is a rather simple thing, which is consequently very simple to normalise, serialise and thus sign.
We here define a simple normalisation mechanism, which straightforwardly turns an XML document into a stream of bytes, in a well-defined, streamable and reversible way. The resulting stream can be signed by GPG in a natural way, and the signature embedded into the XML equally naturally.
The simple normalisation turns the XML:
<doc><p class='foo'>Hello</p> <p> there, chum </p> </doc>
into the normalised form:
(doc Aclass foo (p -Hello )p (p -there,\nchum )p )doc
This normalised form can then be signed, and the signature reinserted into the original XML, or else included as the parsed XML is passed downstream.
This Java library provides support for the various steps involved. See guidance on using the library below.
This is a rather aggressive normalisation, meaning that it defines
a large class of XML documents which are equivalent in the sense that
they produce identical normalisations. This normalisation (at least
in this release of the software) may not be appropriate for documents with
mixed XML content (for example ‘text with <em>embedded</em>
markup
'). A future version may either adjust the normalisation, or else
support an additional mode, which is suitable for this case.
You may sign and verify XML documents using the
GPGSigner
and
GPGVerifier
classes, and perform the
normalisation step (for debugging purposes, for example) using the
EsisHandler
class. However the most
usual way of using the library will be using the
SigningXMLReader
class. This is an
implementation of the SAX XMLReader interface, which passes on SAX
events and manages a <?signature?>
processing instruction.
For example, consider the following XML file.
<doc> <p att='foo'> Hello </p> <?signature armor="-----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (Darwin) iEYEABECAAYFAlJVjmkACgkQcZAEv05sGSBJZgCeN3AJzN+Mmp8kcDgcmE14nRAP 8hAAnAl14jR5yAIJoo3edD4LaqtBiJ9+\n=KeDl -----END PGP SIGNATURE----- "?> <p> there </p></doc>
Then the following will verify the signature in the file, passing
the XML content on to the MyHandler
SAX handler. The
verification is insensitive to changes in the whitespace or attribute
quoting within the file.
XMLReader reader = SigningXMLReader.getXMLReader(null); reader.setContentHandler(new MyHandler()); // if you don't set a content-handler, // the reader just throws the results away, // and lets you check the signature. reader.parse(new org.xml.sax.InputSource(new FileReader(myXmlFile))); GPGInformation result = SigningXMLReader.getGPGInformation(reader); assertEquals(GPGInformation.Action.VERIFYING, result.getAction()); assertEquals(GPGInformation.KeyStatus.GOOD, result.getSignatureStatus()); assertEquals("719004BF4E6C1920", result.getSignatureKeyId()); assertEquals(GPGInformation.Validity.ULTIMATE, result.getValidity()); assertEquals(1381338729000L, result.getSignatureDate().getTime());
Copyright © 2014. All rights reserved.