In a SAML flow, the assertion is passed through the user's browser. Because the browser is an untrusted environment, the SP must have a way to guarantee that the assertion was not modified. This is achieved through XML-DSig (Digital Signatures) and XML-Enc (Encryption).
Unlike a simple HMAC, XML-DSig allows for 'partial' signing. The IdP can sign the entire assertion, or just specific elements. The SP uses the IdP's public key (from the metadata) to verify the signature.
The signature is not just a hash; it's a complex structure that defines which parts of the XML were signed (C14N - Canonicalization).
<ds:Signature xmlns:ds="http://www.w3.org/2000/09-10/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="...C14N" />
<ds:SignatureMethod Algorithm="...RSA-SHA256" />
<ds:Reference URI="#_assertion_id">
<ds:DigestMethod Algorithm="...SHA256" />
<ds:DigestValue>aB3...xY9</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>M1z...pL0</ds:SignatureValue>
</ds:Signature>Canonicalization (C14N) is critical. Since XML can have different spacing or attribute orders but remain logically identical, C14N 'standardizes' the XML before hashing it, ensuring the signature doesn't break due to a stray newline.
๐ก Signing provides *Integrity* (it wasn't changed), but Encryption provides *Confidentiality* (it can't be read).
When an assertion is encrypted, the IdP uses the SP's public key to encrypt the data. Only the SP, possessing the corresponding private key, can decrypt and read the user's attributes.
| Feature | SAML Signing | SAML Encryption |
|---|---|---|
| Goal | Integrity & Authenticity | Confidentiality |
| Key Used | IdP Private Key $ o$ SP Public Key | SP Public Key $ o$ SP Private Key |
| Visibility | Content is plaintext, signature is verified | Content is ciphertext, only SP can read |
| Attack Risk | Signature Wrapping (XSW) | Key Leakage |
A critical error is verifying the signature of the *assertion* but not the signature of the *response* (or vice versa), which can lead to assertion injection.
Never use the 'None' algorithm for signatures. Always require a valid, trusted certificate from the IdP metadata.
Verify exercises to earn โ 220 XP and unlock next lab level.