Я реализую SP с использованием SAML. Когда я пытаюсь расшифровать 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. я использую опенсамл
Я могу получить ответ 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.
Зашифрованное утверждение 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>