You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by co...@apache.org on 2015/12/19 12:11:55 UTC
svn commit: r1720895 [23/34] - in /webservices/wss4j/branches/2_1_x-fixes:
bindings/src/main/java/org/apache/wss4j/binding/wss10/
bindings/src/main/java/org/apache/wss4j/binding/wss11/
bindings/src/main/java/org/apache/wss4j/binding/wssc13/ bindings/sr...
Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/TimestampTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/TimestampTest.java?rev=1720895&r1=1720894&r2=1720895&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/TimestampTest.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/TimestampTest.java Sat Dec 19 11:11:50 2015
@@ -49,15 +49,15 @@ import org.w3c.dom.Element;
* WS-Security Test Case for Timestamps.
*/
public class TimestampTest extends org.junit.Assert {
- private static final org.slf4j.Logger LOG =
+ private static final org.slf4j.Logger LOG =
org.slf4j.LoggerFactory.getLogger(TimestampTest.class);
-
+
@org.junit.AfterClass
public static void cleanup() throws Exception {
SecurityTestUtil.cleanup();
}
-
+
/**
* This is a test for processing a valid Timestamp.
*/
@@ -67,35 +67,35 @@ public class TimestampTest extends org.j
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(300);
Document createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
//
// Do some processing
//
WSHandlerResult wsResult = verify(createdDoc);
- WSSecurityEngineResult actionResult =
+ WSSecurityEngineResult actionResult =
wsResult.getActionResults().get(WSConstants.TS).get(0);
assertTrue(actionResult != null);
-
- Timestamp receivedTimestamp =
+
+ Timestamp receivedTimestamp =
(Timestamp)actionResult.get(WSSecurityEngineResult.TAG_TIMESTAMP);
assertTrue(receivedTimestamp != null);
-
+
Timestamp clone = new Timestamp(receivedTimestamp.getElement(), new BSPEnforcer(true));
assertTrue(clone.equals(receivedTimestamp));
assertTrue(clone.hashCode() == receivedTimestamp.hashCode());
}
-
-
+
+
/**
* This is a test for processing a valid Timestamp with no expires element
*/
@@ -105,47 +105,47 @@ public class TimestampTest extends org.j
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(0);
Document createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
//
// Do some processing
//
WSHandlerResult wsResult = verify(createdDoc);
- WSSecurityEngineResult actionResult =
+ WSSecurityEngineResult actionResult =
wsResult.getActionResults().get(WSConstants.TS).get(0);
assertTrue(actionResult != null);
-
- Timestamp receivedTimestamp =
+
+ Timestamp receivedTimestamp =
(Timestamp)actionResult.get(WSSecurityEngineResult.TAG_TIMESTAMP);
assertTrue(receivedTimestamp != null);
}
-
+
@org.junit.Test
public void testInvalidTimestampNoExpires() throws Exception {
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(0);
Document createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
//
// Do some processing
//
@@ -157,22 +157,22 @@ public class TimestampTest extends org.j
secEngine.processSecurityHeader(doc, requestData);
fail("Failure expected on no Expires Element");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.SECURITY_ERROR);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.SECURITY_ERROR);
}
requestData.setWssConfig(WSSConfig.getNewInstance());
requestData.setRequireTimestampExpires(false);
WSHandlerResult wsResult = secEngine.processSecurityHeader(doc, requestData);
- WSSecurityEngineResult actionResult =
+ WSSecurityEngineResult actionResult =
wsResult.getActionResults().get(WSConstants.TS).get(0);
assertTrue(actionResult != null);
-
- Timestamp receivedTimestamp =
+
+ Timestamp receivedTimestamp =
(Timestamp)actionResult.get(WSSecurityEngineResult.TAG_TIMESTAMP);
assertTrue(receivedTimestamp != null);
}
-
-
+
+
/**
* This is a test for processing an expired Timestamp.
*/
@@ -182,46 +182,46 @@ public class TimestampTest extends org.j
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(-1);
Document createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
try {
verify(createdDoc);
fail("Expected failure on an expired timestamp");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
- }
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
+ }
}
-
-
+
+
/**
* This is a test for processing an "old" Timestamp, i.e. one with a "Created" element that is
* out of date
*/
@org.junit.Test
public void testOldTimestamp() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
Document createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
//
// Do some processing
//
@@ -232,24 +232,24 @@ public class TimestampTest extends org.j
verify(createdDoc, requestData);
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
- }
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
+ }
}
-
-
+
+
/**
* This is a test for processing an Timestamp where the "Created" element is in the (near)
- * future. It should be accepted by default when it is created 30 seconds in the future,
+ * future. It should be accepted by default when it is created 30 seconds in the future,
* and then rejected once we configure "0 seconds" for future-time-to-live.
*/
@org.junit.Test
public void testNearFutureCreated() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -266,9 +266,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementCreated);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -282,22 +282,22 @@ public class TimestampTest extends org.j
verify(doc, requestData);
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
}
}
-
+
/**
* This is a test for processing an Timestamp where the "Created" element is in the future.
* A Timestamp that is 120 seconds in the future should be rejected by default.
*/
@org.junit.Test
public void testFutureCreated() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -314,9 +314,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementCreated);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -327,23 +327,23 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
}
}
-
-
+
+
/**
* This is a test for processing an Timestamp where the "Created" element is greater than
* the expiration time.
*/
@org.junit.Test
public void testExpiresBeforeCreated() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -358,7 +358,7 @@ public class TimestampTest extends org.j
createdDate.setTime(currentTime);
elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
timestampElement.appendChild(elementCreated);
-
+
Date expiresDate = new Date();
expiresDate.setTime(expiresDate.getTime() - 300000);
@@ -370,9 +370,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementExpires);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -383,10 +383,10 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
}
}
-
+
/**
* This is a test for processing multiple Timestamps in the security header
*/
@@ -396,21 +396,21 @@ public class TimestampTest extends org.j
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(300);
Document createdDoc = timestamp.build(doc, secHeader);
-
+
timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(60);
createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
//
// Do some processing
//
@@ -418,24 +418,24 @@ public class TimestampTest extends org.j
verify(createdDoc);
fail("Expected failure on multiple timestamps");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
verify(createdDoc, Collections.singletonList(BSPRule.R3227));
}
-
+
/**
* This is a test for processing an Timestamp where it contains multiple "Created" elements.
* This Timestamp should be rejected.
*/
@org.junit.Test
public void testMultipleCreated() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -451,9 +451,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementCreated.cloneNode(true));
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -464,24 +464,24 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed on multiple Created elements");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
verify(doc, Collections.singletonList(BSPRule.R3203));
}
-
+
/**
* This is a test for processing an Timestamp where it contains no "Created" element.
* This Timestamp should be rejected.
*/
@org.junit.Test
public void testNoCreated() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -498,9 +498,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementCreated);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -511,31 +511,31 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed on no Created element");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
List<BSPRule> rules = new ArrayList<>();
rules.add(BSPRule.R3203);
rules.add(BSPRule.R3221);
verify(doc, rules);
}
-
+
/**
* This is a test for processing an Timestamp where it contains multiple "Expires" elements.
* This Timestamp should be rejected.
*/
@org.junit.Test
public void testMultipleExpires() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
-
+
DateFormat zulu = new XmlSchemaDateFormat();
Element elementCreated =
doc.createElementNS(
@@ -558,9 +558,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementExpires.cloneNode(true));
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -571,24 +571,24 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed on multiple Expires elements");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
verify(doc, Collections.singletonList(BSPRule.R3224));
}
-
+
/**
* This is a test for processing an Timestamp where it contains an "Expires" element before
* the Created element. This Timestamp should be rejected as per the BSP spec.
*/
@org.junit.Test
public void testExpiresInFrontOfCreated() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -603,7 +603,7 @@ public class TimestampTest extends org.j
expiresDate.setTime(currentTime);
elementCreated.appendChild(doc.createTextNode(zulu.format(expiresDate)));
timestampElement.appendChild(elementCreated);
-
+
Element elementExpires =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN
@@ -612,9 +612,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementExpires);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -625,25 +625,25 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
verify(doc, Collections.singletonList(BSPRule.R3221));
}
-
-
+
+
/**
* This is a test for processing an Timestamp where it contains a Created element with
* seconds > 60. This should be rejected as per the BSP spec.
*/
@org.junit.Test
public void testCreatedSeconds() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -656,9 +656,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementCreated);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -672,23 +672,23 @@ public class TimestampTest extends org.j
verify(doc, wssConfig, new ArrayList<BSPRule>());
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
}
-
-
+
+
/**
* This is a test for processing an Timestamp where it contains a Created element with
* a ValueType. This should be rejected as per the BSP spec.
*/
@org.junit.Test
public void testCreatedValueType() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -706,9 +706,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementCreated);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -719,15 +719,15 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
// Now it should pass...
WSSConfig wssConfig = WSSConfig.getNewInstance();
wssConfig.setValidator(WSConstants.TIMESTAMP, new NoOpValidator());
verify(doc, wssConfig, Collections.singletonList(BSPRule.R3225));
}
-
+
/**
@@ -736,12 +736,12 @@ public class TimestampTest extends org.j
*/
@org.junit.Test
public void testCustomElement() throws Exception {
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
- Element timestampElement =
+
+ Element timestampElement =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN
);
@@ -754,7 +754,7 @@ public class TimestampTest extends org.j
Date createdDate = new Date();
elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
timestampElement.appendChild(elementCreated);
-
+
Element elementExpires =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN
@@ -762,7 +762,7 @@ public class TimestampTest extends org.j
createdDate.setTime(createdDate.getTime() + 300000);
elementExpires.appendChild(doc.createTextNode(zulu.format(createdDate)));
timestampElement.appendChild(elementExpires);
-
+
Element elementCustom =
doc.createElementNS(
WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + "Custom"
@@ -770,9 +770,9 @@ public class TimestampTest extends org.j
timestampElement.appendChild(elementCustom);
secHeader.getSecurityHeader().appendChild(timestampElement);
-
+
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
LOG.debug(outputString);
}
@@ -783,13 +783,13 @@ public class TimestampTest extends org.j
verify(doc);
fail("The timestamp validation should have failed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
// Now it should pass...
verify(doc, Collections.singletonList(BSPRule.R3222));
}
-
+
/**
* This is a test to create a "Spoofed" Timestamp (see WSS-441)
*/
@@ -799,10 +799,10 @@ public class TimestampTest extends org.j
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(300);
-
+
WSTimeSource spoofedTimeSource = new WSTimeSource() {
public Date now() {
@@ -810,18 +810,18 @@ public class TimestampTest extends org.j
currentTime.setTime(currentTime.getTime() - (500L * 1000L));
return currentTime;
}
-
+
};
timestamp.setWsTimeSource(spoofedTimeSource);
-
+
Document createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
//
// Do some processing
//
@@ -829,64 +829,64 @@ public class TimestampTest extends org.j
verify(createdDoc);
fail("Expected failure on an expired timestamp");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
}
}
-
+
@org.junit.Test
public void testTimestampNoMilliseconds() throws Exception {
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setPrecisionInMilliSeconds(false);
timestamp.setTimeToLive(300);
Document createdDoc = timestamp.build(doc, secHeader);
if (LOG.isDebugEnabled()) {
- String outputString =
+ String outputString =
XMLUtils.PrettyDocumentToString(createdDoc);
LOG.debug(outputString);
}
-
+
//
// Do some processing
//
WSHandlerResult wsResult = verify(createdDoc);
- WSSecurityEngineResult actionResult =
+ WSSecurityEngineResult actionResult =
wsResult.getActionResults().get(WSConstants.TS).get(0);
assertTrue(actionResult != null);
}
-
+
@org.junit.Test
public void testThaiLocaleVerification() throws Exception {
-
+
Locale defaultLocale = Locale.getDefault();
try {
Locale.setDefault(new Locale("th", "TH"));
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(300);
Document createdDoc = timestamp.build(doc, secHeader);
-
+
//
// Do some processing
//
WSHandlerResult wsResult = verify(createdDoc);
- WSSecurityEngineResult actionResult =
+ WSSecurityEngineResult actionResult =
wsResult.getActionResults().get(WSConstants.TS).get(0);
assertTrue(actionResult != null);
} finally {
Locale.setDefault(defaultLocale);
}
}
-
+
/**
* Verifies the soap envelope
*/
@@ -898,14 +898,14 @@ public class TimestampTest extends org.j
requestData.setWssConfig(WSSConfig.getNewInstance());
return secEngine.processSecurityHeader(doc, requestData);
}
-
+
private WSHandlerResult verify(
Document doc, RequestData requestData
) throws Exception {
WSSecurityEngine secEngine = new WSSecurityEngine();
return secEngine.processSecurityHeader(doc, requestData);
}
-
+
/**
* Verifies the soap envelope
*/
@@ -917,7 +917,7 @@ public class TimestampTest extends org.j
requestData.setIgnoredBSPRules(ignoredRules);
return secEngine.processSecurityHeader(doc, requestData);
}
-
+
/**
* Verifies the soap envelope
*/
@@ -930,6 +930,6 @@ public class TimestampTest extends org.j
requestData.setIgnoredBSPRules(ignoredRules);
return secEngine.processSecurityHeader(doc, requestData);
}
-
-
+
+
}
Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTDerivedKeyTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTDerivedKeyTest.java?rev=1720895&r1=1720894&r2=1720895&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTDerivedKeyTest.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTDerivedKeyTest.java Sat Dec 19 11:11:50 2015
@@ -49,37 +49,37 @@ import org.apache.xml.security.utils.Bas
import org.w3c.dom.Document;
/**
- * WS-Security Test Case for UsernameToken Key Derivation, as defined in the
+ * WS-Security Test Case for UsernameToken Key Derivation, as defined in the
* UsernameTokenProfile 1.1 specification. The derived keys are used to encrypt
* and sign, as per wsc:DerivedKeyToken.
*/
public class UTDerivedKeyTest extends org.junit.Assert {
- private static final org.slf4j.Logger LOG =
+ private static final org.slf4j.Logger LOG =
org.slf4j.LoggerFactory.getLogger(UTDerivedKeyTest.class);
private CallbackHandler callbackHandler = new UsernamePasswordCallbackHandler();
private Crypto crypto = null;
-
+
@org.junit.AfterClass
public static void cleanup() throws Exception {
SecurityTestUtil.cleanup();
}
-
+
public UTDerivedKeyTest() throws Exception {
crypto = CryptoFactory.getInstance();
}
/**
- * Unit test for the UsernameToken derived key functionality
+ * Unit test for the UsernameToken derived key functionality
*/
@org.junit.Test
public void testUsernameTokenUnit() throws Exception {
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
UsernameToken usernameToken = new UsernameToken(true, doc, null);
usernameToken.setName("bob");
-
+
byte[] salt = usernameToken.addSalt(doc, null, false);
assertTrue(salt.length == 16);
assertTrue(salt[0] == 0x02);
@@ -88,24 +88,24 @@ public class UTDerivedKeyTest extends or
for (int i = 0; i < salt.length; i++) {
assertTrue(salt[i] == utSalt[i]);
}
-
+
usernameToken.addIteration(doc, 500);
assertTrue(usernameToken.getIteration() == 500);
-
+
WSSecurityUtil.prependChildElement(
secHeader.getSecurityHeader(), usernameToken.getElement()
);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
assertTrue(outputString.contains("wsse11:Salt"));
assertTrue(outputString.contains("wsse11:Iteration"));
-
+
byte[] derivedKey = UsernameTokenUtil.generateDerivedKey("security", salt, 500);
assertTrue(derivedKey.length == 20);
-
+
// "c2VjdXJpdHk=" is the Base64 encoding of "security"
derivedKey = UsernameTokenUtil.generateDerivedKey(Base64.decode("c2VjdXJpdHk="), salt, 500);
assertTrue(derivedKey.length == 20);
@@ -135,17 +135,17 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("bob", "security");
builder.addDerivedKey(false, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key encryption
//
@@ -154,10 +154,10 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, tokenIdentifier);
encrBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -167,17 +167,17 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
verify(encryptedDoc);
-
+
try {
verify(encryptedDoc, false);
fail("Failure expected on deriving keys from a UsernameToken not allowed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
}
}
-
+
/**
* Test using a UsernameToken derived key for encrypting a SOAP body
*/
@@ -186,17 +186,17 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setPasswordsAreEncoded(true);
builder.setUserInfo("bob", Base64.encode(MessageDigest.getInstance("SHA-1").digest("security".getBytes(StandardCharsets.UTF_8))));
builder.addDerivedKey(false, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
String tokenIdentifier = builder.getId();
-
+
//
// Derived key encryption
//
@@ -205,10 +205,10 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, tokenIdentifier);
encrBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -218,16 +218,16 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
RequestData requestData = new RequestData();
requestData.setEncodePasswords(true);
requestData.setAllowUsernameTokenNoPassword(true);
requestData.setCallbackHandler(new EncodedPasswordCallbackHandler());
-
+
WSSecurityEngine newEngine = new WSSecurityEngine();
newEngine.processSecurityHeader(encryptedDoc, requestData);
}
-
+
/**
* Test using a UsernameToken derived key for encrypting a SOAP body. In this test the
* derived key is modified before encryption, and so decryption should fail.
@@ -237,19 +237,19 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("bob", "security");
builder.addDerivedKey(false, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
derivedKey[5] = 'z';
derivedKey[6] = 'a';
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key encryption
//
@@ -258,10 +258,10 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, tokenIdentifier);
encrBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -271,7 +271,7 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
try {
verify(encryptedDoc);
fail("Failure expected on a bad derived encryption");
@@ -279,7 +279,7 @@ public class UTDerivedKeyTest extends or
assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_CHECK);
}
}
-
+
/**
* Test using a UsernameToken derived key for encrypting a SOAP body. In this test the
* user is "colm" rather than "bob", and so decryption should fail.
@@ -289,17 +289,17 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("colm", "security");
builder.addDerivedKey(false, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key encryption
//
@@ -308,10 +308,10 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, tokenIdentifier);
encrBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -321,7 +321,7 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
try {
verify(encryptedDoc);
fail("Failure expected on a bad derived encryption");
@@ -329,7 +329,7 @@ public class UTDerivedKeyTest extends or
assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
}
}
-
+
/**
* Test using a UsernameToken derived key for signing a SOAP body
*/
@@ -338,17 +338,17 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("bob", "security");
builder.addDerivedKey(true, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key signature
//
@@ -357,10 +357,10 @@ public class UTDerivedKeyTest extends or
sigBuilder.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
sigBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document signedDoc = sigBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(signedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -369,16 +369,16 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
WSHandlerResult results = verify(signedDoc);
WSSecurityEngineResult actionResult =
results.getActionResults().get(WSConstants.SIGN).get(0);
- java.security.Principal principal =
+ java.security.Principal principal =
(java.security.Principal) actionResult.get(WSSecurityEngineResult.TAG_PRINCIPAL);
// System.out.println(principal.getName());
assertTrue(principal.getName().contains("DK"));
}
-
+
/**
* Test using a UsernameToken derived key for signing a SOAP body
*/
@@ -387,18 +387,18 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setPasswordsAreEncoded(true);
builder.setUserInfo("bob", Base64.encode(MessageDigest.getInstance("SHA-1").digest("security".getBytes(StandardCharsets.UTF_8))));
builder.addDerivedKey(true, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key signature
//
@@ -407,10 +407,10 @@ public class UTDerivedKeyTest extends or
sigBuilder.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
sigBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document signedDoc = sigBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(signedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -419,23 +419,23 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
RequestData requestData = new RequestData();
requestData.setEncodePasswords(true);
requestData.setAllowUsernameTokenNoPassword(true);
requestData.setCallbackHandler(new EncodedPasswordCallbackHandler());
-
+
WSSecurityEngine newEngine = new WSSecurityEngine();
WSHandlerResult results = newEngine.processSecurityHeader(signedDoc, requestData);
-
+
WSSecurityEngineResult actionResult =
results.getActionResults().get(WSConstants.SIGN).get(0);
- java.security.Principal principal =
+ java.security.Principal principal =
(java.security.Principal) actionResult.get(WSSecurityEngineResult.TAG_PRINCIPAL);
// System.out.println(principal.getName());
assertTrue(principal.getName().contains("DK"));
}
-
+
/**
* Test using a UsernameToken derived key for signing a SOAP body. In this test the
* derived key is modified before signature, and so signature verification should
@@ -446,12 +446,12 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("bob", "security");
builder.addDerivedKey(true, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
if (derivedKey[5] != 12) {
derivedKey[5] = 12;
@@ -459,9 +459,9 @@ public class UTDerivedKeyTest extends or
derivedKey[5] = 13;
}
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key signature
//
@@ -470,10 +470,10 @@ public class UTDerivedKeyTest extends or
sigBuilder.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
sigBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document signedDoc = sigBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(signedDoc);
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
@@ -486,7 +486,7 @@ public class UTDerivedKeyTest extends or
assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_CHECK);
}
}
-
+
/**
* Test using a UsernameToken derived key for signing a SOAP body. In this test the
* user is "colm" rather than "bob", and so signature verification should fail.
@@ -496,17 +496,17 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("colm", "security");
builder.addDerivedKey(true, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key signature
//
@@ -515,10 +515,10 @@ public class UTDerivedKeyTest extends or
sigBuilder.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
sigBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document signedDoc = sigBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(signedDoc);
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
@@ -531,7 +531,7 @@ public class UTDerivedKeyTest extends or
assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
}
}
-
+
/**
* Unit test for creating a Username Token with no salt element that is used for
* deriving a key for encryption.
@@ -541,17 +541,17 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
UsernameToken usernameToken = new UsernameToken(true, doc, null);
usernameToken.setName("bob");
WSSConfig config = WSSConfig.getNewInstance();
usernameToken.setID(config.getIdAllocator().createId("UsernameToken-", usernameToken));
-
+
byte[] salt = UsernameTokenUtil.generateSalt(false);
usernameToken.addIteration(doc, 1000);
-
+
byte[] derivedKey = UsernameTokenUtil.generateDerivedKey("security", salt, 1000);
-
+
//
// Derived key encryption
//
@@ -560,12 +560,12 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, usernameToken.getID());
encrBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
WSSecurityUtil.prependChildElement(
secHeader.getSecurityHeader(), usernameToken.getElement()
);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -574,15 +574,15 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
try {
verify(encryptedDoc);
fail("Failure expected on no salt element");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILURE);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILURE);
}
}
-
+
/**
* Unit test for creating a Username Token with no iteration element that is used for
* deriving a key for encryption.
@@ -592,15 +592,15 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
UsernameToken usernameToken = new UsernameToken(true, doc, null);
usernameToken.setName("bob");
WSSConfig config = WSSConfig.getNewInstance();
usernameToken.setID(config.getIdAllocator().createId("UsernameToken-", usernameToken));
-
+
byte[] salt = usernameToken.addSalt(doc, null, false);
byte[] derivedKey = UsernameTokenUtil.generateDerivedKey("security", salt, 1000);
-
+
//
// Derived key encryption
//
@@ -609,12 +609,12 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, usernameToken.getID());
encrBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
WSSecurityUtil.prependChildElement(
secHeader.getSecurityHeader(), usernameToken.getElement()
);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -623,15 +623,15 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
try {
verify(encryptedDoc);
fail("Failure expected on no iteration element");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY_TOKEN);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY_TOKEN);
}
}
-
+
/**
* Unit test for creating a Username Token with an iteration value < 1000 that is used for
* deriving a key for encryption.
@@ -641,16 +641,16 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
UsernameToken usernameToken = new UsernameToken(true, doc, null);
usernameToken.setName("bob");
WSSConfig config = WSSConfig.getNewInstance();
usernameToken.setID(config.getIdAllocator().createId("UsernameToken-", usernameToken));
-
+
usernameToken.addIteration(doc, 500);
byte[] salt = usernameToken.addSalt(doc, null, false);
byte[] derivedKey = UsernameTokenUtil.generateDerivedKey("security", salt, 500);
-
+
//
// Derived key encryption
//
@@ -659,12 +659,12 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, usernameToken.getID());
encrBuilder.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
WSSecurityUtil.prependChildElement(
secHeader.getSecurityHeader(), usernameToken.getElement()
);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -673,12 +673,12 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
try {
verify(encryptedDoc);
fail("Failure expected on a low iteration value");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
RequestData data = new RequestData();
@@ -686,13 +686,13 @@ public class UTDerivedKeyTest extends or
data.setDecCrypto(crypto);
data.setIgnoredBSPRules(Collections.singletonList(BSPRule.R4218));
data.setAllowUsernameTokenNoPassword(true);
-
+
WSSecurityEngine engine = new WSSecurityEngine();
engine.setWssConfig(config);
engine.processSecurityHeader(doc, data);
}
-
+
/**
* Test using a UsernameToken derived key for encrypting a SOAP body. The Reference to the
* UsernameToken contains a non-standard value type, which is rejected when the corresponding
@@ -703,17 +703,17 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("bob", "security");
builder.addDerivedKey(false, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key encryption
//
@@ -722,10 +722,10 @@ public class UTDerivedKeyTest extends or
encrBuilder.setExternalKey(derivedKey, tokenIdentifier);
encrBuilder.setCustomValueType(WSConstants.WSS_SAML_TOKEN_TYPE);
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -735,20 +735,20 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
try {
verify(encryptedDoc);
fail("Failure expected on a bad value type");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
// Turn off BSP compliance and it should work
RequestData data = new RequestData();
data.setCallbackHandler(callbackHandler);
data.setDecCrypto(crypto);
data.setAllowUsernameTokenNoPassword(true);
-
+
WSSConfig config = WSSConfig.getNewInstance();
WSSecurityEngine newEngine = new WSSecurityEngine();
newEngine.setWssConfig(config);
@@ -756,7 +756,7 @@ public class UTDerivedKeyTest extends or
newEngine.processSecurityHeader(encryptedDoc, data);
}
-
+
/**
* Test using a UsernameToken derived key for encrypting a SOAP body. A KeyIdentifier is
* used to refer to the UsernameToken, which is forbidden by the BSP.
@@ -766,34 +766,34 @@ public class UTDerivedKeyTest extends or
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("bob", "security");
builder.addDerivedKey(false, null, 1000);
builder.prepare(doc);
-
+
byte[] derivedKey = builder.getDerivedKey();
assertTrue(derivedKey.length == 20);
-
+
String tokenIdentifier = builder.getId();
-
+
//
// Derived key encryption
//
WSSecDKEncrypt encrBuilder = new WSSecDKEncrypt();
encrBuilder.setSymmetricEncAlgorithm(WSConstants.AES_128);
-
+
SecurityTokenReference strEncKey = new SecurityTokenReference(doc);
strEncKey.setKeyIdentifier(
WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE, tokenIdentifier, true
);
encrBuilder.setExternalKey(derivedKey, strEncKey.getElement());
-
+
Document encryptedDoc = encrBuilder.build(doc, secHeader);
-
+
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -803,49 +803,49 @@ public class UTDerivedKeyTest extends or
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
try {
verify(encryptedDoc);
fail("Failure expected on a key identifier");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY);
}
-
+
WSSecurityEngine newEngine = new WSSecurityEngine();
RequestData data = new RequestData();
data.setCallbackHandler(callbackHandler);
data.setDecCrypto(crypto);
data.setIgnoredBSPRules(Collections.singletonList(BSPRule.R4215));
data.setAllowUsernameTokenNoPassword(true);
-
+
WSSConfig config = WSSConfig.getNewInstance();
newEngine.setWssConfig(config);
newEngine.processSecurityHeader(encryptedDoc, data);
}
-
+
/**
* Verifies the soap envelope.
- *
+ *
* @param env soap envelope
* @throws java.lang.Exception Thrown when there is a problem in verification
*/
private WSHandlerResult verify(Document doc) throws Exception {
return verify(doc, true);
}
-
+
private WSHandlerResult verify(
- Document doc,
+ Document doc,
boolean allowUsernameTokenDerivedKeys
) throws Exception {
WSSecurityEngine secEngine = new WSSecurityEngine();
-
+
RequestData requestData = new RequestData();
requestData.setAllowUsernameTokenNoPassword(allowUsernameTokenDerivedKeys);
requestData.setCallbackHandler(callbackHandler);
requestData.setDecCrypto(crypto);
requestData.setSigVerCrypto(crypto);
-
+
return secEngine.processSecurityHeader(doc, requestData);
}
Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTSignatureTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTSignatureTest.java?rev=1720895&r1=1720894&r2=1720895&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTSignatureTest.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/UTSignatureTest.java Sat Dec 19 11:11:50 2015
@@ -42,24 +42,24 @@ import javax.security.auth.callback.Call
import java.util.Collections;
/**
- * WS-Security Test Case for UsernameToken Key Derivation, as defined in the
+ * WS-Security Test Case for UsernameToken Key Derivation, as defined in the
* UsernameTokenProfile 1.1 specification. The derived keys are used for signature.
* Note that this functionality is different to the UTDerivedKeyTest test case,
* which uses the derived key in conjunction with wsc:DerivedKeyToken. It's also
- * different to UTWseSignatureTest, which derives a key for signature using a
+ * different to UTWseSignatureTest, which derives a key for signature using a
* non-standard implementation.
*/
public class UTSignatureTest extends org.junit.Assert {
- private static final org.slf4j.Logger LOG =
+ private static final org.slf4j.Logger LOG =
org.slf4j.LoggerFactory.getLogger(UTSignatureTest.class);
private CallbackHandler callbackHandler = new UsernamePasswordCallbackHandler();
private Crypto crypto = null;
-
+
@org.junit.AfterClass
public static void cleanup() throws Exception {
SecurityTestUtil.cleanup();
}
-
+
public UTSignatureTest() throws Exception {
crypto = CryptoFactory.getInstance();
}
@@ -72,23 +72,23 @@ public class UTSignatureTest extends org
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("bob", "security");
builder.addDerivedKey(true, null, 1000);
builder.prepare(doc);
-
+
WSSecSignature sign = new WSSecSignature();
sign.setCustomTokenValueType(WSConstants.USERNAMETOKEN_NS + "#UsernameToken");
sign.setCustomTokenId(builder.getId());
sign.setSecretKey(builder.getDerivedKey());
sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
sign.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
-
+
Document signedDoc = sign.build(doc, null, secHeader);
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(signedDoc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -97,23 +97,23 @@ public class UTSignatureTest extends org
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
WSHandlerResult results = verify(signedDoc);
WSSecurityEngineResult actionResult =
results.getActionResults().get(WSConstants.UT_SIGN).get(0);
- java.security.Principal principal =
+ java.security.Principal principal =
(java.security.Principal) actionResult.get(WSSecurityEngineResult.TAG_PRINCIPAL);
assertTrue(principal.getName().contains("bob"));
-
+
try {
verify(signedDoc, false);
fail("Failure expected on deriving keys from a UsernameToken not allowed");
} catch (WSSecurityException ex) {
- assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
+ assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
}
}
-
-
+
+
/**
* Test using a UsernameToken derived key for signing a SOAP body. In this test the
* user is "colm" rather than "bob", and so signature verification should fail.
@@ -123,23 +123,23 @@ public class UTSignatureTest extends org
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
-
+
WSSecUsernameToken builder = new WSSecUsernameToken();
builder.setUserInfo("colm", "security");
builder.addDerivedKey(true, null, 1000);
builder.prepare(doc);
-
+
WSSecSignature sign = new WSSecSignature();
sign.setCustomTokenValueType(WSConstants.USERNAMETOKEN_NS + "#UsernameToken");
sign.setCustomTokenId(builder.getId());
sign.setSecretKey(builder.getDerivedKey());
sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
sign.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
-
+
Document signedDoc = sign.build(doc, null, secHeader);
builder.prependToHeader(secHeader);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(signedDoc);
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
@@ -153,13 +153,13 @@ public class UTSignatureTest extends org
// expected
}
}
-
+
/**
* Test using a UsernameToken derived key for signing a SOAP body via WSHandler
*/
@org.junit.Test
public void testHandlerSignature() throws Exception {
-
+
final WSSConfig cfg = WSSConfig.getNewInstance();
RequestData reqData = new RequestData();
reqData.setWssConfig(cfg);
@@ -167,18 +167,18 @@ public class UTSignatureTest extends org
messageContext.put(WSHandlerConstants.PW_CALLBACK_REF, callbackHandler);
reqData.setMsgContext(messageContext);
reqData.setUsername("bob");
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
CustomHandler handler = new CustomHandler();
HandlerAction action = new HandlerAction(WSConstants.UT_SIGN);
handler.send(
- doc,
- reqData,
+ doc,
+ reqData,
Collections.singletonList(action),
true
);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -187,21 +187,21 @@ public class UTSignatureTest extends org
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
WSHandlerResult results = verify(doc);
WSSecurityEngineResult actionResult =
results.getActionResults().get(WSConstants.UT_SIGN).get(0);
- java.security.Principal principal =
+ java.security.Principal principal =
(java.security.Principal) actionResult.get(WSSecurityEngineResult.TAG_PRINCIPAL);
assertTrue(principal.getName().contains("bob"));
}
-
+
/**
* Test using a UsernameToken derived key for signing a SOAP body via WSHandler
*/
@org.junit.Test
public void testHandlerSignatureIterations() throws Exception {
-
+
final WSSConfig cfg = WSSConfig.getNewInstance();
RequestData reqData = new RequestData();
reqData.setWssConfig(cfg);
@@ -210,18 +210,18 @@ public class UTSignatureTest extends org
messageContext.put(WSHandlerConstants.DERIVED_KEY_ITERATIONS, "1234");
reqData.setMsgContext(messageContext);
reqData.setUsername("bob");
-
+
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
CustomHandler handler = new CustomHandler();
HandlerAction action = new HandlerAction(WSConstants.UT_SIGN);
handler.send(
- doc,
- reqData,
+ doc,
+ reqData,
Collections.singletonList(action),
true
);
-
- String outputString =
+
+ String outputString =
XMLUtils.PrettyDocumentToString(doc);
assertTrue(outputString.contains("wsse:Username"));
assertFalse(outputString.contains("wsse:Password"));
@@ -231,37 +231,37 @@ public class UTSignatureTest extends org
if (LOG.isDebugEnabled()) {
LOG.debug(outputString);
}
-
+
WSHandlerResult results = verify(doc);
WSSecurityEngineResult actionResult =
results.getActionResults().get(WSConstants.UT_SIGN).get(0);
- java.security.Principal principal =
+ java.security.Principal principal =
(java.security.Principal) actionResult.get(WSSecurityEngineResult.TAG_PRINCIPAL);
assertTrue(principal.getName().contains("bob"));
}
-
+
/**
* Verifies the soap envelope.
- *
+ *
* @param doc soap envelope
* @throws java.lang.Exception Thrown when there is a problem in verification
*/
private WSHandlerResult verify(Document doc) throws Exception {
return verify(doc, true);
}
-
+
private WSHandlerResult verify(
- Document doc,
+ Document doc,
boolean allowUsernameTokenDerivedKeys
) throws Exception {
WSSecurityEngine secEngine = new WSSecurityEngine();
-
+
RequestData requestData = new RequestData();
requestData.setAllowUsernameTokenNoPassword(allowUsernameTokenDerivedKeys);
requestData.setCallbackHandler(callbackHandler);
requestData.setSigVerCrypto(crypto);
requestData.setDecCrypto(crypto);
-
+
return secEngine.processSecurityHeader(doc, requestData);
}