Saturday, May 20, 2017

Decrypting a SAML Assertion in OpenSAML v3

As you probably should know at this point, the SAML Assertion contains the description of a authenticated user and how it was autheunticitoted. The assertion is generally signed to prevent manipulation but there are also cases where you would want to encrypt the assertion to prevent someone to see the information within. For example the assertion can be used to communicate personal information about the user such as social security number and adress.

As most people reading my blog seem to be on the SP side of SAML I will explain how to decrypt an assertion.

The general method of encryption and subsequent decryption of a assertion uses two keys, one symetric and on asymetric.
The symtric key is generated during encryption and used to encrypt the actual assertion data. A  asymetric key, generally a SP key contained in the metadata, is then used to encrypt the symetric key which is then stored in the XML together with the ecrypted assertion.

TestIDP




TestIDP












THE ENCRYPTED SYMMETRIC KEY




THE ASSERTION ENCRYPTED USING THE SYMMETRIC KEY





To decrypt the reverse of this is done. The asymmetric is used to decrypt the symmetric key, which is then used to decrypt the assertion.

Now for the OpenSAML part.

First we get the assertion from the XML. The assertion is stored in a EncryptedAssertion object and is retrieved with the method getEncryptedAssertions() on the response instead of getAssertions() which is used otherwise.

StaticKeyInfoCredentialResolver keyInfoCredentialResolver = new StaticKeyInfoCredentialResolver(SPCredentials.getCredential());

Decrypter decrypter = new Decrypter(null, keyInfoCredentialResolver, new InlineEncryptedKeyResolver());
decrypter.setRootInNewDocument(true);

decrypter.decrypt(encryptedAssertion);

In this example a StaticKeyInfoCredentialResolver is used to point out the key that is used for decryption, in this case I have stored it in the static class SPCredentials. The InlineEncryptedKeyResolver is used to tell the decryptor that the symmetric encryption key can be found in the XML next to the encrypted assertion.

The decryptor returns a decrypted Assertion object.


TestIDP












UDJ762cOSK14nBjbFCq9a47x3WgSCJ1qZpeBYKdy5s0=



syb20Rjh1pDJTc6CY1nN3x1v8WNlG9lRf71A5ik3i97/A+q0NZCvoJLEBVM73K/U8hxo/hbHOmBs
7KkF4g3yYLVunRlm/tapnTESKqZ8v8SJZQcbY4xMzi0PUxiUJKBugiho114ijDPAYs+U1UZhXGHL
zyHVd7XJYoOATKVdh3U=



Some NameID value






http://localhost:8080/webprofile-ref-project/sp/consumer




bob


999999999




urn:oasis:names:tc:SAML:2.0:ac:classes:Smartcard




No comments:

Post a Comment