001: /*
002: * $Id: BinarySecurityToken.java,v 1.6 2007/01/08 16:06:10 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: package com.sun.xml.wss.core;
027:
028: import org.w3c.dom.Document;
029:
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: import javax.xml.soap.SOAPElement;
034:
035: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
036: import com.sun.xml.wss.impl.misc.Base64;
037: import com.sun.xml.wss.logging.LogDomainConstants;
038: import com.sun.xml.wss.impl.MessageConstants;
039:
040: import com.sun.xml.wss.impl.XMLUtil;
041: import com.sun.xml.wss.XWSSecurityException;
042: import com.sun.xml.wss.impl.SecurityTokenException;
043:
044: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
045:
046: /**
047: * A wsse:BinarySecurityToken.
048: *
049: * @author Manveen Kaur
050: * @author Edwin Goei
051: */
052: public class BinarySecurityToken extends SecurityHeaderBlockImpl
053: implements SecurityToken {
054:
055: /**
056: * Valid values are:
057: * #X509v3
058: * #X509PKIPathv1
059: * #PKCS7
060: */
061: protected String valueType = null;
062:
063: /** Default encoding */
064: protected String encodingType = MessageConstants.BASE64_ENCODING_NS;
065:
066: protected String wsuId = null;
067:
068: protected String encodedText = null;
069:
070: protected Document soapDoc = null;
071:
072: protected static final Logger log = Logger.getLogger(
073: LogDomainConstants.WSS_API_DOMAIN,
074: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
075:
076: BinarySecurityToken(Document document, String wsuId,
077: String valueType) throws SecurityTokenException {
078:
079: this .soapDoc = document;
080: this .wsuId = wsuId;
081: setValueType(valueType);
082:
083: // BSP:R3029 :EncodingType MUST always be specified.
084: setEncodingType(encodingType);
085: }
086:
087: BinarySecurityToken(SOAPElement binTokenSoapElement)
088: throws SecurityTokenException {
089: this (binTokenSoapElement, false);
090: }
091:
092: BinarySecurityToken(SOAPElement binTokenSoapElement, boolean isBSP)
093: throws SecurityTokenException {
094:
095: setSOAPElement(binTokenSoapElement);
096: this .soapDoc = getOwnerDocument();
097:
098: setTextValue(XMLUtil.getFullTextFromChildren(this ));
099:
100: String wsuId = getAttributeNS(MessageConstants.WSU_NS, "Id");
101: if (!"".equals(wsuId))
102: setId(wsuId);
103:
104: String valueType = getAttribute("ValueType");
105:
106: // BSP:3031: ValueType MUST always be specified
107: if (isBSP && valueType.length() < 1) {
108: log.log(Level.SEVERE, "BSP3031.ValueType.NotPresent");
109: throw new SecurityTokenException(
110: "Any wsse:BinarySecurityToken in a SECURE_ENVELOPE MUST have an ValueType attribute.");
111: }
112:
113: if (!"".equals(valueType)) {
114: setValueType(valueType);
115: }
116:
117: if (isBSP) {
118: String encoding = getAttribute("EncodingType");
119:
120: // BSP:R3029: encodingType MUST be specified.
121: if (encodingType.length() < 1) {
122: log
123: .log(Level.SEVERE,
124: "BSP3029.EncodingType.NotPresent");
125: throw new SecurityTokenException(
126: "Any wsse:BinarySecurityToken in a SECURE_ENVELOPE MUST have an EncodingType attribute.");
127: }
128:
129: if (!encodingType
130: .equalsIgnoreCase(MessageConstants.BASE64_ENCODING_NS)) {
131: log.log(Level.SEVERE, "BSP3030.EncodingType.Invalid");
132: throw new SecurityTokenException(
133: "EncodingType attribute value in wsse:BinarySecurityToken is invalid.");
134: }
135:
136: if (!"".equals(encoding)) {
137: setEncodingType(encoding);
138: }
139: }
140: }
141:
142: public String getValueType() {
143: return this .valueType;
144: }
145:
146: protected void setValueType(String valueType) {
147: if (!(MessageConstants.X509v3_NS.equals(valueType) || MessageConstants.X509v1_NS
148: .equals(valueType))) {
149: log.log(Level.SEVERE, "WSS0342.valtype.invalid");
150: throw new RuntimeException("Unsupported value type: "
151: + valueType);
152: }
153: this .valueType = valueType;
154: }
155:
156: public String getEncodingType() {
157: return this .encodingType;
158: }
159:
160: protected void setEncodingType(String encodingType) {
161:
162: if (!MessageConstants.BASE64_ENCODING_NS.equals(encodingType)) {
163: log.log(Level.SEVERE, "WSS0316.enctype.invalid");
164: throw new RuntimeException("Encoding type invalid");
165: }
166: this .encodingType = encodingType;
167: }
168:
169: public String getId() {
170: return this .wsuId;
171: }
172:
173: protected void setId(String wsuId) {
174: this .wsuId = wsuId;
175: }
176:
177: /** returns the decoded value of the text node.*/
178: public byte[] getRawValue() throws SecurityTokenException {
179: try {
180: return Base64.decode(encodedText);
181: } catch (Base64DecodingException bde) {
182: log.log(Level.SEVERE, "WSS0344.error.decoding.bst");
183: throw new SecurityTokenException(bde);
184: }
185: }
186:
187: protected void setRawValue(byte[] rawText) {
188: this .encodedText = Base64.encode(rawText);
189: }
190:
191: /**
192: * get the actual value of the text node. This will typically be encoded.
193: * It is the onus of the filter to decode this before operation upon it.
194: */
195: public String getTextValue() throws XWSSecurityException {
196: return encodedText;
197: }
198:
199: /**
200: * set the value of the text node. It is assumed that the
201: * filter would have already encoded the value appropriately.
202: */
203: protected void setTextValue(String encodedText) {
204: this .encodedText = encodedText;
205: }
206:
207: public SOAPElement getAsSoapElement() throws SecurityTokenException {
208:
209: if (null != delegateElement)
210: return delegateElement;
211: try {
212: setSOAPElement((SOAPElement) soapDoc.createElementNS(
213: MessageConstants.WSSE_NS,
214: MessageConstants.WSSE_PREFIX
215: + ":BinarySecurityToken"));
216: addNamespaceDeclaration(MessageConstants.WSSE_PREFIX,
217: MessageConstants.WSSE_NS);
218:
219: if (null != valueType)
220: setAttributeNS(null, "ValueType", valueType);
221:
222: if (encodingType != null) {
223: setAttributeNS(null, "EncodingType", encodingType);
224: }
225:
226: if (wsuId != null) {
227: setWsuIdAttr(this , wsuId);
228: }
229:
230: addTextNode(getTextValue());
231:
232: } catch (Exception e) {
233: log.log(Level.SEVERE, "WSS0343.error.creating.bst", e
234: .getMessage());
235: throw new SecurityTokenException(
236: "There was an error in creating the BinarySecurityToken "
237: + e.getMessage());
238: }
239: return delegateElement;
240: }
241: }
|