【发布时间】:2018-02-07 02:14:15
【问题描述】:
我正在使用 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 罐子。我正在使用 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 并使用 PUBLIC 密钥加密,然后获取了加密的 SAML 令牌并使用私钥解密。我得到了相同的示例 SAML。我使用了在线 saml 工具来做到这一点。
加密断言 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>
【问题讨论】:
标签: java encryption saml-2.0 opensaml