FileDocCategorySizeDatePackage
TrustTest.javaAPI DocExample22845Tue May 29 16:57:46 BST 2007com.sun.xml.wss.impl

TrustTest

public class TrustTest extends TestCase

Fields Summary
public static final String
holderOfKeyConfirmation
public static final String
senderVouchesConfirmation
private static Hashtable
map
private static AlgorithmSuite
alg
Constructors Summary
public TrustTest(String testName)

    
         
        super(testName);
    
Methods Summary
public static javax.xml.soap.SOAPMessageconstructMessage(java.lang.String mimeHdrsFile, java.lang.String msgFile)

        SOAPMessage message;
                                                                                                                                                 
        MimeHeaders mimeHeaders = new MimeHeaders();
        FileInputStream fis = new FileInputStream(msgFile);
                                                                                                                                                 
        ObjectInputStream ois = new ObjectInputStream(
        new FileInputStream(mimeHdrsFile));
        Hashtable hashTable = (Hashtable) ois.readObject();
        ois.close();
                                                                                                                                                 
        if(hashTable.isEmpty()) {
            //System.out.println("MimeHeaders Hashtable is empty");
        } else {
            for(int i=0; i < hashTable.size(); i++) {
                Enumeration keys = hashTable.keys();
                Enumeration values = hashTable.elements();
                while (keys.hasMoreElements() && values.hasMoreElements()) {
                    String name = (String) keys.nextElement();
                    String value = (String) values.nextElement();
                    mimeHeaders.addHeader(name, value);
                }
            }
        }
                                                                                                                                                 
        MessageFactory messageFactory = MessageFactory.newInstance();
        message = messageFactory.createMessage(mimeHeaders, fis);
                                                                                                                                                 
        message.saveChanges();
                                                                                                                                                 
        return message;
    
