SAML EncryptedAssertion Decryption failing

2019-03-05 15:08发布

问题:

I am implementing a SP using SAML. When I am trying to decrypt the EncryptedAssertion I am getting the below error.

org.opensaml.xml.encryption.DecryptionException: Failed to decrypt EncryptedData
at org.opensaml.xml.encryption.Decrypter.decryptDataToDOM(Decrypter.java:546)
at org.opensaml.xml.encryption.Decrypter.decryptDataToList(Decrypter.java:453)
at org.opensaml.xml.encryption.Decrypter.decryptData(Decrypter.java:414)
at org.opensaml.saml2.encryption.Decrypter.decryptData(Decrypter.java:141)
at org.opensaml.saml2.encryption.Decrypter.decrypt(Decrypter.java:69)

I looked for this error and I did not find much. I've also update my JCE jars. I am using opensaml

I am able to get the SAML Response and able to verify/validate the signature. At the time of decrypting the Assertions I am getting above error.

Code Details

// Iterating through the Encrypted Assertions

List<EncryptedAssertion> encryptedAssertions = response.getEncryptedAssertions();
    if (encryptedAssertions.size() > 0) {
        for (EncryptedAssertion encryptedAssertion : encryptedAssertions) {
            Assertion assertion = decryptAssertion(encryptedAssertion);
            ... 
        }
    }

This method decrypts the assertion. Getting error in this method

private Assertion decryptAssertion(EncryptedAssertion assertion) throws 
 CertificateException, KeyException
        {
    Assertion ast = null;
    Decrypter decrypter = buildAssertionDecrypter();
 try{
    ast = decrypter.decrypt(assertion);  // <-- Getting ERROR Here  
    }catch (DecryptionException e) {
        e.printStackTrace();
    }
   return ast;
}

Below method build the decrypter

    private  Decrypter buildAssertionDecrypter() throws CertificateException, KeyException {

    List<EncryptedKeyResolver> list = new ArrayList<>();
    list.add(new InlineEncryptedKeyResolver());
    list.add(new EncryptedElementTypeEncryptedKeyResolver());
    list.add(new SimpleRetrievalMethodEncryptedKeyResolver());
    final ChainingEncryptedKeyResolver encryptedKeyResolver = new ChainingEncryptedKeyResolver();
    encryptedKeyResolver.getResolverChain().addAll(list);
    final KeyInfoCredentialResolver resolver = new StaticKeyInfoCredentialResolver(buildCredentials());       
    Decrypter decrypter = new Decrypter(null, resolver, encryptedKeyResolver);
    return decrypter;
}

Build Credential Method

public Credential buildCredentials() throws CertificateException, KeyException {
    X509Certificate cert = getPublicCert();
    PrivateKey privateKey = getPrivateKey();
    BasicX509Credential decryptionCredential = SecurityHelper.getSimpleCredential(cert, privateKey);
     return decryptionCredential;       
}

Loading Public/Private keys

public X509Certificate getPublicCert(){
    X509Certificate cer = null;
    try{
        CertificateFactory fact = CertificateFactory.getInstance("X.509");
        FileInputStream is = new FileInputStream ("pubkey.cer");
        cer = (X509Certificate) fact.generateCertificate(is);

    } catch (Exception e) {
        e.printStackTrace();
    } 
    return cer;
}

public RSAPrivateKey getPrivateKey(){
    Key key = null;
    RSAPrivateKey privateKey = null;
    try {
        KeyStore ks = KeyStore.getInstance("pkcs12", "SunJSSE");         
        ks.load(new FileInputStream("prvkey.pfx"),"".toCharArray());
         Enumeration<String> aliases = ks.aliases();
         while(aliases.hasMoreElements()){
             String alias = aliases.nextElement();
             key  = ks.getKey(alias, "".toCharArray());
        }
         privateKey = (RSAPrivateKey)key;
} catch (Exception e) {
    e.printStackTrace();
} 
    return privateKey;
}

I have verified the Public and Private keys. I took the sample SAML and encrypted with the PUBLIC key and then took the encrypted SAML token and decrypted with the Private key. I get the same sample SAML. I used the online saml tool to do this.

EncryptedAssertion SAML

<saml:EncryptedAssertion>
<xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" Type="http://www.w3.org/2001/04/xmlenc#Element">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
<dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
<xenc:EncryptedKey>
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<xenc:CipherData>
<xenc:CipherValue>Some Value</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedKey>
</dsig:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>Value</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</saml:EncryptedAssertion>

回答1:

I tried this differently and I implemented the decryption logic manually. Here is the link of my post - Decryption of EncryptedAssertion manually



回答2:

For me I had similar issue when my unencrypted SAMPL response was invalid. This was because <saml:Assertion> didn't have valid namespace definition. I had to insert namespace declaration <saml:Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion"> .... to make it work. See the Issue 2 HERE. I am not sure whether you have the same issue. But that was the issue for me.