001: /*
002: * JAXBEncryptedData.java
003: *
004: * Created on August 4, 2006, 2:56 PM
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.ws.security.opt.impl.enc;
028:
029: import com.sun.xml.security.core.xenc.CVAdapter;
030: import com.sun.xml.security.core.xenc.EncryptedDataType;
031: import com.sun.xml.ws.api.SOAPVersion;
032: import com.sun.xml.ws.security.opt.api.EncryptedData;
033: import com.sun.xml.ws.security.opt.api.SecurityElement;
034: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
035: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
036: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
037: import com.sun.xml.ws.security.opt.impl.crypto.SSEData;
038: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo;
039: import com.sun.xml.wss.impl.MessageConstants;
040: import com.sun.xml.wss.impl.c14n.AttributeNS;
041: import com.sun.xml.wss.impl.c14n.StAXEXC14nCanonicalizerImpl;
042: import com.sun.xml.wss.logging.LogDomainConstants;
043: import com.sun.xml.wss.logging.impl.opt.crypto.LogStringsMessages;
044: import java.io.ByteArrayOutputStream;
045: import java.io.IOException;
046: import java.io.OutputStream;
047: import java.security.Key;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.logging.Level;
053: import java.util.logging.Logger;
054: import javax.xml.bind.JAXBElement;
055: import javax.xml.bind.Marshaller;
056: import javax.xml.bind.JAXBException;
057: import org.xml.sax.ContentHandler;
058: import org.xml.sax.ErrorHandler;
059: import org.xml.sax.SAXException;
060: import javax.xml.crypto.Data;
061: import javax.crypto.Cipher;
062: import javax.xml.stream.XMLStreamException;
063:
064: /**
065: *
066: * @author K.Venugopal@sun.com
067: */
068:
069: public class JAXBEncryptedData implements EncryptedData,
070: SecurityHeaderElement, SecurityElementWriter {
071: private static final Logger logger = Logger.getLogger(
072: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN,
073: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN_BUNDLE);
074:
075: private EncryptedDataType edt = null;
076: private Data data = null;
077: private Key key = null;
078: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
079: private CryptoProcessor dep = null;
080:
081: /** Creates a new instance of JAXBEncryptedData */
082: public JAXBEncryptedData(EncryptedDataType edt, Data data, Key key,
083: SOAPVersion soapVersion) {
084: this .edt = edt;
085: this .key = key;
086: this .data = data;
087: this .soapVersion = soapVersion;
088: }
089:
090: public JAXBEncryptedData(EncryptedDataType edt, Data data,
091: SOAPVersion soapVersion) {
092: this .edt = edt;
093: this .data = data;
094: this .soapVersion = soapVersion;
095: }
096:
097: public String getEncryptedLocalName() {
098: if (data instanceof SSEData) {
099: SecurityElement se = ((SSEData) data).getSecurityElement();
100: return se.getLocalPart();
101: }
102: return "";
103: }
104:
105: public String getEncryptedId() {
106: if (data instanceof SSEData) {
107: SecurityElement se = ((SSEData) data).getSecurityElement();
108: return se.getId();
109: }
110: return "";
111: }
112:
113: public void encrypt() {
114: }
115:
116: public void decrypt() {
117: }
118:
119: public String getId() {
120: return edt.getId();
121: }
122:
123: public void setId(String id) {
124: if (edt.getId() == null || edt.getId().length() == 0) {
125: edt.setId(id);
126: }
127: }
128:
129: public String getNamespaceURI() {
130: return MessageConstants.XENC_NS;
131: }
132:
133: public String getLocalPart() {
134: return MessageConstants.ENCRYPTED_DATA_LNAME;
135: }
136:
137: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
138: throws javax.xml.stream.XMLStreamException {
139: try {
140:
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:
152: if (dep == null) {
153: dep = new CryptoProcessor(Cipher.ENCRYPT_MODE, edt
154: .getEncryptionMethod().getAlgorithm(), data,
155: key);
156:
157: if (streamWriter instanceof StAXEXC14nCanonicalizerImpl) {
158: ByteArrayOutputStream bos = new ByteArrayOutputStream();
159: try {
160: dep.encryptData(bos);
161:
162: } catch (IOException ie) {
163: logger
164: .log(
165: Level.SEVERE,
166: LogStringsMessages
167: .WSS_1920_ERROR_CALCULATING_CIPHERVALUE());
168: throw new XMLStreamException(
169: "Error occurred while calculating Cipher Value");
170: }
171: dep.setEncryptedDataCV(bos.toByteArray());
172: }
173: }
174: CVAdapter adapter = new CVAdapter(dep);
175: writer.setAdapter(CVAdapter.class, adapter);
176:
177: com.sun.xml.security.core.xenc.ObjectFactory obj = new com.sun.xml.security.core.xenc.ObjectFactory();
178: JAXBElement ed = obj.createEncryptedData(edt);
179: writer.marshal(ed, streamWriter);
180: } catch (com.sun.xml.wss.XWSSecurityException ex) {
181: logger.log(Level.SEVERE, LogStringsMessages
182: .WSS_1919_ERROR_WRITING_ENCRYPTEDDATA(ex
183: .getMessage()), ex);
184: } catch (javax.xml.bind.JAXBException ex) {
185: logger.log(Level.SEVERE, LogStringsMessages
186: .WSS_1919_ERROR_WRITING_ENCRYPTEDDATA(ex
187: .getMessage()), ex);
188: }
189: }
190:
191: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
192: HashMap props) throws XMLStreamException {
193: try {
194: Marshaller marshaller = getMarshaller();
195: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
196: .iterator();
197: while (itr.hasNext()) {
198: Map.Entry<Object, Object> entry = itr.next();
199: marshaller.setProperty((String) entry.getKey(), entry
200: .getValue());
201: }
202: writeTo(streamWriter);
203: } catch (JAXBException jbe) {
204: logger.log(Level.SEVERE, LogStringsMessages
205: .WSS_1919_ERROR_WRITING_ENCRYPTEDDATA(jbe
206: .getMessage()), jbe);
207: throw new XMLStreamException(jbe);
208: }
209: }
210:
211: public void writeTo(java.io.OutputStream os) {
212: try {
213: Marshaller writer = getMarshaller();
214: CryptoProcessor dep;
215:
216: dep = new CryptoProcessor(Cipher.ENCRYPT_MODE, edt
217: .getEncryptionMethod().getAlgorithm(), data, key);
218:
219: CVAdapter adapter = new CVAdapter(dep);
220: writer.setAdapter(CVAdapter.class, adapter);
221: com.sun.xml.security.core.xenc.ObjectFactory obj = new com.sun.xml.security.core.xenc.ObjectFactory();
222: JAXBElement ed = obj.createEncryptedData(edt);
223: writer.marshal(ed, os);
224: } catch (com.sun.xml.wss.XWSSecurityException ex) {
225: logger.log(Level.SEVERE, LogStringsMessages
226: .WSS_1919_ERROR_WRITING_ENCRYPTEDDATA(ex
227: .getMessage()), ex);
228: } catch (javax.xml.bind.JAXBException ex) {
229: logger.log(Level.SEVERE, LogStringsMessages
230: .WSS_1919_ERROR_WRITING_ENCRYPTEDDATA(ex
231: .getMessage()), ex);
232: }
233: }
234:
235: public void writeTo(javax.xml.soap.SOAPMessage saaj)
236: throws javax.xml.soap.SOAPException {
237: throw new UnsupportedOperationException();
238: }
239:
240: public void writeTo(ContentHandler contentHandler,
241: ErrorHandler errorHandler) throws SAXException {
242: throw new UnsupportedOperationException();
243: }
244:
245: public byte[] canonicalize(String algorithm,
246: List<AttributeNS> namespaceDecls) {
247: throw new UnsupportedOperationException();
248: }
249:
250: public boolean isCanonicalized() {
251: return false;
252: }
253:
254: private Marshaller getMarshaller() throws JAXBException {
255: return JAXBUtil.createMarshaller(soapVersion);
256: }
257:
258: public javax.xml.stream.XMLStreamReader readHeader()
259: throws javax.xml.stream.XMLStreamException {
260: throw new UnsupportedOperationException();
261: }
262:
263: public boolean refersToSecHdrWithId(String id) {
264: KeyInfo ki = (KeyInfo) this .edt.getKeyInfo();
265: if (ki != null) {
266: List list = ki.getContent();
267: if (list.size() > 0) {
268: Object data = ((JAXBElement) list.get(0)).getValue();
269: if (data instanceof SecurityHeaderElement) {
270: if (((SecurityHeaderElement) data)
271: .refersToSecHdrWithId(id)) {
272: return true;
273: }
274: }
275: }
276: }
277: if (data instanceof SSEData) {
278: SecurityElement se = ((SSEData) data).getSecurityElement();
279: if (se instanceof SecurityHeaderElement) {
280: return ((SecurityHeaderElement) se)
281: .refersToSecHdrWithId(id);
282: }
283: }
284: return false;
285: }
286:
287: }
|