FileDocCategorySizeDatePackage
SymmetricBindingTest.javaAPI DocExample13327Tue May 29 16:57:44 BST 2007com.sun.xml.wss.impl

SymmetricBindingTest

public class SymmetricBindingTest extends TestCase

Fields Summary
private static HashMap
client
private static HashMap
server
private static com.sun.xml.wss.impl.AlgorithmSuite
alg
Constructors Summary
public SymmetricBindingTest(String testName)

        
         
        super(testName);
    
Methods Summary
protected voidsetUp()

    	
    
public static junit.framework.Testsuite()

        TestSuite suite = new TestSuite(SymmetricBindingTest.class);                                                                                                 return suite;
    
protected voidtearDown()

    
public static voidtestSymmetricBindingTest()

       
	    //alg.setType(AlgorithmSuiteValue.Basic128);
            alg = new AlgorithmSuite(AlgorithmSuiteValue.Basic128.getDigAlgorithm(), AlgorithmSuiteValue.Basic128.getEncAlgorithm(), AlgorithmSuiteValue.Basic128.getSymKWAlgorithm(), AlgorithmSuiteValue.Basic128.getAsymKWAlgorithm());
    	    SignaturePolicy signaturePolicy = new SignaturePolicy();
            SignatureTarget st = new SignatureTarget();
	    st.setType("qname");
    	    st.setDigestAlgorithm(DigestMethod.SHA1);
            ((SignaturePolicy.FeatureBinding)signaturePolicy.getFeatureBinding()).
                    addTargetBinding(st);
	    ((SignaturePolicy.FeatureBinding)signaturePolicy.getFeatureBinding()).
    	            setCanonicalizationAlgorithm(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);

            QName name = new QName("X509Certificate");
	     Token tok = new Token(name);

    	    SymmetricKeyBinding sigKb = 
        	(SymmetricKeyBinding)signaturePolicy.newSymmetricKeyBinding();
	    AuthenticationTokenPolicy.X509CertificateBinding x509bind = 
    	            (AuthenticationTokenPolicy.X509CertificateBinding)sigKb.newX509CertificateKeyBinding();
            x509bind.setReferenceType(MessageConstants.THUMB_PRINT_TYPE);
	    //x509bind.setPolicyToken(tok);
    	    x509bind.setUUID(new String("1005"));

            EncryptionPolicy encryptPolicy = new EncryptionPolicy();
	    EncryptionTarget et = new EncryptionTarget();
    	    et.setType("qname");
            ((EncryptionPolicy.FeatureBinding)encryptPolicy.getFeatureBinding()).
            	    addTargetBinding(st);

	    SymmetricKeyBinding encKb = 
    	            (SymmetricKeyBinding)encryptPolicy.newSymmetricKeyBinding();
            encKb.newX509CertificateKeyBinding();
	    x509bind = (AuthenticationTokenPolicy.X509CertificateBinding)encKb.newX509CertificateKeyBinding();
	    x509bind.setReferenceType(MessageConstants.THUMB_PRINT_TYPE);
    	    //x509bind.setPolicyToken(tok);
            x509bind.setUUID(new String("1005"));
        
	    SOAPMessage msg = MessageFactory.newInstance().createMessage();
    	    SOAPBody body = msg.getSOAPBody();
            SOAPBodyElement sbe = body.addBodyElement(
            	    SOAPFactory.newInstance().createName(
                	"StockSymbol",
                    	"tru",
	                "http://fabrikam123.com/payloads"));
    	    sbe.addTextNode("QQQ");

	    //Create processing context and set the soap
    	    //message to be processed.
            ProcessingContextImpl context = new ProcessingContextImpl(client);
	    context.setSOAPMessage(msg);
        
            com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
            WSSAssertion wssAssertion = null;
            AssertionSet as = null;
            Policy wssPolicy = new PolicyResourceLoader().loadPolicy("security/policy-binding2.xml");
            Iterator<AssertionSet> i = wssPolicy.iterator();
            if(i.hasNext())
                as = i.next();
            
            for(PolicyAssertion assertion:as){
                if(assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion){
                    wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion)assertion;
                }                      
            }
	    //wssAssertion.addRequiredProperty("RequireSignatureConfirmation");
            wssAssertion = new WSSAssertion(wssAssertionws.getRequiredProperties(), "1.0");
     	    MessagePolicy pol = new MessagePolicy();
	    pol.append(signaturePolicy);
    	    pol.append(encryptPolicy);
            pol.setWSSAssertion(wssAssertion);
        
	    context.setAlgorithmSuite(alg);
        
    	    context.setSecurityPolicy(pol);
            CallbackHandler handler = new PolicyCallbackHandler1("client");
	    SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(handler);
    	    context.setSecurityEnvironment(env);

            SecurityAnnotator.secureMessage(context);

	    SOAPMessage secMsg = context.getSOAPMessage();
    	    //DumpFilter.process(context);

        	// now persist the message and read-back
	        FileOutputStream sentFile = new FileOutputStream("golden.msg");
    	    secMsg.saveChanges();
        	TestUtil.saveMimeHeaders(secMsg, "golden.mh");
	        secMsg.writeTo(sentFile);
    	    sentFile.close();

        	// now create the message
	        SOAPMessage recMsg = TestUtil.constructMessage("golden.mh", "golden.msg");
        
    	    // verify
        	ProcessingContextImpl context1 = verify(recMsg, null, null);
        
	        //Send the response
    	    context1.setAlgorithmSuite(alg);
        	context1.setSecurityPolicy(pol);
	        SecurityAnnotator.secureMessage(context1);
    	    secMsg = context1.getSOAPMessage();
            //DumpFilter.process(context1);
        
	        // now persist the message and read-back
    	    FileOutputStream recvdFile = new FileOutputStream("recvd.msg");
        	secMsg.saveChanges();
	        TestUtil.saveMimeHeaders(secMsg, "recvd.mh");
    	    secMsg.writeTo(recvdFile);
        	recvdFile.close();

	        // now create the message
    	    SOAPMessage clientRecMsg = TestUtil.constructMessage("recvd.mh", "recvd.msg");
        	verifyClientRecMsg(clientRecMsg, null, client);
    
