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: * SignAllHeadersTest.java
038: *
039: * Created on March 31, 2006, 11:36 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 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: import com.sun.xml.wss.impl.AlgorithmSuite;
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 SignAllHeadersTest 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: /** Creates a new instance of SignAllHeadersTest */
086: public SignAllHeadersTest(String testName) {
087: super (testName);
088: }
089:
090: protected void setUp() throws Exception {
091:
092: }
093:
094: protected void tearDown() throws Exception {
095: }
096:
097: public static Test suite() {
098: TestSuite suite = new TestSuite(SignAllHeadersTest.class);
099: return suite;
100: }
101:
102: public static void testSignAllHeadersTest() throws Exception {
103: // alg.setType(AlgorithmSuiteValue.Basic128);
104: alg = new AlgorithmSuite(AlgorithmSuiteValue.Basic128
105: .getDigAlgorithm(), AlgorithmSuiteValue.Basic128
106: .getEncAlgorithm(), AlgorithmSuiteValue.Basic128
107: .getSymKWAlgorithm(), AlgorithmSuiteValue.Basic128
108: .getAsymKWAlgorithm());
109: SignaturePolicy signaturePolicy = new SignaturePolicy();
110: SignatureTarget st = new SignatureTarget();
111: st.setType("uri");
112: st.setValue("ALL_MESSAGE_HEADERS");
113: st.setDigestAlgorithm(DigestMethod.SHA1);
114: ((SignaturePolicy.FeatureBinding) signaturePolicy
115: .getFeatureBinding()).addTargetBinding(st);
116: ((SignaturePolicy.FeatureBinding) signaturePolicy
117: .getFeatureBinding())
118: .setCanonicalizationAlgorithm(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
119:
120: QName name = new QName("X509Certificate");
121: Token tok = new Token(name);
122:
123: SymmetricKeyBinding sigKb = (SymmetricKeyBinding) signaturePolicy
124: .newSymmetricKeyBinding();
125: AuthenticationTokenPolicy.X509CertificateBinding x509bind = (AuthenticationTokenPolicy.X509CertificateBinding) sigKb
126: .newX509CertificateKeyBinding();
127: x509bind
128: .setReferenceType(MessageConstants.DIRECT_REFERENCE_TYPE);
129: //x509bind.setPolicyToken(tok);
130: x509bind.setUUID(new String("1004"));
131:
132: // create SOAPMessage
133: SOAPMessage msg = MessageFactory.newInstance().createMessage();
134: SOAPHeader header = msg.getSOAPHeader();
135: SOAPHeaderElement she1 = header.addHeaderElement(SOAPFactory
136: .newInstance().createName("StockHeader", "stkheader",
137: "http://stockhome.com/quote"));
138: she1.addTextNode("Head Text Node1");
139: SOAPHeaderElement she2 = header.addHeaderElement(SOAPFactory
140: .newInstance().createName("Quote", "quote",
141: "http://stockhome.com/quote"));
142: she2.addTextNode("Head Text Node2");
143: SOAPBody body = msg.getSOAPBody();
144: SOAPBodyElement sbe = body.addBodyElement(SOAPFactory
145: .newInstance().createName("StockSymbol", "tru",
146: "http://fabrikam123.com/payloads"));
147: sbe.addTextNode("QQQ");
148:
149: //Create processing context and set the soap message to be processed.
150: ProcessingContextImpl context = new ProcessingContextImpl(
151: client);
152: context.setSOAPMessage(msg);
153:
154: com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
155: WSSAssertion wssAssertion = null;
156: AssertionSet as = null;
157: Policy wssPolicy = new PolicyResourceLoader()
158: .loadPolicy("security/policy-binding2.xml");
159: Iterator<AssertionSet> i = wssPolicy.iterator();
160: if (i.hasNext())
161: as = i.next();
162:
163: for (PolicyAssertion assertion : as) {
164: if (assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion) {
165: wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion) assertion;
166: }
167: }
168: wssAssertion = new WSSAssertion(wssAssertionws
169: .getRequiredProperties(), "1.0");
170: MessagePolicy pol = new MessagePolicy();
171: pol.append(signaturePolicy);
172: pol.setWSSAssertion(wssAssertion);
173:
174: context.setAlgorithmSuite(alg);
175: context.setSecurityPolicy(pol);
176: CallbackHandler handler = new PolicyCallbackHandler1("client");
177: SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(
178: handler);
179: context.setSecurityEnvironment(env);
180: SecurityAnnotator.secureMessage(context);
181:
182: SOAPMessage secMsg = context.getSOAPMessage();
183: //DumpFilter.process(context);
184:
185: // now persist the message and read-back
186: FileOutputStream sentFile = new FileOutputStream("golden.msg");
187: secMsg.saveChanges();
188: TestUtil.saveMimeHeaders(secMsg, "golden.mh");
189: secMsg.writeTo(sentFile);
190: sentFile.close();
191:
192: // now create the message
193: SOAPMessage recMsg = TestUtil.constructMessage("golden.mh",
194: "golden.msg");
195: // verify
196: verify(recMsg, null, null);
197:
198: }
199:
200: public static ProcessingContextImpl verify(SOAPMessage msg,
201: byte[] proofKey, Map map) throws Exception {
202: //Create processing context and set the soap
203: //message to be processed.
204: ProcessingContextImpl context = new ProcessingContextImpl(map);
205: context.setSOAPMessage(msg);
206:
207: com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
208: WSSAssertion wssAssertion = null;
209: AssertionSet as = null;
210: Policy wssPolicy = new PolicyResourceLoader()
211: .loadPolicy("security/policy-binding2.xml");
212: Iterator<AssertionSet> i = wssPolicy.iterator();
213: if (i.hasNext())
214: as = i.next();
215:
216: for (PolicyAssertion assertion : as) {
217: if (assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion) {
218: wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion) assertion;
219: }
220: }
221: //wssAssertion.addRequiredProperty("RequireSignatureConfirmation");
222: wssAssertion = new WSSAssertion(wssAssertionws
223: .getRequiredProperties(), "1.0");
224: MessagePolicy pol = new MessagePolicy();
225: context.setAlgorithmSuite(alg);
226: pol.setWSSAssertion(wssAssertion);
227:
228: context.setSecurityPolicy(pol);
229: CallbackHandler handler = new PolicyCallbackHandler1("server");
230: SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(
231: handler);
232: context.setSecurityEnvironment(env);
233:
234: SecurityRecipient.validateMessage(context);
235:
236: //System.out.println("Verfied Message");
237: //DumpFilter.process(context);
238:
239: return context;
240: }
241:
242: // public static void main(String[] args) throws Exception{
243: // testSignAllHeadersTest();
244: // }
245:
246: }
|