001: /*
002: * $Id: DirectReferenceStrategy.java,v 1.6 2007/01/08 16:06:04 shyam_rao Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.wss.impl.keyinfo;
028:
029: import java.util.logging.Level;
030: import java.util.logging.Logger;
031: import org.w3c.dom.Document;
032: import com.sun.xml.wss.impl.MessageConstants;
033: import com.sun.xml.wss.logging.LogDomainConstants;
034: import com.sun.xml.wss.impl.SecurableSoapMessage;
035: import com.sun.xml.wss.XWSSecurityException;
036:
037: import java.security.cert.X509Certificate;
038:
039: //import com.sun.xml.wss.impl.filter.FilterParameterConstants;
040: import com.sun.xml.wss.core.reference.DirectReference;
041: import com.sun.xml.wss.core.KeyInfoHeaderBlock;
042: import com.sun.xml.wss.core.SecurityTokenReference;
043:
044: public class DirectReferenceStrategy extends KeyInfoStrategy {
045:
046: X509Certificate cert = null;
047:
048: String alias = null;
049: boolean forSigning;
050:
051: String samlAssertionId = null;
052:
053: protected static final Logger log = Logger.getLogger(
054: LogDomainConstants.WSS_API_DOMAIN,
055: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
056:
057: public DirectReferenceStrategy() {
058:
059: }
060:
061: public DirectReferenceStrategy(String samlAssertionId) {
062: this .samlAssertionId = samlAssertionId;
063: this .cert = null;
064: this .alias = null;
065: this .forSigning = false;
066: }
067:
068: public DirectReferenceStrategy(String alias, boolean forSigning) {
069: this .alias = alias;
070: this .forSigning = forSigning;
071: this .samlAssertionId = null;
072: this .cert = null;
073: }
074:
075: public void insertKey(SecurityTokenReference tokenRef,
076: SecurableSoapMessage secureMsg) throws XWSSecurityException {
077: DirectReference ref = getDirectReference(secureMsg, null, null);
078: tokenRef.setReference(ref);
079: }
080:
081: public void insertKey(KeyInfoHeaderBlock keyInfo,
082: SecurableSoapMessage secureMsg, String x509TokenId)
083: throws XWSSecurityException {
084:
085: Document ownerDoc = keyInfo.getOwnerDocument();
086: SecurityTokenReference tokenRef = new SecurityTokenReference(
087: ownerDoc);
088: DirectReference ref = getDirectReference(secureMsg,
089: x509TokenId, null);
090: tokenRef.setReference(ref);
091: keyInfo.addSecurityTokenReference(tokenRef);
092: }
093:
094: public void insertKey(KeyInfoHeaderBlock keyInfo,
095: SecurableSoapMessage secureMsg, String x509TokenId,
096: String valueType) throws XWSSecurityException {
097:
098: Document ownerDoc = keyInfo.getOwnerDocument();
099: SecurityTokenReference tokenRef = new SecurityTokenReference(
100: ownerDoc);
101: DirectReference ref = getDirectReference(secureMsg,
102: x509TokenId, valueType);
103: tokenRef.setReference(ref);
104: keyInfo.addSecurityTokenReference(tokenRef);
105: }
106:
107: public void setCertificate(X509Certificate cert) {
108: this .cert = cert;
109: }
110:
111: public String getAlias() {
112: return alias;
113: }
114:
115: private DirectReference getDirectReference(
116: SecurableSoapMessage secureMsg, String x509TokenId,
117: String valueType) throws XWSSecurityException {
118:
119: DirectReference ref = new DirectReference();
120:
121: if (samlAssertionId != null) {
122: String uri = "#" + samlAssertionId;
123: ref.setURI(uri);
124: ref
125: .setValueType(MessageConstants.WSSE_SAML_v1_1_VALUE_TYPE);
126:
127: } else {
128: // create a certificate token
129: if (cert == null) {
130: log.log(Level.SEVERE,
131: "WSS0185.filterparameter.not.set",
132: new Object[] { "subjectkeyidentifier" });
133: throw new XWSSecurityException(
134: "No certificate specified and no default found.");
135: }
136: if (x509TokenId == null) {
137: throw new XWSSecurityException("WSU ID is null");
138: }
139: String uri = "#" + x509TokenId;
140: ref.setURI(uri);
141: if (valueType == null || valueType.equals("")) {
142: valueType = MessageConstants.X509v3_NS;
143: }
144: ref.setValueType(valueType);
145: }
146: return ref;
147: }
148: }
|