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