You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fx-dev@ws.apache.org by we...@apache.org on 2006/03/06 16:08:06 UTC
svn commit: r383561 [1/2] - /webservices/wss4j/trunk/test/wssec/
Author: werner
Date: Mon Mar 6 07:08:02 2006
New Revision: 383561
URL: http://svn.apache.org/viewcvs?rev=383561&view=rev
Log:
These are the test cases for the refactored message classes. The old
test cases were removed - the old message classes are depreceated, no
more bug fixes for them.
Added:
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew10.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew11.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew12.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew13.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew14.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew3.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew6.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew7.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNew8.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityNewSOAP12.java
Removed:
webservices/wss4j/trunk/test/wssec/TestWSSecurity.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity10.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity11.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity12.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity13.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity14.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity2.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity3.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity5.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity6.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity7.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity8.java
webservices/wss4j/trunk/test/wssec/TestWSSecurity9.java
webservices/wss4j/trunk/test/wssec/TestWSSecuritySOAP12.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityST1.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityST2.java
webservices/wss4j/trunk/test/wssec/TestWSSecurityST3.java
Modified:
webservices/wss4j/trunk/test/wssec/PackageTests.java
Modified: webservices/wss4j/trunk/test/wssec/PackageTests.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/PackageTests.java?rev=383561&r1=383560&r2=383561&view=diff
==============================================================================
--- webservices/wss4j/trunk/test/wssec/PackageTests.java (original)
+++ webservices/wss4j/trunk/test/wssec/PackageTests.java Mon Mar 6 07:08:02 2006
@@ -49,28 +49,25 @@
public static Test suite() {
TestSuite suite = new TestSuite();
- suite.addTestSuite(TestWSSecurity.class);
- suite.addTestSuite(TestWSSecurity2.class);
- suite.addTestSuite(TestWSSecurity3.class);
-// suite.addTestSuite(TestWSSecurity4.class);
- suite.addTestSuite(TestWSSecurity5.class);
- suite.addTestSuite(TestWSSecurity6.class);
- suite.addTestSuite(TestWSSecurity7.class);
- suite.addTestSuite(TestWSSecurity8.class);
- suite.addTestSuite(TestWSSecurity9.class);
- suite.addTestSuite(TestWSSecurity11.class);
- suite.addTestSuite(TestWSSecurity12.class);
- suite.addTestSuite(TestWSSecurity13.class);
- suite.addTestSuite(TestWSSecurity14.class);
- suite.addTestSuite(TestWSSecuritySOAP12.class);
- // suite.addTestSuite(TestWSSecurityHooks.class);
- suite.addTestSuite(TestWSSecurityST1.class);
- suite.addTestSuite(TestWSSecurityST2.class);
- suite.addTestSuite(TestWSSecurityST3.class);
- suite.addTestSuite(TestWSSecurityNew15.class);
+ suite.addTestSuite(TestWSSecurityNew.class);
suite.addTestSuite(TestWSSecurityNew2.class);
+ suite.addTestSuite(TestWSSecurityNew3.class);
+// suite.addTestSuite(TestWSSecurity4.class);
suite.addTestSuite(TestWSSecurityNew5.class);
+ suite.addTestSuite(TestWSSecurityNew6.class);
+ suite.addTestSuite(TestWSSecurityNew7.class);
+ suite.addTestSuite(TestWSSecurityNew8.class);
suite.addTestSuite(TestWSSecurityNew9.class);
+ suite.addTestSuite(TestWSSecurityNew10.class);
+ suite.addTestSuite(TestWSSecurityNew11.class);
+ suite.addTestSuite(TestWSSecurityNew12.class);
+ suite.addTestSuite(TestWSSecurityNew13.class);
+ suite.addTestSuite(TestWSSecurityNew14.class);
+ suite.addTestSuite(TestWSSecurityNewSOAP12.class);
+ // suite.addTestSuite(TestWSSecurityHooks.class);
+ suite.addTestSuite(TestWSSecurityNewST1.class);
+ suite.addTestSuite(TestWSSecurityNewST2.class);
+ suite.addTestSuite(TestWSSecurityNewST3.class);
suite.addTestSuite(TestWSSecurityNewDK.class);
return suite;
}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew10.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew10.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew10.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew10.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,245 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSConstants;
+import org.apache.ws.security.WSPasswordCallback;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecEncrypt;
+import org.apache.ws.security.message.WSSecUsernameToken;
+import org.apache.ws.security.message.WSSecHeader;
+import org.apache.ws.security.message.token.Reference;
+import org.apache.ws.security.message.token.SecurityTokenReference;
+import org.apache.ws.security.util.WSSecurityUtil;
+import org.w3c.dom.Document;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+/**
+ * TestCase10 for testing HMAC_SHA1 in wss4j. Based on TestCase9.
+ *
+ * The objective of this TestCase is to test the HMAC_SHA1 signature.
+ *
+ * @author Dimuthu Leelarathne. (muthulee@yahoo.com)
+ */
+public class TestWSSecurityNew10 extends TestCase implements CallbackHandler {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew10.class);
+
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ + " <soapenv:Body>"
+ + " <ns1:testMethod xmlns:ns1=\"http://axis/service/security/test10/LogTestService10\"></ns1:testMethod>"
+ + " </soapenv:Body>" + "</soapenv:Envelope>";
+
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+
+ static final Crypto crypto = CryptoFactory.getInstance();
+
+ MessageContext msgContext;
+
+ Message message;
+
+ private byte[] sharedSecret = "SriLankaSriLankaSriLanka".getBytes();
+
+ /**
+ * TestWSSecurity constructor <p/>
+ *
+ * @param name
+ * name of the test
+ */
+ public TestWSSecurityNew10(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew10.class);
+ }
+
+ /**
+ * Main method <p/>
+ *
+ * @param args
+ * command line args
+ */
+ // public static void main(String[] args) {
+ // junit.textui.TestRunner.run(suite());
+ // }
+ /**
+ * Setup method <p/>
+ *
+ * @throws Exception
+ * Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ message = getSOAPMessage();
+ }
+
+ /**
+ * Constructs a soap envelope <p/>
+ *
+ * @return soap envelope
+ * @throws Exception
+ * if there is any problem constructing the soap envelope
+ */
+ protected Message getSOAPMessage() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg;
+ }
+
+ /**
+ * Test that encrypts and signs a WS-Security envelope, then performs
+ * verification and decryption. <p/>
+ *
+ * @throws Exception
+ * Thrown when there is any problem in signing, encryption,
+ * decryption, or verification
+ */
+ public void testEMBED_SECURITY_TOKEN_REF() throws Exception {
+
+ SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
+ SOAPEnvelope envelope = null;
+ WSSecEncrypt wsEncrypt = new WSSecEncrypt();
+
+ // Get the message as document
+ log.info("Before Encryption....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ /*
+ * Step 1 :: Add a UserNameToken. Step 2 :: Add an Id to it. Step 3 ::
+ * Create a Reference to the UserNameToken. Step 4 :: Setting necessary
+ * parameters in WSEncryptBody. Step 5 :: Encrypt using the using the
+ * password of UserNameToken.
+ */
+
+ // Step 1
+ String username = "Dimthu";
+ String password = "Sri Lanka Sri Lanka UOM ";
+ byte[] key = password.getBytes();
+
+ WSSecUsernameToken builder = new WSSecUsernameToken();
+ builder.setPasswordType(WSConstants.PASSWORD_TEXT);
+ builder.setUserInfo(username, password);
+ builder.build(doc, secHeader);
+
+ // Step 3 ::
+ Reference ref = new Reference(doc);
+ ref.setURI("#" + builder.getId());
+ ref.setValueType("UsernameToken");
+ SecurityTokenReference secRef = new SecurityTokenReference(doc);
+ secRef.setReference(ref);
+
+ // adding the namespace
+ WSSecurityUtil.setNamespace(secRef.getElement(), WSConstants.WSSE_NS,
+ WSConstants.WSSE_PREFIX);
+
+ // Step 4 ::
+ wsEncrypt.setKeyIdentifierType(WSConstants.EMBED_SECURITY_TOKEN_REF);
+ wsEncrypt.setSecurityTokenReference(secRef);
+ wsEncrypt.setKey(key);
+ wsEncrypt.setSymmetricEncAlgorithm(WSConstants.TRIPLE_DES);
+
+ // Step 4 :: Encrypting using the key.
+ Document encDoc = wsEncrypt.build(doc, crypto, secHeader);
+
+ /*
+ * convert the resulting document into a message first. The
+ * toSOAPMessage() mehtod performs the necessary c14n call to properly
+ * set up the signed document and convert it into a SOAP message. After
+ * that we extract it as a document again for further processing.
+ */
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(encDoc);
+
+ if (log.isDebugEnabled()) {
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope()
+ .getAsDOM(), new PrintWriter(System.out));
+ }
+ log.info("Encryption Done\n");
+ // verifyEMBED_SECURITY_TOKEN_REF(signedMsg.getSOAPEnvelope().getAsDocument());
+ }
+
+ /**
+ * Verifies the soap envelope <p/>
+ *
+ * @param doc
+ * @throws Exception
+ * Thrown when there is a problem in verification
+ */
+ private void verifyEMBED_SECURITY_TOKEN_REF(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, "", this, null);
+ log.info("Success ......");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
+ */
+ public void handle(Callback[] callbacks) throws IOException,
+ UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof WSPasswordCallback) {
+ WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
+ if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN) {
+ pc.setPassword("Sri Lanka Sri Lanka UOM ");
+ } else if (pc.getUsage() == WSPasswordCallback.DECRYPT) {
+ pc.setKey("Sri Lanka Sri Lanka UOM ".getBytes());
+
+ }
+ } else {
+ throw new UnsupportedCallbackException(callbacks[i],
+ "Unrecognized Callback");
+ }
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ junit.textui.TestRunner.run(suite());
+ }
+
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew11.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew11.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew11.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew11.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,409 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.SOAPConstants;
+import org.apache.ws.security.WSEncryptionPart;
+import org.apache.ws.security.WSConstants;
+import org.apache.ws.security.util.WSSecurityUtil;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.Vector;
+
+/**
+ * WS-Security Test Case
+ * <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com)
+ */
+public class TestWSSecurityNew11 extends TestCase {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew11.class);
+ static final String NS = "http://www.w3.org/2000/09/xmldsig#";
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SOAP-ENV:Body>" + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" + "<value xmlns=\"\">15</value>" + "</add>" + "</SOAP-ENV:Body>\r\n \r\n" + "</SOAP-ENV:Envelope>";
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+ static final Crypto crypto = CryptoFactory.getInstance("cryptoSKI.properties");
+
+ MessageContext msgContext;
+ SOAPEnvelope unsignedEnvelope;
+
+ /**
+ * TestWSSecurity constructor
+ * <p/>
+ *
+ * @param name name of the test
+ */
+ public TestWSSecurityNew11(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite
+ * <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew11.class);
+ }
+
+ /**
+ * Main method
+ * <p/>
+ *
+ * @param args command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ unsignedEnvelope = getSOAPEnvelope();
+ }
+
+ /**
+ * Constructs a soap envelope
+ * <p/>
+ *
+ * @return soap envelope
+ * @throws java.lang.Exception if there is any problem constructing the soap envelope
+ */
+ protected SOAPEnvelope getSOAPEnvelope() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg.getSOAPEnvelope();
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope.
+ * This test uses the direct reference key identifier (certificate included
+ * as a BinarySecurityToken (BST) in the message). The test signs the message
+ * body (SOAP Body) and uses the STRTransform to sign the embedded certificate
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testX509SignatureDirectSTR() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("wss4jcert", "security");
+ // builder.setUserInfo("john", "keypass");
+ SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(unsignedEnvelope.getAsDOM());
+ Vector parts = new Vector();
+
+ /*
+ * Set up to sign body and use STRTransorm to sign
+ * the signature token (e.g. X.509 certificate)
+ */
+ WSEncryptionPart encP =
+ new WSEncryptionPart(
+ soapConstants.getBodyQName().getLocalPart(),
+ soapConstants.getEnvelopeURI(),
+ "Content");
+ parts.add(encP);
+ encP =
+ new WSEncryptionPart(
+ "STRTransform",
+ soapConstants.getEnvelopeURI(),
+ "Content");
+ parts.add(encP);
+
+ builder.setParts(parts);
+ builder.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
+
+ log.info("Before Signing STR DirectReference....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+
+ /*
+ * convert the resulting document into a message first. The toSOAPMessage()
+ * mehtod performs the necessary c14n call to properly set up the signed
+ * document and convert it into a SOAP message. After that we extract it
+ * as a document again for further processing.
+ */
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed message with STR DirectReference key identifier:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After Signing STR DirectReference....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope.
+ * This test uses the IssuerSerialDirect reference key identifier (certificate included
+ * as a BinarySecurityToken (BST) in the message) but identified with IssuerSerialNumber
+ * and <b>not</b> with a Reference (relative URI). The test signs the message
+ * body (SOAP Body) and uses the STRTransform to sign the embedded certificate
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+// public void testX509SignatureISDirectSTR() throws Exception {
+// SOAPEnvelope envelope = null;
+// WSSignEnvelope builder = new WSSignEnvelope();
+// builder.setUserInfo("wss4jcert", "security");
+// // builder.setUserInfo("john", "keypass");
+// SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(unsignedEnvelope);
+// Vector parts = new Vector();
+//
+// /*
+// * Set up to sign body and use STRTransorm to sign
+// * the signature token (e.g. X.509 certificate)
+// */
+// WSEncryptionPart encP =
+// new WSEncryptionPart(
+// soapConstants.getBodyQName().getLocalPart(), // define the body
+// soapConstants.getEnvelopeURI(),
+// "Content");
+// parts.add(encP);
+// encP =
+// new WSEncryptionPart(
+// "STRTransform", // reserved word to use STRTransform
+// soapConstants.getEnvelopeURI(),
+// "Content");
+// parts.add(encP);
+//
+// builder.setParts(parts);
+// builder.setKeyIdentifierType(WSConstants.ISSUER_SERIAL_DIRECT);
+//
+// log.info("Before Signing STR ISDirect....");
+// Document doc = unsignedEnvelope.getAsDocument();
+// Document signedDoc = builder.build(doc, crypto);
+//
+// Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+// if (log.isDebugEnabled()) {
+// log.debug("Signed message with STR IssuerSerialDirect key identifier:");
+// XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+// }
+// signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+// log.info("After Signing STR ISDirect....");
+// verify(signedDoc);
+// }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope.
+ * This test uses the IssuerSerial reference key identifier (certificate not included
+ * in the message)and reads the certificate from a keystore using IssuerSerialNumber
+ * to identify it.
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+
+
+ public void testX509SignatureISSTR() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("wss4jcert", "security");
+ // builder.setUserInfo("john", "keypass");
+ SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(unsignedEnvelope.getAsDOM());
+ Vector parts = new Vector();
+
+ /*
+ * Set up to sign body and use STRTransorm to sign
+ * the signature token (e.g. X.509 certificate)
+ */
+ WSEncryptionPart encP =
+ new WSEncryptionPart(
+ soapConstants.getBodyQName().getLocalPart(), // define the body
+ soapConstants.getEnvelopeURI(),
+ "Content");
+ parts.add(encP);
+ encP =
+ new WSEncryptionPart(
+ "STRTransform", // reserved word to use STRTransform
+ soapConstants.getEnvelopeURI(),
+ "Content");
+ parts.add(encP);
+
+ builder.setParts(parts);
+ builder.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
+
+ log.info("Before Signing STR IS....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed message with STR IssuerSerial key identifier:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After Signing STR IS....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope.
+ * This test uses the SubjectKeyIdentifier key identifier (certificate not included
+ * in the message) and reads the certificate from a keystore using SKI
+ * to identify it.
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+
+
+ public void testX509SignatureSKISTR() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("wss4jcert", "security");
+ // builder.setUserInfo("john", "keypass");
+ SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(unsignedEnvelope.getAsDOM());
+ Vector parts = new Vector();
+
+ /*
+ * Set up to sign body and use STRTransorm to sign
+ * the signature token (e.g. X.509 certificate)
+ */
+ WSEncryptionPart encP =
+ new WSEncryptionPart(
+ soapConstants.getBodyQName().getLocalPart(), // define the body
+ soapConstants.getEnvelopeURI(),
+ "Content");
+ parts.add(encP);
+ encP =
+ new WSEncryptionPart(
+ "STRTransform", // reserved word to use STRTransform
+ soapConstants.getEnvelopeURI(),
+ "Content");
+ parts.add(encP);
+
+ builder.setParts(parts);
+ builder.setKeyIdentifierType(WSConstants.SKI_KEY_IDENTIFIER);
+
+ log.info("Before Signing STR SKI....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed message with STR SKI key identifier:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After Signing STR SKI....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope.
+ * This test uses the SubjectKeyIdentifierDirect key identifier (certificate included
+ * in the message).
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+
+
+// public void testX509SignatureSKIDirectSTR() throws Exception {
+// SOAPEnvelope envelope = null;
+// WSSignEnvelope builder = new WSSignEnvelope();
+// builder.setUserInfo("wss4jcert", "security");
+// // builder.setUserInfo("john", "keypass");
+// SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(unsignedEnvelope);
+// Vector parts = new Vector();
+//
+// /*
+// * Set up to sign body and use STRTransorm to sign
+// * the signature token (e.g. X.509 certificate)
+// */
+// WSEncryptionPart encP =
+// new WSEncryptionPart(
+// soapConstants.getBodyQName().getLocalPart(), // define the body
+// soapConstants.getEnvelopeURI(),
+// "Content");
+// parts.add(encP);
+// encP =
+// new WSEncryptionPart(
+// "STRTransform", // reserved word to use STRTransform
+// soapConstants.getEnvelopeURI(),
+// "Content");
+// parts.add(encP);
+//
+// builder.setParts(parts);
+// builder.setKeyIdentifierType(WSConstants.SKI_KEY_IDENTIFIER_DIRECT);
+//
+// log.info("Before Signing STR SKIDirect....");
+// Document doc = unsignedEnvelope.getAsDocument();
+// Document signedDoc = builder.build(doc, crypto);
+//
+// Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+// if (log.isDebugEnabled()) {
+// log.debug("Signed message with STR SKIDirect key identifier:");
+// XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+// }
+// signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+// log.info("After Signing STR SKIDirect....");
+// verify(signedDoc);
+// }
+
+
+ /**
+ * Verifies the soap envelope
+ *
+ * @param env soap envelope
+ * @throws java.lang.Exception Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, null, null, crypto);
+ }
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew12.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew12.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew12.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew12.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,253 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.WSConstants;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import org.apache.xml.security.signature.XMLSignature;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+
+/**
+ * WS-Security Test Case
+ * <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com)
+ * @author Werner Dittmann (Werner.Dittmann@siemens.com)
+ */
+public class TestWSSecurityNew12 extends TestCase {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew12.class);
+ static final String NS = "http://www.w3.org/2000/09/xmldsig#";
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SOAP-ENV:Body>" + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" + "<value xmlns=\"\">15</value>" + "</add>" + "</SOAP-ENV:Body>\r\n \r\n" + "</SOAP-ENV:Envelope>";
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+ static final Crypto crypto = CryptoFactory.getInstance("cryptoSKI.properties");
+
+ MessageContext msgContext;
+ SOAPEnvelope unsignedEnvelope;
+
+ /**
+ * TestWSSecurity constructor
+ * <p/>
+ *
+ * @param name name of the test
+ */
+ public TestWSSecurityNew12(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite
+ * <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew12.class);
+ }
+
+ /**
+ * Main method
+ * <p/>
+ *
+ * @param args command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ unsignedEnvelope = getSOAPEnvelope();
+ }
+
+ /**
+ * Constructs a soap envelope
+ * <p/>
+ *
+ * @return soap envelope
+ * @throws java.lang.Exception if there is any problem constructing the soap envelope
+ */
+ protected SOAPEnvelope getSOAPEnvelope() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg.getSOAPEnvelope();
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope using SubjectKeyIdentifier.
+ * This test uses the SubjectKeyIdentifier to identify the certificate. It
+ * uses the Direct version, that is it embedds the certificate in the message.
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testX509SignatureDSA_SKI() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("wss4jcertDSA", "security");
+ builder.setKeyIdentifierType(WSConstants.SKI_KEY_IDENTIFIER);
+ builder.setSignatureAlgorithm(XMLSignature.ALGO_ID_SIGNATURE_DSA);
+
+ // builder.setUserInfo("john", "keypass");
+ log.info("Before SigningDSA_SKIDirect....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+
+ /*
+ * convert the resulting document into a message first. The toSOAPMessage()
+ * mehtod performs the necessary c14n call to properly set up the signed
+ * document and convert it into a SOAP message. After that we extract it
+ * as a document again for further processing.
+ */
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed message with DSA_SKI key identifier:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After SigningDSA_SKIDirect....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope using SubjectKeyIdentifier.
+ * This test uses the SubjectKeyIdentifier to identify the certificate.
+ * It gets a certificate with a DSA public key algo to sign, WSSignEnvelope shall
+ * detect the algo and set the signature algo accordingly.
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testX509SignatureDSA_Autodetect() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("wss4jcertDSA", "security");
+ builder.setKeyIdentifierType(WSConstants.SKI_KEY_IDENTIFIER);
+
+ // builder.setUserInfo("john", "keypass");
+ log.info("Before SigningDSA_Autodetect....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+
+ /*
+ * convert the resulting document into a message first. The toSOAPMessage()
+ * mehtod performs the necessary c14n call to properly set up the signed
+ * document and convert it into a SOAP message. After that we extract it
+ * as a document again for further processing.
+ */
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed message with DSA_Autodetect:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After SigningDSA_Autodetect....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope using SubjectKeyIdentifier.
+ * This test uses the SubjectKeyIdentifier to identify the certificate.
+ * It gets a certificate with a RSA public key algo to sign, WSSignEnvelope shall
+ * detect the algo and set the signature algo accordingly.
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testX509SignatureRSA_Autodetect() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("wss4jcert", "security");
+ builder.setKeyIdentifierType(WSConstants.SKI_KEY_IDENTIFIER);
+
+ // builder.setUserInfo("john", "keypass");
+ log.info("Before SigningRSA_Autodetect....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+
+ /*
+ * convert the resulting document into a message first. The toSOAPMessage()
+ * mehtod performs the necessary c14n call to properly set up the signed
+ * document and convert it into a SOAP message. After that we extract it
+ * as a document again for further processing.
+ */
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed message with RSA Autodetect:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After SigningRSA_Autodetect....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Verifies the soap envelope
+ *
+ * @param env soap envelope
+ * @throws java.lang.Exception Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, null, null, crypto);
+ }
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew13.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew13.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew13.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew13.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,190 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSPasswordCallback;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.WSConstants;
+import org.apache.ws.security.message.WSSecUsernameToken;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+
+import org.apache.xml.security.signature.XMLSignature;
+
+import org.w3c.dom.Document;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+
+/**
+ * WS-Security Test Case
+ * <p/>
+ *
+ * @author Werner Dittmann (Wern.erDittmann@siemens.com)
+ */
+public class TestWSSecurityNew13 extends TestCase implements CallbackHandler {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew13.class);
+ static final String NS = "http://www.w3.org/2000/09/xmldsig#";
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SOAP-ENV:Body>" + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" + "<value xmlns=\"\">15</value>" + "</add>" + "</SOAP-ENV:Body>\r\n \r\n" + "</SOAP-ENV:Envelope>";
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+ MessageContext msgContext;
+ SOAPEnvelope unsignedEnvelope;
+
+ /**
+ * TestWSSecurity constructor
+ * <p/>
+ *
+ * @param name name of the test
+ */
+ public TestWSSecurityNew13(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite
+ * <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew13.class);
+ }
+
+ /**
+ * Main method
+ * <p/>
+ *
+ * @param args command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ unsignedEnvelope = getSOAPEnvelope();
+ }
+
+ /**
+ * Constructs a soap envelope
+ * <p/>
+ *
+ * @return soap envelope
+ * @throws java.lang.Exception if there is any problem constructing the soap envelope
+ */
+ protected SOAPEnvelope getSOAPEnvelope() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg.getSOAPEnvelope();
+ }
+
+
+ /**
+ * Test the specific signing mehtod that use UsernameToken values
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testUsernameTokenSigning() throws Exception {
+ SOAPEnvelope envelope = null;
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ WSSecUsernameToken builder = new WSSecUsernameToken();
+ builder.setPasswordType(WSConstants.PASSWORD_TEXT);
+ builder.setUserInfo("wernerd", "verySecret");
+ builder.addCreated();
+ builder.addNonce();
+ builder.prepare(doc);
+
+ WSSecSignature sign = new WSSecSignature();
+ sign.setUsernameToken(builder);
+ sign.setKeyIdentifierType(WSConstants.UT_SIGNING);
+ sign.setSignatureAlgorithm(XMLSignature.ALGO_ID_MAC_HMAC_SHA1);
+ log.info("Before signing....");
+ sign.build(doc, null, secHeader);
+ log.info("Before adding UsernameToken PW Text....");
+ builder.prependToHeader(secHeader);
+ Document signedDoc = doc;
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Message with UserNameToken PW Text:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After adding UsernameToken PW Text....");
+ verify(signedDoc);
+ }
+ /**
+ * Verifies the soap envelope
+ * <p/>
+ *
+ * @param env soap envelope
+ * @throws java.lang.Exception Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ log.info("Before verifying UsernameToken....");
+ secEngine.processSecurityHeader(doc, null, this, null);
+ log.info("After verifying UsernameToken....");
+ }
+
+ public void handle(Callback[] callbacks)
+ throws IOException, UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof WSPasswordCallback) {
+ WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
+ /*
+ * here call a function/method to lookup the password for
+ * the given identifier (e.g. a user name or keystore alias)
+ * e.g.: pc.setPassword(passStore.getPassword(pc.getIdentfifier))
+ * for Testing we supply a fixed name here.
+ */
+ pc.setPassword("verySecret");
+ } else {
+ throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
+ }
+ }
+ }
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew14.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew14.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew14.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew14.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.WSConstants;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+
+/**
+ * WS-Security Test Case
+ * <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com)
+ */
+public class TestWSSecurityNew14 extends TestCase {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew14.class);
+ static final String NS = "http://www.w3.org/2000/09/xmldsig#";
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SOAP-ENV:Body>" + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" + "<value xmlns=\"\">15</value>" + "</add>" + "</SOAP-ENV:Body>\r\n \r\n" + "</SOAP-ENV:Envelope>";
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+ static final Crypto crypto = CryptoFactory.getInstance();
+
+ MessageContext msgContext;
+ SOAPEnvelope unsignedEnvelope;
+
+ /**
+ * TestWSSecurity constructor
+ * <p/>
+ *
+ * @param name name of the test
+ */
+ public TestWSSecurityNew14(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite
+ * <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew14.class);
+ }
+
+ /**
+ * Main method
+ * <p/>
+ *
+ * @param args command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ unsignedEnvelope = getSOAPEnvelope();
+ }
+
+ /**
+ * Constructs a soap envelope
+ * <p/>
+ *
+ * @return soap envelope
+ * @throws java.lang.Exception if there is any problem constructing the soap envelope
+ */
+ protected SOAPEnvelope getSOAPEnvelope() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg.getSOAPEnvelope();
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope.
+ * The test uses the ThumbprintSHA1 key identifier type.
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testX509SignatureThumb() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
+ builder.setKeyIdentifierType(WSConstants.THUMBPRINT_IDENTIFIER);
+ // builder.setUserInfo("john", "keypass");
+ log.info("Before Signing ThumbprintSHA1....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+
+ /*
+ * convert the resulting document into a message first. The toSOAPMessage()
+ * mehtod performs the necessary c14n call to properly set up the signed
+ * document and convert it into a SOAP message. After that we extract it
+ * as a document again for further processing.
+ */
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed message with ThumbprintSHA1 key identifier:");
+ XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After Signing ThumbprintSHA1....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Test that signs (twice) and verifies a WS-Security envelope.
+ * The test uses the ThumbprintSHA1 key identifier type.
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testDoubleX509SignatureThumb() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
+ // builder.setUserInfo("john", "keypass");
+ builder.setKeyIdentifierType(WSConstants.THUMBPRINT_IDENTIFIER);
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = builder.build(doc, crypto, secHeader);
+ Document signedDoc1 = builder.build(signedDoc, crypto, secHeader);
+ verify(signedDoc1);
+ }
+
+ /**
+ * Verifies the soap envelope.
+ * This method verfies all the signature generated.
+ *
+ * @param env soap envelope
+ * @throws java.lang.Exception Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, null, null, crypto);
+ }
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew3.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew3.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew3.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew3.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSPasswordCallback;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * WS-Security Test Case
+ * <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com)
+ */
+public class TestWSSecurityNew3 extends TestCase implements CallbackHandler {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew3.class);
+ static final String NS = "http://www.w3.org/2000/09/xmldsig#";
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SOAP-ENV:Body>" + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" + "<value xmlns=\"\">15</value>" + "</add>" + "</SOAP-ENV:Body>\r\n \r\n" + "</SOAP-ENV:Envelope>";
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+ static final Crypto crypto = CryptoFactory.getInstance();
+ MessageContext msgContext;
+ SOAPEnvelope unsignedEnvelope;
+
+ /**
+ * TestWSSecurity constructor
+ * <p/>
+ *
+ * @param name name of the test
+ */
+ public TestWSSecurityNew3(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite
+ * <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew3.class);
+ }
+
+ /**
+ * Main method
+ * <p/>
+ *
+ * @param args command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ unsignedEnvelope = getSOAPEnvelope();
+ }
+
+ /**
+ * Constructs a soap envelope
+ * <p/>
+ *
+ * @return soap envelope
+ * @throws java.lang.Exception if there is any problem constructing the soap envelope
+ */
+ protected SOAPEnvelope getSOAPEnvelope() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg.getSOAPEnvelope();
+ }
+
+ /**
+ * Test that signs and verifies a WS-Security envelope
+ * <p/>
+ *
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ */
+ public void testX509Signature() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSecSignature builder = new WSSecSignature();
+ builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
+ log.info("Before Signing....");
+ Document doc = unsignedEnvelope.getAsDocument();
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+ Document signedDoc = builder.build(doc, CryptoFactory.getInstance(), secHeader);
+
+ /*
+ * convert the resulting document into a message first. The toSOAPMessage()
+ * mehtod performs the necessary c14n call to properly set up the signed
+ * document and convert it into a SOAP message. After that we extract it
+ * as a document again for further processing.
+ */
+
+ Message signedMsg = (Message) SOAPUtil.toSOAPMessage(signedDoc);
+ signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After Signing....");
+ verify(signedDoc);
+ }
+
+ /**
+ * Test that signs (twice) and verifies a WS-Security envelope
+ * <p>
+ * @throws java.lang.Exception Thrown when there is any problem in signing or verification
+ public void testDoubleX509Signature() throws Exception {
+ SOAPEnvelope envelope = null;
+ WSSignEnvelope builder = new WSSignEnvelope();
+ Document doc = unsignedEnvelope.getAsDocument();
+ Document signedDoc = builder.build(doc);
+ signedDoc = builder.build(signedDoc);
+ verify(signedDoc);
+ }
+ */
+
+ /**
+ * Verifies the soap envelope
+ * <p/>
+ *
+ * @param env soap envelope
+ * @throws java.lang.Exception Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, null, this, crypto);
+ }
+
+ public void handle(Callback[] callbacks)
+ throws IOException, UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof WSPasswordCallback) {
+ WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
+ /*
+ * here call a function/method to lookup the password for
+ * the given identifier (e.g. a user name or keystore alias)
+ * e.g.: pc.setPassword(passStore.getPassword(pc.getIdentfifier))
+ * for Testing we supply a fixed name here.
+ */
+ pc.setPassword("password");
+ } else {
+ throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
+ }
+ }
+ }
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew6.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew6.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew6.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew6.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,204 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.SOAPPart;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSPasswordCallback;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecEncrypt;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+/**
+ * WS-Security Test Case <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com)
+ */
+public class TestWSSecurityNew6 extends TestCase implements CallbackHandler {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew6.class);
+
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ + " <soapenv:Body>"
+ + " <ns1:testMethod xmlns:ns1=\"http://axis/service/security/test6/LogTestService6\"></ns1:testMethod>"
+ + " </soapenv:Body>" + "</soapenv:Envelope>";
+
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+
+ static final Crypto crypto = CryptoFactory.getInstance();
+
+ MessageContext msgContext;
+
+ Message message;
+
+ /**
+ * TestWSSecurity constructor <p/>
+ *
+ * @param name
+ * name of the test
+ */
+ public TestWSSecurityNew6(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew6.class);
+ }
+
+ /**
+ * Main method <p/>
+ *
+ * @param args
+ * command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method <p/>
+ *
+ * @throws Exception
+ * Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ message = getSOAPMessage();
+ }
+
+ /**
+ * Constructs a soap envelope <p/>
+ *
+ * @return soap envelope
+ * @throws Exception
+ * if there is any problem constructing the soap envelope
+ */
+ protected Message getSOAPMessage() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg;
+ }
+
+ /**
+ * Test that encrypts and signs a WS-Security envelope, then performs
+ * verification and decryption <p/>
+ *
+ * @throws Exception
+ * Thrown when there is any problem in signing, encryption,
+ * decryption, or verification
+ */
+ public void testEncryptionSigning() throws Exception {
+ SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
+ SOAPEnvelope envelope = null;
+ WSSecEncrypt encrypt = new WSSecEncrypt();
+ WSSecSignature sign = new WSSecSignature();
+ encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e");
+ sign.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
+ log.info("Before Encryption....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document encryptedDoc = encrypt.build(doc, crypto, secHeader);
+ Document encryptedSignedDoc = sign.build(encryptedDoc, crypto,
+ secHeader);
+ /*
+ * convert the resulting document into a message first. The
+ * toSOAPMessage() mehtod performs the necessary c14n call to properly
+ * set up the signed document and convert it into a SOAP message. After
+ * that we extract it as a document again for further processing.
+ */
+
+ Message encryptedMsg = (Message) SOAPUtil
+ .toSOAPMessage(encryptedSignedDoc);
+ String s = encryptedMsg.getSOAPPartAsString();
+ ((SOAPPart) message.getSOAPPart()).setCurrentMessage(s,
+ SOAPPart.FORM_STRING);
+
+ Document encryptedSignedDoc1 = message.getSOAPEnvelope()
+ .getAsDocument();
+ log.info("After Encryption....");
+ verify(encryptedSignedDoc1);
+ }
+
+ /**
+ * Verifies the soap envelope <p/>
+ *
+ * @param envelope
+ * @throws Exception
+ * Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, null, this, crypto);
+ SOAPUtil.updateSOAPMessage(doc, message);
+ if (log.isDebugEnabled()) {
+ XMLUtils.PrettyElementToWriter(
+ message.getSOAPEnvelope().getAsDOM(), new PrintWriter(
+ System.out));
+ }
+ }
+
+ public void handle(Callback[] callbacks) throws IOException,
+ UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof WSPasswordCallback) {
+ WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
+ /*
+ * here call a function/method to lookup the password for the
+ * given identifier (e.g. a user name or keystore alias) e.g.:
+ * pc.setPassword(passStore.getPassword(pc.getIdentfifier)) for
+ * Testing we supply a fixed name here.
+ */
+ pc.setPassword("security");
+ } else {
+ throw new UnsupportedCallbackException(callbacks[i],
+ "Unrecognized Callback");
+ }
+ }
+ }
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew7.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew7.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew7.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew7.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,195 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSPasswordCallback;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecEncrypt;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+/**
+ * WS-Security Test Case <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com)
+ */
+public class TestWSSecurityNew7 extends TestCase implements CallbackHandler {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew7.class);
+
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ + " <soapenv:Body>"
+ + " <ns1:testMethod xmlns:ns1=\"urn:LogTestService7\"></ns1:testMethod>"
+ + " </soapenv:Body>" + "</soapenv:Envelope>";
+
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+
+ static final Crypto crypto = CryptoFactory.getInstance();
+
+ MessageContext msgContext;
+
+ Message message;
+
+ /**
+ * TestWSSecurity constructor <p/>
+ *
+ * @param name
+ * name of the test
+ */
+ public TestWSSecurityNew7(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew7.class);
+ }
+
+ /**
+ * Main method <p/>
+ *
+ * @param args
+ * command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method <p/>
+ *
+ * @throws Exception
+ * Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ message = getSOAPMessage();
+ }
+
+ /**
+ * Constructs a soap envelope <p/>
+ *
+ * @return soap envelope
+ * @throws Exception
+ * if there is any problem constructing the soap envelope
+ */
+ protected Message getSOAPMessage() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg;
+ }
+
+ /**
+ * Test that encrypt and then again encrypts (Super encryption) WS-Security
+ * envelope and then verifies ist <p/>
+ *
+ * @throws Exception
+ * Thrown when there is any problem in encryption or
+ * verification
+ */
+ public void testEncryptionEncryption() throws Exception {
+ SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
+ SOAPEnvelope envelope = null;
+ WSSecEncrypt encrypt = new WSSecEncrypt();
+ encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e");
+ log.info("Before Encryption....");
+ Document doc = unsignedEnvelope.getAsDocument();
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document encryptedDoc = encrypt.build(doc, crypto, secHeader);
+ Document encryptedEncryptedDoc = encrypt.build(encryptedDoc, crypto,
+ secHeader);
+
+ /*
+ * convert the resulting document into a message first. The
+ * toSOAPMessage() mehtod performs the necessary c14n call to properly
+ * set up the signed document and convert it into a SOAP message. After
+ * that we extract it as a document again for further processing.
+ */
+
+ Message encryptedMsg = (Message) SOAPUtil
+ .toSOAPMessage(encryptedEncryptedDoc);
+ encryptedEncryptedDoc = encryptedMsg.getSOAPEnvelope().getAsDocument();
+ log.info("After Encryption....");
+ verify(encryptedEncryptedDoc);
+ }
+
+ /**
+ * Verifies the soap envelope <p/>
+ *
+ * @param envelope
+ * @throws Exception
+ * Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, null, this, crypto);
+ SOAPUtil.updateSOAPMessage(doc, message);
+ if (log.isDebugEnabled()) {
+ XMLUtils.PrettyElementToWriter(
+ message.getSOAPEnvelope().getAsDOM(), new PrintWriter(
+ System.out));
+ }
+ }
+
+ public void handle(Callback[] callbacks) throws IOException,
+ UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof WSPasswordCallback) {
+ WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
+ /*
+ * here call a function/method to lookup the password for the
+ * given identifier (e.g. a user name or keystore alias) e.g.:
+ * pc.setPassword(passStore.getPassword(pc.getIdentfifier)) for
+ * Testing we supply a fixed name here.
+ */
+ pc.setPassword("security");
+ } else {
+ throw new UnsupportedCallbackException(callbacks[i],
+ "Unrecognized Callback");
+ }
+ }
+ }
+}
Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityNew8.java
URL: http://svn.apache.org/viewcvs/webservices/wss4j/trunk/test/wssec/TestWSSecurityNew8.java?rev=383561&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityNew8.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityNew8.java Mon Mar 6 07:08:02 2006
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.SOAPPart;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSPasswordCallback;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.WSConstants;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecEncrypt;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+/**
+ * WS-Security Test Case
+ * <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com)
+ */
+public class TestWSSecurityNew8 extends TestCase implements CallbackHandler {
+ private static Log log = LogFactory.getLog(TestWSSecurityNew8.class);
+ static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
+ " <soapenv:Body>" +
+ " <ns1:testMethod xmlns:ns1=\"http://axis/service/security/test6/LogTestService8\"></ns1:testMethod>" +
+ " </soapenv:Body>" +
+ "</soapenv:Envelope>";
+
+ static final WSSecurityEngine secEngine = new WSSecurityEngine();
+ static final Crypto crypto = CryptoFactory.getInstance();
+ MessageContext msgContext;
+ Message message;
+
+ /**
+ * TestWSSecurity constructor
+ * <p/>
+ *
+ * @param name name of the test
+ */
+ public TestWSSecurityNew8(String name) {
+ super(name);
+ }
+
+ /**
+ * JUnit suite
+ * <p/>
+ *
+ * @return a junit test suite
+ */
+ public static Test suite() {
+ return new TestSuite(TestWSSecurityNew8.class);
+ }
+
+ /**
+ * Main method
+ * <p/>
+ *
+ * @param args command line args
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Setup method
+ * <p/>
+ *
+ * @throws Exception Thrown when there is a problem in setup
+ */
+ protected void setUp() throws Exception {
+ AxisClient tmpEngine = new AxisClient(new NullProvider());
+ msgContext = new MessageContext(tmpEngine);
+ message = getSOAPMessage();
+ }
+
+ /**
+ * Constructs a soap envelope
+ * <p/>
+ *
+ * @return soap envelope
+ * @throws Exception if there is any problem constructing the soap envelope
+ */
+ protected Message getSOAPMessage() throws Exception {
+ InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+ Message msg = new Message(in);
+ msg.setMessageContext(msgContext);
+ return msg;
+ }
+
+ /**
+ * Test that first signs, then encrypts a WS-Security envelope.
+ * The test uses the IssuerSerial key identifier to get the keys for
+ * signature and encryption. Encryption uses 3DES.
+ * <p/>
+ *
+ * @throws Exception Thrown when there is any problem in signing, encryption,
+ * decryption, or verification
+ */
+ public void testSigningEncryptionIS3DES() throws Exception {
+ SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
+ SOAPEnvelope envelope = null;
+
+ WSSecEncrypt encrypt = new WSSecEncrypt();
+ encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e");
+ encrypt.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
+ encrypt.setSymmetricEncAlgorithm(WSConstants.TRIPLE_DES);
+
+ WSSecSignature sign = new WSSecSignature();
+ sign.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
+ sign.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
+
+ log.info("Before Sign/Encryption....");
+ Document doc = unsignedEnvelope.getAsDocument();
+
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document signedDoc = sign.build(doc, crypto, secHeader);
+ Document encryptedSignedDoc = encrypt.build(signedDoc, crypto, secHeader);
+ /*
+ * convert the resulting document into a message first. The toSOAPMessage()
+ * method performs the necessary c14n call to properly set up the signed
+ * document and convert it into a SOAP message. After that we extract it
+ * as a document again for further processing.
+ */
+
+ Message encryptedMsg = (Message) SOAPUtil.toSOAPMessage(encryptedSignedDoc);
+ if (log.isDebugEnabled()) {
+ log.debug("Signed and encrypted message with IssuerSerial key identifier (both), 3DES:");
+ XMLUtils.PrettyElementToWriter(encryptedMsg.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+
+ String s = encryptedMsg.getSOAPPartAsString();
+ ((SOAPPart)message.getSOAPPart()).setCurrentMessage(s, SOAPPart.FORM_STRING);
+
+ Document encryptedSignedDoc1 = message.getSOAPEnvelope().getAsDocument();
+ log.info("After Sign/Encryption....");
+ verify(encryptedSignedDoc1);
+ }
+
+ /**
+ * Verifies the soap envelope
+ * <p/>
+ *
+ * @param doc
+ * @throws Exception Thrown when there is a problem in verification
+ */
+ private void verify(Document doc) throws Exception {
+ secEngine.processSecurityHeader(doc, null, this, crypto);
+ SOAPUtil.updateSOAPMessage(doc, message);
+ if (log.isDebugEnabled()) {
+ log.debug("Verfied and decrypted message:");
+ XMLUtils.PrettyElementToWriter(message.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
+ }
+ }
+
+ public void handle(Callback[] callbacks)
+ throws IOException, UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof WSPasswordCallback) {
+ WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
+ /*
+ * here call a function/method to lookup the password for
+ * the given identifier (e.g. a user name or keystore alias)
+ * e.g.: pc.setPassword(passStore.getPassword(pc.getIdentfifier))
+ * for Testing we supply a fixed name here.
+ */
+ pc.setPassword("security");
+ } else {
+ throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
+ }
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: wss4j-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: wss4j-dev-help@ws.apache.org