private static AssertioncreateHOKAssertion(byte[] keyBytes, org.w3c.dom.Document doc)

        
        Assertion assertion = null;
        try {
                             
            SAMLAssertionFactory factory = SAMLAssertionFactory.newInstance(SAMLAssertionFactory.SAML1_1);
                                                                                                
            // create the assertion id
            String assertionID = String.valueOf(System.currentTimeMillis());
            String issuer = "CN=Assertion Issuer,OU=AI,O=Assertion Issuer,L=Waltham,ST=MA,C=US";
                                                                                                                             
                                                                                                                             
            GregorianCalendar c = new GregorianCalendar();
            long beforeTime = c.getTimeInMillis();
            // roll the time by one hour
            long offsetHours = 60*60*1000;
                                                                                                                             
            c.setTimeInMillis(beforeTime - offsetHours);
            GregorianCalendar before= (GregorianCalendar)c.clone();
                                                                                                                             
            c = new GregorianCalendar();
            long afterTime = c.getTimeInMillis();
            c.setTimeInMillis(afterTime + offsetHours);
            GregorianCalendar after = (GregorianCalendar)c.clone();
                                                                                                                             
            GregorianCalendar issueInstant = new GregorianCalendar();
            // statements
            List statements = new LinkedList();
            NameIdentifier nmId =
            factory.createNameIdentifier(
            "CN=SAML User,OU=SU,O=SAML User,L=Los Angeles,ST=CA,C=US",
            null, // not sure abt this value
            "urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName");           

            //default priv key cert req
            SOAPElement elem = (SOAPElement)doc.createElementNS(WSTrustConstants.WST_NAMESPACE, "wst:BinarySecret");
            elem.addTextNode(Base64.encode(keyBytes));

            KeyInfoHeaderBlock kiHB = new KeyInfoHeaderBlock(doc);

            SOAPElement binSecret = null;
            kiHB.addBinarySecret(elem);

            List subConfirmation = new ArrayList();
            subConfirmation.add(senderVouchesConfirmation);

            SubjectConfirmation scf =
            factory.createSubjectConfirmation(subConfirmation, null, kiHB.getAsSoapElement());
                                                                                                                             
                                                                                                                             
            Subject subj = factory.createSubject(nmId, scf);
                                                                                                                             
            List attributes = new LinkedList();
            List attributeValues = new LinkedList();
            attributeValues.add("ATTRIBUTE1");
            attributes.add( factory.createAttribute(
                "attribute1",
                "urn:com:sun:xml:wss:attribute",
                attributeValues));
                                                                                                                             
            statements.add(
            factory.createAttributeStatement(subj, attributes));
                                                                                                                             
            Conditions conditions = factory.createConditions(before, after, null, null, null);
                                                                                                                             
            assertion = factory.createAssertion(assertionID, issuer, issueInstant,
            conditions, null, statements);
            assertion.setMajorVersion(BigInteger.ONE);
            assertion.setMinorVersion(BigInteger.ONE);
 
            return assertion;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        
    
public static voidsaveMimeHeaders(javax.xml.soap.SOAPMessage msg, java.lang.String fileName)

                                                                                                                                                 
        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
                                                                                                                                                 
        Hashtable hashTable = new Hashtable();
        MimeHeaders mimeHeaders = msg.getMimeHeaders();
        Iterator iterator = mimeHeaders.getAllHeaders();
                                                                                                                                                 
        while(iterator.hasNext()) {
            MimeHeader mimeHeader = (MimeHeader) iterator.next();
            hashTable.put(mimeHeader.getName(), mimeHeader.getValue());
        }
                                                                                                                                                 
        oos.writeObject(hashTable);
        oos.flush();
        oos.close();
                                                                                                                                                 
        fos.flush();
        fos.close();
    
protected voidsetUp()

    	
    
public static junit.framework.Testsuite()

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

    
private static javax.xml.soap.SOAPMessagetestResponse(com.sun.xml.wss.impl.ProcessingContextImpl context)

    	        SignaturePolicy signaturePolicy = new SignaturePolicy();
        	SignatureTarget st = new SignatureTarget();
	        st.setType("qname");
    	        st.setDigestAlgorithm(DigestMethod.SHA1);
        	SignatureTarget.Transform trans = new SignatureTarget.Transform();
	        trans.setTransform(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
    	        st.addTransform(trans);

        	((SignaturePolicy.FeatureBinding)signaturePolicy.getFeatureBinding()).
            	        addTargetBinding(st);
	        ((SignaturePolicy.FeatureBinding)signaturePolicy.getFeatureBinding()).
    	                setCanonicalizationAlgorithm(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);

        	IssuedTokenKeyBinding isKB = 
            	(IssuedTokenKeyBinding)signaturePolicy.newIssuedTokenKeyBinding();

	        EncryptionPolicy encryptPolicy = new EncryptionPolicy();
    	        EncryptionTarget et = new EncryptionTarget();
        	et.setType("qname");
	        ((EncryptionPolicy.FeatureBinding)encryptPolicy.getFeatureBinding()).
    	                addTargetBinding(st);
        	((EncryptionPolicy.FeatureBinding)encryptPolicy.getFeatureBinding()).setDataEncryptionAlgorithm(MessageConstants.AES_BLOCK_ENCRYPTION_128);
	        IssuedTokenKeyBinding ieKB = 
    	        (IssuedTokenKeyBinding)encryptPolicy.newIssuedTokenKeyBinding();

        	QName name = new QName("IssuedToken");
	        Token tok = new Token(name);
    	        //isKB.setPolicyToken(tok);
                isKB.setUUID(new String("20029"));
                ieKB.setUUID(new String("20029"));
        	//ieKB.setPolicyToken(tok);
    	        MessagePolicy pol = new MessagePolicy();
                //pol.dumpMessages(true);
        	signaturePolicy.setUUID("22222");
	        pol.append(encryptPolicy);
    	        pol.append(signaturePolicy);
 
                context.setSecurityPolicy(pol);

    	        SecurityAnnotator.secureMessage(context);

                return context.getSOAPMessage();
    
public static voidtestTrustIntegrationTest()

    
                //System.setProperty("com.sun.xml.wss.saml.binding.jaxb", "true");
	        //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);
        	SignatureTarget.Transform trans = new SignatureTarget.Transform();
	        trans.setTransform(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
    	        st.addTransform(trans);

        	((SignaturePolicy.FeatureBinding)signaturePolicy.getFeatureBinding()).
            	        addTargetBinding(st);
	        ((SignaturePolicy.FeatureBinding)signaturePolicy.getFeatureBinding()).
    	                setCanonicalizationAlgorithm(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);

        	IssuedTokenKeyBinding isKB = 
            	(IssuedTokenKeyBinding)signaturePolicy.newIssuedTokenKeyBinding();

	        EncryptionPolicy encryptPolicy = new EncryptionPolicy();
    	        EncryptionTarget et = new EncryptionTarget();
        	et.setType("qname");
	        ((EncryptionPolicy.FeatureBinding)encryptPolicy.getFeatureBinding()).
    	                addTargetBinding(st);
        	((EncryptionPolicy.FeatureBinding)encryptPolicy.getFeatureBinding()).setDataEncryptionAlgorithm(MessageConstants.AES_BLOCK_ENCRYPTION_128);
	        IssuedTokenKeyBinding ieKB = 
    	        (IssuedTokenKeyBinding)encryptPolicy.newIssuedTokenKeyBinding();

        	QName name = new QName("IssuedToken");
	        Token tok = new Token(name);
    	        //isKB.setPolicyToken(tok);
        	//ieKB.setPolicyToken(tok);
                isKB.setUUID(new String("1011"));
                ieKB.setUUID(new String("1011"));
    	        MessagePolicy pol = new MessagePolicy();
                //pol.dumpMessages(true);
        	signaturePolicy.setUUID("22222");
	        pol.append(encryptPolicy);
    	        pol.append(signaturePolicy);
        
	        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();
	        context.setSOAPMessage(msg);

    	        // create a new IssuedTokenContext
        	IssuedTokenContextImpl impl = new IssuedTokenContextImpl();
        
    	        SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
        	byte[] keyBytes = new byte[16];
	        rnd.nextBytes(keyBytes);
    	        impl.setProofKey(keyBytes);

	        // create a SAML Token and set it here
                Assertion assertion = createHOKAssertion(keyBytes, msg.getSOAPPart());
                Element samlElem = SAMLUtil.toElement(null, assertion);

	        impl.setSecurityToken(new GenericToken(samlElem));
                SecurityTokenReference str = new SecurityTokenReference(msg.getSOAPPart());
                KeyIdentifier samlRef = new SamlKeyIdentifier(msg.getSOAPPart());
                samlRef.setReferenceValue(assertion.getAssertionID());
                str.setReference(samlRef);           
                impl.setAttachedSecurityTokenReference(str);
                impl.setUnAttachedSecurityTokenReference(str);

	        map.put(new String("1011"), impl);
    	        context.setIssuedTokenContextMap(map);
        	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();

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

        	// now create the message
	        SOAPMessage recMsg = constructMessage("golden.mh", "golden.msg");
        
	        // verify
                /* uncomment after signing SAML Assertion
    	        ProcessingContextImpl ctxImpl = verify(recMsg);
                SOAPMessage vMsg = ctxImpl.getSOAPMessage();
                vMsg.saveChanges();
                ctxImpl.setSOAPMessage(vMsg);
 
                SOAPMessage newMsg = testResponse(ctxImpl);
                newMsg.saveChanges();
                context.setSOAPMessage(newMsg);
                SecurityRecipient.validateMessage(context);
                */
    
public static com.sun.xml.wss.impl.ProcessingContextImplverify(javax.xml.soap.SOAPMessage msg)

       //Create processing context and set the soap
       //message to be processed.
       ProcessingContextImpl context = new ProcessingContextImpl();
       context.setSOAPMessage(msg);
                                                                                                           
        MessagePolicy pol = new MessagePolicy();
        //pol.dumpMessages(true);
        //pol.append(signaturePolicy);
        context.setAlgorithmSuite(alg);
                                                                                                           
        context.setSecurityPolicy(pol);
        CallbackHandler handler = new PolicyCallbackHandler1("server");
        SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(handler);
        context.setSecurityEnvironment(env);

        SecurityRecipient.validateMessage(context);

        //context.getSOAPMessage().writeTo(System.out);

        return context;