@@ -609,6 +609,23 @@ public void testDoesNotAllowSignatureWrappingAttack4() throws Exception {
609609 assertEquals ("someone@example.org" , samlResponse .getNameId ());
610610 }
611611
612+ @ Test
613+ public void testValidatesTheExpectedSignatures () throws Exception {
614+ // having
615+ Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.my.properties" ).build ();
616+ settings .setWantAssertionsSigned (true );
617+ settings .setWantMessagesSigned (true );
618+
619+ String samlResponseEncoded = Util .base64encoder (Util .getFileAsString ("data/responses/invalids/attacks/response_with_spoofed_response_signature.xml" ));
620+
621+ // when
622+ SamlResponse samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
623+
624+ // then
625+ assertFalse (samlResponse .isValid ());
626+ assertEquals ("Unexpected number of Response signatures found. SAML Response rejected." , samlResponse .getError ());
627+ }
628+
612629 /**
613630 * Tests the getSessionNotOnOrAfter method of SamlResponse
614631 *
@@ -851,6 +868,7 @@ public void testIsValidWrongEncryptedID() throws Exception {
851868 Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.my.properties" ).build ();
852869 String samlResponseEncoded = Util .getFileAsString ("data/responses/invalids/response_encrypted_subconfirm_as_nameid.xml.base64" );
853870 settings .setStrict (false );
871+ settings .setWantAssertionsSigned (false );
854872 SamlResponse samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
855873 assertTrue (samlResponse .isValid ());
856874 String nameId = samlResponse .getNameId ();
@@ -1431,39 +1449,44 @@ public void testIsValid2() throws Exception {
14311449 * @see com.onelogin.saml2.authn.SamlResponse#isValid
14321450 */
14331451 @ Test
1434- public void testIsValidEnc () throws Exception {
1452+ public void testIsValid_doubleSignedEncrypted () throws Exception {
14351453 Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.my.properties" ).build ();
1436- settings .setWantAssertionsSigned (false );
1437- settings .setWantMessagesSigned (false );
1454+ settings .setWantAssertionsSigned (true );
1455+ settings .setWantMessagesSigned (true );
14381456 String samlResponseEncoded = Util .getFileAsString ("data/responses/double_signed_encrypted_assertion.xml.base64" );
14391457
1440- settings .setStrict (false );
1441- SamlResponse samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
1442- assertTrue (samlResponse .isValid ());
1443-
1444- settings .setStrict (true );
1445- samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
1446- assertTrue (samlResponse .isValid ());
1458+ assertResponseValid (settings , samlResponseEncoded , false , true , null );
1459+ assertResponseValid (settings , samlResponseEncoded , true , true , null );
1460+ }
14471461
1448- samlResponseEncoded = Util .getFileAsString ("data/responses/signed_message_encrypted_assertion.xml.base64" );
1462+ @ Test
1463+ public void testIsValid_signedResponseEncryptedAssertion () throws Exception {
1464+ Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.my.properties" ).build ();
1465+ settings .setWantAssertionsSigned (false );
1466+ settings .setWantMessagesSigned (true );
14491467
1450- settings .setStrict (false );
1451- samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
1452- assertTrue (samlResponse .isValid ());
1468+ String samlResponseEncoded = Util .getFileAsString ("data/responses/signed_message_encrypted_assertion.xml.base64" );
14531469
1454- settings .setStrict (true );
1455- samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
1456- assertTrue (samlResponse .isValid ());
1470+ assertResponseValid (settings , samlResponseEncoded , false , true , null );
1471+ assertResponseValid (settings , samlResponseEncoded , true , true , null );
1472+ settings .setWantAssertionsSigned (true );
1473+ assertResponseValid (settings , samlResponseEncoded , false , true , null );
1474+ assertResponseValid (settings , samlResponseEncoded , true , false , "The Assertion of the Response is not signed and the SP requires it" );
1475+ }
14571476
1458- samlResponseEncoded = Util .getFileAsString ("data/responses/signed_encrypted_assertion.xml.base64" );
1477+ @ Test
1478+ public void testIsValid_signedEncryptedAssertion () throws Exception {
1479+ Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.my.properties" ).build ();
1480+ settings .setWantAssertionsSigned (true );
1481+ settings .setWantMessagesSigned (false );
14591482
1460- settings .setStrict (false );
1461- samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
1462- assertTrue (samlResponse .isValid ());
1483+ String samlResponseEncoded = Util .getFileAsString ("data/responses/signed_encrypted_assertion.xml.base64" );
14631484
1464- settings .setStrict (true );
1465- samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
1466- assertTrue (samlResponse .isValid ());
1485+ assertResponseValid (settings , samlResponseEncoded , false , true , null );
1486+ assertResponseValid (settings , samlResponseEncoded , true , true , null );
1487+ settings .setWantMessagesSigned (true );
1488+ assertResponseValid (settings , samlResponseEncoded , false , true , null );
1489+ assertResponseValid (settings , samlResponseEncoded , true , false , "The Message of the Response is not signed and the SP requires it" );
14671490 }
14681491
14691492 /**
@@ -1630,17 +1653,17 @@ public void testIsInValidSign() throws Exception {
16301653 samlResponseEncoded = Util .getFileAsString ("data/responses/invalids/triple_signed_response.xml.base64" );
16311654 samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
16321655 assertFalse (samlResponse .isValid ());
1633- assertEquals ("Duplicated ID . SAML Response rejected" , samlResponse .getError ());
1656+ assertEquals ("Unexpected number of Response signatures found . SAML Response rejected. " , samlResponse .getError ());
16341657
16351658 samlResponseEncoded = Util .getFileAsString ("data/responses/invalids/signed_assertion_response_with_2signatures.xml.base64" );
16361659 samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
16371660 assertFalse (samlResponse .isValid ());
1638- assertEquals ("Duplicated ID . SAML Response rejected" , samlResponse .getError ());
1661+ assertEquals ("Unexpected number of Response signatures found . SAML Response rejected. " , samlResponse .getError ());
16391662
16401663 samlResponseEncoded = Util .getFileAsString ("data/responses/invalids/signed_message_response_with_2signatures.xml.base64" );
16411664 samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
16421665 assertFalse (samlResponse .isValid ());
1643- assertEquals ("Duplicated ID . SAML Response rejected" , samlResponse .getError ());
1666+ assertEquals ("Unexpected number of Response signatures found . SAML Response rejected. " , samlResponse .getError ());
16441667
16451668 samlResponseEncoded = Util .getFileAsString ("data/responses/invalids/wrong_signed_element.xml.base64" );
16461669 samlResponse = new SamlResponse (settings , newHttpRequest (samlResponseEncoded ));
0 commit comments