Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

XML and XML signatures for identity

Many identity messages are XML documents, and an XML signature can protect one part of a document while the rest stays outside it. That flexibility is where most of XML's security problems come from.

Why identity still speaks XML

The printing service that Ava connects to her photos has reported each print order's status to Lab Photos for years, long before JSON became the usual format for web APIs. Its messages are written in XML, the Extensible Markup Language, and so are many of the sign-in messages that large organizations exchange every day. SAML, the federation standard that enterprise single sign-on grew up on, is XML from end to end, and so is the metadata that describes its participants.

The ideas from Digital signatures carry over unchanged: find the trusted key, verify the exact bytes, then check that the content fits this use. What changes is the format around them.

Reading an XML document

Here is a fictional status report for one of Ava's print orders, before anything is signed:

<?xml version="1.0" encoding="UTF-8"?>
<p:Order xmlns:p="urn:example:printer:orders" ID="order-7f3c21" Customer="ava">
  <p:Item>20 prints, matte</p:Item>
  <p:Status>ready</p:Status>
</p:Order>

The building blocks are elements, written as a start tag and an end tag around their content, such as <p:Status>ready</p:Status>. Elements nest inside one another, so the whole document is a tree with one outermost element, here Order. An element's start tag can also carry attributes, short named values such as ID="order-7f3c21". The first line is a declaration that names the XML version and character encoding; it is not part of the tree.

The p: in front of each name is a prefix, and it stands for a namespace. The attribute xmlns:p="urn:example:printer:orders" declares that, inside this element, p: means the vocabulary named by that URI. Namespaces let documents mix vocabularies without their names colliding: a SAML response contains SAML protocol elements, SAML assertion elements and XML Signature elements, each from its own namespace. The prefix is only a local abbreviation. <x:Status xmlns:x="urn:example:printer:orders"> names exactly the same element, while <p:Status> under a different URI names a different one. Software that finds elements by their prefix, or by the local name alone, can be pointed at the wrong element.

Programs usually find elements with XPath, a small query language for XML trees. // means anywhere in the document, so //* is every element, while a single / steps down to an element's children; a condition in square brackets filters them, so //*[@ID='order-7f3c21'] is the element whose ID attribute has that value, and (//*)[1] is the first match. The functions local-name() and namespace-uri() give an element's name without its prefix and the URI its prefix stands for, so a query can match both together, as a careful program does.

One attribute matters more than the rest. The ID on Order gives the element a name that other parts of a document can refer to, and that is how an XML signature says what it covers.

Signing part of a document

A JSON Web Signature signs its whole encoded header and payload, as Digital signatures described. An XML signature, defined by the W3C's XML Signature standard, instead points at the content it protects. When the printing service signs its report, it adds a Signature element inside it:

<p:Order xmlns:p="urn:example:printer:orders" ID="order-7f3c21" Customer="ava">
  <p:Item>20 prints, matte</p:Item>
  <p:Status>ready</p:Status>
  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:SignedInfo>
      <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
      <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
      <ds:Reference URI="#order-7f3c21">
        <ds:Transforms>
          <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
          <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
        </ds:Transforms>
        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <ds:DigestValue>n90AxOdeu6Fh5INpKA+NPLUmlPSSV/jRLLS9HyauZII=</ds:DigestValue>
      </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>Ko3m7Hqv...</ds:SignatureValue>
    <ds:KeyInfo>
      <ds:X509Data><ds:X509Certificate>MIIB...</ds:X509Certificate></ds:X509Data>
    </ds:KeyInfo>
  </ds:Signature>
</p:Order>

Read it from the inside out. The Reference names what is protected: URI="#order-7f3c21" means the element whose ID is order-7f3c21. The printing service took that element, applied the listed transforms, hashed the result with SHA-256 and wrote the hash, base64 encoded, into DigestValue. Everything it intends to protect is now summarized in SignedInfo, and SignatureValue is its signature over SignedInfo itself. The private key never signs the order directly; it signs the list of digests.

The first transform solves a puzzle. The Signature sits inside the very element it protects, and adding it changed that element, so a hash taken now could never match a hash taken before. The enveloped signature transform says: before hashing, remove this Signature element. The verifier does the same and gets back the element exactly as the signer hashed it.

A verifier reverses the steps. It checks SignatureValue over SignedInfo with a key it already trusts for the printing service, then, for each reference, finds the element the URI names, repeats the transforms, hashes the result and compares it with DigestValue. Only when both match has it established that this element came from the printing service unchanged. KeyInfo carries a certificate as a hint about the signing key, in the same spirit as a key identifier: the verifier compares it with the key it already trusts for this signer, never trusting it simply because it arrived with the message.

