Skip to content
Ashish.
All posts
Diagram illustrating the flow of SAML assertions with XML Signature and XML Encryption layers.

Securing SAML Assertions: XML Sig & Encryption

An examination of securing SAML assertions using XML signature and encryption to ensure data integrity and confidentiality.

By Ashish KumarPart 6 of SAML Mastery Series

Transport layer security protocols like TLS 1.3 secure the communication channel between an Identity Provider (IdP) and a Service Provider (SP), but they do not protect the SAML assertion once it exits that tunnel. If an attacker intercepts the HTTP POST containing the assertion and modifies the <Subject> element from alice@example.com to admin@example.com before the SP validates it, the integrity of the authentication event is compromised. SAML assertions are XML documents, and XML is inherently mutable. To prevent this, the protocol relies on two distinct, complementary mechanisms defined by the W3C: XML Signature for integrity and XML Encryption for confidentiality. These are not optional add-ons; they are the structural components that define a valid SAML assertion.

The Mechanism of Integrity: XML Signature

The XML Signature specification (XMLDSig) does not simply "hash" the document. It creates a cryptographic binding between the assertion content and a digital signature using a specific sequence of transforms and references. When an IdP signs an assertion, it does not sign the entire XML tree as a single blob. Instead, it constructs a <ds:Signature> element that contains one or more <ds:Reference> elements. Each reference points to a specific part of the assertion (usually the <saml:Assertion> root) via a URI.

The mechanism works in three steps during validation. First, the SP extracts the <ds:Reference> and follows the URI to locate the target node. Second, the SP applies the specified <ds:Transforms>. This is critical. A common transform is http://www.w3.org/2001/10/xml-exc-c14n#, which canonicalizes the XML. Canonicalization removes insignificant whitespace, normalizes line endings, and resolves entity references, ensuring that the byte stream presented to the hash function is identical regardless of how the XML was formatted by the IdP's serializer. Without this step, a valid signature could be rejected because the IdP added a newline or the SP stripped one. Third, the SP applies the <ds:DigestMethod> (e.g., sha256) to the canonicalized data to produce a digest, then compares this digest against the value stored in <ds:DigestValue>. If they match, the content has not changed since the hash was calculated. Finally, the SP verifies the <ds:SignatureValue> using the IdP's public key to ensure the digest was indeed signed by the holder of the private key.

<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="#assertion-id-123">
      <ds:Transforms>
        <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>Q0qG... (base64 encoded)</ds:DigestValue>
    </ds:Reference>
  </ds:SignedInfo>
  <ds:SignatureValue>...</ds:SignatureValue>
  <ds:KeyInfo>
    <ds:X509Data>
      <ds:X509Certificate>MIIDXTCCAkWgAwIBAg...</ds:X509Certificate>
    </ds:X509Data>
  </ds:KeyInfo>
</ds:Signature>

This structure allows for selective signing. An IdP might sign only the <saml:Subject> and <saml:Conditions> but leave the <saml:AttributeStatement> unsigned if those attributes are considered low-risk or if the IdP delegates attribute issuance to a third party. However, for a strong security posture, the entire assertion should be signed. The SP must validate the signature before processing any claims within the assertion. If the signature is missing, invalid, or has been altered, the assertion must be rejected immediately.

The Mechanism of Confidentiality: XML Encryption

While signatures ensure the message hasn't changed, they do not hide the content. SAML assertions often contain sensitive PII (Personally Identifiable Information) like email addresses, roles, or group memberships. If an attacker gains access to the log files of a load balancer or a network sniffer captures the traffic, they can read these attributes. XML Encryption (XMLEnc) solves this by encrypting specific parts of the XML document rather than the whole packet.

The mechanism uses a hybrid approach. The IdP generates a random symmetric session key (e.g., AES-256-CBC) to encrypt the actual assertion data. The IdP encrypts this session key using the SP's public key (asymmetric encryption, usually RSA or ECDSA) and places the result inside an <xenc:EncryptedKey> element. The encrypted assertion data itself resides in an <xenc:EncryptedData> element. The SP receives the XML, locates the <xenc:EncryptedData>, and uses its private key to decrypt the <xenc:EncryptedKey> to recover the symmetric session key. With the session key, the SP decrypts the assertion payload.

This separation of concerns is vital for performance. Symmetric encryption is orders of magnitude faster than asymmetric encryption. By generating a unique session key for every assertion, the system ensures that even if the same assertion is sent twice, the resulting ciphertext is completely different, preventing replay analysis. Furthermore, the encryption can be scoped. An IdP might encrypt only the <saml:AttributeStatement> while leaving the <saml:Subject> and <saml:Conditions> in plaintext so the SP can route the request before decryption is required.

<saml:Assertion MajorVersion="2" MinorVersion="1" ... ID="_id123" ...>
  <saml:Subject>
    <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">alice@example.com</saml:NameID>
  </saml:Subject>
  <xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element">
    <xenc:CipherData>
      <xenc:CipherValue>gY... (base64 encoded ciphertext)</xenc:CipherValue>
    </xenc:CipherData>
  </xenc:EncryptedData>
  <xenc:EncryptedKey>
    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
    <ds:KeyInfo>
      <ds:X509Data>
        <ds:X509Certificate>...SP Public Key...</ds:X509Certificate>
      </ds:X509Data>
    </ds:KeyInfo>
    <xenc:CipherData>
      <xenc:CipherValue>...Symmetric Key Encrypted with SP Public Key...</xenc:CipherValue>
    </xenc:CipherData>
  </xenc:EncryptedKey>
</saml:Assertion>

