diff --git a/core/src/main/java/org/codelibs/saml2/core/util/Util.java b/core/src/main/java/org/codelibs/saml2/core/util/Util.java index a5cdd1c..bb5642e 100644 --- a/core/src/main/java/org/codelibs/saml2/core/util/Util.java +++ b/core/src/main/java/org/codelibs/saml2/core/util/Util.java @@ -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. @@ -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); } diff --git a/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java b/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java index 55bf080..ab6ffe4 100644 --- a/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java +++ b/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java @@ -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(""); + } + for (int i = 0; i < depth; i++) { + deep.append(""); + } + String xml = "idp" + + "" + + "idp" + deep + "" + + ""; + // 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)); + } + }