Canonicalization

The second transform deals with a problem that a JSON Web Signature avoids by signing fixed encoded bytes. The same XML can be written in many ways that every XML parser treats as equal: attributes in a different order, single quotes instead of double, extra spaces inside a tag, an empty element written as <a/> or <a></a>, or a namespace declared on a different element. XML documents are routinely parsed and written out again as they pass between systems, so the bytes that arrive are often not the bytes that were signed.

Canonicalization rewrites XML into one standard form before hashing, so that equivalent documents produce the same bytes and the same digest. It sorts attributes, uses double quotes, expands empty elements, removes the declaration and the spaces inside tags, and handles namespace declarations by fixed rules. The exclusive variant named in the example, written exc-c14n, keeps only the namespace declarations the signed element actually uses, so the element can be moved into another document without breaking its signature, which SAML relies on.

Canonicalization changes form, never content. Text inside elements is kept exactly, so ready and Ready, or a value with a trailing space, still produce different digests. So is the whitespace between elements, such as the line breaks and indentation before each child. That is the point: anything that could change what the document means must change the hash.

Why parser settings matter

Before any signature is checked, the receiver has to parse the document, and XML parsers can do more than read tags. An XML document may begin with a document type definition (DTD), which can declare entities: names that the parser replaces with other text wherever they appear. An entity can be defined as fixed text, or as the contents of a file or web address that the parser fetches while reading.

That second kind is called an external entity, and a parser that resolves external entities from untrusted input has an XML external entity (XXE) weakness. Someone who controls the document can make the receiver's own parser read files from the receiver's server or send requests from inside its network, and some of what it reads may come back in an error message or a response. Entities that expand into other entities can also multiply a small document into gigabytes of text and exhaust the receiver's memory.

None of this depends on a signature, because parsing happens first. The defense is in the parser's configuration: refuse documents that contain a DTD at all, which SAML messages never need, and never resolve external entities or fetch network resources while parsing. Many XML libraries now ship safe defaults, but some older ones and some configuration options do not, which is why the SAML lessons insist on a maintained library with entity resolution turned off.

When the signed part is not the part you read

Because a signature points at an element instead of covering the whole document, a verifier can answer a narrower question than its application asked. "Is there a valid signature somewhere in this document?" is not the same question as "Is the element I am about to use the one that was signed?"

Suppose Lab Photos' code verifies the signature, which locates the signed element through its ID, and then separately reads the status with a query such as "the first Order element in the document". Those are two different ways of finding an element. If a document arrives holding the genuine signed order and also a second, unsigned Order placed where the query looks first, the signature check succeeds on the genuine element while the application reads the other one. This family of problems is called XML signature wrapping. It is possible because XML separates where a signature points from where the content sits, and it has affected widely used SAML libraries.

Two Order elements

First Order
order-decoy: cancelled, unsigned
Second Order
order-7f3c21: ready, signed

Reads the first Order

Signature check
Valid, for order-7f3c21
Element read
order-decoy
Status used
cancelled

Acts on an unsigned order

Uses the verified element

Signature check
Valid, for order-7f3c21
Element read
Only the one verification covered
Structure
Two Order elements: refused

Uses no unsigned data

A duplicate ID would be refused the same way.

The signature check and the application can look at different elements of the same document. A safe receiver reads only the element verification covered.

The defenses follow from the cause. The receiver uses the element that verification actually covered, handed over by the verification step, rather than looking it up again. It refuses documents with duplicate ID values or unexpected structure, checks that the signed element is the one its protocol expects in the expected position, matches elements by namespace and local name together, and requires the signature to cover what it will use. A well-maintained SAML library does these things, and an application that reads the document itself afterwards has to keep to the same element.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

An integration service passes a signed XML order status along after rewriting it: each start tag now lists its attributes in a different order, in single quotes. The receiver verifies the signature with exclusive canonicalization. What happens? 1 of 3

Choose one answer.

A service verifies an XML signature whose reference points at the order with ID order-7f3c21, then reads the status from the first Order element it finds in the document. Why is that unsafe? 2 of 3

Choose one answer.

A receiver checks XML signatures carefully, but its parser resolves external entities. A document arrives whose DTD defines an entity that points at a file on the receiver's server. What protects the receiver? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity