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




3 comments:

  1. Hi Stefan,

    I have bought your book and with the help of that , i could integrate my web application using openSaml version 3.

    Now i have a different use case to implement , my company has a desktop application implemented in .Net , we want to enable SAML based sso for this application as well , i googled various sources but neither helped me . So we have arrived at our ow n idea to implement this.

    With the help of the already implemented web app , we are going to open an embedded browser from desktop which calls the url in which we create SAML request , once the IDP responds back with assertions we will return an loginid to the desktop app to authenticate. I will use the same saml request servlet and saml response consumer servlet for both web app and desktop , but for desktop i will redirect to another servlet which writes loginid in servlet response. So basic thing i need is a way to send a saml resuest with an identifier identifies application and the same identifier responed back by IDP in Saml response ( may be SAML request with assertion attribute and the same returned by IDP in saml response)


    WebAPP --> SAML request xml (without identifier )--> SAML repsonse(without identifier) <--IDP


    Desktop --> WebApp --> (SAML request xml (with identifier as from desktop ) --> SAML response ((with identifier as from desktop) <--- IDP

    Here the identifier may be custom assertion/any saml request attribute which can be send as it is from IDP in response

    ReplyDelete
    Replies
    1. Sound like you could set a RelayState parameter on the SAML request URL going out. This should be echoed back in the response.

      However you should think twice on you acrobatics with the second servlet where you take in a assertion and return user id in the servlet response.

      If you blindly trust what is returned in the response it might be easy to by pass the authentication by just inserting another user id.

      Delete
  2. Thanks Stefan ,I will try relaystate and let you know. By the way the loginid mentioned is not user ID it's a n encrypted id which includes various other parameters which is specific to application

    ReplyDelete