001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: /*
037: * TimestampTest.java
038: *
039: * Created on April 6, 2006, 11:58 AM
040: *
041: * To change this template, choose Tools | Template Manager
042: * and open the template in the editor.
043: */
044:
045: package com.sun.xml.wss.impl;
046:
047: import java.util.*;
048: import java.io.*;
049:
050: import com.sun.xml.wss.callback.PolicyCallbackHandler1;
051: import com.sun.xml.wss.*;
052:
053: import javax.xml.soap.*;
054: import com.sun.xml.wss.impl.policy.mls.*;
055: import com.sun.xml.wss.impl.filter.*;
056: import com.sun.xml.wss.ProcessingContext;
057: import com.sun.xml.ws.security.impl.*;
058: import com.sun.xml.wss.core.*;
059: import com.sun.xml.wss.impl.ProcessingContextImpl;
060: import com.sun.xml.ws.security.impl.policy.*;
061: import javax.xml.namespace.QName;
062: import java.security.SecureRandom;
063: import com.sun.xml.wss.impl.misc.*;
064: import javax.security.auth.callback.CallbackHandler;
065: import com.sun.xml.wss.impl.*;
066: import javax.xml.crypto.dsig.DigestMethod;
067: import com.sun.xml.ws.security.policy.AlgorithmSuiteValue;
068: import com.sun.xml.wss.impl.WSSAssertion;
069: import com.sun.xml.wss.impl.util.PolicyResourceLoader;
070: import com.sun.xml.wss.impl.util.TestUtil;
071:
072: import com.sun.xml.wss.impl.AlgorithmSuite;
073: import com.sun.xml.ws.policy.Policy;
074: import com.sun.xml.ws.policy.PolicyAssertion;
075: import com.sun.xml.ws.policy.AssertionSet;
076:
077: import junit.framework.Test;
078: import junit.framework.TestCase;
079: import junit.framework.TestSuite;
080:
081: /**
082: *
083: * @author admin
084: */
085: public class TimestampTest extends TestCase {
086: private static HashMap client = new HashMap();
087: private static AlgorithmSuite alg = null;
088:
089: /** Creates a new instance of TimestampTest */
090: public TimestampTest(String testName) {
091: super (testName);
092: }
093:
094: protected void setUp() throws Exception {
095:
096: }
097:
098: protected void tearDown() throws Exception {
099: }
100:
101: public static Test suite() {
102: TestSuite suite = new TestSuite(TimestampTest.class);
103: return suite;
104: }
105:
106: public static void testTimestampOnTop() throws Exception {
107: //alg.setType(AlgorithmSuiteValue.Basic128);
108: alg = new AlgorithmSuite(AlgorithmSuiteValue.Basic128
109: .getDigAlgorithm(), AlgorithmSuiteValue.Basic128
110: .getEncAlgorithm(), AlgorithmSuiteValue.Basic128
111: .getSymKWAlgorithm(), AlgorithmSuiteValue.Basic128
112: .getAsymKWAlgorithm());
113: SignaturePolicy signaturePolicy = new SignaturePolicy();
114: SignatureTarget st = new SignatureTarget();
115: st.setType("qname");
116: st.setDigestAlgorithm(DigestMethod.SHA1);
117: ((SignaturePolicy.FeatureBinding) signaturePolicy
118: .getFeatureBinding()).addTargetBinding(st);
119: ((SignaturePolicy.FeatureBinding) signaturePolicy
120: .getFeatureBinding())
121: .setCanonicalizationAlgorithm(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
122:
123: QName name = new QName("X509Certificate");
124: Token tok = new Token(name);
125:
126: AuthenticationTokenPolicy.X509CertificateBinding x509bind = (AuthenticationTokenPolicy.X509CertificateBinding) signaturePolicy
127: .newX509CertificateKeyBinding();
128: x509bind
129: .setReferenceType(MessageConstants.DIRECT_REFERENCE_TYPE);
130: x509bind.setIncludeToken(Token.INCLUDE_ALWAYS);
131: //x509bind.setPolicyToken(tok);
132: x509bind.setUUID(new String("1008"));
133:
134: TimestampPolicy tsPolicy = new TimestampPolicy();
135:
136: SOAPMessage msg = MessageFactory.newInstance().createMessage();
137: SOAPBody body = msg.getSOAPBody();
138: SOAPBodyElement sbe = body.addBodyElement(SOAPFactory
139: .newInstance().createName("StockSymbol", "tru",
140: "http://fabrikam123.com/payloads"));
141: sbe.addTextNode("QQQ");
142:
143: //Create processing context and set the soap
144: //message to be processed.
145: ProcessingContextImpl context = new ProcessingContextImpl(
146: client);
147: context.setSOAPMessage(msg);
148:
149: com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
150: WSSAssertion wssAssertion = null;
151: AssertionSet as = null;
152: Policy wssPolicy = new PolicyResourceLoader()
153: .loadPolicy("security/policy-binding2.xml");
154: Iterator<AssertionSet> i = wssPolicy.iterator();
155: if (i.hasNext())
156: as = i.next();
157:
158: for (PolicyAssertion assertion : as) {
159: if (assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion) {
160: wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion) assertion;
161: }
162: }
163: wssAssertion = new WSSAssertion(wssAssertionws
164: .getRequiredProperties(), "1.0");
165: MessagePolicy pol = new MessagePolicy();
166: pol.append(tsPolicy);
167: pol.append(signaturePolicy);
168: pol.setWSSAssertion(wssAssertion);
169:
170: context.setAlgorithmSuite(alg);
171:
172: context.setSecurityPolicy(pol);
173: CallbackHandler handler = new PolicyCallbackHandler1("client");
174: SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(
175: handler);
176: context.setSecurityEnvironment(env);
177:
178: SecurityAnnotator.secureMessage(context);
179:
180: SecurableSoapMessage secMsg = context.getSecurableSoapMessage();
181: //DumpFilter.process(context);
182:
183: SecurityHeader securityHeader = secMsg.findSecurityHeader();
184: org.w3c.dom.Node timestamp = securityHeader.getFirstChild();
185: assertEquals("Timestamp", timestamp.getLocalName());
186:
187: }
188:
189: // public static void main(String[] args) throws Exception{
190: // testTimestampOnTop();
191: // }
192:
193: }
|