<div dir="ltr"><div><div><div><div><div>Hi,<br><br></div><div>We are using Shibboleth as IdP, and OpenSaml for SP. <br><br></div><div>1. We turned on Encryption in IdP in relying-party.xml for our SP<br></div><div>2. SP supplies it&#39;s public key in its metadata<br></div><div>3. IdP is encrypting using the public key in SP&#39;s metadata(Sample assertion below).<br></div><div>4. While decrypting at SP, we are using the private key for the public key in SP metadata(code below)<br></div><div><br></div><div>The decryption fails with the following Exception:<br><br>ERROR org.opensaml.xml.encryption.Decrypter  - Error decrypting the encrypted data element<br>org.apache.xml.security.encryption.XMLEncryptionException: Invalid AES key length: 1218 bytes<br><br></div><div>My Cryptography knowledge is limited, so pardon me if I&#39;m wrong. WikiPedia says AES is symmetric key algorithm. If IdP uses public key for encryption, why does org.opensaml.xml.encryption.Decrypter try to decrypt using AES and fails?<br></div><div><br></div>---------------- Encrypted Assertion from IdP-------------------------<br></div><div><br>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;saml2p:Response xmlns:saml2p=&quot;urn:oasis:names:tc:SAML:2.0:protocol&quot; Destination=&quot;<a href="http://myhost:8080/saml.jsp">http://myhost:8080/saml.jsp</a>&quot; ID=&quot;_b5fc7c6e0aee49f7f8cae692d4f95300&quot; InResponseTo=&quot;1421769637471&quot; IssueInstant=&quot;2015-01-20T16:00:37.504Z&quot; Version=&quot;2.0&quot;&gt;<br>  &lt;saml2:Issuer xmlns:saml2=&quot;urn:oasis:names:tc:SAML:2.0:assertion&quot; Format=&quot;urn:oasis:names:tc:SAML:2.0:nameid-format:entity&quot;&gt;<a href="https://idp.mycompany.com/idp/shibboleth">https://idp.mycompany.com/idp/shibboleth</a>&lt;/saml2:Issuer&gt;<br>  &lt;saml2p:Status&gt;<br>    &lt;saml2p:StatusCode Value=&quot;urn:oasis:names:tc:SAML:2.0:status:Success&quot;/&gt;<br>  &lt;/saml2p:Status&gt;<br>  &lt;saml2:EncryptedAssertion xmlns:saml2=&quot;urn:oasis:names:tc:SAML:2.0:assertion&quot;&gt;<br>    &lt;xenc:EncryptedData xmlns:xenc=&quot;<a href="http://www.w3.org/2001/04/xmlenc#">http://www.w3.org/2001/04/xmlenc#</a>&quot; Id=&quot;_52920151365a097df61102971ab20384&quot; Type=&quot;<a href="http://www.w3.org/2001/04/xmlenc#Element">http://www.w3.org/2001/04/xmlenc#Element</a>&quot;&gt;<br>      &lt;xenc:EncryptionMethod xmlns:xenc=&quot;<a href="http://www.w3.org/2001/04/xmlenc#">http://www.w3.org/2001/04/xmlenc#</a>&quot; Algorithm=&quot;<a href="http://www.w3.org/2001/04/xmlenc#aes128-cbc">http://www.w3.org/2001/04/xmlenc#aes128-cbc</a>&quot;/&gt;<br>      &lt;ds:KeyInfo xmlns:ds=&quot;<a href="http://www.w3.org/2000/09/xmldsig#">http://www.w3.org/2000/09/xmldsig#</a>&quot;&gt;<br>        &lt;xenc:EncryptedKey xmlns:xenc=&quot;<a href="http://www.w3.org/2001/04/xmlenc#">http://www.w3.org/2001/04/xmlenc#</a>&quot; Id=&quot;_557c37d85f9e2048780ac25e3b90bf39&quot;&gt;<br>          &lt;xenc:EncryptionMethod xmlns:xenc=&quot;<a href="http://www.w3.org/2001/04/xmlenc#">http://www.w3.org/2001/04/xmlenc#</a>&quot; Algorithm=&quot;<a href="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p</a>&quot;&gt;<br>            &lt;ds:DigestMethod xmlns:ds=&quot;<a href="http://www.w3.org/2000/09/xmldsig#">http://www.w3.org/2000/09/xmldsig#</a>&quot; Algorithm=&quot;<a href="http://www.w3.org/2000/09/xmldsig#sha1">http://www.w3.org/2000/09/xmldsig#sha1</a>&quot;/&gt;<br>          &lt;/xenc:EncryptionMethod&gt;<br>          &lt;ds:KeyInfo&gt;<br>            &lt;ds:X509Data&gt;<br>              &lt;ds:X509Certificate&gt;.... Certificate Elided...&lt;/ds:X509Certificate&gt;<br>            &lt;/ds:X509Data&gt;<br>          &lt;/ds:KeyInfo&gt;<br>          &lt;xenc:CipherData xmlns:xenc=&quot;<a href="http://www.w3.org/2001/04/xmlenc#">http://www.w3.org/2001/04/xmlenc#</a>&quot;&gt;<br>            &lt;xenc:CipherValue&gt;..544 characters elided..&lt;/xenc:CipherValue&gt;<br>          &lt;/xenc:CipherData&gt;<br>        &lt;/xenc:EncryptedKey&gt;<br>      &lt;/ds:KeyInfo&gt;<br>      &lt;xenc:CipherData xmlns:xenc=&quot;<a href="http://www.w3.org/2001/04/xmlenc#">http://www.w3.org/2001/04/xmlenc#</a>&quot;&gt;<br>        &lt;xenc:CipherValue&gt;..5632 characters elided...&lt;/xenc:CipherValue&gt;<br>      &lt;/xenc:CipherData&gt;<br>    &lt;/xenc:EncryptedData&gt;<br>  &lt;/saml2:EncryptedAssertion&gt;<br>&lt;/saml2p:Response&gt;<br><br></div><div>------------------ End of Assertion ------------------------------------------------<br></div><div><br></div>----------- Decryption code at SP ---------------------------------------------<br><br>FileInputStream fin = new FileInputStream(configuration.getTrustStore());<br>KeyStore keystore = KeyStore.getInstance(keyStoreType);<br>keystore.load(fin,null);<br>trustedCredentials = new ArrayList&lt;Credential&gt;();<br>Enumeration&lt;String&gt; aliases = keystore.aliases();<br>KeyStore.PasswordProtection passwordProt = new KeyStore.PasswordProtection(&quot;mypassword&quot;.toCharArray());<br>//Add all certificates from trust store<br>while(aliases.hasMoreElements())<br>{<br>        alias = aliases.nextElement();<br>        if(alias!=null &amp;&amp; !alias.isEmpty()) {<br>             X509Certificate cert = (X509Certificate) keystore.getCertificate(alias);<br>             //keystore.<br>            BasicX509Credential otherCred1 = new BasicX509Credential();<br>            otherCred1.setEntityCertificate(cert);<br>            otherCred1.setEntityId(entityID);<br></div><div>            if(keystore.isKeyEntry(alias)) {<br>                        KeyStore.Entry ksEntry = keystore.getEntry(alias,passwordProt);<br>                          if(ksEntry instanceof KeyStore.PrivateKeyEntry) {<br>                              otherCred1.setPrivateKey(((KeyStore.PrivateKeyEntry) ksEntry).getPrivateKey());<br>                          }<br>                        }<br></div><div>            <br>             trustedCredentials.add(otherCred1);<br>        }<br>}<br><br> List&lt;EncryptedAssertion&gt; encAssertions = response.getEncryptedAssertions();<br>        <br>if(encAssertions!=null &amp;&amp; !encAssertions.isEmpty())<br>{<br>    StaticKeyInfoCredentialResolver skicr = new StaticKeyInfoCredentialResolver(trustedCredentials);<br>    Decrypter samlDecrypter = new Decrypter(skicr,null,null);<br>    //samlDecrypter.setRootInNewDocument(true);<br>    if(assertions==null) assertions = new ArrayList&lt;Assertion&gt;();<br>    try<br>    {<br>        for(EncryptedAssertion encAssertion: encAssertions)<br>        {<br>            Assertion decAssertion = samlDecrypter.decrypt(encAssertion);<br>            assertions.add(decAssertion);<br>        }<br>    }catch(DecryptionException e)<br>    {<br>        fLogger.error(&quot;Error while decrypting assertion&quot;,e);<br>    }<br>}<br><br></div><div>-------------- End of decryption code-------------------------<br></div></div><br></div><br><div><div>Thank you,<br></div><div>Sundeep<br></div><div><div><br><br></div></div></div></div>