public static com.sun.xml.wss.impl.ProcessingContextImplverify(javax.xml.soap.SOAPMessage msg, byte[] proofKey, java.util.Map map)

       //Create processing context and set the soap
       //message to be processed.
       ProcessingContextImpl context = new ProcessingContextImpl(map);
       context.setSOAPMessage(msg);
       
       com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
       WSSAssertion wssAssertion = null;
       AssertionSet as = null;
       Policy wssPolicy = new PolicyResourceLoader().loadPolicy("security/policy-binding2.xml");
       Iterator<AssertionSet> i = wssPolicy.iterator();
       if(i.hasNext())
           as = i.next();
            
       for(PolicyAssertion assertion:as){
           if(assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion){
               wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion)assertion;
           }                      
       }
        wssAssertion = new WSSAssertion(wssAssertionws.getRequiredProperties(), "1.0");
        MessagePolicy pol = new MessagePolicy();
        context.setAlgorithmSuite(alg);
        pol.setWSSAssertion(wssAssertion);
                                                                                                           
        context.setSecurityPolicy(pol);
        CallbackHandler handler = new PolicyCallbackHandler1("server");
        SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(handler);
        context.setSecurityEnvironment(env);

        SecurityRecipient.validateMessage(context);
        //System.out.println("Verfied Message");
        //DumpFilter.process(context);
      
        return context;

   
public static com.sun.xml.wss.impl.ProcessingContextImplverifyClientRecMsg(javax.xml.soap.SOAPMessage msg, byte[] proofKey, java.util.Map map)

       //Create processing context and set the soap
       //message to be processed.
       ProcessingContextImpl context = new ProcessingContextImpl(map);
       context.setSOAPMessage(msg);
       
       com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
       WSSAssertion wssAssertion = null;
       AssertionSet as = null;
       Policy wssPolicy = new PolicyResourceLoader().loadPolicy("security/policy-binding2.xml");
       Iterator<AssertionSet> i = wssPolicy.iterator();
       if(i.hasNext())
           as = i.next();
            
       for(PolicyAssertion assertion:as){
           if(assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion){
               wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion)assertion;
           }                      
       }
        wssAssertion = new WSSAssertion(wssAssertionws.getRequiredProperties(), "1.0");
        MessagePolicy pol = new MessagePolicy();
        context.setAlgorithmSuite(alg);
        pol.setWSSAssertion(wssAssertion);
                                                                                                           
        context.setSecurityPolicy(pol);
        CallbackHandler handler = new PolicyCallbackHandler1("client");
        SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(handler);
        context.setSecurityEnvironment(env);

        SecurityRecipient.validateMessage(context);
        //System.out.println("Verfied Message");
        //DumpFilter.process(context);
      
        return context;