SAML加密的Assertion解密失败 [英] SAML EncryptedAssertion Decryption failing

查看:70
本文介绍了SAML加密的Assertion解密失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SAML实现SP。当我尝试解密EncryptedAssertion时,收到以下错误。

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)

我寻找了这个错误,但没有找到太多。 我还更新了我的JCE JAR。我正在使用opensaml

我能够获得SAML响应,并且能够验证/验证签名。 在解密断言的时候,我得到了上面的错误。

代码详细信息

//迭代加密的断言

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

此方法解密断言。此方法出错

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;
}

下面的方法生成解密器

    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;
}

生成凭据方法

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

加载公钥/私钥

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;
}

我已验证公钥和私钥。我获取示例SAML并使用公钥进行加密,然后获取加密的SAML令牌并使用私钥进行解密。我得到了相同的SAML样本。我使用在线SAML工具来完成此操作。

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>

推荐答案

对于我来说,当我的未加密样本响应无效时,我也遇到了类似的问题。这是因为<;SAML:Assertion>;没有有效的名称空间定义。我不得不插入命名空间声明<;saml:Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion">;...才能让它发挥作用。见问题2HERE。我不确定你是否也有同样的问题。但这就是我的问题所在。

这篇关于SAML加密的Assertion解密失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