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: * SignSOAPHeadersOnlyTest.java
038: *
039: * Created on April 4, 2006, 6:38 PM
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 junit.framework.Test;
048: import junit.framework.TestCase;
049: import junit.framework.TestSuite;
050:
051: import com.sun.xml.wss.callback.PolicyCallbackHandler1;
052: import com.sun.xml.ws.security.impl.policy.*;
053: import com.sun.xml.ws.security.policy.AlgorithmSuiteValue;
054: import com.sun.xml.wss.impl.policy.mls.*;
055: import com.sun.xml.wss.impl.*;
056: import com.sun.xml.wss.*;
057: import com.sun.xml.wss.impl.misc.DefaultSecurityEnvironmentImpl;
058: import com.sun.xml.wss.impl.filter.*;
059: import com.sun.xml.wss.impl.WSSAssertion;
060: import com.sun.xml.wss.impl.util.PolicyResourceLoader;
061: import com.sun.xml.wss.impl.util.TestUtil;
062:
063: import com.sun.xml.ws.policy.Policy;
064: import com.sun.xml.ws.policy.PolicyAssertion;
065: import com.sun.xml.ws.policy.AssertionSet;
066:
067: import javax.security.auth.callback.CallbackHandler;
068: import javax.xml.crypto.dsig.DigestMethod;
069: import javax.xml.namespace.QName;
070: import javax.xml.soap.*;
071:
072: import java.util.*;
073: import java.io.*;
074:
075: /**
076: *
077: * @author ashutosh.shahi@sun.com
078: */
079: public class SignSOAPHeadersOnlyTest extends TestCase {
080:
081: private static HashMap client = new HashMap();
082: private static HashMap server = new HashMap();
083: private static AlgorithmSuite alg = null;
084:
085: /**
086: * Creates a new instance of SignSOAPHeadersOnlyTest
087: */
088: public SignSOAPHeadersOnlyTest(String testName) {
089: super (testName);
090: }
091:
092: protected void setUp() throws Exception {
093:
094: }
095:
096: protected void tearDown() throws Exception {
097: }
098:
099: public static Test suite() {
100: TestSuite suite = new TestSuite(SignSOAPHeadersOnlyTest.class);
101: return suite;
102: }
103:
104: public static void testSignSOAPHeadersOnlyTest() throws Exception {
105: // alg.setType(AlgorithmSuiteValue.Basic128);
106: alg = new AlgorithmSuite(AlgorithmSuiteValue.Basic128
107: .getDigAlgorithm(), AlgorithmSuiteValue.Basic128
108: .getEncAlgorithm(), AlgorithmSuiteValue.Basic128
109: .getSymKWAlgorithm(), AlgorithmSuiteValue.Basic128
110: .getAsymKWAlgorithm());
111: SignaturePolicy signaturePolicy = new SignaturePolicy();
112: SignatureTarget st = new SignatureTarget();
113: st.setType("qname");
114: st.isSOAPHeadersOnly(true);
115: st.setValue("{http://stockhome.com/quote}");
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: SymmetricKeyBinding sigKb = (SymmetricKeyBinding) signaturePolicy
127: .newSymmetricKeyBinding();
128: AuthenticationTokenPolicy.X509CertificateBinding x509bind = (AuthenticationTokenPolicy.X509CertificateBinding) sigKb
129: .newX509CertificateKeyBinding();
130: x509bind
131: .setReferenceType(MessageConstants.DIRECT_REFERENCE_TYPE);
132: //x509bind.setPolicyToken(tok);
133: x509bind.setUUID(new String("1019"));
134:
135: // create SOAPMessage
136: SOAPMessage msg = MessageFactory.newInstance().createMessage();
137: SOAPHeader header = msg.getSOAPHeader();
138: SOAPHeaderElement she1 = header.addHeaderElement(SOAPFactory
139: .newInstance().createName("StockHeader", "stkheader",
140: "http://stockhome.com/quote"));
141: she1.addTextNode("Head Text Node1");
142: SOAPHeaderElement she2 = header.addHeaderElement(SOAPFactory
143: .newInstance().createName("Quote", "quote",
144: "http://stockhome.com/quote"));
145: she2.addTextNode("Head Text Node2");
146: SOAPBody body = msg.getSOAPBody();
147: SOAPBodyElement sbe = body.addBodyElement(SOAPFactory
148: .newInstance().createName("StockSymbol", "tru",
149: "http://stockhome.com/quote"));
150: sbe.addTextNode("QQQ");
151:
152: //Create processing context and set the soap message to be processed.
153: ProcessingContextImpl context = new ProcessingContextImpl(
154: client);
155: context.setSOAPMessage(msg);
156:
157: com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
158: WSSAssertion wssAssertion = null;
159: AssertionSet as = null;
160: Policy wssPolicy = new PolicyResourceLoader()
161: .loadPolicy("security/policy-binding2.xml");
162: Iterator<AssertionSet> i = wssPolicy.iterator();
163: if (i.hasNext())
164: as = i.next();
165:
166: for (PolicyAssertion assertion : as) {
167: if (assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion) {
168: wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion) assertion;
169: }
170: }
171: wssAssertion = new WSSAssertion(wssAssertionws
172: .getRequiredProperties(), "1.0");
173: MessagePolicy pol = new MessagePolicy();
174: pol.append(signaturePolicy);
175: pol.setWSSAssertion(wssAssertion);
176:
177: context.setAlgorithmSuite(alg);
178: context.setSecurityPolicy(pol);
179: CallbackHandler handler = new PolicyCallbackHandler1("client");
180: SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(
181: handler);
182: context.setSecurityEnvironment(env);
183: SecurityAnnotator.secureMessage(context);
184:
185: SOAPMessage secMsg = context.getSOAPMessage();
186: //DumpFilter.process(context);
187:
188: // now persist the message and read-back
189: FileOutputStream sentFile = new FileOutputStream("golden.msg");
190: secMsg.saveChanges();
191: TestUtil.saveMimeHeaders(secMsg, "golden.mh");
192: secMsg.writeTo(sentFile);
193: sentFile.close();
194:
195: // now create the message
196: SOAPMessage recMsg = TestUtil.constructMessage("golden.mh",
197: "golden.msg");
198: // verify
199: verify(recMsg, null, null);
200:
201: }
202:
203: public static ProcessingContextImpl verify(SOAPMessage msg,
204: byte[] proofKey, Map map) throws Exception {
205: //Create processing context and set the soap
206: //message to be processed.
207: ProcessingContextImpl context = new ProcessingContextImpl(map);
208: context.setSOAPMessage(msg);
209:
210: com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
211: WSSAssertion wssAssertion = null;
212: AssertionSet as = null;
213: Policy wssPolicy = new PolicyResourceLoader()
214: .loadPolicy("security/policy-binding2.xml");
215: Iterator<AssertionSet> i = wssPolicy.iterator();
216: if (i.hasNext())
217: as = i.next();
218:
219: for (PolicyAssertion assertion : as) {
220: if (assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion) {
221: wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion) assertion;
222: }
223: }
224: //wssAssertion.addRequiredProperty("RequireSignatureConfirmation");
225: wssAssertion = new WSSAssertion(wssAssertionws
226: .getRequiredProperties(), "1.0");
227: MessagePolicy pol = new MessagePolicy();
228: context.setAlgorithmSuite(alg);
229: pol.setWSSAssertion(wssAssertion);
230:
231: context.setSecurityPolicy(pol);
232: CallbackHandler handler = new PolicyCallbackHandler1("server");
233: SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(
234: handler);
235: context.setSecurityEnvironment(env);
236:
237: SecurityRecipient.validateMessage(context);
238:
239: //System.out.println("Verfied Message");
240: //DumpFilter.process(context);
241:
242: return context;
243: }
244:
245: // public static void main(String[] args) throws Exception{
246: // testSignSOAPHeadersOnlyTest();
247: // }
248: //
249: }
|