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: /*
038: * WSTrustUtil.java
039: *
040: * Created on February 7, 2006, 3:37 PM
041: *
042: */
043:
044: package com.sun.xml.ws.security.trust.util;
045:
046: import com.sun.xml.ws.policy.PolicyAssertion;
047: import com.sun.xml.ws.security.impl.policy.PolicyUtil;
048: import com.sun.xml.ws.security.secconv.WSSCElementFactory;
049: import com.sun.xml.ws.security.secconv.WSSecureConversationException;
050: import com.sun.xml.ws.policy.impl.bindings.AppliesTo;
051: import com.sun.xml.ws.security.trust.impl.bindings.AttributedURI;
052: import com.sun.xml.ws.security.trust.impl.bindings.EndpointReference;
053:
054: import java.net.URI;
055: import java.net.URISyntaxException;
056: import java.security.SecureRandom;
057: import java.util.List;
058: import java.util.UUID;
059: import javax.xml.soap.SOAPFault;
060: import javax.xml.bind.JAXBElement;
061:
062: import javax.xml.soap.SOAPElement;
063: import javax.xml.soap.SOAPHeader;
064: import javax.xml.soap.SOAPMessage;
065:
066: import com.sun.xml.ws.api.message.Message;
067:
068: import com.sun.xml.ws.security.SecurityContextToken;
069: import com.sun.xml.ws.security.trust.WSTrustSOAPFaultException;
070: import com.sun.xml.ws.security.secconv.WSSCConstants;
071:
072: import com.sun.xml.wss.core.SecurityContextTokenImpl;
073:
074: import org.w3c.dom.NodeList;
075:
076: /**
077: *
078: * @author ws-trust-implementation-team
079: */
080: public class WSTrustUtil {
081:
082: private WSTrustUtil() {
083: //private constructor
084: }
085:
086: /**
087: *create and return a SOAP 1.1 Fault corresponding to this exception
088: */
089: public static SOAPFault createSOAP11Fault(
090: final WSTrustSOAPFaultException sfex) {
091:
092: throw new UnsupportedOperationException("To Do");
093: }
094:
095: /**
096: *create and return a SOAP 1.2 Fault corresponding to this exception
097: */
098: public static SOAPFault createSOAP12Fault(
099: final WSTrustSOAPFaultException sfex) {
100:
101: throw new UnsupportedOperationException("To Do");
102: }
103:
104: /*public static String getSecurityContext(final Message msg){
105:
106: try {
107: final SOAPMessage soapMessage = msg.readAsSOAPMessage();
108: final SOAPHeader header = soapMessage.getSOAPHeader();
109: if (header != null){
110: final NodeList list = header.getElementsByTagNameNS(WSSCConstants.WSC_NAMESPACE,
111: WSSCConstants.SECURITY_CONTEXT_TOKEN);
112: SOAPElement sctElement = null;
113: if (list.getLength() > 0) {
114: sctElement = (SOAPElement)list.item(0);
115: }
116:
117: if (sctElement != null){
118: final SecurityContextToken sct = new SecurityContextTokenImpl(sctElement);
119:
120: return sct.getIdentifier().toString();
121: }
122: }
123: }catch (Exception ex){
124: throw new RuntimeException(ex);
125: }
126:
127: return null;
128: } */
129:
130: public static byte[] generateRandomSecret(final int keySize) {
131: // Create binary secret
132: final SecureRandom random = new SecureRandom();
133: final byte[] secret = new byte[(int) keySize];
134: random.nextBytes(secret);
135: return secret;
136: }
137:
138: public static SecurityContextToken createSecurityContextToken(
139: final WSSCElementFactory eleFac)
140: throws WSSecureConversationException {
141: final String identifier = "urn:uuid:"
142: + UUID.randomUUID().toString();
143: URI idURI;
144: try {
145: idURI = new URI(identifier);
146: } catch (URISyntaxException ex) {
147: throw new WSSecureConversationException(ex.getMessage(), ex);
148: }
149: final String wsuId = "uuid-" + UUID.randomUUID().toString();
150:
151: return eleFac.createSecurityContextToken(idURI, null, wsuId);
152: }
153:
154: public static AppliesTo createAppliesTo(final String appliesTo) {
155: final AttributedURI uri = new AttributedURI();
156: uri.setValue(appliesTo);
157: final EndpointReference epr = new EndpointReference();
158: epr.setAddress(uri);
159: final AppliesTo applTo = (new com.sun.xml.ws.policy.impl.bindings.ObjectFactory())
160: .createAppliesTo();
161: applTo
162: .getAny()
163: .add(
164: (new com.sun.xml.ws.security.trust.impl.bindings.ObjectFactory())
165: .createEndpointReference(epr));
166:
167: return applTo;
168: }
169:
170: public static String getAppliesToURI(final AppliesTo appliesTo) {
171: final List list = appliesTo.getAny();
172: EndpointReference epr = null;
173: if (!list.isEmpty()) {
174: for (int i = 0; i < list.size(); i++) {
175: final Object obj = list.get(i);
176: if (obj instanceof EndpointReference) {
177: epr = (EndpointReference) obj;
178: } else if (obj instanceof JAXBElement) {
179: final JAXBElement ele = (JAXBElement) obj;
180: final String local = ele.getName().getLocalPart();
181: if (local.equalsIgnoreCase("EndpointReference")) {
182: epr = (EndpointReference) ele.getValue();
183: }
184: }
185:
186: if (epr != null) {
187: final AttributedURI uri = epr.getAddress();
188: if (uri != null) {
189: return uri.getValue();
190: }
191: }
192: }
193: }
194: return null;
195: }
196:
197: public static boolean isMetadata(final PolicyAssertion assertion) {
198: if (!isMEXNS(assertion)) {
199: return false;
200: }
201:
202: if (assertion.getName().getLocalPart().equals(Metadata)) {
203: return true;
204: }
205:
206: return false;
207: }
208:
209: public static final String MEX_NS = "http://schemas.xmlsoap.org/ws/2004/09/mex";
210: public static final String Metadata = "Metadata";
211: public static final String MetadataSection = "MetadataSection";
212: public static final String MetadataReference = "MetadataReference";
213:
214: public static boolean isMEXNS(final PolicyAssertion assertion) {
215: if (MEX_NS.equals(assertion.getName().getNamespaceURI())) {
216: return true;
217: }
218: return false;
219: }
220:
221: public static boolean isMetadataSection(
222: final PolicyAssertion assertion) {
223: if (!isMEXNS(assertion)) {
224: return false;
225: }
226:
227: if (assertion.getName().getLocalPart().equals(MetadataSection)) {
228: return true;
229: }
230:
231: return false;
232: }
233:
234: public static boolean isMetadataReference(
235: final PolicyAssertion assertion) {
236: if (!isMEXNS(assertion)) {
237: return false;
238: }
239:
240: if (assertion.getName().getLocalPart()
241: .equals(MetadataReference)) {
242: return true;
243: }
244:
245: return false;
246: }
247:
248: public static boolean isAddressingMetadata(
249: final PolicyAssertion assertion) {
250: if (!PolicyUtil.isAddressingNS(assertion)) {
251: return false;
252: }
253:
254: if (assertion.getName().getLocalPart().equals(Metadata)) {
255: return true;
256: }
257: return false;
258: }
259:
260: }
|