on
Post
- Get link
- X
- Other Apps
public String decodeBase64(String base64EncodedInput) {
// its safe to URL Decode first
URLCodec encoder = new URLCodec();
String urlDecoded = encoder.decode(base64EncodedInput);
byte[] bytes = Base64.getDecoder().decode(urlDecoded);
return new String(bytes);
}
public Response parse(String samlXml) throws IOException, SAXException, ParserConfigurationException {
Response response;
Element root;
StringReader reader = new StringReader(samlXml);
InputSource is = new InputSource(reader);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder documentBuilder = null;
documentBuilder = factory.newDocumentBuilder();
Document doc = documentBuilder.parse(is);
root = doc.getDocumentElement();
response = unmarshall(root);
return response;
}
public T unmarshall(Element element) {
try {
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
return (T) unmarshallerFactory.getUnmarshaller(element).unmarshall(element);
} catch (UnmarshallingException ux) {
throw new RuntimeException(ux);
}
}
Assertion assertion = response.getAssertions().get(0);
AttributeStatement statement = assertion.getAttributeStatements().get(0);
Attribute attribute = statement.getAttributes().get(0);
private Assertion decryptEncryptedAssertion(EncryptedAssertion encryptedAssertion) throws IOException, NoSuchPaddingException,
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException,
DecryptionException, InvalidKeySpecException, CertificateException {
File privateKeyFile = new File(FORMATTED_PRIVATE_KEY_FILE_PATH);
InputStream privateKeyStream = new FileInputStream(privateKeyFile);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(privateKeyStream));
KeyFactory factory = KeyFactory.getInstance("RSA"); // Algorithm as "RSA" here can differ based on your actual encryption
PrivateKey privateKey = factory.generatePrivate(spec);
BasicX509Credential cred = new BasicX509Credential();
cred.setPrivateKey(privateKey);
StaticKeyInfoCredentialResolver resolver = new StaticKeyInfoCredentialResolver(cred);
Decrypter decrypter = new Decrypter(null, resolver, new InlineEncryptedKeyResolver());
decrypter.setRootInNewDocument(true);
Assertion decrypted = decrypter.decrypt(encryptedAssertion);
return decrypted;
} // decryptEncryptedAssertion
openssl pkcs8 -topk8 -inform PEM -outform DER -in <PRIVATE_KEY_FILE_NAME> -nocrypt > pkcs8_key
ERROR [main] encryption.Decrypter - Failed to decrypt EncryptedKey, valid decryption key could not be resolved
[2018-03-02 15:05:16,478] ERROR [main] encryption.Decrypter - Failed to decrypt EncryptedData using either EncryptedData KeyInfoCredentialResolver or EncryptedKeyResolver + EncryptedKey KeyInfoCredentialResolver
[2018-03-02 15:05:16,482] ERROR [main] encryption.Decrypter - SAML Decrypter encountered an error decrypting element content
org.opensaml.xml.encryption.DecryptionException: Failed to decrypt EncryptedData
java.lang.ClassCastException: com.sun.crypto.provider.AESCipher cannot be cast to javax.crypto.CipherSpi
com.sun.crypto.provider.RSACipher cannot be cast to javax.crypto.CipherSpi
@PowerMockIgnore({"com.sun.net.ssl.internal.ssl.Provider", "javax.crypto.*"})
org.opensaml.xml.validation.ValidationException: Unable to evaluate key against signature
at org.opensaml.xml.signature.SignatureValidator.validate(SignatureValidator.java:74)
at com.worldlingo.servlet.SamlServlet.verifySignature(SamlServlet.java:447)
at com.worldlingo.servlet.SamlServlet.validateResponse(SamlServlet.java:315)
...........
...........
Caused by: org.apache.xml.security.signature.XMLSignatureException: Signature length not correct: got 256 but was expecting 128
Original Exception was java.security.SignatureException: Signature length not correct: got 256 but was expecting 128
at org.apache.xml.security.algorithms.implementations.SignatureBaseRSA.engineVerify(SignatureBaseRSA.java:93)
at org.apache.xml.security.algorithms.SignatureAlgorithm.verify(SignatureAlgorithm.java:301)
at org.apache.xml.security.signature.XMLSignature.checkSignatureValue(XMLSignature.java:723)
at org.opensaml.xml.signature.SignatureValidator.validate(SignatureValidator.java:69)
Comments
Post a Comment