The SP must have the corresponding private key and the correct certificate chain to perform this decryption. If the IdP encrypts to the wrong public key (e.g., a key from a previous rotation that hasn't been revoked), the SP will fail to decrypt, and the login will fail. This highlights the dependency on robust certificate management.

Technical diagram showing the hybrid encryption process of a SAML assertion. The image should depict a symmetric key wrapping a payload, and that symmetric key being wrapped by an asymmetric public key envelope. Clean, vector-style architecture sketch with blue and grey tones.

Operational Reality: Certificate Management and Algorithm Selection

The theoretical mechanisms described above rely on a foundation of trust that is often the weakest link in practice: certificate management. The W3C XML Signature specification allows for a wide range of algorithms, including RSA-SHA1, RSA-SHA256, ECDSA-SHA256, and HMAC-SHA256. In a production environment, the choice of algorithm is a tradeoff between compatibility and security. SHA-1 is cryptographically broken for collision resistance and is deprecated in modern SAML configurations. Using RSA-SHA1 for signing assertions invites forgery attacks. Similarly, using RSA with small key sizes (e.g., 1024-bit) is no longer sufficient against modern computational power.

Opinion: While many legacy systems still default to RSA-SHA1 for backward compatibility, this is a severe security risk that should be mitigated by configuring the IdP and SP to reject such assertions at the gateway level, not at the application logic level.

Certificate rotation is another critical operational detail. When an IdP rotates its signing certificate, the SP must update its trust store. If the SP still holds the old public key, it will fail to verify the signature of new assertions. Conversely, if the IdP stops signing with the old key but the SP still trusts it, an attacker who compromised the old private key (or a rogue SP) could issue valid-looking assertions. The standard practice involves a "grace period" where both the old and new certificates are trusted simultaneously. The IdP signs with the new key, but the SP accepts signatures from either key until the old one is retired. This transition must be coordinated to avoid service outages.

The Attack Surface: Signature Wrapping

Even with correct signature and encryption mechanisms, implementation flaws can lead to devastating vulnerabilities. A prime example is the XML Signature Wrapping Attack (XSWA). This attack exploits the fact that XML parsers can handle multiple elements with the same name. An attacker takes a valid, signed assertion and inserts a second, malicious element with the same tag name (e.g., a second <saml:Subject>) into the document.

Consider a scenario where the IdP signs an assertion containing a <saml:Subject> for alice. The attacker intercepts this and injects a new <saml:Subject> for admin after the original one. The XML parser on the SP side, depending on its configuration, might process the first <saml:Subject> it encounters (the legitimate one) or the last one (the malicious one). If the SP processes the last one, it grants admin access. Crucially, the signature remains valid because the attacker did not modify the original signed <saml:Subject> node. The vulnerability arises because the <ds:Reference> scope often includes the entire assertion root, allowing the parser to be tricked into selecting the injected malicious node over the signed one.

To prevent XSWA, the SP must implement strict validation rules. It should expect exactly one instance of critical elements like <saml:Subject> and reject assertions with duplicates. Furthermore, the SP must ensure that the signature covers the entire relevant portion of the document, not just a fragment that can be easily augmented. The <ds:Reference> URI should point to the root of the assertion, and the SP should verify that no extra nodes exist between the signed content and the signature itself. This requires deep parsing logic that goes beyond simple XML validation.

Conclusion

Securing SAML assertions is not a configuration toggle; it is a rigorous engineering discipline involving the precise application of XML Signature and XML Encryption. The integrity of the assertion depends on the correct canonicalization and hashing of the document content, while confidentiality relies on the proper management of hybrid encryption keys. These mechanisms are only as strong as the certificate lifecycle management that supports them and the strict validation logic that prevents parsing-based attacks like signature wrapping. In an identity federation landscape where trust is paramount, understanding these underlying mechanisms is the only way to ensure that the authentication event remains untampered and unreadable by unauthorized parties.

Common Pitfalls

  1. Relying on Transport Security Alone: Assuming TLS protects the assertion content after it leaves the secure channel. SAML assertions must be signed and encrypted independently of the transport layer.
  2. Insecure Algorithm Defaults: Leaving systems configured to accept RSA-SHA1 or weak key sizes (e.g., 1024-bit RSA) simply because older documentation suggests it, exposing the system to collision and brute-force attacks.
  3. Missing Duplicate Element Checks: Failing to validate that critical elements like <saml:Subject> appear exactly once, which allows Signature Wrapping Attacks to succeed by injecting duplicate elements that parsers may process differently.

Practical Takeaways

  • Enforce Strong Algorithms: Configure IdPs and SPs to reject SHA-1 and require SHA-256 or stronger for signatures, and use at least 2048-bit RSA or ECDSA keys.
  • Validate Element Uniqueness: Implement strict parsers that reject assertions containing multiple instances of critical elements like <saml:Subject> or <saml:Conditions>.
  • Manage Certificates Proactively: Establish a clear rotation policy with a grace period to ensure SPs and IdPs can transition between keys without service interruption or security gaps.

FAQ

Q: Can XML Encryption protect against a Signature Wrapping Attack? A: No. Encryption protects confidentiality, not integrity or parsing logic. An attacker can inject malicious nodes into an encrypted payload or manipulate the structure in ways that bypass signature validation regardless of whether the content is encrypted.

Q: Why is canonicalization necessary if I am using a strong hash algorithm? A: Hash functions are deterministic based on byte sequences. Without canonicalization (C14N), minor formatting differences like whitespace or line endings between the IdP's serialization and the SP's parsing would result in different byte streams, causing valid signatures to fail validation.

Q: Is it safe to sign only specific parts of a SAML assertion? A: While technically possible (e.g., signing only the Subject), it is generally discouraged for high-security environments. Selective signing can leave other critical data (like attributes) vulnerable to tampering. Signing the entire assertion root is the recommended practice.

Related posts