001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: /*
024: * DerivedKeyTokenHeaderBlock.java
025: *
026: * Created on December 15, 2005, 6:41 PM
027: *
028: * To change this template, choose Tools | Template Manager
029: * and open the template in the editor.
030: */
031:
032: package com.sun.xml.wss.core;
033:
034: import com.sun.xml.wss.XWSSecurityException;
035: import com.sun.xml.wss.impl.MessageConstants;
036: import com.sun.xml.wss.impl.SecurityTokenException;
037: import com.sun.xml.wss.impl.XMLUtil;
038: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
039:
040: import com.sun.xml.ws.security.Token;
041: import java.util.Iterator;
042: import javax.xml.soap.SOAPElement;
043: import javax.xml.soap.SOAPException;
044:
045: import org.w3c.dom.Document;
046: import org.w3c.dom.Node;
047:
048: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
049: import com.sun.xml.wss.impl.misc.Base64;
050:
051: /**
052: *
053: * @author Abhijit Das
054: */
055: public class DerivedKeyTokenHeaderBlock extends SecurityHeaderBlockImpl
056: implements Token, SecurityToken {
057:
058: /**
059: *
060: * @param element
061: * @return
062: * @throws XWSSecurityException
063: */
064: public static SecurityHeaderBlock fromSoapElement(
065: SOAPElement element) throws XWSSecurityException {
066: return SecurityHeaderBlockImpl.fromSoapElement(element,
067: DerivedKeyTokenHeaderBlock.class);
068: }
069:
070: private Document contextDocument = null;
071: private SecurityTokenReference securityTokenRefElement = null;
072: private long offset = 0;
073: private long length = 32;
074: private String nonce = null;
075: private long generation = -1;
076: private String wsuId = null;
077:
078: private byte[] decodedNonce = null;
079:
080: public DerivedKeyTokenHeaderBlock(Document contextDocument,
081: SecurityTokenReference securityTokenRefElement, String wsuId)
082: throws XWSSecurityException {
083: if (securityTokenRefElement != null) {
084: this .contextDocument = contextDocument;
085: this .securityTokenRefElement = securityTokenRefElement;
086: this .wsuId = wsuId;
087: } else {
088: throw new XWSSecurityException(
089: "DerivedKeyToken can not be null");
090: }
091: }
092:
093: public DerivedKeyTokenHeaderBlock(Document contextDocument,
094: SecurityTokenReference securityTokenRefElement,
095: String nonce, String wsuId) throws XWSSecurityException {
096:
097: if (securityTokenRefElement != null) {
098: this .contextDocument = contextDocument;
099: this .securityTokenRefElement = securityTokenRefElement;
100: this .wsuId = wsuId;
101: } else {
102: throw new XWSSecurityException(
103: "DerivedKeyToken can not be null");
104: }
105:
106: if (nonce != null) {
107: this .nonce = nonce;
108: } else {
109: throw new XWSSecurityException("Nonce can not be null");
110: }
111: }
112:
113: public DerivedKeyTokenHeaderBlock(Document contextDocument,
114: SecurityTokenReference securityTokenRefElement,
115: String nonce, long generation, String wsuId)
116: throws XWSSecurityException {
117: this (contextDocument, securityTokenRefElement, nonce, wsuId);
118: this .generation = generation;
119: }
120:
121: public DerivedKeyTokenHeaderBlock(Document contextDocument,
122: SecurityTokenReference securityTokenRefElement,
123: String nonce, long offset, long length, String wsuId)
124: throws XWSSecurityException {
125: this (contextDocument, securityTokenRefElement, nonce, -1, wsuId);
126: this .length = length;
127: this .offset = offset;
128:
129: }
130:
131: public DerivedKeyTokenHeaderBlock(
132: SOAPElement derivedKeyTokenHeaderBlock)
133: throws XWSSecurityException {
134: setSOAPElement(derivedKeyTokenHeaderBlock);
135:
136: this .contextDocument = getOwnerDocument();
137:
138: if (!("DerivedKeyToken".equals(getLocalName()) && XMLUtil
139: .inWsscNS(this ))) {
140: throw new SecurityTokenException(
141: "Expected DerivedKeyToken Element, but Found "
142: + getPrefix() + ":" + getLocalName());
143: }
144:
145: boolean invalidToken = false;
146:
147: Iterator children = getChildElements();
148:
149: // Check whether SecurityTokenReference is present inside DerivedKeyToken
150: String wsuId = getAttributeNS(MessageConstants.WSU_NS, "Id");
151: if (!"".equals(wsuId))
152: setId(wsuId);
153:
154: Node object = null;
155: boolean offsetSpecified = false;
156: boolean genSpecified = false;
157: boolean lenSpecified = false;
158:
159: while (children.hasNext()) {
160:
161: object = (Node) children.next();
162:
163: if (object.getNodeType() == Node.ELEMENT_NODE) {
164:
165: SOAPElement element = (SOAPElement) object;
166: //TODO: Check for other attributes
167: if ("SecurityTokenReference".equals(element
168: .getLocalName())
169: && XMLUtil.inWsseNS(element)) {
170: securityTokenRefElement = new SecurityTokenReference(
171: element);
172: } else if ("Offset".equals(element.getLocalName())) {
173: try {
174: offsetSpecified = true;
175: offset = Long.valueOf(element.getValue())
176: .longValue();
177: } catch (NumberFormatException nfe) {
178: throw new XWSSecurityException(nfe);
179: }
180: } else if ("Length".equals(element.getLocalName())) {
181: try {
182: lenSpecified = true;
183: length = Long.valueOf(element.getValue())
184: .longValue();
185: } catch (NumberFormatException nfe) {
186: throw new XWSSecurityException(nfe);
187: }
188: } else if ("Nonce".equals(element.getLocalName())) {
189: nonce = element.getValue();
190: } else if ("Generation".equals(element.getLocalName())) {
191: try {
192: genSpecified = true;
193: generation = Long.valueOf(element.getValue())
194: .longValue();
195: } catch (NumberFormatException nfe) {
196: throw new XWSSecurityException(nfe);
197: }
198: } else {
199: invalidToken = true;
200: break;
201: }
202: }
203: }
204:
205: if (offsetSpecified && genSpecified) {
206: invalidToken = true;
207: }
208:
209: if (invalidToken) {
210: throw new XWSSecurityException("Invalid DerivedKeyToken");
211: }
212: }
213:
214: public SOAPElement getAsSoapElement() throws XWSSecurityException {
215: if (delegateElement != null)
216: return delegateElement;
217:
218: try {
219: setSOAPElement((SOAPElement) contextDocument
220: .createElementNS(MessageConstants.WSSC_NS,
221: MessageConstants.WSSC_PREFIX
222: + ":DerivedKeyToken"));
223: addNamespaceDeclaration(MessageConstants.WSSC_PREFIX,
224: MessageConstants.WSSC_NS);
225:
226: if (securityTokenRefElement == null) {
227: throw new SecurityTokenException(
228: "securitytokenreference was not set");
229: } else {
230: SOAPElement elem = securityTokenRefElement
231: .getAsSoapElement();
232: delegateElement.appendChild(elem);
233: }
234: if (generation == -1) {
235: addChildElement("Offset", MessageConstants.WSSC_PREFIX)
236: .addTextNode(String.valueOf(offset));
237: addChildElement("Length", MessageConstants.WSSC_PREFIX)
238: .addTextNode(String.valueOf(length));
239: } else {
240: addChildElement("Generation",
241: MessageConstants.WSSC_PREFIX).addTextNode(
242: String.valueOf(generation));
243: }
244:
245: if (nonce != null) {
246: addChildElement("Nonce", MessageConstants.WSSC_PREFIX)
247: .addTextNode(nonce);
248: }
249:
250: if (wsuId != null) {
251: setWsuIdAttr(this , wsuId);
252: }
253:
254: } catch (SOAPException se) {
255: throw new SecurityTokenException(
256: "There was an error creating DerivedKey Token "
257: + se.getMessage());
258: }
259:
260: return super .getAsSoapElement();
261: }
262:
263: public Document getContextDocument() {
264: return contextDocument;
265: }
266:
267: public byte[] getNonce() {
268: if (decodedNonce != null)
269: return decodedNonce;
270: try {
271: decodedNonce = Base64.decode(nonce);
272: } catch (Base64DecodingException bde) {
273: throw new RuntimeException(bde);
274: }
275: return decodedNonce;
276: }
277:
278: public long getOffset() {
279: return offset;
280: }
281:
282: public long getLength() {
283: return length;
284: }
285:
286: public SecurityTokenReference getDerivedKeyElement() {
287: return securityTokenRefElement;
288: }
289:
290: public String getType() {
291: return MessageConstants.DERIVEDKEY_TOKEN_NS;
292: }
293:
294: public Object getTokenValue() {
295: return this ;
296: }
297:
298: private void setId(String wsuId) {
299: this.wsuId = wsuId;
300: }
301:
302: }
|