本文介紹了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解密失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,