Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions core/src/main/java/org/codelibs/saml2/core/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,12 @@ public static void decryptElement(final Element encryptedDataElement, final Priv

xmlCipher.setKEK(inputKey);
xmlCipher.doFinal(encryptedDataElement.getOwnerDocument(), encryptedDataElement, false);
} catch (final Exception e) {
} catch (final Exception | StackOverflowError e) {
// A StackOverflowError is caught as well: anyone can encrypt for the certificate of a
// service provider, and the XML security library handles the plaintext before parseXML
// can limit how deeply it is nested. The element stays encrypted, and the response is
// refused later like any other whose decryption failed.
//
// One line at warn, the trace at debug: the encrypted element is whatever was posted to a
// service provider's assertion consumer service, which is anonymous, so a trace per
// request would let an unauthenticated client fill the log.
Expand Down Expand Up @@ -1410,7 +1415,7 @@ public static void decryptUsingHsm(final Element encryptedDataElement, final HSM
xmlCipher.init(XMLCipher.DECRYPT_MODE, encryptionKey);
xmlCipher.setKEK(encryptionKey);
xmlCipher.doFinal(encryptedDataElement.getOwnerDocument(), encryptedDataElement, false);
} catch (final Exception e) {
} catch (final Exception | StackOverflowError e) {
LOGGER.warn("Failed to decrypt an element using HSM: {}", sanitizeForLog(e));
LOGGER.debug("Failed to decrypt an element using HSM.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3716,4 +3716,73 @@ public void testDefaultClockDriftValue() throws Exception {
Constants.ALLOWED_CLOCK_DRIFT, settings.getClockDrift());
}

/**
* Tests the constructor
* Case: an assertion encrypted for the service provider (which anyone can do) whose plaintext is
* nested deeper than the parser would have accepted
*/
@Test
public void testEncryptedAssertionNestedTooDeeplyIsRefused() throws Exception {
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();

// the control: the helper makes a response that is decrypted
SamlResponse samlResponse = new SamlResponse(settings, newHttpRequest("/", encryptedAssertionResponse(settings, 50)));
assertNotNull(samlResponse);

// deeper than the limit: refused when the decrypted assertion is parsed
try {
new SamlResponse(settings, newHttpRequest("/", encryptedAssertionResponse(settings, 300)));
org.junit.Assert.fail("the assertion is nested more than 100 levels deep");
} catch (org.codelibs.saml2.core.exception.XMLParsingException e) {
assertThat(e.getMessage(), org.hamcrest.CoreMatchers.containsString("nested more than 100 levels"));
}

// far deeper: the XML security library overflows the stack while it decrypts, before the
// parser can look at the plaintext. That is refused like any decryption that failed, and
// does not leave the library as an Error.
try {
new SamlResponse(settings, newHttpRequest("/", encryptedAssertionResponse(settings, 50000)));
org.junit.Assert.fail("the assertion is nested 50000 levels deep");
} catch (SAMLException e) {
// refused
}
}

private static String encryptedAssertionResponse(Saml2Settings settings, int depth) throws Exception {
StringBuilder deep = new StringBuilder();
for (int i = 0; i < depth; i++) {
deep.append("<x>");
}
for (int i = 0; i < depth; i++) {
deep.append("</x>");
}
String xml = "<samlp:Response xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\""
+ " xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" ID=\"r1\" Version=\"2.0\""
+ " IssueInstant=\"2026-01-01T00:00:00Z\"><saml:Issuer>idp</saml:Issuer>"
+ "<saml:EncryptedAssertion><saml:Assertion ID=\"a1\" Version=\"2.0\" IssueInstant=\"2026-01-01T00:00:00Z\">"
+ "<saml:Issuer>idp</saml:Issuer><saml:Advice>" + deep + "</saml:Advice></saml:Assertion></saml:EncryptedAssertion>"
+ "</samlp:Response>";
// Util.loadXML would refuse the deep document, which is what the test is about
javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = factory.newDocumentBuilder().parse(new org.xml.sax.InputSource(new java.io.StringReader(xml)));
org.w3c.dom.Element assertion = (org.w3c.dom.Element) doc.getElementsByTagNameNS(Constants.NS_SAML, "Assertion").item(0);

javax.crypto.KeyGenerator generator = javax.crypto.KeyGenerator.getInstance("AES");
generator.init(128);
javax.crypto.SecretKey key = generator.generateKey();
org.apache.xml.security.encryption.XMLCipher keyCipher =
org.apache.xml.security.encryption.XMLCipher.getInstance(org.apache.xml.security.encryption.XMLCipher.RSA_OAEP);
keyCipher.init(org.apache.xml.security.encryption.XMLCipher.WRAP_MODE, settings.getSPcert().getPublicKey());
org.apache.xml.security.encryption.EncryptedKey encryptedKey = keyCipher.encryptKey(doc, key);
org.apache.xml.security.encryption.XMLCipher dataCipher =
org.apache.xml.security.encryption.XMLCipher.getInstance(org.apache.xml.security.encryption.XMLCipher.AES_128);
dataCipher.init(org.apache.xml.security.encryption.XMLCipher.ENCRYPT_MODE, key);
org.apache.xml.security.keys.KeyInfo keyInfo = new org.apache.xml.security.keys.KeyInfo(doc);
keyInfo.add(encryptedKey);
dataCipher.getEncryptedData().setKeyInfo(keyInfo);
dataCipher.doFinal(doc, assertion, false);
return Util.base64encoder(Util.convertDocumentToString(doc));
}

}
Loading