com.sun.xml.ws.security.trust.util.WSTrustUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webservices-rt Show documentation
Show all versions of webservices-rt Show documentation
This module contains the Metro runtime code.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
* WSTrustUtil.java
*
* Created on February 7, 2006, 3:37 PM
*
*/
package com.sun.xml.ws.security.trust.util;
import com.sun.xml.ws.api.security.trust.WSTrustException;
import com.sun.xml.ws.api.security.trust.STSAttributeProvider;
import com.sun.xml.ws.policy.impl.bindings.AppliesTo;
import com.sun.xml.ws.security.SecurityContextToken;
import com.sun.xml.ws.security.impl.policy.PolicyUtil;
import com.sun.xml.ws.security.secconv.WSSCElementFactory;
import com.sun.xml.ws.security.secconv.WSSCElementFactory13;
import com.sun.xml.ws.security.secconv.WSSecureConversationException;
import com.sun.xml.ws.security.trust.WSTrustElementFactory;
import com.sun.xml.ws.security.trust.WSTrustSOAPFaultException;
import com.sun.xml.ws.security.trust.WSTrustConstants;
import com.sun.xml.ws.security.trust.WSTrustVersion;
import com.sun.xml.ws.security.trust.elements.BaseSTSRequest;
import com.sun.xml.ws.security.trust.elements.BaseSTSResponse;
import com.sun.xml.ws.security.trust.elements.Lifetime;
import com.sun.xml.ws.security.trust.elements.RequestSecurityToken;
import com.sun.xml.ws.security.trust.elements.RequestSecurityTokenResponse;
import com.sun.xml.ws.security.trust.elements.str.KeyIdentifier;
import com.sun.xml.ws.security.trust.elements.str.SecurityTokenReference;
import com.sun.xml.ws.security.trust.impl.elements.str.KeyIdentifierImpl;
import com.sun.xml.ws.security.trust.impl.elements.str.SecurityTokenReferenceImpl;
import com.sun.xml.ws.security.trust.impl.bindings.AttributedURI;
import com.sun.xml.ws.security.trust.impl.bindings.EndpointReference;
import com.sun.xml.ws.security.wsu10.AttributedDateTime;
import com.sun.xml.wss.core.reference.X509SubjectKeyIdentifier;
import com.sun.xml.wss.impl.MessageConstants;
import com.sun.xml.wss.impl.misc.Base64;
import com.sun.xml.wss.saml.Assertion;
import com.sun.xml.wss.saml.SAMLAssertionFactory;
import com.sun.org.apache.xml.internal.security.keys.KeyInfo;
import com.sun.org.apache.xml.internal.security.encryption.XMLCipher;
import com.sun.org.apache.xml.internal.security.encryption.EncryptedKey;
import com.sun.org.apache.xml.internal.security.keys.content.X509Data;
import com.sun.xml.wss.WSITXMLFactory;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
*
* @author ws-trust-implementation-team
*/
public class WSTrustUtil {
private WSTrustUtil(){
//private constructor
}
/**
*create and return a SOAP 1.1 Fault corresponding to this exception
*/
public static SOAPFault createSOAP11Fault(final WSTrustSOAPFaultException sfex){
throw new UnsupportedOperationException("To Do");
}
/**
*create and return a SOAP 1.2 Fault corresponding to this exception
*/
public static SOAPFault createSOAP12Fault(final WSTrustSOAPFaultException sfex){
throw new UnsupportedOperationException("To Do");
}
public static byte[] generateRandomSecret(final int keySize) {
// Create binary secret
final SecureRandom random = new SecureRandom();
final byte[] secret = new byte[(int)keySize];
random.nextBytes(secret);
return secret;
}
public static SecurityContextToken createSecurityContextToken(final WSTrustElementFactory wsscEleFac) throws WSSecureConversationException{
final String identifier = "urn:uuid:" + UUID.randomUUID().toString();
URI idURI;
try{
idURI = new URI(identifier);
}catch (URISyntaxException ex){
throw new WSSecureConversationException(ex.getMessage(), ex);
}
final String wsuId = "uuid-" + UUID.randomUUID().toString();
if(wsscEleFac instanceof com.sun.xml.ws.security.secconv.WSSCElementFactory){
return ((WSSCElementFactory)wsscEleFac).createSecurityContextToken(idURI, null, wsuId);
}else if(wsscEleFac instanceof com.sun.xml.ws.security.secconv.WSSCElementFactory13){
return ((WSSCElementFactory13)wsscEleFac).createSecurityContextToken(idURI, null, wsuId);
}
return null;
}
public static SecurityContextToken createSecurityContextToken(final WSSCElementFactory eleFac) throws WSSecureConversationException{
final String identifier = "urn:uuid:" + UUID.randomUUID().toString();
URI idURI;
try{
idURI = new URI(identifier);
}catch (URISyntaxException ex){
throw new WSSecureConversationException(ex.getMessage(), ex);
}
final String wsuId = "uuid-" + UUID.randomUUID().toString();
return eleFac.createSecurityContextToken(idURI, null, wsuId);
}
public static SecurityContextToken createSecurityContextToken(final WSTrustElementFactory wsscEleFac, final String identifier) throws WSSecureConversationException{
URI idURI;
try{
idURI = new URI(identifier);
}catch (URISyntaxException ex){
throw new WSSecureConversationException(ex.getMessage(), ex);
}
final String wsuId = "uuid-" + UUID.randomUUID().toString();
final String wsuInstance = "uuid-" + UUID.randomUUID().toString();
if(wsscEleFac instanceof com.sun.xml.ws.security.secconv.WSSCElementFactory){
return ((WSSCElementFactory)wsscEleFac).createSecurityContextToken(idURI, wsuInstance, wsuId);
}else if(wsscEleFac instanceof com.sun.xml.ws.security.secconv.WSSCElementFactory13){
return ((WSSCElementFactory13)wsscEleFac).createSecurityContextToken(idURI, wsuInstance, wsuId);
}
return null;
}
public static SecurityContextToken createSecurityContextToken(final WSSCElementFactory eleFac, final String identifier) throws WSSecureConversationException{
URI idURI;
try{
idURI = new URI(identifier);
}catch (URISyntaxException ex){
throw new WSSecureConversationException(ex.getMessage(), ex);
}
final String wsuId = "uuid-" + UUID.randomUUID().toString();
final String wsuInstance = "uuid-" + UUID.randomUUID().toString();
return eleFac.createSecurityContextToken(idURI, wsuInstance, wsuId);
}
public static SecurityTokenReference createSecurityTokenReference(final String id, final String valueType){
WSTrustElementFactory eleFac = WSTrustElementFactory.newInstance();
final KeyIdentifier ref = eleFac.createKeyIdentifier(valueType, null);
ref.setValue(id);
return eleFac.createSecurityTokenReference(ref);
}
public static AppliesTo createAppliesTo(final String appliesTo){
final AttributedURI uri = new AttributedURI();
uri.setValue(appliesTo);
final EndpointReference epr = new EndpointReference();
epr.setAddress(uri);
final AppliesTo applTo = (new com.sun.xml.ws.policy.impl.bindings.ObjectFactory()).createAppliesTo();
applTo.getAny().add((new com.sun.xml.ws.security.trust.impl.bindings.ObjectFactory()).createEndpointReference(epr));
return applTo;
}
public static List