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: * EndorsingSignatureTest.java
038: *
039: * Created on April 7, 2006, 12:34 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 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: import com.sun.xml.wss.impl.AlgorithmSuite;
072: import com.sun.xml.ws.policy.Policy;
073: import com.sun.xml.ws.policy.PolicyAssertion;
074: import com.sun.xml.ws.policy.AssertionSet;
075: import junit.framework.*;
076:
077: /**
078: *
079: * @author root
080: */
081: public class EndorsingSignatureTest extends TestCase {
082: private static HashMap client = new HashMap();
083: private static HashMap server = new HashMap();
084: private static AlgorithmSuite alg = null;
085:
086: /** Creates a new instance of EndorsingSignatureTest */
087: public EndorsingSignatureTest(String testName) {
088: super (testName);
089: }
090:
091: protected void setUp() throws Exception {
092:
093: }
094:
095: protected void tearDown() throws Exception {
096: }
097:
098: public static Test suite() {
099: TestSuite suite = new TestSuite(EndorsingSignatureTest.class);
100:
101: return suite;
102: }
103:
104: public void testEndorsingSignatureTest() throws Exception {
105:
106: // alg.setType(AlgorithmSuiteValue.Basic128);
107: alg = new AlgorithmSuite(AlgorithmSuiteValue.Basic128
108: .getDigAlgorithm(), AlgorithmSuiteValue.Basic128
109: .getEncAlgorithm(), AlgorithmSuiteValue.Basic128
110: .getSymKWAlgorithm(), AlgorithmSuiteValue.Basic128
111: .getAsymKWAlgorithm());
112: SignaturePolicy signaturePolicy = new SignaturePolicy();
113: signaturePolicy.setUUID("22222");
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: SymmetricKeyBinding sigKb = (SymmetricKeyBinding) signaturePolicy
127: .newSymmetricKeyBinding();
128: AuthenticationTokenPolicy.X509CertificateBinding x509bind = (AuthenticationTokenPolicy.X509CertificateBinding) sigKb
129: .newX509CertificateKeyBinding();
130: x509bind.setReferenceType(MessageConstants.THUMB_PRINT_TYPE);
131: //x509bind.setPolicyToken(tok);
132: x509bind.setIncludeToken(Token.INCLUDE_NEVER);
133: x509bind.setUUID(new String("1002"));
134: x509bind.setIncludeToken(Token.INCLUDE_NEVER);
135:
136: SignaturePolicy signaturePolicy1 = new SignaturePolicy();
137: SignatureTarget st1 = new SignatureTarget();
138: st1.setType("uri");
139: st1.setValue("#22222");
140: st1.setDigestAlgorithm(DigestMethod.SHA1);
141: ((SignaturePolicy.FeatureBinding) signaturePolicy1
142: .getFeatureBinding()).addTargetBinding(st1);
143: ((SignaturePolicy.FeatureBinding) signaturePolicy1
144: .getFeatureBinding())
145: .setCanonicalizationAlgorithm(MessageConstants.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
146:
147: AuthenticationTokenPolicy.X509CertificateBinding x509bind1 = (AuthenticationTokenPolicy.X509CertificateBinding) signaturePolicy1
148: .newX509CertificateKeyBinding();
149: x509bind1
150: .setReferenceType(MessageConstants.DIRECT_REFERENCE_TYPE);
151: x509bind1.setIncludeToken(Token.INCLUDE_ALWAYS);
152: //x509bind1.setPolicyToken(tok);
153: x509bind1.setUUID(new String("1003"));
154:
155: SOAPMessage msg = MessageFactory.newInstance().createMessage();
156: SOAPBody body = msg.getSOAPBody();
157: SOAPBodyElement sbe = body.addBodyElement(SOAPFactory
158: .newInstance().createName("StockSymbol", "tru",
159: "http://fabrikam123.com/payloads"));
160: sbe.addTextNode("QQQ");
161:
162: //Create processing context and set the soap
163: //message to be processed.
164: ProcessingContextImpl context = new ProcessingContextImpl(
165: client);
166: context.setSOAPMessage(msg);
167:
168: com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
169: WSSAssertion wssAssertion = null;
170: AssertionSet as = null;
171: Policy wssPolicy = new PolicyResourceLoader()
172: .loadPolicy("security/policy-binding2.xml");
173: Iterator<AssertionSet> i = wssPolicy.iterator();
174: if (i.hasNext())
175: as = i.next();
176:
177: for (PolicyAssertion assertion : as) {
178: if (assertion.getName().getLocalPart().equals("Wss11")) {
179: wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion) assertion;
180: }
181: }
182: wssAssertion = new WSSAssertion(wssAssertionws
183: .getRequiredProperties(), "1.0");
184: MessagePolicy pol = new MessagePolicy();
185: pol.append(signaturePolicy);
186: pol.append(signaturePolicy1);
187: pol.setWSSAssertion(wssAssertion);
188:
189: context.setAlgorithmSuite(alg);
190:
191: context.setSecurityPolicy(pol);
192: CallbackHandler handler = new PolicyCallbackHandler1("client");
193: SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(
194: handler);
195: context.setSecurityEnvironment(env);
196:
197: SecurityAnnotator.secureMessage(context);
198:
199: SOAPMessage secMsg = context.getSOAPMessage();
200: //DumpFilter.process(context);
201:
202: // now persist the message and read-back
203: FileOutputStream sentFile = new FileOutputStream("golden.msg");
204: secMsg.saveChanges();
205: TestUtil.saveMimeHeaders(secMsg, "golden.mh");
206: secMsg.writeTo(sentFile);
207: sentFile.close();
208:
209: // now create the message
210: SOAPMessage recMsg = TestUtil.constructMessage("golden.mh",
211: "golden.msg");
212:
213: // verify
214: ProcessingContextImpl context1 = verify(recMsg, null, null);
215:
216: }
217:
218: public ProcessingContextImpl verify(SOAPMessage msg,
219: byte[] proofKey, Map map) throws Exception {
220: //Create processing context and set the soap
221: //message to be processed.
222: ProcessingContextImpl context = new ProcessingContextImpl(map);
223: context.setSOAPMessage(msg);
224:
225: com.sun.xml.ws.security.policy.WSSAssertion wssAssertionws = null;
226: WSSAssertion wssAssertion = null;
227: AssertionSet as = null;
228: Policy wssPolicy = new PolicyResourceLoader()
229: .loadPolicy("security/policy-binding2.xml");
230: Iterator<AssertionSet> i = wssPolicy.iterator();
231: if (i.hasNext())
232: as = i.next();
233:
234: for (PolicyAssertion assertion : as) {
235: if (assertion instanceof com.sun.xml.ws.security.policy.WSSAssertion) {
236: wssAssertionws = (com.sun.xml.ws.security.policy.WSSAssertion) assertion;
237: }
238: }
239: wssAssertion = new WSSAssertion(wssAssertionws
240: .getRequiredProperties(), "1.0");
241: MessagePolicy pol = new MessagePolicy();
242: context.setAlgorithmSuite(alg);
243: pol.setWSSAssertion(wssAssertion);
244:
245: context.setSecurityPolicy(pol);
246: CallbackHandler handler = new PolicyCallbackHandler1("server");
247: SecurityEnvironment env = new DefaultSecurityEnvironmentImpl(
248: handler);
249: context.setSecurityEnvironment(env);
250:
251: SecurityRecipient.validateMessage(context);
252:
253: //System.out.println("Verfied Message");
254: //DumpFilter.process(context);
255:
256: return context;
257: }
258:
259: // public static void main(String[] args) throws Exception{
260: // testEndorsingSignatureTest();
261: // }
262: }
|