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: package com.sun.xml.ws.security.opt.impl.enc;
024:
025: import com.sun.xml.security.core.xenc.CVAdapter;
026: import com.sun.xml.ws.api.SOAPVersion;
027: import com.sun.xml.ws.security.opt.api.SecurityElement;
028: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
029: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
030: import com.sun.xml.ws.security.opt.impl.crypto.SSEData;
031: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
032: import com.sun.xml.ws.security.secext11.EncryptedHeaderType;
033: import com.sun.xml.ws.security.secext11.ObjectFactory;
034: import com.sun.xml.wss.impl.MessageConstants;
035: import com.sun.xml.wss.impl.c14n.AttributeNS;
036: import com.sun.xml.wss.impl.c14n.StAXEXC14nCanonicalizerImpl;
037: import com.sun.xml.wss.logging.LogDomainConstants;
038: import java.io.ByteArrayOutputStream;
039: import java.io.IOException;
040: import java.io.OutputStream;
041: import java.security.Key;
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Map;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.xml.bind.JAXBElement;
049: import javax.xml.bind.JAXBException;
050: import javax.xml.bind.Marshaller;
051: import javax.xml.crypto.Data;
052: import javax.xml.stream.XMLStreamException;
053: import javax.xml.stream.XMLStreamReader;
054: import javax.xml.stream.XMLStreamWriter;
055: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo;
056: import javax.crypto.Cipher;
057: import com.sun.xml.wss.logging.impl.opt.crypto.LogStringsMessages;
058:
059: /**
060: *
061: * @author Ashutosh.Shahi@sun.com
062: */
063: public class EncryptedHeader implements SecurityHeaderElement,
064: SecurityElementWriter {
065:
066: private static final Logger logger = Logger.getLogger(
067: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN,
068: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN_BUNDLE);
069:
070: private EncryptedHeaderType eht = null;
071: private boolean isCanonicalized = false;
072: //private ObjectFactory objFac = new ObjectFactory();
073: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
074: private Data data = null;
075: private Key key = null;
076: private CryptoProcessor dep = null;
077:
078: /** Creates a new instance of EncryptedHeader */
079: public EncryptedHeader(EncryptedHeaderType eht, Data data, Key key,
080: SOAPVersion soapVersion) {
081: this .eht = eht;
082: this .key = key;
083: this .data = data;
084: this .soapVersion = soapVersion;
085: }
086:
087: public boolean refersToSecHdrWithId(String id) {
088: KeyInfo ki = (KeyInfo) eht.getEncryptedData().getKeyInfo();
089: if (ki != null) {
090: List list = ki.getContent();
091: if (list.size() > 0) {
092: Object data = list.get(0);
093: if (data instanceof SecurityHeaderElement) {
094: return ((SecurityHeaderElement) data)
095: .refersToSecHdrWithId(id);
096: }
097: }
098: }
099: if (data instanceof SSEData) {
100: SecurityElement se = ((SSEData) data).getSecurityElement();
101: if (se instanceof SecurityHeaderElement) {
102: return ((SecurityHeaderElement) se)
103: .refersToSecHdrWithId(id);
104: }
105: }
106: return false;
107: }
108:
109: public String getId() {
110: return eht.getId();
111: }
112:
113: public void setId(String id) {
114: eht.setId(id);
115: }
116:
117: public String getNamespaceURI() {
118: return MessageConstants.WSSE11_NS;
119: }
120:
121: public String getLocalPart() {
122: return MessageConstants.ENCRYPTED_HEADER_LNAME;
123: }
124:
125: public XMLStreamReader readHeader() throws XMLStreamException {
126: throw new UnsupportedOperationException();
127: }
128:
129: public byte[] canonicalize(String algorithm,
130: List<AttributeNS> namespaceDecls) {
131: throw new UnsupportedOperationException();
132: }
133:
134: public boolean isCanonicalized() {
135: return isCanonicalized;
136: }
137:
138: public void writeTo(XMLStreamWriter streamWriter)
139: throws XMLStreamException {
140: try {
141: if (streamWriter instanceof Map && !(dep != null)) {
142: OutputStream os = (OutputStream) ((Map) streamWriter)
143: .get("sjsxp-outputstream");
144: if (os != null) {
145: streamWriter.writeCharacters(""); // Force completion of open elems
146: writeTo(os);
147: return;
148: }
149: }
150: Marshaller writer = getMarshaller();
151: if (dep == null) {
152: dep = new CryptoProcessor(Cipher.ENCRYPT_MODE, eht
153: .getEncryptedData().getEncryptionMethod()
154: .getAlgorithm(), data, key);
155:
156: if (streamWriter instanceof StAXEXC14nCanonicalizerImpl) {
157: ByteArrayOutputStream bos = new ByteArrayOutputStream();
158: try {
159: dep.encryptData(bos);
160:
161: } catch (IOException ie) {
162: logger
163: .log(
164: Level.SEVERE,
165: LogStringsMessages
166: .WSS_1920_ERROR_CALCULATING_CIPHERVALUE());
167: throw new XMLStreamException(
168: "Error occurred while calculating Cipher Value");
169: }
170: dep.setEncryptedDataCV(bos.toByteArray());
171: }
172: }
173: CVAdapter adapter = new CVAdapter(dep);
174: writer.setAdapter(CVAdapter.class, adapter);
175:
176: com.sun.xml.ws.security.secext11.ObjectFactory obj = new com.sun.xml.ws.security.secext11.ObjectFactory();
177: JAXBElement eh = obj.createEncryptedHeader(eht);
178: writer.marshal(eh, streamWriter);
179: } catch (javax.xml.bind.JAXBException ex) {
180: logger.log(Level.SEVERE, LogStringsMessages
181: .WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex
182: .getMessage()), ex);
183: } catch (com.sun.xml.wss.XWSSecurityException ex) {
184: logger.log(Level.SEVERE, LogStringsMessages
185: .WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex
186: .getMessage()), ex);
187: }
188: }
189:
190: public void writeTo(XMLStreamWriter streamWriter, HashMap props)
191: throws XMLStreamException {
192: try {
193: Marshaller marshaller = getMarshaller();
194: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
195: .iterator();
196: while (itr.hasNext()) {
197: Map.Entry<Object, Object> entry = itr.next();
198: marshaller.setProperty((String) entry.getKey(), entry
199: .getValue());
200: }
201: writeTo(streamWriter);
202: } catch (JAXBException jbe) {
203: logger.log(Level.SEVERE, LogStringsMessages
204: .WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(jbe
205: .getMessage()), jbe);
206: throw new XMLStreamException(jbe);
207: }
208: }
209:
210: public void writeTo(OutputStream os) {
211: try {
212: Marshaller writer = getMarshaller();
213: CryptoProcessor dep;
214:
215: dep = new CryptoProcessor(Cipher.ENCRYPT_MODE, eht
216: .getEncryptedData().getEncryptionMethod()
217: .getAlgorithm(), data, key);
218: CVAdapter adapter = new CVAdapter(dep);
219: writer.setAdapter(CVAdapter.class, adapter);
220: com.sun.xml.ws.security.secext11.ObjectFactory obj = new com.sun.xml.ws.security.secext11.ObjectFactory();
221: JAXBElement eh = obj.createEncryptedHeader(eht);
222: writer.marshal(eh, os);
223: } catch (com.sun.xml.wss.XWSSecurityException ex) {
224: logger.log(Level.SEVERE, LogStringsMessages
225: .WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex
226: .getMessage()), ex);
227: } catch (javax.xml.bind.JAXBException ex) {
228: logger.log(Level.SEVERE, LogStringsMessages
229: .WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex
230: .getMessage()), ex);
231: }
232: }
233:
234: private Marshaller getMarshaller() throws JAXBException {
235: return JAXBUtil.createMarshaller(soapVersion);
236: }
237:
238: }
|