001: /*
002: * $Id: EncryptedTypeHeaderBlock.java,v 1.5 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:
027: package com.sun.xml.wss.core;
028:
029: import com.sun.xml.wss.logging.LogDomainConstants;
030: import com.sun.xml.wss.impl.MessageConstants;
031: import com.sun.xml.wss.XWSSecurityException;
032: import java.util.Iterator;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035:
036: import javax.xml.soap.SOAPElement;
037: import javax.xml.soap.SOAPException;
038:
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041: import org.w3c.dom.NodeList;
042: import org.w3c.dom.Text;
043:
044: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
045:
046: /**
047: * Schema definition for an EncryptedType is as follows:
048: * <xmp>
049: * <complexType name='EncryptedType' abstract='true'>
050: * <sequence>
051: * <element name='EncryptionMethod' type='xenc:EncryptionMethodType'
052: * minOccurs='0'/>
053: * <element ref='ds:KeyInfo' minOccurs='0'/>
054: * <element ref='xenc:CipherData'/>
055: * <element ref='xenc:EncryptionProperties' minOccurs='0'/>
056: * </sequence>
057: * <attribute name='Id' type='ID' use='optional'/>
058: * <attribute name='Type' type='anyURI' use='optional'/>
059: * <attribute name='MimeType' type='string' use='optional'/>
060: * <attribute name='Encoding' type='anyURI' use='optional'/>
061: * </complexType>
062: * </xmp>
063: *
064: * @author Vishal Mahajan
065: */
066: public abstract class EncryptedTypeHeaderBlock extends
067: SecurityHeaderBlockImpl {
068:
069: SOAPElement encryptionMethod;
070:
071: KeyInfoHeaderBlock keyInfo;
072:
073: SOAPElement cipherData;
074:
075: SOAPElement encryptionProperties;
076:
077: boolean updateRequired = false;
078:
079: protected static final Logger log = Logger.getLogger(
080: LogDomainConstants.WSS_API_DOMAIN,
081: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
082:
083: /**
084: * Returns null if id attr is not present
085: */
086: public String getId() {
087: String id = getAttribute("Id");
088: if (id.equals(""))
089: return null;
090: return id;
091: }
092:
093: public void setId(String id) {
094: setAttribute("Id", id);
095: setIdAttribute("Id", true);
096: }
097:
098: /**
099: * Returns null if Type attr is not present
100: */
101: public String getType() {
102: String type = getAttribute("Type");
103: if (type.equals(""))
104: return null;
105: return type;
106: }
107:
108: public void setType(String type) {
109: setAttribute("Type", type);
110: }
111:
112: /**
113: * Returns null if MimeType attr is not present
114: */
115: public String getMimeType() {
116: String mimeType = getAttribute("MimeType");
117: if (mimeType.equals(""))
118: return null;
119: return mimeType;
120: }
121:
122: public void setMimeType(String mimeType) {
123: setAttribute("MimeType", mimeType);
124: }
125:
126: /**
127: * Returns null if Encoding attr is not present
128: */
129: public String getEncoding() {
130: String encoding = getAttribute("Encoding");
131: if (encoding.equals(""))
132: return null;
133: return encoding;
134: }
135:
136: public void setEncoding(String encoding) {
137: setAttribute("Encoding", encoding);
138: }
139:
140: public SOAPElement getEncryptionMethod() {
141: return encryptionMethod;
142: }
143:
144: /**
145: * returns the algorithm URI
146: */
147: public String getEncryptionMethodURI() {
148: if (encryptionMethod == null)
149: return null;
150: return encryptionMethod.getAttribute("Algorithm");
151: }
152:
153: public void setEncryptionMethod(SOAPElement encryptionMethod) {
154: this .encryptionMethod = encryptionMethod;
155: updateRequired = true;
156: }
157:
158: public void setEncryptionMethod(String algorithmURI)
159: throws XWSSecurityException {
160:
161: try {
162: encryptionMethod = getSoapFactory().createElement(
163: "EncryptionMethod", MessageConstants.XENC_PREFIX,
164: MessageConstants.XENC_NS);
165: } catch (SOAPException e) {
166: log.log(Level.SEVERE,
167: "WSS0351.error.setting.encryption.method", e
168: .getMessage());
169: throw new XWSSecurityException(e);
170: }
171: encryptionMethod.setAttribute("Algorithm", algorithmURI);
172: updateRequired = true;
173: }
174:
175: public KeyInfoHeaderBlock getKeyInfo() {
176: return keyInfo;
177: }
178:
179: public void setKeyInfo(KeyInfoHeaderBlock keyInfo) {
180: this .keyInfo = keyInfo;
181: updateRequired = true;
182: }
183:
184: /**
185: * @throws XWSSecurityException
186: * If CipherData element is not present OR
187: * If CipherValue element is not present inside CipherData.
188: */
189: public String getCipherValue() throws XWSSecurityException {
190:
191: if (cipherData == null) {
192: log.log(Level.SEVERE, "WSS0347.missing.cipher.data");
193: throw new XWSSecurityException(
194: "Cipher data has not been set");
195: }
196:
197: Iterator cipherValues = null;
198: try {
199: cipherValues = cipherData.getChildElements(getSoapFactory()
200: .createName("CipherValue",
201: MessageConstants.XENC_PREFIX,
202: MessageConstants.XENC_NS));
203: } catch (SOAPException e) {
204: log.log(Level.SEVERE, "WSS0352.error.getting.cipherValue",
205: e.getMessage());
206: throw new XWSSecurityException(e);
207: }
208:
209: if (!cipherValues.hasNext()) {
210: log.log(Level.SEVERE, "WSS0353.missing.cipherValue");
211: throw new XWSSecurityException("Cipher Value not present");
212: }
213:
214: return getFullTextChildrenFromElement((SOAPElement) cipherValues
215: .next());
216: }
217:
218: public SOAPElement getCipherData(boolean create)
219: throws XWSSecurityException {
220: if (cipherData == null && create) {
221: try {
222: cipherData = getSoapFactory().createElement(
223: "CipherData", MessageConstants.XENC_PREFIX,
224: MessageConstants.XENC_NS);
225: } catch (SOAPException e) {
226: log.log(Level.SEVERE, "WSS0395.creating.cipherData");
227: throw new XWSSecurityException(e);
228: }
229: }
230:
231: return cipherData;
232: }
233:
234: public SOAPElement getCipherReference(boolean create, String uri)
235: throws XWSSecurityException {
236: SOAPElement cipherReference = null;
237: if (create) {
238: try {
239: cipherReference = getSoapFactory().createElement(
240: "CipherReference",
241: MessageConstants.XENC_PREFIX,
242: MessageConstants.XENC_NS);
243:
244: cipherReference.setAttribute("URI", uri);
245:
246: getCipherData(create).addChildElement(cipherReference);
247: } catch (SOAPException e) {
248: // log
249: throw new XWSSecurityException(e);
250: }
251: } else {
252: if (cipherData == null) {
253: // log
254: throw new XWSSecurityException(
255: "CipherData is not present");
256: }
257:
258: // need to check
259: //Iterator i = cipherData.getChildElements();
260: //cipherReference = (SOAPElement)i.next();
261: NodeList nl = cipherData.getElementsByTagNameNS(
262: MessageConstants.XENC_NS, "CipherReference");
263: if (nl.getLength() > 0)
264: return (SOAPElement) nl.item(0);
265: }
266:
267: return cipherReference;
268: }
269:
270: public void addTransform(String algorithmURI)
271: throws XWSSecurityException {
272: SOAPElement cipherReference = getCipherReference(false, null);
273:
274: try {
275: SOAPElement dsTransform = getSoapFactory().createElement(
276: "Transform", MessageConstants.DSIG_PREFIX,
277: MessageConstants.DSIG_NS);
278:
279: dsTransform.setAttribute("Algorithm", algorithmURI);
280:
281: // need to check
282: SOAPElement xencTransforms = null;
283: Iterator i = cipherReference.getChildElements();
284: if (i == null || !i.hasNext()) {
285: xencTransforms = getSoapFactory().createElement(
286: "Transforms", MessageConstants.XENC_PREFIX,
287: MessageConstants.XENC_NS);
288:
289: xencTransforms = cipherReference
290: .addChildElement(xencTransforms);
291: } else
292: xencTransforms = (SOAPElement) i.next();
293:
294: xencTransforms.addChildElement(dsTransform);
295:
296: } catch (SOAPException e) {
297: // log
298: throw new XWSSecurityException(e);
299: }
300: }
301:
302: public Iterator getTransforms() throws XWSSecurityException {
303: SOAPElement cr = getCipherReference(false, null);
304: if (cr != null) {
305: Iterator it = cr.getChildElements();
306: if (it.hasNext()) {
307: SOAPElement transforms = (SOAPElement) it.next();
308: return transforms.getChildElements();
309: }
310: }
311: return null;
312: }
313:
314: public SOAPElement getEncryptionProperties() {
315: return encryptionProperties;
316: }
317:
318: public void setEncryptionProperties(SOAPElement encryptionProperties) {
319: this .encryptionProperties = encryptionProperties;
320: updateRequired = true;
321: }
322:
323: /**
324: * This method should be called when changes are made inside an object
325: * through its reference obtained from any of the get methods of this
326: * class. For example, if getKeyInfo() call is made and then changes are made
327: * inside the keyInfo, this method should be called to reflect changes
328: * in the EncryptedType.
329: */
330: public void saveChanges() {
331: updateRequired = true;
332: }
333:
334: public void initializeEncryptedType(SOAPElement element)
335: throws XWSSecurityException {
336: try {
337: Iterator cnodes = element.getChildElements();
338: while (cnodes.hasNext()) {
339: Node se = (Node) cnodes.next();
340:
341: while (cnodes.hasNext()
342: && !(se.getNodeType() == Node.ELEMENT_NODE))
343: se = (Node) cnodes.next();
344:
345: if ((se == null)
346: || !(se.getNodeType() == Node.ELEMENT_NODE))
347: break;
348:
349: if (((SOAPElement) se).getLocalName().equals(
350: "EncryptionMethod"))
351: encryptionMethod = (SOAPElement) se;
352: else if (((SOAPElement) se).getLocalName().equals(
353: "CipherData"))
354: cipherData = (SOAPElement) se;
355: else if (((SOAPElement) se).getLocalName().equals(
356: "KeyInfo"))
357: keyInfo = new KeyInfoHeaderBlock(
358: new com.sun.org.apache.xml.internal.security.keys.KeyInfo(
359: (Element) se, null));
360: }
361:
362: } catch (Exception e) {
363: log.log(Level.SEVERE,
364: "WSS0354.error.initializing.encryptedType", e
365: .getMessage());
366: throw new XWSSecurityException(e);
367: }
368: }
369:
370: /**
371: * @throws XWSSecurityException
372: * If there is problem in initializing the EncryptedType
373: *
374: public void initializeEncryptedType(SOAPElement element)
375: throws XWSSecurityException {
376:
377: try {
378:
379: Iterator encryptionMethods =
380: getChildElements(
381: getSoapFactory().createName(
382: "EncryptionMethod",
383: MessageConstants.XENC_PREFIX,
384: MessageConstants.XENC_NS));
385: if (encryptionMethods.hasNext())
386: this.encryptionMethod = (SOAPElement) encryptionMethods.next();
387:
388: Iterator keyInfos =
389: getChildElements(
390: getSoapFactory().createName(
391: "KeyInfo",
392: MessageConstants.DSIG_PREFIX,
393: MessageConstants.DSIG_NS));
394: if (keyInfos.hasNext())
395: this.keyInfo =
396: new KeyInfoHeaderBlock((SOAPElement) keyInfos.next());
397:
398: Iterator cipherDatas =
399: getChildElements(
400: getSoapFactory().createName(
401: "CipherData",
402: MessageConstants.XENC_PREFIX,
403: MessageConstants.XENC_NS));
404: if (cipherDatas.hasNext())
405: this.cipherData = (SOAPElement) cipherDatas.next();
406: else {
407: log.log(Level.SEVERE, "WSS0347.missing.cipher.data");
408: throw new XWSSecurityException(
409: "CipherData is not present inside EncryptedType");
410: }
411:
412: Iterator allEncryptionProperties =
413: getChildElements(
414: getSoapFactory().createName(
415: "EncryptionProperties",
416: MessageConstants.XENC_PREFIX,
417: MessageConstants.XENC_NS));
418: if (allEncryptionProperties.hasNext())
419: this.encryptionProperties =
420: (SOAPElement) allEncryptionProperties.next();
421:
422: } catch (SOAPException e) {
423: log.log(Level.SEVERE, "WSS0354.error.initializing.encryptedType", e.getMessage());
424: throw new XWSSecurityException(e);
425: }
426: } */
427:
428: /**
429: * From XMLUtil.getFullTextChildrenFromElement() apache xml-secuirty
430: * 16may03
431: *
432: * @param element
433: */
434: private String getFullTextChildrenFromElement(Element element) {
435:
436: StringBuffer sb = new StringBuffer();
437: NodeList children = element.getChildNodes();
438: int iMax = children.getLength();
439:
440: for (int i = 0; i < iMax; i++) {
441: Node curr = children.item(i);
442:
443: if (curr.getNodeType() == Node.TEXT_NODE) {
444: sb.append(((Text) curr).getData());
445: }
446: }
447:
448: return sb.toString();
449: }
450: }
|